summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRafi Zadanly <zadanlyr@gmail.com>2023-09-13 14:49:13 +0700
committerRafi Zadanly <zadanlyr@gmail.com>2023-09-13 14:49:13 +0700
commit4cfc5c3214cc6060f978bc9e1f52533899330ee2 (patch)
tree129c9f52f41604675ce3d630e6fe3045cdd15ff6
parent7c67529ed27e32d32a6ddc4cd3fe296ce0d7bca2 (diff)
Update product and variant sync flashsale data
-rw-r--r--indoteknik_api/controllers/api_v1/product.py2
-rw-r--r--indoteknik_api/models/product_product.py70
-rw-r--r--indoteknik_custom/models/solr/__init__.py1
-rw-r--r--indoteknik_custom/models/solr/product_pricelist.py10
-rw-r--r--indoteknik_custom/models/solr/product_product.py14
-rw-r--r--indoteknik_custom/models/solr/product_template.py14
6 files changed, 65 insertions, 46 deletions
diff --git a/indoteknik_api/controllers/api_v1/product.py b/indoteknik_api/controllers/api_v1/product.py
index e3b7701a..2cbdede7 100644
--- a/indoteknik_api/controllers/api_v1/product.py
+++ b/indoteknik_api/controllers/api_v1/product.py
@@ -66,7 +66,7 @@ class Product(controller.Controller):
'sla_date': sla_date,
}
- return self.response(data, headers=[('Cache-Control', 'max-age=180, private')])
+ return self.response(data, headers=[('Cache-Control', 'max-age=600, private')])
@http.route(prefix + 'product/template/price/<id>', auth='public', methods=['GET', 'OPTIONS'])
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
+
diff --git a/indoteknik_custom/models/solr/__init__.py b/indoteknik_custom/models/solr/__init__.py
index 45013d3d..16da73af 100644
--- a/indoteknik_custom/models/solr/__init__.py
+++ b/indoteknik_custom/models/solr/__init__.py
@@ -1,5 +1,6 @@
from . import apache_solr
from . import apache_solr_queue
+from . import product_pricelist
from . import product_pricelist_item
from . import product_product
from . import product_template
diff --git a/indoteknik_custom/models/solr/product_pricelist.py b/indoteknik_custom/models/solr/product_pricelist.py
new file mode 100644
index 00000000..e29baf1d
--- /dev/null
+++ b/indoteknik_custom/models/solr/product_pricelist.py
@@ -0,0 +1,10 @@
+from odoo import models, api
+
+
+class ProductPricelist(models.Model):
+ _inherit = 'product.pricelist'
+
+ @api.constrains('is_flash_sale', 'flashsale_option', 'flashsale_tag', 'banner', 'banner_mobile', 'start_date', 'end_date')
+ def _constrains_related_solr_field(self):
+ for item in self.item_ids:
+ item.product_id.product_tmpl_id._create_solr_queue('_sync_price_to_solr') \ No newline at end of file
diff --git a/indoteknik_custom/models/solr/product_product.py b/indoteknik_custom/models/solr/product_product.py
index 7e2a99cc..93e26793 100644
--- a/indoteknik_custom/models/solr/product_product.py
+++ b/indoteknik_custom/models/solr/product_product.py
@@ -62,10 +62,11 @@ class ProductProduct(models.Model):
'category_name_s': category_name,
'category_name': category_name,
'search_rank_i': variant.product_tmpl_id.search_rank,
- 'has_product_info_b': True,
- 'search_rank_weekly_i': variant.product_tmpl_id.search_rank_weekly
+ 'search_rank_weekly_i': variant.product_tmpl_id.search_rank_weekly,
+ 'attributes': [x.name for x in variant.product_template_attribute_value_ids],
+ 'has_product_info_b': True
})
-
+
self.solr().add(docs=[document], softCommit=True)
variant.change_solr_data('Perubahan pada data product')
@@ -92,9 +93,12 @@ class ProductProduct(models.Model):
document = solr_model.get_doc('variants', variant.id)
document.update({
+ "id": variant.id,
"flashsale_id_i": flashsale_data.get('flashsale_id', 0),
"flashsale_tag_s": flashsale_data.get('flashsale_tag', ''),
"flashsale_name_s": flashsale_data.get('flashsale_name', ''),
+ "flashsale_start_date_s": flashsale_data.get('flashsale_start_date', ''),
+ "flashsale_end_date_s": flashsale_data.get('flashsale_end_date', ''),
"flashsale_base_price_f": flashsale_data.get('flashsale_base_price', 0),
"flashsale_discount_f": flashsale_data.get('flashsale_discount', 0),
"flashsale_price_f": flashsale_data.get('flashsale_price', 0),
@@ -102,13 +106,13 @@ class ProductProduct(models.Model):
"discount_f": discount,
"price_discount_f": price_excl_after_disc,
"tax_f": tax,
- "has_price_info_b": True,
"discount_tier1_f": tier1.get('discount_tier1', 0),
"price_tier1_f": tier1.get('price_tier1', 0),
"discount_tier2_f": tier2.get('discount_tier2', 0),
"price_tier2_f": tier2.get('price_tier2', 0),
"discount_tier3_f": tier3.get('discount_tier3', 0),
- "price_tier3_f": tier3.get('price_tier3', 0)
+ "price_tier3_f": tier3.get('price_tier3', 0),
+ "has_price_info_b": True
})
self.solr().add(docs=[document], softCommit=True)
variant.change_solr_data('Ada perubahan pada harga product')
diff --git a/indoteknik_custom/models/solr/product_template.py b/indoteknik_custom/models/solr/product_template.py
index 3470621e..ea976359 100644
--- a/indoteknik_custom/models/solr/product_template.py
+++ b/indoteknik_custom/models/solr/product_template.py
@@ -91,10 +91,11 @@ class ProductTemplate(models.Model):
"variants_code_t": variant_codes,
"search_rank_i": template.search_rank,
"search_rank_weekly_i": template.search_rank_weekly,
- 'has_product_info_b': True,
"category_id_i": category_id,
"category_name_s": category_name,
- "category_name": category_name
+ "category_name": category_name,
+ "description_t": template.website_description or '',
+ 'has_product_info_b': True,
})
self.solr().add(docs=[document], softCommit=True)
@@ -106,13 +107,8 @@ class ProductTemplate(models.Model):
def _sync_price_to_solr(self):
solr_model = self.env['apache.solr']
- solr = self.solr()
for template in self:
- document = {
- "id": template.id,
- "has_price_info_b": True
- }
flashsale_data = {}
for variant in template.product_variant_ids:
@@ -133,9 +129,12 @@ class ProductTemplate(models.Model):
document = solr_model.get_doc('product', template.id)
document.update({
+ "id": template.id,
"flashsale_id_i": flashsale_data.get('flashsale_id', 0),
"flashsale_tag_s": flashsale_data.get('flashsale_tag', ''),
"flashsale_name_s": flashsale_data.get('flashsale_name', ''),
+ "flashsale_start_date_s": flashsale_data.get('flashsale_start_date', ''),
+ "flashsale_end_date_s": flashsale_data.get('flashsale_end_date', ''),
"flashsale_base_price_f": flashsale_data.get('flashsale_base_price', 0),
"flashsale_discount_f": flashsale_data.get('flashsale_discount', 0),
"flashsale_price_f": flashsale_data.get('flashsale_price', 0),
@@ -149,6 +148,7 @@ class ProductTemplate(models.Model):
"price_tier2_f": tier2.get('price_tier2', 0),
"discount_tier3_f": tier3.get('discount_tier3', 0),
"price_tier3_f": tier3.get('price_tier3', 0),
+ "has_price_info_b": True
})
self.solr().add(docs=[document], softCommit=True)