OrderItemClass.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <?php
  2. namespace bizGhs\order\classes;
  3. use biz\product\classes\XjClass;
  4. use biz\stat\classes\StatBookClass;
  5. use bizGhs\custom\classes\CustomClass;
  6. use bizGhs\ghs\classes\GhsClass;
  7. use bizGhs\product\classes\ProductClass;
  8. use bizGhs\stock\classes\StockRecordClass;
  9. use bizHd\purchase\classes\PurchaseClass;
  10. use bizHd\purchase\classes\PurchaseItemClass;
  11. use common\components\dict;
  12. use common\components\imgUtil;
  13. use common\components\orderSn;
  14. use common\components\util;
  15. use Yii;
  16. use bizGhs\base\classes\BaseClass;
  17. class OrderItemClass extends BaseClass
  18. {
  19. public static $baseFile = '\bizGhs\order\models\OrderItem';
  20. //预订单删除花材项 ssh 20240123
  21. public static function bookDelItem($order, $smallId)
  22. {
  23. }
  24. //预订单添加花材项 ssh 20240123
  25. public static function bookAddItem($order, $itemList)
  26. {
  27. $orderSn = $order->orderSn ?? '';
  28. $addIds = array_column($itemList, 'productId');
  29. $existList = self::getAllByCondition(['orderSn' => $orderSn], null, '*');
  30. foreach ($existList as $ex) {
  31. $productId = $ex['productId'] ?? 0;
  32. $name = $ex['name'] ?? '';
  33. if (in_array($productId, $addIds)) {
  34. util::fail($name . ' 已经存在');
  35. }
  36. }
  37. $addInfo = ProductClass::getByIds($addIds, null, 'id');
  38. if (empty($addInfo)) {
  39. util::fail('没有花材');
  40. }
  41. $productList = [];
  42. foreach ($itemList as $item) {
  43. $num = $item['num'] ?? 0;
  44. $productId = $item['productId'] ?? 0;
  45. $info = $addInfo[$productId] ?? [];
  46. $current = [];
  47. $current['name'] = $info['name'] ?? '';
  48. $current['cover'] = $info['cover'] ?? '';
  49. $current['orderSn'] = $orderSn;
  50. $ptItemId = $info['itemId'] ?? 0;
  51. $current['itemId'] = $ptItemId;
  52. $current['productId'] = $productId;
  53. $classId = $info['classId'] ?? 0
  54. $current['classId'] = $classId;
  55. $current['variety'] = $info['variety'] ?? 0;
  56. $current['shopId'] = $info['shopId'] ?? 0;
  57. $current['mainId'] = $info['mainId'] ?? 0;
  58. $current['smallUnit'] = $info['smallUnit'] ?? 0;
  59. $current['bigUnit'] = $info['bigUnit'] ?? 0;
  60. $current['price'] = 0;
  61. $current['unitWeight'] = $info['weight'] ?? 0;
  62. $current['cost'] = $info['cost'] ?? 0;
  63. $current['num'] = $num;
  64. $current['bigNum'] = $num;
  65. $current['preNum'] = $num;
  66. $current['preBigNum'] = $num;
  67. $current['ratio'] = $info['ratio'] ?? 20;
  68. $current['variety'] = $info['variety'] ?? 0;
  69. $current['xhNum'] = $num;
  70. $current['xhUnitName'] = $info['bigUnit'] ?? '';
  71. $current['xhUnitType'] = 0;
  72. $current['xhPreNum'] = $num;
  73. $current['xhPreUnitName'] = $info['bigUnit'] ?? '';
  74. $current['xhPreUnitType'] = 0;
  75. self::add($current);
  76. $productList[] = [
  77. 'productId' => $productId,
  78. 'bigNum' => $num,
  79. 'smallNum' => 0,
  80. 'classId' => $classId,
  81. 'itemId' => $ptItemId,
  82. 'userPrice' => 0,
  83. ];
  84. }
  85. $shopId = $order->shopId ?? 0;
  86. $sjId = $order->sjId ?? 0;
  87. $mainId = $order->mainId ?? 0;
  88. $ghsId = $order->ghsId ?? 0;
  89. $ghs = GhsClass::getById($ghsId, true);
  90. if (empty($ghs)) {
  91. util::fail('供货商无效');
  92. }
  93. $productList = ProductClass::toggleProductList($productList, $shopId, $sjId, $mainId, $ghs);
  94. $cgId = $order->purchaseId ?? 0;
  95. $cg = PurchaseClass::getById($cgId, true);
  96. if (empty($cg)) {
  97. util::fail('没有采购信息');
  98. }
  99. $cgProductIds = array_column($productList, 'productId');
  100. $cgProductInfoList = ProductClass::getByIds($cgProductIds, null, 'id');
  101. $cgOrderSn = $cg->orderSn ?? '';
  102. foreach ($productList as $key => $val) {
  103. $productId = $val['productId'] ?? 0;
  104. $num = $val['num'] ?? 0;
  105. $ghsProductId = $val['ghsProductId'] ?? 0;
  106. $thisInfo = $cgProductInfoList[$productId] ?? [];
  107. $newItem = [];
  108. $newItem['name'] = $thisInfo['name'] ?? '';
  109. $newItem['cover'] = $thisInfo['cover'] ?? '';
  110. $newItem['orderSn'] = $cgOrderSn;
  111. $newItem['productId'] = $productId;
  112. $newItem['itemId'] = $thisInfo['itemId'] ?? 0;
  113. $newItem['mainId'] = $cg->mainId ?? 0;
  114. $newItem['ghsId'] = $ghsId;
  115. $newItem['itemNum'] = $num;
  116. $newItem['preItemNum'] = $num;
  117. $newItem['ghsProductId'] = $ghsProductId;
  118. $newItem['ratio'] = $thisInfo['ratio'] ?? 0
  119. $newItem['price'] = 0;
  120. $newItem['variety'] = $thisInfo['variety'] ?? 0
  121. $newItem['unitWeight'] = $thisInfo['weight'] ?? 0
  122. $newItem['xhNum'] = $num;
  123. $newItem['xhUnitName'] = $thisInfo['bigUnit'] ?? '';
  124. $newItem['xhUnitType'] = 0;
  125. $newItem['xhPreNum'] = $num;
  126. $newItem['xhPreUnitName'] = $thisInfo['bigUnit'] ?? '';
  127. $newItem['xhPreUnitType'] = 0;
  128. PurchaseItemClass::add($newItem);
  129. }
  130. }
  131. public static function giveItem($order, $orderItem, $product, $num, $data)
  132. {
  133. $productId = $product->id ?? 0;
  134. $ptItemId = $product->itemId ?? 0;
  135. $name = $product->name ?? '';
  136. $cover = $product->cover ?? '';
  137. $ratio = $product->ratio ?? 20;
  138. $ratioType = $product->ratioType ?? 0;
  139. $mainId = $product->mainId ?? 0;
  140. $targetId = $order->id ?? 0;
  141. $targetSonId = $orderItem->id ?? 0;
  142. $orderItem->giveNum = bcadd($orderItem->giveNum, $num);
  143. $orderItem->save();
  144. $orderSn = orderSn::getGiveSn();
  145. $giveData = [
  146. 'orderSn' => $orderSn,
  147. 'mainId' => $mainId,
  148. 'productId' => $productId,
  149. 'name' => $name,
  150. 'cover' => $cover,
  151. 'ratio' => $ratio,
  152. 'ratioType' => $ratioType,
  153. 'ptItemId' => $ptItemId,
  154. 'giveNum' => $num,
  155. 'targetId' => $targetId,
  156. 'targetSonId' => $targetSonId,
  157. 'staffId' => $data['staffId'] ?? 0,
  158. 'staffName' => $data['staffName'] ?? '',
  159. 'ghsId' => $order->ghsId ?? 0,
  160. 'customId' => $order->customId ?? 0,
  161. 'customName' => $order->customName ?? '',
  162. 'customNamePy' => $order->customNamePy ?? '',
  163. 'customAvatar' => $order->customAvatar ?? '',
  164. 'customMobile' => $order->customMobile ?? '',
  165. ];
  166. $giveInfo = GiveClass::add($giveData, true);
  167. $stockInfo = ProductClass::decreaseStock($productId, $num, 0, true);
  168. $oldStock = $stockInfo['oldStock'];
  169. $newStock = $stockInfo['newStock'];
  170. $stockItem = [[
  171. 'productId' => $productId,
  172. 'itemId' => $ptItemId,
  173. 'itemNum' => $num,
  174. 'oldStock' => $oldStock,
  175. 'itemStock' => $oldStock,
  176. 'newStock' => $newStock,
  177. 'bigNum' => $num,
  178. 'smallNum' => 0,
  179. ]];
  180. $stockData = [];
  181. $stockData['shopId'] = $data['shopId'];
  182. $stockData['relateName'] = $data['staffName'] ?? '';
  183. $stockData['ptStyle'] = dict::getDict('ptStyle', 'ghs');
  184. $stockData['orderSn'] = $orderSn;
  185. $stockData['sjId'] = $data['sjId'] ?? 0;
  186. $stockData['itemInfo'] = $stockItem;
  187. $stockData['relateName'] = $order->customName ?? '';
  188. StockRecordClass::addRecordByOrder($stockData, StockRecordClass::STOCK_RECORD_TYPE_GIVE);
  189. return $giveInfo;
  190. }
  191. //获取订单的花材 ssh 2021.3.2
  192. public static function getOrderItem($orderSn)
  193. {
  194. $list = self::getAllByCondition(['orderSn' => $orderSn], null, '*');
  195. return self::groupInfo($list);
  196. }
  197. //组合信息 ssh 2021.3.2
  198. public static function groupInfo($list)
  199. {
  200. if (empty($list)) {
  201. return [];
  202. }
  203. $productIds = array_unique(array_filter(array_column($list, 'productId')));
  204. $productData = ProductClass::getProductByIds($productIds);
  205. $productData = array_column($productData, NULL, 'id');
  206. foreach ($list as $key => $val) {
  207. $shortCover = $val['cover'] ?? '';
  208. $list[$key]['cover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  209. $list[$key]['bigCover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  210. if (!$val['ratio']) {
  211. $ratio = $productData[$val['productId']]['ratio'] ?? 1;
  212. $list[$key]['ratio'] = $ratio;
  213. }
  214. //保存实时成本,预订单到货时使用
  215. $list[$key]['currentCost'] = $productData[$val['productId']]['cost'] ?? 0;
  216. }
  217. return $list;
  218. }
  219. //添加修改花材 ssh 2021.1.22
  220. public static function replaceItem($orderSn, $product, $post = null, $stockMayChange = false, $moreParams = [])
  221. {
  222. $customId = $post['customId'] ?? 0;
  223. $custom = CustomClass::getById($customId, true);
  224. $level = $custom->level ?? 0;
  225. $live = isset($custom->live) ? $custom->live : 1;
  226. $respond = ProductClass::formatProductInfo($product, $level, $live, $moreParams);
  227. $product = $respond['product'];
  228. $weight = 0;
  229. $mainId = $post['mainId'] ?? 0;
  230. self::deleteByCondition(['orderSn' => $orderSn]);
  231. foreach ($product as $key => $val) {
  232. $val['orderSn'] = $orderSn;
  233. $val['mainId'] = $mainId;
  234. $item = self::add($val, true);
  235. $currentId = $item->id ?? 0;
  236. $currentPtItemId = $item->itemId ?? 0;
  237. $unitPrice = $val['unitPrice'] ?? 0;
  238. if (isset($post['xj'][$currentPtItemId]) && !empty($post['xj'][$currentPtItemId])) {
  239. $xjArr = $post['xj'][$currentPtItemId];
  240. if (is_array($xjArr)) {
  241. $ids = array_column($xjArr, 'id');
  242. $sjInfo = XjClass::getByIds($ids, null, 'id');
  243. $xjData = [];
  244. foreach ($xjArr as $xjItem) {
  245. $id = $xjItem['id'] ?? 0;
  246. $num = $xjItem['num'] ?? 0;
  247. $name = $sjInfo[$id]['name'] ?? '';
  248. $cover = $sjInfo[$id]['cover'] ?? '';
  249. $stock = $sjInfo[$id]['stock'] ?? -1;
  250. if ($stock == -1) {
  251. } else {
  252. if ($stock < $num) {
  253. util::fail($name . ' 库存不足 ' . $num);
  254. } else {
  255. $newStock = bcsub($stock, $num);
  256. $status = $newStock == 0 ? 2 : 1;
  257. XjClass::updateById($id, ['stock' => $newStock, 'status' => $status]);
  258. }
  259. }
  260. $xjData[] = ['xjId' => $id, 'name' => $name, 'ptItemId' => $currentPtItemId,
  261. 'num' => $num, 'price' => $unitPrice, 'itemId' => $currentId, 'cover' => $cover];
  262. }
  263. OrderXjClass::batchAdd($xjData);
  264. $item->variety = 1;
  265. $item->save();
  266. }
  267. }
  268. $productId = $val['productId'];
  269. $ratio = $val['ratio'] ?? 0;
  270. $currentWeight = $val['weight'];
  271. $bigNum = $val['bigNum'] ?? 0;
  272. $smallNum = $val['smallNum'] ?? 0;
  273. $itemNum = ProductClass::mergeItemNum($bigNum, $smallNum, $ratio);
  274. $w = bcmul($itemNum, $currentWeight, 2);
  275. $weight = bcadd($w, $weight, 2);
  276. if ($stockMayChange == true) {
  277. //小单位花材开单暂时不扣库存
  278. if (intval($itemNum) > 0) {
  279. $checkStock = true;
  280. //如果是虚拟客户,说明自己也是虚拟批发店,虚拟批发店不用考虑库存
  281. if ($live == 0) {
  282. $checkStock = false;
  283. }
  284. $stockInfo = ProductClass::decreaseStock($productId, $bigNum, $smallNum, $checkStock);
  285. $recordData = [];
  286. $recordData['sjId'] = $post['sjId'] ?? 0;
  287. $recordData['shopId'] = $post['shopId'] ?? 0;
  288. $recordData['mainId'] = $post['mainId'] ?? 0;
  289. $recordData['itemId'] = $val['itemId'] ?? 0;
  290. $recordData['itemNum'] = intval($itemNum);
  291. $recordData['oldStock'] = $stockInfo['oldStock'];
  292. $recordData['newStock'] = $stockInfo['newStock'];
  293. $recordData['productId'] = $productId;
  294. $recordData['orderSn'] = $orderSn;
  295. $recordData['relateName'] = $post['customName'] ?? '';
  296. $recordData['ptStyle'] = dict::getDict('ptStyle', 'ghs');
  297. StockRecordClass::addSellStockOrderRecord($recordData);
  298. }
  299. }
  300. }
  301. $respond['weight'] = $weight;
  302. return $respond;
  303. }
  304. public static function getOrderInfoList($where)
  305. {
  306. $respond = self::getList('orderSn,id', $where, 'id DESC');
  307. if (isset($respond['list']) && !empty($respond['list'])) {
  308. $orderSnData = array_unique(array_filter(array_column($respond['list'], 'orderSn')));
  309. $orderInfoList = OrderClass::getOrderInfoBySnList($orderSnData);
  310. foreach ($respond['list'] as $key => $val) {
  311. $orderSn = $val['orderSn'] ?? '';
  312. $orderInfo = $orderInfoList[$orderSn] ?? [];
  313. $respond['list'][$key]['orderInfo'] = $orderInfo;
  314. }
  315. }
  316. return $respond;
  317. }
  318. }