|
|
@@ -0,0 +1,157 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace bizGhs\order\classes;
|
|
|
+
|
|
|
+use biz\shop\classes\ShopCapitalClass;
|
|
|
+use biz\shop\classes\ShopClass;
|
|
|
+use biz\stat\classes\StatOutClass;
|
|
|
+use biz\stat\classes\StatStockInClass;
|
|
|
+use biz\stat\classes\StatStockOutClass;
|
|
|
+use bizGhs\order\traits\OrderTrait;
|
|
|
+use bizGhs\product\classes\ProductClass;
|
|
|
+use bizGhs\stock\classes\StockRecordClass;
|
|
|
+use bizHd\stat\classes\StatIncomeClass;
|
|
|
+use common\components\dict;
|
|
|
+use common\components\orderSn;
|
|
|
+use common\components\util;
|
|
|
+use Yii;
|
|
|
+use bizGhs\base\classes\BaseClass;
|
|
|
+
|
|
|
+class StockWastageOrderClass extends BaseClass
|
|
|
+{
|
|
|
+ use OrderTrait;
|
|
|
+ public static $baseFile = '\bizGhs\order\models\StockWastageOrder';
|
|
|
+ const STATUS_COMPLETE = 1; //已完成
|
|
|
+
|
|
|
+
|
|
|
+ // 订单列表
|
|
|
+ public static function getOrderList($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)
|
|
|
+ {
|
|
|
+ $data = [];
|
|
|
+ $data['status'] = self::STATUS_COMPLETE;
|
|
|
+ $orderSn = orderSn::getGhsStockWastageSn();
|
|
|
+ $data['orderSn'] = $orderSn;
|
|
|
+ $data['sjId'] = $post['sjId'];
|
|
|
+ $data['shopId'] = $post['shopId'];
|
|
|
+ $data['adminId'] = $post['adminId'];
|
|
|
+
|
|
|
+ //花材列表
|
|
|
+ $ghsItemInfo = $post['product'];
|
|
|
+ $ghsItemInfo = self::mergeItemInfo($ghsItemInfo);
|
|
|
+ $sumNum = self::sumNumByItemInfo($ghsItemInfo);
|
|
|
+ $data['bigNum'] = $sumNum['bigNum'];// 总的大单位数量 总共多少扎
|
|
|
+ $data['smallNum'] = $sumNum['smallNum']; //总的小单位数量 总共多少支
|
|
|
+ $data['remark'] = $post['remark']??'';
|
|
|
+
|
|
|
+ try {
|
|
|
+ //写入订单表
|
|
|
+ $order = self::add($data);
|
|
|
+ foreach ($ghsItemInfo as $k => $v) {
|
|
|
+ $stockInfo = ProductClass::decreaseStock($v['productId'], $v['bigNum'], $v['smallNum']);
|
|
|
+ $ghsItemInfo[$k]['oldStock'] = $stockInfo['oldStock'];
|
|
|
+ $ghsItemInfo[$k]['newStock'] = $stockInfo['newStock'];
|
|
|
+ }
|
|
|
+ //写入详情表
|
|
|
+ $batchData = self::groupOrderItem($ghsItemInfo, $orderSn);
|
|
|
+ StockWastageOrderItemClass::batchAddOrderItem($batchData);
|
|
|
+ $orderData = self::getOrderDetail($orderSn);
|
|
|
+ $orderData['shopId'] = $data['shopId'];
|
|
|
+ //写流水
|
|
|
+ StockRecordClass::addRecordByOrder($orderData, StockRecordClass::STOCK_RECORD_TYPE_WASTAGE);
|
|
|
+ return $order;
|
|
|
+ } catch (\Exception $exception) {
|
|
|
+ util::fail("入库失败" . $exception->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //2021.1.23 linqh 订单详情
|
|
|
+ public static function getOrderDetail($orderSn)
|
|
|
+ {
|
|
|
+ $orderData = self::orderExist(['orderSn' => $orderSn]);
|
|
|
+ $orderInfo = StockWastageOrderItemClass::getOrderItemDetail($orderSn);
|
|
|
+ $itemData = [];
|
|
|
+ if ($orderInfo) {
|
|
|
+ foreach ($orderInfo as $v) {
|
|
|
+ $info = json_decode($v['itemInfo'], true);
|
|
|
+ $tmp = $info;
|
|
|
+ $tmp['itemCover'] = self::groupImg($info['itemCover']);
|
|
|
+ unset($v['itemInfo']);
|
|
|
+ //兼容 旧数据,没有 rank (花材等级 默认给B)
|
|
|
+ if (!isset($tmp['rank'])) {
|
|
|
+ $tmp['rank'] = 'B';
|
|
|
+ }
|
|
|
+ $tmp = array_merge($tmp, $v);
|
|
|
+ $itemData[] = $tmp;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $orderData = self::groupAdmin($orderData);
|
|
|
+ $orderData['itemInfo'] = $itemData;
|
|
|
+ return $orderData;
|
|
|
+ }
|
|
|
+
|
|
|
+ //2021.1.23 linqh 判断定时是否存在
|
|
|
+ public static function orderExist($where)
|
|
|
+ {
|
|
|
+ $orderData = self::getByCondition($where);
|
|
|
+ if (!$orderData) {
|
|
|
+ util::fail('订单不存在或不能取消');
|
|
|
+ }
|
|
|
+ return $orderData;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ // 组装 orderItem linqh 2021.1.25
|
|
|
+ public static function groupOrderItem($ghsItemInfo, $orderSn)
|
|
|
+ {
|
|
|
+ $productData = self::getProductMapData($ghsItemInfo);
|
|
|
+ //写入详情表
|
|
|
+ $batchData = [];
|
|
|
+ foreach ($ghsItemInfo as $v) {
|
|
|
+ $tmp = [];
|
|
|
+ $productId = $v['productId'];
|
|
|
+ $itemId = $productData[$productId]['itemId'];
|
|
|
+ $rank = $productData[$productId]['rank'] ?? 'B';// 花材级别
|
|
|
+ $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; //当时售卖的价格
|
|
|
+ $stockFormat = ProductClass::formatStock($tmp['itemStock'], $unitNum); // 库存总单位数量转大小单位的数量
|
|
|
+ $itemInfo = [
|
|
|
+ 'itemName' => $productData[$productId]['name'] ?? '',//花材名称
|
|
|
+ 'itemCover' => $productData[$productId]['cover'] ?? '',//花材图片
|
|
|
+ 'bigNum' => $v['bigNum'] ?? 0,//数量(大单位)
|
|
|
+ 'smallNum' => $v['smallNum'] ?? 0,//数量(小单位)
|
|
|
+ 'bigNumStock' => $stockFormat['bigNum'],// 当时库存大单位数
|
|
|
+ 'smallNumStock' => $stockFormat['smallNum'],//当时库存 小单位数
|
|
|
+ 'itemUnit' => $unitNum ? 2 : 1,//单位数量 2表示有两个单位(e.g 扎和支) 1表是只有一个单位(e.g 扎)
|
|
|
+ 'ratio' => $unitNum,// 比例
|
|
|
+ 'rank' => $rank,// 花材级别
|
|
|
+ 'bigUnit' => $productData[$productId]['bigUnit'],//大单位名称 扎
|
|
|
+ 'smallUnit' => $productData[$productId]['smallUnit'],//小单位名称 支
|
|
|
+ ];
|
|
|
+ $tmp['itemInfo'] = json_encode($itemInfo);
|
|
|
+ $batchData[] = $tmp;
|
|
|
+ }
|
|
|
+ return $batchData;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|