From bb87d3f674d593b68d2cceeff0403687f930d2b8 Mon Sep 17 00:00:00 2001 From: mgh-odoo Date: Fri, 28 Jun 2019 09:07:44 +0000 Subject: [PATCH] [IMP] sale_coupon: refactor the coupon stages and usability improvement Fix the layout of logo to the printed coupon. The traceability of the coupons is not clear. for Ex. coupon is send or not? should I resend? so updated the stages of the coupon Now the stages will be like -pending(previously reserved) -- hidden by default -Valid -Sent : updated when mail is sent to customer -used -expired -cancelled : hidden by default(added button to cancel the new coupon) Cron added to expire coupon automatically. PS: model sale.coupon do not inherit the thread so instead of message_post change the state 'sent' from mail_compose_message. Task-ID: 2027296 Closes #34620 --- .../data/sale_coupon_email_data.xml | 12 ++++++++ addons/sale_coupon/models/__init__.py | 1 + addons/sale_coupon/models/sale_coupon.py | 29 +++++++++++++++---- .../sale_coupon/views/sale_coupon_views.xml | 5 ++-- .../wizard/sale_coupon_generate.py | 2 +- 5 files changed, 41 insertions(+), 8 deletions(-) diff --git a/addons/sale_coupon/data/sale_coupon_email_data.xml b/addons/sale_coupon/data/sale_coupon_email_data.xml index 9d385b9272c..6d09a51e62c 100644 --- a/addons/sale_coupon/data/sale_coupon_email_data.xml +++ b/addons/sale_coupon/data/sale_coupon_email_data.xml @@ -101,5 +101,17 @@ + + + Coupon: expire coupon based on date + + code + model.cron_expire_coupon() + + + 1 + days + -1 + \ No newline at end of file diff --git a/addons/sale_coupon/models/__init__.py b/addons/sale_coupon/models/__init__.py index 903f2686d6d..f9faf790d5c 100644 --- a/addons/sale_coupon/models/__init__.py +++ b/addons/sale_coupon/models/__init__.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. +from . import mail_compose_message from . import sale_coupon from . import sale_coupon_reward from . import sale_coupon_rules diff --git a/addons/sale_coupon/models/sale_coupon.py b/addons/sale_coupon/models/sale_coupon.py index 4a0ae195bfc..686e697f624 100644 --- a/addons/sale_coupon/models/sale_coupon.py +++ b/addons/sale_coupon/models/sale_coupon.py @@ -27,16 +27,18 @@ class SaleCoupon(models.Model): code = fields.Char(default=_generate_code, required=True, readonly=True) expiration_date = fields.Date('Expiration Date', compute='_compute_expiration_date') state = fields.Selection([ - ('reserved', 'Reserved'), + ('reserved', 'Pending'), ('new', 'Valid'), - ('used', 'Consumed'), - ('expired', 'Expired') - ], required=True, default='new') + ('sent', 'Sent'), + ('used', 'Used'), + ('expired', 'Expired'), + ('cancel', 'Cancelled') + ], required=True, default='new') partner_id = fields.Many2one('res.partner', "For Customer") program_id = fields.Many2one('sale.coupon.program', "Program") order_id = fields.Many2one('sale.order', 'Order Reference', readonly=True, help="The sales order from which coupon is generated") - sales_order_id = fields.Many2one('sale.order', 'Applied on order', readonly=True, + sales_order_id = fields.Many2one('sale.order', 'Used in', readonly=True, help="The sales order on which the coupon is applied") discount_line_product_id = fields.Many2one('product.product', related='program_id.discount_line_product_id', readonly=False, help='Product used in the sales order to apply the discount.') @@ -98,6 +100,8 @@ class SaleCoupon(models.Model): default_template_id=template.id, default_composition_mode='comment', custom_layout='mail.mail_notification_light', + mark_coupon_as_sent=True, + force_email=True, ) return { 'name': _('Compose Email'), @@ -109,3 +113,18 @@ class SaleCoupon(models.Model): 'target': 'new', 'context': ctx, } + + def action_coupon_cancel(self): + for coupon in self: + coupon.state = 'cancel' + + def cron_expire_coupon(self): + self._cr.execute(""" + SELECT C.id FROM SALE_COUPON as C + INNER JOIN SALE_COUPON_PROGRAM as P ON C.program_id = P.id + WHERE C.STATE in ('reserved', 'new', 'sent') + AND P.validity_duration > 0 + AND C.create_date + interval '1 day' * P.validity_duration < now()""") + + expired_ids = [res[0] for res in self._cr.fetchall()] + self.browse(expired_ids).write({'state': 'expired'}) diff --git a/addons/sale_coupon/views/sale_coupon_views.xml b/addons/sale_coupon/views/sale_coupon_views.xml index 2cc22c6f89b..6b7dc0e7cca 100644 --- a/addons/sale_coupon/views/sale_coupon_views.xml +++ b/addons/sale_coupon/views/sale_coupon_views.xml @@ -30,8 +30,9 @@
-
diff --git a/addons/sale_coupon/wizard/sale_coupon_generate.py b/addons/sale_coupon/wizard/sale_coupon_generate.py index cadf07e4016..08bdfffc4e1 100644 --- a/addons/sale_coupon/wizard/sale_coupon_generate.py +++ b/addons/sale_coupon/wizard/sale_coupon_generate.py @@ -42,5 +42,5 @@ class SaleCouponGenerate(models.TransientModel): @api.depends('partners_domain') def _compute_has_partner_email(self): for record in self: - domain = expression.AND([safe_eval(record.partners_domain), [('email', '=', False)]]) + domain = expression.AND([ast.literal_eval(record.partners_domain), [('email', '=', False)]]) record.has_partner_email = self.env['res.partner'].search_count(domain) == 0