StatController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace ghs\controllers;
  3. use bizGhs\order\classes\StockWastageOrderClass;
  4. use bizGhs\order\classes\StockWastageOrderItemClass;
  5. use bizGhs\stat\classes\StatSaleClass;
  6. use common\components\arrayUtil;
  7. use common\components\imgUtil;
  8. use Yii;
  9. use common\components\util;
  10. use common\components\dateUtil;
  11. class StatController extends BaseController
  12. {
  13. public function actionProfit()
  14. {
  15. ini_set('memory_limit', '2045M');
  16. set_time_limit(0);
  17. $shopAdmin = $this->shopAdmin;
  18. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  19. util::fail('超管才能查看');
  20. }
  21. //惠雅鲜花员工不能看利润表
  22. $shopAdmin = $this->shopAdmin;
  23. $adminId = $shopAdmin->adminId ?? 0;
  24. if (in_array($adminId, [2366, 2812, 4004, 3405, 3407])) {
  25. util::fail('暂无权限');
  26. }
  27. $mainId = $this->mainId;
  28. $respond = StatSaleClass::profile($mainId);
  29. $income = $respond['income'] ?? 0;
  30. $expend = $respond['expend'] ?? 0;
  31. $balance = $respond['balance'] ?? 0;
  32. //由于损耗而造成少赚的钱
  33. $wastageRespond = StatSaleClass::wastage($mainId);
  34. $wastagePrice = $wastageRespond['totalPrice'] ?? 0;
  35. $wastagePrice = floatval($wastagePrice);
  36. util::success(['income' => $income, 'expend' => $expend, 'balance' => floatval($balance), 'wastagePrice' => $wastagePrice]);
  37. }
  38. //按月统计损耗情况
  39. public function actionWaste()
  40. {
  41. ini_set('memory_limit', '2045M');
  42. set_time_limit(0);
  43. $mainId = $this->mainId;
  44. $list = StockWastageOrderClass::getAllByCondition(['mainId' => $mainId], null, '*');
  45. $arr = [];
  46. $totalNum = 0;
  47. $totalCost = 0;
  48. $totalPrice = 0;
  49. if (!empty($list)) {
  50. foreach ($list as $order) {
  51. $addTime = $order['addTime'];
  52. $time = date("Ym", strtotime($addTime));
  53. $date = date("Y年n月", strtotime($addTime));
  54. $bigNum = $order['bigNum'] ?? 0;
  55. $cost = $order['cost'] ?? 0;
  56. $price = $order['price'] ?? 0;
  57. if (isset($arr[$time])) {
  58. $arr[$time]['num'] = bcadd($arr[$time]['num'], $bigNum);
  59. $arr[$time]['cost'] = bcadd($arr[$time]['cost'], $cost, 2);
  60. $arr[$time]['price'] = bcadd($arr[$time]['price'], $price, 2);
  61. $arr[$time]['time'] = $date;
  62. } else {
  63. $arr[$time]['num'] = $bigNum;
  64. $arr[$time]['cost'] = $cost;
  65. $arr[$time]['price'] = $price;
  66. $arr[$time]['time'] = $date;
  67. }
  68. $totalNum = bcadd($totalNum, $bigNum);
  69. $totalCost = bcadd($totalCost, $cost, 2);
  70. $totalPrice = bcadd($totalPrice, $price, 2);
  71. }
  72. }
  73. $totalCost = floatval($totalCost);
  74. $totalPrice = floatval($totalPrice);
  75. util::success(['list' => $arr, 'totalNum' => $totalNum, 'totalCost' => $totalCost, 'totalPrice' => $totalPrice]);
  76. }
  77. //损耗花材
  78. public function actionWastage()
  79. {
  80. $get = Yii::$app->request->get();
  81. $where = [];
  82. $where['mainId'] = $this->mainId;
  83. $searchTime = $get['searchTime'] ?? 'today';
  84. if (!empty($searchTime)) {
  85. $startTime = $get['startTime'] ?? '';
  86. $endTime = $get['endTime'] ?? '';
  87. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  88. $start = date("Y-m-d 00:00:00", strtotime($period['startTime']));
  89. $end = date("Y-m-d 23:59:59", strtotime($period['endTime']));
  90. $where['addTime'] = ['between', [$start, $end]];
  91. }
  92. $wastageList = StockWastageOrderItemClass::getAllByCondition($where, null, '*', null, true);
  93. $stat = [];
  94. $totalCost = 0;
  95. $totalNum = 0;
  96. if (!empty($wastageList)) {
  97. foreach ($wastageList as $waste) {
  98. $name = $waste->name ?? '';
  99. $shortCover = $waste->cover ?? '';
  100. $cover = imgUtil::groupImg($shortCover);
  101. $num = $waste->itemNum ?? 0;
  102. $cost = $waste->itemCost ?? 0;
  103. $currentCost = bcmul($cost, $num, 2);
  104. $productId = $waste->productId ?? 0;
  105. if (isset($stat[$productId]) == false) {
  106. $stat[$productId]['name'] = $name;
  107. $stat[$productId]['cover'] = $cover;
  108. $stat[$productId]['num'] = 0;
  109. $stat[$productId]['amount'] = 0;
  110. }
  111. $stat[$productId]['num'] = bcadd($stat[$productId]['num'], $num, 2);
  112. $stat[$productId]['amount'] = bcadd($stat[$productId]['amount'], $currentCost, 2);
  113. $totalNum = bcadd($totalNum, $num, 2);
  114. $totalCost = bcadd($totalCost, $currentCost, 2);
  115. }
  116. $stat = array_values($stat);
  117. $stat = arrayUtil::arraySort($stat, 'num');
  118. }
  119. util::success(['stat' => $stat, 'totalNum' => $totalNum, 'totalCost' => $totalCost]);
  120. }
  121. }