DashboardService.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace App\Services;
  3. use Carbon\Carbon;
  4. use Illuminate\Support\Facades\DB;
  5. class DashboardService
  6. {
  7. public static function productBestSelling()
  8. {
  9. $query = DB::table("sale_details")
  10. ->selectRaw(
  11. "products.name AS title,
  12. SUM(sale_details.qty) AS qty,
  13. products.profit"
  14. )
  15. ->join(
  16. "products",
  17. "products.number",
  18. "=",
  19. "sale_details.product_number"
  20. )
  21. ->groupByRaw("product_number")
  22. ->orderByRaw("qty DESC")
  23. ->limit(5)
  24. ->get();
  25. return [
  26. "title" => __("words.best_product"),
  27. "data" => $query,
  28. ];
  29. }
  30. public static function productAmount()
  31. {
  32. return [
  33. "data" => [
  34. self::amount("sales", __("words.sale"), __("words.today")),
  35. self::amount(
  36. "purchases",
  37. __("words.purchase"),
  38. __("words.today")
  39. ),
  40. self::amount(
  41. "products",
  42. __("words.product"),
  43. __("words.today")
  44. ),
  45. self::amount(
  46. "stock_products",
  47. __("words.stock_product"),
  48. __("words.today")
  49. ),
  50. ],
  51. ];
  52. }
  53. public static function salePurchaseAmountStatistic()
  54. {
  55. return [
  56. "title" => __("words.sale_and_purchase"),
  57. "description" => __("words.period", [
  58. "number" => now()->translatedFormat("Y"),
  59. ]),
  60. "data" => [
  61. __("words.sale") => self::amountStatistic("sales"),
  62. __("words.purchase") => self::amountStatistic("purchases"),
  63. ],
  64. ];
  65. }
  66. public static function salePriceStatistic()
  67. {
  68. return [
  69. "title" => __("words.sale"),
  70. "data" => self::priceStatistic("sale_details"),
  71. ];
  72. }
  73. public static function purchasePriceStatistic()
  74. {
  75. return [
  76. "title" => __("words.purchase"),
  77. "data" => self::priceStatistic("purchase_details"),
  78. ];
  79. }
  80. public static function amount(
  81. string $table,
  82. string $title,
  83. string $description
  84. ) {
  85. return DB::table($table)
  86. ->selectRaw(
  87. "COUNT(*) AS amount, (
  88. SELECT
  89. COUNT(*)
  90. FROM
  91. $table
  92. WHERE
  93. DATE(created_at) = CURDATE()) AS amountToday"
  94. )
  95. ->get()
  96. ->transform(
  97. fn($table) => [
  98. "title" => $title,
  99. "amount" => $table->amount,
  100. "amountToday" => $table->amountToday,
  101. "amountTodayDescription" => $description,
  102. ]
  103. )
  104. ->first();
  105. }
  106. public static function amountStatistic(string $table)
  107. {
  108. return DB::table($table)
  109. ->selectRaw(
  110. "COUNT(*) AS amount,
  111. DATE_FORMAT(created_at, '%b') AS month"
  112. )
  113. ->whereRaw("YEAR(created_at) = YEAR(CURDATE())")
  114. ->groupByRaw("month")
  115. ->orderByRaw("created_at")
  116. ->get()
  117. ->pluck("amount", "month");
  118. }
  119. public static function priceStatistic(string $table)
  120. {
  121. return DB::table($table)
  122. ->selectRaw(
  123. "price,
  124. created_at"
  125. )
  126. ->orderByRaw("created_at")
  127. ->get()
  128. ->groupBy([
  129. fn($value) => Carbon::parse($value->created_at)->format("Y"),
  130. fn($value) => Carbon::parse($value->created_at)->format("M"),
  131. ])
  132. ->take(2)
  133. ->transform(
  134. fn($year) => $year->transform(
  135. fn($month) => $month->sum(fn($arr) => $arr->price)
  136. )
  137. );
  138. }
  139. public static function dump()
  140. {
  141. return dd(
  142. self::productAmount(),
  143. self::productBestSelling(),
  144. self::salePurchaseAmountStatistic(),
  145. self::salePriceStatistic(),
  146. self::purchasePriceStatistic()
  147. );
  148. }
  149. }