summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAzka Nathan <darizkyfaz@gmail.com>2025-08-28 11:29:54 +0700
committerAzka Nathan <darizkyfaz@gmail.com>2025-08-28 11:29:54 +0700
commit26623e1b2e8ba83367814ac704fbb19a4370f56c (patch)
treed0a5ce1ef58eedc80cf8d3ad3115bae5d387aff6
parente8c10203b9cac4e8fe020a56f39945fbd360b605 (diff)
fix bug
-rw-r--r--indoteknik_custom/models/solr/promotion_program_line.py112
1 files changed, 61 insertions, 51 deletions
diff --git a/indoteknik_custom/models/solr/promotion_program_line.py b/indoteknik_custom/models/solr/promotion_program_line.py
index 64ad4209..b1b2f88e 100644
--- a/indoteknik_custom/models/solr/promotion_program_line.py
+++ b/indoteknik_custom/models/solr/promotion_program_line.py
@@ -2,6 +2,10 @@ from odoo import models, api
from typing import Type
import pysolr
import json
+import logging
+from odoo.exceptions import UserError
+
+_logger = logging.getLogger(__name__)
class PromotionProgramLine(models.Model):
_inherit = 'promotion.program.line'
@@ -20,58 +24,64 @@ class PromotionProgramLine(models.Model):
def _sync_to_solr(self):
solr_model = self.env['apache.solr']
-
for rec in self:
- document = solr_model.get_doc(self._solr_schema, rec.id)
-
- products = [{
- 'product_id': x.product_id.id,
- 'qty': x.qty,
- 'qty_sold': x.product_id.qty_sold
- } for x in rec.product_ids]
-
- free_products = [{
- 'product_id': x.product_id.id,
- 'qty': x.qty
- } for x in rec.free_product_ids]
-
- promotion_type = rec._res_promotion_type()
-
- # Gathering all categories
- category_names = [category.name for category in rec.product_ids.product_id.public_categ_ids]
-
- # Set sequence_i to None if rec.sequence is 0
- sequence_value = None if rec.sequence == 0 else rec.sequence
-
- document.update({
- 'id': rec.id,
- 'program_id_i': rec.program_id.id or 0,
- 'name_s': rec.name,
- 'type_value_s': promotion_type['value'],
- 'type_label_s': promotion_type['label'],
- 'package_limit_i': rec.package_limit,
- 'package_limit_user_i': rec.package_limit_user,
- 'package_limit_trx_i': rec.package_limit_trx,
- 'price_f': rec.price,
- 'price_tier_1_f': rec.price_tier_1,
- 'price_tier_2_f': rec.price_tier_2,
- 'price_tier_3_f': rec.price_tier_3,
- 'price_tier_4_f': rec.price_tier_4,
- 'price_tier_5_f': rec.price_tier_5,
- 'sequence_i': sequence_value,
- 'product_ids': [x.product_id.id for x in rec.product_ids],
- 'products_s': json.dumps(products),
- 'free_product_ids': [x.product_id.id for x in rec.free_product_ids],
- 'free_products_s': json.dumps(free_products),
- 'total_qty_i': sum([x.qty for x in rec.product_ids] + [x.qty for x in rec.free_product_ids]),
- 'total_qty_sold_f': [x.product_id.qty_sold for x in rec.product_ids],
- 'active_b': rec.active,
- "manufacture_name_s": rec.product_ids.product_id.x_manufacture.x_name or '',
- "category_name": category_names,
- })
-
- self.solr().add([document])
- self.solr().commit()
+ try:
+ document = solr_model.get_doc(self._solr_schema, rec.id)
+
+ products = [{
+ 'product_id': x.product_id.id,
+ 'qty': x.qty,
+ 'qty_sold': x.product_id.qty_sold
+ } for x in rec.product_ids]
+
+ free_products = [{
+ 'product_id': x.product_id.id,
+ 'qty': x.qty
+ } for x in rec.free_product_ids]
+
+ promotion_type = rec._res_promotion_type()
+
+ category_names = [category.name for category in rec.product_ids.product_id.public_categ_ids]
+ sequence_value = None if rec.sequence == 0 else rec.sequence
+
+ document.update({
+ 'id': rec.id,
+ 'program_id_i': rec.program_id.id or 0,
+ 'name_s': rec.name,
+ 'type_value_s': promotion_type['value'],
+ 'type_label_s': promotion_type['label'],
+ 'package_limit_i': rec.package_limit,
+ 'package_limit_user_i': rec.package_limit_user,
+ 'package_limit_trx_i': rec.package_limit_trx,
+ 'price_f': rec.price,
+ 'price_tier_1_f': rec.price_tier_1,
+ 'price_tier_2_f': rec.price_tier_2,
+ 'price_tier_3_f': rec.price_tier_3,
+ 'price_tier_4_f': rec.price_tier_4,
+ 'price_tier_5_f': rec.price_tier_5,
+ 'sequence_i': sequence_value,
+ 'product_ids': [x.product_id.id for x in rec.product_ids],
+ 'products_s': json.dumps(products),
+ 'free_product_ids': [x.product_id.id for x in rec.free_product_ids],
+ 'free_products_s': json.dumps(free_products),
+ 'total_qty_i': sum([x.qty for x in rec.product_ids] + [x.qty for x in rec.free_product_ids]),
+ 'total_qty_sold_f': sum([x.product_id.qty_sold for x in rec.product_ids]),
+ 'active_b': rec.active,
+ "manufacture_name_s": rec.product_ids[0].product_id.x_manufacture.x_name or '',
+ "category_name": category_names,
+ })
+
+ self.solr().add([document])
+ self.solr().commit()
+
+ except Exception as e:
+ _logger.error(
+ "Failed to sync record %s (ID: %s) to Solr. Error: %s",
+ rec._name, rec.id, str(e),
+ exc_info=True # biar stack trace keluar
+ )
+ # opsional -> kalau mau hard fail:
+ raise UserError(_("Sync to Solr failed for record %s: %s") % (rec.name, str(e)))
@api.model
def create(self, vals):