|
|
@@ -907,28 +907,36 @@ class ProductClass extends BaseClass
|
|
|
util::fail("花材不存在");
|
|
|
}
|
|
|
|
|
|
- $lastCost = 0;
|
|
|
- $lastStock = 0;
|
|
|
+ $newItemCost = 0;
|
|
|
+ $newItemStock = 0;
|
|
|
$avCost = 0;
|
|
|
$changeCost = 0;
|
|
|
$unitCost = 0;
|
|
|
+ //涉及平均成本计算的,多处要修改,搜索关键词 calc_av_cost
|
|
|
if (!empty($params['cost']) && $params['cost'] > 0) {
|
|
|
+
|
|
|
+ //原库存和原成本,也叫剩余库存和剩余成本
|
|
|
+ $oldItemStock = $productData->stock ?? 0;
|
|
|
+ $oldAvCost = $productData->avCost ?? 0;
|
|
|
+ $oldItemCost = bcmul($oldItemStock, $oldAvCost, 2);
|
|
|
+
|
|
|
+ //变动库存和成本
|
|
|
$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);
|
|
|
- if($lastStock>0){
|
|
|
- $avCost = bcdiv($lastCost, $lastStock, 2);
|
|
|
- }else{
|
|
|
- $avCost = 0;
|
|
|
- $lastStock = 0;
|
|
|
- $lastCost = 0;
|
|
|
+
|
|
|
+ //新库存和新成本
|
|
|
+ $newItemCost = bcadd($changeCost, $oldItemCost, 2);
|
|
|
+ $newItemStock = bcadd($itemNum, $oldItemStock, 2);
|
|
|
+
|
|
|
+ if ($newItemStock > 0) {
|
|
|
+ $avCost = bcdiv($newItemCost, $newItemStock, 2);
|
|
|
+ } else {
|
|
|
+ $newItemStock = 0;
|
|
|
+ $newItemCost = 0;
|
|
|
}
|
|
|
$productData->avCost = $avCost;
|
|
|
- $productData->totalCost = $lastCost;
|
|
|
- $productData->totalStock = $lastStock;
|
|
|
+ $productData->totalCost = $newItemCost;
|
|
|
+ $productData->totalStock = $newItemStock;
|
|
|
$productData->save();
|
|
|
}
|
|
|
|
|
|
@@ -945,7 +953,7 @@ class ProductClass extends BaseClass
|
|
|
}
|
|
|
util::unlock($key);
|
|
|
return ['oldStock' => $productData['stock'], 'newStock' => $newStock, 'itemNum' => $itemNum,
|
|
|
- 'avCost' => $avCost, 'totalCost' => $lastCost, 'totalStock' => $lastStock, 'changeCost' => $changeCost, 'unitCost' => $unitCost];
|
|
|
+ 'avCost' => $avCost, 'totalCost' => $newItemCost, 'totalStock' => $newItemStock, 'changeCost' => $changeCost, 'unitCost' => $unitCost];
|
|
|
}
|
|
|
|
|
|
//减库存,允许负库存,负数数量(盘点时)
|
|
|
@@ -973,25 +981,33 @@ class ProductClass extends BaseClass
|
|
|
$itemNum = self::mergeItemNum($itemNumBundle, $itemNumPiece, $ratio);
|
|
|
|
|
|
$avCost = 0;
|
|
|
- $lastStock = 0;
|
|
|
- $lastCost = 0;
|
|
|
+ $newItemStock = 0;
|
|
|
+ $newItemCost = 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);
|
|
|
- if($lastStock>0){
|
|
|
- $avCost = bcdiv($lastCost, $lastStock, 2);
|
|
|
- }else{
|
|
|
- $avCost = 0;
|
|
|
- $lastStock = 0;
|
|
|
- $lastCost=0;
|
|
|
+
|
|
|
+ //原库存和原成本,也叫剩余库存和剩余成本
|
|
|
+ $oldItemStock = $productData->stock;
|
|
|
+ $oldAvCost = $productData->avCost ?? 0;
|
|
|
+ $oldItemCost = bcmul($oldItemStock, $oldAvCost, 2);
|
|
|
+
|
|
|
+ //新库存和新成本
|
|
|
+ $newItemCost = bcsub($oldItemCost, $changeCost, 2);
|
|
|
+ $newItemStock = bcsub($oldItemStock, $itemNum, 2);
|
|
|
+
|
|
|
+ if ($newItemStock > 0) {
|
|
|
+ $avCost = bcdiv($newItemCost, $newItemStock, 2);
|
|
|
+ } else {
|
|
|
+ $newItemStock = 0;
|
|
|
+ $newItemCost = 0;
|
|
|
}
|
|
|
$productData->avCost = $avCost;
|
|
|
- $productData->totalCost = $lastCost;
|
|
|
- $productData->totalStock = $lastStock;
|
|
|
+ $productData->totalCost = $newItemCost;
|
|
|
+ $productData->totalStock = $newItemStock;
|
|
|
$productData->save();
|
|
|
}
|
|
|
|
|
|
@@ -1019,7 +1035,7 @@ class ProductClass extends BaseClass
|
|
|
|
|
|
util::unlock($key);
|
|
|
return ['oldStock' => $productData['stock'], 'newStock' => $newStock, 'itemNum' => $itemNum,
|
|
|
- 'avCost' => $avCost, 'totalCost' => $lastCost, 'totalStock' => $lastStock, 'changeCost' => $changeCost, 'unitCost' => $unitCost];
|
|
|
+ 'avCost' => $avCost, 'totalCost' => $newItemCost, 'totalStock' => $newItemStock, 'changeCost' => $changeCost, 'unitCost' => $unitCost];
|
|
|
}
|
|
|
|
|
|
|