StatController.php 7.3 KB

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