diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-07-21 11:06:58 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-07-21 11:06:58 +0700 |
| commit | da2389775171d288b96958651a45f07865a2e014 (patch) | |
| tree | fe31280f59adf1718bd8e43e542bccd846d556a9 | |
| parent | 7489d3fba967e711cb0fdfd6d3cb5097751e883f (diff) | |
Update voucher use manufacture rules
| -rw-r--r-- | indoteknik_api/controllers/api_v1/voucher.py | 29 | ||||
| -rw-r--r-- | indoteknik_custom/models/voucher.py | 18 |
2 files changed, 43 insertions, 4 deletions
diff --git a/indoteknik_api/controllers/api_v1/voucher.py b/indoteknik_api/controllers/api_v1/voucher.py index 5cdafba4..96c52e1e 100644 --- a/indoteknik_api/controllers/api_v1/voucher.py +++ b/indoteknik_api/controllers/api_v1/voucher.py @@ -25,14 +25,36 @@ class Voucher(controller.Controller): checkout = cart.get_user_checkout(user_id) for voucher in vouchers: - subtotal = checkout['subtotal'] + apply_status = '' + products = checkout['products'] min_purchase_amount = voucher['min_purchase_amount'] max_discount_amount = voucher['max_discount_amount'] discount_type = voucher['discount_type'] discount_amount = voucher['discount_amount'] - can_apply = subtotal >= min_purchase_amount + can_apply = True difference_to_apply = 0 - if not can_apply: + + manufacture_ids = [x['id'] for x in voucher['manufactures']] + subtotal = 0 + has_match_manufacture = False + for product in products: + price = product['price']['price'] + price_discount = product['price']['price_discount'] + quantity = product['quantity'] + manufacture_id = product['manufacture']['id'] or False + + if len(manufacture_ids) == 0 or manufacture_id in manufacture_ids: + purchase_amt = price * quantity + discount_amt = (price - price_discount) * quantity + subtotal += purchase_amt - discount_amt + has_match_manufacture = True + + if not has_match_manufacture: + can_apply = False + apply_status = 'UM' + elif subtotal < min_purchase_amount: + can_apply = False + apply_status = 'MPA' difference_to_apply = min_purchase_amount - subtotal discount_voucher = 0 @@ -45,6 +67,7 @@ class Voucher(controller.Controller): discount_voucher = max_discount_amount voucher['can_apply'] = can_apply + voucher['apply_status'] = apply_status voucher['discount_voucher'] = discount_voucher voucher['difference_to_apply'] = difference_to_apply diff --git a/indoteknik_custom/models/voucher.py b/indoteknik_custom/models/voucher.py index b327df6d..1cdf88c4 100644 --- a/indoteknik_custom/models/voucher.py +++ b/indoteknik_custom/models/voucher.py @@ -1,5 +1,6 @@ -from odoo import models, fields +from odoo import models, fields, api from datetime import datetime, timedelta +from odoo.exceptions import ValidationError class Voucher(models.Model): @@ -37,6 +38,14 @@ class Voucher(models.Model): min_purchase_amount = fields.Integer(string='Min. Purchase Amount', help='Nominal minimum untuk dapat menggunakan voucher. Isi 0 jika tidak ada minimum purchase amount') max_discount_amount = fields.Integer(string='Max. Discount Amount', help='Max nominal terhadap persentase diskon') order_ids = fields.One2many('sale.order', 'voucher_id', string='Order') + limit = fields.Integer(string='Limit', help='Voucher limit by sale order. Masukan 0 untuk tanpa limit') + manufacture_ids = fields.Many2many('x_manufactures', string='Brands', help='Voucher appplied only for brand') + + @api.constrains('description') + def _check_description_length(self): + for record in self: + if len(record.description) > 120: + raise ValidationError('Description cannot exceed 120 characters') def _compute_display_name(self): for voucher in self: @@ -50,6 +59,12 @@ class Voucher(models.Model): ir_attachment = self.env['ir.attachment'] discount_type = self.discount_type max_discount_amount = self.max_discount_amount if discount_type == 'percentage' else 0 + manufactures = [] + for manufacture in self.manufacture_ids: + manufactures.append({ + 'id': manufacture.id, + 'name': manufacture.x_name, + }) data = { 'id': self.id, 'image': ir_attachment.api_image('voucher', 'image', self.id), @@ -61,6 +76,7 @@ class Voucher(models.Model): 'remaining_time': self._res_remaining_time(), 'min_purchase_amount': self.min_purchase_amount, 'max_discount_amount': max_discount_amount, + 'manufactures': manufactures } return data |
