68 lines
2.3 KiB
PHP
68 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace Modules\Business\App\Exports;
|
|
|
|
use App\Models\Product;
|
|
use Illuminate\Contracts\View\View;
|
|
use Maatwebsite\Excel\Concerns\FromView;
|
|
|
|
class ExportCurrentStock implements FromView
|
|
{
|
|
public function view(): View
|
|
{
|
|
$businessId = auth()->user()->business_id;
|
|
$alert_qty_filter = request('alert_qty');
|
|
$search_term = request('search');
|
|
$per_page = request('per_page', 20);
|
|
|
|
// Base query
|
|
$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 <= 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 view('business::stocks.excel-csv', compact('products', 'total_stock_qty', 'total_stock_value'));
|
|
}
|
|
}
|