summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/solr
diff options
context:
space:
mode:
authorIT Fixcomart <it@fixcomart.co.id>2024-01-04 02:27:45 +0000
committerIT Fixcomart <it@fixcomart.co.id>2024-01-04 02:27:45 +0000
commit63abd2dc32f952af7234f3d6a7774cbb8c7bcb47 (patch)
treeb4cee127fc7d467b33cef4ffae9218f8acb9aeaa /indoteknik_custom/models/solr
parent13b31fc3d89957d23582efb2c51ab143ca1d425a (diff)
parent717cb3b43c729e265603b3df61234c0b430742a7 (diff)
Merged in change/feature/promotion-program (pull request #133)
Change/feature/promotion program
Diffstat (limited to 'indoteknik_custom/models/solr')
-rw-r--r--indoteknik_custom/models/solr/__init__.py4
-rw-r--r--indoteknik_custom/models/solr/promotion_program.py68
-rw-r--r--indoteknik_custom/models/solr/promotion_program_line.py69
3 files changed, 140 insertions, 1 deletions
diff --git a/indoteknik_custom/models/solr/__init__.py b/indoteknik_custom/models/solr/__init__.py
index f2d13116..606c0035 100644
--- a/indoteknik_custom/models/solr/__init__.py
+++ b/indoteknik_custom/models/solr/__init__.py
@@ -8,4 +8,6 @@ from . import website_categories_homepage
from . import x_manufactures
from . import x_banner_banner
from . import product_public_category
-from . import x_banner_category \ No newline at end of file
+from . import x_banner_category
+from . import promotion_program
+from . import promotion_program_line \ No newline at end of file
diff --git a/indoteknik_custom/models/solr/promotion_program.py b/indoteknik_custom/models/solr/promotion_program.py
new file mode 100644
index 00000000..0d417b3e
--- /dev/null
+++ b/indoteknik_custom/models/solr/promotion_program.py
@@ -0,0 +1,68 @@
+from odoo import models, api
+from datetime import datetime
+from pytz import timezone
+from typing import Type
+import pysolr
+
+
+class PromotionProgram(models.Model):
+ _inherit = 'promotion.program'
+ _solr_schema = 'promotion_programs'
+
+ 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):
+ ir_attachment = self.env['ir.attachment']
+ solr_model = self.env['apache.solr']
+
+ for rec in self:
+ document = solr_model.get_doc(self._solr_schema, rec.id)
+ document.update({
+ 'id': rec.id,
+ 'name_s': rec.name,
+ 'banner_s': ir_attachment.api_image(self._name, 'banner', rec.id) if rec.banner else '',
+ 'keywords': [x.name for x in rec.keyword_ids],
+ 'line_ids': [x.id for x in rec.program_line],
+ 'start_time_s': self._time_format(rec.start_time),
+ 'end_time_s': self._time_format(rec.end_time),
+ 'applies_to_s': rec.applies_to,
+ 'icon_s': ir_attachment.api_image(self._name, 'icon', rec.id) if rec.icon else '',
+ 'icon_top_s': ir_attachment.api_image(self._name, 'icon_top', rec.id) if rec.icon_top else '',
+ 'icon_bottom_s': ir_attachment.api_image(self._name, 'icon_bottom', rec.id) if rec.icon_bottom else '',
+ })
+
+ self.solr().add([document])
+
+ self.solr().commit()
+
+ def _time_format(self, object) -> str:
+ time = ''
+ tz_jakarta = timezone('Asia/Jakarta')
+ if isinstance(object, datetime):
+ time = object.astimezone(tz_jakarta).strftime("%Y-%m-%d %H:%M:%S")
+ return time
+
+ @api.model
+ def create(self, vals):
+ self._create_solr_queue('_sync_to_solr')
+ return super(PromotionProgram, self).create(vals)
+
+ def write(self, vals):
+ self._create_solr_queue('_sync_to_solr')
+ return super(PromotionProgram, self).write(vals)
+
+ @api.constrains('program_line')
+ def constrains_program_line(self):
+ for rec in self:
+ for line in rec.program_line:
+ line._create_solr_queue('_sync_to_solr')
+
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)