summaryrefslogtreecommitdiff
path: root/addons/website_sale_coupon/models/sale_coupon_program.py
diff options
context:
space:
mode:
authorstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
committerstephanchrst <stephanchrst@gmail.com>2022-05-10 21:51:50 +0700
commit3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch)
treea44932296ef4a9b71d5f010906253d8c53727726 /addons/website_sale_coupon/models/sale_coupon_program.py
parent0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff)
initial commit 2
Diffstat (limited to 'addons/website_sale_coupon/models/sale_coupon_program.py')
-rw-r--r--addons/website_sale_coupon/models/sale_coupon_program.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/addons/website_sale_coupon/models/sale_coupon_program.py b/addons/website_sale_coupon/models/sale_coupon_program.py
new file mode 100644
index 00000000..b55d2a0e
--- /dev/null
+++ b/addons/website_sale_coupon/models/sale_coupon_program.py
@@ -0,0 +1,36 @@
+# -*- coding: utf-8 -*-
+# Part of Odoo. See LICENSE file for full copyright and licensing details.
+
+from odoo import api, models, _
+from odoo.exceptions import ValidationError
+
+
+class CouponProgram(models.Model):
+ _name = 'coupon.program'
+ _inherit = ['coupon.program', 'website.multi.mixin']
+
+ @api.constrains('promo_code', 'website_id')
+ def _check_promo_code_constraint(self):
+ """ Only case where multiple same code could coexists is if they all belong to their own website.
+ If the program is website generic, we should ensure there is no generic and no specific (even for other website) already
+ If the program is website specific, we should ensure there is no existing code for this website or False
+ """
+ for program in self.filtered(lambda p: p.promo_code):
+ domain = [('id', '!=', program.id), ('promo_code', '=', program.promo_code)]
+ if program.website_id:
+ domain += program.website_id.website_domain()
+ if self.search(domain):
+ raise ValidationError(_('The program code must be unique by website!'))
+
+ def _filter_programs_on_website(self, order):
+ return self.filtered(lambda program: not program.website_id or program.website_id.id == order.website_id.id)
+
+ @api.model
+ def _filter_programs_from_common_rules(self, order, next_order=False):
+ programs = self._filter_programs_on_website(order)
+ return super(CouponProgram, programs)._filter_programs_from_common_rules(order, next_order)
+
+ def _check_promo_code(self, order, coupon_code):
+ if self.website_id and self.website_id != order.website_id:
+ return {'error': 'This promo code is not valid on this website.'}
+ return super()._check_promo_code(order, coupon_code)