2026-03-15 17:08:23 +07:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Modules\Business\App\Http\Controllers;
|
|
|
|
|
|
|
|
|
|
use App\Models\Product;
|
|
|
|
|
use App\Http\Controllers\Controller;
|
2026-05-14 11:55:22 +07:00
|
|
|
use App\Services\PdfService;
|
2026-03-15 17:08:23 +07:00
|
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
|
|
|
use Modules\Business\App\Exports\ExportCurrentStock;
|
|
|
|
|
|
|
|
|
|
class AcnooStockController extends Controller
|
|
|
|
|
{
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this->middleware('check.permission:stocks.read')->only(['index']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function index()
|
|
|
|
|
{
|
|
|
|
|
$businessId = auth()->user()->business_id;
|
|
|
|
|
$alert_qty_filter = request('alert_qty');
|
|
|
|
|
$search_term = request('search');
|
|
|
|
|
$per_page = request('per_page', 20);
|
|
|
|
|
|
2026-05-14 11:55:22 +07:00
|
|
|
$query = Product::with('stocks', 'category:id,categoryName')
|
|
|
|
|
->where('product_type', '!=', 'combo')
|
|
|
|
|
->where('business_id', $businessId);
|
|
|
|
|
|
|
|
|
|
if ($search_term) {
|
|
|
|
|
$query->where(function ($q) use ($search_term) {
|
|
|
|
|
$q->where('productName', 'like', '%' . $search_term . '%')
|
|
|
|
|
->orWhere('productCode', 'like', '%' . $search_term . '%')
|
|
|
|
|
->orWhere('hsn_code', 'like', '%' . $search_term . '%')
|
|
|
|
|
->orWhereHas('category', function ($q) use ($search_term) {
|
|
|
|
|
$q->where('categoryName', 'like', '%' . $search_term . '%');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
2026-03-15 17:08:23 +07:00
|
|
|
|
|
|
|
|
if ($alert_qty_filter) {
|
2026-05-14 11:55:22 +07:00
|
|
|
$products = $query
|
|
|
|
|
->withSum('stocks as total_stock', 'productStock')
|
|
|
|
|
->whereRaw('(SELECT COALESCE(SUM("productStock"),0) FROM stocks WHERE stocks.product_id = products.id) <= products.alert_qty')
|
|
|
|
|
->latest()
|
|
|
|
|
->paginate($per_page)
|
|
|
|
|
->appends(request()->query());
|
2026-03-15 17:08:23 +07:00
|
|
|
|
2026-05-14 11:55:22 +07:00
|
|
|
$total_stock_qty = $products->sum('total_stock');
|
2026-03-15 17:08:23 +07:00
|
|
|
|
2026-05-14 11:55:22 +07:00
|
|
|
$total_stock_value = $products->sum(function ($product) {
|
2026-03-15 17:08:23 +07:00
|
|
|
return $product->stocks->sum(function ($stock) {
|
|
|
|
|
return $stock->productStock * $stock->productPurchasePrice;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2026-05-14 11:55:22 +07:00
|
|
|
$products = $query
|
|
|
|
|
->withSum('stocks as total_stock', 'productStock')
|
|
|
|
|
->latest()
|
|
|
|
|
->paginate($per_page)
|
|
|
|
|
->appends(request()->query());
|
2026-03-15 17:08:23 +07:00
|
|
|
|
2026-05-14 11:55:22 +07:00
|
|
|
$total_stock_qty = $products->sum('total_stock');
|
2026-03-15 17:08:23 +07:00
|
|
|
|
2026-05-14 11:55:22 +07:00
|
|
|
$total_stock_value = $products->sum(function ($product) {
|
|
|
|
|
return $product->stocks->sum(function ($stock) {
|
|
|
|
|
return $stock->productStock * $stock->productPurchasePrice;
|
|
|
|
|
});
|
|
|
|
|
});
|
2026-03-15 17:08:23 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (request()->ajax()) {
|
|
|
|
|
return response()->json([
|
2026-05-14 11:55:22 +07:00
|
|
|
'data' => view('business::stocks.datas', compact('products', 'total_stock_value', 'total_stock_qty'))->render(),
|
|
|
|
|
'total_stock_qty' => $total_stock_qty,
|
|
|
|
|
'total_stock_value' => currency_format($total_stock_value, currency: business_currency())
|
2026-03-15 17:08:23 +07:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return view('business::stocks.index', [
|
|
|
|
|
'products' => $products,
|
|
|
|
|
'total_stock_value' => $total_stock_value,
|
2026-05-14 11:55:22 +07:00
|
|
|
'total_stock_qty' => $total_stock_qty,
|
2026-03-15 17:08:23 +07:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function exportExcel()
|
|
|
|
|
{
|
2026-05-14 11:55:22 +07:00
|
|
|
return Excel::download(new ExportCurrentStock, 'stock-list.xlsx');
|
2026-03-15 17:08:23 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function exportCsv()
|
|
|
|
|
{
|
2026-05-14 11:55:22 +07:00
|
|
|
return Excel::download(new ExportCurrentStock, 'stock-list.csv');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function exportPdf()
|
|
|
|
|
{
|
|
|
|
|
$businessId = auth()->user()->business_id;
|
|
|
|
|
$alert_qty_filter = request('alert_qty');
|
|
|
|
|
$search_term = request('search');
|
|
|
|
|
$per_page = request('per_page', 20);
|
|
|
|
|
|
|
|
|
|
$query = Product::with('stocks', 'category:id,categoryName')
|
|
|
|
|
->where('product_type', '!=', 'combo')
|
|
|
|
|
->where('business_id', $businessId);
|
|
|
|
|
|
|
|
|
|
if ($search_term) {
|
|
|
|
|
$query->where(function ($q) use ($search_term) {
|
|
|
|
|
$q->where('productName', 'like', '%' . $search_term . '%')
|
|
|
|
|
->orWhere('productCode', 'like', '%' . $search_term . '%')
|
|
|
|
|
->orWhere('hsn_code', 'like', '%' . $search_term . '%')
|
|
|
|
|
->orWhereHas('category', function ($q) use ($search_term) {
|
|
|
|
|
$q->where('categoryName', 'like', '%' . $search_term . '%');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($alert_qty_filter) {
|
|
|
|
|
$products = $query
|
|
|
|
|
->withSum('stocks as total_stock', 'productStock')
|
|
|
|
|
->havingRaw('total_stock <= products.alert_qty')
|
|
|
|
|
->latest()
|
|
|
|
|
->limit($per_page)
|
|
|
|
|
->get();
|
|
|
|
|
|
|
|
|
|
$total_stock_qty = $products->sum('total_stock');
|
|
|
|
|
|
|
|
|
|
$total_stock_value = $products->sum(function ($product) {
|
|
|
|
|
return $product->stocks->sum(function ($stock) {
|
|
|
|
|
return $stock->productStock * $stock->productPurchasePrice;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
$products = $query
|
|
|
|
|
->withSum('stocks as total_stock', 'productStock')
|
|
|
|
|
->latest()
|
|
|
|
|
->limit($per_page)
|
|
|
|
|
->get();
|
|
|
|
|
|
|
|
|
|
$total_stock_qty = $products->sum('total_stock');
|
|
|
|
|
|
|
|
|
|
$total_stock_value = $products->sum(function ($product) {
|
|
|
|
|
return $product->stocks->sum(function ($stock) {
|
|
|
|
|
return $stock->productStock * $stock->productPurchasePrice;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return PdfService::render('business::stocks.pdf', compact('products', 'total_stock_qty', 'total_stock_value'), 'stock-list.pdf');
|
2026-03-15 17:08:23 +07:00
|
|
|
}
|
|
|
|
|
}
|