user()->business_id)->whereNotNull('party_id')->get(); $totalAmount = $sale->sum('totalAmount'); $totalProfit = $sale->where('lossProfit', '>', 0)->sum('lossProfit') ?? 0; $totalLoss = $sale->where('lossProfit', '<', 0)->sum('lossProfit') ?? 0; $parties = Party::with('sales') ->where('business_id', auth()->user()->business_id) ->where('type', '!=', 'Supplier') ->when($request->search, function ($query) use ($request) { $query->where(function ($q) use ($request) { $q->where('name', 'like', '%' . $request->search . '%'); }); }) ->latest() ->paginate($request->per_page ?? 20) ->appends($request->query()); if ($request->ajax()) { return response()->json([ 'data' => view('business::party-reports.loss-profit.datas', compact('parties'))->render() ]); } return view('business::party-reports.loss-profit.index', compact('parties', 'totalAmount', 'totalProfit', 'totalLoss')); } public function exportExcel() { return Excel::download(new ExportPartyLossProfit, 'party-loss-profit.xlsx'); } public function exportCsv() { return Excel::download(new ExportPartyLossProfit, 'party-loss-profit.csv'); } public function exportPdf() { $parties = Party::with('sales') ->where('business_id', auth()->user()->business_id) ->where('type', '!=', 'Supplier') ->latest() ->get(); return PdfService::render('business::party-reports.loss-profit.pdf', compact('parties'),'party-loss-profit-report.pdf'); } public function view($id) { $party = Party::with('sales.details', 'sales.details.product') ->where('id', $id) ->where('business_id', auth()->user()->business_id) ->firstOrFail(); return response()->json([ 'sales' => $party->sales ]); } }