| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208 |
- <?php
- namespace bizHd\part\classes;
- use bizHd\base\classes\BaseClass;
- use bizHd\item\classes\UnitChangeClass;
- use bizHd\shop\classes\MainClass;
- use common\components\dict;
- use common\components\orderSn;
- use bizGhs\product\classes\ProductClass;
- use bizGhs\stock\classes\StockRecordClass;
- use Yii;
- class PartClass extends BaseClass
- {
- public static $baseFile = '\bizHd\part\models\Part';
- const STATUS_COMPLETE = 1; //已完成
- //列表
- public static function getPartList($where)
- {
- $data = self::getList('*', $where, 'addTime DESC');
- $list = isset($data['list']) && !empty($data['list']) ? $data['list'] : [];
- if (empty($list)) {
- return $data;
- }
- $data['list'] = $list;
- return $data;
- }
- public static function addOrder($post)
- {
- $mainId = $post['mainId'] ?? 0;
- $data = [];
- $data['status'] = self::STATUS_COMPLETE;
- $orderSn = orderSn::getPartSn();
- $data['orderSn'] = $orderSn;
- $data['sjId'] = $post['sjId'];
- $data['shopId'] = $post['shopId'];
- $data['shopAdminId'] = $post['shopAdminId'] ?? 0;
- $data['shopAdminName'] = $post['shopAdminName'] ?? '';
- //花材列表
- $productInfoList = $post['product'];
- $productInfoList = self::mergeItemInfo($productInfoList);
- $totalNum = 0;
- $totalBig = 0;
- $totalSmall = 0;
- if (!empty($productInfoList)) {
- foreach ($productInfoList as $current) {
- $bigNum = $current['bigNum'] ?? 0;
- $totalNum = bcadd($totalNum, $bigNum, 2);
- $smallNum = $current['smallNum'] ?? 0;
- $ratio = isset($current['ratio']) && $current['ratio'] > 0 ? $current['ratio'] : 1;
- $div = bcdiv($smallNum, $ratio, 2);
- $totalNum = bcadd($totalNum, $div, 2);
- $totalBig = bcadd($totalBig, $bigNum);
- $totalSmall = bcadd($totalSmall, $smallNum);
- }
- }
- $data['bigNum'] = $totalBig;
- $data['smallNum'] = $totalSmall;
- $data['num'] = $totalNum;
- $data['remark'] = $post['remark'] ?? '';
- $data['mainId'] = $mainId;
- //写入订单表
- $order = self::add($data, true);
- foreach ($productInfoList as $k => $v) {
- $stockInfo = ProductClass::decreaseStock($v['productId'], $v['bigNum'], $v['smallNum'], true, false);
- $productInfoList[$k]['oldStock'] = $stockInfo['oldStock'];
- $productInfoList[$k]['newStock'] = $stockInfo['newStock'];
- }
- //写入详情表
- $batchData = self::groupOrderItem($productInfoList, $orderSn);
- $stockItem = [];
- foreach ($batchData as $currentItem) {
- $currentItem['mainId'] = $mainId;
- PartItemClass::add($currentItem);
- $stockItem[] = [
- 'productId' => $currentItem['productId'] ?? 0,
- 'itemId' => $currentItem['itemId'] ?? 0,
- 'itemNum' => $currentItem['itemNum'] ?? 0,
- 'oldStock' => $currentItem['itemStock'] ?? 0,
- 'itemStock' => $currentItem['itemStock'] ?? 0,
- 'newStock' => $currentItem['newStock'] ?? 0,
- 'bigNum' => $currentItem['itemNum'] ?? 0,
- 'smallNum' => 0,
- ];
- }
- $stockData = [];
- $stockData['shopId'] = $data['shopId'];
- $stockData['relateName'] = $data['shopAdminName'] ?? '';
- $stockData['ptStyle'] = $post['ptStyle'] ?? dict::getDict('ptStyle', 'hd');
- $stockData['orderSn'] = $orderSn;
- $stockData['sjId'] = $data['sjId'] ?? 0;
- $stockData['itemInfo'] = $stockItem;
- StockRecordClass::addRecordByOrder($stockData, StockRecordClass::STOCK_RECORD_TYPE_PART);
- $main = MainClass::getLockById($mainId);
- if (empty($main)) {
- util::fail('没有main的信息');
- }
- $totalCost = 0;
- $totalNum = 0;
- foreach ($batchData as $v) {
- $num = $v['itemNum'] ?? 0;
- $cost = $v['itemCost'] ?? 0;
- $totalCost = bcadd($totalCost, bcmul($num, $cost, 2), 2);
- $totalNum = bcadd($totalNum, $num);
- }
- $order->cost = $totalCost;
- $order->save();
- StatPartClass::replace($main, $totalCost, $totalNum);
- //拆散后子花材的库存需要增加,多处要同步修改,关键词part_to_sub_item
- $ids = array_column($productInfoList, 'productId');
- $bigInfoList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, 'id,name,ratio', 'id');
- $map = UnitChangeClass::getAllByCondition(['big' => ['in', $ids]], null, '*', 'big');
- if (!empty($map)) {
- foreach ($productInfoList as $key => $value) {
- $num = $value['bigNum'] ?? 0;
- $productId = $value['productId'] ?? 0;
- $mapInfo = $map[$productId] ?? [];
- $bigInfo = $bigInfoList[$productId] ?? [];
- if (!empty($mapInfo) && !empty($bigInfo)) {
- $bigRatio = $bigInfo['ratio'] ?? 0;
- $bigName = $bigInfo['name'] ?? '';
- $totalNum = bcmul($num, $bigRatio);
- $smallId = $mapInfo['small'] ?? 0;
- $stockInfo = ProductClass::addStock($smallId, $totalNum, 0);
- $smallInfo = ProductClass::getById($smallId, true);
- $sjId = $smallInfo->sjId ?? 0;
- $shopId = $smallInfo->shopId ?? 0;
- $ptItemId = $smallInfo->itemId ?? 0;
- $mainId = $smallInfo->mainId ?? 0;
- $oldStock = $stockInfo['oldStock'] ?? 0;
- $newStock = $stockInfo['newStock'] ?? 0;
- //库存变动记录
- $recordData = [];
- $recordData['relateName'] = $data['shopAdminName'] ?? '';
- $recordData['itemNum'] = $totalNum;
- $recordData['sjId'] = $sjId;
- $recordData['shopId'] = $shopId;
- $recordData['mainId'] = $mainId;
- $recordData['orderSn'] = $orderSn;
- $recordData['itemId'] = $ptItemId;
- $recordData['oldStock'] = $oldStock; //当时库存
- $recordData['productId'] = $smallId;
- $recordData['newStock'] = $newStock;// 最新库存
- $recordData['io'] = 1;
- $recordData['remark'] = "拆散{$num}扎 " . $bigName;
- StockRecordClass::partItemAddRecord($recordData);
- }
- }
- }
- return $order;
- }
- // 组装 orderItem lqh 2021.1.25
- public static function groupOrderItem($productInfoList, $orderSn)
- {
- $productIds = array_column($productInfoList, 'productId');
- $productData = ProductClass::getProductByIds($productIds);
- $productData = array_column($productData, NULL, 'id');
- //写入详情表
- $batchData = [];
- foreach ($productInfoList as $v) {
- $tmp = [];
- $productId = $v['productId'];
- $itemId = $productData[$productId]['itemId'];
- $tmp['orderSn'] = $orderSn;
- $tmp['itemId'] = $itemId;
- $tmp['productId'] = $v['productId'];
- $unitNum = $productData[$productId]['ratio'] ?? 0;
- $tmp['itemStock'] = $v['oldStock'];//当时库存
- $tmp['newStock'] = $v['newStock'];//当时库存
- $tmp['itemNum'] = ProductClass::mergeItemNum($v['bigNum'], $v['smallNum'], $unitNum);// 数量
- $tmp['itemPrice'] = $productData[$productId]['price'] ?? 0; //当时售卖的价格
- $tmp['itemCost'] = $productData[$productId]['cost'] ?? 0; //当时成本价
- $tmp['cover'] = $productData[$productId]['cover'] ?? '';
- $tmp['name'] = $productData[$productId]['name'] ?? '';
- $batchData[] = $tmp;
- }
- return $batchData;
- }
- //itemInfo 相同product合并处理 [{"classId":5,"productId":3,"bigNum":1,"smallNum":2},{"classId":4,"productId":3,"bigNum":1,"smallNum":5}]
- public static function mergeItemInfo($itemInfo)
- {
- $newItemInfo = [];
- foreach ($itemInfo as $v) {
- $productId = $v['productId'];
- if (isset($newItemInfo[$productId])) {
- $newItemInfo[$productId]['bigNum'] = bcadd($v['bigNum'], $newItemInfo[$productId]['bigNum'], 2);
- $newItemInfo[$productId]['smallNum'] = bcadd($v['smallNum'], $newItemInfo[$productId]['smallNum'], 2);
- } else {
- $newItemInfo[$productId] = $v;
- }
- }
- $newItemInfo = array_merge($newItemInfo);
- return $newItemInfo;
- }
- }
|