|
@@ -43,7 +43,11 @@ from app.services.market_field_scope import (
|
|
|
apply_donation_scope,
|
|
apply_donation_scope,
|
|
|
)
|
|
)
|
|
|
from app.services.sample_transport_mode import DICT_TYPE, resolve_transport_mode_value
|
|
from app.services.sample_transport_mode import DICT_TYPE, resolve_transport_mode_value
|
|
|
-from app.services.sf_express_settlement import freight_amount_label
|
|
|
|
|
|
|
+from app.services.sf_express_settlement import (
|
|
|
|
|
+ assert_freight_mutable,
|
|
|
|
|
+ freight_amount_label,
|
|
|
|
|
+ resolve_header_for_save,
|
|
|
|
|
+)
|
|
|
from app.services.web_auth_roles import (
|
|
from app.services.web_auth_roles import (
|
|
|
_map_role_name_to_web,
|
|
_map_role_name_to_web,
|
|
|
resolve_web_role_for_user,
|
|
resolve_web_role_for_user,
|
|
@@ -1056,7 +1060,7 @@ def _next_freight_no(db: Session, tenant_no: str, now: datetime, offset: int = 0
|
|
|
return f'{prefix}{seq:03d}'
|
|
return f'{prefix}{seq:03d}'
|
|
|
|
|
|
|
|
|
|
|
|
|
-def create_freight(db: Session, *, ctx: MarketFieldContext, user_info: dict, transport_mode: str, freight_amount: Any, items: List[Dict[str, Any]]):
|
|
|
|
|
|
|
+def create_freight(db: Session, *, ctx: MarketFieldContext, user_info: dict, transport_mode: str, freight_amount: Any, items: List[Dict[str, Any]], waybill_no: Any = None):
|
|
|
keys = _normalize_items(items)
|
|
keys = _normalize_items(items)
|
|
|
try:
|
|
try:
|
|
|
samples = _lock_sample_graph(db, keys, ctx, user_info)
|
|
samples = _lock_sample_graph(db, keys, ctx, user_info)
|
|
@@ -1066,11 +1070,20 @@ def create_freight(db: Session, *, ctx: MarketFieldContext, user_info: dict, tra
|
|
|
if conflicts:
|
|
if conflicts:
|
|
|
raise ValueError('样本已关联其他运费批次')
|
|
raise ValueError('样本已关联其他运费批次')
|
|
|
mode = resolve_transport_mode_value(db, tenant_no=ctx.tenant_no, raw=transport_mode, user_id=ctx.user_id)
|
|
mode = resolve_transport_mode_value(db, tenant_no=ctx.tenant_no, raw=transport_mode, user_id=ctx.user_id)
|
|
|
- amount = _amount(freight_amount)
|
|
|
|
|
|
|
+ plan = resolve_header_for_save(
|
|
|
|
|
+ transport_mode=mode,
|
|
|
|
|
+ existing_waybill=None,
|
|
|
|
|
+ existing_amount=None,
|
|
|
|
|
+ existing_settle_status=None,
|
|
|
|
|
+ waybill_supplied=True,
|
|
|
|
|
+ waybill_no=waybill_no,
|
|
|
|
|
+ amount_supplied=True,
|
|
|
|
|
+ freight_amount=freight_amount,
|
|
|
|
|
+ )
|
|
|
now = datetime.now()
|
|
now = datetime.now()
|
|
|
obj = None
|
|
obj = None
|
|
|
for attempt in range(3):
|
|
for attempt in range(3):
|
|
|
- candidate = BizSampleFreight(id=generate_uuid(), freight_no=_next_freight_no(db, ctx.tenant_no, now, attempt), transport_mode=mode, freight_amount=amount, tenant_no=ctx.tenant_no, order_num=0, remark=None, create_by=ctx.user_id, create_dept_id=str(user_info.get('dept_id') or user_info.get('region_id') or ''), create_time=now, update_by=ctx.user_id, update_time=now, del_flag=0)
|
|
|
|
|
|
|
+ candidate = BizSampleFreight(id=generate_uuid(), freight_no=_next_freight_no(db, ctx.tenant_no, now, attempt), transport_mode=plan.transport_mode, freight_amount=plan.freight_amount, waybill_no=plan.waybill_no, settle_status=plan.settle_status, settle_time=None, tenant_no=ctx.tenant_no, order_num=0, remark=None, create_by=ctx.user_id, create_dept_id=str(user_info.get('dept_id') or user_info.get('region_id') or ''), create_time=now, update_by=ctx.user_id, update_time=now, del_flag=0)
|
|
|
try:
|
|
try:
|
|
|
with db.begin_nested():
|
|
with db.begin_nested():
|
|
|
db.add(candidate)
|
|
db.add(candidate)
|
|
@@ -1095,7 +1108,7 @@ def create_freight(db: Session, *, ctx: MarketFieldContext, user_info: dict, tra
|
|
|
raise
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
-def update_freight(db: Session, freight_id: str, *, ctx: MarketFieldContext, user_info: dict, transport_mode: Optional[str] = None, freight_amount: Any = None):
|
|
|
|
|
|
|
+def update_freight(db: Session, freight_id: str, *, ctx: MarketFieldContext, user_info: dict, transport_mode: Optional[str] = None, freight_amount: Any = None, waybill_no: Any = None, waybill_supplied: bool = False, amount_supplied: bool = False):
|
|
|
try:
|
|
try:
|
|
|
obj = _lock_batch_header(db, freight_id, ctx)
|
|
obj = _lock_batch_header(db, freight_id, ctx)
|
|
|
expected_keys = _discover_batch_keys(db, freight_id, ctx)
|
|
expected_keys = _discover_batch_keys(db, freight_id, ctx)
|
|
@@ -1112,10 +1125,25 @@ def update_freight(db: Session, freight_id: str, *, ctx: MarketFieldContext, use
|
|
|
(row.sample_source, row.sample_id) not in visible for row in active
|
|
(row.sample_source, row.sample_id) not in visible for row in active
|
|
|
):
|
|
):
|
|
|
raise PermissionError('仅当批次全部有效样本均在当前范围内时可修改批次信息')
|
|
raise PermissionError('仅当批次全部有效样本均在当前范围内时可修改批次信息')
|
|
|
- if transport_mode not in (None, ''):
|
|
|
|
|
- obj.transport_mode = resolve_transport_mode_value(db, tenant_no=ctx.tenant_no, raw=str(transport_mode), user_id=ctx.user_id)
|
|
|
|
|
- if freight_amount not in (None, ''):
|
|
|
|
|
- obj.freight_amount = _amount(freight_amount)
|
|
|
|
|
|
|
+ mode = obj.transport_mode or ''
|
|
|
|
|
+ if transport_mode is not None and str(transport_mode).strip() != '':
|
|
|
|
|
+ mode = resolve_transport_mode_value(db, tenant_no=ctx.tenant_no, raw=str(transport_mode), user_id=ctx.user_id)
|
|
|
|
|
+ plan = resolve_header_for_save(
|
|
|
|
|
+ transport_mode=mode,
|
|
|
|
|
+ existing_waybill=obj.waybill_no,
|
|
|
|
|
+ existing_amount=obj.freight_amount,
|
|
|
|
|
+ existing_settle_status=obj.settle_status,
|
|
|
|
|
+ waybill_supplied=waybill_supplied,
|
|
|
|
|
+ waybill_no=waybill_no,
|
|
|
|
|
+ amount_supplied=amount_supplied,
|
|
|
|
|
+ freight_amount=freight_amount,
|
|
|
|
|
+ )
|
|
|
|
|
+ obj.transport_mode = plan.transport_mode
|
|
|
|
|
+ obj.waybill_no = plan.waybill_no
|
|
|
|
|
+ obj.freight_amount = plan.freight_amount
|
|
|
|
|
+ obj.settle_status = plan.settle_status
|
|
|
|
|
+ if plan.clear_settle_time:
|
|
|
|
|
+ obj.settle_time = None
|
|
|
obj.update_by, obj.update_time = ctx.user_id, datetime.now()
|
|
obj.update_by, obj.update_time = ctx.user_id, datetime.now()
|
|
|
db.flush()
|
|
db.flush()
|
|
|
db.commit()
|
|
db.commit()
|
|
@@ -1145,6 +1173,7 @@ def link_samples(db: Session, freight_id: str, *, ctx: MarketFieldContext, user_
|
|
|
if not _freight_accessible(db, freight_id, ctx=ctx, user_info=user_info):
|
|
if not _freight_accessible(db, freight_id, ctx=ctx, user_info=user_info):
|
|
|
raise HTTPException(status_code=404, detail='数据不存在或无权访问')
|
|
raise HTTPException(status_code=404, detail='数据不存在或无权访问')
|
|
|
obj = _lock_batch_header(db, freight_id, ctx)
|
|
obj = _lock_batch_header(db, freight_id, ctx)
|
|
|
|
|
+ assert_freight_mutable(obj.settle_status, 'items')
|
|
|
expected_keys = _discover_batch_keys(db, freight_id, ctx)
|
|
expected_keys = _discover_batch_keys(db, freight_id, ctx)
|
|
|
samples = _lock_sample_graph(
|
|
samples = _lock_sample_graph(
|
|
|
db, sorted(set(expected_keys + keys)), ctx, user_info
|
|
db, sorted(set(expected_keys + keys)), ctx, user_info
|
|
@@ -1185,6 +1214,7 @@ def unlink_samples(db: Session, freight_id: str, *, ctx: MarketFieldContext, use
|
|
|
keys = _normalize_items(items)
|
|
keys = _normalize_items(items)
|
|
|
try:
|
|
try:
|
|
|
obj = _lock_batch_header(db, freight_id, ctx)
|
|
obj = _lock_batch_header(db, freight_id, ctx)
|
|
|
|
|
+ assert_freight_mutable(obj.settle_status, 'items')
|
|
|
expected_keys = _discover_batch_keys(db, freight_id, ctx)
|
|
expected_keys = _discover_batch_keys(db, freight_id, ctx)
|
|
|
visible = _lock_sample_graph(db, expected_keys, ctx, user_info)
|
|
visible = _lock_sample_graph(db, expected_keys, ctx, user_info)
|
|
|
active = _lock_active_items(
|
|
active = _lock_active_items(
|
|
@@ -1233,6 +1263,7 @@ def delete_freight(db: Session, freight_id: str, *, ctx: MarketFieldContext, use
|
|
|
"""级联软删运费单;仅当全部有效关联样本均在当前范围内时允许。"""
|
|
"""级联软删运费单;仅当全部有效关联样本均在当前范围内时允许。"""
|
|
|
try:
|
|
try:
|
|
|
obj = _lock_batch_header(db, freight_id, ctx)
|
|
obj = _lock_batch_header(db, freight_id, ctx)
|
|
|
|
|
+ assert_freight_mutable(obj.settle_status, 'delete')
|
|
|
expected_keys = _discover_batch_keys(db, freight_id, ctx)
|
|
expected_keys = _discover_batch_keys(db, freight_id, ctx)
|
|
|
visible = _lock_sample_graph(db, expected_keys, ctx, user_info)
|
|
visible = _lock_sample_graph(db, expected_keys, ctx, user_info)
|
|
|
active = _lock_active_items(
|
|
active = _lock_active_items(
|