PartClass.php 8.8 KB

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