DashboardController.php 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Expense;
  5. use App\Models\Laundry;
  6. use App\Models\Mutation;
  7. use App\Models\Product;
  8. use App\Models\Transaction;
  9. use App\Services\ExpenseService;
  10. use App\Services\MutationService;
  11. use App\Services\TransactionService;
  12. use Carbon\Carbon;
  13. use Illuminate\Http\Request;
  14. use Inertia\Inertia;
  15. class DashboardController extends Controller
  16. {
  17. /**
  18. * Handle the incoming request.
  19. *
  20. * @param \Illuminate\Http\Request $request
  21. * @return \Inertia\Response
  22. */
  23. public function __invoke(Request $request)
  24. {
  25. $transactions = Transaction::whereDate('created_at', date('Y-m-d'))->get();
  26. $expenses = Expense::whereDate('created_at', date('Y-m-d'))->get();
  27. $laundries = Laundry::get();
  28. $products = Product::get();
  29. $transactionChart = Transaction::get()
  30. ->groupBy([
  31. fn($transaction) => Carbon::parse($transaction->getRawOriginal('created_at'))->format('Y'),
  32. fn($transaction) => Carbon::parse($transaction->getRawOriginal('created_at'))->format('M'),
  33. ]);
  34. $mutationChart = Mutation::whereYear('created_at', date('Y'))
  35. ->get()
  36. ->groupBy([
  37. fn($mutation) => $mutation->type,
  38. fn($mutation) => Carbon::parse($mutation->getRawOriginal('created_at'))->format('M'),
  39. ]);
  40. $transactionOutletChart = Transaction::whereYear('created_at', date('Y'))
  41. ->whereMonth('created_at', date('m'))
  42. ->get()
  43. ->groupBy([
  44. fn($transaction) => Carbon::parse($transaction->getRawOriginal('created_at'))->format('M'),
  45. fn($transaction) => $transaction->outlet->name,
  46. ]);
  47. $topTransactionChart = Transaction::get()
  48. ->groupBy('customer_number');
  49. return inertia('home/Index', [
  50. 'cardStatistics' => [
  51. [
  52. 'title' => __('words.transaction'),
  53. 'icon' => 'pi pi-shopping-cart',
  54. 'amount' => $transactions->count(),
  55. 'amountLabel' => __('words.today'),
  56. 'value' => (new TransactionService)->totalPriceGroupAsString($transactions),
  57. ],
  58. [
  59. 'title' => __('words.expense'),
  60. 'icon' => 'pi pi-wallet',
  61. 'amount' => $expenses->count(),
  62. 'amountLabel' => __('words.today'),
  63. 'value' => (new ExpenseService)->totalPriceAsString($expenses),
  64. ],
  65. [
  66. 'title' => __('words.laundry_type'),
  67. 'icon' => 'pi pi-table',
  68. 'amount' => $laundries->count(),
  69. 'amountLabel' => __('words.total'),
  70. ],
  71. [
  72. 'title' => __('words.product_type'),
  73. 'icon' => 'pi pi-table',
  74. 'amount' => $products->count(),
  75. 'amountLabel' => __('words.total'),
  76. ],
  77. ],
  78. 'chartTransactionStatistics' => [
  79. 'transaction' => [
  80. 'title' => __('words.transaction_statistic'),
  81. 'description' => __('words.per_year') . ' ' . now()->subYear(1)->format('Y') . '-' . date('Y'),
  82. 'data' => (new TransactionService)->statisticData($transactionChart, -2),
  83. ],
  84. 'transactionMutation' => [
  85. 'title' => __('words.mutation_statistic'),
  86. 'description' => __('words.per_year') . ' ' . date('Y'),
  87. 'data' => (new MutationService)->statisticData($mutationChart, -2),
  88. ],
  89. ],
  90. 'chartOutletStatistic' => [
  91. 'title' => __('words.transaction_outlet_statistic'),
  92. 'description' => Carbon::parse(date('Y-m-d'))->translatedFormat('F, Y'),
  93. 'data' => (new TransactionService)->statisticData($transactionOutletChart)->first(),
  94. ],
  95. 'chartTopTransactionStatistic' => [
  96. 'title' => __('words.top_customer'),
  97. 'description' => __('words.top_number_customer', ['number' => 5]),
  98. 'data' => (new TransactionService)->topTransaction($topTransactionChart),
  99. ],
  100. ]);
  101. }
  102. }