diff --git a/admin/src/api/tcm.ts b/admin/src/api/tcm.ts index ddfaaae6b..36ba888b0 100755 --- a/admin/src/api/tcm.ts +++ b/admin/src/api/tcm.ts @@ -633,7 +633,7 @@ export function prescriptionOrderExport(params: any) { } /** - * 诊单下可关联的支付单(已支付 zyt_order;已占用且未撤回的会排除;编辑时传 prescription_order_id 保留当前单已选)。 + * 诊单下可关联的支付单(已支付 zyt_order;已关联其他业务订单的会排除;编辑时传 prescription_order_id 保留当前单已选)。 * 服务端仅返回创建时间在 2026-04-20(含)之后的支付单;编辑时本单已关联的旧单仍会出现在列表中。 */ export function prescriptionOrderPaidPayOrders(params: { @@ -662,8 +662,27 @@ export function prescriptionOrderLogisticsJdUpdate(params: { id: number }) { return request.post({ url: '/tcm.prescriptionOrder/logisticsJdUpdate', params }) } -export function prescriptionOrderDetail(params: { id: number }) { - return request.get({ url: '/tcm.prescriptionOrder/detail', params }) +export function prescriptionOrderDetail(params: { id: number; prescription_id?: number }, signal?: AbortSignal) { + return request.get({ url: '/tcm.prescriptionOrder/detail', params, signal }, signal ? { ignoreCancelToken: true } : undefined) +} + +export interface PrescriptionBindingOrder { + id: number + order_no: string + fulfillment_status: number + is_primary: boolean + is_linked: boolean +} + +export function prescriptionOrdersForBinding(params: { prescription_id: number; order_no?: string }, signal: AbortSignal) { + return request.get<{ orders: PrescriptionBindingOrder[]; can_bind: boolean }>( + { url: '/tcm.prescriptionOrder/prescriptionOrdersForBinding', params, signal }, + { ignoreCancelToken: true } + ) +} + +export function prescriptionOrderBindPrescription(params: { id: number; prescription_id: number }) { + return request.post({ url: '/tcm.prescriptionOrder/bindPrescription', params }) } export function prescriptionOrderEdit(params: Record) { @@ -836,6 +855,10 @@ export function prescriptionOrderUnlinkPayOrder(params: { id: number; pay_order_ return request.post({ url: '/tcm.prescriptionOrder/unlinkPayOrder', params }) } +export function prescriptionOrderAttachReceipt(params: { id: number; pay_order_id: number }) { + return request.post({ url: '/tcm.prescriptionOrder/attachReceipt', params }) +} + /** 修改订单金额 */ export function prescriptionOrderUpdateAmount(params: { id: number; amount: number }) { return request.post({ url: '/tcm.prescriptionOrder/updateAmount', params }) diff --git a/admin/src/views/consumer/prescription/components/LinkedOrderPrescriptionSelect.vue b/admin/src/views/consumer/prescription/components/LinkedOrderPrescriptionSelect.vue new file mode 100644 index 000000000..dd0cc6477 --- /dev/null +++ b/admin/src/views/consumer/prescription/components/LinkedOrderPrescriptionSelect.vue @@ -0,0 +1,27 @@ + + + diff --git a/admin/src/views/consumer/prescription/components/PrescriptionLinkedOrdersDialog.vue b/admin/src/views/consumer/prescription/components/PrescriptionLinkedOrdersDialog.vue new file mode 100644 index 000000000..a105255ea --- /dev/null +++ b/admin/src/views/consumer/prescription/components/PrescriptionLinkedOrdersDialog.vue @@ -0,0 +1,177 @@ + + + diff --git a/admin/src/views/consumer/prescription/components/PrescriptionOrderDetailDrawer.vue b/admin/src/views/consumer/prescription/components/PrescriptionOrderDetailDrawer.vue index e897f1310..49fb8c326 100755 --- a/admin/src/views/consumer/prescription/components/PrescriptionOrderDetailDrawer.vue +++ b/admin/src/views/consumer/prescription/components/PrescriptionOrderDetailDrawer.vue @@ -42,7 +42,7 @@
- +
@@ -201,22 +201,22 @@ -
+
审核通过
-
+
已驳回
+ + + > + 重试 + - + @@ -1085,13 +1106,14 @@ import { prescriptionOrderAddLog, prescriptionOrderLogisticsTrace, prescriptionOrderLogisticsJdUpdate, - prescriptionOrderPaidPayOrders, - prescriptionOrderUnlinkPayOrder, prescriptionOrderPatchPrescriptionUsage } from '@/api/tcm' import { getDictData } from '@/api/app' import feedback from '@/utils/feedback' import useUserStore from '@/stores/modules/user' +import LinkedOrderPrescriptionSelect from './LinkedOrderPrescriptionSelect.vue' +import { useLinkedOrderPrescription } from './useLinkedOrderPrescription' +import { usePrescriptionOrderReceipts } from './usePrescriptionOrderReceipts' import { TCM_ASSISTANT_ROLE_ID, PRESCRIPTION_AUDIT_ROLE_IDS, @@ -1149,13 +1171,13 @@ defineSlots<{ /** 抽屉头部左侧扩展(完整版的发货类型切换等) */ 'header-extra'?: (props: { detail: any }) => any /** 抽屉头部右侧操作按钮(审核/发货/退款等) */ - 'header-actions'?: (props: { detail: any }) => any + 'header-actions'?: (props: { detail: any; isPrimaryPrescription: boolean }) => any }>() const emit = defineEmits<{ (e: 'update-amount'): void (e: 'patch-rx-patient'): void - (e: 'view-prescription'): void + (e: 'view-prescription', prescriptionId: number): void (e: 'test-gancao-preview'): void (e: 'view-patient'): void (e: 'detail-changed'): void @@ -1178,46 +1200,35 @@ const showPrescriptionAuditFilter = computed(() => { const detailVisible = ref(false) const detailLoading = ref(false) const detailData = ref | null>(null) -const detailLogs = ref([]) -const detailUnlinkedPayOrders = ref([]) -const unlinkPayOrderId = ref(null) -const canUnlinkPayOrder = computed(() => - !props.readonly && canUpdateAmount(detailData.value) && hasPerm('tcm.prescriptionOrder/unlinkPayOrder') +const { + options: linkedPrescriptionOptions, + selectedId: selectedPrescriptionId, + isPrimary: isPrimaryPrescription, + prescription: detailPrescription, + loading: linkedPrescriptionLoading, + detailError: selectedPrescriptionError, + herbsVisible: detailHerbsVisible, + reset: resetLinkedPrescription, + select: selectLinkedPrescription +} = useLinkedOrderPrescription(detailData, detailVisible, computed(() => !props.detailLoader)) +const detailPrescriptionDoctor = computed(() => + isPrimaryPrescription.value ? detailData.value?.doctor_name : detailPrescription.value?.doctor_name ) - -async function confirmUnlinkPayOrder(row: { id: number; order_no?: string; amount: number | string; status: number | string }) { - const orderId = Number(detailData.value?.id) - const payOrderId = Number(row.id) - if (!canUnlinkPayOrder.value || !orderId || !payOrderId || unlinkPayOrderId.value !== null) return - if (![2, 4, 5].includes(Number(row.status))) return - - unlinkPayOrderId.value = payOrderId - try { - await feedback.confirm( - `确定移除收款记录「${row.order_no || '#' + payOrderId}」(¥${formatMoney(row.amount)})?` + - '仅解除关联,原收款记录保留;订单总金额不变,已付金额与需代收金额按剩余关联收款重新计算。' - ) - if (!detailVisible.value || Number(detailData.value?.id) !== orderId) return - const res: any = await prescriptionOrderUnlinkPayOrder({ id: orderId, pay_order_id: payOrderId }) - const updated = res?.data ?? res - if (Number(detailData.value?.id) === orderId && Number(updated?.id) === orderId) { - Object.assign(detailData.value!, updated) - } - feedback.msgSuccess('收款关联已移除,金额已同步更新') - emit('detail-changed') - await refreshIfCurrent(orderId) - } catch { - /* 取消不提交;接口错误由拦截器提示,保留当前收款记录 */ - } finally { - unlinkPayOrderId.value = null - } -} - -const detailPrescription = computed(() => { - const p = detailData.value?.prescription - return p && typeof p === 'object' ? p : null +const detailLogs = ref([]) +const { + unlinkedReceipts: detailUnlinkedPayOrders, + loadUnlinkedReceipts, + linkingReceiptId, + unlinkingReceiptId, + changingReceipts, + canAttachReceipt, + canUnlinkReceipt, + confirmAttachReceipt, + confirmUnlinkReceipt +} = usePrescriptionOrderReceipts(detailData, detailVisible, (orderId) => { + emit('detail-changed') + if (detailVisible.value && Number(detailData.value?.id) === orderId) void fetchLogs(orderId) }) - const detailDietaryText = computed(() => formatDietaryTaboo(detailPrescription.value?.dietary_taboo)) const detailLinkedPayOrders = computed(() => { @@ -1294,13 +1305,13 @@ const workflowFulfillStatus = computed(() => { /** 详情抽屉:业务收货手机 vs 处方登记手机 */ const detailRecipientRxPhoneMismatch = computed(() => { - const rx = detailPrescription.value?.phone + const rx = detailData.value?.prescription?.phone const biz = detailData.value?.recipient_phone return recipientVsPrescriptionPhoneMismatch(biz, rx) }) const detailPrescriptionPhoneDisplay = computed(() => { - const s = normalizeBizPhone(detailPrescription.value?.phone) + const s = normalizeBizPhone(detailData.value?.prescription?.phone) return s || '—' }) @@ -1377,7 +1388,7 @@ const detailHasAuxHerbs = computed(() => prescriptionHasAuxFormula(detailPrescri const daysLocked = computed(() => detailData.value?.days_locked === true) /** 处方已进入电子签署(内容冻结),服用参数改写为订单级覆盖值 */ const usageFrozenBySignature = computed(() => { - const esign = (detailPrescription.value as any)?.esign + const esign = detailData.value?.prescription?.esign return esign?.required === true && esign?.status !== 'pending' }) @@ -1390,9 +1401,6 @@ const detailAuxUsage = computed(() => { return normalizeSlipAuxUsageForm(raw, rx.prescription_type || '浓缩水丸') }) -/** false=无权限;true/缺省兼容旧接口(旧版未下发该字段时仍展示药材) */ -const detailHerbsVisible = computed(() => detailData.value?.prescription_detail_herbs_visible !== false) - /** 内部成本(财务字段):仅完整版展示 */ const showInternalCost = computed( () => !props.readonly && detailData.value?.internal_cost != null && detailData.value?.internal_cost !== '' @@ -1466,6 +1474,7 @@ function canAddPrescriptionOrderLog() { /** 与后端 canAuditPrescriptionOrder 同档:超管或 prescription_audit_roles */ const canSetRxAuditOnAddLog = computed(() => { + if (!isPrimaryPrescription.value) return false if (Number(detailData.value?.prescription_audit_status) === 0 && detailData.value?.can_review_prescription === false) return false const u = userStore.userInfo @@ -1534,6 +1543,7 @@ function openPatchUsageDialog() { feedback.msgWarning('无处方数据') return } + if (rx.id && Number(rx.id) !== Number(ord.prescription_id)) return const aux = detailAuxUsage.value patchUsageForm.dosage_bag_count = Number(rx.dosage_bag_count) > 0 ? Number(rx.dosage_bag_count) : 1 patchUsageForm.times_per_day = @@ -1668,28 +1678,6 @@ async function submitAddLog() { } } -// ─── 未关联支付单 ─── -async function loadDetailUnlinkedPayOrders(diagnosisId: number, prescriptionOrderId: number, linkedIds: number[]) { - if (!diagnosisId) { - detailUnlinkedPayOrders.value = [] - return - } - try { - const res: any = await prescriptionOrderPaidPayOrders({ - diagnosis_id: diagnosisId, - prescription_order_id: prescriptionOrderId - }) - const lists = res?.lists ?? res?.data?.lists ?? [] - // 列表已由服务端限制;此处再过滤已关联项 - const filtered = Array.isArray(lists) - ? lists.filter((item: any) => !linkedIds.includes(item.id)) - : [] - detailUnlinkedPayOrders.value = filtered - } catch { - detailUnlinkedPayOrders.value = [] - } -} - // ─── 物流轨迹 ─── const detailLogisticsExpress = ref('auto') const logisticsTraceLoading = ref(false) @@ -1795,11 +1783,15 @@ function requestDetail(id: number) { return props.detailLoader ? props.detailLoader({ id }) : prescriptionOrderDetail({ id }) } -async function open(id: number) { +let detailOpenRequest = 0 + +async function open(id: number, prescriptionId?: number) { + const request = ++detailOpenRequest // 页面级同参数字典请求会取消抽屉挂载时的那次(axios 去重取消),打开时兜底重试 void loadServicePackageOptions() // 显式彻底清空缓存,防止前一次弹窗的数据残留 detailData.value = null + resetLinkedPrescription() detailUnlinkedPayOrders.value = [] bumpLogisticsTraceRequestToken() detailLogisticsExpress.value = 'auto' @@ -1810,6 +1802,7 @@ async function open(id: number) { detailLoading.value = true try { const res: any = await requestDetail(id) + if (request !== detailOpenRequest || !detailVisible.value) return const d = res?.data ?? res ?? null detailData.value = d if (d) { @@ -1821,29 +1814,27 @@ async function open(id: number) { } if (props.loadRelatedData) fetchLogs(id) - // 加载未关联的支付单 - const diagId = d.diagnosis_id - const linkedIds = d.pay_order_ids || [] - if (props.loadRelatedData && diagId) { - void loadDetailUnlinkedPayOrders(diagId, id, linkedIds) - } + if (props.loadRelatedData) void loadUnlinkedReceipts(d) + if (prescriptionId) void selectLinkedPrescription(prescriptionId) } } catch (e: any) { + if (request !== detailOpenRequest || !detailVisible.value) return feedback.msgError(e?.message || '加载失败') detailVisible.value = false } finally { - detailLoading.value = false + if (request === detailOpenRequest) detailLoading.value = false } } /** 静默刷新当前详情(含日志/未关联支付单;快递单号变化时重拉轨迹),供父页面操作成功后调用 */ async function refresh() { - const id = Number(detailData.value?.id) + const current = detailData.value + const id = Number(current?.id) if (!id) return try { const res: any = await requestDetail(id) const d = res?.data ?? res ?? null - if (!d) return + if (!d || detailData.value !== current) return const prevTracking = String(detailData.value?.tracking_number || '').trim() detailData.value = d const nextTracking = String(d.tracking_number || '').trim() @@ -1853,10 +1844,7 @@ async function refresh() { void fetchLogisticsTrace() } if (props.loadRelatedData) await fetchLogs(id) - const diagId = d.diagnosis_id - if (props.loadRelatedData && diagId) { - void loadDetailUnlinkedPayOrders(diagId, id, d.pay_order_ids || []) - } + if (props.loadRelatedData) void loadUnlinkedReceipts(d) } catch { /* 静默失败,避免打断当前抽屉 */ } diff --git a/admin/src/views/consumer/prescription/components/prescription-order-utils.ts b/admin/src/views/consumer/prescription/components/prescription-order-utils.ts index 18495b041..bf36c676b 100755 --- a/admin/src/views/consumer/prescription/components/prescription-order-utils.ts +++ b/admin/src/views/consumer/prescription/components/prescription-order-utils.ts @@ -168,6 +168,7 @@ export function logActionText(act: string) { ship: '确认发货', withdraw: '撤销', link_pay_order: '关联支付单', + bind_prescription: '绑定关联处方', unlink_pay_order: '移除收款关联', completion_request: '完单申请', auto_complete: '自动完成', diff --git a/admin/src/views/consumer/prescription/components/useLinkedOrderPrescription.ts b/admin/src/views/consumer/prescription/components/useLinkedOrderPrescription.ts new file mode 100644 index 000000000..85bc1bb53 --- /dev/null +++ b/admin/src/views/consumer/prescription/components/useLinkedOrderPrescription.ts @@ -0,0 +1,89 @@ +import { computed, onScopeDispose, ref, watch, type Ref } from 'vue' +import { prescriptionOrderDetail } from '@/api/tcm' + +export interface LinkedPrescription { + id: number + sn: string + void_status: number + is_primary: boolean +} + +export type OrderDetail = Record + +export function linkedPrescriptionOptions(order: OrderDetail | null): LinkedPrescription[] { + const entries: LinkedPrescription[] = Array.isArray(order?.linked_prescriptions) ? order.linked_prescriptions : [] + return entries.map((entry) => ({ + ...entry, + id: Number(entry.id), + is_primary: Number(entry.id) === Number(order?.prescription_id) + })) +} + +export async function loadLinkedOrderPrescription(orderId: number, prescriptionId: number, signal: AbortSignal) { + const response = await prescriptionOrderDetail({ id: orderId, prescription_id: prescriptionId }, signal) + const detail = response?.data ?? response + if (Number(detail?.id) !== orderId || Number(detail?.prescription?.id) !== prescriptionId) { + throw new Error(detail?.prescription_detail_error || '无法加载处方详情') + } + return detail as OrderDetail +} + +export function useLinkedOrderPrescription( + order: Ref, + visible: Ref, + enabled?: Ref +) { + const options = computed(() => enabled?.value === false ? [] : linkedPrescriptionOptions(order.value)) + const linkedId = ref(0) + const selectedId = computed(() => linkedId.value || Number(order.value?.prescription_id) || 0) + const isPrimary = computed(() => linkedId.value === 0) + const linkedDetail = ref(null) + const loading = ref(false) + const error = ref('') + const prescription = computed(() => isPrimary.value ? order.value?.prescription : linkedDetail.value?.prescription) + const detailError = computed(() => isPrimary.value ? String(order.value?.prescription_detail_error || '').trim() : error.value) + const herbsVisible = computed(() => (isPrimary.value ? order.value : linkedDetail.value)?.prescription_detail_herbs_visible !== false) + let activeRequest: AbortController | undefined + + function reset() { + activeRequest?.abort() + linkedId.value = 0 + linkedDetail.value = null + loading.value = false + error.value = '' + } + + async function select(id: number) { + const orderId = Number(order.value?.id) + const option = options.value.find((entry) => entry.id === id) + if (!visible.value || !orderId || !option) return + + reset() + if (option.is_primary) return + linkedId.value = id + const request = new AbortController() + activeRequest = request + loading.value = true + + try { + const detail = await loadLinkedOrderPrescription(orderId, id, request.signal) + if (!request.signal.aborted) linkedDetail.value = detail + } catch (e) { + if (!request.signal.aborted) error.value = e instanceof Error ? e.message : '无法加载处方详情' + } finally { + if (!request.signal.aborted) loading.value = false + } + } + + watch(visible, (show) => { + if (!show) reset() + }, { flush: 'sync' }) + watch([() => order.value?.id, options], ([orderId, next], [previousOrderId]) => { + if (orderId !== previousOrderId || (linkedId.value && !next.some((entry) => entry.id === linkedId.value && !entry.is_primary))) { + reset() + } + }, { flush: 'sync' }) + onScopeDispose(() => activeRequest?.abort()) + + return { options, selectedId, isPrimary, prescription, loading, detailError, herbsVisible, reset, select } +} diff --git a/admin/src/views/consumer/prescription/components/usePrescriptionOrderPreview.ts b/admin/src/views/consumer/prescription/components/usePrescriptionOrderPreview.ts new file mode 100644 index 000000000..9cc87852c --- /dev/null +++ b/admin/src/views/consumer/prescription/components/usePrescriptionOrderPreview.ts @@ -0,0 +1,105 @@ +import { computed, onScopeDispose, ref, shallowRef, watch } from 'vue' +import { prescriptionDetail } from '@/api/tcm' +import { applyOrderUsageOverride } from '@/components/prescription-slip/utils' +import feedback from '@/utils/feedback' +import { linkedPrescriptionOptions, loadLinkedOrderPrescription, type OrderDetail } from './useLinkedOrderPrescription' + +function mergePrimaryOrderContext(base: OrderDetail, order: OrderDetail) { + const prescription: OrderDetail = applyOrderUsageOverride({ ...base }, order.usage_override) + if (order.medication_days != null && String(order.medication_days).trim() !== '') { + prescription.medication_days = order.medication_days + } + for (const key of ['order_no', 'recipient_name', 'recipient_phone', 'shipping_address']) { + if (order[key] && !prescription[key]) prescription[key] = order[key] + } + if (order.amount != null && prescription.order_amount == null) prescription.order_amount = order.amount + if (Number(order.dose_count) > 0) prescription.dose_count = Number(order.dose_count) + if (order.dose_unit != null && String(order.dose_unit).trim() !== '') { + prescription.dose_unit = String(order.dose_unit) + } + if (!(Number(prescription.appointment_id) > 0) && Number(order.linked_appointment?.id) > 0) { + prescription.appointment_id = Number(order.linked_appointment.id) + } + return prescription +} + +export function usePrescriptionOrderPreview(resolveAuxLibraryName?: (prescription: OrderDetail) => Promise) { + const visible = ref(false) + const loading = ref(false) + const error = ref('') + const prescription = ref(null) + const auxLibraryName = ref('') + const tabType = ref<'internal' | 'user'>('internal') + const order = shallowRef(null) + const selectedId = ref(0) + const isPrimary = computed(() => selectedId.value === Number(order.value?.prescription_id)) + const canExport = ref(false) + const options = computed(() => linkedPrescriptionOptions(order.value)) + let activeRequest: AbortController | undefined + + async function select(id: number) { + const context = order.value + if (!visible.value || !context || !id) return + const primary = id === Number(context.prescription_id) + if (!primary && !options.value.some((entry) => entry.id === id)) return + + activeRequest?.abort() + const request = new AbortController() + activeRequest = request + selectedId.value = id + canExport.value = false + loading.value = true + prescription.value = null + auxLibraryName.value = '' + error.value = '' + + try { + let data: OrderDetail + let exportAllowed = primary + if (primary) { + const response = await prescriptionDetail({ id }, request.signal) + const base = response?.data ?? response + if (Number(base?.id) !== id) throw new Error('无法加载处方详情') + data = mergePrimaryOrderContext(base, context) + } else { + const detail = await loadLinkedOrderPrescription(Number(context.id), id, request.signal) + data = detail.prescription + exportAllowed = detail.prescription_detail_herbs_visible === true + } + if (request.signal.aborted) return + const libraryName = resolveAuxLibraryName ? await resolveAuxLibraryName(data) : '' + if (request.signal.aborted) return + prescription.value = data + auxLibraryName.value = libraryName + canExport.value = exportAllowed + } catch { + if (!request.signal.aborted) error.value = '无法加载处方详情。' + } finally { + if (!request.signal.aborted) loading.value = false + } + } + + function open(context: OrderDetail, initialId?: number) { + const entries = linkedPrescriptionOptions(context) + const id = entries.find((entry) => entry.id === initialId)?.id + || entries.find((entry) => entry.is_primary)?.id + || entries[0]?.id + || Number(context.prescription_id) + if (!id) { + feedback.msgWarning('该订单未关联处方') + return + } + + order.value = { ...context } + tabType.value = 'internal' + visible.value = true + void select(id) + } + + watch(visible, (show) => { + if (!show) activeRequest?.abort() + }, { flush: 'sync' }) + onScopeDispose(() => activeRequest?.abort()) + + return { visible, loading, error, prescription, auxLibraryName, tabType, selectedId, isPrimary, canExport, options, open, select } +} diff --git a/admin/src/views/consumer/prescription/components/usePrescriptionOrderReceipts.ts b/admin/src/views/consumer/prescription/components/usePrescriptionOrderReceipts.ts new file mode 100644 index 000000000..36bd7ece4 --- /dev/null +++ b/admin/src/views/consumer/prescription/components/usePrescriptionOrderReceipts.ts @@ -0,0 +1,107 @@ +import { computed, ref } from 'vue' +import type { Ref } from 'vue' +import { prescriptionOrderAttachReceipt, prescriptionOrderPaidPayOrders, prescriptionOrderUnlinkPayOrder } from '@/api/tcm' +import feedback from '@/utils/feedback' +import { hasPermission } from '@/utils/perm' +import { formatMoney } from './prescription-order-utils' + +type Receipt = { + id: number | string + order_no?: string + amount: number | string +} + +export function usePrescriptionOrderReceipts( + detail: Ref | null>, + visible: Ref, + onChanged: (orderId: number) => void +) { + const unlinkedReceipts = ref([]) + const linkingReceiptId = ref(null) + const unlinkingReceiptId = ref(null) + const changingReceipts = computed(() => linkingReceiptId.value !== null || unlinkingReceiptId.value !== null) + const canAttachReceipt = computed(() => Array.isArray(detail.value?.attachable_receipts)) + const canUnlinkReceipt = computed(() => + Number(detail.value?.id) > 0 && Number(detail.value?.fulfillment_status) !== 4 + && hasPermission(['tcm.prescriptionOrder/unlinkPayOrder']) + ) + const isCurrentOrder = (id: number) => visible.value && Number(detail.value?.id) === id + + async function loadUnlinkedReceipts(order: Record) { + const id = Number(order.id) + const diagnosisId = Number(order.diagnosis_id) + if (!isCurrentOrder(id)) return + if (Array.isArray(order.attachable_receipts) || !diagnosisId) { + unlinkedReceipts.value = order.attachable_receipts ?? [] + return + } + try { + const res = await prescriptionOrderPaidPayOrders({ diagnosis_id: diagnosisId, prescription_order_id: id }) + const rows: Receipt[] = res?.lists ?? res?.data?.lists ?? [] + const linkedIds = new Set((order.pay_order_ids ?? []).map(Number)) + if (isCurrentOrder(id)) { + unlinkedReceipts.value = rows.filter((row) => !linkedIds.has(Number(row.id))) + } + } catch { + if (isCurrentOrder(id)) unlinkedReceipts.value = [] + } + } + + async function confirmAttachReceipt(receipt: Receipt) { + const orderId = Number(detail.value?.id) + const payId = Number(receipt.id) + if (!orderId || !payId || !canAttachReceipt.value || changingReceipts.value) return + linkingReceiptId.value = payId + try { + const orderComplete = Number(detail.value?.fulfillment_status) === 3 + const message = `将收款「${receipt.order_no || '#' + payId}」(¥${formatMoney(receipt.amount)})关联到当前订单?` + await feedback.confirm(orderComplete ? message + '收款会先冲减需代收,超出部分计入总金额。' : message) + if (!isCurrentOrder(orderId)) return + const res = await prescriptionOrderAttachReceipt({ id: orderId, pay_order_id: payId }) + const updated = res?.data ?? res + if (isCurrentOrder(orderId) && Number(updated?.id) === orderId) { + const remaining = unlinkedReceipts.value.filter((row) => Number(row.id) !== payId) + detail.value = { ...detail.value, ...updated, attachable_receipts: remaining } + unlinkedReceipts.value = remaining + } + feedback.msgSuccess('收款已关联') + onChanged(orderId) + } catch { + // 接口错误由请求拦截器提示。取消确认时保留当前列表。 + } finally { + linkingReceiptId.value = null + } + } + + async function confirmUnlinkReceipt(receipt: Receipt & { status: number | string }) { + const orderId = Number(detail.value?.id) + const payId = Number(receipt.id) + if (!payId || !canUnlinkReceipt.value || changingReceipts.value || ![2, 4, 5].includes(Number(receipt.status))) return + unlinkingReceiptId.value = payId + try { + await feedback.confirm( + `确定移除收款记录「${receipt.order_no || '#' + payId}」(¥${formatMoney(receipt.amount)})?` + + '移除后会保留原收款记录,并按订单总金额和剩余收款重算已付金额和需代收。' + ) + if (!isCurrentOrder(orderId)) return + const response = await prescriptionOrderUnlinkPayOrder({ id: orderId, pay_order_id: payId }) + const updated = response?.data ?? response + if (isCurrentOrder(orderId) && Number(updated?.id) === orderId) { + const order = { ...detail.value, ...updated, attachable_receipts: updated.attachable_receipts } + detail.value = order + await loadUnlinkedReceipts(order) + } + feedback.msgSuccess('收款关联已移除,金额已同步更新') + onChanged(orderId) + } catch { + // 请求拦截器负责显示接口报错。 + } finally { + unlinkingReceiptId.value = null + } + } + + return { + unlinkedReceipts, loadUnlinkedReceipts, linkingReceiptId, unlinkingReceiptId, changingReceipts, + canAttachReceipt, canUnlinkReceipt, confirmAttachReceipt, confirmUnlinkReceipt + } +} diff --git a/admin/src/views/consumer/prescription/index.vue b/admin/src/views/consumer/prescription/index.vue index 5c29b477f..c4908cf06 100755 --- a/admin/src/views/consumer/prescription/index.vue +++ b/admin/src/views/consumer/prescription/index.vue @@ -221,11 +221,17 @@ {{ formatListCreateTime(row.create_time) }} - + -