From d67965aae2a3f381db2495d3a33dc445b41ae540 Mon Sep 17 00:00:00 2001 From: zlwu <704541015@qq.com> Date: Wed, 21 Oct 2026 18:10:46 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=A0=B7=E6=9C=AC=E4=B8=8A=E7=9A=84?= =?UTF-8?q?=E7=A9=BA=E8=BF=90=E8=B4=B9=E6=8C=89=E7=A9=BA=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=EF=BC=8C=E9=81=BF=E5=85=8D=E9=A1=BA=E4=B8=B0=E6=9C=88=E7=BB=93?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=E6=88=90=200?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Cursor --- backend/app/crud/sample_freight.py | 25 ++++++++++++++++----- backend/tests/test_sf_express_settlement.py | 15 +++++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/backend/app/crud/sample_freight.py b/backend/app/crud/sample_freight.py index c64e7035..fad0041d 100644 --- a/backend/app/crud/sample_freight.py +++ b/backend/app/crud/sample_freight.py @@ -1037,6 +1037,21 @@ def delete_freight( } +def freight_amount_payload( + amount: Any, transport_mode: Optional[str], money_as_string: bool +) -> Dict[str, Any]: + if amount is None: + freight_amount: Any = None + elif money_as_string: + freight_amount = format(Decimal(str(amount)).quantize(Decimal("0.01")), "f") + else: + freight_amount = float(amount) + return { + "freightAmount": freight_amount, + "freightAmountLabel": freight_amount_label(transport_mode, amount), + } + + def freight_map_for_samples( db: Session, *, @@ -1089,13 +1104,11 @@ def freight_map_for_samples( "transportModeLabel": labels.get( freight.transport_mode or "", freight.transport_mode or "" ), - "freightAmount": ( - format(Decimal(str(freight.freight_amount)).quantize(Decimal('0.01')), 'f') - if money_as_string and freight.freight_amount is not None - else float(freight.freight_amount) - if freight.freight_amount is not None - else '0.00' if money_as_string else 0.0 + **freight_amount_payload( + freight.freight_amount, freight.transport_mode, money_as_string ), + "waybillNo": getattr(freight, "waybill_no", None) or "", + "settleStatus": getattr(freight, "settle_status", None) or "", "linkedSampleCount": count_map.get(freight.id, 0), } return out diff --git a/backend/tests/test_sf_express_settlement.py b/backend/tests/test_sf_express_settlement.py index b689cf97..f34880e4 100644 --- a/backend/tests/test_sf_express_settlement.py +++ b/backend/tests/test_sf_express_settlement.py @@ -211,3 +211,18 @@ def test_amount_serializer_keeps_null_instead_of_zero(): assert admin["freightAmountLabel"] == "月结" assert market["freightAmount"] is None assert market["freightAmountLabel"] == "月结" + + +def test_sample_card_payload_keeps_null_amount(): + from app.crud.sample_freight import freight_amount_payload + + monthly = freight_amount_payload(None, "sfkd", True) + assert monthly["freightAmount"] is None + assert monthly["freightAmountLabel"] == "月结" + assert freight_amount_payload(None, "sfkd", False)["freightAmount"] is None + assert freight_amount_payload(None, "zxc", True)["freightAmountLabel"] == "" + + numeric = freight_amount_payload(Decimal("88"), "zxc", True) + assert numeric["freightAmount"] == "88.00" + assert numeric["freightAmountLabel"] == "88.00" + assert freight_amount_payload(Decimal("0"), "zxc", False)["freightAmount"] == 0.0