DashboardService.php 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. "data" => [
  61. __("words.sale") => QueryService::amountStatistic("sales"),
  62. __("words.purchase") => QueryService::amountStatistic(
  63. "purchases"
  64. ),
  65. ],
  66. ];
  67. }
  68. public static function salePriceStatistic()
  69. {
  70. return [
  71. "title" => __("words.sale"),
  72. "data" => QueryService::priceStatistic("sale_details"),
  73. ];
  74. }
  75. public static function purchasePriceStatistic()
  76. {
  77. return [
  78. "title" => __("words.purchase"),
  79. "data" => QueryService::priceStatistic("purchase_details"),
  80. ];
  81. }
  82. public static function dump()
  83. {
  84. return dd(
  85. self::productAmount(),
  86. self::productBestSelling(),
  87. self::salePurchaseAmountStatistic(),
  88. self::salePriceStatistic(),
  89. self::purchasePriceStatistic()
  90. );
  91. }
  92. }