代领多人物品

This commit is contained in:
2026-02-24 14:19:17 +08:00
parent 976945378e
commit cf7d8a9f5b
2 changed files with 78 additions and 48 deletions
+74 -45
View File
@@ -402,7 +402,7 @@ class InvRequisitionService
}
/**
* 管理员代领
* 管理员代领(支持多人)
*
* @param Request $request
* @param array $data
@@ -414,7 +414,7 @@ class InvRequisitionService
return Db::transaction(function () use ($request, $data) {
$itemId = (int)($data['item_id'] ?? 0);
$qty = (int)($data['qty'] ?? 1);
$targetUserId = (int)($data['target_user_id'] ?? 0);
$targetUserIds = $data['target_user_id'];
if ($itemId <= 0) {
throw new BusinessException('物品ID无效');
@@ -422,7 +422,7 @@ class InvRequisitionService
if ($qty <= 0) {
throw new BusinessException('领取数量必须大于0');
}
if ($targetUserId <= 0) {
if (empty($targetUserIds)) {
throw new BusinessException('请选择代领用户');
}
@@ -432,6 +432,8 @@ class InvRequisitionService
throw new BusinessException('物品不存在');
}
$targetUserIds = explode(',', $targetUserIds);
// 获取操作人(管理员)信息
$proxyUser = (new UserService())->getUserInfo($request->user->uid, '');
$proxyUserId = (int)$proxyUser['user']['id'];
@@ -442,59 +444,79 @@ class InvRequisitionService
throw new BusinessException('您不是该物品的管理员,无权代领');
}
// 获取目标用户信息
$targetUser = (new UserService())->getUserInfo($targetUserId, '');
if (!$targetUser['user']) {
throw new BusinessException('目标用户不存在');
}
// 计算总需求量
$totalQty = $qty * count($targetUserIds);
// 检查库存
if ($item->stock_qty < $qty) {
throw new BusinessException('库存不足,当前库存: ' . $item->stock_qty);
if ($item->stock_qty < $totalQty) {
throw new BusinessException('库存不足,当前库存: ' . $item->stock_qty . ',需要: ' . $totalQty);
}
// 创建领用申请记录(直接完成)
$requisition = InvRequisition::create([
'order_no' => $this->generateOrderNo(),
'applicant_company_id' => $targetUser['user']['company_id'] ?? 0,
'dept_id' => $targetUser['user']['dept_id'] ?? 0,
'applicant_id' => $targetUserId,
'applicant_name' => $targetUser['user']['name'],
'leader_id' => $targetUser['user']['report_to'] ?? 0,
'manager_id' => $item->manage_user_id,
'borrow_reason' => $data['borrow_reason'] ?? '管理员代领',
'use_start_date' => date('Y-m-d', time()),
'use_end_date' => date('Y-m-d', time()),
'status' => 19, // 已审批(无需归还)
'claim_type' => InvRequisition::CLAIM_TYPE_PROXY,
'proxy_claim_user_id' => $proxyUserId,
'proxy_claim_user_name' => $proxyUserName,
'remark' => $data['remark'] ?? '管理员代领',
]);
$createdRequisitions = [];
$remark = $data['remark'] ?? '管理员代领';
// 创建领用明细
InvRequisitionItem::create([
'requisition_id' => $requisition->id,
'item_id' => $itemId,
'item_name_snap' => $item->item_name,
'spec_model_snap' => $item->spec_model,
'unit_snap' => $item->unit,
'is_consumable_snap' => $item->is_consumable,
'request_qty' => $qty,
'approved_qty' => $qty,
'remark' => $data['remark'] ?? '管理员代领',
]);
// 为每个目标用户创建领用记录
foreach ($targetUserIds as $targetUserId) {
$targetUserId = (int)$targetUserId;
if ($targetUserId <= 0) {
continue;
}
// 扣减库存
(new InvItemService())->stockOut($itemId, $qty, false, [
// 获取目标用户信息
$targetUser = (new UserService())->getUserInfo($targetUserId, '');
if (!$targetUser['user']) {
throw new BusinessException('用户ID ' . $targetUserId . ' 不存在');
}
// 创建领用申请记录(直接完成)
$requisition = InvRequisition::create([
'order_no' => $this->generateOrderNo(),
'applicant_company_id' => $targetUser['user']['company_id'] ?? 0,
'dept_id' => $targetUser['user']['dept_id'] ?? 0,
'applicant_id' => $targetUserId,
'applicant_name' => $targetUser['user']['name'],
'leader_id' => $targetUser['user']['report_to'] ?? 0,
'manager_id' => $item->manage_user_id,
'borrow_reason' => $data['borrow_reason'] ?? '管理员代领',
'use_start_date' => date('Y-m-d', time()),
'use_end_date' => date('Y-m-d', time()),
'status' => 19, // 已审批(无需归还)
'claim_type' => InvRequisition::CLAIM_TYPE_PROXY,
'proxy_claim_user_id' => $proxyUserId,
'proxy_claim_user_name' => $proxyUserName,
'remark' => $remark,
]);
// 创建领用明细
InvRequisitionItem::create([
'requisition_id' => $requisition->id,
'item_id' => $itemId,
'item_name_snap' => $item->item_name,
'spec_model_snap' => $item->spec_model,
'unit_snap' => $item->unit,
'is_consumable_snap' => $item->is_consumable,
'request_qty' => $qty,
'approved_qty' => $qty,
'remark' => $remark,
]);
$createdRequisitions[] = $requisition->toArray();
}
// 一次性扣减总库存
(new InvItemService())->stockOut($itemId, $totalQty, false, [
'biz_type' => InvStockLog::BIZ_TYPE_PROXY_CLAIM,
'biz_no' => $requisition->order_no,
'biz_no' => 'PROXY_' . time(),
'operator_id' => $proxyUserId,
'operator_name' => $proxyUserName,
'remark' => '管理员代领',
'remark' => '管理员代领(' . count($targetUserIds) . '人×' . $qty . ')',
]);
return $requisition->toArray();
return [
'created_count' => count($createdRequisitions),
'requisitions' => $createdRequisitions,
'total_qty' => $totalQty,
];
});
}
@@ -531,6 +553,13 @@ class InvRequisitionService
$query->where('proxy_claim_user_id', $request->get('proxy_user_id'));
}
// 按物品ID筛选
if (!empty($request->get('item_id'))) {
$query->whereHas('items', function ($q) use ($request) {
$q->where('item_id', $request->get('item_id'));
});
}
// 时间范围筛选
if (!empty($request->get('start_time'))) {
$query->where('qr_claim_time', '>=', strtotime($request->get('start_time')));
@@ -19,7 +19,9 @@ class InvRequisitionValidate extends Validate
'item_id' => 'require|integer|gt:0',
'qty' => 'require|integer|gt:0',
// 管理员代领规则
'target_user_id' => 'require|integer|gt:0',
'target_user_id' => 'integer|gt:0',
'target_user_ids' => 'array',
'target_user_ids.*' => 'integer|gt:0',
];
// 提示
@@ -44,8 +46,6 @@ class InvRequisitionValidate extends Validate
'qty.integer' => '领取数量必须为整数',
'qty.gt' => '领取数量必须大于0',
'target_user_id.require' => '代领用户不能为空',
'target_user_id.integer' => '代领用户ID必须为整数',
'target_user_id.gt' => '代领用户ID必须大于0',
];
// 场景
@@ -83,6 +83,7 @@ class InvRequisitionValidate extends Validate
'item_id',
'qty',
'target_user_id',
'target_user_ids',
'borrow_reason',
],
];