|
@@ -949,11 +949,13 @@ def link_samples(
|
|
|
items: List[Dict[str, Any]],
|
|
items: List[Dict[str, Any]],
|
|
|
commit: bool = True,
|
|
commit: bool = True,
|
|
|
user_info: Optional[dict] = None,
|
|
user_info: Optional[dict] = None,
|
|
|
|
|
+ allow_settled_relink: bool = False,
|
|
|
) -> Dict[str, Any]:
|
|
) -> Dict[str, Any]:
|
|
|
header = get_freight(db, freight_id, tenant_no, user_info)
|
|
header = get_freight(db, freight_id, tenant_no, user_info)
|
|
|
if not header:
|
|
if not header:
|
|
|
raise ValueError("运费批次不存在")
|
|
raise ValueError("运费批次不存在")
|
|
|
- assert_freight_mutable(header.settle_status, "items")
|
|
|
|
|
|
|
+ if not allow_settled_relink:
|
|
|
|
|
+ assert_freight_mutable(header.settle_status, "items")
|
|
|
if not items:
|
|
if not items:
|
|
|
raise ValueError("请选择要关联的样本")
|
|
raise ValueError("请选择要关联的样本")
|
|
|
if user_info is not None:
|
|
if user_info is not None:
|
|
@@ -1156,6 +1158,16 @@ def freight_map_for_samples(
|
|
|
return out
|
|
return out
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+def _input_freight_id(raw: Any) -> str:
|
|
|
|
|
+ if not isinstance(raw, dict):
|
|
|
|
|
+ return ""
|
|
|
|
|
+ nested = raw.get("freight") if isinstance(raw.get("freight"), dict) else {}
|
|
|
|
|
+ return (
|
|
|
|
|
+ str(raw.get("freightId") or raw.get("freight_id") or "").strip()
|
|
|
|
|
+ or str(nested.get("id") or "").strip()
|
|
|
|
|
+ )
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
def apply_freight_on_protocol_save(
|
|
def apply_freight_on_protocol_save(
|
|
|
db: Session,
|
|
db: Session,
|
|
|
*,
|
|
*,
|
|
@@ -1190,6 +1202,34 @@ def apply_freight_on_protocol_save(
|
|
|
)
|
|
)
|
|
|
now = _now()
|
|
now = _now()
|
|
|
affected_freight_ids = {item.freight_id for item in stale_items}
|
|
affected_freight_ids = {item.freight_id for item in stale_items}
|
|
|
|
|
+ stale_by_freight: Dict[str, List[BizSampleFreightItem]] = {}
|
|
|
|
|
+ for item in stale_items:
|
|
|
|
|
+ stale_by_freight.setdefault(item.freight_id, []).append(item)
|
|
|
|
|
+ settled_relink_ids = set()
|
|
|
|
|
+ if stale_by_freight:
|
|
|
|
|
+ headers = (
|
|
|
|
|
+ db.query(BizSampleFreight)
|
|
|
|
|
+ .filter(
|
|
|
|
|
+ BizSampleFreight.id.in_(list(stale_by_freight.keys())),
|
|
|
|
|
+ BizSampleFreight.tenant_no == tenant_no,
|
|
|
|
|
+ BizSampleFreight.del_flag == 0,
|
|
|
|
|
+ )
|
|
|
|
|
+ .all()
|
|
|
|
|
+ )
|
|
|
|
|
+ header_by_id = {row.id: row for row in headers}
|
|
|
|
|
+ for freight_id, grouped in stale_by_freight.items():
|
|
|
|
|
+ header = header_by_id.get(freight_id)
|
|
|
|
|
+ if header is None or (header.settle_status or "") != "yjs":
|
|
|
|
|
+ continue
|
|
|
|
|
+ stale_count = len(grouped)
|
|
|
|
|
+ keep_count = sum(
|
|
|
|
|
+ 1
|
|
|
|
|
+ for raw in (input_samples or [])
|
|
|
|
|
+ if _input_freight_id(raw) == freight_id
|
|
|
|
|
+ )
|
|
|
|
|
+ if keep_count != stale_count:
|
|
|
|
|
+ raise ValueError("已结算的运费不能调整样本")
|
|
|
|
|
+ settled_relink_ids.add(freight_id)
|
|
|
for stale in stale_items:
|
|
for stale in stale_items:
|
|
|
stale.del_flag = 1
|
|
stale.del_flag = 1
|
|
|
stale.update_by = user_id
|
|
stale.update_by = user_id
|
|
@@ -1218,10 +1258,7 @@ def apply_freight_on_protocol_save(
|
|
|
"protocolId": protocol_id,
|
|
"protocolId": protocol_id,
|
|
|
"sampleCode": sample_code,
|
|
"sampleCode": sample_code,
|
|
|
}
|
|
}
|
|
|
- freight_id = (
|
|
|
|
|
- str(raw.get("freightId") or raw.get("freight_id") or "").strip()
|
|
|
|
|
- or str((raw.get("freight") or {}).get("id") or "").strip()
|
|
|
|
|
- )
|
|
|
|
|
|
|
+ freight_id = _input_freight_id(raw)
|
|
|
draft = raw.get("freightDraft")
|
|
draft = raw.get("freightDraft")
|
|
|
if draft is None:
|
|
if draft is None:
|
|
|
draft = raw.get("freight_draft")
|
|
draft = raw.get("freight_draft")
|
|
@@ -1233,6 +1270,7 @@ def apply_freight_on_protocol_save(
|
|
|
user_id=user_id,
|
|
user_id=user_id,
|
|
|
items=[item],
|
|
items=[item],
|
|
|
commit=False,
|
|
commit=False,
|
|
|
|
|
+ allow_settled_relink=freight_id in settled_relink_ids,
|
|
|
)
|
|
)
|
|
|
continue
|
|
continue
|
|
|
if isinstance(draft, dict):
|
|
if isinstance(draft, dict):
|
|
@@ -1281,6 +1319,7 @@ def apply_freight_on_protocol_save(
|
|
|
.first()
|
|
.first()
|
|
|
)
|
|
)
|
|
|
if freight is not None:
|
|
if freight is not None:
|
|
|
|
|
+ assert_freight_mutable(freight.settle_status, "delete")
|
|
|
freight.del_flag = 1
|
|
freight.del_flag = 1
|
|
|
freight.update_by = user_id
|
|
freight.update_by = user_id
|
|
|
freight.update_time = now
|
|
freight.update_time = now
|