Browse Source

采购入库改价与成本管理

shish 9 tháng trước cách đây
mục cha
commit
65ce553c16
1 tập tin đã thay đổi với 23 bổ sung5 xóa
  1. 23 5
      app-ghs/controllers/PurchaseOrderItemController.php

+ 23 - 5
app-ghs/controllers/PurchaseOrderItemController.php

@@ -81,9 +81,27 @@ class PurchaseOrderItemController extends BaseController
             $itemList = ProductClass::getByIds($productIdList, null, 'id');
             foreach ($list as $key => $info) {
                 $productId = $info['productId'] ?? 0;
-                $cost = $info['cost'] ?? 0;
-                $cost = round($cost);
                 $current = $itemList[$productId] ?? [];
+                //待入库的,需要计算新的平均成本,用于改价参考
+                if ($cg->status == 3) {
+                    $oldStock = $current['stock'] ?? 0;
+                    $oldAvCost = $current['avCost'] ?? 0;
+                    $oldCost = bcmul($oldAvCost, $oldStock, 2);
+
+                    $itemNum = $info['itemNum'] ?? 0;
+                    $addUnitCost = $info['cost']??0;
+                    $addCost = bcmul($itemNum, $addUnitCost, 2);
+
+                    $newCost = bcadd($oldCost, $addCost, 2);
+                    $newStock = bcadd($oldStock, $itemNum, 2);
+                    $newAvCost = bcdiv($newCost, $newStock, 2);
+                }else{
+                    //已经入库存了,再次进入改价时使用
+                    $newAvCost = $current['avCost'] ?? 0;
+                }
+                //前端改价格,显示的要新的平均成本
+                $list[$key]['cost'] = $newAvCost;
+
                 $addPrice = $current['addPrice'] ?? 0;
                 $hjAddPrice = $current['hjAddPrice'] ?? 0;
                 $skMore = $current['skMore'] ?? 0;
@@ -93,9 +111,9 @@ class PurchaseOrderItemController extends BaseController
                     $suggestSkPrice = $info['saveSkPrice'] ?? 0;
                     $suggestHjPrice = $info['saveHjPrice'] ?? 0;
                 } else {
-                    $suggestPrice = bcadd($cost, $addPrice, 2);
-                    $suggestSkPrice = bcadd($cost, $skMore, 2);
-                    $suggestHjPrice = bcadd($cost, $hjAddPrice, 2);
+                    $suggestPrice = bcadd(round($newAvCost), $addPrice, 2);
+                    $suggestSkPrice = bcadd(round($newAvCost), $skMore, 2);
+                    $suggestHjPrice = bcadd(round($newAvCost), $hjAddPrice, 2);
                 }
                 $suggestPrice = floatval($suggestPrice);
                 $suggestSkPrice = floatval($suggestSkPrice);