diff --git a/erpnext_shipping/erpnext_shipping/doctype/sendcloud/sendcloud.py b/erpnext_shipping/erpnext_shipping/doctype/sendcloud/sendcloud.py index 7108391..47ea61c 100644 --- a/erpnext_shipping/erpnext_shipping/doctype/sendcloud/sendcloud.py +++ b/erpnext_shipping/erpnext_shipping/doctype/sendcloud/sendcloud.py @@ -6,6 +6,8 @@ import requests import frappe import json + +from requests.exceptions import HTTPError from frappe import _ from frappe.utils import flt from frappe.utils.data import get_link_to_form @@ -113,6 +115,19 @@ def get_label(self, shipment_id): except Exception: show_error_alert("printing SendCloud Label") + def download_label(self, label_url: str): + """Download label from SendCloud.""" + try: + resp = requests.get(label_url, auth=(self.api_key, self.api_secret)) + resp.raise_for_status() + return resp.content + except HTTPError: + frappe.msgprint( + _("An error occurred while downloading label from SendCloud"), + indicator='orange', + alert=True + ) + def get_tracking_data(self, shipment_id): # return SendCloud tracking data try: diff --git a/erpnext_shipping/erpnext_shipping/shipping.py b/erpnext_shipping/erpnext_shipping/shipping.py index 9a2cf9d..c969892 100644 --- a/erpnext_shipping/erpnext_shipping/shipping.py +++ b/erpnext_shipping/erpnext_shipping/shipping.py @@ -5,8 +5,6 @@ import frappe import json from six import string_types -from frappe import _ -from frappe.utils import flt from erpnext.stock.doctype.shipment.shipment import get_company_contact from erpnext_shipping.erpnext_shipping.utils import get_address, get_contact, match_parcel_service_type_carrier from erpnext_shipping.erpnext_shipping.doctype.letmeship.letmeship import LETMESHIP_PROVIDER, LetMeShipUtils @@ -164,7 +162,11 @@ def get_delivery_company_name(shipment: str) -> str | None: @frappe.whitelist() -def print_shipping_label(service_provider, shipment_id): +def print_shipping_label(shipment: str): + shipment_doc = frappe.get_doc("Shipment", shipment) + service_provider = shipment_doc.service_provider + shipment_id = shipment_doc.shipment_id + if service_provider == LETMESHIP_PROVIDER: letmeship = LetMeShipUtils() shipping_label = letmeship.get_label(shipment_id) @@ -173,9 +175,31 @@ def print_shipping_label(service_provider, shipment_id): shipping_label = packlink.get_label(shipment_id) elif service_provider == SENDCLOUD_PROVIDER: sendcloud = SendCloudUtils() - shipping_label = sendcloud.get_label(shipment_id) + shipping_label = [] + _labels = sendcloud.get_label(shipment_id) + for label_url in _labels: + content = sendcloud.download_label(label_url) + file_url = save_label_as_attachment(shipment, content) + shipping_label.append(file_url) + return shipping_label + +def save_label_as_attachment(shipment: str, content: bytes) -> str: + """Store label as attachment to Shipment and return the URL.""" + attachment = frappe.new_doc("File") + + attachment.file_name = f"label_{shipment}.pdf" + attachment.content = content + attachment.folder = "Home/Attachments" + attachment.attached_to_doctype = "Shipment" + attachment.attached_to_name = shipment + attachment.is_private = 1 + attachment.save() + + return attachment.file_url + + @frappe.whitelist() def update_tracking(shipment, service_provider, shipment_id, delivery_notes=[]): # Update Tracking info in Shipment diff --git a/erpnext_shipping/public/js/shipment.js b/erpnext_shipping/public/js/shipment.js index 1be636a..9f1bc3b 100644 --- a/erpnext_shipping/public/js/shipment.js +++ b/erpnext_shipping/public/js/shipment.js @@ -69,8 +69,7 @@ frappe.ui.form.on('Shipment', { freeze: true, freeze_message: __("Printing Shipping Label"), args: { - shipment_id: frm.doc.shipment_id, - service_provider: frm.doc.service_provider + shipment: frm.doc.name, }, callback: function(r) { if (r.message) {