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/sale_coupon/wizard | |
| parent | 0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff) | |
initial commit 2
Diffstat (limited to 'addons/sale_coupon/wizard')
| -rw-r--r-- | addons/sale_coupon/wizard/__init__.py | 4 | ||||
| -rw-r--r-- | addons/sale_coupon/wizard/sale_coupon_apply_code.py | 54 | ||||
| -rw-r--r-- | addons/sale_coupon/wizard/sale_coupon_apply_code_views.xml | 28 |
3 files changed, 86 insertions, 0 deletions
diff --git a/addons/sale_coupon/wizard/__init__.py b/addons/sale_coupon/wizard/__init__.py new file mode 100644 index 00000000..5de28877 --- /dev/null +++ b/addons/sale_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 sale_coupon_apply_code diff --git a/addons/sale_coupon/wizard/sale_coupon_apply_code.py b/addons/sale_coupon/wizard/sale_coupon_apply_code.py new file mode 100644 index 00000000..6eb8aaf2 --- /dev/null +++ b/addons/sale_coupon/wizard/sale_coupon_apply_code.py @@ -0,0 +1,54 @@ +# -*- coding: utf-8 -*- + +from odoo import api, fields, models, _ +from odoo.exceptions import UserError + + +class SaleCouponApplyCode(models.TransientModel): + _name = 'sale.coupon.apply.code' + _rec_name = 'coupon_code' + _description = 'Sales Coupon Apply Code' + + coupon_code = fields.Char(string="Code", required=True) + + def process_coupon(self): + """ + Apply the entered coupon code if valid, raise an UserError otherwise. + """ + sales_order = self.env['sale.order'].browse(self.env.context.get('active_id')) + error_status = self.apply_coupon(sales_order, self.coupon_code) + if error_status.get('error', False): + raise UserError(error_status.get('error', False)) + if error_status.get('not_found', False): + raise UserError(error_status.get('not_found', False)) + + def apply_coupon(self, order, coupon_code): + error_status = {} + program = self.env['coupon.program'].search([('promo_code', '=', coupon_code)]) + if program: + error_status = program._check_promo_code(order, coupon_code) + if not error_status: + if program.promo_applicability == 'on_next_order': + # Avoid creating the coupon if it already exist + if program.discount_line_product_id.id not in order.generated_coupon_ids.filtered(lambda coupon: coupon.state in ['new', 'reserved']).mapped('discount_line_product_id').ids: + coupon = order._create_reward_coupon(program) + return { + 'generated_coupon': { + 'reward': coupon.program_id.discount_line_product_id.name, + 'code': coupon.code, + } + } + else: # The program is applied on this order + order._create_reward_line(program) + order.code_promo_program_id = program + else: + coupon = self.env['coupon.coupon'].search([('code', '=', coupon_code)], limit=1) + if coupon: + error_status = coupon._check_coupon_code(order) + if not error_status: + order._create_reward_line(coupon.program_id) + order.applied_coupon_ids += coupon + coupon.write({'state': 'used'}) + else: + error_status = {'not_found': _('This coupon is invalid (%s).') % (coupon_code)} + return error_status diff --git a/addons/sale_coupon/wizard/sale_coupon_apply_code_views.xml b/addons/sale_coupon/wizard/sale_coupon_apply_code_views.xml new file mode 100644 index 00000000..bc146e58 --- /dev/null +++ b/addons/sale_coupon/wizard/sale_coupon_apply_code_views.xml @@ -0,0 +1,28 @@ +<?xml version="1.0" encoding="utf-8"?> +<odoo> + <record id="sale_coupon_apply_code_view_form" model="ir.ui.view"> + <field name="name">sale.coupon.apply.code.form</field> + <field name="model">sale.coupon.apply.code</field> + <field name="arch" type="xml"> + <form string="Apply coupon"> + <group> + <group> + <field name="coupon_code"/> + </group> + </group> + <footer> + <button name="process_coupon" string="Apply" type="object" class="oe_highlight"/> + <button special="cancel" string="Cancel"/> + </footer> + </form> + </field> + </record> + + <record id="sale_coupon_apply_code_action" model="ir.actions.act_window"> + <field name="name">Enter Promotion or Coupon Code</field> + <field name="res_model">sale.coupon.apply.code</field> + <field name="view_mode">form</field> + <field name="target">new</field> + <field name="view_id" ref="sale_coupon_apply_code_view_form"/> + </record> +</odoo> |
