| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351 |
- <?php
- namespace bizGhs\order\classes;
- use biz\product\classes\XjClass;
- use biz\stat\classes\StatBookClass;
- use bizGhs\custom\classes\CustomClass;
- use bizGhs\ghs\classes\GhsClass;
- use bizGhs\product\classes\ProductClass;
- use bizGhs\stock\classes\StockRecordClass;
- use bizHd\purchase\classes\PurchaseClass;
- use bizHd\purchase\classes\PurchaseItemClass;
- use common\components\dict;
- use common\components\imgUtil;
- use common\components\orderSn;
- use common\components\util;
- use Yii;
- use bizGhs\base\classes\BaseClass;
- class OrderItemClass extends BaseClass
- {
- public static $baseFile = '\bizGhs\order\models\OrderItem';
- //预订单删除花材项 ssh 20240123
- public static function bookDelItem($order, $smallId)
- {
- }
- //预订单添加花材项 ssh 20240123
- public static function bookAddItem($order, $itemList)
- {
- $orderSn = $order->orderSn ?? '';
- $addIds = array_column($itemList, 'productId');
- $existList = self::getAllByCondition(['orderSn' => $orderSn], null, '*');
- foreach ($existList as $ex) {
- $productId = $ex['productId'] ?? 0;
- $name = $ex['name'] ?? '';
- if (in_array($productId, $addIds)) {
- util::fail($name . ' 已经存在');
- }
- }
- $addInfo = ProductClass::getByIds($addIds, null, 'id');
- if (empty($addInfo)) {
- util::fail('没有花材');
- }
- $productList = [];
- foreach ($itemList as $item) {
- $num = $item['num'] ?? 0;
- $productId = $item['productId'] ?? 0;
- $info = $addInfo[$productId] ?? [];
- $current = [];
- $current['name'] = $info['name'] ?? '';
- $current['cover'] = $info['cover'] ?? '';
- $current['orderSn'] = $orderSn;
- $ptItemId = $info['itemId'] ?? 0;
- $current['itemId'] = $ptItemId;
- $current['productId'] = $productId;
- $classId = $info['classId'] ?? 0
- $current['classId'] = $classId;
- $current['variety'] = $info['variety'] ?? 0;
- $current['shopId'] = $info['shopId'] ?? 0;
- $current['mainId'] = $info['mainId'] ?? 0;
- $current['smallUnit'] = $info['smallUnit'] ?? 0;
- $current['bigUnit'] = $info['bigUnit'] ?? 0;
- $current['price'] = 0;
- $current['unitWeight'] = $info['weight'] ?? 0;
- $current['cost'] = $info['cost'] ?? 0;
- $current['num'] = $num;
- $current['bigNum'] = $num;
- $current['preNum'] = $num;
- $current['preBigNum'] = $num;
- $current['ratio'] = $info['ratio'] ?? 20;
- $current['variety'] = $info['variety'] ?? 0;
- $current['xhNum'] = $num;
- $current['xhUnitName'] = $info['bigUnit'] ?? '';
- $current['xhUnitType'] = 0;
- $current['xhPreNum'] = $num;
- $current['xhPreUnitName'] = $info['bigUnit'] ?? '';
- $current['xhPreUnitType'] = 0;
- self::add($current);
- $productList[] = [
- 'productId' => $productId,
- 'bigNum' => $num,
- 'smallNum' => 0,
- 'classId' => $classId,
- 'itemId' => $ptItemId,
- 'userPrice' => 0,
- ];
- }
- $shopId = $order->shopId ?? 0;
- $sjId = $order->sjId ?? 0;
- $mainId = $order->mainId ?? 0;
- $ghsId = $order->ghsId ?? 0;
- $ghs = GhsClass::getById($ghsId, true);
- if (empty($ghs)) {
- util::fail('供货商无效');
- }
- $productList = ProductClass::toggleProductList($productList, $shopId, $sjId, $mainId, $ghs);
- $cgId = $order->purchaseId ?? 0;
- $cg = PurchaseClass::getById($cgId, true);
- if (empty($cg)) {
- util::fail('没有采购信息');
- }
- $cgProductIds = array_column($productList, 'productId');
- $cgProductInfoList = ProductClass::getByIds($cgProductIds, null, 'id');
- $cgOrderSn = $cg->orderSn ?? '';
- foreach ($productList as $key => $val) {
- $productId = $val['productId'] ?? 0;
- $num = $val['num'] ?? 0;
- $ghsProductId = $val['ghsProductId'] ?? 0;
- $thisInfo = $cgProductInfoList[$productId] ?? [];
- $newItem = [];
- $newItem['name'] = $thisInfo['name'] ?? '';
- $newItem['cover'] = $thisInfo['cover'] ?? '';
- $newItem['orderSn'] = $cgOrderSn;
- $newItem['productId'] = $productId;
- $newItem['itemId'] = $thisInfo['itemId'] ?? 0;
- $newItem['mainId'] = $cg->mainId ?? 0;
- $newItem['ghsId'] = $ghsId;
- $newItem['itemNum'] = $num;
- $newItem['preItemNum'] = $num;
- $newItem['ghsProductId'] = $ghsProductId;
- $newItem['ratio'] = $thisInfo['ratio'] ?? 0
- $newItem['price'] = 0;
- $newItem['variety'] = $thisInfo['variety'] ?? 0
- $newItem['unitWeight'] = $thisInfo['weight'] ?? 0
- $newItem['xhNum'] = $num;
- $newItem['xhUnitName'] = $thisInfo['bigUnit'] ?? '';
- $newItem['xhUnitType'] = 0;
- $newItem['xhPreNum'] = $num;
- $newItem['xhPreUnitName'] = $thisInfo['bigUnit'] ?? '';
- $newItem['xhPreUnitType'] = 0;
- PurchaseItemClass::add($newItem);
- }
- }
- public static function giveItem($order, $orderItem, $product, $num, $data)
- {
- $productId = $product->id ?? 0;
- $ptItemId = $product->itemId ?? 0;
- $name = $product->name ?? '';
- $cover = $product->cover ?? '';
- $ratio = $product->ratio ?? 20;
- $ratioType = $product->ratioType ?? 0;
- $mainId = $product->mainId ?? 0;
- $targetId = $order->id ?? 0;
- $targetSonId = $orderItem->id ?? 0;
- $orderItem->giveNum = bcadd($orderItem->giveNum, $num);
- $orderItem->save();
- $orderSn = orderSn::getGiveSn();
- $giveData = [
- 'orderSn' => $orderSn,
- 'mainId' => $mainId,
- 'productId' => $productId,
- 'name' => $name,
- 'cover' => $cover,
- 'ratio' => $ratio,
- 'ratioType' => $ratioType,
- 'ptItemId' => $ptItemId,
- 'giveNum' => $num,
- 'targetId' => $targetId,
- 'targetSonId' => $targetSonId,
- 'staffId' => $data['staffId'] ?? 0,
- 'staffName' => $data['staffName'] ?? '',
- 'ghsId' => $order->ghsId ?? 0,
- 'customId' => $order->customId ?? 0,
- 'customName' => $order->customName ?? '',
- 'customNamePy' => $order->customNamePy ?? '',
- 'customAvatar' => $order->customAvatar ?? '',
- 'customMobile' => $order->customMobile ?? '',
- ];
- $giveInfo = GiveClass::add($giveData, true);
- $stockInfo = ProductClass::decreaseStock($productId, $num, 0, true);
- $oldStock = $stockInfo['oldStock'];
- $newStock = $stockInfo['newStock'];
- $stockItem = [[
- 'productId' => $productId,
- 'itemId' => $ptItemId,
- 'itemNum' => $num,
- 'oldStock' => $oldStock,
- 'itemStock' => $oldStock,
- 'newStock' => $newStock,
- 'bigNum' => $num,
- 'smallNum' => 0,
- ]];
- $stockData = [];
- $stockData['shopId'] = $data['shopId'];
- $stockData['relateName'] = $data['staffName'] ?? '';
- $stockData['ptStyle'] = dict::getDict('ptStyle', 'ghs');
- $stockData['orderSn'] = $orderSn;
- $stockData['sjId'] = $data['sjId'] ?? 0;
- $stockData['itemInfo'] = $stockItem;
- $stockData['relateName'] = $order->customName ?? '';
- StockRecordClass::addRecordByOrder($stockData, StockRecordClass::STOCK_RECORD_TYPE_GIVE);
- return $giveInfo;
- }
- //获取订单的花材 ssh 2021.3.2
- public static function getOrderItem($orderSn)
- {
- $list = self::getAllByCondition(['orderSn' => $orderSn], null, '*');
- return self::groupInfo($list);
- }
- //组合信息 ssh 2021.3.2
- public static function groupInfo($list)
- {
- if (empty($list)) {
- return [];
- }
- $productIds = array_unique(array_filter(array_column($list, 'productId')));
- $productData = ProductClass::getProductByIds($productIds);
- $productData = array_column($productData, NULL, 'id');
- foreach ($list as $key => $val) {
- $shortCover = $val['cover'] ?? '';
- $list[$key]['cover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
- $list[$key]['bigCover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
- if (!$val['ratio']) {
- $ratio = $productData[$val['productId']]['ratio'] ?? 1;
- $list[$key]['ratio'] = $ratio;
- }
- //保存实时成本,预订单到货时使用
- $list[$key]['currentCost'] = $productData[$val['productId']]['cost'] ?? 0;
- }
- return $list;
- }
- //添加修改花材 ssh 2021.1.22
- public static function replaceItem($orderSn, $product, $post = null, $stockMayChange = false, $moreParams = [])
- {
- $customId = $post['customId'] ?? 0;
- $custom = CustomClass::getById($customId, true);
- $level = $custom->level ?? 0;
- $live = isset($custom->live) ? $custom->live : 1;
- $respond = ProductClass::formatProductInfo($product, $level, $live, $moreParams);
- $product = $respond['product'];
- $weight = 0;
- $mainId = $post['mainId'] ?? 0;
- self::deleteByCondition(['orderSn' => $orderSn]);
- foreach ($product as $key => $val) {
- $val['orderSn'] = $orderSn;
- $val['mainId'] = $mainId;
- $item = self::add($val, true);
- $currentId = $item->id ?? 0;
- $currentPtItemId = $item->itemId ?? 0;
- $unitPrice = $val['unitPrice'] ?? 0;
- if (isset($post['xj'][$currentPtItemId]) && !empty($post['xj'][$currentPtItemId])) {
- $xjArr = $post['xj'][$currentPtItemId];
- if (is_array($xjArr)) {
- $ids = array_column($xjArr, 'id');
- $sjInfo = XjClass::getByIds($ids, null, 'id');
- $xjData = [];
- foreach ($xjArr as $xjItem) {
- $id = $xjItem['id'] ?? 0;
- $num = $xjItem['num'] ?? 0;
- $name = $sjInfo[$id]['name'] ?? '';
- $cover = $sjInfo[$id]['cover'] ?? '';
- $stock = $sjInfo[$id]['stock'] ?? -1;
- if ($stock == -1) {
- } else {
- if ($stock < $num) {
- util::fail($name . ' 库存不足 ' . $num);
- } else {
- $newStock = bcsub($stock, $num);
- $status = $newStock == 0 ? 2 : 1;
- XjClass::updateById($id, ['stock' => $newStock, 'status' => $status]);
- }
- }
- $xjData[] = ['xjId' => $id, 'name' => $name, 'ptItemId' => $currentPtItemId,
- 'num' => $num, 'price' => $unitPrice, 'itemId' => $currentId, 'cover' => $cover];
- }
- OrderXjClass::batchAdd($xjData);
- $item->variety = 1;
- $item->save();
- }
- }
- $productId = $val['productId'];
- $ratio = $val['ratio'] ?? 0;
- $currentWeight = $val['weight'];
- $bigNum = $val['bigNum'] ?? 0;
- $smallNum = $val['smallNum'] ?? 0;
- $itemNum = ProductClass::mergeItemNum($bigNum, $smallNum, $ratio);
- $w = bcmul($itemNum, $currentWeight, 2);
- $weight = bcadd($w, $weight, 2);
- if ($stockMayChange == true) {
- //小单位花材开单暂时不扣库存
- if (intval($itemNum) > 0) {
- $checkStock = true;
- //如果是虚拟客户,说明自己也是虚拟批发店,虚拟批发店不用考虑库存
- if ($live == 0) {
- $checkStock = false;
- }
- $stockInfo = ProductClass::decreaseStock($productId, $bigNum, $smallNum, $checkStock);
- $recordData = [];
- $recordData['sjId'] = $post['sjId'] ?? 0;
- $recordData['shopId'] = $post['shopId'] ?? 0;
- $recordData['mainId'] = $post['mainId'] ?? 0;
- $recordData['itemId'] = $val['itemId'] ?? 0;
- $recordData['itemNum'] = intval($itemNum);
- $recordData['oldStock'] = $stockInfo['oldStock'];
- $recordData['newStock'] = $stockInfo['newStock'];
- $recordData['productId'] = $productId;
- $recordData['orderSn'] = $orderSn;
- $recordData['relateName'] = $post['customName'] ?? '';
- $recordData['ptStyle'] = dict::getDict('ptStyle', 'ghs');
- StockRecordClass::addSellStockOrderRecord($recordData);
- }
- }
- }
- $respond['weight'] = $weight;
- return $respond;
- }
- public static function getOrderInfoList($where)
- {
- $respond = self::getList('orderSn,id', $where, 'id DESC');
- if (isset($respond['list']) && !empty($respond['list'])) {
- $orderSnData = array_unique(array_filter(array_column($respond['list'], 'orderSn')));
- $orderInfoList = OrderClass::getOrderInfoBySnList($orderSnData);
- foreach ($respond['list'] as $key => $val) {
- $orderSn = $val['orderSn'] ?? '';
- $orderInfo = $orderInfoList[$orderSn] ?? [];
- $respond['list'][$key]['orderInfo'] = $orderInfo;
- }
- }
- return $respond;
- }
- }
|