diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-09-18 16:20:15 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-09-18 16:20:15 +0700 |
| commit | adbc9b985f1c5fb2b2f41f79c686b3a573003e62 (patch) | |
| tree | 27ca97d7c6540ccaf3556efadb9c8b439ac2f2fb /indoteknik_custom/models/promotion_program_line.py | |
| parent | 0ace4356bdbe27c3acd75c33d5259ef950eecb24 (diff) | |
Update promotion program feature
Diffstat (limited to 'indoteknik_custom/models/promotion_program_line.py')
| -rw-r--r-- | indoteknik_custom/models/promotion_program_line.py | 53 |
1 files changed, 37 insertions, 16 deletions
diff --git a/indoteknik_custom/models/promotion_program_line.py b/indoteknik_custom/models/promotion_program_line.py index 077f7e12..71418d6c 100644 --- a/indoteknik_custom/models/promotion_program_line.py +++ b/indoteknik_custom/models/promotion_program_line.py @@ -1,5 +1,6 @@ from odoo import fields, models, api from datetime import datetime +import math class PromotionProgramLine(models.Model): @@ -14,15 +15,21 @@ class PromotionProgramLine(models.Model): ("fixed_price", "Fixed Price"), ], string="Discount Type") discount_amount = fields.Float(string="Discount Amount") - promotion_type = fields.Selection(selection=[ - ("special_price", "Special Price"), - ("bundling", "Bundling"), - ("discount_loading", "Discount Loading"), - ("merchandise", "Merchandise") - ], string="Promotion Type") - minimum_purchase_qty = fields.Integer(string="Minimum Purchase Qty", help="Minimum Qty to applied discount loading") - applies_multiply = fields.Boolean(string="Applies Multiply", help="Is applies multiply") - limit_qty = fields.Integer(string="Limit Qty", help="Limit Qty product in promotion") + promotion_type = fields.Selection(string="Promotion Type", + selection=[ + ("special_price", "Special Price"), + ("bundling", "Bundling"), + ("discount_loading", "Discount Loading"), + ("merchandise", "Merchandise") + ], + help='- Special Price: Potongan harga barang.\n' + '- Bundling: Menggabungkan beberapa produk menjadi satu paket.\n' + '- Discount Loading: Semakin banyak unit yang dibeli maka semakin murah harga yang dibayar.\n' + '- Merchandise: Pemberian barang gratis.' + ) + minimum_purchase_qty = fields.Integer(string="Minimum Purchase Qty", help="Minimum unit barang yang perlu dibeli untuk dapat menggunakan promosi") + applies_multiply = fields.Boolean(string="Applies Multiply", help="Diterapkan berlipat ganda") + limit_qty = fields.Integer(string="Limit Qty", help="Kuota promosi keseluruhan") limit_qty_user = fields.Integer(string="Limit Qty / User", help="Limit Qty per User") limit_qty_transaction = fields.Integer(string="Limit Qty / Transaction", help="Limit Qty per Transaction") line_free_item = fields.One2many(comodel_name="promotion.program.free_item", inverse_name="line_id", string="Line Free Item") @@ -118,7 +125,7 @@ class PromotionProgramLine(models.Model): 'label': dict(self._fields['promotion_type'].selection).get(self.promotion_type) } - def format(self, user = None): + def format(self, user = None, qty = 0): ir_attachment = self.env['ir.attachment'] product_price = self.product_id.calculate_website_price() limit_qty = self._res_limit_qty() @@ -126,7 +133,19 @@ class PromotionProgramLine(models.Model): percent_remaining = 0 if limit_qty['all'] > 0: percent_remaining = (limit_qty['all'] - remaining_qty['all']) / limit_qty['all'] * 100 - return { + + qty_can_buy = min(remaining_qty['user'], qty) + if self.limit_qty_transaction > 0: + qty_can_buy = min(qty_can_buy, self.limit_qty_transaction) + + multiplier = 0 + if qty_can_buy > self.minimum_purchase_qty: + multiplier = 1 + + if self.applies_multiply and qty_can_buy > 0: + multiplier = math.floor(qty_can_buy / self.minimum_purchase_qty) + + response = { 'id': self.id, 'name': self.name, 'image': ir_attachment.api_image('promotion.program.line', 'image', self.id), @@ -137,14 +156,16 @@ class PromotionProgramLine(models.Model): 'limit_qty': limit_qty, 'remaining_qty': remaining_qty, 'used_percentage': percent_remaining, - 'price': self.calculate_price(price=product_price) + 'price': self.calculate_price(price=product_price), + 'items': [{ + 'product_id': line.product_id.id, + 'quantity': line.qty * multiplier + } for line in self.line_free_item] } + + return response def res_format(self, user): data = [x.format(user) for x in self] return data - - def res_format_cart(self, user): - data = self.format(user) - return data |
