changeset fbf4b19da8c7 in modules/stock_package_shipping_dpd:default details: https://hg.tryton.org/modules/stock_package_shipping_dpd?cmd=changeset&node=fbf4b19da8c7 description: Add notification preference
issue11738 review425741003 diffstat: 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(-) diffs (113 lines): diff -r 02c1417e21ae -r fbf4b19da8c7 CHANGELOG --- a/CHANGELOG Sat Oct 01 14:52:06 2022 +0200 +++ b/CHANGELOG Sun Oct 09 18:52:32 2022 +0200 @@ -1,3 +1,4 @@ +* Add notification preference * Use contact mechanism from address Version 6.4.0 - 2022-05-02 diff -r 02c1417e21ae -r fbf4b19da8c7 carrier.py --- a/carrier.py Sat Oct 01 14:52:06 2022 +0200 +++ b/carrier.py Sun Oct 09 18:52:32 2022 +0200 @@ -92,6 +92,17 @@ '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 -r 02c1417e21ae -r fbf4b19da8c7 stock.py --- a/stock.py Sat Oct 01 14:52:06 2022 +0200 +++ b/stock.py Sun Oct 09 18:52:32 2022 +0200 @@ -276,5 +276,44 @@ '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 -r 02c1417e21ae -r fbf4b19da8c7 tests/scenario_shipping_dpd.rst --- a/tests/scenario_shipping_dpd.rst Sat Oct 01 14:52:06 2022 +0200 +++ b/tests/scenario_shipping_dpd.rst Sun Oct 09 18:52:32 2022 +0200 @@ -64,8 +64,7 @@ >>> 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 @@ >>> 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 -r 02c1417e21ae -r fbf4b19da8c7 view/carrier_form.xml --- a/view/carrier_form.xml Sat Oct 01 14:52:06 2022 +0200 +++ b/view/carrier_form.xml Sun Oct 09 18:52:32 2022 +0200 @@ -6,7 +6,9 @@ <separator string="DPD" colspan="4" id="dpd"/> <label name="dpd_product"/> <field name="dpd_product"/> - <newline/> + <label name="dpd_notification"/> + <field name="dpd_notification"/> + <label name="dpd_printer_language"/> <field name="dpd_printer_language"/> <label name="dpd_paper_format"/>