shish 5 lat temu
rodzic
commit
3145e456f1

+ 80 - 0
app-ghs/controllers/OrderController.php

@@ -169,6 +169,86 @@ class OrderController extends BaseController
         }
     }
 
+    //修改订单 ssh 20210810
+    public function actionUpdateOrder()
+    {
+        $post = Yii::$app->request->post();
+
+        //PC端开单
+        if (isset($post['clearType'])) {
+            $clearType = $post['clearType'];
+            if ($clearType == OrderClass::CLEAR_DEBT) {
+                $post['debt'] = OrderClass::DEBT_YES;
+                $post['payWay'] = dict::getDict('payWay', 'debtPay');
+            } else {
+                $post['debt'] = OrderClass::DEBT_NO;
+            }
+        }
+        //员工不能开已收款订单
+        $debt = $post['debt'] ?? OrderClass::DEBT_YES;
+        if ($debt == OrderClass::DEBT_NO) {
+            $shopAdmin = $this->shopAdmin;
+            if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
+                util::fail('不能开已收款订单');
+            }
+        }
+        //PC端开单
+        if (isset($post['getType'])) {
+            util::fail('暂不支持PC端修改订单');
+            $getType = $post['getType'];
+            //自取
+            if ($getType == 1) {
+                $post['sendType'] = OrderClass::SEND_TYPE_NO;
+            } else {
+                //选择配送时,在发货时让商家自己再确认一下用什么方式
+                $post['sendType'] = OrderClass::SEND_TYPE_UNKNOWN;
+            }
+        }
+
+        $id = $post['id'] ?? 0;
+        $order = OrderClass::getLockById($id);
+        if (empty($order)) {
+            util::fail('没有找到订单');
+        }
+        OrderClass::valid($order, $this->shopId);
+        unset($post['id']);
+
+        $shopAdmin = $this->shopAdmin;
+        $post['shopAdminId'] = $this->shopAdminId;
+        $post['shopAdminName'] = $shopAdmin->name ?? '';
+        $post['fromType'] = 1;
+        $post['sendCost'] = $post['sendCost'] ?? '0.00';
+
+        $productJson = $post['product'] ?? '';
+        if (empty($productJson)) {
+            util::fail('请选择花材');
+        }
+        $productList = json_decode($productJson, true);
+        if (empty($productList)) {
+            util::fail('请选择花材');
+        }
+        $connection = Yii::$app->db;//事务处理
+        $transaction = $connection->beginTransaction();
+        try {
+            //判断花材有效性
+            ProductClass::valid($productList, $this->shopId);
+            $post['product'] = $productList;
+
+            //商家开单订单有效期更长(30)天
+            $validTime = dict::getDict('kd_order_valid');
+            $current = time() + $validTime;
+            $post['deadline'] = date("Y-m-d H:i:s", $current);
+
+            $return = OrderService::updateOrder($order, $post);
+            $transaction->commit();
+            util::success($return);
+        } catch (\Exception $e) {
+            $transaction->rollBack();
+            Yii::error("下单失败原因:" . $e->getMessage());
+            util::fail('下单失败');
+        }
+    }
+
     //延期支付 ssh 2021.1.19
     public function actionDebt()
     {

+ 94 - 0
biz-ghs/order/classes/OrderClass.php

@@ -201,6 +201,100 @@ class OrderClass extends BaseClass
         return $return;
     }
 
+    //添加订单 ssh 2019.12.5
+    public static function updateOrder($order, $data)
+    {
+        $sjId = $order->sjId ?? 0;
+        $orderSn = $order->orderSn ?? 0;
+
+        $data['payStatus'] = self::PAY_STATUS_UN_PAY;
+
+        //花材列表
+        $product = $data['product'];
+        //删除花材再添加
+        OrderItemClass::deleteByCondition(['orderSn' => $orderSn]);
+        $respond = OrderItemClass::replaceItem($orderSn, $product);
+
+        $sendCost = isset($data['sendCost']) && is_numeric($data['sendCost']) ? $data['sendCost'] : 0;
+        $data['sendCost'] = $sendCost;
+        $data['bigNum'] = $respond['bigNum'] ?? 0;
+        $data['smallNum'] = $respond['smallNum'] ?? 0;
+        $price = $respond['price'] ?? 0;
+        $currentPrice = bcadd($price, $sendCost, 2);
+        $data['prePrice'] = $currentPrice;
+        if (isset($data['modifyPrice'])) {
+            //商家打折,修改金额
+            $modifyPrice = $data['modifyPrice'] ?? 0;
+            if ($modifyPrice > $currentPrice) {
+                util::fail('实收金额大于订单总金额');
+            }
+            if ($modifyPrice < $currentPrice) {
+                $data['discountAmount'] = bcsub($currentPrice, $modifyPrice, 2);
+                $data['discountType'] = dict::getDict('discountType', 'discount');
+                $currentPrice = $modifyPrice;
+            }
+        }
+
+        $data['actPrice'] = $currentPrice;
+        $data['realPrice'] = $currentPrice;
+
+        //如果有平台补贴
+        if (isset($data['discountType']) && $data['discountType'] == dict::getDict('discountType', 'usePtCoupon')) {
+            $discountAmount = $data['discountAmount'] ?? 0;
+            $data['realPrice'] = bcsub($currentPrice, $discountAmount, 2);
+        }
+
+        $data['sendTime'] = isset($data['sendTime']) && !empty($data['sendTime']) ? $data['sendTime'] : '0000-00-00 00:00:00';
+        //订单有效期
+        $validTime = dict::getDict('sale_purchase_order_valid');
+        $current = time() + $validTime;
+        $data['deadline'] = $data['deadline'] ?? date("Y-m-d H:i:s", $current);
+        $data['status'] = self::ORDER_STATUS_UN_PAY;
+        $currentShopId = $data['shopId'] ?? 0;
+        //计算总重量(kg)
+        $weight = 0;
+        $productMapData = ProductClass::getProductMapData($product);
+        foreach ($product as $key => $val) {
+            $w = ProductClass::getWeight($val['productId'], $val['bigNum'], $val['smallNum']);
+            $productId = $val['productId'];
+            $weight = bcadd($w, $weight, 2);
+            $checkStock = true;
+            $stockInfo = ProductClass::decreaseStock($val['productId'], $val['bigNum'], $val['smallNum'], $checkStock);
+            //记录库存流水
+            $recordData = [];
+            $recordData['sjId'] = $sjId;
+            $recordData['shopId'] = $currentShopId;
+            $recordData['itemId'] = $productMapData[$productId]['itemId'] ?? 0;
+            $ratio = $productMapData[$productId]['ratio'] ?? 0;
+            $recordData['itemNum'] = ProductClass::mergeItemNum($val['bigNum'], $val['smallNum'], $ratio);
+            $recordData['oldStock'] = $stockInfo['oldStock'];
+            $recordData['newStock'] = $stockInfo['newStock'];
+            $recordData['productId'] = $productId;
+            $recordData['orderSn'] = $orderSn;
+            StockRecordService::addSellStockOrderRecord($recordData);
+        }
+        $data['weight'] = $weight;
+        $current = time() + 300;
+        $data['deadline'] = isset($data['deadline']) ? $data['deadline'] : date("Y-m-d H:i:s", $current);
+        //修改订单暂时不打印
+        $data['needPrint'] = dict::getDict('needPrint', 'noNeed');
+
+        $orderId = $order->id ?? 0;
+        self::updateById($orderId, $data);
+        $return = self::getLockById($orderId);
+
+        //门店总订单数+1
+        $shop = ShopClass::getLockById($currentShopId);
+        if (empty($shop)) {
+            util::fail('没有找到门店');
+        }
+        $shop->unPayOrder += 1;
+        $shop->cancelOrder -= 1;
+        $shop->save();
+
+        return $return;
+    }
+
     //根据订单编号查询 ssh 2021.19
     public static function getByOrderSn($orderSn, $returnObj = false)
     {

+ 70 - 6
biz-ghs/order/services/OrderService.php

@@ -33,8 +33,8 @@ class OrderService extends BaseService
     public static function createOrder($data, $custom)
     {
         //供应商销售单
-        $return = OrderClass::addOrder($data);
-        $saleId = $return->id ?? 0;
+        $returnOrder = OrderClass::addOrder($data);
+        $saleId = $returnOrder->id ?? 0;
 
         $customSjId = $custom['sjId'] ?? 0;
         $customShopId = $custom['shopId'] ?? 0;
@@ -106,18 +106,82 @@ class OrderService extends BaseService
         $purchase = PurchaseClass::addPurchase($purchaseData);
         $purchaseId = $purchase->id ?? 0;
 
-        $return->purchaseId = $purchaseId;
-        $return->save();
+        $returnOrder->purchaseId = $purchaseId;
+        $returnOrder->save();
 
         //除了自取订单直接变成已确认(欠款或已收款方式确认),其它都进入待确认流程 shish 20210809
         if (isset($data['sendType']) && $data['sendType'] == OrderClass::SEND_TYPE_NO) {
             PurchaseService::payAfter($purchase, $payWay);
-            self::payAfter($return, $payWay);
+            self::payAfter($returnOrder, $payWay);
         }
 
-        return $return->attributes;
+        return $returnOrder;
     }
 
+    //修改订单
+    public static function updateOrder($order, $data)
+    {
+        $purchaseId = $order->purchaseId ?? 0;
+        $purchase = PurchaseClass::getLockById($purchaseId);
+
+        //修改供应商销售单
+        $returnOrder = OrderClass::updateOrder($order, $data);
+
+        //供应商销售单的 0未欠款 1欠款 转成零售采购单的 1欠款 2未欠款
+        $debt = $data['debt'] ?? 0;
+        $purchaseDebt = $debt == 1 ? PurchaseClass::DEBT_YES : PurchaseClass::DEBT_NO;
+
+        $payWay = $data['payWay'] ?? dict::getDict('payWay', 'wxPay');
+        if ($debt == 1) {
+            $payWay = dict::getDict('payWay', 'debtPay');
+        }
+
+        //自取还是配送订单,销售单与采购单之间转换
+        $sendType = $data['sendType'] ?? OrderClass::SEND_TYPE_MYSELF;
+        $getType = $sendType == OrderClass::SEND_TYPE_NO ? PurchaseClass::GET_TYPE_SELF_GET : PurchaseClass::GET_TYPE_SEND;
+
+        //供应商价格是可以修改的,修改的价格一起传给采购单保存
+        $currentProduct = $data['product'];
+        foreach ($currentProduct as $currentKey => $currentItem) {
+            if (isset($currentProduct[$currentKey]['price'])) {
+                $currentProduct[$currentKey]['userPrice'] = $currentProduct[$currentKey]['price'];
+                unset($currentProduct[$currentKey]['price']);
+            }
+        }
+
+        $purchaseData = [
+            'product' => $currentProduct,
+            'sendTime' => date("H:i:s"),
+            'sendCost' => $data['sendCost'] ?: '0.00',
+            'payWay' => $payWay,
+            'status' => PurchaseClass::STATUS_UN_PAY,
+            'cgStyle' => dict::getDict('cgStyle', 'ghs'),
+            'debt' => $purchaseDebt,
+            'getType' => $getType,
+        ];
+        if (isset($data['deadline']) && !empty($data['deadline'])) {
+            $purchaseData['deadline'] = $data['deadline'];
+        }
+        if (isset($data['modifyPrice']) && is_numeric($data['modifyPrice'])) {
+            //优惠之后的金额
+            if (is_numeric($data['modifyPrice']) == false || $data['modifyPrice'] <= 0) {
+                util::fail('金额填错了哦');
+            }
+            $purchaseData['modifyPrice'] = $data['modifyPrice'];
+        }
+        //修改采购单
+        $purchase = PurchaseClass::updatePurchase($purchase, $purchaseData);
+
+        //除了自取订单直接变成已确认,其它都进入待确认流程。订单的确认以欠款或已收款方式确认。 shish 20210809
+        if (isset($data['sendType']) && $data['sendType'] == OrderClass::SEND_TYPE_NO) {
+            PurchaseService::payAfter($purchase, $payWay);
+            self::payAfter($returnOrder, $payWay);
+        }
+        return $returnOrder;
+
+    }
+
+
     //自取免配送流程处理 ssh 2021.1.24
     public static function withoutSend($order)
     {

+ 142 - 1
biz-hd/purchase/classes/PurchaseClass.php

@@ -171,15 +171,156 @@ class PurchaseClass extends BaseClass
         $current = time() + $validTime;
         $data['deadline'] = $data['deadline'] ?? date("Y-m-d H:i:s", $current);
         $respond = self::add($data, true);
+        PurchaseItemClass::batchAdd($productList);
+        $shop = ShopClass::getLockById($shopId);
+        if (empty($shop)) {
+            util::fail('没有找到门店');
+        }
+        $shop->totalPurchaseOrder += 1;
+        $shop->cgUnPay += 1;
+        $shop->save();
+
+        return $respond;
+    }
+
+    //添加采购 ssh 2021.1.18
+    public static function updatePurchase($purchase, $data)
+    {
+        $orderSn = $purchase->orderSn ?? '';
+        $shopId = $purchase->shopId ?? 0;
+        $sjId = $purchase->sjId ?? 0;
+
+        $prePrice = 0;
+
+        $data['sendTimeWant'] = date("Y-m-d") . ' ' . $data['sendTime'];
+        unset($data['sendTime']);
+
         if (isset($data['product']) == false || empty($data['product'])) {
             util::fail('请选择花材');
         }
+        $productList = $data['product'];
+        $productList = self::mergeItemInfo($productList);
+        $data['kindNum'] = count($productList); //多少种花材
+        //供应商productId转成零售的productId
+        $productList = ProductClass::toggleProductList($productList, $shopId, $sjId);
+
+        $productData = self::getProductMapData($productList, "productId");
+        $hdProductData = self::getProductMapData($productList, "hdProductId");
+
+        //计算价格= 各个productId的price*num(bigNum,smallNum) + sendCost(配送费)
+        $bigNum = 0; //总共多少扎
+        $smallNum = 0; //总共多少支
+        foreach ($productList as $key => $product) {
+
+            $bigNum += $product['bigNum'] ?? 0;
+            $smallNum += $product['smallNum'] ?? 0;
+            $productList[$key]['orderSn'] = $orderSn;
+            $productId = $product['productId'];
+            $hdProductId = $product['hdProductId'];
+
+            $ratio = $hdProductData[$hdProductId]['ratio'] ?? 0; //比例必须一致
+            $itemId = $hdProductData[$hdProductId]['itemId'] ?? 0;
+            $rank = $hdProductData[$hdProductId]['rank'] ?? 'B';// 花材级别
+            $stock = $hdProductData[$hdProductId]['stock'] ?? 0;//当时库存
+            $stockFormat = ProductClass::formatStock($stock, $ratio);
+
+            $itemNum = ProductClass::mergeItemNum($product['bigNum'], $product['smallNum'], $ratio);//采购数量
+            $price = $productData[$productId]['price']; //卖的单价
+            //每个花材总价(原价)
+            $productPrePrice = bcmul($itemNum, $price, 2); //采购总价
+            //供应商开单的每个花材总价,可能会改价
+            $productTotalPrice = isset($product['userPrice']) && $product['userPrice'] > 0 ? $product['userPrice'] : $productPrePrice;
+
+            $itemInfo = [
+                'itemName' => $hdProductData[$hdProductId]['name'] ?? '',//花材名称
+                'itemCover' => $hdProductData[$hdProductId]['cover'] ?? '',//花材图片
+                'bigNum' => $product['bigNum'] ?? 0,//采购数量(大单位)
+                'smallNum' => $product['smallNum'] ?? 0,//采购数量(小单位)
+                'bigNumStock' => $stockFormat['bigNum'],// 当时库存大单位数
+                'smallNumStock' => $stockFormat['smallNum'],//当时库存 小单位数
+                'itemUnit' => $ratio ? 2 : 1,//单位
+                'ratio' => $ratio,// 比例
+                'rank' => $rank,// 花材级别
+                'bigUnit' => $hdProductData[$hdProductId]['bigUnit'],//大单位名称 扎
+                'smallUnit' => $hdProductData[$hdProductId]['smallUnit'],//小单位名称 支
+            ];
+
+            $productList[$key]['itemInfo'] = json_encode($itemInfo);
+            $productList[$key]['itemId'] = $itemId;
+            $productList[$key]['itemNum'] = $itemNum;
+            $productList[$key]['price'] = $price;
+            $productList[$key]['ratio'] = $ratio;
+            $productList[$key]['totalPrice'] = $productTotalPrice;
+            $productList[$key]['preTotalPrice'] = $productPrePrice;
+            $productList[$key]['productId'] = $hdProductId;
+            $productList[$key]['ghsProductId'] = $productId;
+            //小单位单价
+            $smallPrice = bcdiv($price, $ratio, 2);
+            $productList[$key]['smallPrice'] = $smallPrice;
+
+            $prePrice = bcadd($prePrice, $productTotalPrice, 2);
+
+        }
+        //删除花材再添加
+        PurchaseItemClass::deleteByCondition(['orderSn' => $orderSn]);
         PurchaseItemClass::batchAdd($productList);
+
+        $data['itemPrice'] = $prePrice;
+        $sendCost = $data['sendCost'] ?? 0.00;
+        $prePrice = bcadd($prePrice, $sendCost, 2);
+        $actPrice = $prePrice;
+        $realPrice = $prePrice;
+        if (isset($data['modifyPrice'])) {
+            //商家打折,修改金额
+            $modifyPrice = $data['modifyPrice'] ?? 0;
+            if ($modifyPrice > $prePrice) {
+                util::fail('实收金额大于订单总金额');
+            }
+            if ($modifyPrice < $prePrice) {
+                $data['discountAmount'] = bcsub($prePrice, $modifyPrice, 2);
+                $data['discountType'] = dict::getDict('discountType', 'discount');
+                $actPrice = $modifyPrice;
+                $realPrice = $modifyPrice;
+            }
+        } else {
+            //验证优惠券
+            $couponId = isset($data['couponId']) ? $data['couponId'] : 0;
+            if (!empty($couponId)) {
+                $coupon = ShopCouponClass::getLockById($couponId);
+                ShopCouponClass::valid($coupon, $data['shopId']);
+                if ($coupon->miniCost != 0 && $coupon->miniCost > $prePrice) {
+                    util::fail('达到最低消费才能使用优惠券');
+                }
+                if ($prePrice < $coupon->amount) {
+                    util::fail('总价应大于优惠金额');
+                }
+                $coupon->take = ShopCouponClass::TAKE_HAS;
+                $coupon->save();
+                $realPrice = bcsub($actPrice, $coupon->amount, 2);
+                $data['discountType'] = dict::getDict('discountType', 'usePtCoupon');
+                $data['discountAmount'] = $coupon->amount;
+            }
+            $data['couponId'] = $couponId;
+        }
+        $data['prePrice'] = $prePrice;
+        $data['actPrice'] = $actPrice;
+        $data['realPrice'] = $realPrice;
+        $data['bigNum'] = $bigNum;
+        $data['smallNum'] = $smallNum;
+        //采购单有效期
+        $validTime = dict::getDict('sale_purchase_order_valid');
+        $current = time() + $validTime;
+        $data['deadline'] = $data['deadline'] ?? date("Y-m-d H:i:s", $current);
+
+        $purchaseId = $pur->id ?? 0;
+        self::updateById($purchaseId, $data);
+        $respond = self::getLockById($purchaseId);
+
         $shop = ShopClass::getLockById($shopId);
         if (empty($shop)) {
             util::fail('没有找到门店');
         }
-        $shop->totalPurchaseOrder += 1;
+        $shop->cancelOrder -= 1;
         $shop->cgUnPay += 1;
         $shop->save();