|
|
@@ -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;
|