代领多人物品
This commit is contained in:
@@ -402,7 +402,7 @@ class InvRequisitionService
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 管理员代领
|
* 管理员代领(支持多人)
|
||||||
*
|
*
|
||||||
* @param Request $request
|
* @param Request $request
|
||||||
* @param array $data
|
* @param array $data
|
||||||
@@ -414,7 +414,7 @@ class InvRequisitionService
|
|||||||
return Db::transaction(function () use ($request, $data) {
|
return Db::transaction(function () use ($request, $data) {
|
||||||
$itemId = (int)($data['item_id'] ?? 0);
|
$itemId = (int)($data['item_id'] ?? 0);
|
||||||
$qty = (int)($data['qty'] ?? 1);
|
$qty = (int)($data['qty'] ?? 1);
|
||||||
$targetUserId = (int)($data['target_user_id'] ?? 0);
|
$targetUserIds = $data['target_user_id'];
|
||||||
|
|
||||||
if ($itemId <= 0) {
|
if ($itemId <= 0) {
|
||||||
throw new BusinessException('物品ID无效');
|
throw new BusinessException('物品ID无效');
|
||||||
@@ -422,7 +422,7 @@ class InvRequisitionService
|
|||||||
if ($qty <= 0) {
|
if ($qty <= 0) {
|
||||||
throw new BusinessException('领取数量必须大于0');
|
throw new BusinessException('领取数量必须大于0');
|
||||||
}
|
}
|
||||||
if ($targetUserId <= 0) {
|
if (empty($targetUserIds)) {
|
||||||
throw new BusinessException('请选择代领用户');
|
throw new BusinessException('请选择代领用户');
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -432,6 +432,8 @@ class InvRequisitionService
|
|||||||
throw new BusinessException('物品不存在');
|
throw new BusinessException('物品不存在');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$targetUserIds = explode(',', $targetUserIds);
|
||||||
|
|
||||||
// 获取操作人(管理员)信息
|
// 获取操作人(管理员)信息
|
||||||
$proxyUser = (new UserService())->getUserInfo($request->user->uid, '');
|
$proxyUser = (new UserService())->getUserInfo($request->user->uid, '');
|
||||||
$proxyUserId = (int)$proxyUser['user']['id'];
|
$proxyUserId = (int)$proxyUser['user']['id'];
|
||||||
@@ -442,15 +444,28 @@ class InvRequisitionService
|
|||||||
throw new BusinessException('您不是该物品的管理员,无权代领');
|
throw new BusinessException('您不是该物品的管理员,无权代领');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 计算总需求量
|
||||||
|
$totalQty = $qty * count($targetUserIds);
|
||||||
|
|
||||||
|
// 检查库存
|
||||||
|
if ($item->stock_qty < $totalQty) {
|
||||||
|
throw new BusinessException('库存不足,当前库存: ' . $item->stock_qty . ',需要: ' . $totalQty);
|
||||||
|
}
|
||||||
|
|
||||||
|
$createdRequisitions = [];
|
||||||
|
$remark = $data['remark'] ?? '管理员代领';
|
||||||
|
|
||||||
|
// 为每个目标用户创建领用记录
|
||||||
|
foreach ($targetUserIds as $targetUserId) {
|
||||||
|
$targetUserId = (int)$targetUserId;
|
||||||
|
if ($targetUserId <= 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// 获取目标用户信息
|
// 获取目标用户信息
|
||||||
$targetUser = (new UserService())->getUserInfo($targetUserId, '');
|
$targetUser = (new UserService())->getUserInfo($targetUserId, '');
|
||||||
if (!$targetUser['user']) {
|
if (!$targetUser['user']) {
|
||||||
throw new BusinessException('目标用户不存在');
|
throw new BusinessException('用户ID ' . $targetUserId . ' 不存在');
|
||||||
}
|
|
||||||
|
|
||||||
// 检查库存
|
|
||||||
if ($item->stock_qty < $qty) {
|
|
||||||
throw new BusinessException('库存不足,当前库存: ' . $item->stock_qty);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建领用申请记录(直接完成)
|
// 创建领用申请记录(直接完成)
|
||||||
@@ -469,7 +484,7 @@ class InvRequisitionService
|
|||||||
'claim_type' => InvRequisition::CLAIM_TYPE_PROXY,
|
'claim_type' => InvRequisition::CLAIM_TYPE_PROXY,
|
||||||
'proxy_claim_user_id' => $proxyUserId,
|
'proxy_claim_user_id' => $proxyUserId,
|
||||||
'proxy_claim_user_name' => $proxyUserName,
|
'proxy_claim_user_name' => $proxyUserName,
|
||||||
'remark' => $data['remark'] ?? '管理员代领',
|
'remark' => $remark,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// 创建领用明细
|
// 创建领用明细
|
||||||
@@ -482,19 +497,26 @@ class InvRequisitionService
|
|||||||
'is_consumable_snap' => $item->is_consumable,
|
'is_consumable_snap' => $item->is_consumable,
|
||||||
'request_qty' => $qty,
|
'request_qty' => $qty,
|
||||||
'approved_qty' => $qty,
|
'approved_qty' => $qty,
|
||||||
'remark' => $data['remark'] ?? '管理员代领',
|
'remark' => $remark,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// 扣减库存
|
$createdRequisitions[] = $requisition->toArray();
|
||||||
(new InvItemService())->stockOut($itemId, $qty, false, [
|
}
|
||||||
|
|
||||||
|
// 一次性扣减总库存
|
||||||
|
(new InvItemService())->stockOut($itemId, $totalQty, false, [
|
||||||
'biz_type' => InvStockLog::BIZ_TYPE_PROXY_CLAIM,
|
'biz_type' => InvStockLog::BIZ_TYPE_PROXY_CLAIM,
|
||||||
'biz_no' => $requisition->order_no,
|
'biz_no' => 'PROXY_' . time(),
|
||||||
'operator_id' => $proxyUserId,
|
'operator_id' => $proxyUserId,
|
||||||
'operator_name' => $proxyUserName,
|
'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'));
|
$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'))) {
|
if (!empty($request->get('start_time'))) {
|
||||||
$query->where('qr_claim_time', '>=', strtotime($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',
|
'item_id' => 'require|integer|gt:0',
|
||||||
'qty' => '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.integer' => '领取数量必须为整数',
|
||||||
'qty.gt' => '领取数量必须大于0',
|
'qty.gt' => '领取数量必须大于0',
|
||||||
'target_user_id.require' => '代领用户不能为空',
|
'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',
|
'item_id',
|
||||||
'qty',
|
'qty',
|
||||||
'target_user_id',
|
'target_user_id',
|
||||||
|
'target_user_ids',
|
||||||
'borrow_reason',
|
'borrow_reason',
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
|||||||
Reference in New Issue
Block a user