diff --git a/modules/stock_package_shipping_sendcloud/CHANGELOG b/modules/stock_package_shipping_sendcloud/CHANGELOG index 09051b82760b40ccf85724b3ec228c96599ca105_bW9kdWxlcy9zdG9ja19wYWNrYWdlX3NoaXBwaW5nX3NlbmRjbG91ZC9DSEFOR0VMT0c=..ea51994919b063eb0131b3a66ca16ad164a24f25_bW9kdWxlcy9zdG9ja19wYWNrYWdlX3NoaXBwaW5nX3NlbmRjbG91ZC9DSEFOR0VMT0c= 100644 --- a/modules/stock_package_shipping_sendcloud/CHANGELOG +++ b/modules/stock_package_shipping_sendcloud/CHANGELOG @@ -1,3 +1,4 @@ +* Set multicollo on shipping method * Warn when changing credential Version 7.2.0 - 2024-04-29 diff --git a/modules/stock_package_shipping_sendcloud/carrier.py b/modules/stock_package_shipping_sendcloud/carrier.py index 09051b82760b40ccf85724b3ec228c96599ca105_bW9kdWxlcy9zdG9ja19wYWNrYWdlX3NoaXBwaW5nX3NlbmRjbG91ZC9jYXJyaWVyLnB5..ea51994919b063eb0131b3a66ca16ad164a24f25_bW9kdWxlcy9zdG9ja19wYWNrYWdlX3NoaXBwaW5nX3NlbmRjbG91ZC9jYXJyaWVyLnB5 100644 --- a/modules/stock_package_shipping_sendcloud/carrier.py +++ b/modules/stock_package_shipping_sendcloud/carrier.py @@ -135,10 +135,7 @@ pattern = self._get_shipping_method_pattern(shipment) for method in self.shipping_methods: if method.match(pattern): - if method.shipping_method: - return int(method.shipping_method) - else: - return None + return method @classmethod def _get_shipping_method_pattern(cls, shipment): @@ -166,6 +163,17 @@ return response.json()['parcels'] @sendcloud_api + def create_parcel(self, parcel): + response = requests.post( + SENDCLOUD_API_URL + 'parcels', json={'parcel': parcel}, + auth=self.auth, timeout=TIMEOUT, headers=HEADERS) + if response.status_code == 400: + msg = response.json()['error']['message'] + raise requests.HTTPError(msg, response=response) + response.raise_for_status() + return response.json()['parcel'] + + @sendcloud_api def get_label(self, url): response = requests.get( url, auth=self.auth, timeout=TIMEOUT, headers=HEADERS) @@ -241,6 +249,7 @@ ]) shipping_method = fields.Selection( 'get_shipping_methods', "Shipping Method") + multicollo = fields.Boolean("Multicollo") @fields.depends('sendcloud', '_parent_sendcloud.id') def get_shipping_methods(self): @@ -253,6 +262,10 @@ for m in self.sendcloud.get_shipping_methods()] return methods + @classmethod + def default_multicollo(cls): + return True + class Carrier(metaclass=PoolMeta): __name__ = 'carrier' diff --git a/modules/stock_package_shipping_sendcloud/stock.py b/modules/stock_package_shipping_sendcloud/stock.py index 09051b82760b40ccf85724b3ec228c96599ca105_bW9kdWxlcy9zdG9ja19wYWNrYWdlX3NoaXBwaW5nX3NlbmRjbG91ZC9zdG9jay5weQ==..ea51994919b063eb0131b3a66ca16ad164a24f25_bW9kdWxlcy9zdG9ja19wYWNrYWdlX3NoaXBwaW5nX3NlbmRjbG91ZC9zdG9jay5weQ== 100644 --- a/modules/stock_package_shipping_sendcloud/stock.py +++ b/modules/stock_package_shipping_sendcloud/stock.py @@ -109,5 +109,6 @@ carrier = shipment.carrier packages = shipment.root_packages + multicollo = True parcels = [] for package in packages: @@ -112,7 +113,17 @@ parcels = [] for package in packages: - parcels.append(self.get_parcel(shipment, package, credential)) - parcels = credential.create_parcels(parcels) + parcel, shipping_method = self.get_parcel( + shipment, package, credential) + if shipping_method: + multicollo &= shipping_method.multicollo + parcels.append(parcel) + if multicollo: + parcels = credential.create_parcels(parcels) + else: + new_parcels = [] + for parcel in parcels: + new_parcels.append(credential.create_parcel(parcel)) + parcels = new_parcels for package, parcel in zip_longest(packages, parcels): format_ = shipment.carrier.sendcloud_format.split() @@ -140,6 +151,7 @@ return 'end' def get_parcel(self, shipment, package, credential, usage=None): + "Returns the parcel and shipping method" pool = Pool() UoM = pool.get('product.uom') ModelData = pool.get('ir.model.data') @@ -180,7 +192,7 @@ 'request_label': True, } shipping_method = credential.get_shipping_method(shipment) - if shipping_method: - parcel['shipment'] = {'id': shipping_method} + if shipping_method and shipping_method.shipping_method: + parcel['shipment'] = {'id': int(shipping_method.shipping_method)} else: parcel['apply_shipping_rules'] = True @@ -185,3 +197,3 @@ else: parcel['apply_shipping_rules'] = True - return parcel + return parcel, shipping_method diff --git a/modules/stock_package_shipping_sendcloud/tests/scenario_stock_package_shipping_sendcloud.json b/modules/stock_package_shipping_sendcloud/tests/scenario_stock_package_shipping_sendcloud.json new file mode 100644 index 0000000000000000000000000000000000000000..ea51994919b063eb0131b3a66ca16ad164a24f25_bW9kdWxlcy9zdG9ja19wYWNrYWdlX3NoaXBwaW5nX3NlbmRjbG91ZC90ZXN0cy9zY2VuYXJpb19zdG9ja19wYWNrYWdlX3NoaXBwaW5nX3NlbmRjbG91ZC5qc29u --- /dev/null +++ b/modules/stock_package_shipping_sendcloud/tests/scenario_stock_package_shipping_sendcloud.json @@ -0,0 +1,4 @@ +[ + {"multicollo": true} + ,{"multicollo": false} +] diff --git a/modules/stock_package_shipping_sendcloud/tests/scenario_stock_package_shipping_sendcloud.rst b/modules/stock_package_shipping_sendcloud/tests/scenario_stock_package_shipping_sendcloud.rst index 09051b82760b40ccf85724b3ec228c96599ca105_bW9kdWxlcy9zdG9ja19wYWNrYWdlX3NoaXBwaW5nX3NlbmRjbG91ZC90ZXN0cy9zY2VuYXJpb19zdG9ja19wYWNrYWdlX3NoaXBwaW5nX3NlbmRjbG91ZC5yc3Q=..ea51994919b063eb0131b3a66ca16ad164a24f25_bW9kdWxlcy9zdG9ja19wYWNrYWdlX3NoaXBwaW5nX3NlbmRjbG91ZC90ZXN0cy9zY2VuYXJpb19zdG9ja19wYWNrYWdlX3NoaXBwaW5nX3NlbmRjbG91ZC5yc3Q= 100644 --- a/modules/stock_package_shipping_sendcloud/tests/scenario_stock_package_shipping_sendcloud.rst +++ b/modules/stock_package_shipping_sendcloud/tests/scenario_stock_package_shipping_sendcloud.rst @@ -20,6 +20,8 @@ ... SENDCLOUD_API_URL) >>> from trytond.tests.tools import activate_modules + >>> multicollo = globals().get('multicollo', True) + Activate modules:: >>> config = activate_modules( @@ -163,6 +165,7 @@ ... {'id': shipping_method.id, 'sendcloud': {'id': credential.id}}, ... shipping_method._context) ... if m[1] == "Unstamped letter"] + >>> shipping_method.multicollo = multicollo >>> credential.save() >>> template = ProductTemplate() diff --git a/modules/stock_package_shipping_sendcloud/view/carrier_shipping_method_form.xml b/modules/stock_package_shipping_sendcloud/view/carrier_shipping_method_form.xml index 09051b82760b40ccf85724b3ec228c96599ca105_bW9kdWxlcy9zdG9ja19wYWNrYWdlX3NoaXBwaW5nX3NlbmRjbG91ZC92aWV3L2NhcnJpZXJfc2hpcHBpbmdfbWV0aG9kX2Zvcm0ueG1s..ea51994919b063eb0131b3a66ca16ad164a24f25_bW9kdWxlcy9zdG9ja19wYWNrYWdlX3NoaXBwaW5nX3NlbmRjbG91ZC92aWV3L2NhcnJpZXJfc2hpcHBpbmdfbWV0aG9kX2Zvcm0ueG1s 100644 --- a/modules/stock_package_shipping_sendcloud/view/carrier_shipping_method_form.xml +++ b/modules/stock_package_shipping_sendcloud/view/carrier_shipping_method_form.xml @@ -11,4 +11,7 @@ <field name="carrier"/> <label name="shipping_method"/> <field name="shipping_method"/> + + <label name="multicollo"/> + <field name="multicollo"/> </form> diff --git a/modules/stock_package_shipping_sendcloud/view/carrier_shipping_method_list.xml b/modules/stock_package_shipping_sendcloud/view/carrier_shipping_method_list.xml index 09051b82760b40ccf85724b3ec228c96599ca105_bW9kdWxlcy9zdG9ja19wYWNrYWdlX3NoaXBwaW5nX3NlbmRjbG91ZC92aWV3L2NhcnJpZXJfc2hpcHBpbmdfbWV0aG9kX2xpc3QueG1s..ea51994919b063eb0131b3a66ca16ad164a24f25_bW9kdWxlcy9zdG9ja19wYWNrYWdlX3NoaXBwaW5nX3NlbmRjbG91ZC92aWV3L2NhcnJpZXJfc2hpcHBpbmdfbWV0aG9kX2xpc3QueG1s 100644 --- a/modules/stock_package_shipping_sendcloud/view/carrier_shipping_method_list.xml +++ b/modules/stock_package_shipping_sendcloud/view/carrier_shipping_method_list.xml @@ -5,4 +5,5 @@ <field name="sendcloud" expand="1"/> <field name="carrier" expand="1"/> <field name="shipping_method" expand="2"/> + <field name="multicollo"/> </tree>