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;
|
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\ExportExpiredProduct;
|
|
|
|
|
|
|
|
|
|
class AcnooExpireProductController extends Controller
|
|
|
|
|
{
|
|
|
|
|
public function __construct()
|
|
|
|
|
{
|
|
|
|
|
$this->middleware('check.permission:expired-products.read')->only(['index']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function index(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$search = $request->input('search');
|
|
|
|
|
|
|
|
|
|
$expired_products = Product::with('stocks', 'unit:id,unitName', 'brand:id,brandName', 'category:id,categoryName')
|
|
|
|
|
->where('business_id', auth()->user()->business_id)
|
|
|
|
|
->withSum('stocks as total_stock', 'productStock')
|
|
|
|
|
->whereHas('stocks', function ($query) {
|
|
|
|
|
$query->whereDate('expire_date', '<', today())->where('productStock', '>', 0);
|
|
|
|
|
})
|
|
|
|
|
->when($search, function ($q) use ($search) {
|
|
|
|
|
$q->where(function ($q) use ($search) {
|
|
|
|
|
$q->where('productName', 'like', '%' . $search . '%')
|
|
|
|
|
->orWhere('productCode', 'like', '%' . $search . '%')
|
|
|
|
|
->orWhere('productPurchasePrice', 'like', '%' . $search . '%')
|
|
|
|
|
->orWhere('productSalePrice', 'like', '%' . $search . '%')
|
|
|
|
|
->orWhereHas('category', function ($q) use ($search) {
|
|
|
|
|
$q->where('categoryName', 'like', '%' . $search . '%');
|
|
|
|
|
})
|
|
|
|
|
->orWhereHas('brand', function ($q) use ($search) {
|
|
|
|
|
$q->where('brandName', 'like', '%' . $search . '%');
|
|
|
|
|
})
|
|
|
|
|
->orWhereHas('unit', function ($q) use ($search) {
|
|
|
|
|
$q->where('unitName', 'like', '%' . $search . '%');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
->latest()
|
|
|
|
|
->paginate($request->per_page ?? 20)->appends($request->query());
|
|
|
|
|
|
|
|
|
|
if ($request->ajax()) {
|
|
|
|
|
return response()->json([
|
|
|
|
|
'data' => view('business::expired-products.datas', compact('expired_products'))->render()
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return view('business::expired-products.index', compact('expired_products'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function exportExcel()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
return Excel::download(new ExportExpiredProduct, 'expired-products.xlsx');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function exportCsv()
|
|
|
|
|
{
|
|
|
|
|
return Excel::download(new ExportExpiredProduct, 'expired-products.csv');
|
|
|
|
|
}
|
2026-05-14 11:55:22 +07:00
|
|
|
|
|
|
|
|
public function exportPdf(Request $request)
|
|
|
|
|
{
|
|
|
|
|
$expired_products = Product::with('unit:id,unitName', 'brand:id,brandName', 'category:id,categoryName', 'stocks')
|
|
|
|
|
->where('business_id', auth()->user()->business_id)
|
|
|
|
|
->withSum('stocks as total_stock', 'productStock')
|
|
|
|
|
->whereHas('stocks', function ($query) {
|
|
|
|
|
$query->whereDate('expire_date', '<', today())
|
|
|
|
|
->where('productStock', '>', 0);
|
|
|
|
|
})
|
|
|
|
|
->when($request->search, function ($q) use ($request) {
|
|
|
|
|
$q->where(function ($q) use ($request) {
|
|
|
|
|
$q->where('type', 'like', '%' . $request->search . '%')
|
|
|
|
|
->orWhere('productName', 'like', '%' . $request->search . '%')
|
|
|
|
|
->orWhere('productCode', 'like', '%' . $request->search . '%')
|
|
|
|
|
->orWhere('hsn_code', 'like', '%' . $request->search . '%')
|
|
|
|
|
->orWhere('productSalePrice', 'like', '%' . $request->search . '%')
|
|
|
|
|
->orWhere('productPurchasePrice', 'like', '%' . $request->search . '%')
|
|
|
|
|
->orWhereHas('unit', function ($q) use ($request) {
|
|
|
|
|
$q->where('unitName', 'like', '%' . $request->search . '%');
|
|
|
|
|
})
|
|
|
|
|
->orWhereHas('brand', function ($q) use ($request) {
|
|
|
|
|
$q->where('brandName', 'like', '%' . $request->search . '%');
|
|
|
|
|
})
|
|
|
|
|
->orWhereHas('category', function ($q) use ($request) {
|
|
|
|
|
$q->where('categoryName', 'like', '%' . $request->search . '%');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
->latest()
|
|
|
|
|
->limit($request->per_page ?? 20)->get();
|
|
|
|
|
|
|
|
|
|
return PdfService::render('business::expired-products.pdf', compact('expired_products'),'expired-products.pdf');
|
|
|
|
|
}
|
2026-03-15 17:08:23 +07:00
|
|
|
}
|