已签署处方在业务订单中可修改服用参数(只改订单页面与药房下单,不改 CA 签署原件);查看处方可下载 CA 原件或当前样式的药房联/处方联

- 处方已电子签署(内容冻结)时,「修改服用参数」改写为订单级覆盖值 usage_override(次数/天数/袋数,含辅方),
  签署原文、快照哈希与 CA PDF 不变;订单详情、处方笺、甘草/洛阳下单按本订单生效值;未签署处方仍直接修改处方
- 已发货锁定天数按生效值比较;重新开方改关联时清除旧覆盖值
- 迁移 2026_11_30_prescription_order_usage_override.sql
- 业务订单「查看处方」下载改为下拉:CA 签署处方 PDF、药房联 PDF、处方联 PDF(当前页面样式,A4);PC 与 H5 一致
- 重新构建管理后台

Co-Authored-By: Claude Opus 5 <[EMAIL]>
This commit is contained in:
Your Name
2026-11-30 14:49:34 +08:00
co-authored by Claude Opus 5
parent 562f8d6ce4
commit dc18098566
392 changed files with 566 additions and 397 deletions
@@ -1370,7 +1370,7 @@ class PrescriptionOrderLogic
if ($rxId > 0) {
$rxDetail = PrescriptionLogic::detail($rxId, $adminId, $adminInfo);
if ($rxDetail !== null) {
$arr['prescription'] = $rxDetail;
$arr['prescription'] = self::applyUsageOverride($rxDetail, $arr);
// 添加开方人姓名(优先使用处方的 doctor_name,否则从 creator_id 获取)
$doctorName = (string) ($rxDetail['doctor_name'] ?? '');
if ($doctorName === '') {
@@ -5254,6 +5254,7 @@ class PrescriptionOrderLogic
if ($prescription === null) {
throw new \DomainException(PrescriptionLogic::getError() ?: '处方不存在');
}
$prescription = self::applyUsageOverride($prescription, $order->toArray());
$herbs = PharmacyHerbIdentityResolver::resolve(
is_array($prescription['herbs'] ?? null) ? $prescription['herbs'] : [],
@@ -5395,6 +5396,7 @@ class PrescriptionOrderLogic
return false;
}
$rx = self::applyUsageOverride($rx, $order->toArray());
$c = Config::get('gancao_scm', []);
$nameMap = is_array($c['herb_id_map'] ?? null) ? $c['herb_id_map'] : [];
$herbs = is_array($rx['herbs'] ?? null) ? $rx['herbs'] : [];
@@ -5550,6 +5552,7 @@ class PrescriptionOrderLogic
self::$error = PrescriptionLogic::getError() !== '' ? PrescriptionLogic::getError() : '处方不存在';
return false;
}
$rx = self::applyUsageOverride($rx, $order->toArray());
$c = Config::get('gancao_scm', []);
$nameMap = is_array($c['herb_id_map'] ?? null) ? $c['herb_id_map'] : [];
@@ -5979,6 +5982,33 @@ class PrescriptionOrderLogic
public const SHIPPED_DAYS_LOCKED = '订单已发货,不能再修改服用天数';
/**
* 合并订单级服用参数覆盖(已签署处方的修改写在订单 usage_override),返回本订单生效的处方数组。
* 签署原文、处方表与 CA PDF 均不变;无覆盖时原样返回。
*/
public static function applyUsageOverride(array $rx, array $order): array
{
$override = $order['usage_override'] ?? null;
if (is_string($override)) {
$override = json_decode($override, true);
}
if (!is_array($override) || $override === []) {
return $rx;
}
foreach (['times_per_day', 'usage_days', 'dosage_bag_count'] as $key) {
if (isset($override[$key])) {
$rx[$key] = (int) $override[$key];
}
}
if (isset($override['aux_usage']) && is_array($override['aux_usage'])) {
$aux = $rx['aux_usage'] ?? null;
$aux = is_string($aux) ? (json_decode($aux, true) ?: []) : (is_array($aux) ? $aux : []);
$rx['aux_usage'] = $override['aux_usage'] + $aux;
}
$rx['usage_overridden'] = 1;
return $rx;
}
/** 已发货:履约已完成/已发货/已签收,或已填快递单号,或药房回传已发货(洛阳 SHIPPED/COMPLETED,甘草 20/30)。 */
public static function isOrderShipped(array $order): bool
{
@@ -6038,7 +6068,7 @@ class PrescriptionOrderLogic
continue; // 已提交药房的订单不改处方,须先完成远端取消。
}
Db::name('tcm_prescription_order')->where('id', (int) $order['id'])->update([
'prescription_id' => $prescriptionId, 'reissue_from_prescription_id' => 0,
'prescription_id' => $prescriptionId, 'reissue_from_prescription_id' => 0, 'usage_override' => null,
'prescription_audit_status' => 0, 'prescription_audit_remark' => '', 'update_time' => time(),
]);
self::writeLog((int) $order['id'], $adminId, $adminInfo, 'rx_reissue_link',
@@ -6143,7 +6173,9 @@ class PrescriptionOrderLogic
return false;
}
PrescriptionEsignPolicy::assertMutable($rx->toArray());
// CA 签名覆盖服用参数:已签署(冻结)的处方不改签署原文,修改写入订单级覆盖值,仅作用于本订单页面与药房下单。
$frozen = !empty($rx->esign_required) && (string) ($rx->esign_status ?? 'pending') !== 'pending';
$current = self::applyUsageOverride($rx->toArray(), $order->toArray());
if (!PrescriptionLogic::canViewPrescription($rx, $adminId, $adminInfo)) {
self::setError('无权限修改此处方');
@@ -6214,10 +6246,11 @@ class PrescriptionOrderLogic
// 已发货后天数随药品已定,不可再改;次数、袋数等仍可修正。
if (self::isOrderShipped($order->toArray())) {
$oldUsage = is_array($rx->aux_usage) ? $rx->aux_usage : (json_decode((string) $rx->aux_usage, true) ?: []);
// 订单未单独设置天数时,以处方开立天数为准(弹窗会用它预填)。
$effectiveMedDays = (int) ($order->medication_days ?? 0) ?: (int) ($rx->usage_days ?? 0);
if ($usageDays !== (int) ($rx->usage_days ?? 0) || $medDays !== $effectiveMedDays
$oldUsage = is_array($current['aux_usage'] ?? null) ? $current['aux_usage']
: (json_decode((string) ($current['aux_usage'] ?? ''), true) ?: []);
// 订单未单独设置天数时,以处方开立天数为准(弹窗会用它预填)。
$effectiveMedDays = (int) ($order->medication_days ?? 0) ?: (int) ($current['usage_days'] ?? 0);
if ($usageDays !== (int) ($current['usage_days'] ?? 0) || $medDays !== $effectiveMedDays
|| ($hasAux && $auxUsageDays !== (int) ($oldUsage['usage_days'] ?? 0))) {
self::setError(self::SHIPPED_DAYS_LOCKED);
@@ -6225,11 +6258,11 @@ class PrescriptionOrderLogic
}
}
$oldTimes = (int) ($rx->times_per_day ?? 0);
$oldUsageDays = (int) ($rx->usage_days ?? 0);
$oldDosageBagCount = max(1, (int) ($rx->dosage_bag_count ?? 1));
$oldTimes = (int) ($current['times_per_day'] ?? 0);
$oldUsageDays = (int) ($current['usage_days'] ?? 0);
$oldDosageBagCount = max(1, (int) ($current['dosage_bag_count'] ?? 1));
$oldMedDays = (int) ($order->medication_days ?? 0);
$oldAuxUsage = $rx->aux_usage;
$oldAuxUsage = $current['aux_usage'] ?? null;
if (is_string($oldAuxUsage)) {
$decoded = json_decode($oldAuxUsage, true);
$oldAuxUsage = is_array($decoded) ? $decoded : [];
@@ -6286,15 +6319,22 @@ class PrescriptionOrderLogic
}
$rxUpdates['aux_usage'] = $auxUsage;
}
if ($prescriptionChanged && PrescriptionEsignPolicy::isPendingReview($rx->toArray())) {
$rxUpdates['review_submitted_at'] = 0;
if ($frozen) {
// 签署原文不变;订单记录覆盖值(含未修改项,便于完整还原当前生效参数)。
$override = $rxUpdates + ['dosage_bag_count' => max(1, (int) ($current['dosage_bag_count'] ?? 1))];
$order->usage_override = json_encode($override, JSON_UNESCAPED_UNICODE);
} else {
if ($prescriptionChanged && PrescriptionEsignPolicy::isPendingReview($rx->toArray())) {
$rxUpdates['review_submitted_at'] = 0;
}
$rx->save($rxUpdates);
}
$rx->save($rxUpdates);
$order->medication_days = $medDays;
$order->save();
$summary = '服用参数:' . implode(';', $parts);
$summary = ($frozen && $prescriptionChanged ? '服用参数(仅本订单生效,CA 签署处方不变):' : '服用参数:')
. implode(';', $parts);
self::writeLog($prescriptionOrderId, $adminId, $adminInfo, 'patch_rx_usage', $summary, true);
return true;