shish 3 years ago
parent
commit
0f2b28acaf

+ 35 - 63
app-hd/controllers/PdGoodsController.php

@@ -3,6 +3,7 @@
 namespace hd\controllers;
 
 use bizHd\pd\classes\PdGoodsClass;
+use bizHd\pd\classes\PdGoodsDetailClass;
 use Yii;
 use common\components\util;
 
@@ -24,84 +25,55 @@ class PdGoodsController extends BaseController
     }
 
     //订单详情
-    public function actionDetail()
+    public function actionGetInfo()
     {
-        $orderSn = Yii::$app->request->get('orderSn', '');
-        $orderData = PdGoodsClass::getOrderDetail($orderSn, $this->shopId);
-        util::success($orderData);
-    }
-
-    //盘点确认
-    public function actionCreateOrder()
-    {
-        $post = Yii::$app->request->post();
-        $post['sjId'] = $this->sjId;
-        $post['shopId'] = $this->shopId;
-        $post['adminId'] = $this->adminId;
-        $this->saveOrder($post, false, false);
-    }
-
-    //盘点存草稿
-    public function actionCreateDraft()
-    {
-        $post = Yii::$app->request->post();
-        $post['sjId'] = $this->sjId;
-        $post['shopId'] = $this->shopId;
-        $post['adminId'] = $this->adminId;
-
-        $this->saveOrder($post, true, false);
-    }
-
-    //编辑草稿 ,继续保存草稿
-    public function actionUpdateDraft()
-    {
-        $post = Yii::$app->request->post();
-        $post['sjId'] = $this->sjId;
-        $post['shopId'] = $this->shopId;
-        $post['adminId'] = $this->adminId;
-        $orderSn = $post['orderSn'] ?? '';
-        $orderData = PdGoodsClass::orderExist($orderSn, $this->shopId);
-        if ($orderData['status'] != PdGoodsClass::STATUS_DRAFT) {
-            util::fail("订单不存在");
+        $get = Yii::$app->request->get();
+        $orderSn = $get['orderSn'] ?? '';
+        $info = PdGoodsClass::getByCondition(['orderSn' => $orderSn], true);
+        if (empty($info) || $info->mainId != $this->mainId) {
+            util::fail('没有找到盘点记录');
         }
-        $this->saveOrder($post, true, true);
-
+        $detail = PdGoodsDetailClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
+        $data = ['info' => $info, 'goodsList' => $detail];
+        util::success($data);
     }
 
-    //编辑草稿, 确认
-    public function actionUpdateOrder()
+    //创建盘点 ssh 20230302
+    public function actionCreateOrder()
     {
         $post = Yii::$app->request->post();
         $post['sjId'] = $this->sjId;
         $post['shopId'] = $this->shopId;
+        $staff = $this->shopAdmin;
         $post['adminId'] = $this->adminId;
-        $orderSn = $post['orderSn'] ?? '';
-        $orderData = PdGoodsClass::orderExist($orderSn, $this->shopId);
-        if ($orderData['status'] != PdGoodsClass::STATUS_DRAFT) {
-            util::fail("订单不存在");
-        }
-        $this->saveOrder($post, false, true);
-    }
-
-    protected function saveOrder($post, $draft = false, $update = false)
-    {
-        $ghsItemInfo = $post['itemInfo'] ?? '';
-        if (empty($ghsItemInfo)) {
+        $post['staffName'] = $staff->name ?? '';
+        $post['mainId'] = $this->mainId;
+        $goodsItemInfo = $post['goodsInfo'] ?? '';
+        if (empty($goodsItemInfo)) {
             util::fail('请选择花材');
         }
-        $ghsItemInfo = json_decode($ghsItemInfo, true);
-        if (!is_array($ghsItemInfo)) {
-            util::fail('花材格式不正确');
+        $goodsItemInfo = json_decode($goodsItemInfo, true);
+        if (!is_array($goodsItemInfo)) {
+            util::fail('商品格式错误');
         }
-        foreach ($ghsItemInfo as $key => $item) {
+        foreach ($goodsItemInfo as $key => $item) {
             //如果盘点数是99999则转为0
-            if (isset($item['bigNum']) && $item['bigNum'] == 99999) {
-                $ghsItemInfo[$key]['bigNum'] = 0;
+            if (isset($item['num']) && $item['num'] == 99999) {
+                $goodsItemInfo[$key]['num'] = 0;
             }
         }
-        $post['itemInfo'] = $ghsItemInfo;
-        PdGoodsClass::opOrder($post, $draft, $update);
-        util::complete();
+        $post['goodsInfo'] = $goodsItemInfo;
+        $connection = Yii::$app->db;
+        $transaction = $connection->beginTransaction();
+        try {
+            PdGoodsClass::createPd($post);
+            $transaction->commit();
+            util::complete();
+        } catch (\Exception $e) {
+            $transaction->rollBack();
+            Yii::info("操作原因:" . $e->getMessage());
+            util::fail('操作失败');
+        }
     }
 
 }

+ 88 - 1
biz-hd/pd/classes/PdGoodsClass.php

@@ -1,7 +1,12 @@
 <?php
+
 namespace bizHd\pd\classes;
 
+use biz\stock\classes\GoodsStockRecordClass;
 use bizHd\base\classes\BaseClass;
+use bizHd\goods\classes\GoodsClass;
+use common\components\orderSn;
+use common\components\util;
 use Yii;
 
 class PdGoodsClass extends BaseClass
@@ -9,6 +14,11 @@ class PdGoodsClass extends BaseClass
 
     public static $baseFile = '\bizHd\pd\models\PdGoods';
 
+    public static function addBase($data, $bool = false)
+    {
+        return self::add($data, $bool);
+    }
+
     public static function getPdList($where)
     {
         $data = self::getList('*', $where, 'addTime DESC');
@@ -19,6 +29,83 @@ class PdGoodsClass extends BaseClass
         return $data;
     }
 
-
+    //创建盘点 ssh 20230302
+    public static function createPd($data)
+    {
+        $goodsList = $data['goodsInfo'];
+        $mainId = $data['mainId'] ?? 0;
+        $sjId = $data['sjId'] ?? 0;
+        $shopId = $data['shopId'] ?? 0;
+        $staffName = $data['staffName'] ?? '';
+        $orderSn = orderSn::getPdGoodsSn();
+        $data['orderSn'] = $orderSn;
+        $ids = array_column($goodsList, 'goodsId');
+        $ids = array_unique(array_filter($ids));
+        $goodsInfo = GoodsClass::getByIds($ids, null, 'id');
+        if (empty($goodsInfo)) {
+            util::fail('没有商品');
+        }
+        foreach ($goodsInfo as $goodsData) {
+            $name = $goodsData['name'] ?? '';
+            if (isset($goodsData['mainId']) == false || $goodsData['mainId'] != $mainId) {
+                util::fail($name . '没有权限盘点');
+            }
+        }
+        foreach ($goodsList as $item) {
+            $goodsId = $item['goodsId'] ?? 0;
+            $name = $item['name'] ?? '';
+            $cover = $item['cover'] ?? '';
+            $flower = $item['flower'] ?? 0;
+            $kindId = $item['kindId'] ?? 0;
+            $num = $item['num'] ?? 0;
+            $current = $goodsInfo[$goodsId] ?? [];
+            if (empty($current)) {
+                util::fail('存在有问题商品');
+            }
+            $stock = $current['stock'];
+            $price = $current['price'] ?? 0;
+            $cost = $current['cost'] ?? 0;
+            if ($num != $stock) {
+                if ($num > $stock) {
+                    $diffNum = bcsub($num, $stock);
+                    $respond = GoodsClass::addStock($goodsId, $mainId, $diffNum);
+                } else {
+                    $diffNum = bcsub($stock, $num);
+                    $checkStock = false;
+                    $params = [];
+                    $respond = GoodsClass::decreaseStock($goodsId, $mainId, $diffNum, $checkStock, $params);
+                }
+                $oldStock = $respond['oldStock'] ?? 0;
+                $newStock = $respond['newStock'] ?? 0;
+                $recordData = [];
+                $recordData['sjId'] = $sjId;
+                $recordData['shopId'] = $shopId;
+                $recordData['mainId'] = $mainId;
+                $recordData['orderSn'] = $orderSn;
+                $recordData['goodsId'] = $goodsId;
+                $recordData['goodsNum'] = $diffNum;
+                $recordData['oldStock'] = $oldStock;
+                $recordData['newStock'] = $newStock;
+                $recordData['relateName'] = $staffName;
+                GoodsStockRecordClass::pdGoods($recordData);
+                $itemData = [
+                    'orderSn' => $orderSn,
+                    'goodsId' => $goodsId,
+                    'name' => $name,
+                    'cover' => $cover,
+                    'flower' => $flower,
+                    'kindId' => $kindId,
+                    'num' => $num,
+                    'stock' => $oldStock,
+                    'price' => $price,
+                    'cost' => $cost,
+                ];
+                PdGoodsDetailClass::add($itemData);
+            }
+        }
+        $data['status'] = 2;
+        $result = self::addBase($data, true);
+        return $result;
+    }
 
 }

+ 13 - 0
biz-hd/pd/classes/PdGoodsDetailClass.php

@@ -0,0 +1,13 @@
+<?php
+
+namespace bizHd\pd\classes;
+
+use bizHd\base\classes\BaseClass;
+use Yii;
+
+class PdGoodsDetailClass extends BaseClass
+{
+
+    public static $baseFile = '\bizHd\pd\models\PdGoodsDetail';
+
+}

+ 13 - 0
biz-hd/pd/classes/PdGoodsSnClass.php

@@ -0,0 +1,13 @@
+<?php
+
+namespace bizHd\pd\classes;
+
+use bizHd\base\classes\BaseClass;
+use Yii;
+
+class PdGoodsSnClass extends BaseClass
+{
+
+    public static $baseFile = '\bizHd\pd\models\PdGoodsSn';
+
+}

+ 13 - 0
biz-hd/pd/models/PdGoodsDetail.php

@@ -0,0 +1,13 @@
+<?php
+namespace bizHd\pd\models;
+use bizHd\base\models\Base;
+
+class PdGoodsDetail extends Base
+{
+
+    public static function tableName()
+    {
+        return 'xhPdGoodsDetail';
+    }
+
+}

+ 13 - 0
biz-hd/pd/models/PdGoodsSn.php

@@ -0,0 +1,13 @@
+<?php
+namespace bizHd\pd\models;
+use bizHd\base\models\Base;
+
+class PdGoodsSn extends Base
+{
+
+    public static function tableName()
+    {
+        return 'xhPdGoodsSn';
+    }
+
+}

+ 16 - 2
biz/stock/classes/GoodsStockRecordClass.php

@@ -40,6 +40,12 @@ class GoodsStockRecordClass extends BaseClass
         self::STOCK_RECORD_TYPE_CANCEL_ORDER => '取消订单',
     ];
 
+    public static function pdGoods($data)
+    {
+        $data['recordType'] = self::STOCK_RECORD_TYPE_CHECK_STOCK;
+        return self::addRecord($data);
+    }
+
     //取消订单 ssh 20220516
     public static function cancelOrder($data)
     {
@@ -107,8 +113,16 @@ class GoodsStockRecordClass extends BaseClass
         $record['goodsId'] = $data['goodsId'] ?? '';
         $record['goodsNum'] = $data['goodsNum'] ?? '';
         $record['relateName'] = $data['relateName'] ?? '';
-        $record['oldStock'] = $data['oldStock'] ?? '';
-        $record['newStock'] = $data['newStock'] ?? '';
+        $record['oldStock'] = $data['oldStock'] ?? 0;
+        $record['newStock'] = $data['newStock'] ?? 0;
+        $io = -1;
+        if ($record['newStock'] > $record['oldStock']) {
+            $io = 1;
+        }
+        if ($record['newStock'] < $record['oldStock']) {
+            $io = 0;
+        }
+        $record['io'] = $io;
         $respond = self::add($record);
         return $respond;
     }

+ 15 - 0
common/components/orderSn.php

@@ -9,6 +9,7 @@ use bizHd\goods\classes\GoodsCgSnClass;
 use bizHd\goods\classes\GoodsSnClass;
 use bizHd\order\classes\SettleSnClass;
 use bizHd\part\classes\PartSnClass;
+use bizHd\pd\classes\PdGoodsSnClass;
 use bizHd\purchase\classes\PurchaseClearSnClass;
 use biz\recharge\classes\RechargeSnClass;
 use bizGhs\check\classes\CheckSnClass;
@@ -85,6 +86,20 @@ class orderSn
         return $prefix . $id;
     }
 
+    public static function getPdGoodsSn()
+    {
+        $prefix = 'PDG_CS';
+        if (getenv('YII_ENV', 'local') == 'production') {
+            $prefix = 'PDG';
+        }
+        $respond = PdGoodsSnClass::add(['id' => null]);
+        $id = $respond['id'] ?? 0;
+        if (empty($id)) {
+            util::fail('盘点单号没有生成');
+        }
+        return $prefix . $id;
+    }
+
     //零售商采购单号 ssh 2021.1.19
     public static function getPurchaseSn()
     {