[REF][FIX] coupon,sale_coupon: move mail_template_sale_coupon

mail_template_sale_coupon is specific to sale_coupon so we are moving
it back to sale_coupon module. Additionally, we add a way of selecting
a mail template when generating coupons for customers.
This commit is contained in:
Joseph Caburnay
2020-11-03 10:48:04 +00:00
parent 54c42ae836
commit 4d59d3d1f3
9 changed files with 43 additions and 23 deletions
+1 -1
View File
@@ -15,7 +15,7 @@
'views/coupon_program_views.xml',
'report/coupon_report.xml',
'report/coupon_report_templates.xml',
'data/coupon_email_data.xml',
'data/expire_coupon_cron.xml',
],
'demo': [
'demo/coupon_demo.xml',
+16
View File
@@ -0,0 +1,16 @@
<?xml version="1.0" ?>
<odoo>
<data noupdate="1">
<record id="expire_coupon_cron" model="ir.cron">
<field name="name">Coupon: expire coupon based on date</field>
<field name="model_id" ref="coupon.model_coupon_coupon"/>
<field name="state">code</field>
<field name="code">model.cron_expire_coupon()</field>
<field name="active" eval="True"/>
<field name="user_id" ref="base.user_root"/>
<field name="interval_number">1</field>
<field name="interval_type">days</field>
<field name="numbercall">-1</field>
</record>
</data>
</odoo>
+6 -3
View File
@@ -49,18 +49,21 @@ class Coupon(models.Model):
for coupon in self.filtered(lambda x: x.program_id.validity_duration > 0):
coupon.expiration_date = (coupon.create_date + relativedelta(days=coupon.program_id.validity_duration)).date()
def _get_default_template(self):
return False
def action_coupon_sent(self):
""" Open a window to compose an email, with the edi invoice template
message loaded by default
"""
self.ensure_one()
template = self.env.ref('coupon.mail_template_sale_coupon', False)
default_template = self._get_default_template()
compose_form = self.env.ref('mail.email_compose_message_wizard_form', False)
ctx = dict(
default_model='coupon.coupon',
default_res_id=self.id,
default_use_template=bool(template),
default_template_id=template.id,
default_use_template=bool(default_template),
default_template_id=default_template and default_template.id,
default_composition_mode='comment',
custom_layout='mail.mail_notification_light',
mark_coupon_as_sent=True,
+3 -3
View File
@@ -17,6 +17,7 @@ class CouponGenerate(models.TransientModel):
], default='nbr_coupon')
partners_domain = fields.Char(string="Customer", default='[]')
has_partner_email = fields.Boolean(compute='_compute_has_partner_email')
template_id = fields.Many2one('mail.template', 'Use template', domain="[('model', '=', 'coupon.coupon')]")
def generate_coupon(self):
"""Generates the number of coupons entered in wizard field nbr_coupons
@@ -34,10 +35,9 @@ class CouponGenerate(models.TransientModel):
vals.update({'partner_id': partner.id, 'state': 'sent' if partner.email else 'new'})
coupon = self.env['coupon.coupon'].create(vals)
subject = '%s, a coupon has been generated for you' % (partner.name)
template = self.env.ref('coupon.mail_template_sale_coupon', raise_if_not_found=False)
if template:
if self.template_id:
email_values = {'email_to': partner.email, 'email_from': self.env.user.email or '', 'subject': subject}
template.send_mail(coupon.id, email_values=email_values, notif_layout='mail.mail_notification_light')
self.template_id.send_mail(coupon.id, email_values=email_values, notif_layout='mail.mail_notification_light')
@api.depends('partners_domain')
def _compute_has_partner_email(self):
@@ -10,6 +10,13 @@
<field name="generation_type" widget="radio"/>
<field name="partners_domain" attrs="{'invisible': [('generation_type', '!=', 'nbr_customer')]}" widget="domain" options="{'model': 'res.partner'}"/>
<field name="nbr_coupons" attrs="{'invisible': [('generation_type', '!=', 'nbr_coupon')]}"/>
<field name="template_id"
options="{'no_create': True, 'no_edit': True}"
context="{'default_model': 'coupon.coupon'}"
attrs="{'invisible': [('generation_type', '!=', 'nbr_customer')]}" />
<div class="text-muted" attrs="{'invisible': [('generation_type', '!=', 'nbr_customer')]}">
Specify a mail template to send the generated coupons as email.
</div>
</group>
<div role="alert" class="alert alert-warning" attrs="{'invisible': ['|', ('generation_type', '!=', 'nbr_customer'), ('has_partner_email', '=', True)]}">
Some selected customers do not have an email address and will not receive the coupon.
+1
View File
@@ -8,6 +8,7 @@
'version': '1.0',
'depends': ['coupon', 'sale'],
'data': [
'data/coupon_email_data.xml',
'security/sale_coupon_security.xml',
'security/ir.model.access.csv',
'wizard/sale_coupon_apply_code_views.xml',
@@ -1,8 +1,8 @@
<?xml version="1.0" ?>
<odoo>
<data noupdate="1">
<record id="coupon.mail_template_sale_coupon" model="mail.template">
<field name="name">Coupon: Send by Email</field>
<record id="sale_coupon.mail_template_sale_coupon" model="mail.template">
<field name="name">[Sales] Coupon: Send by Email</field>
<field name="model_id" ref="coupon.model_coupon_coupon"/>
<field name="subject">Your reward coupon from ${object.program_id.company_id.name} </field>
<field name="email_from">${object.program_id.company_id.email | safe}</field>
@@ -95,22 +95,10 @@
</td></tr>
</tbody></table>
</field>
<field name="report_template" ref="report_coupon_code"/>
<field name="report_template" ref="coupon.report_coupon_code"/>
<field name="report_name">Your Coupon Code</field>
<field name="lang">${object.partner_id.lang}</field>
<field name="auto_delete" eval="True"/>
</record>
<record id="expire_coupon_cron" model="ir.cron">
<field name="name">Coupon: expire coupon based on date</field>
<field name="model_id" ref="coupon.model_coupon_coupon"/>
<field name="state">code</field>
<field name="code">model.cron_expire_coupon()</field>
<field name="active" eval="True"/>
<field name="user_id" ref="base.user_root"/>
<field name="interval_number">1</field>
<field name="interval_type">days</field>
<field name="numbercall">-1</field>
</record>
</data>
</odoo>
+5
View File
@@ -41,3 +41,8 @@ class Coupon(models.Model):
if self.program_id not in applicable_programs and self.program_id.promo_applicability == 'on_current_order':
message = {'error': _('At least one of the required conditions is not met to get the reward!')}
return message
def _get_default_template(self):
if self.order_id:
return self.env.ref('sale_coupon.mail_template_sale_coupon', False)
return super()._get_default_template()
+1 -1
View File
@@ -239,7 +239,7 @@ class SaleOrder(models.Model):
return coupon
def _send_reward_coupon_mail(self):
template = self.env.ref('coupon.mail_template_sale_coupon', raise_if_not_found=False)
template = self.env.ref('sale_coupon.mail_template_sale_coupon', raise_if_not_found=False)
if template:
for order in self:
for coupon in order.generated_coupon_ids: