This commit is contained in:
Your Name
2026-11-26 15:46:20 +08:00
parent db6d327e91
commit be79e09c83
170 changed files with 21159 additions and 13217 deletions
@@ -28,6 +28,7 @@ class PrescriptionLogic
public const PRESCRIBE_PERMISSIONS = [
'tcm.prescription/add', 'cf.prescription/add', 'tcm.diagnosis/chufang', 'tcm.diagnosis/kaifang',
];
public const REVIEW_SUBMIT_PERMISSIONS = [...self::PRESCRIBE_PERMISSIONS, 'tcm.prescription/edit', 'cf.prescription/edit'];
public const VOID_MANAGE_PERMISSIONS = ['tcm.prescription/void', 'cf.prescription/del'];
public const AUDIT_PERMISSIONS = ['tcm.prescription/audit', 'cf.prescription/audit'];
@@ -188,18 +189,67 @@ class PrescriptionLogic
&& (int) ($row['audit_by'] ?? 0) > 0 && (int) $row['audit_by'] === (int) ($row['void_by'] ?? 0)
&& (empty($row['esign_required']) || ($row['esign_status'] ?? 'pending') === 'pending');
}
public static function canSubmitForReview(int $adminId, array $adminInfo): bool
{
return $adminId > 0 && ((int) ($adminInfo['root'] ?? 0) === 1
|| array_intersect(self::REVIEW_SUBMIT_PERMISSIONS, array_map('strtolower', AuthLogic::getAuthByAdminId($adminId))) !== []);
}
public static function withSigningStatus(array $row, int $adminId, bool $maySubmit, bool $details = false): array
{
$row['esign'] = PrescriptionEsignService::describe($row, $adminId, $details, $maySubmit);
$row['review_submitted_at'] = $row['esign']['review_submitted_at'];
$row['can_submit_for_review'] = $row['esign']['can_submit_for_review'];
$row['can_review_prescription'] = $row['esign']['can_review_prescription'];
return $row;
}
public static function submitForReview(int $id, int $adminId, array $adminInfo): array
{
if (!self::canSubmitForReview($adminId, $adminInfo)) {
throw new \DomainException('无权限上传处方');
}
return Db::transaction(static function () use ($id, $adminId): array {
$prescription = Prescription::where('id', $id)->whereNull('delete_time')->lock(true)->find();
if (!$prescription || (int) $prescription->creator_id !== $adminId) {
throw new \DomainException('处方不存在或非本人开具');
}
$row = $prescription->toArray();
$required = PrescriptionEsignService::requiresSignature($row, PrescriptionEsignService::enabled());
if (!empty($row['void_status']) || !PrescriptionEsignService::hasUploadContent($row, $required)) {
throw new \DomainException('请确认处方未作废并已补全内容');
}
$submittedAt = (int) ($row['review_submitted_at'] ?? 0);
if ($submittedAt <= 0) {
if (!PrescriptionEsignPolicy::isPendingReview($row)) {
throw new \DomainException('当前处方状态不可上传');
}
$submittedAt = time();
if (!$prescription->save(['review_submitted_at' => $submittedAt])) {
throw new \DomainException('处方上传未完成,请重试');
}
}
return ['review_submitted_at' => $submittedAt, 'can_submit_for_review' => false];
});
}
private static function withActionAvailability(array $row, int $viewerAdminId, array $viewerAdminInfo): array
{
$id = (int) $row['id'];
// 与列表和创建校验一致:已取消 / 已退款的订单保留历史关联,但不再占用处方。
$hasBizOrder = PrescriptionOrder::where('prescription_id', $id)
$orders = PrescriptionOrder::where('prescription_id', $id)
->whereNull('delete_time')
->whereNotIn('fulfillment_status', PrescriptionOrder::RELEASED_PRESCRIPTION_STATUSES)
->count() > 0;
->field(['fulfillment_status', 'prescription_audit_status', 'payment_slip_audit_status'])
->select()->toArray();
$hasBizOrder = array_filter($orders, static fn (array $order): bool =>
!in_array((int) $order['fulfillment_status'], PrescriptionOrder::RELEASED_PRESCRIPTION_STATUSES, true)) !== [];
$row['has_prescription_order'] = $hasBizOrder ? 1 : 0;
$row['can_restore_rejected'] = self::canRestoreRejected($row);
$row['can_void'] = !$hasBizOrder && empty($row['void_status'])
$row['can_void'] = empty($row['void_status']) && !self::hasBlockingVoidOrder($row, $orders)
&& self::hasVoidAccess(
$row,
$viewerAdminId,
@@ -211,6 +261,24 @@ class PrescriptionLogic
return $row;
}
/** Pending prescriptions can be voided while any active order is still waiting for both reviews. */
public static function hasBlockingVoidOrder(array $prescription, array $orders): bool
{
foreach ($orders as $order) {
$fulfillmentStatus = (int) ($order['fulfillment_status'] ?? -1);
if (in_array($fulfillmentStatus, PrescriptionOrder::RELEASED_PRESCRIPTION_STATUSES, true)) {
continue;
}
if ($fulfillmentStatus !== 1
|| !in_array($order['prescription_audit_status'] ?? null, [0, '0'], true)
|| !in_array($order['payment_slip_audit_status'] ?? null, [0, '0'], true)
|| !PrescriptionEsignPolicy::isPendingReview($prescription)) {
return true;
}
}
return false;
}
private static function generateSn(): string
{
return 'RX' . date('Ymd') . str_pad((string)mt_rand(1, 9999), 4, '0', STR_PAD_LEFT);
@@ -648,6 +716,7 @@ class PrescriptionLogic
'is_system_auto' => 0,
// 开方医生修改后重新进入待审核
'audit_status' => 0,
'review_submitted_at' => 0,
'audit_time' => null,
'audit_by' => null,
'audit_by_name' => '',
@@ -790,7 +859,7 @@ class PrescriptionLogic
'patient_name' => $patientName,
'phone' => $phone,
'gender' => $gender,
]);
] + (PrescriptionEsignPolicy::isPendingReview($prescription->toArray()) ? ['review_submitted_at' => 0] : []));
$latestPo = PrescriptionOrder::where('prescription_id', $rxId)
->whereNull('delete_time')
@@ -881,7 +950,8 @@ class PrescriptionLogic
return null;
}
$arr = $row->toArray();
$arr['esign'] = PrescriptionEsignService::describe($arr, $viewerAdminId);
$arr = self::withSigningStatus($arr, $viewerAdminId,
(int) $row->creator_id === $viewerAdminId && self::canSubmitForReview($viewerAdminId, $viewerAdminInfo), true);
$arr['gender_desc'] = $arr['gender'] == 1 ? '男' : '女';
$bizPo = PrescriptionOrder::where('prescription_id', $id)
@@ -942,7 +1012,7 @@ class PrescriptionLogic
if (!$row || (int) ($row->void_status ?? 0) === 1 || (int) ($row->audit_status ?? 1) !== 0) {
return null;
}
PrescriptionEsignService::assertSignedById($prescriptionId, false, $adminId);
PrescriptionEsignService::assertSignedById($prescriptionId, false, $adminId, orderApproval: true);
$row->audit_status = 1;
$row->audit_time = time();
$row->audit_by = $adminId;
@@ -1046,6 +1116,7 @@ class PrescriptionLogic
}
if ($action === 'reject') {
PrescriptionEsignService::assertReviewReady($row->toArray());
if (trim($remark) === '') {
self::setError('驳回时请填写审核意见');
@@ -1166,10 +1237,9 @@ class PrescriptionLogic
$rows,
static fn (array $row): bool => self::canViewPrescription($row, $viewerAdminId, $viewerAdminInfo)
));
return array_map(static function (array $row) use ($viewerAdminId): array {
$row['esign'] = PrescriptionEsignService::describe($row, $viewerAdminId);
return $row;
}, $visible);
$maySubmit = in_array($viewerAdminId, array_map('intval', array_column($visible, 'creator_id')), true)
&& self::canSubmitForReview($viewerAdminId, $viewerAdminInfo);
return array_map(static fn (array $row): array => self::withSigningStatus($row, $viewerAdminId, $maySubmit), $visible);
}
/**
@@ -1206,8 +1276,8 @@ class PrescriptionLogic
return null;
}
$result = $row->toArray();
$result['esign'] = PrescriptionEsignService::describe($result, $viewerAdminId);
$result = self::withSigningStatus($row->toArray(), $viewerAdminId,
(int) $row->creator_id === $viewerAdminId && self::canSubmitForReview($viewerAdminId, $viewerAdminInfo));
return self::withActionAvailability($result, $viewerAdminId, $viewerAdminInfo);
}
@@ -1348,7 +1418,7 @@ class PrescriptionLogic
/**
* 作废处方
*/
public static function void(int $id, int $adminId, array $adminInfo): bool
public static function void(int $id, int $adminId, array $adminInfo, bool $pendingOnly = false): bool
{
self::setError('');
try {
@@ -1362,7 +1432,7 @@ class PrescriptionLogic
return (bool) LockedPharmacySnapshotMutation::executeForPrescription(
$id,
static fn (): bool => self::voidLocked($id, $adminId, $adminInfo, $access)
static fn (array $orders): bool => self::voidLocked($id, $adminId, $adminInfo, $access, $pendingOnly, $orders)
);
} catch (\Throwable $exception) {
if (self::getError() === '') {
@@ -1372,7 +1442,7 @@ class PrescriptionLogic
}
}
private static function voidLocked(int $id, int $adminId, array $adminInfo, array $access): bool
private static function voidLocked(int $id, int $adminId, array $adminInfo, array $access, bool $pendingOnly, array $orders): bool
{
$row = Prescription::where('id', $id)->whereNull('delete_time')->lock(true)->find();
if (!$row || !self::hasVoidAccess($row->toArray(), $adminId, $adminInfo, $access)) {
@@ -1383,13 +1453,12 @@ class PrescriptionLogic
self::setError('该处方已作废');
return false;
}
$rxId = (int) ($row->id ?? 0);
$bizCount = (int) PrescriptionOrder::where('prescription_id', $rxId)
->whereNull('delete_time')
->whereNotIn('fulfillment_status', PrescriptionOrder::RELEASED_PRESCRIPTION_STATUSES)
->count();
if ($bizCount > 0) {
self::setError('该处方已存在业务订单,无法作废');
if ($pendingOnly && !PrescriptionEsignPolicy::isPendingReview($row->toArray())) {
self::setError('处方审核状态已变化,请刷新列表后查看');
return false;
}
if (self::hasBlockingVoidOrder($row->toArray(), $orders)) {
self::setError('关联业务订单的审核或履约状态不允许作废此处方');
return false;
}