Skip to content

Commit

Permalink
feat(Sendcloud): filter services by weight
Browse files Browse the repository at this point in the history
  • Loading branch information
barredterra committed Feb 16, 2024
1 parent 4a70f22 commit d132976
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions erpnext_shipping/erpnext_shipping/doctype/sendcloud/sendcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,17 @@ def get_available_services(self, delivery_address, parcels: list[dict]):
frappe.throw(error_message, title=_("SendCloud"))

available_services = []
iso_code = delivery_address.country_code
for service in responses_dict.get("shipping_methods", []):
countries = [
country
for country in service["countries"]
if country["iso_2"].upper() == iso_code.upper()
if country["iso_2"] == to_country
]
if countries:
available_service = self.get_service_dict(service, countries[0], parcels)

if countries and check_weight(service, parcels):
available_service = self.get_service_dict(
service, countries[0], parcels
)
available_services.append(available_service)

return available_services
Expand Down Expand Up @@ -253,3 +255,13 @@ def get_parcel_dict(
"weight": parcel.get("weight"),
"parcel_items": self.get_parcel_items(parcel, description_of_content, value_of_goods),
}


def check_weight(service: dict, parcels: list[dict]) -> bool:
"""Check if the weight of any parcel is within the range of the service."""
max_weight_kg = float(service["max_weight"])
min_weight_kg = float(service["min_weight"])
return any(
max_weight_kg > parcel.get("weight") and min_weight_kg <= parcel.get("weight")
for parcel in parcels
)

0 comments on commit d132976

Please sign in to comment.