StatController.php 8.5 KB

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