diff options
| author | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
|---|---|---|
| committer | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
| commit | 3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch) | |
| tree | a44932296ef4a9b71d5f010906253d8c53727726 /addons/coupon/wizard | |
| parent | 0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff) | |
initial commit 2
Diffstat (limited to 'addons/coupon/wizard')
| -rw-r--r-- | addons/coupon/wizard/__init__.py | 4 | ||||
| -rw-r--r-- | addons/coupon/wizard/coupon_generate.py | 48 | ||||
| -rw-r--r-- | addons/coupon/wizard/coupon_generate_views.xml | 32 |
3 files changed, 84 insertions, 0 deletions
diff --git a/addons/coupon/wizard/__init__.py b/addons/coupon/wizard/__init__.py new file mode 100644 index 00000000..0eb266ee --- /dev/null +++ b/addons/coupon/wizard/__init__.py @@ -0,0 +1,4 @@ +# -*- coding: utf-8 -*- +# Part of Odoo. See LICENSE file for full copyright and licensing details. + +from . import coupon_generate diff --git a/addons/coupon/wizard/coupon_generate.py b/addons/coupon/wizard/coupon_generate.py new file mode 100644 index 00000000..ac7df482 --- /dev/null +++ b/addons/coupon/wizard/coupon_generate.py @@ -0,0 +1,48 @@ +# -*- coding: utf-8 -*- + +from odoo import _, api, fields, models + +import ast +from odoo.osv import expression + + +class CouponGenerate(models.TransientModel): + _name = 'coupon.generate.wizard' + _description = 'Generate Coupon' + + nbr_coupons = fields.Integer(string="Number of Coupons", help="Number of coupons", default=1) + generation_type = fields.Selection([ + ('nbr_coupon', 'Number of Coupons'), + ('nbr_customer', 'Number of Selected Customers') + ], default='nbr_coupon') + partners_domain = fields.Char(string="Customer", default='[]') + has_partner_email = fields.Boolean(compute='_compute_has_partner_email') + + def generate_coupon(self): + """Generates the number of coupons entered in wizard field nbr_coupons + """ + program = self.env['coupon.program'].browse(self.env.context.get('active_id')) + + vals = {'program_id': program.id} + + if self.generation_type == 'nbr_coupon' and self.nbr_coupons > 0: + for count in range(0, self.nbr_coupons): + self.env['coupon.coupon'].create(vals) + + if self.generation_type == 'nbr_customer' and self.partners_domain: + for partner in self.env['res.partner'].search(ast.literal_eval(self.partners_domain)): + vals.update({'partner_id': partner.id, 'state': 'sent' if partner.email else 'new'}) + coupon = self.env['coupon.coupon'].create(vals) + context = dict(lang=partner.lang) + subject = _('%s, a coupon has been generated for you') % (partner.name) + del context + template = self.env.ref('coupon.mail_template_sale_coupon', raise_if_not_found=False) + if template: + email_values = {'email_from': self.env.user.email or '', 'subject': subject} + template.send_mail(coupon.id, email_values=email_values, notif_layout='mail.mail_notification_light') + + @api.depends('partners_domain') + def _compute_has_partner_email(self): + for record in self: + domain = expression.AND([ast.literal_eval(record.partners_domain), [('email', '=', False)]]) + record.has_partner_email = self.env['res.partner'].search_count(domain) == 0 diff --git a/addons/coupon/wizard/coupon_generate_views.xml b/addons/coupon/wizard/coupon_generate_views.xml new file mode 100644 index 00000000..57d03ded --- /dev/null +++ b/addons/coupon/wizard/coupon_generate_views.xml @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="utf-8"?> +<odoo> + <record id="coupon_generate_view_form" model="ir.ui.view"> + <field name="name">coupon.generate.wizard.form</field> + <field name="model">coupon.generate.wizard</field> + <field name="arch" type="xml"> + <form string="Generate Coupons"> + <group> + <field name="has_partner_email" invisible="1"/> + <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')]}"/> + </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. + </div> + <footer> + <button name="generate_coupon" type="object" string="Generate" class="oe_highlight"/> + <button special="cancel" string="Cancel"/> + </footer> + </form> + </field> + </record> + + <record id="coupon_generate_action" model="ir.actions.act_window"> + <field name="name">Number of Coupons To Generate</field> + <field name="res_model">coupon.generate.wizard</field> + <field name="view_mode">form</field> + <field name="target">new</field> + <field name="view_id" ref="coupon_generate_view_form"/> + </record> +</odoo> |
