PartClass.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. namespace bizHd\part\classes;
  3. use bizHd\base\classes\BaseClass;
  4. use bizHd\shop\classes\MainClass;
  5. use common\components\dict;
  6. use common\components\orderSn;
  7. use bizGhs\product\classes\ProductClass;
  8. use bizGhs\stock\classes\StockRecordClass;
  9. use Yii;
  10. class PartClass extends BaseClass
  11. {
  12. public static $baseFile = '\bizHd\part\models\Part';
  13. const STATUS_COMPLETE = 1; //已完成
  14. //列表
  15. public static function getPartList($where)
  16. {
  17. $data = self::getList('*', $where, 'addTime DESC');
  18. $list = isset($data['list']) && !empty($data['list']) ? $data['list'] : [];
  19. if (empty($list)) {
  20. return $data;
  21. }
  22. $data['list'] = $list;
  23. return $data;
  24. }
  25. public static function addOrder($post)
  26. {
  27. $mainId = $post['mainId'] ?? 0;
  28. $data = [];
  29. $data['status'] = self::STATUS_COMPLETE;
  30. $orderSn = orderSn::getPartSn();
  31. $data['orderSn'] = $orderSn;
  32. $data['sjId'] = $post['sjId'];
  33. $data['shopId'] = $post['shopId'];
  34. $data['shopAdminId'] = $post['shopAdminId'] ?? 0;
  35. $data['shopAdminName'] = $post['shopAdminName'] ?? '';
  36. //花材列表
  37. $productInfoList = $post['product'];
  38. $productInfoList = self::mergeItemInfo($productInfoList);
  39. $totalNum = 0;
  40. $totalBig = 0;
  41. $totalSmall = 0;
  42. if (!empty($productInfoList)) {
  43. foreach ($productInfoList as $current) {
  44. $bigNum = $current['bigNum'] ?? 0;
  45. $totalNum = bcadd($totalNum, $bigNum, 2);
  46. $smallNum = $current['smallNum'] ?? 0;
  47. $ratio = isset($current['ratio']) && $current['ratio'] > 0 ? $current['ratio'] : 1;
  48. $div = bcdiv($smallNum, $ratio, 2);
  49. $totalNum = bcadd($totalNum, $div, 2);
  50. $totalBig = bcadd($totalBig, $bigNum);
  51. $totalSmall = bcadd($totalSmall, $smallNum);
  52. }
  53. }
  54. $data['bigNum'] = $totalBig;
  55. $data['smallNum'] = $totalSmall;
  56. $data['num'] = $totalNum;
  57. $data['remark'] = $post['remark'] ?? '';
  58. $data['mainId'] = $mainId;
  59. //写入订单表
  60. $order = self::add($data, true);
  61. foreach ($productInfoList as $k => $v) {
  62. $stockInfo = ProductClass::decreaseStock($v['productId'], $v['bigNum'], $v['smallNum'], true, false);
  63. $productInfoList[$k]['oldStock'] = $stockInfo['oldStock'];
  64. $productInfoList[$k]['newStock'] = $stockInfo['newStock'];
  65. }
  66. //写入详情表
  67. $batchData = self::groupOrderItem($productInfoList, $orderSn);
  68. $stockItem = [];
  69. foreach ($batchData as $currentItem) {
  70. $currentItem['mainId'] = $mainId;
  71. PartItemClass::add($currentItem);
  72. $stockItem[] = [
  73. 'productId' => $currentItem['productId'] ?? 0,
  74. 'itemId' => $currentItem['itemId'] ?? 0,
  75. 'itemNum' => $currentItem['itemNum'] ?? 0,
  76. 'oldStock' => $currentItem['itemStock'] ?? 0,
  77. 'itemStock' => $currentItem['itemStock'] ?? 0,
  78. 'newStock' => $currentItem['newStock'] ?? 0,
  79. 'bigNum' => $currentItem['itemNum'] ?? 0,
  80. 'smallNum' => 0,
  81. ];
  82. }
  83. $stockData = [];
  84. $stockData['shopId'] = $data['shopId'];
  85. $stockData['relateName'] = $data['shopAdminName'] ?? '';
  86. $stockData['ptStyle'] = $post['ptStyle'] ?? dict::getDict('ptStyle', 'hd');
  87. $stockData['orderSn'] = $orderSn;
  88. $stockData['sjId'] = $data['sjId'] ?? 0;
  89. $stockData['itemInfo'] = $stockItem;
  90. StockRecordClass::addRecordByOrder($stockData, StockRecordClass::STOCK_RECORD_TYPE_PART);
  91. $main = MainClass::getLockById($mainId);
  92. if (empty($main)) {
  93. util::fail('没有main信息29');
  94. }
  95. $totalCost = 0;
  96. $totalNum = 0;
  97. foreach ($batchData as $v) {
  98. $num = $v['itemNum'] ?? 0;
  99. $cost = $v['itemCost'] ?? 0;
  100. $totalCost = bcadd($totalCost, bcmul($num, $cost, 2), 2);
  101. $totalNum = bcadd($totalNum, $num);
  102. }
  103. $order->cost = $totalCost;
  104. $order->save();
  105. StatPartClass::replace($main, $totalCost, $totalNum);
  106. return $order;
  107. }
  108. // 组装 orderItem lqh 2021.1.25
  109. public static function groupOrderItem($productInfoList, $orderSn)
  110. {
  111. $productIds = array_column($productInfoList, 'productId');
  112. $productData = ProductClass::getProductByIds($productIds);
  113. $productData = array_column($productData, NULL, 'id');
  114. //写入详情表
  115. $batchData = [];
  116. foreach ($productInfoList as $v) {
  117. $tmp = [];
  118. $productId = $v['productId'];
  119. $itemId = $productData[$productId]['itemId'];
  120. $tmp['orderSn'] = $orderSn;
  121. $tmp['itemId'] = $itemId;
  122. $tmp['productId'] = $v['productId'];
  123. $unitNum = $productData[$productId]['ratio'] ?? 0;
  124. $tmp['itemStock'] = $v['oldStock'];//当时库存
  125. $tmp['newStock'] = $v['newStock'];//当时库存
  126. $tmp['itemNum'] = ProductClass::mergeItemNum($v['bigNum'], $v['smallNum'], $unitNum);// 数量
  127. $tmp['itemPrice'] = $productData[$productId]['price'] ?? 0; //当时售卖的价格
  128. $tmp['itemCost'] = $productData[$productId]['cost'] ?? 0; //当时成本价
  129. $tmp['cover'] = $productData[$productId]['cover'] ?? '';
  130. $tmp['name'] = $productData[$productId]['name'] ?? '';
  131. $batchData[] = $tmp;
  132. }
  133. return $batchData;
  134. }
  135. //itemInfo 相同product合并处理 [{"classId":5,"productId":3,"bigNum":1,"smallNum":2},{"classId":4,"productId":3,"bigNum":1,"smallNum":5}]
  136. public static function mergeItemInfo($itemInfo)
  137. {
  138. $newItemInfo = [];
  139. foreach ($itemInfo as $v) {
  140. $productId = $v['productId'];
  141. if (isset($newItemInfo[$productId])) {
  142. $newItemInfo[$productId]['bigNum'] = bcadd($v['bigNum'], $newItemInfo[$productId]['bigNum'], 2);
  143. $newItemInfo[$productId]['smallNum'] = bcadd($v['smallNum'], $newItemInfo[$productId]['smallNum'], 2);
  144. } else {
  145. $newItemInfo[$productId] = $v;
  146. }
  147. }
  148. $newItemInfo = array_merge($newItemInfo);
  149. return $newItemInfo;
  150. }
  151. }