Ver código fonte

Merge branch 'avcost'

shish 9 meses atrás
pai
commit
c7ac56a544

+ 2 - 2
app-ghs/controllers/CgRefundController.php

@@ -129,7 +129,7 @@ class CgRefundController extends BaseController
     {
 
         $shopAdmin = $this->shopAdmin;
-        if (isset($shopAdmin['super']) == false || $shopAdmin['super'] != 1) {
+        if (!isset($shopAdmin['super']) || $shopAdmin['super'] != 1) {
             util::fail('超管才能操作退款');
         }
         $connection = Yii::$app->db;
@@ -137,7 +137,7 @@ class CgRefundController extends BaseController
         try {
 
             $post = Yii::$app->request->post();
-            $id = isset($post['id']) ? $post['id'] : 0;
+            $id = $post['id'] ?? 0;
             $order = PurchaseOrderClass::getLockById($id);
 
             if ($order->mainId != $this->mainId) {

+ 10 - 6
app-ghs/controllers/CheckOrderController.php

@@ -158,7 +158,7 @@ class CheckOrderController extends BaseController
     public function actionSendStock()
     {
         $shopAdmin = $this->shopAdmin;
-        if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
+        if (!isset($shopAdmin->super) || $shopAdmin->super != 1) {
             util::fail('超管才能盘点');
         }
         $post = Yii::$app->request->post();
@@ -207,7 +207,11 @@ class CheckOrderController extends BaseController
         $post['shopId'] = $this->shopId;
         $post['adminId'] = $this->adminId;
         $post['mainId'] = $this->mainId;
-        $order = $this->saveOrder($post, false, false);
+
+        $staff = $this->shopAdmin;
+        $post['staffName'] = $staff->name ?? '';
+
+        $order = $this->saveOrder($post, false, false, ['motherId' => $motherId]);
         util::success($order);
     }
 
@@ -306,10 +310,10 @@ class CheckOrderController extends BaseController
         util::success($order);
     }
 
-    protected function saveOrder($post, $draft = false, $update = false)
+    protected function saveOrder($post, $draft = false, $update = false, $params = [])
     {
         $shopAdmin = $this->shopAdmin;
-        if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
+        if (!isset($shopAdmin->super) || $shopAdmin->super != 1) {
             util::fail('超管才能操作');
         }
 
@@ -332,9 +336,9 @@ class CheckOrderController extends BaseController
         }
         $post['itemInfo'] = $ghsItemInfo;
         if ($update === true) {
-            $order = CheckOrderClass::opOrder($post, $draft, $update);
+            $order = CheckOrderClass::opOrder($post, $draft, $update, true, $params);
         } else {
-            $order = CheckOrderClass::opOrder($post, $draft, $update);
+            $order = CheckOrderClass::opOrder($post, $draft, $update, true, $params);
         }
         return $order;
     }

+ 9 - 0
app-ghs/controllers/StockOutController.php

@@ -258,6 +258,10 @@ class StockOutController extends BaseController
             util::fail('花材格式不正确');
         }
 
+        //避免重复提交
+        $adminId = $this->adminId;
+        util::checkRepeatCommit($adminId, 5);
+
         $connection = Yii::$app->db;
         $transaction = $connection->beginTransaction();
 
@@ -274,6 +278,11 @@ class StockOutController extends BaseController
             ShopClass::valid($inShop, $this->sjId);
             $post['inMainId'] = $inShop['mainId'] ?? 0;
 
+            $staff = $this->shopAdmin;
+            $post['staffName'] = $staff->name ?? '';
+            $post['staffId'] = $staff->id ?? 0;
+            $post['adminId'] = $this->adminId;
+
             ProductClass::valid($ghsItemInfo, $this->mainId);
             //判断花材里的信息
             foreach ($ghsItemInfo as $v) {

+ 2 - 1
app-ghs/controllers/TestController.php

@@ -419,7 +419,8 @@ class TestController extends BaseController
             //['name' => '泉州馨喜鲜花批发', 'merchantNo' => '8223970599200EC', 'wx' => [818293487, 818293387], 'zfb' => '2088080143950734'],
             //['name' => '广州王艺百合', 'merchantNo' => '8225810599201FZ', 'wx' => [817598762, 817598757], 'zfb' => '2088080092969250'],
             //['name' => '广州鑫源花卉批发', 'merchantNo' => '8225810599201G3', 'wx' => [819164143, 819164039], 'zfb' => '2088080202837723'],
-            ['name' => '昆明小向', 'merchantNo' => '8227310599200VK', 'wx' => [821387260, 821388013], 'zfb' => '2088080376224927'],
+            ['name' => '固始县花语鲜花批发', 'merchantNo' => '82251505992004R', 'wx' => [822243163, 822242931], 'zfb' => '2088080448601764'],
+			['name' => '昆明小向', 'merchantNo' => '8227310599200VK', 'wx' => [821387260, 821388013], 'zfb' => '2088080376224927'],
             ['name' => '张家口实惠花屋', 'merchantNo' => '82213805992004V', 'wx' => [820489785, 820487296], 'zfb' => '2088080312197467'],
             ['name' => '深圳鼎盛皇室花卉', 'merchantNo' => '8225840599201MF', 'wx' => [807078304, 807076486], 'zfb' => '2088970117746334'],
             //['name' => '东莞惠雅鲜花(莞城店)', 'merchantNo' => '8226020599200GJ', 'wx' => [796953190, 796976959], 'zfb' => '2088770940104912'],

+ 7 - 1
biz-ghs/cg/classes/CgRefundItemClass.php

@@ -22,13 +22,14 @@ class CgRefundItemClass extends BaseClass
         $refundSn = $data['refundSn'] ?? '';
         $ptItemId = $data['ptItemId'] ?? 0;
         $name = $data['name'] ?? '';
+        $unitPrice = $data['unitPrice'] ?? 0;
 
         $mainId = $order->mainId ?? 0;
         $shopId = $order->shopId ?? 0;
         $sjId = $order->sjId ?? 0;
         $ghsName = $order->ghsName ?? '';
 
-        $stockInfo = ProductClass::decreaseStock($productId, $bigNum, $smallNum);
+        $stockInfo = ProductClass::decreaseStock($productId, $bigNum, $smallNum, false, true, ['cost' => $unitPrice]);
         $oldStock = $stockInfo['oldStock'] ?? 0;
         $newStock = $stockInfo['newStock'] ?? 0;
         if ($newStock < 0) {
@@ -47,6 +48,11 @@ class CgRefundItemClass extends BaseClass
         $recordData['productId'] = $productId;
         $recordData['newStock'] = $newStock;// 最新库存
         $recordData['relateName'] = $ghsName;
+        $recordData['avCost'] = $stockInfo['avCost'] ?? 0;
+        $recordData['totalCost'] = $stockInfo['totalCost'] ?? 0;
+        $recordData['totalStock'] = $stockInfo['totalStock'] ?? 0;
+        $recordData['changeCost'] = $stockInfo['changeCost'] ?? 0;
+        $recordData['unitCost'] = $stockInfo['unitCost'] ?? 0;
         StockRecordClass::ghsCgApplyRefundAddRecord($recordData);
 
         self::add($data);

+ 48 - 6
biz-ghs/order/classes/CheckOrderClass.php

@@ -267,7 +267,7 @@ class CheckOrderClass extends BaseClass
     }
 
     //2021.1.22 lqh 操作订单  添加/保存草稿/草稿编辑保存: 区别在添加是立即改库存和记录流水记录,而保存草稿是只保存订单信息
-    public static function opOrder($data, $draft = false, $update = false, $check = true)
+    public static function opOrder($data, $draft = false, $update = false, $check = true, $params = [])
     {
         $sjId = $data['sjId'];
         $shopId = $data['shopId'];
@@ -324,6 +324,16 @@ class CheckOrderClass extends BaseClass
             $batchData = self::groupOrderItem($ghsItemInfo, $orderSn);
             CheckOrderItemClass::batchAddOrderItem($batchData);
 
+            $avCost = 0;
+            $changeCost = 0;
+            $unitCost = 0;
+            $totalCost = 0;
+            $totalStock = 0;
+            if (isset($params['motherId']) && $params['motherId'] > 0) {
+                $motherInfo = ProductClass::getById($params['motherId'], true);
+                $unitCost = $motherInfo->avCost ?? 0;
+            }
+
             $addAmount = 0;
             $subAmount = 0;
             $ids = [];
@@ -338,32 +348,64 @@ class CheckOrderClass extends BaseClass
                     if (!$lock) {
                         util::fail("系统繁忙,请稍后再试");
                     }
-                    $productData = ProductClass::getById($productId);
+                    $productData = ProductClass::getById($productId, true);
+
+                    if (isset($params['motherId']) && $params['motherId'] > 0) {
+                        $changeNum = bcsub($productData->stock,$v['itemNum'],2);
+                        $changeNum = abs($changeNum);
+                        $changeCost = bcmul($unitCost, $changeNum, 2);
+                        $totalCost = $productData->totalCost ?? 0;
+                        $totalStock = $productData->totalStock ?? 0;
+                        if ($productId == $params['motherId']) {
+                            $totalCost = bcsub($totalCost, $changeCost, 2);
+                            $totalStock = bcsub($totalStock, $changeNum, 2);
+                            $avCost = $unitCost;
+                            $productData->avCost = $avCost;
+                            $productData->totalCost = $totalCost;
+                            $productData->totalStock = $totalStock;
+                            $productData->save();
+                        } else {
+                            $totalCost = bcadd($changeCost, $totalCost, 2);
+                            $totalStock = bcadd($changeNum, $totalStock, 2);
+                            $avCost = bcdiv($totalCost, $totalStock, 2);
+                            $productData->avCost = $avCost;
+                            $productData->totalCost = $totalCost;
+                            $productData->totalStock = $totalStock;
+                            $productData->save();
+                        }
+                    }
+
                     ProductClass::updateStockById($productId, $v['itemNum'], $adminId);
                     //上架
-                    if ($v['itemNum'] > 0 && $productData['status'] == 2) {
+                    if ($v['itemNum'] > 0 && $productData->status == 2) {
                         ProductClass::changeStatus($productId, 1);
                     }
                     //下架
-                    if ($v['itemNum'] <= 0 && $productData['status'] == 1) {
+                    if ($v['itemNum'] <= 0 && $productData->status == 1) {
                         ProductClass::changeStatus($productId, 2);
                     }
                     util::unlock($key);
+
                     //流水记录
                     $record = [];
                     $record['sjId'] = $sjId;
                     $record['shopId'] = $shopId;
-                    $record['mainId'] = $productData['mainId'];
+                    $record['mainId'] = $productData->mainId ?? 0;
                     $record['orderSn'] = $orderSn;
                     $record['ptStyle'] = 2;
                     $record['relateName'] = $data['staffName'] ?? '';
                     $record['itemId'] = $v['itemId'];
                     $record['productId'] = $productId;
                     $record['itemNum'] = abs($v['profitLossNum']);
-                    $record['oldStock'] = $productData['stock'];
+                    $record['oldStock'] = $productData->stock ?? 0;
                     $record['newStock'] = $v['itemNum'];
                     $record['targetId'] = $order->id ?? 0;
                     $record['remark'] = $remark;
+                    $record['avCost'] = $avCost;
+                    $record['changeCost'] = $changeCost;
+                    $record['unitCost'] = $unitCost;
+                    $record['totalStock'] = $totalStock;
+                    $record['totalCost'] = $totalCost;
                     StockRecordClass::addCheckOrderRecord($record);
 
                     $amount = $v['amount'] ?? 0;

+ 1 - 1
biz-ghs/order/classes/OrderClass.php

@@ -381,7 +381,7 @@ class OrderClass extends BaseClass
                     $className = isset($classList[$classId]) && isset($classList[$classId]['name']) ? $classList[$classId]['name'] : '';
                     $objPHPExcel->getActiveSheet()->setCellValue('T' . $i, $className);
                     $objPHPExcel->getActiveSheet()->setCellValue('U' . $i, $orderInfo['remark']);
-                    $objPHPExcel->getActiveSheet()->setCellValue('V' . $i, $orderInfo['cost']);
+                    $objPHPExcel->getActiveSheet()->setCellValue('V' . $i, $orderInfo['avCost']);
 
                     //居左
                     $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

+ 8 - 2
biz-ghs/order/classes/PurchaseOrderClass.php

@@ -547,6 +547,7 @@ class PurchaseOrderClass extends BaseClass
             $ptItemId = $cgItem['itemId'] ?? 0;
             $unitCost = $cgItem['cost'] ?? 0;
             $presell = $cgItem['presell'] ?? 0;
+            $bigPrice = $cgItem['bigPrice'] ?? 0;
 
             $product = ProductClass::getLockById($productId);
             if (empty($product)) {
@@ -556,7 +557,7 @@ class PurchaseOrderClass extends BaseClass
             if ($presell == 0) {
 
                 //加库存和流水记录
-                $stockInfo = ProductClass::addStockByItemNum($productId, $currentNum);
+                $stockInfo = ProductClass::addStockByItemNum($productId, $currentNum, ['cost' => $bigPrice]);
                 $record = [];
                 $record['sjId'] = $sjId;
                 $record['shopId'] = $shopId;
@@ -568,6 +569,11 @@ class PurchaseOrderClass extends BaseClass
                 $record['itemNum'] = $currentNum;
                 $record['oldStock'] = $stockInfo['oldStock'];
                 $record['newStock'] = $stockInfo['newStock'];
+                $record['totalCost'] = $stockInfo['totalCost'] ?? 0;
+                $record['totalStock'] = $stockInfo['totalStock'] ?? 0;
+                $record['avCost'] = $stockInfo['avCost'] ?? 0;
+                $record['changeCost'] = $stockInfo['changeCost'] ?? 0;
+                $record['unitCost'] = $stockInfo['unitCost'] ?? 0;
                 StockRecordClass::addPurchaseOrderRecord($record);
 
                 //减少路上库存
@@ -780,7 +786,7 @@ class PurchaseOrderClass extends BaseClass
         return $cg;
     }
 
-    //添加采购单 lqh 2021.1.19
+    //添加采购单 lqh 2021.1.20
     public static function addOrder($data)
     {
         $sjId = $data['sjId'] ?? 0;

+ 21 - 1
biz-ghs/order/classes/StockInOrderClass.php

@@ -27,6 +27,7 @@ use bizGhs\base\classes\BaseClass;
 class StockInOrderClass extends BaseClass
 {
     use OrderTrait;
+
     const STATUS_WAIT = 1; //待入库
     const STATUS_COMPLETE = 2; //已入库
     const STATUS_CANCEL = 3; //取消入库
@@ -83,6 +84,18 @@ class StockInOrderClass extends BaseClass
         $ghsItemInfo = $outOrderData['itemInfo'];
         $shopId = $outOrderData['inShopId'] ?? 0;
         $mainId = $outOrderData['inMainId'] ?? 0;
+
+        $adminId = $outOrderData['adminId'] ?? 0;
+        $staffId = 0;
+        $staffName = $outOrderData['staffName'] ?? '';
+        if (!empty($adminId)) {
+            $staff = ShopAdminClass::getByCondition(['mainId' => $mainId, 'adminId' => $adminId], true);
+            $staffId = $staff->id ?? 0;
+            $staffName = $staff->name ?? '';
+        }
+        $data['staffId'] = $staffId;
+        $data['staffName'] = $staffName;
+
         //入库: 需要将itemId 转换成对应的productId,若没有对应的productId,则需要新增
         ProductClass::complementProduct($data['sjId'], $mainId, $shopId, $ghsItemInfo);
         $itemIds = array_column($ghsItemInfo, "itemId");
@@ -177,6 +190,7 @@ class StockInOrderClass extends BaseClass
         if (empty($itemInfo)) {
             util::fail("订单异常");
         }
+
         $totalCost = $orderData['price'] ?? 0;
         foreach ($itemInfo as $k => $v) {
             $currentItemId = $v['productId'] ?? 0;
@@ -238,16 +252,22 @@ class StockInOrderClass extends BaseClass
                 }
             }
 
-            $stockInfo = ProductClass::addStockByItemNum($currentItemId, $v['itemNum']);
+            $stockInfo = ProductClass::addStockByItemNum($currentItemId, $v['itemNum'], ['cost' => $currentCost]);
             $itemInfo[$k]['oldStock'] = $stockInfo['oldStock'];
             $itemInfo[$k]['itemStock'] = $stockInfo['oldStock'];
             $itemInfo[$k]['newStock'] = $stockInfo['newStock'];
+            $itemInfo[$k]['avCost'] = $stockInfo['avCost'] ?? 0;
+            $itemInfo[$k]['totalCost'] = $stockInfo['totalCost'] ?? 0;
+            $itemInfo[$k]['totalStock'] = $stockInfo['totalStock'] ?? 0;
+            $itemInfo[$k]['changeCost'] = $stockInfo['changeCost'] ?? 0;
+            $itemInfo[$k]['unitCost'] = $stockInfo['unitCost'] ?? 0;
         }
 
         //流水记录
         $orderData['shopId'] = $shopId;
         $orderData['itemInfo'] = $itemInfo;
         $orderData['ptStyle'] = dict::getDict('ptStyle', 'ghs');
+        $orderData['relateName'] = $orderData['staffName'] ?? '';
         StockRecordClass::addRecordByOrder($orderData, StockRecordClass::STOCK_RECORD_TYPE_IN_STOCK);
         $outOrderSn = $orderData['outOrderSn'];
         $outShopId = $orderData['outShopId'];

+ 38 - 4
biz-ghs/order/classes/StockOutOrderClass.php

@@ -50,13 +50,27 @@ class StockOutOrderClass extends BaseClass
 
         $outShopId = $data['outShopId'] ?? 0;
         $outMainId = $data['outMainId'] ?? 0;
+        $staffName = $data['staffName'] ?? '';
+
+        $ids = array_column($ghsItemInfo, 'productId');
+        $productList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, 'id,avCost', 'id');
 
         $order = self::add($data, true);
+        $map = [];
         //先减库存,若取消出库则加回来
         foreach ($ghsItemInfo as $k => $v) {
-            $stockInfo = ProductClass::decreaseStock($v['productId'], $v['bigNum'], $v['smallNum'], true, false);
+            $productId = $v['productId'] ?? 0;
+            $product = $productList[$productId] ?? [];
+            $avCost = $product['avCost'] ?? 0;
+            $stockInfo = ProductClass::decreaseStock($v['productId'], $v['bigNum'], $v['smallNum'], true, false, ['cost' => $avCost]);
             $ghsItemInfo[$k]['oldStock'] = $stockInfo['oldStock'];
             $ghsItemInfo[$k]['newStock'] = $stockInfo['newStock'];
+            $newAvCost = $stockInfo['avCost'] ?? 0;
+            $totalCost = $stockInfo['totalCost'] ?? 0;
+            $totalStock = $stockInfo['totalStock'] ?? 0;
+            $changeCost = $stockInfo['changeCost'] ?? 0;
+            $unitCost = $stockInfo['unitCost'] ?? 0;
+            $map[$productId] = ['avCost' => $newAvCost, 'totalCost' => $totalCost, 'totalStock' => $totalStock, 'changeCost' => $changeCost,'unitCost'=>$unitCost];
         }
 
         //写入详情表
@@ -78,10 +92,30 @@ class StockOutOrderClass extends BaseClass
         $orderData = self::getOrderDetail($orderSn, $data['outShopId']);
         $orderData['shopId'] = $outShopId;
         $orderData['mainId'] = $outMainId;
+
+        $orderData['staffName'] = $staffName;
+        $orderData['relateName'] = $staffName;
+
         $itemInfo = $orderData['itemInfo'] ?? [];
         if (empty($itemInfo)) {
             util::fail("订单异常");
         }
+        //把平均成本相关参数加进入,存到库存变动明细里去 ssh
+        foreach ($itemInfo as $cKey => $cVal) {
+            $productId = $cVal['productId'] ?? 0;
+            $mapItem = $map[$productId] ?? [];
+            $avCost = $mapItem['avCost'] ?? 0;
+            $totalCost = $mapItem['totalCost'] ?? 0;
+            $totalStock = $mapItem['totalStock'] ?? 0;
+            $changeCost = $mapItem['changeCost'] ?? 0;
+            $unitCost = $mapItem['unitCost']??0;
+            $itemInfo[$cKey]['avCost'] = $avCost;
+            $itemInfo[$cKey]['totalCost'] = $totalCost;
+            $itemInfo[$cKey]['totalStock'] = $totalStock;
+            $itemInfo[$cKey]['changeCost'] = $changeCost;
+            $itemInfo[$cKey]['unitCost'] = $unitCost;
+        }
+        $orderData['itemInfo'] = $itemInfo;
         $orderData['ptStyle'] = dict::getDict('ptStyle', 'ghs');
         StockRecordClass::addRecordByOrder($orderData, StockRecordClass::STOCK_RECORD_TYPE_OUT_STOCK);
         if ($action === self::ACTION_CONFIRM) {
@@ -132,7 +166,7 @@ class StockOutOrderClass extends BaseClass
             util::fail("订单异常");
         }
         $orderData['shopId'] = $shopId;
-        //写入一条数据到入库店的入库订单表,生成入库订单号
+        //写入一条数据到入库店的入库订单表,生成入库订单号
         $inOrderData = StockInOrderClass::addOrder($orderData);
         $inOrderSn = $inOrderData['orderSn'];
         //更新入库订单号
@@ -255,11 +289,11 @@ class StockOutOrderClass extends BaseClass
             $tmp['itemStock'] = $v['oldStock'];//当时库存
             $tmp['newStock'] = $v['newStock'];//当时库存
             $tmp['itemNum'] = ProductClass::mergeItemNum($v['bigNum'], $v['smallNum'], $unitNum);// 数量
-            $tmp['itemPrice'] = $productData[$productId]['cost'] ?? 0; //当时成本价
+            $tmp['itemPrice'] = $productData[$productId]['avCost'] ?? 0; //平均成本
             $tmp['price'] = $productData[$productId]['price'] ?? 0; //当时批发价
             $tmp['skPrice'] = $productData[$productId]['skPrice'] ?? 0; //当时零售价
             $tmp['hjPrice'] = $productData[$productId]['hjPrice'] ?? 0; //当时会员价
-            $tmp['cost'] = $productData[$productId]['cost'] ?? 0; //当时成本价
+            $tmp['cost'] = $productData[$productId]['avCost'] ?? 0; //平均成本
             $stockFormat = ProductClass::formatStock($tmp['itemStock'], $unitNum); // 库存总单位数量转大小单位的数量
             $itemInfo = [
                 'itemName' => $productData[$productId]['name'] ?? '',//花材名称

+ 45 - 5
biz-ghs/product/classes/ProductClass.php

@@ -894,7 +894,7 @@ class ProductClass extends BaseClass
     }
 
     //加库存
-    public static function addStockByItemNum($productId, $itemNum)
+    public static function addStockByItemNum($productId, $itemNum, $params = [])
     {
         $key = self::LOCK_STOCK . '_' . $productId;
         $lock = util::lock($key);
@@ -906,6 +906,26 @@ class ProductClass extends BaseClass
             util::unlock($key);
             util::fail("花材不存在");
         }
+
+        $lastCost = 0;
+        $lastStock = 0;
+        $avCost = 0;
+        $changeCost = 0;
+        $unitCost = 0;
+        if (!empty($params['cost']) && $params['cost'] > 0) {
+            $unitCost = $params['cost'];
+            $changeCost = bcmul($params['cost'], $itemNum, 2);
+            $totalCost = $productData->totalCost ?? 0;
+            $lastCost = bcadd($changeCost, $totalCost, 2);
+            $totalStock = $productData->totalStock ?? 0;
+            $lastStock = bcadd($itemNum, $totalStock, 2);
+            $avCost = bcdiv($lastCost, $lastStock, 2);
+            $productData->avCost = $avCost;
+            $productData->totalCost = $lastCost;
+            $productData->totalStock = $lastStock;
+            $productData->save();
+        }
+
         $newStock = bcadd($productData->stock, $itemNum, 2);
         self::updateStockById($productId, $newStock);
         //上架
@@ -918,13 +938,14 @@ class ProductClass extends BaseClass
             $productData->save();
         }
         util::unlock($key);
-        return ['oldStock' => $productData['stock'], 'newStock' => $newStock, 'itemNum' => $itemNum];
+        return ['oldStock' => $productData['stock'], 'newStock' => $newStock, 'itemNum' => $itemNum,
+            'avCost' => $avCost, 'totalCost' => $lastCost, 'totalStock' => $lastStock, 'changeCost' => $changeCost, 'unitCost' => $unitCost];
     }
 
     //减库存,允许负库存,负数数量(盘点时)
     //checkStock boolean 是否需要判断库存
     //addSold boolean 是否增加销量,报损和拆散不要增加销量
-    public static function decreaseStock($productId, $itemNumBundle, $itemNumPiece, $checkStock = false, $addSold = true)
+    public static function decreaseStock($productId, $itemNumBundle, $itemNumPiece, $checkStock = false, $addSold = true, $params = [])
     {
 
         //小单位库存不再考虑 ssh 20230303
@@ -944,6 +965,24 @@ class ProductClass extends BaseClass
         $ratio = $productData->ratio;
         //转成小数,更新库存
         $itemNum = self::mergeItemNum($itemNumBundle, $itemNumPiece, $ratio);
+
+        $avCost = 0;
+        $lastStock = 0;
+        $lastCost = 0;
+        $changeCost = 0;
+        $unitCost = 0;
+        if (!empty($params['cost']) && $params['cost'] > 0) {
+            $unitCost = $params['cost'];
+            $changeCost = bcmul($params['cost'], $itemNum, 2);
+            $lastCost = bcsub($productData->totalCost, $changeCost, 2);
+            $lastStock = bcsub($productData->totalStock, $itemNum, 2);
+            $avCost = bcdiv($lastCost, $lastStock, 2);
+            $productData->avCost = $avCost;
+            $productData->totalCost = $lastCost;
+            $productData->totalStock = $lastStock;
+            $productData->save();
+        }
+
         $newStock = bcsub($productData->stock, $itemNum, 2);
         if ($checkStock && $newStock < 0) {
             //需要判断库存是否充足
@@ -967,7 +1006,8 @@ class ProductClass extends BaseClass
         }
 
         util::unlock($key);
-        return ['oldStock' => $productData['stock'], 'newStock' => $newStock, 'itemNum' => $itemNum];
+        return ['oldStock' => $productData['stock'], 'newStock' => $newStock, 'itemNum' => $itemNum,
+            'avCost' => $avCost, 'totalCost' => $lastCost, 'totalStock' => $lastStock, 'changeCost' => $changeCost, 'unitCost' => $unitCost];
     }
 
 
@@ -1307,7 +1347,7 @@ class ProductClass extends BaseClass
             $currentGhsProduct = $ghsProductData[$ghsProductId] ?? [];
             $itemId = $currentGhsProduct['itemId'] ?? 0;
             $ratio = $currentGhsProduct['ratio'] ?? 0;
-            $cost = $currentGhsProduct['cost'] ?? 0;
+            $cost = $currentGhsProduct['avCost'] ?? 0;
             $classId = $currentGhsProduct['classId'] ?? 0;
 
             $bigNum = $v['bigNum'] ?? 0;

+ 8 - 2
biz-ghs/stock/classes/StockRecordClass.php

@@ -12,6 +12,7 @@ use common\services\xhItemService;
 class StockRecordClass extends BaseClass
 {
     use OrderTrait;
+
     public static $baseFile = '\bizGhs\stock\models\StockRecord';
 
     const STOCK_RECORD_TYPE_PURCHASE = 1; //采购
@@ -90,8 +91,8 @@ class StockRecordClass extends BaseClass
     {
         $list = self::$typeMap;
         $arr = [];
-        foreach($list as $id => $val){
-            $arr[] = ['value'=>$id,'label'=>$val];
+        foreach ($list as $id => $val) {
+            $arr[] = ['value' => $id, 'label' => $val];
         }
         return $arr;
     }
@@ -146,6 +147,11 @@ class StockRecordClass extends BaseClass
             $record['newStock'] = $v['newStock'];// 最新库存
             $record['relateName'] = $orderData['relateName'] ?? '';
             $record['ptStyle'] = $orderData['ptStyle'] ?? dict::getDict('ptStyle', 'ghs');
+            $record['avCost'] = $v['avCost'] ?? 0;
+            $record['totalCost'] = $v['totalCost'] ?? 0;
+            $record['totalStock'] = $v['totalStock'] ?? 0;
+            $record['changeCost'] = $v['changeCost'] ?? 0;
+            $record['unitCost'] = $v['unitCost'] ?? 0;
             self::addRecord($record);
         }
     }