shish 3 年 前
コミット
010ce76345

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

@@ -116,6 +116,26 @@ class PurchaseOrderController extends BaseController
         util::complete();
     }
 
+    //取消入库单 ssh 20221211
+    public function actionCancel()
+    {
+        $get = Yii::$app->request->get();
+        $id = $get['id'] ?? 0;
+        $cg = PurchaseOrderClass::getLockById($id);
+        if (isset($cg->mainId) == false || $cg->mainId != $this->mainId) {
+            util::fail('请确认是否您的采购单');
+        }
+        if ($cg->status == PurchaseOrderClass::PURCHASE_ORDER_STATUS_CANCEL) {
+            util::fail('已取消过了');
+        }
+        if ($cg->status == PurchaseOrderClass::PURCHASE_ORDER_STATUS_COMPLETE) {
+            util::fail('已入库,无法取消');
+        }
+        $cg->status = PurchaseOrderClass::PURCHASE_ORDER_STATUS_CANCEL;
+        $cg->save();
+        util::complete();
+    }
+
     //确认入库 sssh 20221211
     public function actionConfirmPutIn()
     {
@@ -127,11 +147,16 @@ class PurchaseOrderController extends BaseController
         try {
             $get = Yii::$app->request->get();
             $id = $get['id'] ?? 0;
-            $cg = PurchaseOrderClass::getById($id, true);
+            $cg = PurchaseOrderClass::getLockById($id);
             if (isset($cg->mainId) == false || $cg->mainId != $this->mainId) {
                 util::fail('请确认是否您的采购单');
             }
-            $return = PurchaseOrderClass::putIn($cg);
+            $staff = $this->shopAdmin;
+            $data = [];
+            $data['staffId'] = $staff->id ?? 0;
+            $data['staffName'] = $staff->name ?? '';
+            $data['adminId'] = $this->adminId ?? 0;
+            $return = PurchaseOrderClass::putIn($cg, $data);
             $transaction->commit();
             util::success($return);
         } catch (\Exception $e) {

+ 214 - 4
biz-ghs/order/classes/PurchaseOrderClass.php

@@ -33,12 +33,14 @@ class PurchaseOrderClass extends BaseClass
     const PURCHASE_ORDER_STATUS_UN_SEND = 2; //待配送,待发货
     const PURCHASE_ORDER_STATUS_SENDING = 3; //配送中,待入库
     const PURCHASE_ORDER_STATUS_COMPLETE = 4; //已入库
+    const PURCHASE_ORDER_STATUS_CANCEL = 5; //已取消
 
     public static $statusMap = [
         self::PURCHASE_ORDER_STATUS_WAIT => '待确认',
         self::PURCHASE_ORDER_STATUS_UN_SEND => '待配送',
         self::PURCHASE_ORDER_STATUS_SENDING => '待入库',
         self::PURCHASE_ORDER_STATUS_COMPLETE => '已入库',
+        self::PURCHASE_ORDER_STATUS_CANCEL => '已取消',
     ];
 
     const DEBT_UNKNOWN = 0;
@@ -53,9 +55,214 @@ class PurchaseOrderClass extends BaseClass
     const IN_TYPE_LATER = 0;
     const IN_TYPE_NOW = 1;
 
-    public static function putIn($cg)
+    public static function putIn($cg, $data)
     {
-        return $cg;
+        if ($cg->status == self::PURCHASE_ORDER_STATUS_COMPLETE) {
+            util::fail('已经入过库了');
+        }
+        if ($cg->status == self::PURCHASE_ORDER_STATUS_CANCEL) {
+            util::fail('已取消');
+        }
+        $orderSn = $cg->orderSn ?? '';
+        $mainId = $cg->mainId ?? 0;
+        $sjId = $cg->sjId ?? 0;
+        $shopId = $cg->shopId ?? 0;
+        $staffId = $data['staffId'] ?? 0;
+        $staffName = $data['staffName'] ?? '';
+        $adminId = $data['adminId'] ?? 0;
+        $shop = ShopClass::getLockById($shopId);
+        if (empty($shop)) {
+            util::fail('没有找到门店');
+        }
+        $ghsName = $cg->ghsName ?? '';
+        $itemList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
+        if (empty($itemList)) {
+            util::fail('没有找到花材');
+        }
+        foreach ($itemList as $cgKey => $cgItem) {
+            $productId = $cgItem['productId'] ?? 0;
+            $currentNum = $cgItem['itemNum'] ?? 0;
+            $ptItemId = $cgItem['itemId'] ?? 0;
+            $bigPrice = $cgItem['bigPrice'] ?? 0;
+            $bigFreight = $cgItem['bigFreight'] ?? 0;
+
+            $product = ProductClass::getLockById($productId);
+            if (empty($product)) {
+                util::fail('没有找到花材哦');
+            }
+
+            //加库存和流水记录
+            $stockInfo = ProductClass::addStockByItemNum($productId, $currentNum);
+            $record = [];
+            $record['sjId'] = $sjId;
+            $record['shopId'] = $shopId;
+            $record['mainId'] = $mainId;
+            $record['orderSn'] = $orderSn;
+            $record['productId'] = $productId;
+            $record['itemId'] = $ptItemId;
+            $record['relateName'] = $ghsName;
+            $record['itemNum'] = $currentNum;
+            $record['oldStock'] = $stockInfo['oldStock'];
+            $record['newStock'] = $stockInfo['newStock'];
+            StockRecordClass::addPurchaseOrderRecord($record);
+
+            $unitCost = bcadd($bigPrice, $bigFreight, 2);
+            $product->cost = $unitCost;
+            $product->priceLabel = ProductClass::PRICE_LABEL_AUTO;
+            $product->save();
+            //成本变动记录
+            $changeData = [
+                'ptStyle' => 2,
+                'sjId' => $sjId,
+                'mainId' => $mainId,
+                'itemId' => $productId,
+                'ptItemId' => $ptItemId,
+                'cost' => $unitCost,
+                'staffId' => $staffId,
+                'staffName' => $staffName,
+                'name' => $product->name ?? '',
+                'cover' => $product->cover ?? '',
+                'targetId' => $order->id ?? 0,
+                'event' => '采购',
+            ];
+            CostChangeClass::addData($changeData);
+            if (isset($shop->default) && $shop->default == 1) {
+                //直营门店的成本价也要一起改掉
+                $allShop = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
+                if (!empty($allShop)) {
+                    $currentItemId = $product->itemId ?? 0;
+                    foreach ($allShop as $currentShop) {
+                        $currentId = $currentShop->mainId ?? 0;
+                        if ($currentId == $mainId) {
+                            continue;
+                        }
+                        if (isset($currentShop->join) && $currentShop->join == 1) {
+                            continue;
+                        }
+                        $currentProduct = ProductClass::getByCondition(['mainId' => $currentId, 'itemId' => $currentItemId], true);
+                        if (!empty($currentProduct)) {
+                            $currentProduct->cost = $unitCost;
+                            $currentProduct->save();
+                            $currentStaff = ShopAdminClass::getByCondition(['mainId' => $currentProduct->mainId, 'adminId' => $adminId], true);
+                            //成本变动记录
+                            $changeData = [
+                                'ptStyle' => 2,
+                                'sjId' => $currentProduct->sjId ?? 0,
+                                'mainId' => $currentProduct->mainId ?? 0,
+                                'itemId' => $currentProduct->id ?? 0,
+                                'ptItemId' => $currentProduct->itemId ?? 0,
+                                'cost' => $unitCost,
+                                'staffId' => $currentStaff->id ?? 0,
+                                'staffName' => $currentStaff->name ?? '',
+                                'name' => $currentProduct->name ?? '',
+                                'cover' => $currentProduct->cover ?? '',
+                                'targetId' => $order->id ?? 0,
+                                'event' => $shop->shopName . '采购',
+                            ];
+                            CostChangeClass::addData($changeData);
+                        }
+                    }
+                }
+            }
+        }
+
+        //门店支出增加
+        $currentPrice = $cg->actPrice ?? 0;
+        $mainId = $shop->mainId ?? 0;
+        $main = MainClass::getLockById($mainId);
+        if (empty($main)) {
+            util::fail('没有资产信息');
+        }
+        //支出增加
+        $currentTotalExpend = bcadd($main->totalExpend, $currentPrice, 2);
+        $main->totalExpend = $currentTotalExpend;
+        $main->save();
+
+        //采购增加
+        $main->cgFinish += 1;
+        $main->totalPurchaseOrder += 1;
+        $currentTotalPurchase = bcadd($main->totalPurchase, $currentPrice, 2);
+        $main->totalPurchase = $currentTotalPurchase;
+        $main->save();
+
+        //供货商资产增加
+        $ghsId = $cg->ghsId ?? 0;
+        $ghs = GhsClass::getLockById($ghsId);
+        if (empty($ghs)) {
+            util::fail('没有找到供货商');
+        }
+        $ghs->debt = GhsClass::DEBT_YES;
+        $ghs->debtAmount = bcadd($ghs->debtAmount, $currentPrice, 2);
+
+        $ghs->debtNum += 1;
+        $ghs->expendAmount = bcadd($ghs->expendAmount, $currentPrice, 2);
+        $ghs->expendNum += 1;
+        $ghs->save();
+
+        //采购统计
+        $cgNum = $order->itemNum ?? 0;
+        StatCgClass::replace($main, $shop, $currentPrice, $cgNum);
+        //采购按供货商统计
+        StatCgGhsClass::ghsReplace($cg);
+
+        //门店应收客户款增加
+        $ghsShopId = $ghs->shopId;
+        $ghsShop = ShopClass::getLockById($ghsShopId);
+        if (empty($ghsShop)) {
+            util::fail('没有找到门店');
+        }
+        $ghsMainId = $ghsShop->mainId ?? 0;
+        $ghsMain = \bizGhs\shop\classes\MainClass::getLockById($ghsMainId);
+        if (empty($ghsMain)) {
+            util::fail('没有main信息');
+        }
+        $currentMayGathering = bcadd($ghsMain->mayGathering, $currentPrice, 2);
+        $ghsMain->mayGathering = $currentMayGathering;
+
+        //客户资产增加
+        $customId = $ghs->customId;
+        $custom = CustomClass::getLockById($customId);
+        if (empty($custom)) {
+            util::fail('没有找到客户信息');
+        }
+        $custom->isDebt = CustomClass::IS_DEBT_YES;
+        $custom->debtAmount = bcadd($custom->debtAmount, $currentPrice, 2);
+        if ($custom->debtNum == 0) {
+            $ghsMain->mayGatheringNum += 1;
+        }
+        $ghsMain->save();
+        $custom->debtNum += 1;
+        $custom->buyNum += 1;
+        $custom->buyAmount = bcadd($custom->buyAmount, $currentPrice, 2);
+        $custom->save();
+
+        //当天和当月支出统计
+        StatOutClass::updateOrInsert($main, $shop, $currentPrice);
+        //支出流水
+        $payWay = dict::getDict('payWay', 'unknown');
+        $capitalType = dict::getDict('capitalType', 'ghsPurchase', 'id');
+        $sjId = $order->sjId ?? 0;
+        $shopId = $order->shopId ?? 0;
+        $event = '采购';
+        $mainId = $shop->mainId ?? 0;
+        $capitalData = [
+            'capitalType' => $capitalType,
+            'io' => 0,
+            'totalExpend' => $currentTotalExpend,
+            'payWay' => $payWay,
+            'amount' => $currentPrice,
+            'sjId' => $sjId,
+            'shopId' => $shopId,
+            'event' => $event,
+            'mainId' => $mainId
+        ];
+        ShopCapitalClass::addCapital($capitalData);
+
+        $cg->debt = self::DEBT_YES;
+        $cg->status = self::PURCHASE_ORDER_STATUS_COMPLETE;
+        $cg->entryTime = date("Y-m-d H:i:s");
+        $cg->save();
+
     }
 
     //添加采购单 lqh 2021.1.19
@@ -140,8 +347,11 @@ class PurchaseOrderClass extends BaseClass
             unset($data['avgKgWeight']);
         }
 
-        if (empty($data['entryTime'])) {
-            $data['entryTime'] = date('Y-m-d H:i:s');
+        //直接入库的有入库时间
+        if ($inType == self::IN_TYPE_NOW) {
+            if (empty($data['entryTime'])) {
+                $data['entryTime'] = date('Y-m-d H:i:s');
+            }
         }
 
         $packingCharge = $data['packingCharge'] ?? 0;