finishing to dev
All checks were successful
Build, Push and Deploy / build-and-push (push) Successful in 3m41s
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 16s
Build, Push and Deploy / deploy-staging (push) Successful in 42s
Build, Push and Deploy / deploy-production (push) Has been skipped

This commit is contained in:
2026-05-14 22:50:44 +07:00
parent 2fabdf8fc9
commit 8307b9e66d
212 changed files with 2451 additions and 4493 deletions

View File

@@ -2,8 +2,10 @@
namespace Modules\Business\App\Http\Controllers;
use App\Models\Stock;
use App\Models\Product;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;
use App\Services\PdfService;
use Maatwebsite\Excel\Facades\Excel;
@@ -20,42 +22,31 @@ public function index(Request $request)
{
$businessId = auth()->user()->business_id;
$productsQuery = Product::with(['stocks' => function ($q) {
$q->select('id', 'product_id', 'productStock', 'productPurchasePrice', 'productSalePrice');
}])
->where('business_id', $businessId)
->where('product_type', '!=', 'combo');
$total_stock_value = Stock::whereHas('product', function ($q) use ($businessId) {
$q->where('business_id', $businessId);
})->sum(DB::raw('"productPurchasePrice" * "productStock"'));
if ($request->search) {
$productsQuery->where(function ($q) use ($request) {
$q->where('productName', 'like', '%' . $request->search . '%')
->orWhereHas('stocks', function ($stock) use ($request) {
$stock->where('productSalePrice', 'like', '%' . $request->search . '%')
->orWhere('productPurchasePrice', 'like', '%' . $request->search . '%');
});
});
}
$total_qty = Stock::whereHas('product', function ($q) use ($businessId) {
$q->where('business_id', $businessId);
})->sum('productStock');
$stock_reports = $productsQuery
$stock_reports = Product::with('stocks')
->where('business_id', auth()->user()->business_id)
->where('product_type', '!=', 'combo')
->when($request->search, function ($query) use ($request) {
$query->where(function ($q) use ($request) {
$q->where('productName', 'like', '%' . $request->search . '%')
->orwhere('productSalePrice', 'like', '%' . $request->search . '%')
->orwhere('productPurchasePrice', 'like', '%' . $request->search . '%');
});
})
->withSum('stocks', 'productStock')
->latest()
->paginate($request->per_page ?? 20)
->appends($request->query());
$total_qty = $stock_reports->getCollection()->sum(function ($product) {
return $product->stocks->sum('productStock');
});
$total_stock_value = $stock_reports->getCollection()->sum(function ($product) {
return $product->stocks->sum(function ($stock) {
return $stock->productStock * $stock->productPurchasePrice;
});
});
->paginate($request->per_page ?? 20)->appends($request->query());
if ($request->ajax()) {
return response()->json([
'data' => view('business::reports.stocks.datas', compact('stock_reports'))->render(),
'total_stock_value' => currency_format($total_stock_value, currency: business_currency()),
'total_qty' => $total_qty
'data' => view('business::reports.stocks.datas', compact('stock_reports'))->render()
]);
}
@@ -72,29 +63,21 @@ public function exportCsv()
return Excel::download(new ExportCurrentStockReport, 'current-stock-report.csv');
}
public function exportPdf(Request $request)
public function exportPdf()
{
$productsQuery = Product::with(['stocks' => function ($q) {
$q->select('id', 'product_id', 'productStock', 'productPurchasePrice', 'productSalePrice');
}])
$query = Product::with('stocks')
->where('business_id', auth()->user()->business_id)
->where('product_type', '!=', 'combo');
if ($request->search) {
$productsQuery->where(function ($q) use ($request) {
$q->where('productName', 'like', '%' . $request->search . '%')
->orWhereHas('stocks', function ($stock) use ($request) {
$stock->where('productSalePrice', 'like', '%' . $request->search . '%')
->orWhere('productPurchasePrice', 'like', '%' . $request->search . '%');
});
if (request('alert_qty')) {
$stock_reports = $query->get()->filter(function ($product) {
$totalStock = $product->stocks->sum('productStock');
return $totalStock <= $product->alert_qty;
});
} else {
$stock_reports = $query->latest()->get();
}
$stock_reports = $productsQuery
->latest()
->limit($request->per_page ?? 20)
->get();
return PdfService::render('business::reports.stocks.pdf', compact('stock_reports'), 'stock-report.pdf');
}
}