Files
kulakpos_web/Modules/Business/App/Http/Controllers/AcnooStockReportController.php

101 lines
3.7 KiB
PHP
Raw Normal View History

2026-03-15 17:08:23 +07:00
<?php
namespace Modules\Business\App\Http\Controllers;
use App\Models\Product;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Services\PdfService;
use Maatwebsite\Excel\Facades\Excel;
use Modules\Business\App\Exports\ExportCurrentStockReport;
class AcnooStockReportController extends Controller
{
public function __construct()
{
$this->middleware('check.permission:stock-reports.read')->only(['index']);
}
public function index(Request $request)
{
$businessId = auth()->user()->business_id;
2026-05-14 11:55:22 +07:00
$productsQuery = Product::with(['stocks' => function ($q) {
$q->select('id', 'product_id', 'productStock', 'productPurchasePrice', 'productSalePrice');
}])
->where('business_id', $businessId)
->where('product_type', '!=', 'combo');
2026-03-15 17:08:23 +07:00
2026-05-14 11:55:22 +07:00
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 . '%');
});
});
}
2026-03-15 17:08:23 +07:00
2026-05-14 11:55:22 +07:00
$stock_reports = $productsQuery
2026-03-15 17:08:23 +07:00
->latest()
2026-05-14 11:55:22 +07:00
->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;
});
});
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::reports.stocks.datas', compact('stock_reports'))->render(),
'total_stock_value' => currency_format($total_stock_value, currency: business_currency()),
'total_qty' => $total_qty
2026-03-15 17:08:23 +07:00
]);
}
return view('business::reports.stocks.stock-reports', compact('stock_reports', 'total_stock_value', 'total_qty'));
}
public function exportExcel()
{
return Excel::download(new ExportCurrentStockReport, 'current-stock-report.xlsx');
}
public function exportCsv()
{
return Excel::download(new ExportCurrentStockReport, 'current-stock-report.csv');
}
2026-05-14 11:55:22 +07:00
public function exportPdf(Request $request)
2026-03-15 17:08:23 +07:00
{
2026-05-14 11:55:22 +07:00
$productsQuery = Product::with(['stocks' => function ($q) {
$q->select('id', 'product_id', 'productStock', 'productPurchasePrice', 'productSalePrice');
}])
2026-03-15 17:08:23 +07:00
->where('business_id', auth()->user()->business_id)
->where('product_type', '!=', 'combo');
2026-05-14 11:55:22 +07:00
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 . '%');
});
2026-03-15 17:08:23 +07:00
});
}
2026-05-14 11:55:22 +07:00
$stock_reports = $productsQuery
->latest()
->limit($request->per_page ?? 20)
->get();
2026-03-15 17:08:23 +07:00
return PdfService::render('business::reports.stocks.pdf', compact('stock_reports'), 'stock-report.pdf');
}
}