summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRafi Zadanly <zadanlyr@gmail.com>2023-10-19 16:03:34 +0700
committerRafi Zadanly <zadanlyr@gmail.com>2023-10-19 16:03:34 +0700
commit1292cb9fc57d972dec21a13df874099cf6d8bef2 (patch)
treefdf7c95c663d395b730f6d617429b900fc48485a
parent91d393eb699188b536c3c44d4460f6fed9596d3b (diff)
Fix product price calculation for solr
-rw-r--r--indoteknik_custom/models/solr/product_product.py46
-rw-r--r--indoteknik_custom/models/solr/product_template.py82
2 files changed, 53 insertions, 75 deletions
diff --git a/indoteknik_custom/models/solr/product_product.py b/indoteknik_custom/models/solr/product_product.py
index 567e2a3e..e1342fc2 100644
--- a/indoteknik_custom/models/solr/product_product.py
+++ b/indoteknik_custom/models/solr/product_product.py
@@ -75,24 +75,12 @@ class ProductProduct(models.Model):
def _sync_price_to_solr(self):
solr_model = self.env['apache.solr']
+ TIER_NUMBERS = ['1_v2', '2_v2', '3_v2', '4_v2', '5_v2']
for variant in self:
- price_excl_after_disc = price_excl = discount = tax = 0
- flashsale_data = {}
+ flashsale_data = variant.with_context(price_for="web")._get_flashsale_price()
- if price_excl_after_disc == 0 or variant._get_website_price_after_disc_and_tax() < price_excl_after_disc:
- price_excl = variant._get_website_price_exclude_tax()
- price_excl_after_disc = variant._get_website_price_after_disc_and_tax()
- tax = variant._get_website_tax()
- discount = variant._get_website_disc(0)
- flashsale_data = variant.with_context(price_for="web")._get_flashsale_price()
-
- price_excl_v2 = variant._v2_get_website_price_exclude_tax()
- price_excl_after_disc_v2 = variant._v2_get_website_price_after_disc_and_tax()
- tax_v2 = variant._v2_get_website_tax()
-
- document = solr_model.get_doc('variants', variant.id)
- document.update({
+ flashsale_doc = {
"id": variant.id,
"flashsale_id_i": flashsale_data.get('flashsale_id', 0),
"flashsale_tag_s": flashsale_data.get('flashsale_tag', ''),
@@ -102,25 +90,19 @@ class ProductProduct(models.Model):
"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),
- "price_f": price_excl,
- "discount_f": discount,
- "price_discount_f": price_excl_after_disc,
- "tax_f": tax,
- "price_v2_f": price_excl_v2,
- "price_discount_v2_f": price_excl_after_disc_v2,
- "tax_v2_f": tax_v2,
- })
-
- for tier_number in [1, 2, 3, '1_v2', '2_v2', '3_v2', '4_v2', '5_v2']:
+ }
+
+ price_doc = {}
+
+ for tier_number in TIER_NUMBERS:
tier = variant._get_pricelist_tier(tier_number)
- document.update({
- f"discount_tier{tier_number}_f": tier.get(f'discount_tier{tier_number}', 0),
- f"price_tier{tier_number}_f": tier.get(f'price_tier{tier_number}', 0),
- })
-
- # for tier_number in [1, 2, 3, '1_v2', '2_v2', '3_v2', '4_v2', '5_v2']:
- # tier = tier_data[tier_number]
+ price_doc[f"discount_tier{tier_number}_f"] = tier.get(f'discount_tier{tier_number}', 0)
+ price_doc[f"price_tier{tier_number}_f"] = tier.get(f'price_tier{tier_number}', 0)
+ document = solr_model.get_doc('variants', variant.id)
+ document['id'] = variant.id
+ document.update(flashsale_doc)
+ document.update(price_doc)
document.update({"has_price_info_b": True})
self.solr().add(docs=[document], softCommit=True)
diff --git a/indoteknik_custom/models/solr/product_template.py b/indoteknik_custom/models/solr/product_template.py
index f178dd5f..aa1708cc 100644
--- a/indoteknik_custom/models/solr/product_template.py
+++ b/indoteknik_custom/models/solr/product_template.py
@@ -97,56 +97,52 @@ class ProductTemplate(models.Model):
def _sync_price_to_solr(self):
solr_model = self.env['apache.solr']
+ TIER_NUMBERS = ['1_v2', '2_v2', '3_v2', '4_v2', '5_v2']
for template in self:
- flashsale_data = {}
- price_excl = price_excl_after_disc = discount = tax = 0
- tier_data = {}
+ cheapest_flashsale = {}
for variant in template.product_variant_ids:
variant_flashsale = variant.with_context(price_for="web")._get_flashsale_price()
variant_flashsale_price = variant_flashsale.get('flashsale_price', 0)
- flashsale_data_price = flashsale_data.get('flashsale_price', 0)
-
- if flashsale_data_price == 0 or (variant_flashsale_price != 0 and variant_flashsale_price < flashsale_data_price):
- flashsale_data = variant_flashsale
-
- price_excl = variant._get_website_price_exclude_tax()
- price_excl_after_disc = variant._get_website_price_after_disc_and_tax()
- discount = variant._get_website_disc(0)
- tax = variant._get_website_tax()
-
- price_excl_v2 = variant._v2_get_website_price_exclude_tax()
- price_excl_after_disc_v2 = variant._v2_get_website_price_after_disc_and_tax()
- tax_v2 = variant._v2_get_website_tax()
-
+ cheapest_flashsale_price = cheapest_flashsale.get('flashsale_price', 0)
+
+ if cheapest_flashsale_price == 0 or (variant_flashsale_price != 0 and variant_flashsale_price < cheapest_flashsale_price):
+ cheapest_flashsale = variant_flashsale
+
+ flashsale_doc = {
+ "flashsale_id_i": cheapest_flashsale.get('flashsale_id', 0),
+ "flashsale_tag_s": cheapest_flashsale.get('flashsale_tag', ''),
+ "flashsale_name_s": cheapest_flashsale.get('flashsale_name', ''),
+ "flashsale_start_date_s": cheapest_flashsale.get('flashsale_start_date', ''),
+ "flashsale_end_date_s": cheapest_flashsale.get('flashsale_end_date', ''),
+ "flashsale_base_price_f": cheapest_flashsale.get('flashsale_base_price', 0),
+ "flashsale_discount_f": cheapest_flashsale.get('flashsale_discount', 0),
+ "flashsale_price_f": cheapest_flashsale.get('flashsale_price', 0)
+ }
+
+ price_doc = {}
+
+ # Loop tier to get each variant price
+ for tier_number in TIER_NUMBERS:
+ for variant in template.product_variant_ids:
+ tier = variant._get_pricelist_tier(tier_number)
+ discount_tier_key = f'discount_tier{tier_number}'
+ price_tier_key = f'price_tier{tier_number}'
+
+ variant_discount = tier.get(discount_tier_key, 0)
+ variant_price = tier.get(price_tier_key, 0)
+
+ price_tier = price_doc.get(f"{price_tier_key}_f", 0)
+ # When price tier is 0 or variant_price less than price tier then use variant price
+ if price_tier == 0 or variant_price < price_tier:
+ price_doc[f"{discount_tier_key}_f"] = variant_discount
+ price_doc[f"{price_tier_key}_f"] = variant_price
+
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),
- "price_f": price_excl,
- "discount_f": discount,
- "price_discount_f": price_excl_after_disc,
- "tax_f": tax,
- "price_v2_f": price_excl_v2,
- "price_discount_v2_f": price_excl_after_disc_v2,
- "tax_v2_f": tax_v2,
- })
-
- for tier_number in [1, 2, 3, '1_v2', '2_v2', '3_v2', '4_v2', '5_v2']:
- tier = variant._get_pricelist_tier(tier_number)
- document.update({
- f"discount_tier{tier_number}_f": tier.get(f'discount_tier{tier_number}', 0),
- f"price_tier{tier_number}_f": tier.get(f'price_tier{tier_number}', 0),
- })
-
+ document['id'] = template.id
+ document.update(flashsale_doc)
+ document.update(price_doc)
document.update({"has_price_info_b": True})
self.solr().add(docs=[document], softCommit=True)