修复了库存管理逻辑中的错误,优化了库存不足时的异常处理。

This commit is contained in:
mkm 2024-08-13 19:56:27 +08:00
parent 6e17d78d52
commit 7eaf64af41

View File

@ -28,22 +28,22 @@ class InventoryTransferLogic extends BaseLogic
*/ */
public static function add(array $params): bool public static function add(array $params): bool
{ {
$one_before_nums=0; $one_before_nums = 0;
$one_after_nums=0; $one_after_nums = 0;
$two_before_nums=0; $two_before_nums = 0;
$two_after_nums=0; $two_after_nums = 0;
if ($params['type'] == 1) { if ($params['type'] == 1) {
$stock = StoreBranchProduct::where('product_id', $params['one_id'])->value('stock'); $stock = StoreBranchProduct::where('product_id', $params['one_id'])->value('stock');
$stock_two = StoreBranchProduct::where('product_id', $params['two_id'])->value('stock'); $stock_two = StoreBranchProduct::where('product_id', $params['two_id'])->value('stock');
if ($stock < $params['nums']) { if ($stock < $params['nums']) {
self::setError('调拨数量不能大于当前库存'); self::setError('调拨数量不能大于当前库存');
return false; return false;
}else{ } else {
$one_before_nums=$stock; $one_before_nums = $stock;
$one_after_nums=bcsub($stock,$params['nums']); $one_after_nums = bcsub($stock, $params['nums']);
$two_before_nums=$stock_two; $two_before_nums = $stock_two;
$two_after_nums=bcadd($stock_two,$params['nums']); $two_after_nums = bcadd($stock_two, $params['nums']);
} }
} elseif ($params['type'] == 2) { } elseif ($params['type'] == 2) {
$stock = WarehouseProductStorege::where('product_id', $params['product_id'])->where('warehouse_id', $params['one_id'])->value('nums'); $stock = WarehouseProductStorege::where('product_id', $params['product_id'])->where('warehouse_id', $params['one_id'])->value('nums');
@ -51,39 +51,37 @@ class InventoryTransferLogic extends BaseLogic
if ($stock < $params['nums']) { if ($stock < $params['nums']) {
self::setError('调拨数量不能大于当前库存'); self::setError('调拨数量不能大于当前库存');
return false; return false;
}else{ } else {
$one_before_nums=$stock; $one_before_nums = $stock;
$one_after_nums=bcsub($stock,$params['nums']); $one_after_nums = bcsub($stock, $params['nums']);
$two_before_nums=$stock_two; $two_before_nums = $stock_two;
$two_after_nums=bcadd($stock_two,$params['nums']); $two_after_nums = bcadd($stock_two, $params['nums']);
} }
} }
Db::startTrans(); Db::startTrans();
try { try {
InventoryTransfer::create([ InventoryTransfer::create([
'product_id' => $params['product_id'], 'product_id' => $params['product_id'],
'nums' => $params['nums'], 'nums' => $params['nums'],
'one_before_nums' => $one_before_nums, 'one_before_nums' => $one_before_nums,
'one_after_nums' => $one_after_nums, 'one_after_nums' => $one_after_nums,
'two_before_nums' => $two_before_nums, 'two_before_nums' => $two_before_nums,
'two_after_nums' => $two_after_nums, 'two_after_nums' => $two_after_nums,
'type' => $params['type'], 'type' => $params['type'],
'one_id' => $params['one_id'], 'one_id' => $params['one_id'],
'two_id' => $params['two_id'] 'two_id' => $params['two_id']
]); ]);
Db::commit(); Db::commit();
return true; return true;
} catch (\Exception $e) { } catch (\Exception $e) {
Db::rollback(); Db::rollback();
self::setError($e->getMessage()); self::setError($e->getMessage());
return false; return false;
}
} }
} }
/** /**
* @notes 编辑商品调拨 * @notes 编辑商品调拨
* @param array $params * @param array $params