summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/solr/promotion_program_line.py
diff options
context:
space:
mode:
Diffstat (limited to 'indoteknik_custom/models/solr/promotion_program_line.py')
-rw-r--r--indoteknik_custom/models/solr/promotion_program_line.py69
1 files changed, 69 insertions, 0 deletions
diff --git a/indoteknik_custom/models/solr/promotion_program_line.py b/indoteknik_custom/models/solr/promotion_program_line.py
new file mode 100644
index 00000000..6e182324
--- /dev/null
+++ b/indoteknik_custom/models/solr/promotion_program_line.py
@@ -0,0 +1,69 @@
+from odoo import models, api
+from typing import Type
+import pysolr
+import json
+
+
+class PromotionProgramLine(models.Model):
+ _inherit = 'promotion.program.line'
+ _solr_schema = 'promotion_program_lines'
+
+ def solr(self) -> Type[pysolr.Solr]:
+ return self.env['apache.solr'].connect(self._solr_schema)
+
+ def _create_solr_queue(self, function_name: str):
+ for rec in self:
+ self.env['apache.solr.queue'].create_unique({
+ 'res_model': self._name,
+ 'res_id': rec.id,
+ 'function_name': function_name
+ })
+
+ 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
+ } 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()
+
+ document.update({
+ 'id': rec.id,
+ 'program_id_i': rec.program_id.id,
+ 'name_s': rec.name,
+ 'image_s': self.env['ir.attachment'].api_image(self._name, 'image', rec.id) if rec.image else '',
+ '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,
+ '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]),
+ })
+
+ self.solr().add([document])
+
+ self.solr().commit()
+
+ @api.model
+ def create(self, vals):
+ self._create_solr_queue('_sync_to_solr')
+ return super(PromotionProgramLine, self).create(vals)
+
+ def write(self, vals):
+ self._create_solr_queue('_sync_to_solr')
+ return super(PromotionProgramLine, self).write(vals)