qc_work_order_fee_summary.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. """质检工单 · 用户费用摘要(采集协议 / 基础实验)"""
  2. from __future__ import annotations
  3. from decimal import Decimal
  4. from datetime import datetime
  5. from typing import Any, Dict, List, Optional
  6. from sqlalchemy.orm import Session
  7. from app.models.clinical_payment import BizPilotPayment
  8. from app.models.models import (
  9. BizPilotInfusionSchedule,
  10. BizProtocolPayment,
  11. BizProtocolPaymentDetail,
  12. BizProtocolPaymentSchedule,
  13. BizProtocolPaymentTransfer,
  14. )
  15. _PAY_MODE_LABELS = {"dj": "趸交", "fq": "分期", "djfq": "趸交+分期"}
  16. _PAY_STATUS_LABELS = {
  17. "dfj": "已付款",
  18. "jfqrz": "付款确认中",
  19. "yjf": "付款完成",
  20. "tfqrz": "退费确认中",
  21. "ytf": "已退费",
  22. }
  23. # 出库工单:输注计划缴费(基础实验 / 临床应用共用 biz_pilot_payment)
  24. _OUTBOUND_FEE_BIZ_TYPES = frozenset({"basic_experiment", "clinical_application"})
  25. def _is_outbound_fee_biz_type(biz_type: Optional[str]) -> bool:
  26. return (biz_type or "").strip() in _OUTBOUND_FEE_BIZ_TYPES
  27. def _dec(val: Any) -> Optional[float]:
  28. if val is None:
  29. return None
  30. try:
  31. return float(val)
  32. except (TypeError, ValueError):
  33. return None
  34. def _received_amount_from_details(details: List[BizProtocolPaymentDetail]) -> float:
  35. total = Decimal("0")
  36. for d in details:
  37. st = (d.pay_status or "").strip()
  38. if st in ("yjf", "jfqrz"):
  39. total += d.transfer_amount or Decimal("0")
  40. return float(total)
  41. def _protocol_status_hint(pay_mode: str, statuses: List[str]) -> str:
  42. counts: Dict[str, int] = {}
  43. for st in statuses:
  44. if st:
  45. counts[st] = counts.get(st, 0) + 1
  46. paid = counts.get("yjf", 0)
  47. confirm = counts.get("jfqrz", 0)
  48. pending = counts.get("dfj", 0)
  49. if pay_mode in ("fq", "djfq"):
  50. parts: List[str] = ["分期"]
  51. if paid:
  52. parts.append(f"{paid} 期已缴")
  53. if confirm:
  54. parts.append(f"{confirm} 期待确认")
  55. if pending:
  56. parts.append(f"{pending} 期待缴费")
  57. return " · ".join(parts) if len(parts) > 1 else parts[0]
  58. if confirm:
  59. return f"{confirm} 期待确认" if confirm > 1 else "收款确认中"
  60. if pending:
  61. return "待缴费"
  62. return ""
  63. def _protocol_overall_status(
  64. statuses: List[str], contract: Optional[float], pending: Optional[float]
  65. ) -> Dict[str, str]:
  66. if "tfqrz" in statuses:
  67. return {"overallPayStatus": "tfqrz", "overallPayStatusLabel": "退费确认中", "overallPayStatusTagType": "warning"}
  68. if statuses and all(s == "ytf" for s in statuses):
  69. return {"overallPayStatus": "ytf", "overallPayStatusLabel": "已退费", "overallPayStatusTagType": "info"}
  70. if (
  71. contract is not None
  72. and pending is not None
  73. and pending <= 0
  74. and "dfj" not in statuses
  75. and "jfqrz" not in statuses
  76. ):
  77. return {"overallPayStatus": "cleared", "overallPayStatusLabel": "已缴清", "overallPayStatusTagType": "success"}
  78. if "jfqrz" in statuses:
  79. return {"overallPayStatus": "jfqrz", "overallPayStatusLabel": "收款确认中", "overallPayStatusTagType": "warning"}
  80. if "dfj" in statuses:
  81. return {"overallPayStatus": "dfj", "overallPayStatusLabel": "待缴费", "overallPayStatusTagType": "info"}
  82. return {"overallPayStatus": "", "overallPayStatusLabel": "—", "overallPayStatusTagType": "info"}
  83. def _fmt_ymd(val: Any) -> str:
  84. s = (str(val or "")).strip()
  85. if len(s) == 8 and s.isdigit():
  86. return f"{s[:4]}-{s[4:6]}-{s[6:8]}"
  87. return s
  88. def _fmt_dt(v):
  89. if v is None:
  90. return ""
  91. if isinstance(v, datetime):
  92. return v.strftime("%Y-%m-%d %H:%M:%S")
  93. s = str(v).strip()
  94. return s
  95. def _payment_detail_row(d: BizProtocolPaymentDetail, pay_mode: str, idx: int) -> Dict[str, Any]:
  96. st = (d.pay_status or "").strip()
  97. inst_no = d.installment_no
  98. if pay_mode in ("fq", "djfq") and inst_no is not None:
  99. label = f"第{inst_no}期"
  100. else:
  101. label = f"缴费{idx + 1}"
  102. return {
  103. "installmentNo": inst_no,
  104. "installmentLabel": label,
  105. "installmentDueDate": _fmt_ymd(d.installment_due_date),
  106. "dueAmount": _dec(d.due_amount),
  107. "transferAmount": _dec(d.transfer_amount),
  108. "payStatus": st,
  109. "payStatusLabel": _PAY_STATUS_LABELS.get(st, st or "—"),
  110. "transferTime": _fmt_dt(getattr(d, "transfer_time", None)),
  111. }
  112. def _sort_payment_details(details: List[BizProtocolPaymentDetail]) -> List[BizProtocolPaymentDetail]:
  113. return sorted(
  114. details,
  115. key=lambda d: (
  116. d.installment_no if d.installment_no is not None else 10_000,
  117. d.order_num if d.order_num is not None else 0,
  118. d.create_time or datetime.min,
  119. ),
  120. )
  121. def build_protocol_payment_summary(
  122. pay: Optional[BizProtocolPayment],
  123. details: List[BizProtocolPaymentDetail],
  124. ) -> Optional[Dict[str, Any]]:
  125. if not pay:
  126. return None
  127. pay_mode = (pay.pay_mode or "dj").strip() or "dj"
  128. contract = _dec(pay.contract_amount)
  129. sorted_details = _sort_payment_details(details)
  130. detail_rows = [_payment_detail_row(d, pay_mode, i) for i, d in enumerate(sorted_details)]
  131. received = _received_amount_from_details(sorted_details)
  132. pending = max(contract - received, 0) if contract is not None else None
  133. statuses = [(d.pay_status or "").strip() for d in sorted_details]
  134. overall = _protocol_overall_status(statuses, contract, pending)
  135. hint = _protocol_status_hint(pay_mode, statuses) or overall["overallPayStatusLabel"]
  136. return {
  137. "payMode": pay_mode,
  138. "payModeLabel": _PAY_MODE_LABELS.get(pay_mode, pay_mode),
  139. "contractAmount": contract,
  140. "receivedAmount": received,
  141. "pendingAmount": pending,
  142. "statusHint": hint,
  143. "installmentCount": len(detail_rows) if pay_mode in ("fq", "djfq") else None,
  144. "paymentDetails": detail_rows,
  145. **overall,
  146. }
  147. def _first_period_last_transfer_times(
  148. db: Session,
  149. protocol_ids: List[str],
  150. tenant_no: str,
  151. ) -> Dict[str, str]:
  152. """协议 → 第 1 期(schedule_no=1)多笔转账中 transfer_time 最晚的一笔。"""
  153. ids = [x.strip() for x in protocol_ids if (x or "").strip()]
  154. if not ids:
  155. return {}
  156. rows = (
  157. db.query(
  158. BizProtocolPaymentTransfer.collection_protocol_id,
  159. BizProtocolPaymentTransfer.transfer_time,
  160. )
  161. .join(
  162. BizProtocolPaymentSchedule,
  163. BizProtocolPaymentSchedule.id == BizProtocolPaymentTransfer.schedule_id,
  164. )
  165. .filter(
  166. BizProtocolPaymentTransfer.collection_protocol_id.in_(ids),
  167. BizProtocolPaymentTransfer.tenant_no == tenant_no,
  168. BizProtocolPaymentTransfer.del_flag == 0,
  169. BizProtocolPaymentSchedule.tenant_no == tenant_no,
  170. BizProtocolPaymentSchedule.del_flag == 0,
  171. BizProtocolPaymentSchedule.schedule_no == 1,
  172. BizProtocolPaymentTransfer.transfer_time.isnot(None),
  173. )
  174. .all()
  175. )
  176. latest: Dict[str, datetime] = {}
  177. for pid, t in rows:
  178. if not pid or t is None:
  179. continue
  180. prev = latest.get(pid)
  181. if prev is None or t > prev:
  182. latest[pid] = t
  183. return {pid: _fmt_dt(t) for pid, t in latest.items() if _fmt_dt(t)}
  184. def build_protocol_payment_summaries(
  185. db: Session,
  186. protocol_ids: List[str],
  187. tenant_no: str,
  188. ) -> Dict[str, Dict[str, Any]]:
  189. ids = [x.strip() for x in protocol_ids if (x or "").strip()]
  190. if not ids:
  191. return {}
  192. payments = (
  193. db.query(BizProtocolPayment)
  194. .filter(
  195. BizProtocolPayment.collection_protocol_id.in_(ids),
  196. BizProtocolPayment.tenant_no == tenant_no,
  197. BizProtocolPayment.del_flag == 0,
  198. )
  199. .all()
  200. )
  201. pay_by_proto = {p.collection_protocol_id: p for p in payments}
  202. payment_ids = [p.id for p in payments]
  203. details_by_payment: Dict[str, List[BizProtocolPaymentDetail]] = {}
  204. if payment_ids:
  205. for d in (
  206. db.query(BizProtocolPaymentDetail)
  207. .filter(
  208. BizProtocolPaymentDetail.payment_id.in_(payment_ids),
  209. BizProtocolPaymentDetail.tenant_no == tenant_no,
  210. BizProtocolPaymentDetail.del_flag == 0,
  211. )
  212. .all()
  213. ):
  214. details_by_payment.setdefault(d.payment_id, []).append(d)
  215. first_pay_map = _first_period_last_transfer_times(db, ids, tenant_no)
  216. out: Dict[str, Dict[str, Any]] = {}
  217. for pid in ids:
  218. pay = pay_by_proto.get(pid)
  219. summary = (
  220. build_protocol_payment_summary(pay, details_by_payment.get(pay.id, []))
  221. if pay
  222. else None
  223. )
  224. first_pay = first_pay_map.get(pid) or ""
  225. if not summary and not first_pay:
  226. continue
  227. if not summary:
  228. summary = {
  229. "payMode": "",
  230. "payModeLabel": "",
  231. "contractAmount": None,
  232. "receivedAmount": None,
  233. "pendingAmount": None,
  234. "statusHint": "",
  235. "installmentCount": None,
  236. "paymentDetails": [],
  237. "overallPayStatus": "",
  238. "overallPayStatusLabel": "—",
  239. "overallPayStatusTagType": "info",
  240. }
  241. if first_pay:
  242. summary["firstPayTransferTime"] = first_pay
  243. out[pid] = summary
  244. return out
  245. def build_basic_experiment_fee_summary(
  246. schedule: Optional[BizPilotInfusionSchedule],
  247. payment: Optional[BizPilotPayment],
  248. ) -> Optional[Dict[str, Any]]:
  249. sale = _dec(schedule.amount) if schedule else None
  250. if not schedule and not payment:
  251. return None
  252. pay_status = (payment.pay_status or "").strip() if payment else ""
  253. transfer = None
  254. if payment and pay_status in ("yjf", "jfqrz"):
  255. transfer = _dec(payment.transfer_amount)
  256. return {
  257. "saleAmount": sale,
  258. "paymentAmount": _dec(payment.amount) if payment else None,
  259. "transferAmount": transfer,
  260. "payStatus": pay_status or None,
  261. "payStatusLabel": _PAY_STATUS_LABELS.get(pay_status, "—") if pay_status else None,
  262. }
  263. def build_basic_experiment_fee_summaries(
  264. db: Session,
  265. schedule_ids: List[str],
  266. tenant_no: str,
  267. ) -> Dict[str, Dict[str, Any]]:
  268. ids = [x.strip() for x in schedule_ids if (x or "").strip()]
  269. if not ids:
  270. return {}
  271. schedules = {
  272. s.id: s
  273. for s in db.query(BizPilotInfusionSchedule)
  274. .filter(
  275. BizPilotInfusionSchedule.id.in_(ids),
  276. BizPilotInfusionSchedule.tenant_no == tenant_no,
  277. BizPilotInfusionSchedule.del_flag == 0,
  278. )
  279. .all()
  280. }
  281. payments = {
  282. p.infusion_schedule_id: p
  283. for p in db.query(BizPilotPayment)
  284. .filter(
  285. BizPilotPayment.infusion_schedule_id.in_(ids),
  286. BizPilotPayment.tenant_no == tenant_no,
  287. BizPilotPayment.del_flag == 0,
  288. )
  289. .all()
  290. }
  291. out: Dict[str, Dict[str, Any]] = {}
  292. for sid in ids:
  293. summary = build_basic_experiment_fee_summary(schedules.get(sid), payments.get(sid))
  294. if summary:
  295. out[sid] = summary
  296. return out
  297. def _is_inbound_cell_storage_item(item: Dict[str, Any]) -> bool:
  298. wo_type = (item.get("workOrderType") or "inbound").strip() or "inbound"
  299. if wo_type in ("outbound", "temporary"):
  300. return False
  301. return (item.get("receptionSource") or "cell_storage").strip() in ("", "cell_storage")
  302. def _load_reception_map(db: Session, reception_ids: List[str]) -> Dict[str, Any]:
  303. from app.models.models import BizSampleReception
  304. ids = [x for x in reception_ids if x]
  305. if not ids:
  306. return {}
  307. return {
  308. r.id: r
  309. for r in db.query(BizSampleReception)
  310. .filter(BizSampleReception.id.in_(ids), BizSampleReception.del_flag == 0)
  311. .all()
  312. }
  313. def _load_protocol_id_by_no(db: Session, protocol_nos: List[str], tenant_no: str) -> Dict[str, str]:
  314. from app.models.models import BizCollectionProtocol
  315. nos = [x for x in protocol_nos if x]
  316. if not nos:
  317. return {}
  318. rows = (
  319. db.query(BizCollectionProtocol.protocol_no, BizCollectionProtocol.id)
  320. .filter(
  321. BizCollectionProtocol.protocol_no.in_(nos),
  322. BizCollectionProtocol.tenant_no == tenant_no,
  323. BizCollectionProtocol.del_flag == 0,
  324. )
  325. .all()
  326. )
  327. return {(r.protocol_no or "").strip(): r.id for r in rows if (r.protocol_no or "").strip()}
  328. def _effective_collection_protocol_id(
  329. item: Dict[str, Any],
  330. rec: Optional[Any],
  331. proto_no_map: Dict[str, str],
  332. ) -> str:
  333. pid = (item.get("collectionProtocolId") or "").strip()
  334. if pid:
  335. return pid
  336. if rec is not None:
  337. pid = (getattr(rec, "collection_protocol_id", None) or "").strip()
  338. if pid:
  339. return pid
  340. pno = (item.get("protocolNo") or "").strip()
  341. return proto_no_map.get(pno, "")
  342. def enrich_work_order_fee_summaries(
  343. db: Session,
  344. items: List[Dict[str, Any]],
  345. tenant_no: str,
  346. ) -> List[Dict[str, Any]]:
  347. if not items or not tenant_no:
  348. return items
  349. inbound_items = [x for x in items if _is_inbound_cell_storage_item(x)]
  350. rec_ids = [(x.get("receptionId") or "").strip() for x in inbound_items if (x.get("receptionId") or "").strip()]
  351. rec_map = _load_reception_map(db, rec_ids)
  352. proto_nos = list(
  353. dict.fromkeys(
  354. (x.get("protocolNo") or "").strip()
  355. for x in inbound_items
  356. if (x.get("protocolNo") or "").strip()
  357. )
  358. )
  359. proto_no_map = _load_protocol_id_by_no(db, proto_nos, tenant_no)
  360. protocol_ids: List[str] = []
  361. for item in inbound_items:
  362. rec = rec_map.get((item.get("receptionId") or "").strip())
  363. pid = _effective_collection_protocol_id(item, rec, proto_no_map)
  364. if pid:
  365. protocol_ids.append(pid)
  366. schedule_ids = list(
  367. dict.fromkeys(
  368. (x.get("infusionScheduleId") or "").strip()
  369. for x in items
  370. if (x.get("workOrderType") or "") == "outbound"
  371. and _is_outbound_fee_biz_type(x.get("bizType"))
  372. and (x.get("infusionScheduleId") or "").strip()
  373. )
  374. )
  375. payment_map = build_protocol_payment_summaries(db, list(dict.fromkeys(protocol_ids)), tenant_no)
  376. fee_map = build_basic_experiment_fee_summaries(db, schedule_ids, tenant_no)
  377. for item in items:
  378. wo_type = item.get("workOrderType") or "inbound"
  379. if wo_type == "outbound":
  380. item["paymentSummary"] = None
  381. if _is_outbound_fee_biz_type(item.get("bizType")):
  382. sid = (item.get("infusionScheduleId") or "").strip()
  383. item["feeSummary"] = fee_map.get(sid) if sid else None
  384. else:
  385. item["feeSummary"] = None
  386. elif wo_type == "temporary":
  387. item["paymentSummary"] = None
  388. item["feeSummary"] = None
  389. else:
  390. item["feeSummary"] = None
  391. if _is_inbound_cell_storage_item(item):
  392. rec = rec_map.get((item.get("receptionId") or "").strip())
  393. pid = _effective_collection_protocol_id(item, rec, proto_no_map)
  394. item["paymentSummary"] = payment_map.get(pid) if pid else None
  395. else:
  396. item["paymentSummary"] = None
  397. return items