ReportTransactionController.php 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Exports\TransactionExport;
  4. use App\Models\Outlet;
  5. use App\Models\Transaction;
  6. use App\Services\TransactionService;
  7. use Inertia\Controller;
  8. use Inertia\Inertia;
  9. class ReportTransactionController extends Controller
  10. {
  11. /**
  12. * Display a listing of the resource.
  13. *
  14. * @return \Inertia\Response
  15. */
  16. public function index()
  17. {
  18. $transactions = Transaction::filter(request()->only('startDate', 'endDate', 'outlet'))
  19. ->get()
  20. ->groupBy('created_at')
  21. ->transform(fn($transactions) => [[
  22. 'date' => $transactions->first()->getRawOriginal('created_at'),
  23. 'createdAt' => $transactions->first()->created_at,
  24. 'totalTransaction' => $transactions->count(),
  25. 'totalPrice' => (new TransactionService)->totalPriceGroupAsString($transactions),
  26. ]])
  27. ->flatten(1)
  28. ->toArray();
  29. $transaction = (new TransactionService)->getPaginator($transactions);
  30. return inertia('transaction/Report', [
  31. 'filters' => request()->all('startDate', 'endDate', 'outlet'),
  32. 'transactions' => $transaction,
  33. 'outlets' => Outlet::all()
  34. ->transform(fn($outlet) => [
  35. 'label' => $outlet->name,
  36. 'value' => $outlet->id,
  37. ]),
  38. ]);
  39. }
  40. /**
  41. * Export to excel
  42. */
  43. public function exportExcel()
  44. {
  45. return new TransactionExport(request());
  46. }
  47. }