106 lines
3.9 KiB
PHP
106 lines
3.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 Maatwebsite\Excel\Facades\Excel;
|
||
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
||
|
|
use Modules\Business\App\Exports\ExportCurrentStock;
|
||
|
|
|
||
|
|
class AcnooStockController extends Controller
|
||
|
|
{
|
||
|
|
public function __construct()
|
||
|
|
{
|
||
|
|
$this->middleware('check.permission:stocks.read')->only(['index']);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function index()
|
||
|
|
{
|
||
|
|
$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','warehouse:id,name', 'rack:id,name', 'shelf:id,name')->where('product_type', '!=', 'combo')->where('business_id', $businessId);
|
||
|
|
|
||
|
|
// For Low Stock view - apply search if search term exists
|
||
|
|
if ($alert_qty_filter) {
|
||
|
|
// Apply search filter if search term exists
|
||
|
|
if ($search_term) {
|
||
|
|
$query->where(function ($q) use ($search_term) {
|
||
|
|
$q->where('productName', 'like', '%' . $search_term . '%')
|
||
|
|
->orWhere('productPurchasePrice', 'like', '%' . $search_term . '%')
|
||
|
|
->orWhere('productSalePrice', 'like', '%' . $search_term . '%');
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
// Get all products (with search applied if any) then filter for low stock
|
||
|
|
$allProducts = $query->latest()->get();
|
||
|
|
$filteredProducts = $allProducts->filter(function ($product) {
|
||
|
|
$totalStock = $product->stocks->sum('productStock');
|
||
|
|
return $totalStock <= $product->alert_qty;
|
||
|
|
});
|
||
|
|
|
||
|
|
// Manual pagination for filtered collection
|
||
|
|
$currentPage = request('page', 1);
|
||
|
|
$products = new LengthAwarePaginator(
|
||
|
|
$filteredProducts->forPage($currentPage, $per_page),
|
||
|
|
$filteredProducts->count(),
|
||
|
|
$per_page,
|
||
|
|
$currentPage,
|
||
|
|
['path' => request()->url(), 'query' => request()->query()]
|
||
|
|
);
|
||
|
|
|
||
|
|
// Calculate totals for low stock view
|
||
|
|
$total_stock_value = $filteredProducts->sum(function ($product) {
|
||
|
|
return $product->stocks->sum(function ($stock) {
|
||
|
|
return $stock->productStock * $stock->productPurchasePrice;
|
||
|
|
});
|
||
|
|
});
|
||
|
|
$total_qty = $filteredProducts->sum(function ($product) {
|
||
|
|
return $product->stocks->sum('productStock');
|
||
|
|
});
|
||
|
|
} else {
|
||
|
|
// For All Stock view - NO search, just regular pagination
|
||
|
|
$products = $query->latest()->paginate($per_page)->appends(request()->query());
|
||
|
|
|
||
|
|
// Calculate totals for all stock
|
||
|
|
$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');
|
||
|
|
}
|
||
|
|
|
||
|
|
// Handle AJAX request
|
||
|
|
if (request()->ajax()) {
|
||
|
|
return response()->json([
|
||
|
|
'data' => view('business::stocks.datas', compact('products', 'total_stock_value', 'total_qty'))->render()
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
return view('business::stocks.index', [
|
||
|
|
'products' => $products,
|
||
|
|
'total_stock_value' => $total_stock_value,
|
||
|
|
'total_qty' => $total_qty,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function exportExcel()
|
||
|
|
{
|
||
|
|
return Excel::download(new ExportCurrentStock, 'current-stock.xlsx');
|
||
|
|
}
|
||
|
|
|
||
|
|
public function exportCsv()
|
||
|
|
{
|
||
|
|
return Excel::download(new ExportCurrentStock, 'current-stock.csv');
|
||
|
|
}
|
||
|
|
}
|