diff --git a/modules/stock_package_shipping_sendcloud/CHANGELOG b/modules/stock_package_shipping_sendcloud/CHANGELOG index c5068af0e3..5bf83eebf0 100644 --- a/modules/stock_package_shipping_sendcloud/CHANGELOG +++ b/modules/stock_package_shipping_sendcloud/CHANGELOG @@ -1,3 +1,4 @@ +* Add package weight as shipping method criteria * Add warehouse as shipping method criteria * Warn when changing credential diff --git a/modules/stock_package_shipping_sendcloud/carrier.py b/modules/stock_package_shipping_sendcloud/carrier.py index 1f66ae9355..71428b45c1 100644 --- a/modules/stock_package_shipping_sendcloud/carrier.py +++ b/modules/stock_package_shipping_sendcloud/carrier.py @@ -12,7 +12,7 @@ from trytond.model import ( MatchMixin, ModelSQL, ModelView, fields, sequence_ordered) from trytond.pool import Pool, PoolMeta from trytond.protocols.wrappers import HTTPStatus -from trytond.pyson import Eval +from trytond.pyson import Eval, If from .exceptions import SendcloudCredentialWarning, SendcloudError @@ -134,8 +134,8 @@ class CredentialSendcloud(sequence_ordered(), ModelSQL, ModelView, MatchMixin): self._shiping_methods_cache.set(key, methods) return methods - def get_shipping_method(self, shipment): - pattern = self._get_shipping_method_pattern(shipment) + def get_shipping_method(self, shipment, package=None): + pattern = self._get_shipping_method_pattern(shipment, package=package) for method in self.shipping_methods: if method.match(pattern): if method.shipping_method: @@ -144,9 +144,21 @@ class CredentialSendcloud(sequence_ordered(), ModelSQL, ModelView, MatchMixin): return None @classmethod - def _get_shipping_method_pattern(cls, shipment): + def _get_shipping_method_pattern(cls, shipment, package=None): + pool = Pool() + UoM = pool.get('product.uom') + ModelData = pool.get('ir.model.data') + kg = UoM(ModelData.get_id('product', 'uom_kilogram')) + + if package: + weight = UoM.compute_qty( + package.weight_uom, package.total_weight, kg, round=False) + else: + weight = UoM.compute_qty( + shipment.weight_uom, shipment.weight, kg, round=False) return { 'carrier': shipment.carrier.id if shipment.carrier else None, + 'weight': weight, } @sendcloud_api @@ -247,6 +259,30 @@ class SendcloudShippingMethod( domain=[ ('type', '=', 'warehouse'), ]) + min_weight = fields.Float( + "Minimal Weight", + domain=[ + ['OR', + ('min_weight', '=', None), + ('min_weight', '>', 0), + ], + If(Eval('max_weight', 0), + ('min_weight', '<=', Eval('max_weight', 0)), + ()), + ], + help="Minimal weight included in kg.") + max_weight = fields.Float( + "Maximal Weight", + domain=[ + ['OR', + ('max_weight', '=', None), + ('max_weight', '>', 0), + ], + If(Eval('min_weight', 0), + ('max_weight', '>=', Eval('min_weight', 0)), + ()), + ], + help="Maximal weight included in kg.") shipping_method = fields.Selection( 'get_shipping_methods', "Shipping Method") @@ -267,6 +303,15 @@ class SendcloudShippingMethod( sender_address=sender_address)] return methods + def match(self, pattern, match_none=False): + pattern = pattern.copy() + if (weight := pattern.pop('weight')) is not None: + min_weight = self.min_weight or 0 + max_weight = self.max_weight or weight + if not (min_weight <= weight <= max_weight): + return False + return super().match(pattern, match_none=match_none) + 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 3b57951679..8a3ef5cf09 100644 --- a/modules/stock_package_shipping_sendcloud/stock.py +++ b/modules/stock_package_shipping_sendcloud/stock.py @@ -179,7 +179,8 @@ class CreateShippingSendcloud(Wizard): 'height': UoM.compute_qty(package.height_uom, package.height, cm), 'request_label': True, } - shipping_method = credential.get_shipping_method(shipment) + shipping_method = credential.get_shipping_method( + shipment, package=package) if shipping_method: parcel['shipment'] = {'id': shipping_method} else: 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 3fcdcd632d..78b4a06eb0 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 @@ -12,6 +12,11 @@ this repository contains the full copyright notices and license terms. -->