Skip to content

Commit

Permalink
[FIX] account_credit_control: additional fixes
Browse files Browse the repository at this point in the history
- Template translations migration scripts
- Template recipients
  • Loading branch information
chienandalu committed Aug 27, 2020
1 parent ab248cf commit 9a9a5fe
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
2 changes: 1 addition & 1 deletion account_credit_control/data/data.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<field name="name">Credit Control Email</field>
<field name="email_from">${user.company_id.email or ''}</field>
<field name="subject">Credit Control: (${object.policy_level_id.name or 'n/a'})</field>
<field name="email_to">${object.get_email() or ''}</field>
<field name="partner_to">${object.get_emailing_contact().id or ''}</field>
<field name="model_id" ref="model_credit_control_communication"/>
<field name="auto_delete" eval="False"/>
<field name="lang">${object.contact_address_id.lang or 'en_US'}</field>
Expand Down
17 changes: 12 additions & 5 deletions account_credit_control/migrations/12.0.3.0.0/post-migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,18 @@ def migrate(env, version):
openupgrade.logged_query(
env.cr,
"""
UPDATE mail_template SET
value = REPLACE(value, %(from)s, %(to)s),
source = REPLACE(source, %(from)s, %(to)s)
UPDATE ir_translation SET
value = REPLACE(value, %(from)s, %(to)s)
WHERE module = 'account_credit_control'
AND name ilike 'mail.template'
AND name ilike 'mail.template%%'
""",
{"from": from_, "to": to, "model_id": communication_model.id},
{"from": from_, "to": to},
)
# Update the recipients config just for this template, others should be
# manually changed if desired althoug they'll keep working as there's
# backward compatibility with the old method
communication_template = env.ref(
"account_credit_control.email_template_credit_control_base")
communication_template.email_to = False
communication_template.partner_to = (
"${object.get_emailing_contact().id or ''}")
19 changes: 12 additions & 7 deletions account_credit_control/models/credit_control_communication.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,20 @@ def _onchange_partner_id(self):
address_ids = one.partner_id.address_get(adr_pref=['invoice'])
one.contact_address_id = address_ids["invoice"]

@api.multi
def get_email(self):
""" Return a valid email for customer """
def get_emailing_contact(self):
"""Return a valid customer for the emailing. If the contact address
doesn't have a valid email we fallback to the commercial partner"""
self.ensure_one()
contact = self.contact_address_id
email = contact.email
if not email and contact.commercial_partner_id.email:
email = contact.commercial_partner_id.email
return email
if not contact.email:
contact = contact.commercial_partner_id
return contact

def get_email(self):
"""Kept for backwards compatibility. To be removed in v13/v14"""
self.ensure_one()
contact = self.get_emailing_contact()
return contact and contact.email

@api.model
@api.returns('credit.control.line')
Expand Down
2 changes: 1 addition & 1 deletion account_credit_control/wizard/credit_control_emailer.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def email_lines(self):
communications = self._send_emails()
if not communications:
return {'type': 'ir.actions.act_window_close'}
action = self.env.ref(
action = self.sudo().env.ref(
"account_credit_control.credit_control_communication_action")
action['name'] = _('Generated communications')
action["domain"] = [('id', 'in', communications.ids)]
Expand Down

0 comments on commit 9a9a5fe

Please sign in to comment.