summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/promotion/promotion_program_line.py
diff options
context:
space:
mode:
authorAzka Nathan <darizkyfaz@gmail.com>2023-09-26 15:07:27 +0700
committerAzka Nathan <darizkyfaz@gmail.com>2023-09-26 15:07:27 +0700
commitc86979fd504ee06dc19c69797159a13295b0c809 (patch)
treef271661fbc2947073d48cc67c3eb4901692d1643 /indoteknik_custom/models/promotion/promotion_program_line.py
parent01f308991afffaff5eda1b758dbb98d0f3ba8396 (diff)
parentd324fdd8ea3b14c966510bde610a96c8f5c3e3c5 (diff)
Merge branch 'production' of bitbucket.org:altafixco/indoteknik-addons into production
Diffstat (limited to 'indoteknik_custom/models/promotion/promotion_program_line.py')
-rw-r--r--indoteknik_custom/models/promotion/promotion_program_line.py134
1 files changed, 134 insertions, 0 deletions
diff --git a/indoteknik_custom/models/promotion/promotion_program_line.py b/indoteknik_custom/models/promotion/promotion_program_line.py
new file mode 100644
index 00000000..34a0fbb2
--- /dev/null
+++ b/indoteknik_custom/models/promotion/promotion_program_line.py
@@ -0,0 +1,134 @@
+from odoo import models, fields
+from datetime import datetime
+
+
+class PromotionProgramLine(models.Model):
+ _name = 'promotion.program.line'
+
+ program_id = fields.Many2one('promotion.program', 'Program')
+ name = fields.Char('Name')
+ promotion_type = fields.Selection([
+ ("special_price", "Special Price"),
+ ("bundling", "Bundling"),
+ ("discount_loading", "Discount Loading"),
+ ("merchandise", "Merchandise")
+ ], 'Type')
+ image = fields.Binary(string="Image")
+
+ package_limit = fields.Integer('Package limit')
+ package_limit_user = fields.Integer('Package limit / user')
+ package_limit_trx = fields.Integer('Package limit / transaction')
+
+ product_ids = fields.One2many('promotion.product', 'program_line_id', 'Product')
+ free_product_ids = fields.One2many('promotion.free_product', 'program_line_id', 'Free Product')
+
+ price = fields.Float('Price')
+ discount_type = fields.Selection([
+ ("percentage", "Percentage"),
+ ("fixed", "Fixed")
+ ], 'Discount Type')
+ discount_amount = fields.Float('Discount Amount')
+
+ order_promotion_ids = fields.One2many('sale.order.promotion', 'program_line_id', 'Promotions')
+
+ 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_ids.product_id', '=', product_id)
+ ])
+
+ def _res_limit_qty(self):
+ return {
+ 'all': self.package_limit,
+ 'user': self.package_limit_user,
+ 'transaction': self.package_limit_trx,
+ }
+
+ def _get_remaining_qty(self, user):
+ remaining_qty = {
+ 'all': self.package_limit,
+ 'user': self.package_limit_user,
+ 'transaction': self.package_limit_trx
+ }
+
+ for promotion in self.order_promotion_ids:
+ order = promotion.order_id
+ if order.state != 'cancel':
+ remaining_qty['all'] -= promotion.quantity
+ if user and order.partner_id.id == user['partner_id']:
+ remaining_qty['user'] -= promotion.quantity
+
+ 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 _res_promotion_type(self):
+ return {
+ 'value': self.promotion_type,
+ 'label': dict(self._fields['promotion_type'].selection).get(self.promotion_type)
+ }
+
+ def _get_remaining_time(self):
+ calculate_time = self.program_id.end_time - datetime.now()
+ return round(calculate_time.total_seconds())
+
+ def formats(self, user):
+ return [x.format(user) for x in self]
+
+ def format(self, user = None, qty = 1):
+ ir_attachment = self.env['ir.attachment']
+ 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
+
+ products = self.product_ids.formats(purchase_qty=qty)
+ free_products = self.free_product_ids.formats(purchase_qty=qty)
+
+ merged_products = products + free_products
+ weight = 0
+ if not any(x['package_weight'] == 0 for x in merged_products):
+ weight = sum(x['package_weight'] for x in merged_products)
+
+ response = {
+ 'id': self.id,
+ 'name': self.name,
+ 'image': ir_attachment.api_image(self._name, 'image', self.id),
+ 'remaining_time': self._get_remaining_time(),
+ 'promotion_type': self._res_promotion_type(),
+ 'limit_qty': limit_qty,
+ 'remaining_qty': remaining_qty,
+ 'used_percentage': percent_remaining,
+ 'price': {
+ 'price': self.price,
+ 'price_discount': self.price,
+ 'discount_percentage': 0
+ },
+ 'products': products,
+ 'free_products': free_products,
+ 'weight': weight
+ }
+
+ if self.promotion_type == 'special_price':
+ response['price'] = self._calc_special_price_price(products[0])
+
+ return response
+
+ def _calc_special_price_price(self, product):
+ result = product['price']
+ price = result['price']
+ if self.discount_type == 'percentage':
+ result['discount_percentage'] = self.discount_amount
+ result['price_discount'] = price * (1 - self.discount_amount / 100)
+ elif self.discount_type == 'fixed':
+ final_price = price - self.discount_amount
+ result['price_discount'] = final_price
+ result['discount_percentage'] = (price - final_price) / price * 100
+ return result \ No newline at end of file