MutationController.php 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 MutationController extends Controller
  8. {
  9. /**
  10. * Display a listing of the resource.
  11. *
  12. * @return \Inertia\Response
  13. */
  14. public function index()
  15. {
  16. $this->authorize('viewAny', Mutation::class);
  17. $mutations = Mutation::filter(request()->only('startDate', 'endDate'));
  18. return inertia('mutation/Report', [
  19. 'filters' => request()->all('startDate', 'endDate', 'outlet'),
  20. 'mutations' => Inertia::lazy(
  21. fn() => [
  22. 'totalIncome' => (new MutationService)->totalIncomeAsString($mutations->get()),
  23. 'totalExpense' => (new MutationService)->totalExpenseAsString($mutations->get()),
  24. 'totalAmount' => (new MutationService)->totalAmountAsString($mutations->get()),
  25. 'details' => $mutations
  26. ->latest()
  27. ->paginate(10)
  28. ->withQueryString()
  29. ->through(fn($mutation) => [
  30. 'createdAt' => $mutation->created_at,
  31. 'amount' => $mutation->amount,
  32. 'type' => $mutation->type,
  33. 'outTransactionId' => $mutation->out_transaction_id,
  34. 'expenseId' => $mutation->expense_id,
  35. 'topUpId' => $mutation->top_up_id,
  36. ]),
  37. ]
  38. ),
  39. ]);
  40. }
  41. /**
  42. * Export to excel
  43. */
  44. public function exportExcel()
  45. {
  46. $this->authorize('viewAny', Mutation::class);
  47. return new MutationExport(request());
  48. }
  49. }