From 0f1f1531a4ddf2ef388d6cf2ec9b497a4309cbbc Mon Sep 17 00:00:00 2001 From: Daniel K Date: Mon, 30 Sep 2024 21:53:56 -0700 Subject: [PATCH] feat: introduce dhl_shipment_content to allow custom content description for insured items --- .../karrio/providers/dhl_express/shipment.py | 26 ++++++++++++------- .../karrio/providers/dhl_express/units.py | 5 +++- .../tests/dhl_express/test_shipment.py | 6 +++-- modules/sdk/karrio/core/units.py | 9 +++++++ 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/modules/connectors/dhl_express/karrio/providers/dhl_express/shipment.py b/modules/connectors/dhl_express/karrio/providers/dhl_express/shipment.py index 8a87193a9a..41a47bff64 100644 --- a/modules/connectors/dhl_express/karrio/providers/dhl_express/shipment.py +++ b/modules/connectors/dhl_express/karrio/providers/dhl_express/shipment.py @@ -80,7 +80,7 @@ def shipment_request( ) product = provider_units.ShippingService.map(payload.service).value_or_key - weight_unit, dim_unit = ( + weight_unit, dim_unit = lib.identity( provider_units.COUNTRY_PREFERED_UNITS.get(payload.shipper.country_code) or packages.compatible_units ) @@ -113,9 +113,15 @@ def shipment_request( option for _, option in options.items() if option.state is not False ] duty = customs.duty or models.Duty(paid_by="sender") - content = packages[0].parcel.content or customs.content_description or "N/A" + content = lib.identity( + options.dhl_shipment_content.state + or customs.content_description + or packages.content + or packages.description + or "N/A" + ) reference = payload.reference or getattr(payload, "id", None) - currency = ( + currency = lib.identity( options.currency.state or units.CountryCurrency.map(payload.shipper.country_code).value or settings.default_currency @@ -171,7 +177,7 @@ def shipment_request( ), BusinessPartyTypeCode=None, ), - Commodity=( + Commodity=lib.identity( [ dhl.Commodity( CommodityCode=c.hs_code or c.sku or "N/A", @@ -182,7 +188,7 @@ def shipment_request( if any(customs.commodities) else None ), - Dutiable=( + Dutiable=lib.identity( dhl_global.Dutiable( DeclaredValue=( duty.declared_value or options.declared_value.state or 1.0 @@ -212,10 +218,10 @@ def shipment_request( ), UseDHLInvoice=("Y" if is_dutiable else None), DHLInvoiceLanguageCode=("en" if is_dutiable else None), - DHLInvoiceType=( + DHLInvoiceType=lib.identity( ("CMI" if customs.commercial_invoice else "PFI") if is_dutiable else None ), - ExportDeclaration=( + ExportDeclaration=lib.identity( dhl_global.ExportDeclaration( InterConsignee=None, IsPartiesRelation=None, @@ -291,7 +297,7 @@ def shipment_request( "COMMERCIAL" if customs.commercial_invoice else "PERSONAL" ), DocumentFunction=None, - CustomsDocuments=( + CustomsDocuments=lib.identity( dhl_global.CustomsDocuments( CustomsDocument=[ dhl.CustomsDocument( @@ -314,7 +320,7 @@ def shipment_request( if is_dutiable else None ), - Reference=( + Reference=lib.identity( [dhl.Reference(ReferenceID=lib.text(reference, max=30))] if any(reference or "") else None @@ -373,7 +379,7 @@ def shipment_request( IsDutiable=("Y" if is_dutiable else "N"), CurrencyCode=currency, CustData=getattr(payload, "id", None), - ShipmentCharges=( + ShipmentCharges=lib.identity( options.cash_on_delivery.state if options.cash_on_delivery.state else None diff --git a/modules/connectors/dhl_express/karrio/providers/dhl_express/units.py b/modules/connectors/dhl_express/karrio/providers/dhl_express/units.py index 1f4b2ab77f..5d45f48ae1 100644 --- a/modules/connectors/dhl_express/karrio/providers/dhl_express/units.py +++ b/modules/connectors/dhl_express/karrio/providers/dhl_express/units.py @@ -589,6 +589,9 @@ class ShippingOption(lib.Enum): dhl_customer_rebate = lib.OptionEnum("ZD", bool) dhl_e_com_discount = lib.OptionEnum("ZE", bool) + """ Custom Options """ + dhl_shipment_content = lib.OptionEnum("content") + """ Unified Option type mapping """ insurance = dhl_shipment_insurance paperless_trade = dhl_paperless_trade @@ -613,7 +616,7 @@ def shipping_options_initializer( _options.update(package_options.content) def items_filter(key: str) -> bool: - return key in ShippingOption # type: ignore + return key in ShippingOption and key != "dhl_shipment_content" # type: ignore return lib.units.ShippingOptions( _options, ShippingOption, items_filter=items_filter diff --git a/modules/connectors/dhl_express/tests/dhl_express/test_shipment.py b/modules/connectors/dhl_express/tests/dhl_express/test_shipment.py index 45e4fcff09..b154cb37ec 100644 --- a/modules/connectors/dhl_express/tests/dhl_express/test_shipment.py +++ b/modules/connectors/dhl_express/tests/dhl_express/test_shipment.py @@ -136,7 +136,9 @@ def test_not_supported_cancel_shipment(self): "incoterm": "DAP", "invoice": "N/A", "invoice_date": "2021-05-03", - "commodities": [{"description": "cn", "weight": 4.0, "sku": "sku", "hs_code": "hs_code"}], + "commodities": [ + {"description": "cn", "weight": 4.0, "sku": "sku", "hs_code": "hs_code"} + ], "duty": { "account_number": "123456789", "paid_by": "sender", @@ -538,7 +540,7 @@ def test_not_supported_cancel_shipment(self): 8 8 2023-12-15 - N/A + title C JJ Y diff --git a/modules/sdk/karrio/core/units.py b/modules/sdk/karrio/core/units.py index 6d3760e90b..9cbd5dbe90 100644 --- a/modules/sdk/karrio/core/units.py +++ b/modules/sdk/karrio/core/units.py @@ -918,6 +918,15 @@ def description(self) -> typing.Optional[str]: return description + @property + def content(self) -> typing.Optional[str]: + contents = set([item.parcel.content for item in self._items]) + content: typing.Optional[str] = utils.SF.concat_str( + *list(contents), join=True + ) # type:ignore + + return content + @property def options(self) -> "ShippingOptions": def merge_options(acc, pkg) -> dict: