where('store_id', $params['one_id'])->value('stock'); $stock_two = StoreBranchProduct::where('product_id', $params['product_id'])->where('store_id', $params['two_id'])->value('stock'); if ($stock < $params['nums']) { self::setError('调拨数量不能大于当前门店库存'); return false; } else { $one_before_nums = $stock; $one_after_nums = bcsub($stock, $params['nums']); $two_before_nums = $stock_two; $two_after_nums = bcadd($stock_two, $params['nums']); } } elseif ($params['type'] == 2) { $stock = WarehouseProductStorege::where('product_id', $params['product_id'])->where('warehouse_id', $params['one_id'])->value('nums'); $stock_two = WarehouseProductStorege::where('product_id', $params['product_id'])->where('warehouse_id', $params['two_id'])->value('nums'); if ($stock < $params['nums']) { self::setError('调拨数量不能大于当前仓库库存'); return false; } else { $one_before_nums = $stock; $one_after_nums = bcsub($stock, $params['nums']); $two_before_nums = $stock_two; $two_after_nums = bcadd($stock_two, $params['nums']); } } Db::startTrans(); try { InventoryTransfer::create([ 'product_id' => $params['product_id'], 'nums' => $params['nums'], 'one_before_nums' => $one_before_nums, 'one_after_nums' => $one_after_nums, 'two_before_nums' => $two_before_nums, 'two_after_nums' => $two_after_nums, 'type' => $params['type'], 'one_id' => $params['one_id'], 'two_id' => $params['two_id'] ]); if ($params['type'] == 1) { StoreBranchProduct::where('product_id', $params['product_id'])->where('store_id', $params['one_id'])->dec('stock', $params['nums'])->update(); StoreBranchProduct::where('product_id', $params['product_id'])->where('store_id', $params['two_id'])->inc('stock', $params['nums'])->update(); } elseif ($params['type'] == 2) { WarehouseProductStorege::where('product_id', $params['product_id'])->where('warehouse_id', $params['one_id'])->dec('nums', $params['nums'])->update(); WarehouseProductStorege::where('product_id', $params['product_id'])->where('warehouse_id', $params['two_id'])->inc('nums', $params['nums'])->update(); } Db::commit(); return true; } catch (\Exception $e) { Db::rollback(); self::setError($e->getMessage()); return false; } } /** * @notes 编辑商品调拨 * @param array $params * @return bool * @author admin * @date 2024/08/13 16:18 */ public static function edit(array $params): bool { Db::startTrans(); try { InventoryTransfer::where('id', $params['id'])->update([ 'product_id' => $params['product_id'], 'nums' => $params['nums'], 'before_nums' => $params['before_nums'], 'after_nums' => $params['after_nums'], 'type' => $params['type'], 'one_id' => $params['one_id'], 'two_id' => $params['two_id'] ]); Db::commit(); return true; } catch (\Exception $e) { Db::rollback(); self::setError($e->getMessage()); return false; } } /** * @notes 删除商品调拨 * @param array $params * @return bool * @author admin * @date 2024/08/13 16:18 */ public static function delete(array $params): bool { return InventoryTransfer::destroy($params['id']); } /** * @notes 获取商品调拨详情 * @param $params * @return array * @author admin * @date 2024/08/13 16:18 */ public static function detail($params): array { return InventoryTransfer::findOrEmpty($params['id'])->toArray(); } }