find()->toArray(); if (empty($params['product_arr'])) { throw new BusinessException('请选择商品'); } $productIds = array_column($params['product_arr'], 'id'); if ($find['one_type'] == 1) { $outProducts = StoreBranchProduct::whereIn('product_id', $productIds)->where('store_id', $find['one_id'])->field('id,product_id,stock')->select()->toArray(); } else { $outProducts = WarehouseProductStorege::whereIn('product_id', $productIds)->where('warehouse_id', $find['one_id'])->field('id,product_id,nums stock')->select()->toArray(); } $outProducts = reset_index($outProducts, 'product_id'); if ($find['two_type'] == 1) { $inProducts = StoreBranchProduct::whereIn('product_id', $productIds)->where('store_id', $find['two_id'])->field('id,product_id,stock')->select()->toArray(); } else { $inProducts = WarehouseProductStorege::whereIn('product_id', $productIds)->where('warehouse_id', $find['two_id'])->field('id,product_id,nums stock')->select()->toArray(); } $inProducts = reset_index($inProducts, 'product_id'); $insert = []; Db::startTrans(); try { $insert = []; foreach ($params['product_arr'] as $v) { $outProduct = !empty($outProducts[$v['id']]) ? $outProducts[$v['id']] : ['stock' => 0, 'id' => 0, 'product_id' => $v['id']]; $inProduct = !empty($inProducts[$v['id']]) ? $inProducts[$v['id']] : ['stock' => 0, 'id' => 0, 'product_id' => $v['id']]; if ($outProduct['stock'] < $v['nums']) { throw new BusinessException("出库商品库存不足 {$outProduct['product_id']} 调拨数量不能大于当前仓库库存"); continue; } $insert[] = [ 'oid' => $find['id'], 'product_id' => $v['id'], 'nums' => $v['nums'], 'remark' => $v['remark'], 'one_before_nums' => $outProduct['stock'], 'one_after_nums' => bcsub($outProduct['stock'], $v['nums']), 'two_before_nums' => $inProduct['stock'], 'two_after_nums' => bcadd($inProduct['stock'], $v['nums']), 'one_type' => $find['one_type'], 'two_type' => $find['two_type'], 'one_id' => $find['one_id'], 'two_id' => $find['two_id'], 'create_time' => time(), ]; } InventoryTransfer::insertAll($insert); if ($find['two_type'] == 1 && $find['status'] == 1) { InventoryTransferOrderLogic::audit($find, $insert, $admin_id); } Db::commit(); return true; } catch (\Throwable $e) { Db::rollback(); throw new BusinessException($e->getMessage()); } } /** * @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(); throw new BusinessException($e->getMessage()); } } /** * @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(); } }