DashboardController.php 5.1KB

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