From 96fed8045446c5cdf14988b50ee9f5eb81e0dc4e Mon Sep 17 00:00:00 2001 From: Sagar Ahirrao Date: Tue, 27 Dec 2016 11:10:19 +0530 Subject: [PATCH] [IMP] sale_coupon: Allow to print and send coupons by email Purpose ======= A feature was not done when doing the coupon task because of the lack of time : Coupon should be printed from the system and same can be sent to customer via email(attractive email template). Specification ============= Ass the possibility to: - Print coupon - Send coupon by email with the printed report in attachment - Generated coupon by sending to the customers the coupon in attachments --- __init__.py | 1 + __manifest__.py | 4 +- data/sale_coupon_data.xml | 11 -- data/sale_coupon_email_data.xml | 173 ++++++++++++++++++++++++ models/sale_coupon.py | 27 ++++ models/sale_order.py | 9 +- report/__init__.py | 4 + report/sale_coupon_report.py | 18 +++ report/sale_coupon_report.xml | 12 ++ report/sale_coupon_report_templates.xml | 77 +++++++++++ views/sale_coupon_views.xml | 1 + wizard/sale_coupon_generate.py | 13 +- 12 files changed, 322 insertions(+), 28 deletions(-) delete mode 100644 data/sale_coupon_data.xml create mode 100644 data/sale_coupon_email_data.xml create mode 100644 report/__init__.py create mode 100644 report/sale_coupon_report.py create mode 100644 report/sale_coupon_report.xml create mode 100644 report/sale_coupon_report_templates.xml diff --git a/__init__.py b/__init__.py index 2ae6446f9dc..f17ad3dbaec 100644 --- a/__init__.py +++ b/__init__.py @@ -3,3 +3,4 @@ from . import models from . import wizard +from . import report diff --git a/__manifest__.py b/__manifest__.py index 690235be7c6..3471add5356 100644 --- a/__manifest__.py +++ b/__manifest__.py @@ -15,6 +15,8 @@ 'views/sale_coupon_views.xml', 'views/sale_coupon_program_views.xml', 'views/res_config_settings_views.xml', - 'data/sale_coupon_data.xml', + 'report/sale_coupon_report.xml', + 'report/sale_coupon_report_templates.xml', + 'data/sale_coupon_email_data.xml', ], } diff --git a/data/sale_coupon_data.xml b/data/sale_coupon_data.xml deleted file mode 100644 index 3ddf205d1c6..00000000000 --- a/data/sale_coupon_data.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - diff --git a/data/sale_coupon_email_data.xml b/data/sale_coupon_email_data.xml new file mode 100644 index 00000000000..4bae1fb0118 --- /dev/null +++ b/data/sale_coupon_email_data.xml @@ -0,0 +1,173 @@ + + + + + Coupon - Send by Email + ${object.program_id.company_id.email|safe} + Your reward coupon from ${object.program_id.company_id.name} + ${object.partner_id.id} + + + + Your Coupon Code + ${object.partner_id.lang} + +
+ + + + + + % if object.partner_id.name: + + + + % endif + + + + % if object.program_id.reward_type == 'discount': + % if object.program_id.discount_type == 'fixed_amount': + + + + + + + %else + + + + % if object.program_id.discount_apply_on == 'specific_product': + + + + % elif object.program_id.discount_apply_on == 'cheapest_product': + + + + % else + + + + % endif + % endif + % elif object.program_id.reward_type == 'product': + + + + % elif object.program_id.reward_type == 'free_shipping': + + + + % endif + + + + + + + + + + + + + + + + +
+ ${object.program_id.company_id.name} +
+ + Congratulations ${object.partner_id.name}, + +
+
Here is your reward from ${object.program_id.company_id.name}.
+

+
+
+ ${'%s' % (format_amount(object.program_id.discount_fixed_amount, object.program_id.currency_id))} +
+
+
+ OFF ON YOUR NEXT ORDER ! +
+
+
+ ${object.program_id.discount_percentage} % +
+
+
+ ${'ON %s' % (object.program_id.discount_specific_product_id.name.upper())} +
+
+
+ OFF ON THE CHEAPEST PRODUCT ! +
+
+
+ OFF ON YOUR NEXT ORDER ! +
+
+
+ ${'GET %s FREE %s ON YOUR NEXT ORDER !' % (object.program_id.reward_product_quantity, object.program_id.reward_product_id.name.upper())} +
+
+
+ ${'GET FREE SHIPPING ON YOUR NEXT ORDER !'} +
+
+ + + Use this promo code + + % if object.expiration_date: + + before ${object.expiration_date} + + % endif + +
+ + CODE: + + ${object.code} + + +
+ % if object.program_id.rule_min_quantity not in [0, 1] + + + Minimum purchase of ${object.program_id.rule_min_quantity} products + + + % endif +
+ % if object.program_id.rule_minimum_amount != 0.00 + + + Valid for purchase above ${object.program_id.company_id.currency_id.symbol}${'%0.2f' % object.program_id.rule_minimum_amount |float} + + + % endif +
+ % if object.program_id.company_id.name: + + + ${object.program_id.company_id.name} + + +
+

+ Copyright © All Rights Reserved +

+
+ % endif +
+
+
+
+
+
\ No newline at end of file diff --git a/models/sale_coupon.py b/models/sale_coupon.py index 601389227b5..58500211b47 100644 --- a/models/sale_coupon.py +++ b/models/sale_coupon.py @@ -81,3 +81,30 @@ class SaleCoupon(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 + + @api.multi + 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('sale_coupon.mail_template_sale_coupon', False) + compose_form = self.env.ref('mail.email_compose_message_wizard_form', False) + ctx = dict( + default_model='sale.coupon', + default_res_id=self.id, + default_use_template=bool(template), + default_template_id=template.id, + default_composition_mode='comment', + ) + return { + 'name': _('Compose Email'), + 'type': 'ir.actions.act_window', + 'view_type': 'form', + 'view_mode': 'form', + 'res_model': 'mail.compose.message', + 'views': [(compose_form.id, 'form')], + 'view_id': compose_form.id, + 'target': 'new', + 'context': ctx, + } diff --git a/models/sale_order.py b/models/sale_order.py index 9ce09c452e2..8af60935161 100644 --- a/models/sale_order.py +++ b/models/sale_order.py @@ -155,12 +155,9 @@ class SaleOrder(models.Model): 'discount_line_product_id': program.discount_line_product_id.id }) self.generated_coupon_ids |= coupon - subject = '%s, a coupon has been generated from your order %s' % (self.partner_id.name, self.name) - body = self.env.ref('sale_coupon.sale_coupon_created_coupon_email_template').render({ - 'code': coupon.code, - 'reward_description': coupon.program_id.discount_line_product_id.name - }) - self.message_post(body=body, subject=subject, subtype='mail.mt_comment', partner_ids=[(4, self.partner_id.id)]) + template = self.env.ref('sale_coupon.mail_template_sale_coupon', raise_if_not_found=False) + if template: + self.message_post_with_template(template.id, composition_mode='comment') return coupon def _get_applicable_programs(self): diff --git a/report/__init__.py b/report/__init__.py new file mode 100644 index 00000000000..a6332563645 --- /dev/null +++ b/report/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from . import sale_coupon_report diff --git a/report/sale_coupon_report.py b/report/sale_coupon_report.py new file mode 100644 index 00000000000..1b6a5b89937 --- /dev/null +++ b/report/sale_coupon_report.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from odoo import api, models + + +class CouponReport(models.AbstractModel): + _name = 'report.sale_coupon.report_coupon' + + @api.model + def get_report_values(self, docids, data=None): + docs = self.env['sale.coupon'].browse(docids) + return { + 'doc_ids': docs.ids, + 'doc_model': 'sale.coupon', + 'data': data, + 'docs': docs, + } diff --git a/report/sale_coupon_report.xml b/report/sale_coupon_report.xml new file mode 100644 index 00000000000..d44011e2772 --- /dev/null +++ b/report/sale_coupon_report.xml @@ -0,0 +1,12 @@ + + + + + + diff --git a/report/sale_coupon_report_templates.xml b/report/sale_coupon_report_templates.xml new file mode 100644 index 00000000000..9cbc4c51820 --- /dev/null +++ b/report/sale_coupon_report_templates.xml @@ -0,0 +1,77 @@ + + + + + + diff --git a/views/sale_coupon_views.xml b/views/sale_coupon_views.xml index 5ef1d81d7f7..50f5ac2d81d 100644 --- a/views/sale_coupon_views.xml +++ b/views/sale_coupon_views.xml @@ -30,6 +30,7 @@
+
diff --git a/wizard/sale_coupon_generate.py b/wizard/sale_coupon_generate.py index 79bfc52abcb..51f6fad9afd 100644 --- a/wizard/sale_coupon_generate.py +++ b/wizard/sale_coupon_generate.py @@ -31,13 +31,6 @@ class SaleCouponGenerate(models.TransientModel): vals.update({'partner_id': partner.id}) coupon = self.env['sale.coupon'].create(vals) subject = '%s, a coupon has been generated for you' % (partner.name) - body = self.env.ref('sale_coupon.sale_coupon_created_coupon_email_template').render({ - 'code': coupon.code, - 'reward_description': coupon.program_id.discount_line_product_id.name - }) - self.env['mail.mail'].create({ - 'subject': subject, - 'body': body, - 'email_from': self.env.user.email or '', - 'email_to': partner.email, - }) + template = self.env.ref('sale_coupon.mail_template_sale_coupon', raise_if_not_found=False) + if template: + template.send_mail(coupon.id, email_values={'email_to': partner.email, 'email_from': self.env.user.email or '', 'subject': subject,})