DashboardController.php 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. $transactionOutletChartStatistic = Transaction::get()->groupBy('outlet.name');
  35. return inertia('home/Index', [
  36. 'cardStatistics' => [
  37. [
  38. 'title' => __('words.transaction'),
  39. 'icon' => 'pi pi-shopping-cart',
  40. 'amount' => $transactions->count(),
  41. 'amountLabel' => __('words.today'),
  42. 'value' => (new TransactionService)->totalPriceGroupAsString($transactions),
  43. ],
  44. [
  45. 'title' => __('words.expense'),
  46. 'icon' => 'pi pi-wallet',
  47. 'amount' => $expenses->count(),
  48. 'amountLabel' => __('words.today'),
  49. 'value' => (new ExpenseService)->totalPriceAsString($expenses),
  50. ],
  51. [
  52. 'title' => __('words.laundry_type'),
  53. 'icon' => 'pi pi-table',
  54. 'amountLabel' => __('words.total'),
  55. 'amount' => $laundries->count(),
  56. ],
  57. [
  58. 'title' => __('words.product_type'),
  59. 'icon' => 'pi pi-table',
  60. 'amountLabel' => __('words.total'),
  61. 'amount' => $products->count(),
  62. ],
  63. ],
  64. 'transactionStatistics' => [
  65. [
  66. 'title' => __('words.transaction_statistic'),
  67. 'data' => (new TransactionService)->statisticData($transactionChartStatistic),
  68. ],
  69. [
  70. 'title' => __('words.expense_statistic'),
  71. 'data' => (new ExpenseService)->statisticData($expenseChartStatistic),
  72. ],
  73. ],
  74. 'transactionOutletStatistics' => [
  75. 'title' => __('words.transaction_outlet_statistic'),
  76. 'data' => (new TransactionService)->totalPerMonth($transactionOutletChartStatistic),
  77. ],
  78. ]);
  79. }
  80. }