ReportMutationController.php 1.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Exports\MutationExport;
  4. use App\Models\Mutation;
  5. use App\Services\MutationService;
  6. use Inertia\Inertia;
  7. class ReportMutationController extends Controller
  8. {
  9. /**
  10. * Display a listing of the resource.
  11. *
  12. * @return \Inertia\Response
  13. */
  14. public function index()
  15. {
  16. $mutations = Mutation::filter(request()->only('startDate', 'endDate'));
  17. return inertia('mutation/Report', [
  18. 'filters' => request()->all('startDate', 'endDate', 'outlet'),
  19. 'mutations' => Inertia::lazy(
  20. fn() => [
  21. 'totalIncome' => (new MutationService)->totalIncomeAsString($mutations->get()),
  22. 'totalExpense' => (new MutationService)->totalExpenseAsString($mutations->get()),
  23. 'totalAmount' => (new MutationService)->totalAmountAsString($mutations->get()),
  24. 'details' => $mutations
  25. ->latest()
  26. ->paginate(10)
  27. ->withQueryString()
  28. ->through(fn($mutation) => [
  29. 'createdAt' => $mutation->created_at,
  30. 'amount' => $mutation->amount,
  31. 'type' => $mutation->type,
  32. 'transactionOutId' => $mutation->transaction_out,
  33. 'expenseId' => $mutation->expense_id,
  34. 'topupId' => $mutation->top_up_id,
  35. ]),
  36. ]
  37. ),
  38. ]);
  39. }
  40. /**
  41. * Export to excel
  42. */
  43. public function exportExcel()
  44. {
  45. return new MutationExport(request());
  46. }
  47. }