StatController.php 7.1 KB

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