From 87dd9eb287153ec47cfc600c7a2ddf25c8c19824 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Mon, 27 Aug 2018 22:40:40 +0200 Subject: [PATCH] Add fall-back and use mechanism usage issue7624 review70311002 --- CHANGELOG | 2 ++ __init__.py | 1 + account.py | 49 ++++++++++++++++++++++++++++++++++--- account.xml | 6 +++++ view/configuration_form.xml | 9 +++++++ view/dunning_level_form.xml | 6 ++++- 6 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 view/configuration_form.xml diff --git a/CHANGELOG b/CHANGELOG index 585fc68878..e1c54a46cb 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* Add contact mechanism usage +* Add fall-back user * Remove support for Python 2.7 Version 4.8.0 - 2018-04-23 diff --git a/__init__.py b/__init__.py index 40ed6f8f36..96fcaffe71 100644 --- a/__init__.py +++ b/__init__.py @@ -9,6 +9,7 @@ __all__ = ['register'] def register(): Pool.register( + account.Configuration, account.DunningLevel, account.Dunning, account.DunningEmailLog, diff --git a/account.py b/account.py index 0194d0c7e2..f8c4d11f92 100644 --- a/account.py +++ b/account.py @@ -12,7 +12,19 @@ from trytond.sendmail import sendmail_transactional, SMTPDataManager from trytond.transaction import Transaction from trytond.wizard import StateTransition -__all__ = ['DunningLevel', 'ProcessDunning', 'Dunning', 'DunningEmailLog'] +__all__ = ['Configuration', 'DunningLevel', 'ProcessDunning', 'Dunning', + 'DunningEmailLog'] + + +class Configuration(metaclass=PoolMeta): + __name__ = 'account.configuration' + + dunning_email_fallback = fields.Many2One( + 'res.user', "Fall-back User", + domain=[ + ('email', '!=', None), + ], + help="User notified when no e-mail is found to send the dunning") class DunningLevel(metaclass=PoolMeta): @@ -36,6 +48,13 @@ class DunningLevel(metaclass=PoolMeta): }, depends=['send_email'], help="Leave empty for the value defined in the configuration file.") + email_contact_mechanism = fields.Selection( + 'get_contact_mechanisms', "Contact Mechanism", + states={ + 'invisible': ~Eval('send_email'), + }, + depends=['send_email'], + help="Define which e-mail to use from the party's contact mechanisms") @classmethod def default_email_template(cls): @@ -46,6 +65,20 @@ class DunningLevel(metaclass=PoolMeta): except KeyError: return + @classmethod + def get_contact_mechanisms(cls): + pool = Pool() + ContactMechanism = pool.get('party.contact_mechanism') + return ContactMechanism.usages() + + @classmethod + def view_attributes(cls): + return super().view_attributes() + [ + ('//separator[@id="email"]', 'states', { + 'invisible': ~Eval('send_email'), + }), + ] + class ProcessDunning(metaclass=PoolMeta): __name__ = 'account.dunning.process' @@ -81,13 +114,21 @@ class Dunning(metaclass=PoolMeta): def send_email(self, datamanager=None): pool = Pool() Configuration = pool.get('ir.configuration') + AccountConfig = pool.get('account.configuration') Lang = pool.get('ir.lang') + account_config = AccountConfig(1) + from_ = self.level.email_from or config.get('email', 'from') to = [] - if self.party.email: - name = str(Header(self.party.rec_name)) - to.append(formataddr((name, self.party.email))) + contact = self.party.contact_mechanism_get( + 'email', usage=self.level.email_contact_mechanism) + if contact and contact.email: + name = str(Header(contact.name or self.party.rec_name)) + to.append(formataddr((name, contact.email))) + elif account_config.dunning_email_fallback: + user = account_config.dunning_email_fallback + to.append(formataddr((self.party.rec_name, user.email))) cc = [] bcc = [] languages = set() diff --git a/account.xml b/account.xml index 95908959a7..3498663ae9 100644 --- a/account.xml +++ b/account.xml @@ -3,6 +3,12 @@ this repository contains the full copyright notices and license terms. --> + + account.configuration + + configuration_form + + account.dunning.level + + + + + diff --git a/view/dunning_level_form.xml b/view/dunning_level_form.xml index ae479d6b01..50f2488151 100644 --- a/view/dunning_level_form.xml +++ b/view/dunning_level_form.xml @@ -5,10 +5,14 @@ this repository contains the full copyright notices and license terms. -->