shish 3 éve
szülő
commit
b33fcdb963

+ 116 - 0
app-ghs/controllers/PartController.php

@@ -0,0 +1,116 @@
+<?php
+
+namespace ghs\controllers;
+
+use bizGhs\part\classes\PartClass;
+use bizGhs\part\classes\PartItemClass;
+use common\components\dict;
+use common\components\imgUtil;
+use common\components\util;
+use Yii;
+
+class PartController extends BaseController
+{
+
+    public $guestAccess = [];
+
+    public function actionGetFullInfo()
+    {
+        $orderSn = Yii::$app->request->get('orderSn', '');
+        $info = PartClass::getByCondition(['orderSn' => $orderSn], 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;
+        $post['mainId'] = $this->mainId;
+        $post['shopAdminId'] = $this->shopAdminId;
+        $shopAdmin = $this->shopAdmin;
+        $adminName = $shopAdmin['name'] ?? '';
+        $post['shopAdminName'] = $adminName;
+        $productJson = $post['product'] ?? '';
+        if (empty($productJson)) {
+            util::fail('请选择花材');
+        }
+        $productList = json_decode($productJson, true);
+        if (empty($productList)) {
+            util::fail('请选择花材');
+        }
+        //有unitType数据,说明是收银台传过来的数据,转换成和手机端结构一致
+        $hasUnitType = array_column($productList, 'unitType');
+        if (!empty($hasUnitType)) {
+            $newProductList = [];
+            foreach ($productList as $key => $val) {
+                $unitType = $val['unitType'] ?? 0;
+                $bigNum = 0;
+                $smallNum = 0;
+                $num = $val['num'] ?? 0;
+                $id = $val['productId'] ?? 0;
+                if ($unitType == dict::getDict('unitType', 'big')) {
+                    $bigNum = $num;
+                } else {
+                    $smallNum = $num;
+                    util::fail('小单位不能拆散');
+                }
+                $newProductList[] = ['productId' => $id, 'bigNum' => $bigNum, 'smallNum' => $smallNum, 'classId' => 0, 'itemId' => 0];
+            }
+            $productList = $newProductList;
+        }
+        foreach ($productList as $v) {
+            $bigNum = $v['bigNum'] ?? 0;
+            $smallNum = $v['smallNum'] ?? 0;
+            if ($bigNum <= 0) {
+                util::fail('数量不能为0');
+            }
+            if ($smallNum > 0) {
+                util::fail('仅支持整扎的拆散');
+            }
+            $property = $v['property'] ?? 1;
+            if ($property == 0) {
+                util::fail('暂不支持成品的拆散');
+            }
+        }
+        $connection = Yii::$app->db;
+        $transaction = $connection->beginTransaction();
+        try {
+            ProductClass::valid($productList, $this->mainId);
+            $post['product'] = $productList;
+            $post['ptStyle'] = dict::getDict('ptStyle', 'hd');
+            $return = PartClass::addOrder($post);
+            $transaction->commit();
+            util::success($return);
+        } catch (\Exception $e) {
+            $transaction->rollBack();
+            Yii::info("拆散失败原因:" . $e->getMessage());
+            util::fail('拆散保存失败');
+        }
+    }
+
+
+}

+ 159 - 0
biz-ghs/part/classes/PartClass.php

@@ -0,0 +1,159 @@
+<?php
+namespace bizGhs\part\classes;
+
+use bizGhs\base\classes\BaseClass;
+use bizGhs\shop\classes\MainClass;
+use common\components\dict;
+use common\components\orderSn;
+use Yii;
+
+class PartClass extends BaseClass
+{
+
+    public static $baseFile = '\bizGhs\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'] ?? 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);
+        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;
+    }
+
+}

+ 12 - 0
biz-ghs/part/classes/PartItemClass.php

@@ -0,0 +1,12 @@
+<?php
+namespace bizGhs\part\classes;
+
+use bizGhs\base\classes\BaseClass;
+use Yii;
+
+class PartItemClass extends BaseClass
+{
+
+    public static $baseFile = '\bizGhs\part\models\PartItem';
+
+}

+ 12 - 0
biz-ghs/part/models/Part.php

@@ -0,0 +1,12 @@
+<?php
+namespace bizGhs\part\models;
+use bizGhs\base\models\Base;
+class Part extends Base
+{
+
+    public static function tableName()
+    {
+        return 'xhPart';
+    }
+
+}

+ 12 - 0
biz-ghs/part/models/PartItem.php

@@ -0,0 +1,12 @@
+<?php
+namespace bizGhs\part\models;
+use bizGhs\base\models\Base;
+class PartItem extends Base
+{
+
+    public static function tableName()
+    {
+        return 'xhPartItem';
+    }
+
+}

+ 13 - 0
biz-ghs/part/services/PartService.php

@@ -0,0 +1,13 @@
+<?php
+
+namespace bizGhs\product\services;
+
+use bizGhs\base\services\BaseService;
+use Yii;
+
+class PartService extends BaseService
+{
+
+    public static $baseFile = '\bizGhs\part\classes\PartClass';
+
+}

+ 0 - 6
console/controllers/ProductController.php

@@ -14,12 +14,6 @@ use Yii;
 class ProductController extends Controller
 {
 
-    public function actionInitHz()
-    {
-        $newMainId = 4556;
-        ItemClass::initItem($newMainId);
-    }
-
     public function actionChangeSk()
     {
         ini_set('memory_limit', '2045M');