84 lines
2.9 KiB
PHP
84 lines
2.9 KiB
PHP
<?php
|
|
|
|
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;
|
|
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;
|
|
|
|
$total_stock_value = Stock::whereHas('product', function ($q) use ($businessId) {
|
|
$q->where('business_id', $businessId);
|
|
})->sum(DB::raw('productPurchasePrice * productStock'));
|
|
|
|
$total_qty = Stock::whereHas('product', function ($q) use ($businessId) {
|
|
$q->where('business_id', $businessId);
|
|
})->sum('productStock');
|
|
|
|
$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());
|
|
|
|
if ($request->ajax()) {
|
|
return response()->json([
|
|
'data' => view('business::reports.stocks.datas', compact('stock_reports'))->render()
|
|
]);
|
|
}
|
|
|
|
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');
|
|
}
|
|
|
|
public function exportPdf()
|
|
{
|
|
$query = Product::with('stocks')
|
|
->where('business_id', auth()->user()->business_id)
|
|
->where('product_type', '!=', 'combo');
|
|
|
|
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();
|
|
}
|
|
|
|
return PdfService::render('business::reports.stocks.pdf', compact('stock_reports'), 'stock-report.pdf');
|
|
}
|
|
}
|