Add package weight as shipping method criteria

Closes #13390
This commit is contained in:
Cédric Krier
2024-07-04 18:25:59 +02:00
parent 0f0fe9053c
commit 337dc832e0
5 changed files with 59 additions and 5 deletions
@@ -1,3 +1,4 @@
* Add package weight as shipping method criteria
* Add warehouse as shipping method criteria
* Warn when changing credential
@@ -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'
@@ -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:
@@ -12,6 +12,11 @@ this repository contains the full copyright notices and license terms. -->
<label name="warehouse"/>
<field name="warehouse"/>
<label name="min_weight"/>
<field name="min_weight"/>
<label name="max_weight"/>
<field name="max_weight"/>
<label name="shipping_method"/>
<field name="shipping_method"/>
</form>
@@ -5,5 +5,7 @@ this repository contains the full copyright notices and license terms. -->
<field name="sendcloud" expand="1"/>
<field name="carrier" expand="1"/>
<field name="warehouse" expand="1"/>
<field name="min_weight" optional="1"/>
<field name="max_weight" optional="1"/>
<field name="shipping_method" expand="2"/>
</tree>