From 543b737bd1e19d03d6b6c5031583f2e711fe5e87 Mon Sep 17 00:00:00 2001 From: barredterra <14891507+barredterra@users.noreply.github.com> Date: Wed, 7 Feb 2024 19:42:22 +0100 Subject: [PATCH] fix: use correct recipient company (sendcloud) --- .../doctype/sendcloud/sendcloud.py | 10 +++++----- erpnext_shipping/erpnext_shipping/shipping.py | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/erpnext_shipping/erpnext_shipping/doctype/sendcloud/sendcloud.py b/erpnext_shipping/erpnext_shipping/doctype/sendcloud/sendcloud.py index 5fd6cee..7108391 100644 --- a/erpnext_shipping/erpnext_shipping/doctype/sendcloud/sendcloud.py +++ b/erpnext_shipping/erpnext_shipping/doctype/sendcloud/sendcloud.py @@ -57,7 +57,7 @@ def get_available_services(self, delivery_address, shipment_parcel): except Exception: show_error_alert("fetching SendCloud prices") - def create_shipment(self, shipment, delivery_address, delivery_contact, service_info, shipment_parcel, + def create_shipment(self, shipment, delivery_company_name, delivery_address, delivery_contact, service_info, shipment_parcel, description_of_content, value_of_goods): # Create a transaction at SendCloud if not self.enabled or not self.api_key or not self.api_secret: @@ -65,7 +65,7 @@ def create_shipment(self, shipment, delivery_address, delivery_contact, service_ parcels = [] for i, parcel in enumerate(json.loads(shipment_parcel), start=1): - parcel_data = self.get_parcel_dict(shipment, parcel, i, delivery_address, + parcel_data = self.get_parcel_dict(shipment, parcel, i, delivery_company_name, delivery_address, delivery_contact, service_info, description_of_content, value_of_goods) parcels.append(parcel_data) @@ -174,11 +174,11 @@ def get_carrier(self, carrier_name, post_or_get=None): else: return carrier_name.upper() if post_or_get=="get" else carrier_name.lower() - def get_parcel_dict(self, shipment, parcel, index, delivery_address, delivery_contact, + def get_parcel_dict(self, shipment, parcel, index, delivery_company_name, delivery_address, delivery_contact, service_info, description_of_content, value_of_goods): return { 'name': "{} {}".format(delivery_contact.first_name, delivery_contact.last_name), - 'company_name': delivery_address.address_title, + 'company_name': delivery_company_name or delivery_address.address_title, 'address': delivery_address.address_line1, 'address_2': delivery_address.address_line2 or '', 'city': delivery_address.city, @@ -195,4 +195,4 @@ def get_parcel_dict(self, shipment, parcel, index, delivery_address, delivery_co 'external_reference': "{}-{}".format(shipment, index), 'weight': parcel.get('weight'), 'parcel_items': self.get_parcel_items(parcel, description_of_content, value_of_goods) - } \ No newline at end of file + } diff --git a/erpnext_shipping/erpnext_shipping/shipping.py b/erpnext_shipping/erpnext_shipping/shipping.py index 0601c28..d94fdb7 100644 --- a/erpnext_shipping/erpnext_shipping/shipping.py +++ b/erpnext_shipping/erpnext_shipping/shipping.py @@ -84,6 +84,7 @@ def create_shipment(shipment, pickup_from_type, delivery_to_type, pickup_address shipment_info, pickup_contact, delivery_contact = None, None, None pickup_address = get_address(pickup_address_name) delivery_address = get_address(delivery_address_name) + delivery_company_name = get_delivery_company_name(shipment) if pickup_from_type != 'Company': pickup_contact = get_contact(pickup_contact_name) @@ -127,6 +128,7 @@ def create_shipment(shipment, pickup_from_type, delivery_to_type, pickup_address sendcloud = SendCloudUtils() shipment_info = sendcloud.create_shipment( shipment=shipment, + delivery_company_name=delivery_company_name, delivery_address=delivery_address, shipment_parcel=shipment_parcel, description_of_content=description_of_content, @@ -146,6 +148,19 @@ def create_shipment(shipment, pickup_from_type, delivery_to_type, pickup_address return shipment_info + +def get_delivery_company_name(shipment: str) -> str | None: + shipment_doc = frappe.get_doc('Shipment', shipment) + if shipment_doc.delivery_customer: + return frappe.db.get_value('Customer', shipment_doc.delivery_customer, 'customer_name') + if shipment_doc.delivery_supplier: + return frappe.db.get_value('Supplier', shipment_doc.delivery_supplier, 'supplier_name') + if shipment_doc.delivery_company: + return frappe.db.get_value('Company', shipment_doc.delivery_company, 'company_name') + + return None + + @frappe.whitelist() def print_shipping_label(service_provider, shipment_id): if service_provider == LETMESHIP_PROVIDER: