PartClass.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace bizHd\part\classes;
  3. use bizHd\base\classes\BaseClass;
  4. use bizHd\item\classes\UnitChangeClass;
  5. use bizHd\shop\classes\MainClass;
  6. use common\components\dict;
  7. use common\components\orderSn;
  8. use bizGhs\product\classes\ProductClass;
  9. use bizGhs\stock\classes\StockRecordClass;
  10. use Yii;
  11. class PartClass extends BaseClass
  12. {
  13. public static $baseFile = '\bizHd\part\models\Part';
  14. const STATUS_COMPLETE = 1; //已完成
  15. //列表
  16. public static function getPartList($where)
  17. {
  18. $data = self::getList('*', $where, 'addTime DESC');
  19. $list = isset($data['list']) && !empty($data['list']) ? $data['list'] : [];
  20. if (empty($list)) {
  21. return $data;
  22. }
  23. $data['list'] = $list;
  24. return $data;
  25. }
  26. public static function addOrder($post)
  27. {
  28. $mainId = $post['mainId'] ?? 0;
  29. $data = [];
  30. $data['status'] = self::STATUS_COMPLETE;
  31. $orderSn = orderSn::getPartSn();
  32. $data['orderSn'] = $orderSn;
  33. $data['sjId'] = $post['sjId'];
  34. $data['shopId'] = $post['shopId'];
  35. $data['shopAdminId'] = $post['shopAdminId'] ?? 0;
  36. $data['shopAdminName'] = $post['shopAdminName'] ?? '';
  37. //花材列表
  38. $productInfoList = $post['product'];
  39. $productInfoList = self::mergeItemInfo($productInfoList);
  40. $totalNum = 0;
  41. $totalBig = 0;
  42. $totalSmall = 0;
  43. if (!empty($productInfoList)) {
  44. foreach ($productInfoList as $current) {
  45. $bigNum = $current['bigNum'] ?? 0;
  46. $totalNum = bcadd($totalNum, $bigNum, 2);
  47. $smallNum = $current['smallNum'] ?? 0;
  48. $ratio = isset($current['ratio']) && $current['ratio'] > 0 ? $current['ratio'] : 1;
  49. $div = bcdiv($smallNum, $ratio, 2);
  50. $totalNum = bcadd($totalNum, $div, 2);
  51. $totalBig = bcadd($totalBig, $bigNum);
  52. $totalSmall = bcadd($totalSmall, $smallNum);
  53. }
  54. }
  55. $data['bigNum'] = $totalBig;
  56. $data['smallNum'] = $totalSmall;
  57. $data['num'] = $totalNum;
  58. $data['remark'] = $post['remark'] ?? '';
  59. $data['mainId'] = $mainId;
  60. //写入订单表
  61. $order = self::add($data, true);
  62. foreach ($productInfoList as $k => $v) {
  63. $stockInfo = ProductClass::decreaseStock($v['productId'], $v['bigNum'], $v['smallNum'], true, false);
  64. $productInfoList[$k]['oldStock'] = $stockInfo['oldStock'];
  65. $productInfoList[$k]['newStock'] = $stockInfo['newStock'];
  66. }
  67. //写入详情表
  68. $batchData = self::groupOrderItem($productInfoList, $orderSn);
  69. $stockItem = [];
  70. foreach ($batchData as $currentItem) {
  71. $currentItem['mainId'] = $mainId;
  72. PartItemClass::add($currentItem);
  73. $stockItem[] = [
  74. 'productId' => $currentItem['productId'] ?? 0,
  75. 'itemId' => $currentItem['itemId'] ?? 0,
  76. 'itemNum' => $currentItem['itemNum'] ?? 0,
  77. 'oldStock' => $currentItem['itemStock'] ?? 0,
  78. 'itemStock' => $currentItem['itemStock'] ?? 0,
  79. 'newStock' => $currentItem['newStock'] ?? 0,
  80. 'bigNum' => $currentItem['itemNum'] ?? 0,
  81. 'smallNum' => 0,
  82. ];
  83. }
  84. $stockData = [];
  85. $stockData['shopId'] = $data['shopId'];
  86. $stockData['relateName'] = $data['shopAdminName'] ?? '';
  87. $stockData['ptStyle'] = $post['ptStyle'] ?? dict::getDict('ptStyle', 'hd');
  88. $stockData['orderSn'] = $orderSn;
  89. $stockData['sjId'] = $data['sjId'] ?? 0;
  90. $stockData['itemInfo'] = $stockItem;
  91. StockRecordClass::addRecordByOrder($stockData, StockRecordClass::STOCK_RECORD_TYPE_PART);
  92. $main = MainClass::getLockById($mainId);
  93. if (empty($main)) {
  94. util::fail('没有main的信息');
  95. }
  96. $totalCost = 0;
  97. $totalNum = 0;
  98. foreach ($batchData as $v) {
  99. $num = $v['itemNum'] ?? 0;
  100. $cost = $v['itemCost'] ?? 0;
  101. $totalCost = bcadd($totalCost, bcmul($num, $cost, 2), 2);
  102. $totalNum = bcadd($totalNum, $num);
  103. }
  104. $order->cost = $totalCost;
  105. $order->save();
  106. StatPartClass::replace($main, $totalCost, $totalNum);
  107. //拆散后子花材的库存需要增加,多处要同步修改,关键词part_to_sub_item
  108. $ids = array_column($productInfoList, 'productId');
  109. $bigInfoList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, 'id,name,ratio', 'id');
  110. $map = UnitChangeClass::getAllByCondition(['big' => ['in', $ids]], null, '*', 'big');
  111. if (!empty($map)) {
  112. foreach ($productInfoList as $key => $value) {
  113. $num = $value['bigNum'] ?? 0;
  114. $productId = $value['productId'] ?? 0;
  115. $mapInfo = $map[$productId] ?? [];
  116. $bigInfo = $bigInfoList[$productId] ?? [];
  117. if (!empty($mapInfo) && !empty($bigInfo)) {
  118. $bigRatio = $bigInfo['ratio'] ?? 0;
  119. $bigName = $bigInfo['name'] ?? '';
  120. $totalNum = bcmul($num, $bigRatio);
  121. $smallId = $mapInfo['small'] ?? 0;
  122. $stockInfo = ProductClass::addStock($smallId, $totalNum, 0);
  123. $smallInfo = ProductClass::getById($smallId, true);
  124. $sjId = $smallInfo->sjId ?? 0;
  125. $shopId = $smallInfo->shopId ?? 0;
  126. $ptItemId = $smallInfo->itemId ?? 0;
  127. $mainId = $smallInfo->mainId ?? 0;
  128. $oldStock = $stockInfo['oldStock'] ?? 0;
  129. $newStock = $stockInfo['newStock'] ?? 0;
  130. //库存变动记录
  131. $recordData = [];
  132. $recordData['relateName'] = $data['shopAdminName'] ?? '';
  133. $recordData['itemNum'] = $totalNum;
  134. $recordData['sjId'] = $sjId;
  135. $recordData['shopId'] = $shopId;
  136. $recordData['mainId'] = $mainId;
  137. $recordData['orderSn'] = $orderSn;
  138. $recordData['itemId'] = $ptItemId;
  139. $recordData['oldStock'] = $oldStock; //当时库存
  140. $recordData['productId'] = $smallId;
  141. $recordData['newStock'] = $newStock;// 最新库存
  142. $recordData['io'] = 1;
  143. $recordData['remark'] = "拆散{$num}扎 " . $bigName;
  144. StockRecordClass::partItemAddRecord($recordData);
  145. }
  146. }
  147. }
  148. return $order;
  149. }
  150. // 组装 orderItem lqh 2021.1.25
  151. public static function groupOrderItem($productInfoList, $orderSn)
  152. {
  153. $productIds = array_column($productInfoList, 'productId');
  154. $productData = ProductClass::getProductByIds($productIds);
  155. $productData = array_column($productData, NULL, 'id');
  156. //写入详情表
  157. $batchData = [];
  158. foreach ($productInfoList as $v) {
  159. $tmp = [];
  160. $productId = $v['productId'];
  161. $itemId = $productData[$productId]['itemId'];
  162. $tmp['orderSn'] = $orderSn;
  163. $tmp['itemId'] = $itemId;
  164. $tmp['productId'] = $v['productId'];
  165. $unitNum = $productData[$productId]['ratio'] ?? 0;
  166. $tmp['itemStock'] = $v['oldStock'];//当时库存
  167. $tmp['newStock'] = $v['newStock'];//当时库存
  168. $tmp['itemNum'] = ProductClass::mergeItemNum($v['bigNum'], $v['smallNum'], $unitNum);// 数量
  169. $tmp['itemPrice'] = $productData[$productId]['price'] ?? 0; //当时售卖的价格
  170. $tmp['itemCost'] = $productData[$productId]['cost'] ?? 0; //当时成本价
  171. $tmp['cover'] = $productData[$productId]['cover'] ?? '';
  172. $tmp['name'] = $productData[$productId]['name'] ?? '';
  173. $batchData[] = $tmp;
  174. }
  175. return $batchData;
  176. }
  177. //itemInfo 相同product合并处理 [{"classId":5,"productId":3,"bigNum":1,"smallNum":2},{"classId":4,"productId":3,"bigNum":1,"smallNum":5}]
  178. public static function mergeItemInfo($itemInfo)
  179. {
  180. $newItemInfo = [];
  181. foreach ($itemInfo as $v) {
  182. $productId = $v['productId'];
  183. if (isset($newItemInfo[$productId])) {
  184. $newItemInfo[$productId]['bigNum'] = bcadd($v['bigNum'], $newItemInfo[$productId]['bigNum'], 2);
  185. $newItemInfo[$productId]['smallNum'] = bcadd($v['smallNum'], $newItemInfo[$productId]['smallNum'], 2);
  186. } else {
  187. $newItemInfo[$productId] = $v;
  188. }
  189. }
  190. $newItemInfo = array_merge($newItemInfo);
  191. return $newItemInfo;
  192. }
  193. }