From 697bb1ffeaee2374ac39aa1c838958b6dfae40fd Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Thu, 31 Aug 2023 16:07:43 +0700 Subject: Update voucher model - Add voucher line model - Add voucher line on voucher view - Add voucher relation to voucher line --- indoteknik_custom/models/voucher.py | 99 +++++++++++++++++++++++++++++--- indoteknik_custom/models/voucher_line.py | 20 +++++++ indoteknik_custom/views/voucher.xml | 29 ++++++---- 3 files changed, 129 insertions(+), 19 deletions(-) create mode 100644 indoteknik_custom/models/voucher_line.py diff --git a/indoteknik_custom/models/voucher.py b/indoteknik_custom/models/voucher.py index a7151398..485fd0b9 100644 --- a/indoteknik_custom/models/voucher.py +++ b/indoteknik_custom/models/voucher.py @@ -13,22 +13,20 @@ class Voucher(models.Model): code = fields.Char(string='Code', help='Kode voucher yang akan berlaku untuk pengguna') description = fields.Text(string='Description') discount_amount = fields.Integer(string='Discount Amount') - discount_type = fields.Selection( + discount_type = fields.Selection(string='Discount Type', selection=[ ('percentage', 'Percentage'), ('fixed_price', 'Fixed Price'), - ], - string='Discount Type', + ], help='Select the type of discount:\n' - '- Percentage: Persentage dari total harga.\n' + '- Percentage: Persentase dari total harga.\n' '- Fixed Price: Jumlah tetap yang dikurangi dari harga total.' ) - visibility = fields.Selection( + visibility = fields.Selection(string='Visibility', selection=[ ('public', 'Public'), ('private', 'Private') ], - string='Visibility', help='Select the visibility:\n' '- Public: Ditampilkan kepada seluruh pengguna.\n' '- Private: Tidak ditampilkan kepada seluruh pengguna.' @@ -41,6 +39,11 @@ class Voucher(models.Model): 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') excl_pricelist_ids = fields.Many2many('product.pricelist', string='Excluded Pricelists', help='Hide voucher from selected exclude pricelist') + voucher_line = fields.One2many('voucher.line', 'voucher_id', 'Voucher Line') + apply_type = fields.Selection(string='Apply Type', default="all", selection=[ + ('all', "All product"), + ('brand', "Selected product brand"), + ]) @api.constrains('description') def _check_description_length(self): @@ -97,6 +100,84 @@ class Voucher(models.Model): calculate_time = self.end_time - datetime.now() return round(calculate_time.total_seconds()) + def filter_order_line(self, order_line): + if self.apply_type == 'all': + return order_line + + voucher_manufacture_ids = self.collect_manufacture_ids() + results = [] + for line in order_line: + manufacture_id = line['product_id'].x_manufacture.id or None + if manufacture_id not in voucher_manufacture_ids: + continue + + product_flashsale = line['product_id']._get_active_flash_sale() + if len(product_flashsale) > 0: + continue + + results.append(line) + + return results + + def calc_total_order_line(self, order_line): + result = { 'all': 0, 'brand': {} } + for line in order_line: + manufacture_id = line['product_id'].x_manufacture.id or None + manufacture_total = result['brand'].get(manufacture_id, 0) + result['brand'][manufacture_id] = manufacture_total + line['subtotal'] + result['all'] += line['subtotal'] + + return result + + def calc_discount_amount(self, total): + result = { 'all': 0, 'brand': {} } + + if self.apply_type == 'all': + if total['all'] < self.min_purchase_amount: + return result + + if self.discount_type == 'percentage': + decimal_discount = self.discount_amount / 100 + discount_all = total['all'] * decimal_discount + result['all'] = min(discount_all, self.max_discount_amount) if self.max_discount_amount > 0 else discount_all + else: + result['all'] = self.discount_amount + + return result + + for line in self.voucher_line: + manufacture_id = line.manufacture_id.id + total_brand = total['brand'].get(manufacture_id, 0) + + discount_brand = 0 + if total_brand < line.min_purchase_amount: + discount_brand = 0 + elif line.discount_type == 'percentage': + decimal_discount = line.discount_amount / 100 + discount_brand = total_brand * decimal_discount + discount_brand = min(discount_brand, line.max_discount_amount) if line.max_discount_amount > 0 else discount_brand + else: + discount_brand = line.discount_amount + + result['brand'][manufacture_id] = discount_brand + result['all'] += discount_brand + + return result + + def apply(self, order_line): + order_line = self.filter_order_line(order_line) + amount_total = self.calc_total_order_line(order_line) + discount = self.calc_discount_amount(amount_total) + return { + 'discount': discount, + 'total': amount_total, + 'type': self.apply_type, + 'valid_order': order_line + } + + def collect_manufacture_ids(self): + return [x.manufacture_id.id for x in self.voucher_line] + def calculate_discount(self, price): if price < self.min_purchase_amount: return 0 @@ -111,11 +192,11 @@ class Voucher(models.Model): return 0 - def get_active_voucher(self, parameter): + def get_active_voucher(self, domain): current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') - parameter += [ + domain += [ ('start_time', '<=', current_time), ('end_time', '>=', current_time), ] - vouchers = self.search(parameter, order='min_purchase_amount ASC') + vouchers = self.search(domain, order='min_purchase_amount ASC') return vouchers \ No newline at end of file diff --git a/indoteknik_custom/models/voucher_line.py b/indoteknik_custom/models/voucher_line.py new file mode 100644 index 00000000..8b449d1f --- /dev/null +++ b/indoteknik_custom/models/voucher_line.py @@ -0,0 +1,20 @@ +from odoo import models, fields + + +class Voucher(models.Model): + _name = 'voucher.line' + + voucher_id = fields.Many2one('voucher', string='Voucher') + manufacture_id = fields.Many2one('x_manufactures', string='Brand') + min_purchase_amount = fields.Integer(string='Min. Purchase Amount', help='Nominal minimum untuk dapat menggunakan voucher. Isi 0 jika tidak ada minimum purchase amount') + discount_amount = fields.Integer(string='Discount Amount') + discount_type = fields.Selection(string='Discount Type', + selection=[ + ('percentage', 'Percentage'), + ('fixed_price', 'Fixed Price'), + ], + help='Select the type of discount:\n' + '- Percentage: Persentase dari total harga.\n' + '- Fixed Price: Jumlah tetap yang dikurangi dari harga total.' + ) + max_discount_amount = fields.Integer(string='Max. Discount Amount', help='Max nominal terhadap persentase diskon') \ No newline at end of file diff --git a/indoteknik_custom/views/voucher.xml b/indoteknik_custom/views/voucher.xml index 7b181c62..eb74f27d 100755 --- a/indoteknik_custom/views/voucher.xml +++ b/indoteknik_custom/views/voucher.xml @@ -28,8 +28,15 @@ + + + + + + + - + @@ -52,17 +59,19 @@ - - - - - - - - - + + + + + + + + + + + + + + -- cgit v1.2.3 From 1d60288adef95392dfbfc2f170409a611be526ea Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Thu, 7 Sep 2023 15:49:26 +0700 Subject: Update limit on voucher model --- indoteknik_custom/models/voucher.py | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/indoteknik_custom/models/voucher.py b/indoteknik_custom/models/voucher.py index b8d0dc2e..720c465e 100644 --- a/indoteknik_custom/models/voucher.py +++ b/indoteknik_custom/models/voucher.py @@ -36,8 +36,17 @@ class Voucher(models.Model): end_time = fields.Datetime(string='End Time') 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') + order_ids = fields.One2many('sale.order', 'applied_voucher_id', string='Order') + limit = fields.Integer( + string='Limit', + default=0, + help='Batas penggunaan voucher keseluruhan. Isi dengan angka 0 untuk penggunaan tanpa batas' + ) + limit_user = fields.Integer( + string='Limit User', + default=0, + help='Batas penggunaan voucher per pengguna. Misalnya, jika diisi dengan angka 1, maka setiap pengguna hanya dapat menggunakan voucher ini satu kali. Isi dengan angka 0 untuk penggunaan tanpa batas' + ) manufacture_ids = fields.Many2many('x_manufactures', string='Brands', help='Voucher appplied only for brand') excl_pricelist_ids = fields.Many2many('product.pricelist', string='Excluded Pricelists', help='Hide voucher from selected exclude pricelist') voucher_line = fields.One2many('voucher.line', 'voucher_id', 'Voucher Line') @@ -46,22 +55,27 @@ class Voucher(models.Model): ('all', "All product"), ('brand', "Selected product brand"), ]) - can_used = fields.Boolean(string='Can Used?', compute='_compute_can_used') + count_order = fields.Integer(string='Count Order', compute='_compute_count_order') @api.constrains('description') def _check_description_length(self): for record in self: if record.description and len(record.description) > 120: - raise ValidationError('Description cannot exceed 120 characters') + raise ValidationError('Deskripsi tidak boleh lebih dari 120 karakter') + + @api.constrains('limit', 'limit_user') + def _check_limit(self): + for rec in self: + if rec.limit_user > rec.limit: + raise ValidationError('Limit user tidak boleh lebih besar dari limit keseluruhan') def _compute_display_name(self): for voucher in self: voucher.display_name = f'{voucher.name} ({voucher.code})' - def _compute_can_used(self): + def _compute_count_order(self): for rec in self: - valid_order = [x for x in rec.order_ids if x.state != 'cancel'] - rec.can_used = rec.limit <= 0 or len(valid_order) < rec.limit + rec.count_order = len([x for x in rec.order_ids if x.state != 'cancel']) def res_format(self): datas = [voucher.format() for voucher in self] -- cgit v1.2.3 From f73449303d21622036a7e69945836fbc01b8f951 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Thu, 7 Sep 2023 15:49:55 +0700 Subject: Add apply voucher validation on sale order --- indoteknik_custom/models/sale_order.py | 22 ++++++++++++++++++++++ indoteknik_custom/views/sale_order.xml | 2 +- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/indoteknik_custom/models/sale_order.py b/indoteknik_custom/models/sale_order.py index 85ef3ad8..8317e1fd 100755 --- a/indoteknik_custom/models/sale_order.py +++ b/indoteknik_custom/models/sale_order.py @@ -377,6 +377,28 @@ class SaleOrder(models.Model): order.grand_total = order.delivery_amt + order.amount_total else: order.grand_total = order.amount_total + + def action_apply_voucher(self): + for order in self.order_line: + if order.program_line_id: + raise UserError('Voucher tidak dapat digabung dengan promotion program') + + voucher = self.voucher_id + if voucher.limit > 0 and voucher.count_order >= voucher.limit: + raise UserError('Voucher tidak dapat digunakan karena sudah habis digunakan') + + partner_voucher_orders = [] + for order in voucher.order_ids: + if order.partner_id.id == self.partner_id.id: + partner_voucher_orders.append(order) + + if voucher.limit_user > 0 and len(partner_voucher_orders) >= voucher.limit_user: + raise UserError('Voucher tidak dapat digunakan karena Customer ini sudah menghabiskan kuota voucher') + + if self.pricelist_id.id in [x.id for x in voucher.excl_pricelist_ids]: + raise UserError('Voucher tidak dapat digunakan karena pricelist ini tidak berlaku pada voucher') + + self.apply_voucher() def apply_voucher(self): order_line = [] diff --git a/indoteknik_custom/views/sale_order.xml b/indoteknik_custom/views/sale_order.xml index d37b5d1e..f2cab699 100755 --- a/indoteknik_custom/views/sale_order.xml +++ b/indoteknik_custom/views/sale_order.xml @@ -30,7 +30,7 @@
-