소스 검색

损耗接口

林琦海 5 년 전
부모
커밋
dd8960e89b

+ 42 - 1
app-ghs/controllers/OrderController.php

@@ -3,6 +3,7 @@
 namespace ghs\controllers;
 
 use bizGhs\express\services\DadaExpressServices;
+use bizGhs\order\classes\StockWastageOrderClass;
 use bizHd\purchase\classes\PurchaseClass;
 use bizHd\wx\classes\WxOpenClass;
 use bizGhs\custom\classes\CustomClass;
@@ -399,7 +400,7 @@ class OrderController extends BaseController
         $id = isset($get['id']) ? $get['id'] : 0;
         $info = OrderService::getById($id, true);
         OrderClass::valid($info->attributes, $this->shopId);
-       
+
         if (empty($info->lat) || empty($info->long) || empty($info->fullAddress)) {
             util::fail('请完善客户地址');
         }
@@ -460,4 +461,44 @@ class OrderController extends BaseController
         util::success($respond);
     }
 
+    //损耗  linqh 2021.6.11
+    public function actionWastage()
+    {
+
+        $post = Yii::$app->request->post();
+        $post['shopId'] = $this->shopId;
+        $post['sjId'] = $this->sjId;
+        $post['adminId'] = $this->adminId;
+        $productJson = $post['product'] ?? '';
+        if (empty($productJson)) {
+            util::fail('请选择花材');
+        }
+        $productList = json_decode($productJson, true);
+        if (empty($productList)) {
+            util::fail('请选择花材');
+        }
+        //判断花材里的信息
+        foreach ($productList as $v) {
+            $bigNum = $v['bigNum'] ?? 0;
+            $smallNum = $v['smallNum'] ?? 0;
+            if ($bigNum <= 0 && $smallNum <= 0) {
+                util::fail('花材数量不能为0');
+            }
+        }
+        $connection = Yii::$app->db;//事务处理
+        $transaction = $connection->beginTransaction();
+        try {
+            //判断花材有效性
+            ProductClass::valid($productList, $this->shopId);
+            $post['product'] = $productList;
+            $return = StockWastageOrderClass::addOrder($post);
+            $transaction->commit();
+            util::success($return);
+        } catch (\Exception $e) {
+            $transaction->rollBack();
+            Yii::info("损耗失败原因:" . $e->getMessage());
+            util::fail('损耗保存失败');
+        }
+    }
+
 }

+ 157 - 0
biz-ghs/order/classes/StockWastageOrderClass.php

@@ -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;
+    }
+
+}

+ 21 - 0
biz-ghs/order/classes/StockWastageOrderItemClass.php

@@ -0,0 +1,21 @@
+<?php
+
+namespace bizGhs\order\classes;
+
+use Yii;
+use bizGhs\base\classes\BaseClass;
+
+class StockWastageOrderItemClass extends BaseClass
+{
+
+    public static $baseFile = '\bizGhs\order\models\StockWastageOrderItem';
+
+    public static function batchAddOrderItem($data)
+    {
+        self::batchAdd($data);
+    }
+    public static function getOrderItemDetail($orderSn)
+    {
+        return self::getAllByCondition(['orderSn'=>$orderSn],null,'*');
+    }
+}

+ 13 - 0
biz-ghs/order/classes/StockWastageSnClass.php

@@ -0,0 +1,13 @@
+<?php
+
+namespace bizGhs\order\classes;
+
+use Yii;
+use bizGhs\base\classes\BaseClass;
+
+class StockWastageSnClass extends BaseClass
+{
+
+	public static $baseFile = '\bizGhs\order\models\StockWastageSn';
+
+}

+ 13 - 0
biz-ghs/order/models/StockWastageOrder.php

@@ -0,0 +1,13 @@
+<?php
+namespace bizGhs\order\models;
+use bizGhs\base\models\Base;
+
+class StockWastageOrder extends Base
+{
+
+	public static function tableName()
+	{
+		return 'xhGhsStockWastageOrder';
+	}
+
+}

+ 13 - 0
biz-ghs/order/models/StockWastageOrderItem.php

@@ -0,0 +1,13 @@
+<?php
+namespace bizGhs\order\models;
+use bizGhs\base\models\Base;
+
+class StockWastageOrderItem extends Base
+{
+
+	public static function tableName()
+	{
+		return 'xhGhsStockWastageOrderItem';
+	}
+
+}

+ 15 - 0
biz-ghs/order/models/StockWastageSn.php

@@ -0,0 +1,15 @@
+<?php
+
+namespace bizGhs\order\models;
+
+use bizGhs\base\models\Base;
+
+class StockWastageSn extends Base
+{
+
+	public static function tableName()
+	{
+		return 'xhGhsStockWastageSn';
+	}
+
+}

+ 3 - 0
biz-ghs/stock/classes/StockRecordClass.php

@@ -20,6 +20,8 @@ class StockRecordClass extends BaseClass
     const STOCK_RECORD_TYPE_OUT_STOCK_CANCEL = 6; //取消入库
     const STOCK_RECORD_TYPE_IN_STOCK_CANCEL = 7; //取消出库
     const STOCK_RECORD_TYPE_SELL_STOCK_CANCEL = 8;// 取消售卖
+    const STOCK_RECORD_TYPE_WASTAGE = 9;// 损耗
+
 
 
     public static $typeMap = [
@@ -31,6 +33,7 @@ class StockRecordClass extends BaseClass
         self::STOCK_RECORD_TYPE_OUT_STOCK_CANCEL=>'取消入库',
         self::STOCK_RECORD_TYPE_IN_STOCK_CANCEL=>'取消出库',
         self::STOCK_RECORD_TYPE_SELL_STOCK_CANCEL=>'取消开单',
+        self::STOCK_RECORD_TYPE_WASTAGE=>'损耗',
 
     ];
 

+ 17 - 1
common/components/orderSn.php

@@ -214,4 +214,20 @@ class orderSn
         return $prefix . $id;
     }
 
-}
+
+    //供货商损耗订单号 linqh 2021.6.211
+    public static function getGhsStockWastageSn()
+    {
+        $prefix = 'SH_CS';
+        if (getenv('YII_ENV', 'local') == 'production') {
+            $prefix = 'SH';
+        }
+        $respond = \bizGhs\order\classes\StockWastageSnClass::add(['id' => null]);
+        $id = $respond['id'] ?? 0;
+        if (empty($id)) {
+            util::fail('损耗单号没有生成');
+        }
+        return $prefix . $id;
+    }
+
+}