ReportMutationController.php 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Exports\MutationExport;
  4. use App\Http\Controllers\Controller;
  5. use App\Models\Mutation;
  6. use App\Models\Outlet;
  7. use App\Services\MutationService;
  8. use Illuminate\Support\Facades\Gate;
  9. use Inertia\Inertia;
  10. class ReportMutationController extends Controller
  11. {
  12. /**
  13. * Display a listing of the resource.
  14. *
  15. * @return \Inertia\Response
  16. */
  17. public function index()
  18. {
  19. if (Gate::allows('isOutletHead')) {
  20. request()->merge(['outlet' => request()->user()->outlet_id]);
  21. } else if (Gate::allows('isEmployee')) {
  22. request()->merge(['outlet' => request()->user()->outlet_id]);
  23. }
  24. $mutations = Mutation::filter(request()->only('startDate', 'endDate', 'outlet'));
  25. return inertia('mutation/Report', [
  26. 'filters' => request()->all('startDate', 'endDate', 'outlet'),
  27. 'mutations' => Inertia::lazy(
  28. fn() => [
  29. 'totalIncome' => (new MutationService)->totalIncomeAsString($mutations->get()),
  30. 'totalExpense' => (new MutationService)->totalExpenseAsString($mutations->get()),
  31. 'totalAmount' => (new MutationService)->totalAmountAsString($mutations->get()),
  32. 'details' => $mutations
  33. ->latest()
  34. ->paginate(10)
  35. ->withQueryString()
  36. ->through(fn($mutation) => [
  37. 'createdAt' => $mutation->created_at,
  38. 'outlet' => $mutation->outlet->name,
  39. 'amount' => $mutation->amount,
  40. 'type' => $mutation->type,
  41. 'transactionId' => $mutation->transaction_id,
  42. 'expenseId' => $mutation->expense_id,
  43. ]),
  44. ]
  45. ),
  46. 'outlets' => Outlet::all()
  47. ->transform(fn($outlet) => [
  48. 'label' => $outlet->name,
  49. 'value' => $outlet->id,
  50. ]),
  51. ]);
  52. }
  53. /**
  54. * Export to excel
  55. */
  56. public function exportExcel()
  57. {
  58. return new MutationExport(request());
  59. }
  60. }