StatController.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. $hasPower = 0;
  69. if (getenv('YII_ENV') == 'production') {
  70. //小向花卉采购控制
  71. if ($this->mainId == 23390) {
  72. if (in_array($this->adminId, [28456])) {
  73. $hasPower = 1;
  74. }
  75. }
  76. } else {
  77. if ($this->mainId == 644) {
  78. if (in_array($this->adminId, [1230])) {
  79. $hasPower = 1;
  80. }
  81. }
  82. }
  83. if ($lookMoney == 0 && $hasPower == 0) {
  84. util::fail('没有财务权限哦');
  85. }
  86. $mainId = $this->mainId;
  87. StatSaleClass::classProfile($mainId);
  88. }
  89. public function actionIncomeOutItem()
  90. {
  91. ini_set('memory_limit', '2045M');
  92. set_time_limit(0);
  93. //查看财务的权限
  94. $lookMoney = ShopAdminClass::lookMoneyPower($this->shopAdmin, $this->shop);
  95. if ($lookMoney == 0) {
  96. util::fail('无法查看');
  97. }
  98. $mainId = $this->mainId;
  99. StatSaleClass::incomeOutItem($mainId);
  100. }
  101. public function actionProfit()
  102. {
  103. ini_set('memory_limit', '2045M');
  104. set_time_limit(0);
  105. //查看财务的权限
  106. $lookMoney = ShopAdminClass::lookMoneyPower($this->shopAdmin, $this->shop);
  107. if ($lookMoney == 0) {
  108. util::fail('无法查看');
  109. }
  110. $mainId = $this->mainId;
  111. $respond = StatSaleClass::profile($mainId);
  112. $income = $respond['income'] ?? 0;
  113. $expend = $respond['expend'] ?? 0;
  114. $balance = $respond['balance'] ?? 0;
  115. //由于损耗而造成少赚的钱
  116. $wastageRespond = StatSaleClass::wastage($mainId);
  117. $wastagePrice = $wastageRespond['totalPrice'] ?? 0;
  118. $wastagePrice = floatval($wastagePrice);
  119. util::success(['income' => $income, 'expend' => $expend, 'balance' => floatval($balance), 'wastagePrice' => $wastagePrice]);
  120. }
  121. //按月统计损耗情况
  122. public function actionWaste()
  123. {
  124. ini_set('memory_limit', '2045M');
  125. set_time_limit(0);
  126. $mainId = $this->mainId;
  127. $list = StockWastageOrderClass::getAllByCondition(['mainId' => $mainId], null, '*');
  128. $arr = [];
  129. $totalNum = 0;
  130. $totalCost = 0;
  131. $totalPrice = 0;
  132. if (!empty($list)) {
  133. foreach ($list as $order) {
  134. $addTime = $order['addTime'];
  135. $time = date("Ym", strtotime($addTime));
  136. $date = date("Y年n月", strtotime($addTime));
  137. $bigNum = $order['bigNum'] ?? 0;
  138. $cost = $order['cost'] ?? 0;
  139. $price = $order['price'] ?? 0;
  140. if (isset($arr[$time])) {
  141. $arr[$time]['num'] = bcadd($arr[$time]['num'], $bigNum);
  142. $arr[$time]['cost'] = bcadd($arr[$time]['cost'], $cost, 2);
  143. $arr[$time]['price'] = bcadd($arr[$time]['price'], $price, 2);
  144. $arr[$time]['time'] = $date;
  145. } else {
  146. $arr[$time]['num'] = $bigNum;
  147. $arr[$time]['cost'] = $cost;
  148. $arr[$time]['price'] = $price;
  149. $arr[$time]['time'] = $date;
  150. }
  151. $totalNum = bcadd($totalNum, $bigNum);
  152. $totalCost = bcadd($totalCost, $cost, 2);
  153. $totalPrice = bcadd($totalPrice, $price, 2);
  154. }
  155. }
  156. $totalCost = floatval($totalCost);
  157. $totalPrice = floatval($totalPrice);
  158. util::success(['list' => $arr, 'totalNum' => $totalNum, 'totalCost' => $totalCost, 'totalPrice' => $totalPrice]);
  159. }
  160. //损耗花材
  161. public function actionWastage()
  162. {
  163. $get = Yii::$app->request->get();
  164. $where = [];
  165. $where['mainId'] = $this->mainId;
  166. $searchTime = $get['searchTime'] ?? 'today';
  167. if (!empty($searchTime)) {
  168. $startTime = $get['startTime'] ?? '';
  169. $endTime = $get['endTime'] ?? '';
  170. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  171. $start = date("Y-m-d 00:00:00", strtotime($period['startTime']));
  172. $end = date("Y-m-d 23:59:59", strtotime($period['endTime']));
  173. $where['addTime'] = ['between', [$start, $end]];
  174. }
  175. $wastageList = StockWastageOrderItemClass::getAllByCondition($where, null, '*', null, true);
  176. $stat = [];
  177. $totalCost = 0;
  178. $totalNum = 0;
  179. if (!empty($wastageList)) {
  180. foreach ($wastageList as $waste) {
  181. $name = $waste->name ?? '';
  182. $shortCover = $waste->cover ?? '';
  183. $cover = imgUtil::groupImg($shortCover);
  184. $num = $waste->itemNum ?? 0;
  185. $cost = $waste->itemCost ?? 0;
  186. $currentCost = bcmul($cost, $num, 2);
  187. $productId = $waste->productId ?? 0;
  188. if (isset($stat[$productId]) == false) {
  189. $stat[$productId]['name'] = $name;
  190. $stat[$productId]['cover'] = $cover;
  191. $stat[$productId]['num'] = 0;
  192. $stat[$productId]['amount'] = 0;
  193. }
  194. $stat[$productId]['num'] = bcadd($stat[$productId]['num'], $num, 2);
  195. $stat[$productId]['amount'] = bcadd($stat[$productId]['amount'], $currentCost, 2);
  196. $totalNum = bcadd($totalNum, $num, 2);
  197. $totalCost = bcadd($totalCost, $currentCost, 2);
  198. }
  199. $stat = array_values($stat);
  200. $stat = arrayUtil::arraySort($stat, 'num');
  201. }
  202. util::success(['stat' => $stat, 'totalNum' => $totalNum, 'totalCost' => $totalCost]);
  203. }
  204. }