middleware('check.permission:transaction-history-reports.read')->only(['index']); } public function index(Request $request) { $businessId = auth()->user()->business_id; $today = Carbon::today()->format('Y-m-d'); $total_due = DueCollect::where('business_id', $businessId) ->whereDate('paymentDate', $today) ->sum('totalDue'); $total_paid = DueCollect::where('business_id', $businessId) ->whereDate('paymentDate', $today) ->sum('payDueAmount'); $transactionsQuery = DueCollect::with('party:id,name,type', 'payment_type:id,name', 'transactions')->where('business_id', $businessId); // Date Filter $duration = $request->custom_days ?: 'today'; [$filter_from_date, $filter_to_date] = $this->applyDateRange($duration, $request->from_date, $request->to_date); $this->applyDateFilter($transactionsQuery, $duration, 'paymentDate', $request->from_date, $request->to_date); // Search Filter if ($request->filled('search')) { $transactionsQuery->where(function ($query) use ($request) { $query->where('paymentType', 'like', '%' . $request->search . '%') ->orWhere('totalDue', 'like', '%' . $request->search . '%') ->orWhere('invoiceNumber', 'like', '%' . $request->search . '%') ->orWhere('payDueAmount', 'like', '%' . $request->search . '%') ->orWhereHas('party', function ($q) use ($request) { $q->where('name', 'like', '%' . $request->search . '%'); }) ->orWhereHas('payment_type', function ($q) use ($request) { $q->where('name', 'like', '%' . $request->search . '%'); }); }); } if ($request->filled('type')) { $transactionsQuery->whereHas('party', function ($q) use ($request) { $q->where(function ($q) use ($request) { $q->where('type', $request->type); }); }); } $perPage = $request->input('per_page', 20); $transactions = $transactionsQuery->latest()->paginate($perPage)->appends($request->query()); $total_due = $transactionsQuery->sum('totalDue'); $total_paid = $transactionsQuery->sum('payDueAmount'); if ($request->ajax()) { return response()->json([ 'data' => view('business::reports.transaction-history.datas', compact('transactions','total_due', 'total_paid', 'filter_from_date', 'filter_to_date', 'duration'))->render(), 'total_due' => currency_format($total_due, currency: business_currency()), 'total_paid' => currency_format($total_paid, currency: business_currency()), ]); } return view('business::reports.transaction-history.transaction-reports', compact('transactions', 'total_due', 'total_paid', 'filter_from_date', 'filter_to_date', 'duration')); } public function exportExcel() { return Excel::download(new ExportTransaction, 'transaction-history.xlsx'); } public function exportCsv() { return Excel::download(new ExportTransaction, 'transaction-history.csv'); } public function exportPdf() { $transactions = DueCollect::with('party:id,name,type', 'payment_type:id,name', 'transactions')->where('business_id', auth()->user()->business_id) ->latest() ->get(); return PdfService::render('business::reports.transaction-history.pdf', compact('transactions'),'due-transactions-report.pdf'); } }