diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-09-13 14:49:13 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-09-13 14:49:13 +0700 |
| commit | 4cfc5c3214cc6060f978bc9e1f52533899330ee2 (patch) | |
| tree | 129c9f52f41604675ce3d630e6fe3045cdd15ff6 /indoteknik_api/models | |
| parent | 7c67529ed27e32d32a6ddc4cd3fe296ce0d7bca2 (diff) | |
Update product and variant sync flashsale data
Diffstat (limited to 'indoteknik_api/models')
| -rw-r--r-- | indoteknik_api/models/product_product.py | 70 |
1 files changed, 37 insertions, 33 deletions
diff --git a/indoteknik_api/models/product_product.py b/indoteknik_api/models/product_product.py index bc1ce9a6..9454c9bd 100644 --- a/indoteknik_api/models/product_product.py +++ b/indoteknik_api/models/product_product.py @@ -1,6 +1,8 @@ from odoo import models from odoo.http import request import math +from datetime import datetime +import pytz class ProductProduct(models.Model): @@ -76,8 +78,8 @@ class ProductProduct(models.Model): if price[price_key] > 0: price_discount = price[price_key] flashsale = self._get_flashsale_price() - flashsale_price = flashsale['flashsale_price'] - flashsale_discount = flashsale['flashsale_discount'] + flashsale_price = flashsale.get('flashsale_price', 0) + flashsale_discount = flashsale.get('flashsale_discount', 0) if flashsale_price > 0 and flashsale_price < price_discount: price_discount = flashsale_price discount_percentage = flashsale_discount @@ -237,37 +239,39 @@ class ProductProduct(models.Model): return data def _get_flashsale_price(self): - # must get active pricelist - active_flash_sale = self.env['product.pricelist'].get_active_flash_sale() - flashsale_id = 0 - flashsale_name = '' - flashsale_tag = '' - # loop pricelist items - base_price = discount = price_flashsale = 0 - for pricelist in active_flash_sale: - query = [ - ('pricelist_id', '=', pricelist.id), - ('product_id', '=', self.id), - ] - pricelist_items = self.env['product.pricelist.item'].search(query, limit=1) - for item in pricelist_items: - flashsale_id = pricelist.id - flashsale_name = pricelist.name - flashsale_tag = pricelist.flashsale_tag - base_price = self._get_website_price_exclude_tax() - if item.price_discount > 0: - discount = item.price_discount - # base_item = self.env['product.pricelist.item'].search([('pricelist_id', '=', item.base_pricelist_id.id), ('product_id', '=', self.id)], limit=1) - price_flashsale = base_price - (base_price * discount // 100) - elif item.fixed_price > 0: - price_flashsale = item.fixed_price # ask darren for include or exclude - discount = (base_price - price_flashsale) // base_price * 100 - data = { - 'flashsale_id': flashsale_id, - 'flashsale_name': flashsale_name, - 'flashsale_tag': flashsale_tag, + result = {} + + current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') + item = self.env['product.pricelist.item'].search([ + ('pricelist_id.is_flash_sale', '=', True), + ('pricelist_id.start_date', '<=', current_time), + ('pricelist_id.end_date', '>=', current_time), + ('product_id', '=', self.id) + ], limit=1) + + if not item: + return result + + base_price = self._get_website_price_exclude_tax() + discount = 0 + price_flashsale = 0 + if item.price_discount > 0: + discount = item.price_discount + price_flashsale = base_price - (base_price * discount // 100) + elif item.fixed_price > 0: + price_flashsale = item.fixed_price # ask darren for include or exclude + discount = (base_price - price_flashsale) // base_price * 100 + + jkt_tz = pytz.timezone('Asia/Jakarta') + result.update({ + 'flashsale_id': item.pricelist_id.id, + 'flashsale_name': item.pricelist_id.name, + 'flashsale_start_date': item.pricelist_id.start_date.replace(tzinfo=pytz.utc).astimezone(jkt_tz).strftime('%Y-%m-%d %H:%M:%S'), + 'flashsale_end_date': item.pricelist_id.end_date.replace(tzinfo=pytz.utc).astimezone(jkt_tz).strftime('%Y-%m-%d %H:%M:%S'), 'flashsale_base_price': base_price, 'flashsale_discount': discount, 'flashsale_price': price_flashsale - } - return data + }) + + return result + |
