StatController.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. public function actionProfit()
  62. {
  63. ini_set('memory_limit', '2045M');
  64. set_time_limit(0);
  65. //查看财务的权限
  66. $lookMoney = ShopAdminClass::lookMoneyPower($this->shopAdmin, $this->shop);
  67. if ($lookMoney == 0) {
  68. util::fail('无法查看');
  69. }
  70. //惠雅鲜花员工不能看利润表
  71. $shopAdmin = $this->shopAdmin;
  72. $adminId = $shopAdmin->adminId ?? 0;
  73. if (in_array($adminId, [2366, 2812, 4004, 3405, 3407])) {
  74. util::fail('暂无权限');
  75. }
  76. $mainId = $this->mainId;
  77. $respond = StatSaleClass::profile($mainId);
  78. $income = $respond['income'] ?? 0;
  79. $expend = $respond['expend'] ?? 0;
  80. $balance = $respond['balance'] ?? 0;
  81. //由于损耗而造成少赚的钱
  82. $wastageRespond = StatSaleClass::wastage($mainId);
  83. $wastagePrice = $wastageRespond['totalPrice'] ?? 0;
  84. $wastagePrice = floatval($wastagePrice);
  85. util::success(['income' => $income, 'expend' => $expend, 'balance' => floatval($balance), 'wastagePrice' => $wastagePrice]);
  86. }
  87. //按月统计损耗情况
  88. public function actionWaste()
  89. {
  90. ini_set('memory_limit', '2045M');
  91. set_time_limit(0);
  92. $mainId = $this->mainId;
  93. $list = StockWastageOrderClass::getAllByCondition(['mainId' => $mainId], null, '*');
  94. $arr = [];
  95. $totalNum = 0;
  96. $totalCost = 0;
  97. $totalPrice = 0;
  98. if (!empty($list)) {
  99. foreach ($list as $order) {
  100. $addTime = $order['addTime'];
  101. $time = date("Ym", strtotime($addTime));
  102. $date = date("Y年n月", strtotime($addTime));
  103. $bigNum = $order['bigNum'] ?? 0;
  104. $cost = $order['cost'] ?? 0;
  105. $price = $order['price'] ?? 0;
  106. if (isset($arr[$time])) {
  107. $arr[$time]['num'] = bcadd($arr[$time]['num'], $bigNum);
  108. $arr[$time]['cost'] = bcadd($arr[$time]['cost'], $cost, 2);
  109. $arr[$time]['price'] = bcadd($arr[$time]['price'], $price, 2);
  110. $arr[$time]['time'] = $date;
  111. } else {
  112. $arr[$time]['num'] = $bigNum;
  113. $arr[$time]['cost'] = $cost;
  114. $arr[$time]['price'] = $price;
  115. $arr[$time]['time'] = $date;
  116. }
  117. $totalNum = bcadd($totalNum, $bigNum);
  118. $totalCost = bcadd($totalCost, $cost, 2);
  119. $totalPrice = bcadd($totalPrice, $price, 2);
  120. }
  121. }
  122. $totalCost = floatval($totalCost);
  123. $totalPrice = floatval($totalPrice);
  124. util::success(['list' => $arr, 'totalNum' => $totalNum, 'totalCost' => $totalCost, 'totalPrice' => $totalPrice]);
  125. }
  126. //损耗花材
  127. public function actionWastage()
  128. {
  129. $get = Yii::$app->request->get();
  130. $where = [];
  131. $where['mainId'] = $this->mainId;
  132. $searchTime = $get['searchTime'] ?? 'today';
  133. if (!empty($searchTime)) {
  134. $startTime = $get['startTime'] ?? '';
  135. $endTime = $get['endTime'] ?? '';
  136. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  137. $start = date("Y-m-d 00:00:00", strtotime($period['startTime']));
  138. $end = date("Y-m-d 23:59:59", strtotime($period['endTime']));
  139. $where['addTime'] = ['between', [$start, $end]];
  140. }
  141. $wastageList = StockWastageOrderItemClass::getAllByCondition($where, null, '*', null, true);
  142. $stat = [];
  143. $totalCost = 0;
  144. $totalNum = 0;
  145. if (!empty($wastageList)) {
  146. foreach ($wastageList as $waste) {
  147. $name = $waste->name ?? '';
  148. $shortCover = $waste->cover ?? '';
  149. $cover = imgUtil::groupImg($shortCover);
  150. $num = $waste->itemNum ?? 0;
  151. $cost = $waste->itemCost ?? 0;
  152. $currentCost = bcmul($cost, $num, 2);
  153. $productId = $waste->productId ?? 0;
  154. if (isset($stat[$productId]) == false) {
  155. $stat[$productId]['name'] = $name;
  156. $stat[$productId]['cover'] = $cover;
  157. $stat[$productId]['num'] = 0;
  158. $stat[$productId]['amount'] = 0;
  159. }
  160. $stat[$productId]['num'] = bcadd($stat[$productId]['num'], $num, 2);
  161. $stat[$productId]['amount'] = bcadd($stat[$productId]['amount'], $currentCost, 2);
  162. $totalNum = bcadd($totalNum, $num, 2);
  163. $totalCost = bcadd($totalCost, $currentCost, 2);
  164. }
  165. $stat = array_values($stat);
  166. $stat = arrayUtil::arraySort($stat, 'num');
  167. }
  168. util::success(['stat' => $stat, 'totalNum' => $totalNum, 'totalCost' => $totalCost]);
  169. }
  170. }