Skip to content

Commit

Permalink
Merge pull request #673 from jacobshilitz/ups_surcharges_mapping
Browse files Browse the repository at this point in the history
Refactor UPS rate calculation logic
  • Loading branch information
danh91 committed Sep 11, 2024
2 parents 5b235d9 + 38ecfcc commit bb75e15
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 27 deletions.
37 changes: 22 additions & 15 deletions modules/connectors/ups/karrio/providers/ups/rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,36 @@ def _extract_details(
ctx: dict,
) -> typing.List[models.RateDetails]:
rate = lib.to_object(ups_response.RatedShipmentType, detail)

if rate.NegotiatedRateCharges is not None:
total_charge = (
rate.NegotiatedRateCharges.TotalChargesWithTaxes
or rate.NegotiatedRateCharges.TotalCharge
)
taxes = rate.NegotiatedRateCharges.TaxCharges or []
itemized_charges = [*rate.NegotiatedRateCharges.ItemizedCharges, *taxes]
else:
total_charge = rate.TotalChargesWithTaxes or rate.TotalCharges
taxes = rate.TaxCharges or []
itemized_charges = [*rate.ItemizedCharges, *taxes]
effective_rate = rate.NegotiatedRateCharges if rate.NegotiatedRateCharges is not None else rate
total_charge = effective_rate.TotalChargesWithTaxes or effective_rate.TotalCharges
taxes = effective_rate.TaxCharges or []
itemized_charges = [*effective_rate.ItemizedCharges, *taxes]

charges = [
("Base charge", rate.TransportationCharges.MonetaryValue),
("Base charge", effective_rate.BaseServiceCharge.MonetaryValue),
*(
[]
if any(itemized_charges)
else [("Taxes", sum(lib.to_money(c.MonetaryValue) for c in taxes))]
),
(rate.Service.Code, rate.ServiceOptionsCharges.MonetaryValue),
*(
(getattr(c, "Code", None) or getattr(c, "Type", None), c.MonetaryValue)
(rate.Service.Code, rate.ServiceOptionsCharges.MonetaryValue)
if lib.to_int(rate.ServiceOptionsCharges.MonetaryValue) > 0
else []
),
*(
(
(
provider_units.SurchargeType.map(
str(getattr(c, "Code", None) or getattr(c, "Type", None))
).name.capitalize().replace("_", " ")
if provider_units.SurchargeType.map(
str(getattr(c, "Code", None) or getattr(c, "Type", None))
).name
else (getattr(c, "Code", None) or getattr(c, "Type", None))
),
c.MonetaryValue
)
for c in itemized_charges
),
]
Expand Down
121 changes: 121 additions & 0 deletions modules/connectors/ups/karrio/providers/ups/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -462,3 +462,124 @@ class TrackingStatus(utils.Enum):
in_transit = [""]
delivery_failed = ["RS"]
out_for_delivery = ["OT"]

# from https://developer.ups.com/api/reference/rating/appendix?loc=en_US
class SurchargeType(utils.StrEnum):
additional_handling = "100"
cod = "110"
delivery_confirmation = "120"
ship_delivery_confirmation = "121"
pkg_email_ship_notification = "153"
pkg_email_return_notification = "154"
pkg_email_inbound_return_notification = "155"
pkg_email_quantum_view_ship_notification = "156"
pkg_email_quantum_view_exception_notification = "157"
pkg_email_quantum_view_delivery_notification = "158"
pkg_fax_inbound_return_notification = "165"
pkg_fax_quantum_view_ship_notification = "166"
ship_email_erl_notification = "171"
ship_email_ship_notification = "173"
ship_email_return_notification = "174"
ship_email_inbound_return_notification = "175"
ship_email_quantum_view_ship_notification = "176"
ship_email_quantum_view_exception_notification = "177"
ship_email_quantum_view_delivery_notification = "178"
ship_email_quantum_view_notify = "179"
ship_ups_access_point_notification = "187"
ship_eei_filing_notification = "188"
ship_uap_shipper_notification = "189"
extended_area = "190"
haz_mat = "199"
dry_ice = "200"
isc_seeds = "201"
isc_perishables = "202"
isc_tobacco = "203"
isc_plants = "204"
isc_alcoholic_beverages = "205"
isc_biological_substances = "206"
isc_special_exceptions = "207"
hold_for_pickup = "220"
origin_certificate = "240"
print_return_label = "250"
export_license_verification = "258"
print_n_mail = "260"
residential_address = "270"
return_service_1_attempt = "280"
return_service_3_attempt = "290"
saturday_delivery = "300"
saturday_international_processing_fee = "310"
electronic_return_label = "350"
quantum_view_notify_delivery = "372"
ups_prepared_sed_form = "374"
fuel_surcharge = "375"
delivery_area = "376"
large_package = "377"
shipper_pays_duty_tax = "378"
shipper_pays_duty_tax_unpaid = "379"
express_plus_surcharge = "380"
insurance = "400"
ship_additional_handling = "401"
shipper_release = "402"
check_to_shipper = "403"
ups_proactive_response = "404"
german_pickup = "405"
german_road_tax = "406"
extended_area_pickup = "407"
return_of_document = "410"
peak_season = "430"
peak_season_surcharge_large_package = "431"
peak_season_surcharge_additional_handling = "432"
ship_large_package = "440"
carbon_neutral = "441"
pkg_qv_in_transit_notification = "442"
ship_qv_in_transit_notification = "443"
import_control = "444"
commercial_invoice_removal = "445"
import_control_electronic_label = "446"
import_control_print_label = "447"
import_control_print_and_mail_label = "448"
import_control_one_pick_up_attempt_label = "449"
import_control_three_pick_up_attempt_label = "450"
refrigeration = "452"
pac_1_a_box_1 = "454"
pac_3_a_box_1 = "455"
pac_1_a_box_2 = "456"
pac_3_a_box_2 = "457"
pac_1_a_box_3 = "458"
pac_3_a_box_3 = "459"
pac_1_a_box_4 = "460"
pac_3_a_box_4 = "461"
pac_1_a_box_5 = "462"
pac_3_a_box_5 = "463"
exchange_print_return_label = "464"
exchange_forward = "465"
ship_prealert_notification = "466"
committed_delivery_window = "470"
security_surcharge = "480"
customer_transaction_fee = "492"
shipment_cod = "500"
lift_gate_for_pickup = "510"
lift_gate_for_delivery = "511"
drop_off_at_ups_facility = "512"
ups_premium_care = "515"
oversize_pallet = "520"
mi_dual_label_return = "524"
freight_delivery_surcharge = "530"
freight_pickup_surcharge = "531"
direct_to_retail = "540"
direct_delivery_only = "541"
deliver_to_addressee_only = "542"
direct_to_retail_cod = "543"
retail_access_point = "544"
shipping_ticket_notification = "545"
electronic_package_release_authentication = "546"
pay_at_store = "547"
icod_notification = "548"
item_disposal = "550"
uk_border_fee = "551"
master_carton = "552"
simple_rate_accessorial = "553"
ups_premier_gold = "555"
ups_premier_silver = "556"
ups_premier_platinum = "557"
ddu_oversize = "558"
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ class NegotiatedRateChargesType:
TaxCharges: List[TaxChargeType] = JList[TaxChargeType]
TotalCharge: Optional[BaseServiceChargeType] = JStruct[BaseServiceChargeType]
TotalChargesWithTaxes: Optional[BaseServiceChargeType] = JStruct[BaseServiceChargeType]
BaseServiceCharge: Optional[BaseServiceChargeType] = JStruct[BaseServiceChargeType]


@s(auto_attribs=True)
Expand Down
20 changes: 8 additions & 12 deletions modules/connectors/ups/tests/ups/test_rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,8 @@ def test_parse_rate_response(self):
"carrier_name": "ups",
"currency": "CAD",
"extra_charges": [
{"amount": 137.87, "currency": "CAD", "name": "Base charge"},
{"amount": 0.0, "currency": "CAD", "name": "14"},
{"amount": 21.52, "currency": "CAD", "name": "375"},
{"amount": 116.35, "currency": "CAD", "name": "Base charge"},
{"amount": 21.52, "currency": "CAD", "name": "Fuel surcharge"},
{"amount": 20.68, "currency": "CAD", "name": "HST"},
],
"meta": {"service_name": "ups_express_early_ca"},
Expand All @@ -134,9 +133,8 @@ def test_parse_rate_response(self):
"carrier_name": "ups",
"currency": "CAD",
"extra_charges": [
{"amount": 85.26, "currency": "CAD", "name": "Base charge"},
{"amount": 0.0, "currency": "CAD", "name": "01"},
{"amount": 13.31, "currency": "CAD", "name": "375"},
{"amount": 71.95, "currency": "CAD", "name": "Base charge"},
{"amount": 13.31, "currency": "CAD", "name": "Fuel surcharge"},
{"amount": 12.79, "currency": "CAD", "name": "HST"},
],
"meta": {"service_name": "ups_express_ca"},
Expand All @@ -149,9 +147,8 @@ def test_parse_rate_response(self):
"carrier_name": "ups",
"currency": "CAD",
"extra_charges": [
{"amount": 84.02, "currency": "CAD", "name": "Base charge"},
{"amount": 0.0, "currency": "CAD", "name": "13"},
{"amount": 13.12, "currency": "CAD", "name": "375"},
{"amount": 70.90, "currency": "CAD", "name": "Base charge"},
{"amount": 13.12, "currency": "CAD", "name": "Fuel surcharge"},
{"amount": 12.6, "currency": "CAD", "name": "HST"},
],
"meta": {"service_name": "ups_express_saver_ca"},
Expand All @@ -164,9 +161,8 @@ def test_parse_rate_response(self):
"carrier_name": "ups",
"currency": "CAD",
"extra_charges": [
{"amount": 79.51, "currency": "CAD", "name": "Base charge"},
{"amount": 0.0, "currency": "CAD", "name": "02"},
{"amount": 12.41, "currency": "CAD", "name": "375"},
{"amount": 67.10, "currency": "CAD", "name": "Base charge"},
{"amount": 12.41, "currency": "CAD", "name": "Fuel surcharge"},
{"amount": 11.93, "currency": "CAD", "name": "HST"},
],
"meta": {"service_name": "ups_expedited_ca"},
Expand Down

0 comments on commit bb75e15

Please sign in to comment.