林琦海 5 лет назад
Родитель
Сommit
45a73137de

+ 102 - 14
app/ghs/controllers/CheckOrderController.php

@@ -2,32 +2,120 @@
 
 namespace ghs\controllers;
 
-use bizGhs\check\classes\CheckOrderClass;
+use bizGhs\order\classes\CheckOrderClass;
+use bizGhs\product\classes\ProductClass;
 use Yii;
 use common\components\util;
 
 class CheckOrderController extends BaseController
 {
-	
+
 	//订单列表 shish 2021.1.14
 	public function actionList()
 	{
 		$get = Yii::$app->request->get();
-		$orderStatus = isset($get['orderStatus']) ? $get['orderStatus'] : '';
+		$orderStatus = isset($get['status']) ? $get['status'] : '';
 		$where = [];
 		if (!empty($orderStatus)) {
-			$where['orderStatus'] = $orderStatus;
+			$where['status'] = $orderStatus;
 		}
 		$list = CheckOrderClass::getOrderList($where);
 		util::success($list);
 	}
-	
-	//订单详情 shish 2021.1.14
-	public function actionDetail()
-	{
-		$id = Yii::$app->request->get('id');
-		$detail = OrderService::getOrderById($id);
-		util::success($detail);
-	}
-	
-}
+
+    //订单详情 2021.1.22 linqh
+    public function actionDetail()
+    {
+        $orderSn = Yii::$app->request->get('orderSn','');
+        $orderData = CheckOrderClass::getOrderDetail($orderSn,$this->shopId);
+        util::success($orderData);
+    }
+
+	//盘点确认 linqh 2021.1.22
+    public function actionCreateOrder()
+    {
+        $post = Yii::$app->request->post();
+        $post['merchantId'] = $this->merchantId;
+        $post['shopId'] = $this->shopId;
+        $post['adminId'] = $this->adminId;
+        //盘点是否有未处理的草稿,有的话先处理之前的草稿??
+        //todo
+        $this->saveOrder($post,false,false);
+
+    }
+
+    //盘点存草稿 linqh 2021.1.22
+    public function actionCreateDraft()
+    {
+        $post = Yii::$app->request->post();
+        $post['merchantId'] = $this->merchantId;
+        $post['shopId'] = $this->shopId;
+        $post['adminId'] = $this->adminId;
+
+        //盘点是否有未处理的草稿,有的话先处理之前的草稿??
+        //todo ??
+
+        $this->saveOrder($post,true,false);
+    }
+
+    //编辑草稿 ,继续保存草稿
+    public function actionUpdateDraft()
+    {
+        $post = Yii::$app->request->post();
+        $post['merchantId'] = $this->merchantId;
+        $post['shopId'] = $this->shopId;
+        $post['adminId'] = $this->adminId;
+        $orderSn = $post['orderSn']??'';
+        $orderData = CheckOrderClass::orderExist($orderSn,$this->shopId);
+        if($orderData['status'] != CheckOrderClass::STATUS_DRAFT){
+            util::fail("订单不存在");
+        }
+        $this->saveOrder($post,true,true);
+
+    }
+
+    //编辑草稿, 确认
+    public function actionUpdateOrder()
+    {
+        $post = Yii::$app->request->post();
+        $post['merchantId'] = $this->merchantId;
+        $post['shopId'] = $this->shopId;
+        $post['adminId'] = $this->adminId;
+        $orderSn = $post['orderSn']??'';
+        $orderData = CheckOrderClass::orderExist($orderSn,$this->shopId);
+        if($orderData['status'] != CheckOrderClass::STATUS_DRAFT){
+            util::fail("订单不存在");
+        }
+        $this->saveOrder($post,false,true);
+    }
+
+    protected function saveOrder($post,$draft=false,$update=false)
+    {
+        $ghsItemInfo = $post['itemInfo']??'';
+        if(empty($ghsItemInfo)){
+            util::fail('请选择花材');
+        }
+        $ghsItemInfo = json_decode($ghsItemInfo,true); // [{productId:0,bigNum:1,smallNum:0}]
+        if(!is_array($ghsItemInfo)){
+            util::fail('花材格式不正确');
+        }
+        //判断花材格式,是否属于当前门店
+        $productIds = array_column($ghsItemInfo,'productId');
+        ProductClass::valid($productIds,$this->shopId);
+        $post['itemInfo'] = $ghsItemInfo;
+        if($update===true){
+            //编辑
+            $order = CheckOrderClass::opOrder($post,$draft,$update);
+        }else{
+            $order = CheckOrderClass::opOrder($post,$draft,$update);
+        }
+
+        if($order){
+            util::success($order);
+        }else{
+            util::fail();
+        }
+    }
+
+
+}

+ 2 - 3
app/ghs/controllers/PurchaseOrderController.php

@@ -22,7 +22,7 @@ class PurchaseOrderController extends BaseController
         if(empty($ghsItemInfo)){
             util::fail('请选择花材');
         }
-        $ghsItemInfo = json_decode($ghsItemInfo,true); // [{productId:0,itemId:0,bigNum:1,smallNum:0}]
+        $ghsItemInfo = json_decode($ghsItemInfo,true); // [{productId:0,itemPrice:1,bigNum:1,smallNum:0}]
         if(!is_array($ghsItemInfo)){
             util::fail('花材格式不正确');
         }
@@ -34,14 +34,13 @@ class PurchaseOrderController extends BaseController
 
         //判断花材里的信息
         foreach ($ghsItemInfo as $v){
-            $itemId = $v['itemId']??0;
             $bigNum = $v['bigNum']??0;
             $productId = $v['productId']??0;
             $smallNum = $v['smallNum']??0;
             if(!isset($v['itemPrice'])){
                 util::fail('采购价格不能为空');
             }
-            if(!$itemId || !$productId){
+            if(!$productId){
                 util::fail('花材不存在');
             }
             if($bigNum <=0 && $smallNum <=0){

+ 114 - 43
biz-ghs/order/classes/CheckOrderClass.php

@@ -2,104 +2,139 @@
 
 namespace bizGhs\order\classes;
 
+use bizGhs\admin\classes\AdminClass;
 use bizGhs\product\classes\ProductClass;
 use bizGhs\stock\services\StockRecordService;
+use common\components\orderSn;
 use common\components\util;
 use Yii;
 use bizGhs\base\classes\BaseClass;
 use common\services\xhItemService;
+use bizGhs\order\traits\OrderTrait;
 class CheckOrderClass extends BaseClass
 {
-
+    use OrderTrait;
     public static $baseFile = '\bizGhs\order\models\CheckOrder';
-    const STATUS_COMPLETE = 1;
+    const STATUS_DRAFT = 1; //草稿
+    const STATUS_COMPLETE = 2; //完成
 
 
-    //添加
-    public static function addOrder($data)
+    //2021.1.22 linqh 操作订单  添加/保存草稿/草稿编辑保存: 区别在添加是立即改库存和记录流水记录,而保存草稿是只保存订单信息
+    public static function opOrder($data,$draft=false,$update=false)
     {
         $merchantId = $data['merchantId'];
         $shopId = $data['shopId'];
-        $data['status'] = self::STATUS_COMPLETE;
-        $orderSn =self::generateOrderSn(self::CHECK_ORDER);
+        $adminId = $data['adminId']??0;
+        if($draft){
+            //保存草稿
+            $data['status'] = self::STATUS_DRAFT;
+        }else{
+            //确定保存
+            $data['status'] = self::STATUS_COMPLETE;
+        }
+        if($update){
+            $orderSn = $data['orderSn'];
+        }else{
+            $orderSn =orderSn::getGhsCheckSn();
+        }
+
         $data['orderSn'] = $orderSn;
-        $ghsItemInfo = $data['itemInfo']; // [{itemId:0,bigNum:0,smallNum:0,itemPrice:1.0}]
+        $ghsItemInfo = $data['itemInfo']; // [{bigNum:0,smallNum:0,productId:1}]
         $bigNum = 0; //总共多少扎
         $smallNum = 0; //总共多少支
-        $totalItemPrice = 0;
-        $itemIds = array_column($ghsItemInfo,'itemId');
+
+
+        $productIds = array_column($ghsItemInfo,'productId');
+        $productData = ProductClass::getProductByIds($productIds);
+        $itemIds =  array_column($productData,'itemId');
+        $productData =  array_column($productData, NULL, 'id');
+
         $itemData = xhItemService::getByIds($itemIds);
         $itemData = array_column($itemData, NULL, 'id');
+
         if(!empty($ghsItemInfo)){
             foreach ($ghsItemInfo as $v){
-                $bigNum += $v['bigNum'];
-                $smallNum += $v['smallNum'];
-                $totalItemPrice = bcadd($totalItemPrice,$v['itemPrice'],2);
+                $bigNum += $v['bigNum']??0;
+                $smallNum += $v['smallNum']??0;
+
             }
         }
         $data['bigNum'] = $bigNum;
         $data['smallNum'] = $smallNum;
-        $data['totalPrice'] = $totalItemPrice;
+
         $connection = Yii::$app->db;
         $transaction = $connection->beginTransaction();
         try{
+            if($update){
+               //删除 ?? 是否合理,暂时先这样处理
+                self::deleteByCondition(['orderSn'=>$orderSn]);
+            }
             $order = self::add($data);
             //写入详情表
             $batchData = [];
 
             foreach ($ghsItemInfo as $v){
                 $tmp = [];
-                $itemId = $v['itemId'];
-                $tmp['productId'] = $v['productId']??0;
+                $productId =  $v['productId']??0;
+                $itemId = $productData[$productId]['itemId'];
+                $tmp['productId'] = $productId;
                 $tmp['orderSn'] = $orderSn;
-                $tmp['itemId'] = $v['itemId'];
+                $tmp['itemId'] = $itemId;
+                $tmp['itemStock'] = $productData[$productId]['stock']??0;//当时库存
                 $unitNum = $itemData[$itemId]['unitNum']??0;
                 $tmp['itemNum'] = ProductClass::mergeItemNum($v['bigNum'],$v['smallNum'],$unitNum);
-                $tmp['itemPrice'] = $v['itemPrice'];
+                $tmp['profitLossNum'] = bcsub(   $tmp['itemNum']- $tmp['itemStock'],2); //盈亏数 = 盘点数-当时库存数
+                $stockFormat= ProductClass::formatStock($tmp['itemStock'],$unitNum); // 库存总单位数量转大小单位的数量
                 $itemInfo = [
                     'itemName'=> $itemData[$itemId]['name']??'',
                     'itemCover'=> $itemData[$itemId]['cover']??'',
-                    'bigNum'=> $v['bigNum'],
-                    'smallNum'=> $v['smallNum'],
+                    'bigNum'=> $v['bigNum']??0,
+                    'smallNum'=> $v['smallNum']??0,
+                    'bigNumStock'=> $stockFormat['bigNum'],// 当时库存大单位数
+                    'smallNumStock'=> $stockFormat['smallNum'],//当时库存 小单位数
+                    'bigNumProfitLoss'=>bcsub($v['bigNum'],$stockFormat['bigNum'],2),//大单位的盈亏
+                    'smallNumProfitLoss'=>bcsub($v['smallNum'],$stockFormat['smallNum'],2),//小单位的盈亏
                     'itemUnit'=> $unitNum?2:1,
-                    'itemBigUnit'=> xhItemService::getUnitName($itemData[$v['itemId']]['bigUnit']??1),
-                    'itemSmallUnit'=> xhItemService::getUnitName($itemData[$v['itemId']]['smallUnit']??2),
+                    'itemBigUnit'=> xhItemService::getUnitName($itemData[$itemId]['bigUnit']??1),
+                    'itemSmallUnit'=> xhItemService::getUnitName($itemData[$itemId]['smallUnit']??2),
                 ];
                 $tmp['itemInfo'] = json_encode($itemInfo);
                 $batchData[] = $tmp;
             }
-            PurchaseOrderItemClass::batchAddOrderItem($batchData);
-            //现在暂时点击确定,完成 加库存
-            foreach ($batchData as $v){
-                $itemId = $v['itemId'];
-                $ghsItemInfoData = ProductClass::getGhsProductDataByItemId($shopId,$itemId);
-                $ghsItemInfoId = $ghsItemInfoData['id']??0;
-                if(!$ghsItemInfoId){
-                    $transaction->rollBack();
-                    util::fail('有商品不存在');
+            if($update){
+                //先删除
+                CheckOrderItemClass::deleteByCondition(['orderSn'=>$orderSn]);
+            }
+            CheckOrderItemClass::batchAddOrderItem($batchData);
+            if($draft===false){
+                //非草稿
+                //盘点完成完成,直接将库存中的数量改为当时盘点的数量
+                foreach ($batchData as $v){
+                    $productId = $v['productId'];
+                    ProductClass::updateStockById($productId,$v['itemNum'],$adminId);
+                    //流水记录
+                    $record = [];
+                    $record['merchantId'] = $merchantId;
+                    $record['shopId'] = $shopId;
+                    $record['orderSn'] = $orderSn;
+                    $record['itemId'] = $v['itemId'];
+                    $record['itemNum'] = $v['itemNum'];
+                    $record['oldStock'] = $v['itemStock'];
+                    $record['newStock'] =  $v['itemNum']; //新库存直接等于盘点的数量
+                    StockRecordService::addPurchaseOrderRecord($record);
+
                 }
-                //流水记录
-                $record = [];
-                $record['merchantId'] = $merchantId;
-                $record['shopId'] = $shopId;
-                $record['orderSn'] = $orderSn;
-                $record['itemId'] = $itemId;
-                $record['itemNum'] = $v['itemNum'];
-                $record['oldStock'] = $ghsItemInfoData['stock'];
-                $record['newStock'] =  bcadd($ghsItemInfoData['stock'],$v['itemNum'],2);
-                StockRecordService::addPurchaseOrderRecord($record);
-                //加库存
-                ProductClass::addStockByItemNum($ghsItemInfoId,$v['itemNum']);
             }
             $transaction->commit();
             return $order;
 
         }catch (\Exception $exception){
             $transaction->rollBack();
-            util::fail('保存失败');
+            util::fail('保存失败'.$exception->getMessage());
         }
     }
 
+    //2021.1.22 linqh 订单列表
     public static function getOrderList($where)
     {
         $data = self::getList('*', $where, 'addTime DESC');
@@ -108,7 +143,43 @@ class CheckOrderClass extends BaseClass
             return $data;
         }
         $data['list'] = $list;
+        $data['list'] = self::groupAdminList($list);
         return $data;
     }
 
+    //2021.1.22 订单详情
+    //获取订单详情信息 lqh 2021.1.20
+    public static function getOrderDetail($orderSn,$shopId)
+    {
+        $orderData = self::orderExist($orderSn,$shopId);
+        $orderInfo = CheckOrderItemClass::getOrderItemDetail($orderSn);
+        $itemData = [];
+        if($orderInfo) {
+            foreach ($orderInfo as $v) {
+                $info = json_decode($v['itemInfo'],true);
+                $tmp = $info;
+                $tmp['itemCover'] = self::groupImg($info['itemCover']);
+                $itemData[] = $tmp;
+            }
+        }
+        $orderData = self::groupAdmin($orderData);
+        $orderData['itemInfo'] = $itemData;
+        return $orderData;
+    }
+
+    //2021.1.22 linqh 判断订单号是否存在
+    public static function orderExist($orderSn,$shopId)
+    {
+        $where = [
+            'orderSn'=>$orderSn,
+            'shopId'=>$shopId,
+        ];
+        $orderData = self::getByCondition($where);
+        if(!$orderData) {
+            util::fail('订单不存在');
+        }
+        return $orderData;
+    }
+
+
 }

+ 18 - 14
biz-ghs/order/classes/PurchaseOrderClass.php

@@ -31,25 +31,28 @@ class PurchaseOrderClass extends BaseClass
         $merchantId = $data['merchantId'];
         $shopId = $data['shopId'];
         $data['status'] = self::PURCHASE_ORDER_STATUS_COMPLETE;
-        $orderSn = time();
-        $data['orderSn'] = orderSn::getGhsPurchaseSn();
+        $orderSn = orderSn::getGhsPurchaseSn();
+        $data['orderSn'] = $orderSn;
         $ghsItemInfo = $data['itemInfo']; // [{itemId:0,bigNum:0,smallNum:0,productId:1.0,itemPrice:1}]
         //计算出总共多少扎,多少支
         $bigNum = 0; //总共多少扎
         $smallNum = 0; //总共多少支
         $totalItemPrice = 0;
-        $itemIds = array_column($ghsItemInfo,'itemId');
-        $itemData = xhItemService::getByIds($itemIds);
-        $itemData = array_column($itemData, NULL, 'id');
+
 
         $productIds = array_column($ghsItemInfo,'productId');
         $productData = ProductClass::getProductByIds($productIds);
+        //通过productData 获取itemID. 对传过来的itemId 不用
+        $itemIds = array_column($productData,'itemId');
         $productData =  array_column($productData, NULL, 'id');
 
+        $itemData = xhItemService::getByIds($itemIds);
+        $itemData = array_column($itemData, NULL, 'id');
+
         if(!empty($ghsItemInfo)){
             foreach ($ghsItemInfo as $v){
-                $bigNum += $v['bigNum'];
-                $smallNum += $v['smallNum'];
+                $bigNum += $v['bigNum']??0;
+                $smallNum += $v['smallNum']??0;
                 $totalItemPrice = bcadd($totalItemPrice,$v['itemPrice'],2);
             }
         }
@@ -82,9 +85,10 @@ class PurchaseOrderClass extends BaseClass
 
             foreach ($ghsItemInfo as $v){
                 $tmp = [];
-                $itemId = $v['itemId'];
+                $productId = $v['productId'];
+                $itemId = $productData[$productId]['itemId'];
                 $tmp['orderSn'] = $orderSn;
-                $tmp['itemId'] = $v['itemId'];
+                $tmp['itemId'] = $itemId;
                 $tmp['productId'] = $v['productId'];
                 $unitNum = $itemData[$itemId]['unitNum']??0;
                 $tmp['itemNum'] = ProductClass::mergeItemNum($v['bigNum'],$v['smallNum'],$unitNum);
@@ -92,11 +96,11 @@ class PurchaseOrderClass extends BaseClass
                 $itemInfo = [
                     'itemName'=> $itemData[$itemId]['name']??'',
                     'itemCover'=> $itemData[$itemId]['cover']??'',
-                    'bigNum'=> $v['bigNum'],
-                    'smallNum'=> $v['smallNum'],
+                    'bigNum'=> $v['bigNum']??0,
+                    'smallNum'=> $v['smallNum']??0,
                     'unitCount'=> $unitNum?2:1,
-                    'bigUnit'=> xhItemService::getUnitName($itemData[$v['itemId']]['bigUnit']),
-                    'smallUnit'=> xhItemService::getUnitName($itemData[$v['itemId']]['smallUnit']),
+                    'bigUnit'=> xhItemService::getUnitName($itemData[$itemId]['bigUnit']),
+                    'smallUnit'=> xhItemService::getUnitName($itemData[$itemId]['smallUnit']),
                 ];
                 $tmp['itemInfo'] = json_encode($itemInfo);
                 $batchData[] = $tmp;
@@ -164,7 +168,7 @@ class PurchaseOrderClass extends BaseClass
             }
         }
         $gysInfo = SupplierClass::getById($orderData['supplierId']);
-        $orderData['upName'] = $gysInfo['upName']??'';
+        $orderData['adminName'] = $gysInfo['adminName']??'';
         $orderData['upCover'] = $gysInfo['upCover']??'';
         $orderData['itemInfo'] = $itemData;
         return $orderData;

+ 15 - 0
biz-ghs/order/traits/OrderTrait.php

@@ -13,6 +13,7 @@ use bizGhs\merchant\classes\SupplierClass;
 trait OrderTrait
 {
 
+    //二维数组拼装admin信息
     public static function groupAdminList($list)
     {
         if(empty($list)){
@@ -29,6 +30,18 @@ trait OrderTrait
         return $list;
     }
 
+    //2021.1.22 linqh 一维数组拼装admin信息
+    public static function groupAdmin($data)
+    {
+        $adminId = $data['adminId']??0;
+        $adminInfo = AdminClass::getById($adminId);
+        $data['adminName'] = $adminInfo['adminName']??'';
+        $data['nickName'] = $adminInfo['nickName']??'';
+        $data['avatar'] = $adminInfo['avatar']??'';
+        return $data;
+
+    }
+
     public static function groupSupplierList($list)
     {
         if(empty($list)){
@@ -43,4 +56,6 @@ trait OrderTrait
         }
         return $list;
     }
+
+
 }

+ 8 - 6
biz-ghs/product/classes/ProductClass.php

@@ -233,12 +233,6 @@ class ProductClass extends BaseClass
 		return $total;
 	}
 
-	//获取小单位的价格
-    public static function priceConvert($unitNum,$price)
-    {
-
-    }
-
 	//根据门店ID, 平台花材ID, 获取门店的花材ID
 	public static function getGhsProductId($shopId, $itemId)
 	{
@@ -283,4 +277,12 @@ class ProductClass extends BaseClass
     {
         return self::getByIds($ids);
     }
+
+    //修改花材商品的库存(盘点用)
+    public static function updateStockById($id,$stock,$adminId = 0)
+    {
+        $data['stock'] = $stock;
+        $data['adminId'] = $adminId;
+        return self::updateById($id,$data);
+    }
 }

+ 19 - 0
sql.sql

@@ -791,3 +791,22 @@ ALTER TABLE xhPurchase MODIFY `orderSn` CHAR(20) NOT NULL DEFAULT '' COMMENT '
 ALTER TABLE xhGhsStockOrder MODIFY `orderSn` CHAR(20) NOT NULL DEFAULT '' COMMENT '订单编号';
 ALTER TABLE xhGhsStockOrderItem MODIFY `orderSn` CHAR(20) NOT NULL DEFAULT '' COMMENT '订单编号';
 ALTER TABLE `xhGhsOrder` ADD outTime DATETIME NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '出库时间' AFTER `payTime`;
+
+=====
+==linqh 2021.1.22
+
+ALTER TABLE `xhGhsCheckOrder`
+CHANGE COLUMN `orderStatus` `status`  tinyint(2) UNSIGNED NOT NULL DEFAULT 0 COMMENT '盘点订单状态: 1草稿,2完成' AFTER `orderSn`,
+CHANGE COLUMN `itemNumBundle` `bigNum`  int(11) NOT NULL DEFAULT 0 COMMENT '合计多少商品(扎)(盈亏)' AFTER `adminId`,
+CHANGE COLUMN `itemNumPiece` `smallNum`  int(11) NOT NULL DEFAULT 0 COMMENT '合计多少商品(支)(盈亏)' AFTER `bigNum`,
+CHANGE COLUMN `totalItemPrice` `price`  decimal(10,2) NOT NULL DEFAULT 0.00 COMMENT '商品总价格(盈亏)' AFTER `smallNum`,
+MODIFY COLUMN `addTime`  timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间' AFTER `remark`,
+MODIFY COLUMN `updateTime`  timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '最后修改时间' AFTER `addTime`;
+
+ALTER TABLE `xhGhsCheckOrderItem`
+ADD COLUMN `productId`  int(11) UNSIGNED NOT NULL DEFAULT 0 COMMENT '商品id' AFTER `orderSn`;
+
+ALTER TABLE `xhGhsStockRecord`
+MODIFY COLUMN `addTime`  timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER `newStock`,
+MODIFY COLUMN `updateTime`  timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP AFTER `addTime`;
+