diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-09-23 11:50:20 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-09-23 11:50:20 +0700 |
| commit | dae4a3bf266ba4c19b1ba1d11c52ed9e19259b7c (patch) | |
| tree | 8858e0660aa004b582aedf6f437c0f455f8ad1ef /indoteknik_custom/models/promotion_program_line.py | |
| parent | adbc9b985f1c5fb2b2f41f79c686b3a573003e62 (diff) | |
Update promotion program scheme
Diffstat (limited to 'indoteknik_custom/models/promotion_program_line.py')
| -rw-r--r-- | indoteknik_custom/models/promotion_program_line.py | 171 |
1 files changed, 0 insertions, 171 deletions
diff --git a/indoteknik_custom/models/promotion_program_line.py b/indoteknik_custom/models/promotion_program_line.py deleted file mode 100644 index 71418d6c..00000000 --- a/indoteknik_custom/models/promotion_program_line.py +++ /dev/null @@ -1,171 +0,0 @@ -from odoo import fields, models, api -from datetime import datetime -import math - - -class PromotionProgramLine(models.Model): - _name = "promotion.program.line" - - name = fields.Char(string="Name") - image = fields.Binary(string="Image") - product_id = fields.Many2one(comodel_name="product.product", string="Product Variant") - program_id = fields.Many2one(comodel_name="promotion.program", string="Program") - discount_type = fields.Selection(selection=[ - ("percentage", "Percentage"), - ("fixed_price", "Fixed Price"), - ], string="Discount Type") - discount_amount = fields.Float(string="Discount Amount") - 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") - display_on_homepage = fields.Boolean(string="Display on Homepage") - order_line_ids = fields.One2many('sale.order.line', 'program_line_id') - - @api.onchange('product_id') - def _onchange_product_id(self): - if self.product_id and not self.name: - self.name = self.product_id.display_name - self._discount_loading_auto() - - @api.onchange('promotion_type') - def onchange_promotion_type(self): - self._discount_loading_auto() - - def _discount_loading_auto(self): - program_line = self - product = program_line.product_id - promotion_type = program_line.promotion_type - - if promotion_type != 'discount_loading' or not product: - return - - line = self.browse(self.ids) - line.product_id = self.product_id.id - line.promotion_type = self.promotion_type - - product_added = False - line_free_item = program_line.line_free_item - for line in line_free_item: - if line.product_id.id == product.id: - product_added = True - continue - line.unlink() - if not product_added: - data = {'product_id': product.id, - 'qty': 1, 'line_id': program_line.id} - line_free_item.create(data) - - def calculate_price(self, price): - initial_price = price['price'] - if self.discount_type == 'percentage': - price['discount_percentage'] = self.discount_amount - price['price_discount'] = initial_price - (initial_price * self.discount_amount / 100) - elif self.discount_type == 'fixed_price': - price['price_discount'] = self.discount_amount - price['discount_percentage'] = round((initial_price - self.discount_amount) / initial_price * 100) - return price - - def get_active_promotions(self, product_id): - current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') - return self.search([ - ('program_id.start_time', '<=', current_time), - ('program_id.end_time', '>=', current_time), - ('product_id', '=', product_id) - ]) - - def _get_remaining_qty(self, user): - remaining_qty = { - 'all': self.limit_qty, - 'user': self.limit_qty_user, - 'transaction': self.limit_qty_transaction - } - for order in self.order_line_ids: - parent_order = order.order_id - if parent_order.state != 'cancel': - remaining_qty['all'] -= order.product_uom_qty - if user and parent_order.partner_id.id == user['partner_id']: - remaining_qty['user'] -= order.product_uom_qty - - if remaining_qty['all'] < remaining_qty['user']: - remaining_qty['user'] = remaining_qty['all'] - if remaining_qty['user'] < remaining_qty['transaction']: - remaining_qty['transaction'] = remaining_qty['user'] - - return remaining_qty - - def _get_remaining_time(self): - calculate_time = self.program_id.end_time - datetime.now() - return round(calculate_time.total_seconds()) - - def _res_limit_qty(self): - return { - 'all': self.limit_qty, - 'user': self.limit_qty_user, - 'transaction': self.limit_qty_transaction, - } - - def _res_promotion_type(self): - return { - 'value': self.promotion_type, - 'label': dict(self._fields['promotion_type'].selection).get(self.promotion_type) - } - - 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() - remaining_qty = self._get_remaining_qty(user) - percent_remaining = 0 - if limit_qty['all'] > 0: - percent_remaining = (limit_qty['all'] - remaining_qty['all']) / limit_qty['all'] * 100 - - 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), - 'minimum_purchase_qty': self.minimum_purchase_qty, - 'applies_multiply': self.applies_multiply, - 'remaining_time': self._get_remaining_time(), - 'type': self._res_promotion_type(), - 'limit_qty': limit_qty, - 'remaining_qty': remaining_qty, - 'used_percentage': percent_remaining, - '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 - |
