From 3e5c8f9fcaeaeac5ec5abf088ad2823bf57481e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sun, 9 Oct 2022 18:52:32 +0200 Subject: [PATCH] Add notification preference issue11738 review425741003 --- CHANGELOG | 1 + carrier.py | 11 +++++++++ stock.py | 41 ++++++++++++++++++++++++++++++++- tests/scenario_shipping_dpd.rst | 4 ++-- view/carrier_form.xml | 4 +++- 5 files changed, 57 insertions(+), 4 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index c266e11163..0438d35c21 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,4 @@ +* Add notification preference * Use contact mechanism from address Version 6.4.0 - 2022-05-02 diff --git a/carrier.py b/carrier.py index 1e5ad88d1f..a1025d5711 100644 --- a/carrier.py +++ b/carrier.py @@ -92,6 +92,17 @@ class Carrier(metaclass=PoolMeta): 'invisible': Eval('shipping_service') != 'dpd', }) + dpd_notification = fields.Selection([ + (None, ""), + ('email', "E-Mail"), + ('sms', "SMS"), + ], "Notification", + states={ + 'invisible': Eval('shipping_service') != 'dpd', + }, + help="The preferred notification channel.\n" + "Leave empty for no notification.") + @classmethod def __setup__(cls): super(Carrier, cls).__setup__() diff --git a/stock.py b/stock.py index 08cfae3278..fd4c0bb843 100644 --- a/stock.py +++ b/stock.py @@ -276,5 +276,44 @@ class CreateDPDShipping(Wizard): 'parcels': [self.get_parcel(p) for p in packages], 'productAndServiceData': { 'orderType': 'consignment', - } + **self.get_notification(shipment), + }, } + + def get_notification(self, shipment, usage=None): + carrier = shipment.carrier + if not carrier.dpd_notification: + return {} + party = shipment.shipping_to + if party and party.lang: + lang_code = party.lang.code + else: + lang_code = Transaction().language + lang_code = lang_code.upper() + channel2type = { + 'sms': {'mobile'}, + } + + channels = [ + (1, 'email'), + (3, 'sms'), + ] + if carrier.dpd_notification == 'sms': + channels = reversed(channels) + + for channel_id, channel in channels: + mechanism = party.contact_mechanism_get( + channel2type.get(channel, channel), usage=usage) + if not mechanism: + continue + value = mechanism.value + if len(value) > 50: + continue + return { + 'predict': { + 'channel': channel_id, + 'value': value, + 'language': lang_code, + }, + } + return {} diff --git a/tests/scenario_shipping_dpd.rst b/tests/scenario_shipping_dpd.rst index 0c14b120a4..f1179751cd 100644 --- a/tests/scenario_shipping_dpd.rst +++ b/tests/scenario_shipping_dpd.rst @@ -64,8 +64,7 @@ Create parties:: >>> customer_address.city = 'Paris' >>> customer_address.country = france >>> customer_address.save() - >>> customer_phone = customer.contact_mechanisms.new() - >>> customer_phone.type = 'phone' + >>> customer_phone = customer.contact_mechanisms.new(type='mobile') >>> customer_phone.value = '+33 93 842 8862' >>> customer_phone.save() @@ -179,6 +178,7 @@ Create a DPD Carrier and the related credential:: >>> carrier.dpd_product = 'CL' >>> carrier.dpd_printer_language = 'PDF' >>> carrier.dpd_paper_format = 'A6' + >>> carrier.dpd_notification = 'sms' >>> carrier.save() >>> catchall_selection = CarrierSelection(carrier=carrier) diff --git a/view/carrier_form.xml b/view/carrier_form.xml index 3d3c9c205d..e0be4750eb 100644 --- a/view/carrier_form.xml +++ b/view/carrier_form.xml @@ -6,7 +6,9 @@ this repository contains the full copyright notices and license terms. -->