DashboardService.php 4.5KB

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