已提交药房的处方也可作废:先远端撤单再作废

- 洛阳药房:作废时先调用 POST /api/openapi/v1/prescription-orders/{订单号}/cancel 撤单,确认 CANCELLED 后再作废并挂起订单;
  远端拒绝(已发货/已扣库存,409)或未确认时不作废并提示原因;撤单写订单日志。撤单后的订单重新审核通过后以新版本提交药房
- 甘草:暂无撤单接口,须先在甘草撤单;甘草回传撤单/驳回(91/92)后可作废
- 远端已确认取消且无在途新版本提交时,订单不再被药房快照锁定
- 列表与详情的 can_void 按「能否自动撤单」判断
- 重新构建管理后台

Co-Authored-By: Claude Opus 5 <[EMAIL]>
This commit is contained in:
Your Name
2026-11-30 11:47:14 +08:00
co-authored by Claude Opus 5
parent b55f30753c
commit 365b16c14d
388 changed files with 540 additions and 360 deletions
@@ -256,7 +256,8 @@ class PrescriptionLogic
$viewerAdminInfo,
self::getVoidAccess($viewerAdminId, $viewerAdminInfo)
)
&& PrescriptionOrderLogic::remoteSnapshotLockErrorForPrescription($id) === null;
// 已提交洛阳药房的订单在作废时自动撤单;甘草等无法自动撤单的才阻止作废。
&& PrescriptionOrderLogic::voidBlockerForPrescription($id) === null;
return $row;
}
@@ -1418,6 +1419,12 @@ class PrescriptionLogic
self::setError('处方不存在或无权作废');
return false;
}
if ((int) ($row->void_status ?? 0) === 1) {
self::setError('该处方已作废');
return false;
}
// 已提交药房的订单先远端撤单,确认撤销后再在同一锁内作废。
PrescriptionOrderLogic::cancelRemoteOrdersForVoid($id, (string) $row->sn, $adminId, $adminInfo);
return (bool) LockedPharmacySnapshotMutation::executeForPrescription(
$id,
@@ -5886,6 +5886,89 @@ class PrescriptionOrderLogic
];
}
/**
* 处方作废前必须先撤销仍在药房的订单。返回 null 表示可作废(含需由系统自动撤单的洛阳药房订单),否则返回原因。
* 仅判断不调用远端,供列表/详情的 can_void 使用。
*/
public static function voidBlockerForPrescription(int $prescriptionId): ?string
{
foreach (self::lockedPharmacyOrders($prescriptionId) as [$order, $claim]) {
$reason = self::remoteCancelBlocker($order, $claim);
if ($reason !== null) {
return $reason;
}
}
return null;
}
/**
* 作废处方前先远端撤单:洛阳药房订单调用撤单接口,确认已撤销后记录状态与订单日志;
* 甘草订单暂无撤单接口,须先在甘草撤单,回传撤单(91/92)后即可作废。失败抛 DomainException。
*/
public static function cancelRemoteOrdersForVoid(int $prescriptionId, string $prescriptionSn, int $adminId, array $adminInfo): void
{
foreach (self::lockedPharmacyOrders($prescriptionId) as [$order, $claim]) {
$reason = self::remoteCancelBlocker($order, $claim);
if ($reason !== null) {
throw new \DomainException($reason);
}
$remoteNo = (string) $order['ej_pharmacy_order_no'];
try {
$response = app(EjPharmacyClient::class)->cancelPrescriptionOrder((string) $order['order_no'],
max((int) ($claim['source_revision'] ?? 1), 1), "处方 {$prescriptionSn} 作废,撤销药房订单");
} catch (\Throwable) {
throw new \DomainException("洛阳药房订单 {$remoteNo} 撤单请求失败,处方未作废,请稍后重试");
}
$httpStatus = (int) ($response['http_status'] ?? 0);
$body = is_array($response['body'] ?? null) ? $response['body'] : [];
$status = strtoupper(trim((string) (($body['data'] ?? [])['status'] ?? '')));
if ($httpStatus === 409) {
throw new \DomainException("洛阳药房订单 {$remoteNo} 已发货或已扣减库存,无法自动撤单,请联系药房处理后再作废");
}
if ($httpStatus !== 200 || (int) ($body['code'] ?? -1) !== 0 || $status !== 'CANCELLED') {
throw new \DomainException("洛阳药房订单 {$remoteNo} 撤单未确认,处方未作废,请稍后重试");
}
Db::name('tcm_prescription_order')->where('id', (int) $order['id'])
->update(['ej_pharmacy_status' => 'CANCELLED', 'update_time' => time()]);
self::writeLog((int) $order['id'], $adminId, $adminInfo, 'ej_cancel',
"作废处方 {$prescriptionSn} 前已撤销洛阳药房订单 {$remoteNo}", true);
}
}
/** @return list<array{0: array<string,mixed>, 1: ?array<string,mixed>}> 该处方下仍被药房快照锁定的订单 */
private static function lockedPharmacyOrders(int $prescriptionId): array
{
$locked = [];
$orders = Db::name('tcm_prescription_order')->where('prescription_id', $prescriptionId)
->whereNull('delete_time')->order('id')->select()->toArray();
foreach ($orders as $order) {
$claim = PharmacySubmissionClaimService::claimForOrder((int) $order['id']);
if (PharmacyRemoteSnapshotPolicy::isLocked($order, $claim)) {
$locked[] = [$order, $claim];
}
}
return $locked;
}
/** 已锁定订单不能自动撤单的原因;可由系统撤销的洛阳药房订单返回 null。 */
private static function remoteCancelBlocker(array $order, ?array $claim): ?string
{
if (PharmacyRemoteSnapshotPolicy::isClaimInFlight($claim)) {
return '订单正在提交药房,提交结果确认后再作废';
}
$ejNo = trim((string) ($order['ej_pharmacy_order_no'] ?? ''));
if ($ejNo === '') {
return '订单已提交甘草药房,系统暂不支持自动撤单:请先在甘草撤单,甘草回传撤单后即可作废';
}
if (in_array(strtoupper(trim((string) ($order['ej_pharmacy_status'] ?? ''))), ['SHIPPED', 'COMPLETED'], true)) {
return "洛阳药房订单 {$ejNo} 已发货,无法撤单作废";
}
if (!EjPharmacyClient::isConfigured()) {
return '洛阳药房接口未启用,无法自动撤单';
}
return null;
}
/** 处方作废时可随重新开方迁移的订单状态:待审核(1)、履约中(2);已发货/已完成的订单按原处方留档。 */
private const REISSUE_FOLLOW_STATUSES = [1, 2];