feat(sample): 展示样本状态并支持样本废弃与作废原因
生产/质检入库卡片展示样本状态并支持【样本废弃】;市场 Web 列表与移动市场部卡片补充样本状态及问号查看作废原因;样本表新增 void_desc,人工作废不联动接收但全部作废时自动作废协议(#131)。 Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
7751cc0f4a
commit
0b4c8dfda8
53 files changed
+1527
-82
No files matched your search
@@ -0,0 +1,211 @@
|
||||
"""人工样本废弃:写状态/原因,全作废则协议作废,不联动接收。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import create_engine
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
from app.core.database import Base
|
||||
from app.models.models import (
|
||||
BizCollectionProtocol,
|
||||
BizProtocolSample,
|
||||
BizProtocolStatusChange,
|
||||
BizSampleReception,
|
||||
SysDictData,
|
||||
)
|
||||
from app.services.protocol_reception_discard_sync import (
|
||||
LAB_VOID_TYPE_CODE,
|
||||
SAMPLE_VOID_STATUS,
|
||||
void_sample_manually,
|
||||
)
|
||||
|
||||
|
||||
def _session():
|
||||
engine = create_engine("sqlite://")
|
||||
Base.metadata.create_all(
|
||||
bind=engine,
|
||||
tables=[
|
||||
SysDictData.__table__,
|
||||
BizCollectionProtocol.__table__,
|
||||
BizProtocolSample.__table__,
|
||||
BizProtocolStatusChange.__table__,
|
||||
BizSampleReception.__table__,
|
||||
],
|
||||
)
|
||||
return sessionmaker(bind=engine)()
|
||||
|
||||
|
||||
def _now():
|
||||
return datetime(2026, 11, 19, 12, 0, 0)
|
||||
|
||||
|
||||
class TestBusinessSampleVoid(unittest.TestCase):
|
||||
def test_void_one_sample_keeps_protocol_and_skips_reception(self):
|
||||
db = _session()
|
||||
now = _now()
|
||||
db.add(
|
||||
BizCollectionProtocol(
|
||||
id="p1",
|
||||
protocol_no="CP-1",
|
||||
protocol_status="sxz",
|
||||
tenant_no="tenant-a",
|
||||
create_dept_id="",
|
||||
create_time=now,
|
||||
update_time=now,
|
||||
del_flag=0,
|
||||
)
|
||||
)
|
||||
db.add(
|
||||
BizProtocolSample(
|
||||
id="sa",
|
||||
collection_protocol_id="p1",
|
||||
sample_code="A001",
|
||||
sample_status="ycj",
|
||||
tenant_no="tenant-a",
|
||||
create_dept_id="",
|
||||
create_time=now,
|
||||
update_time=now,
|
||||
del_flag=0,
|
||||
)
|
||||
)
|
||||
db.add(
|
||||
BizProtocolSample(
|
||||
id="sb",
|
||||
collection_protocol_id="p1",
|
||||
sample_code="B001",
|
||||
sample_status="ycj",
|
||||
tenant_no="tenant-a",
|
||||
create_dept_id="",
|
||||
create_time=now,
|
||||
update_time=now,
|
||||
del_flag=0,
|
||||
)
|
||||
)
|
||||
db.add(
|
||||
BizSampleReception(
|
||||
id="ra",
|
||||
protocol_sample_id="sa",
|
||||
collection_protocol_id="p1",
|
||||
reception_time=now,
|
||||
accept_conclusion="js",
|
||||
reception_source="cell_storage",
|
||||
sample_code="A001",
|
||||
tenant_no="tenant-a",
|
||||
create_dept_id="",
|
||||
create_time=now,
|
||||
update_time=now,
|
||||
del_flag=0,
|
||||
check_packaging=0,
|
||||
check_sealing=0,
|
||||
check_no_damage=0,
|
||||
check_no_leakage=0,
|
||||
check_color=0,
|
||||
check_tying_rope=0,
|
||||
check_no_stratification=0,
|
||||
)
|
||||
)
|
||||
db.commit()
|
||||
|
||||
void_sample_manually(
|
||||
db,
|
||||
source="cell_storage",
|
||||
tenant_no="tenant-a",
|
||||
void_desc="质检发现污染",
|
||||
user_id="u1",
|
||||
sample_id="sa",
|
||||
)
|
||||
|
||||
a = db.query(BizProtocolSample).filter(BizProtocolSample.id == "sa").one()
|
||||
b = db.query(BizProtocolSample).filter(BizProtocolSample.id == "sb").one()
|
||||
proto = db.query(BizCollectionProtocol).filter(BizCollectionProtocol.id == "p1").one()
|
||||
rec = db.query(BizSampleReception).filter(BizSampleReception.id == "ra").one()
|
||||
self.assertEqual(a.sample_status, SAMPLE_VOID_STATUS)
|
||||
self.assertEqual(a.void_desc, "质检发现污染")
|
||||
self.assertTrue((a.void_date or "").strip())
|
||||
self.assertEqual(b.sample_status, "ycj")
|
||||
self.assertEqual(proto.protocol_status, "sxz")
|
||||
self.assertEqual(rec.accept_conclusion, "js")
|
||||
|
||||
def test_void_all_samples_auto_voids_protocol_without_reception_change(self):
|
||||
db = _session()
|
||||
now = _now()
|
||||
db.add(
|
||||
BizCollectionProtocol(
|
||||
id="p2",
|
||||
protocol_no="CP-2",
|
||||
protocol_status="sxz",
|
||||
tenant_no="tenant-a",
|
||||
create_dept_id="",
|
||||
create_time=now,
|
||||
update_time=now,
|
||||
del_flag=0,
|
||||
)
|
||||
)
|
||||
for sid, code in (("s1", "C001"), ("s2", "C002")):
|
||||
db.add(
|
||||
BizProtocolSample(
|
||||
id=sid,
|
||||
collection_protocol_id="p2",
|
||||
sample_code=code,
|
||||
sample_status="ycj",
|
||||
tenant_no="tenant-a",
|
||||
create_dept_id="",
|
||||
create_time=now,
|
||||
update_time=now,
|
||||
del_flag=0,
|
||||
)
|
||||
)
|
||||
db.add(
|
||||
BizSampleReception(
|
||||
id="r1",
|
||||
protocol_sample_id="s1",
|
||||
collection_protocol_id="p2",
|
||||
reception_time=now,
|
||||
accept_conclusion="js",
|
||||
reception_source="cell_storage",
|
||||
sample_code="C001",
|
||||
tenant_no="tenant-a",
|
||||
create_dept_id="",
|
||||
create_time=now,
|
||||
update_time=now,
|
||||
del_flag=0,
|
||||
check_packaging=0,
|
||||
check_sealing=0,
|
||||
check_no_damage=0,
|
||||
check_no_leakage=0,
|
||||
check_color=0,
|
||||
check_tying_rope=0,
|
||||
check_no_stratification=0,
|
||||
)
|
||||
)
|
||||
db.commit()
|
||||
|
||||
void_sample_manually(
|
||||
db,
|
||||
source="cell_storage",
|
||||
tenant_no="tenant-a",
|
||||
void_desc="原因1",
|
||||
sample_id="s1",
|
||||
commit=True,
|
||||
)
|
||||
void_sample_manually(
|
||||
db,
|
||||
source="cell_storage",
|
||||
tenant_no="tenant-a",
|
||||
void_desc="原因2",
|
||||
sample_id="s2",
|
||||
commit=True,
|
||||
)
|
||||
|
||||
proto = db.query(BizCollectionProtocol).filter(BizCollectionProtocol.id == "p2").one()
|
||||
rec = db.query(BizSampleReception).filter(BizSampleReception.id == "r1").one()
|
||||
self.assertEqual(proto.protocol_status, "yzf")
|
||||
self.assertEqual(proto.void_type, LAB_VOID_TYPE_CODE)
|
||||
self.assertEqual(rec.accept_conclusion, "js")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -99,12 +99,20 @@ def test_specialist_sees_shared_batch_but_only_own_items_and_count():
|
||||
from app.services.market_freight import get_freight_detail, list_freights
|
||||
db = _session()
|
||||
own, own_parent = _sample(db, 'own')
|
||||
own.sample_status = 'yzf'
|
||||
own.void_desc = '冷链中断'
|
||||
db.commit()
|
||||
other, other_parent = _sample(db, 'other', sales='u2')
|
||||
freight = _freight(db, 'mixed', [('cell_storage', own, own_parent), ('cell_storage', other, other_parent)])
|
||||
rows, total = list_freights(db, ctx=_ctx(), user_info=_user(), include_items=True)
|
||||
detail = get_freight_detail(db, freight.id, ctx=_ctx(), user_info=_user())
|
||||
assert total == 1 and rows[0]['visibleItemCount'] == 1
|
||||
assert [item['sampleCode'] for item in rows[0]['items']] == ['C-own']
|
||||
assert rows[0]['items'][0]['sampleStatus'] == 'yzf'
|
||||
assert rows[0]['items'][0]['sampleStatusLabel'] == '已作废'
|
||||
assert rows[0]['items'][0]['voidDesc'] == '冷链中断'
|
||||
assert detail['items'][0]['sampleStatus'] == 'yzf'
|
||||
assert detail['items'][0]['voidDesc'] == '冷链中断'
|
||||
assert [item['sampleCode'] for item in detail['items']] == ['C-own']
|
||||
assert rows[0]['createBy'] == detail['createBy'] == 'u1'
|
||||
assert rows[0]['createByName'] == detail['createByName'] == '专员'
|
||||
|
||||
@@ -674,6 +674,33 @@ def test_chase_filters_counts_orders_and_pages_in_sql_before_materialization():
|
||||
)
|
||||
|
||||
|
||||
def test_payment_and_chase_lists_include_sample_status_when_coded():
|
||||
from app.services.market_payment import list_payment_chase, list_payments
|
||||
|
||||
db = _session()
|
||||
today = date(2026, 10, 11)
|
||||
_payment_tree(db, 'void-sample', due_date=today.strftime('%Y%m%d'))
|
||||
sample = db.get(BizProtocolSample, 'sample-void-sample')
|
||||
sample.sample_status = 'yzf'
|
||||
sample.void_desc = '客户放弃'
|
||||
db.commit()
|
||||
|
||||
payments, payment_total = list_payments(db, ctx=_ctx(), user_info=_user())
|
||||
chase, chase_total = list_payment_chase(
|
||||
db, ctx=_ctx(), user_info=_user(), today=today
|
||||
)
|
||||
|
||||
assert payment_total == 1
|
||||
assert payments[0]['sampleCode'] == 'S-void-sample'
|
||||
assert payments[0]['sampleStatus'] == 'yzf'
|
||||
assert payments[0]['sampleStatusLabel'] == '已作废'
|
||||
assert payments[0]['voidDesc'] == '客户放弃'
|
||||
assert chase_total == 1
|
||||
assert chase[0]['sampleStatus'] == 'yzf'
|
||||
assert chase[0]['sampleStatusLabel'] == '已作废'
|
||||
assert chase[0]['voidDesc'] == '客户放弃'
|
||||
|
||||
|
||||
def test_payment_list_includes_legacy_models_and_uses_parent_scope_not_transfer_department():
|
||||
from app.services.market_payment import list_payments
|
||||
|
||||
|
||||
@@ -3604,6 +3604,10 @@ def test_protocol_list_contains_mobile_card_fields():
|
||||
assert items[0]['deliveryMode'] == 'sc'
|
||||
assert items[0]['newbornWeightGram'] == 3200
|
||||
assert items[0]['salesUserName'] == '当前用户'
|
||||
assert items[0]['sampleStatus'] == ''
|
||||
assert items[0]['sampleStatusLabel'] == ''
|
||||
assert items[0]['sampleVoidDesc'] == ''
|
||||
assert items[0]['voidDesc'] == ''
|
||||
|
||||
by_customer, customer_total = list_protocols(
|
||||
db, ctx=_ctx('specialist'), user_info=_user(), keyword='李安然'
|
||||
@@ -3626,6 +3630,54 @@ def test_protocol_list_contains_mobile_card_fields():
|
||||
} == {row.id}
|
||||
|
||||
|
||||
def test_protocol_list_returns_sample_status_without_replacing_donation_void_desc():
|
||||
from app.services.market_protocol import list_protocols
|
||||
|
||||
db = _session()
|
||||
cell = _cell(db, 'status-cell')
|
||||
db.add(
|
||||
BizProtocolSample(
|
||||
id='sample-status-cell',
|
||||
collection_protocol_id=cell.id,
|
||||
sample_code='S-CELL',
|
||||
sample_status='yzf',
|
||||
void_desc='试管破裂',
|
||||
tenant_no='tenant-a',
|
||||
create_dept_id='',
|
||||
del_flag=0,
|
||||
)
|
||||
)
|
||||
donation = _donation(db, 'status-don')
|
||||
donation.void_desc = '协议作废说明'
|
||||
db.add(
|
||||
BizResearchDonationSample(
|
||||
id='sample-status-don',
|
||||
research_donation_id=donation.id,
|
||||
sample_code='S-DON',
|
||||
sample_status='yrk',
|
||||
void_desc='样本溶血',
|
||||
tenant_no='tenant-a',
|
||||
create_dept_id='',
|
||||
del_flag=0,
|
||||
)
|
||||
)
|
||||
db.commit()
|
||||
|
||||
items, total = list_protocols(db, ctx=_ctx('specialist'), user_info=_user())
|
||||
|
||||
assert total == 2
|
||||
by_id = {item['id']: item for item in items}
|
||||
assert by_id['status-cell']['sampleCode'] == 'S-CELL'
|
||||
assert by_id['status-cell']['sampleStatus'] == 'yzf'
|
||||
assert by_id['status-cell']['sampleStatusLabel'] == '已作废'
|
||||
assert by_id['status-cell']['voidDesc'] == '试管破裂'
|
||||
assert by_id['status-cell']['sampleVoidDesc'] == '试管破裂'
|
||||
assert by_id['status-don']['sampleStatus'] == 'yrk'
|
||||
assert by_id['status-don']['sampleStatusLabel'] == '已入库'
|
||||
assert by_id['status-don']['voidDesc'] == '协议作废说明'
|
||||
assert by_id['status-don']['sampleVoidDesc'] == '样本溶血'
|
||||
|
||||
|
||||
@pytest.mark.parametrize('source', ['cell_storage', 'research_donation'])
|
||||
def test_protocol_list_and_detail_resolve_tenant_void_type_label(source):
|
||||
from app.services.market_protocol import get_protocol, list_protocols
|
||||
|
||||
Reference in new issue
Block a user