Files
kulakpos_web/Modules/Business/App/Exports/ExportCurrentStock.php
eko54r 05fd3230b8
All checks were successful
Build, Push and Deploy / build-and-push (push) Successful in 5m14s
Gitea Actions Demo / Explore-Gitea-Actions (push) Successful in 17s
Build, Push and Deploy / deploy-staging (push) Successful in 41s
Build, Push and Deploy / deploy-production (push) Has been skipped
update marketing
2026-05-14 11:55:22 +07:00

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'));
}
}