DashboardService.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace App\Services;
  3. use Illuminate\Support\Facades\DB;
  4. class DashboardService
  5. {
  6. public static function productBestSelling()
  7. {
  8. $query = DB::table("sale_details")
  9. ->selectRaw(
  10. "products.name AS title,
  11. SUM(sale_details.qty) AS qty,
  12. products.profit"
  13. )
  14. ->join(
  15. "products",
  16. "products.number",
  17. "=",
  18. "sale_details.product_number"
  19. )
  20. ->groupByRaw("product_number")
  21. ->orderByRaw("qty DESC")
  22. ->limit(5)
  23. ->get();
  24. return [
  25. "title" => __("words.best_product"),
  26. "data" => $query,
  27. ];
  28. }
  29. public static function productAmount()
  30. {
  31. return [
  32. "data" => [
  33. QueryService::amount(
  34. "sales",
  35. __("words.sale"),
  36. __("words.today")
  37. ),
  38. QueryService::amount(
  39. "purchases",
  40. __("words.purchase"),
  41. __("words.today")
  42. ),
  43. QueryService::amount(
  44. "products",
  45. __("words.product"),
  46. __("words.today")
  47. ),
  48. QueryService::amount(
  49. "stock_products",
  50. __("words.stock_product"),
  51. __("words.today")
  52. ),
  53. ],
  54. ];
  55. }
  56. public static function salePurchaseAmountStatistic()
  57. {
  58. return [
  59. "title" => __("words.sale_and_purchase"),
  60. "description" => __("words.period", [
  61. "number" => now()->translatedFormat("Y"),
  62. ]),
  63. "data" => [
  64. __("words.sale") => QueryService::amountStatistic("sales"),
  65. __("words.purchase") => QueryService::amountStatistic(
  66. "purchases"
  67. ),
  68. ],
  69. ];
  70. }
  71. public static function salePriceStatistic()
  72. {
  73. return [
  74. "title" => __("words.sale"),
  75. "data" => QueryService::priceStatistic("sale_details"),
  76. ];
  77. }
  78. public static function purchasePriceStatistic()
  79. {
  80. return [
  81. "title" => __("words.purchase"),
  82. "data" => QueryService::priceStatistic("purchase_details"),
  83. ];
  84. }
  85. public static function dump()
  86. {
  87. return dd(
  88. self::productAmount(),
  89. self::productBestSelling(),
  90. self::salePurchaseAmountStatistic(),
  91. self::salePriceStatistic(),
  92. self::purchasePriceStatistic()
  93. );
  94. }
  95. }