Browse Source

拆散功能

shish 3 years ago
parent
commit
16a575e269

+ 41 - 24
app-hd/controllers/PartController.php

@@ -1,9 +1,12 @@
 <?php
 
 namespace hd\controllers;
+
 use bizGhs\product\classes\ProductClass;
 use bizHd\part\classes\PartClass;
+use bizHd\part\classes\PartItemClass;
 use common\components\dict;
+use common\components\imgUtil;
 use common\components\util;
 use Yii;
 
@@ -12,14 +15,39 @@ class PartController extends BaseController
 
     public $guestAccess = [];
 
-    //拆散花材 ssh 20230208
-    public function actionPartItem()
+    public function actionGetFullInfo()
     {
-        $shopAdmin = $this->shopAdmin;
-        if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
-            util::fail('超管才能拆散');
+        $id = Yii::$app->request->get('id', '');
+        $info = PartClass::getById($id, true);
+        if ($info->mainId != $this->mainId) {
+            util::fail('没有权限');
         }
+        $orderSn = $info->orderSn ?? '';
+        $orderInfo = PartItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
+        $itemData = [];
+        if (!empty($orderInfo)) {
+
+            foreach ($orderInfo as $v) {
+                $shortCover = $v['cover'] ?? '';
+                $v['cover'] = imgUtil::groupImg($shortCover);
+                $itemData[] = $v;
+            }
+        }
+        util::success(['info' => $info, 'itemList' => $itemData]);
+    }
+
+    //列表
+    public function actionList()
+    {
+        $where = [];
+        $where['mainId'] = $this->mainId;
+        $list = PartClass::getPartList($where);
+        util::success($list);
+    }
 
+    //拆散花材 ssh 20230208
+    public function actionPartItem()
+    {
         $post = Yii::$app->request->post();
         $post['shopId'] = $this->shopId;
         $post['sjId'] = $this->sjId;
@@ -51,6 +79,7 @@ class PartController extends BaseController
                     $bigNum = $num;
                 } else {
                     $smallNum = $num;
+                    util::fail('小单位不能拆散');
                 }
                 $newProductList[] = ['productId' => $id, 'bigNum' => $bigNum, 'smallNum' => $smallNum, 'classId' => 0, 'itemId' => 0];
             }
@@ -60,11 +89,15 @@ class PartController extends BaseController
         foreach ($productList as $v) {
             $bigNum = $v['bigNum'] ?? 0;
             $smallNum = $v['smallNum'] ?? 0;
-            if ($bigNum <= 0 && $smallNum <= 0) {
+            if ($bigNum <= 0) {
                 util::fail('数量不能为0');
             }
-            if (isset($v['property']) && $v['property'] == 0) {
-                util::fail('花材和商品请分开拆散');
+            if ($smallNum > 0) {
+                util::fail('仅支持整扎的拆散');
+            }
+            $property = $v['property'] ?? 1;
+            if ($property == 0) {
+                util::fail('暂不支持成品的拆散');
             }
         }
         $connection = Yii::$app->db;
@@ -83,20 +116,4 @@ class PartController extends BaseController
         }
     }
 
-    //列表
-    public function actionList()
-    {
-        $where = [];
-        $where['mainId'] = $this->mainId;
-        $list = PartClass::getOrderList($where);
-        util::success($list);
-    }
-
-    public function actionDetail()
-    {
-        $orderSn = Yii::$app->request->get('orderSn', '');
-        $orderData = PartClass::getOrderDetail($orderSn, $this->mainId);
-        util::success($orderData);
-    }
-
 }

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

@@ -31,6 +31,7 @@ class StockRecordClass extends BaseClass
     const STOCK_RECORD_TYPE_GOODS_WASTAGE_CANCEL = 15;//取消制作已报损
     const STOCK_RECORD_TYPE_HD_REFUND = 16;//花店退货退款
     const STOCK_RECORD_TYPE_GHS_REFUND = 17;//供货商申请退货退款
+    const STOCK_RECORD_TYPE_PART = 18;//供货商申请退货退款
 
     public static $typeMap = [
         self::STOCK_RECORD_TYPE_PURCHASE => '采购入库',
@@ -50,6 +51,7 @@ class StockRecordClass extends BaseClass
         self::STOCK_RECORD_TYPE_GOODS_WASTAGE_CANCEL => '取消制作损耗',
         self::STOCK_RECORD_TYPE_HD_REFUND => '退货退款',
         self::STOCK_RECORD_TYPE_GHS_REFUND => '采购申请退货',
+        self::STOCK_RECORD_TYPE_PART => '拆散',
     ];
 
     public static function getRecordList($where)

+ 147 - 0
biz-hd/part/classes/PartClass.php

@@ -1,7 +1,13 @@
 <?php
 
 namespace bizHd\part\classes;
+
 use bizHd\base\classes\BaseClass;
+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
@@ -9,4 +15,145 @@ 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);
+            $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'],
+                'itemId' => $currentItem['itemId'],
+                'itemNum' => $currentItem['itemNum'],
+                'oldStock' => $currentItem['newStock'],
+                'newStock' => $currentItem['newStock'],
+            ];
+        }
+        $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);
+        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;
+    }
+
 }

+ 20 - 0
biz-hd/part/classes/StatPartClass.php

@@ -1,6 +1,7 @@
 <?php
 
 namespace bizHd\part\classes;
+
 use bizHd\base\classes\BaseClass;
 use Yii;
 
@@ -9,4 +10,23 @@ class StatPartClass extends BaseClass
 
     public static $baseFile = '\bizHd\part\models\StatPart';
 
+    //每天统计 ssh 20230209
+    public static function replace($main, $amount, $totalNum, $date = null)
+    {
+        $mainId = $main->id ?? 0;
+        $time = $date == null ? date('Ymd') : $date;
+        $stat = self::getByCondition(['mainId' => $mainId, 'time' => $time], true);
+        if (empty($stat)) {
+            $stat = self::add(['mainId' => $mainId, 'time' => $time, 'num' => 0], true);
+        }
+        $id = $stat->id;
+        $unique = self::getLockById($id);
+        $currentAmount = bcadd($unique->amount, $amount, 2);
+        $unique->amount = $currentAmount;
+        $unique->totalAmount = $main->totalWast;
+        $unique->num = bcadd($totalNum, $unique->num);
+        $unique->save();
+        StatPartMonthClass::replace($main, $amount, $totalNum, $time);
+    }
+
 }

+ 28 - 0
biz-hd/part/classes/StatPartMonthClass.php

@@ -1,6 +1,7 @@
 <?php
 
 namespace bizHd\part\classes;
+
 use bizHd\base\classes\BaseClass;
 use Yii;
 
@@ -9,4 +10,31 @@ class StatPartMonthClass extends BaseClass
 
     public static $baseFile = '\bizHd\part\models\StatPartMonth';
 
+    //每月拆散统计 ssh 20230208
+    public static function replace($main, $amount, $totalNum, $date = null)
+    {
+        if ($date == null) {
+            $time = date('Ym');
+            $year = date('Y');
+            $month = date('m');
+        } else {
+            $timestamp = strtotime($date);
+            $time = date('Ym', $timestamp);
+            $year = date('Y', $timestamp);
+            $month = date('m', $timestamp);
+        }
+        $mainId = $main->id ?? 0;
+        $stat = self::getByCondition(['mainId' => $mainId, 'time' => $time], true);
+        if (empty($stat)) {
+            $stat = self::add(['mainId' => $mainId, 'time' => $time, 'year' => $year, 'month' => $month, 'num' => 1], true);
+        }
+        $id = $stat->id;
+        $unique = self::getLockById($id);
+        $currentAmount = bcadd($unique->amount, $amount, 2);
+        $unique->amount = $currentAmount;
+        $unique->totalAmount = $main->totalWast;
+        $unique->num = bcadd($totalNum, $unique->num);
+        $unique->save();
+    }
+
 }

+ 15 - 0
common/components/orderSn.php

@@ -8,6 +8,7 @@ use bizHd\cg\classes\CgRefundSnClass;
 use bizHd\goods\classes\GoodsCgSnClass;
 use bizHd\goods\classes\GoodsSnClass;
 use bizHd\order\classes\SettleSnClass;
+use bizHd\part\classes\PartSnClass;
 use bizHd\purchase\classes\PurchaseClearSnClass;
 use biz\recharge\classes\RechargeSnClass;
 use bizGhs\check\classes\CheckSnClass;
@@ -236,6 +237,20 @@ class orderSn
         return $prefix . $id;
     }
 
+    public static function getPartSn()
+    {
+        $prefix = 'PT_CS';
+        if (getenv('YII_ENV', 'local') == 'production') {
+            $prefix = 'PT';
+        }
+        $respond = PartSnClass::add(['id' => null]);
+        $id = $respond['id'] ?? 0;
+        if (empty($id)) {
+            util::fail('拆散单号没有生成');
+        }
+        return $prefix . $id;
+    }
+
     //花店销售单的退款单 ssh 20220427
     public static function getHdRefundSn()
     {