update marketing
All checks were successful
All checks were successful
This commit is contained in:
@@ -2,13 +2,10 @@
|
||||
|
||||
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 Illuminate\Pagination\LengthAwarePaginator;
|
||||
use Modules\Business\App\Exports\ExportCurrentStock;
|
||||
|
||||
class AcnooStockController extends Controller
|
||||
@@ -25,81 +22,130 @@ public function index()
|
||||
$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);
|
||||
$query = Product::with('stocks', 'category:id,categoryName')
|
||||
->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;
|
||||
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 . '%');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// 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()]
|
||||
);
|
||||
if ($alert_qty_filter) {
|
||||
$products = $query
|
||||
->withSum('stocks as total_stock', 'productStock')
|
||||
->whereRaw('(SELECT COALESCE(SUM("productStock"),0) FROM stocks WHERE stocks.product_id = products.id) <= products.alert_qty')
|
||||
->latest()
|
||||
->paginate($per_page)
|
||||
->appends(request()->query());
|
||||
|
||||
// Calculate totals for low stock view
|
||||
$total_stock_value = $filteredProducts->sum(function ($product) {
|
||||
$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;
|
||||
});
|
||||
});
|
||||
$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());
|
||||
$products = $query
|
||||
->withSum('stocks as total_stock', 'productStock')
|
||||
->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_stock_qty = $products->sum('total_stock');
|
||||
|
||||
$total_qty = Stock::whereHas('product', function ($q) use ($businessId) {
|
||||
$q->where('business_id', $businessId);
|
||||
})->sum('productStock');
|
||||
$total_stock_value = $products->sum(function ($product) {
|
||||
return $product->stocks->sum(function ($stock) {
|
||||
return $stock->productStock * $stock->productPurchasePrice;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Handle AJAX request
|
||||
if (request()->ajax()) {
|
||||
return response()->json([
|
||||
'data' => view('business::stocks.datas', compact('products', 'total_stock_value', 'total_qty'))->render()
|
||||
'data' => view('business::stocks.datas', compact('products', 'total_stock_value', 'total_stock_qty'))->render(),
|
||||
'total_stock_qty' => $total_stock_qty,
|
||||
'total_stock_value' => currency_format($total_stock_value, currency: business_currency())
|
||||
]);
|
||||
}
|
||||
|
||||
return view('business::stocks.index', [
|
||||
'products' => $products,
|
||||
'total_stock_value' => $total_stock_value,
|
||||
'total_qty' => $total_qty,
|
||||
'total_stock_qty' => $total_stock_qty,
|
||||
]);
|
||||
}
|
||||
|
||||
public function exportExcel()
|
||||
{
|
||||
return Excel::download(new ExportCurrentStock, 'current-stock.xlsx');
|
||||
return Excel::download(new ExportCurrentStock, 'stock-list.xlsx');
|
||||
}
|
||||
|
||||
public function exportCsv()
|
||||
{
|
||||
return Excel::download(new ExportCurrentStock, 'current-stock.csv');
|
||||
return Excel::download(new ExportCurrentStock, 'stock-list.csv');
|
||||
}
|
||||
|
||||
public function exportPdf()
|
||||
{
|
||||
$businessId = auth()->user()->business_id;
|
||||
$alert_qty_filter = request('alert_qty');
|
||||
$search_term = request('search');
|
||||
$per_page = request('per_page', 20);
|
||||
|
||||
$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 <= products.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 PdfService::render('business::stocks.pdf', compact('products', 'total_stock_qty', 'total_stock_value'), 'stock-list.pdf');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user