DashboardController.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Expense;
  4. use App\Models\Laundry;
  5. use App\Models\Product;
  6. use App\Models\Transaction;
  7. use App\Services\ExpenseService;
  8. use App\Services\TransactionService;
  9. use Carbon\Carbon;
  10. use Illuminate\Http\Request;
  11. use Inertia\Inertia;
  12. class DashboardController extends Controller
  13. {
  14. /**
  15. * Handle the incoming request.
  16. *
  17. * @param \Illuminate\Http\Request $request
  18. * @return \Inertia\Response
  19. */
  20. public function __invoke(Request $request)
  21. {
  22. $transactions = Transaction::filter(['startDate' => today()])->get();
  23. $expenses = Expense::filter(['startDate' => today()])->get();
  24. $laundries = Laundry::get();
  25. $products = Product::get();
  26. $transactionChartStatistic = Transaction::get()->groupBy([
  27. fn($transaction) => Carbon::parse($transaction->getRawOriginal('created_at'))->format('Y'),
  28. fn($transaction) => Carbon::parse($transaction->getRawOriginal('created_at'))->format('M'),
  29. ]);
  30. $expenseChartStatistic = Expense::get()->groupBy([
  31. fn($expense) => Carbon::parse($expense->getRawOriginal('created_at'))->format('Y'),
  32. fn($expense) => Carbon::parse($expense->getRawOriginal('created_at'))->format('M'),
  33. ]);
  34. return inertia('home/Index', [
  35. 'cardStatistics' => [
  36. [
  37. 'title' => __('words.transaction'),
  38. 'icon' => 'pi pi-shopping-cart',
  39. 'amount' => $transactions->count(),
  40. 'amountLabel' => __('words.today'),
  41. 'value' => (new TransactionService)->totalPriceGroupAsString($transactions),
  42. ],
  43. [
  44. 'title' => __('words.expense'),
  45. 'icon' => 'pi pi-wallet',
  46. 'amount' => $expenses->count(),
  47. 'amountLabel' => __('words.today'),
  48. 'value' => (new ExpenseService)->totalPriceAsString($expenses),
  49. ],
  50. [
  51. 'title' => __('words.laundry_type'),
  52. 'icon' => 'pi pi-table',
  53. 'amountLabel' => __('words.total'),
  54. 'amount' => $laundries->count(),
  55. ],
  56. [
  57. 'title' => __('words.product_type'),
  58. 'icon' => 'pi pi-table',
  59. 'amountLabel' => __('words.total'),
  60. 'amount' => $products->count(),
  61. ],
  62. ],
  63. 'chartStatistics' => [
  64. [
  65. 'title' => __('words.transaction_statistic'),
  66. 'data' => (new TransactionService)->statisticData($transactionChartStatistic),
  67. ],
  68. [
  69. 'title' => __('words.expense_statistic'),
  70. 'data' => (new ExpenseService)->statisticData($expenseChartStatistic),
  71. ],
  72. ],
  73. ]);
  74. }
  75. }