DashboardController.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. $transactionDiscount = Transaction::whereMonth('created_at', date('m'))->whereNotIn('discount', [0])->get();
  30. $transactionChart = Transaction::get()
  31. ->groupBy([
  32. fn($transaction) => Carbon::parse($transaction->getRawOriginal('created_at'))->format('Y'),
  33. fn($transaction) => Carbon::parse($transaction->getRawOriginal('created_at'))->format('M'),
  34. ]);
  35. $mutationChart = Mutation::whereYear('created_at', date('Y'))
  36. ->get()
  37. ->groupBy([
  38. fn($mutation) => $mutation->type,
  39. fn($mutation) => Carbon::parse($mutation->getRawOriginal('created_at'))->format('M'),
  40. ]);
  41. $transactionOutletChart = Transaction::whereYear('created_at', date('Y'))
  42. ->whereMonth('created_at', date('m'))
  43. ->get()
  44. ->groupBy([
  45. fn($transaction) => Carbon::parse($transaction->getRawOriginal('created_at'))->format('M'),
  46. fn($transaction) => $transaction->outlet->name,
  47. ]);
  48. $topTransactionChart = Transaction::get()
  49. ->groupBy('customer_number');
  50. return inertia('home/Index', [
  51. 'cardStatistics' => [
  52. [
  53. 'title' => __('words.transaction'),
  54. 'icon' => 'pi pi-shopping-cart',
  55. 'amount' => $transactions->count(),
  56. 'amountLabel' => __('words.today'),
  57. 'value' => (new TransactionService)->totalPriceGroupAsString($transactions),
  58. ],
  59. [
  60. 'title' => __('words.expense'),
  61. 'icon' => 'pi pi-wallet',
  62. 'amount' => $expenses->count(),
  63. 'amountLabel' => __('words.today'),
  64. 'value' => (new ExpenseService)->totalPriceAsString($expenses),
  65. ],
  66. [
  67. 'title' => __('words.discount_given'),
  68. 'icon' => 'pi pi-percentage',
  69. 'amount' => $transactionDiscount->count(),
  70. 'amountLabel' => __('words.this_month'),
  71. 'value' => (new TransactionService)->totalDiscountGivenGroupAsString($transactionDiscount),
  72. ],
  73. [
  74. 'title' => __('words.laundry_type'),
  75. 'icon' => 'pi pi-table',
  76. 'amount' => $laundries->count(),
  77. 'amountLabel' => __('words.total'),
  78. ],
  79. [
  80. 'title' => __('words.product_type'),
  81. 'icon' => 'pi pi-table',
  82. 'amount' => $products->count(),
  83. 'amountLabel' => __('words.total'),
  84. ],
  85. ],
  86. 'chartTransactionStatistics' => [
  87. 'transaction' => [
  88. 'title' => __('words.transaction_statistic'),
  89. 'description' => __('words.per_year') . ' ' . now()->subYear(1)->format('Y') . '-' . date('Y'),
  90. 'data' => (new TransactionService)->statisticData($transactionChart, -2),
  91. ],
  92. 'transactionMutation' => [
  93. 'title' => __('words.mutation_statistic'),
  94. 'description' => __('words.per_year') . ' ' . date('Y'),
  95. 'data' => (new MutationService)->statisticData($mutationChart, -2),
  96. ],
  97. ],
  98. 'chartOutletStatistic' => [
  99. 'title' => __('words.transaction_outlet_statistic'),
  100. 'description' => Carbon::parse(date('Y-m-d'))->translatedFormat('F, Y'),
  101. 'data' => (new TransactionService)->statisticData($transactionOutletChart)->first(),
  102. ],
  103. 'chartTopTransactionStatistic' => [
  104. 'title' => __('words.top_customer'),
  105. 'description' => __('words.top_number_customer', ['number' => 5]),
  106. 'data' => (new TransactionService)->topTransaction($topTransactionChart),
  107. ],
  108. ]);
  109. }
  110. }