update marketing
All checks were successful
All checks were successful
This commit is contained in:
@@ -2,10 +2,8 @@
|
||||
|
||||
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;
|
||||
@@ -22,31 +20,42 @@ public function index(Request $request)
|
||||
{
|
||||
$businessId = auth()->user()->business_id;
|
||||
|
||||
$total_stock_value = Stock::whereHas('product', function ($q) use ($businessId) {
|
||||
$q->where('business_id', $businessId);
|
||||
})->sum(DB::raw('"productPurchasePrice" * "productStock"'));
|
||||
$productsQuery = Product::with(['stocks' => function ($q) {
|
||||
$q->select('id', 'product_id', 'productStock', 'productPurchasePrice', 'productSalePrice');
|
||||
}])
|
||||
->where('business_id', $businessId)
|
||||
->where('product_type', '!=', 'combo');
|
||||
|
||||
$total_qty = Stock::whereHas('product', function ($q) use ($businessId) {
|
||||
$q->where('business_id', $businessId);
|
||||
})->sum('productStock');
|
||||
if ($request->search) {
|
||||
$productsQuery->where(function ($q) use ($request) {
|
||||
$q->where('productName', 'like', '%' . $request->search . '%')
|
||||
->orWhereHas('stocks', function ($stock) use ($request) {
|
||||
$stock->where('productSalePrice', 'like', '%' . $request->search . '%')
|
||||
->orWhere('productPurchasePrice', 'like', '%' . $request->search . '%');
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
$stock_reports = Product::with('stocks')
|
||||
->where('business_id', auth()->user()->business_id)
|
||||
->where('product_type', '!=', 'combo')
|
||||
->when($request->search, function ($query) use ($request) {
|
||||
$query->where(function ($q) use ($request) {
|
||||
$q->where('productName', 'like', '%' . $request->search . '%')
|
||||
->orwhere('productSalePrice', 'like', '%' . $request->search . '%')
|
||||
->orwhere('productPurchasePrice', 'like', '%' . $request->search . '%');
|
||||
});
|
||||
})
|
||||
->withSum('stocks', 'productStock')
|
||||
$stock_reports = $productsQuery
|
||||
->latest()
|
||||
->paginate($request->per_page ?? 20)->appends($request->query());
|
||||
->paginate($request->per_page ?? 20)
|
||||
->appends($request->query());
|
||||
|
||||
$total_qty = $stock_reports->getCollection()->sum(function ($product) {
|
||||
return $product->stocks->sum('productStock');
|
||||
});
|
||||
|
||||
$total_stock_value = $stock_reports->getCollection()->sum(function ($product) {
|
||||
return $product->stocks->sum(function ($stock) {
|
||||
return $stock->productStock * $stock->productPurchasePrice;
|
||||
});
|
||||
});
|
||||
|
||||
if ($request->ajax()) {
|
||||
return response()->json([
|
||||
'data' => view('business::reports.stocks.datas', compact('stock_reports'))->render()
|
||||
'data' => view('business::reports.stocks.datas', compact('stock_reports'))->render(),
|
||||
'total_stock_value' => currency_format($total_stock_value, currency: business_currency()),
|
||||
'total_qty' => $total_qty
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -63,21 +72,29 @@ public function exportCsv()
|
||||
return Excel::download(new ExportCurrentStockReport, 'current-stock-report.csv');
|
||||
}
|
||||
|
||||
public function exportPdf()
|
||||
public function exportPdf(Request $request)
|
||||
{
|
||||
$query = Product::with('stocks')
|
||||
$productsQuery = Product::with(['stocks' => function ($q) {
|
||||
$q->select('id', 'product_id', 'productStock', 'productPurchasePrice', 'productSalePrice');
|
||||
}])
|
||||
->where('business_id', auth()->user()->business_id)
|
||||
->where('product_type', '!=', 'combo');
|
||||
|
||||
if (request('alert_qty')) {
|
||||
$stock_reports = $query->get()->filter(function ($product) {
|
||||
$totalStock = $product->stocks->sum('productStock');
|
||||
return $totalStock <= $product->alert_qty;
|
||||
if ($request->search) {
|
||||
$productsQuery->where(function ($q) use ($request) {
|
||||
$q->where('productName', 'like', '%' . $request->search . '%')
|
||||
->orWhereHas('stocks', function ($stock) use ($request) {
|
||||
$stock->where('productSalePrice', 'like', '%' . $request->search . '%')
|
||||
->orWhere('productPurchasePrice', 'like', '%' . $request->search . '%');
|
||||
});
|
||||
});
|
||||
} else {
|
||||
$stock_reports = $query->latest()->get();
|
||||
}
|
||||
|
||||
$stock_reports = $productsQuery
|
||||
->latest()
|
||||
->limit($request->per_page ?? 20)
|
||||
->get();
|
||||
|
||||
return PdfService::render('business::reports.stocks.pdf', compact('stock_reports'), 'stock-report.pdf');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user