summaryrefslogtreecommitdiff
path: root/indoteknik_api/models
diff options
context:
space:
mode:
authorIT Fixcomart <it@fixcomart.co.id>2022-10-05 15:19:27 +0700
committerIT Fixcomart <it@fixcomart.co.id>2022-10-05 15:19:27 +0700
commit14cb30c3f2fb8b15baaf32ab9fca7bb9bda73845 (patch)
treee4039e13e934db6ee148f766882bb82af8db1d7a /indoteknik_api/models
parentd1bc570eae2818bc4b535840f2eb3061b99ca98b (diff)
Update struktur API and fitur API flash sale
Diffstat (limited to 'indoteknik_api/models')
-rw-r--r--indoteknik_api/models/__init__.py2
-rw-r--r--indoteknik_api/models/product_pricelist.py108
-rw-r--r--indoteknik_api/models/product_template.py0
3 files changed, 110 insertions, 0 deletions
diff --git a/indoteknik_api/models/__init__.py b/indoteknik_api/models/__init__.py
new file mode 100644
index 00000000..06f1e1da
--- /dev/null
+++ b/indoteknik_api/models/__init__.py
@@ -0,0 +1,2 @@
+from . import product_pricelist
+from . import product_template
diff --git a/indoteknik_api/models/product_pricelist.py b/indoteknik_api/models/product_pricelist.py
new file mode 100644
index 00000000..3a807f3a
--- /dev/null
+++ b/indoteknik_api/models/product_pricelist.py
@@ -0,0 +1,108 @@
+from odoo import models
+from pytz import timezone
+from datetime import datetime
+
+
+class ProductPricelist(models.Model):
+ _inherit = 'product.pricelist'
+
+ def compute_price(self, pricelist_id: int, product_id: int):
+ """
+ Compute price with tax, discount formula, and fixed_price
+ @param pricelist_id: id of product.pricelist
+ @param product_id: id of product.product
+ @return: returns price value from pricelist.
+ """
+ price = 0
+ discounts = []
+
+ is_flash_sale_product = self.is_flash_sale_product(product_id)
+ if is_flash_sale_product:
+ pricelist_id = is_flash_sale_product
+
+ is_compute_formula = True
+ while is_compute_formula:
+ pricelist = self.env['product.pricelist.item'].search([
+ ('pricelist_id', '=', pricelist_id),
+ ('product_id', '=', product_id)
+ ], limit=1)
+ if pricelist:
+ if pricelist.compute_price == 'formula':
+ pricelist_id = pricelist.base_pricelist_id.id
+ discounts.append(pricelist.price_discount)
+ else:
+ price = pricelist.fixed_price
+ is_compute_formula = False
+ else:
+ is_compute_formula = False
+
+ product = self.env['product.product'].browse(product_id)
+ if product:
+ for tax in product.taxes_id:
+ if not tax.price_include:
+ price *= (100 + tax.amount) / 100
+
+ price_discount = price
+ for discount in discounts:
+ price_discount *= (100 - discount) / 100
+
+ discount_percentage = 0
+ if len(discounts) > 0:
+ discount_percentage = (price - price_discount) / price * 100
+
+ return {
+ 'price': price,
+ 'price_discount': price_discount,
+ 'discount_percentage': discount_percentage
+ }
+
+ def get_lowest_product_variant_price(self, product_id, pricelist_id):
+ """
+ @param product_id: id of product.template
+ @param pricelist_id: id of pricelist which have default price
+ @return price: object
+ """
+ product_template = self.env['product.template'].browse(product_id)
+ product_variant_ids = [x.id for x in product_template.product_variant_ids]
+ product = self.env['product.pricelist.item'].search([
+ ('pricelist_id', '=', pricelist_id),
+ ('product_id', 'in', product_variant_ids)
+ ], order='fixed_price asc', limit=1)
+ product_id = 0
+ if product:
+ product_id = product.product_id.id
+ return self.compute_price(pricelist_id, product_id)
+
+ def get_active_flash_sale(self):
+ """
+ Check whether have active flash sale in range of date
+ @return: returns pricelist: object
+ """
+ current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
+ pricelist = self.search([
+ ('is_flash_sale', '=', True),
+ ('start_date', '<=', current_time),
+ ('end_date', '>=', current_time)
+ ], limit=1)
+ return pricelist
+
+ def is_flash_sale_product(self, product_id: int):
+ """
+ Check whether product is flash sale.
+ @param product_id: id of product.product
+ @return: returns pricelist_id: int, if product is flash sale.
+ """
+ pricelist = self.get_active_flash_sale()
+ if pricelist:
+ pricelist_product_ids = [x.product_id.id for x in pricelist.item_ids]
+ if product_id in pricelist_product_ids:
+ return pricelist.id
+ return False
+
+ # TODO: need testing
+ def get_active_flash_sale_items(self):
+ pricelist = self.get_active_flash_sale()
+ pricelist_item = False
+ if pricelist:
+ pricelist_item = [x.product_id for x in pricelist.item_ids]
+ return pricelist_item
diff --git a/indoteknik_api/models/product_template.py b/indoteknik_api/models/product_template.py
new file mode 100644
index 00000000..e69de29b
--- /dev/null
+++ b/indoteknik_api/models/product_template.py