Skip to content

Commit

Permalink
feat: download label from Sendcloud
Browse files Browse the repository at this point in the history
  • Loading branch information
barredterra committed Feb 8, 2024
1 parent e075741 commit 5d030bb
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
15 changes: 15 additions & 0 deletions erpnext_shipping/erpnext_shipping/doctype/sendcloud/sendcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
32 changes: 28 additions & 4 deletions erpnext_shipping/erpnext_shipping/shipping.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down
3 changes: 1 addition & 2 deletions erpnext_shipping/public/js/shipment.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 5d030bb

Please sign in to comment.