DashboardController.php 4.0KB

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