diff options
| author | IT Fixcomart <it@fixcomart.co.id> | 2024-01-04 02:27:45 +0000 |
|---|---|---|
| committer | IT Fixcomart <it@fixcomart.co.id> | 2024-01-04 02:27:45 +0000 |
| commit | 63abd2dc32f952af7234f3d6a7774cbb8c7bcb47 (patch) | |
| tree | b4cee127fc7d467b33cef4ffae9218f8acb9aeaa /indoteknik_custom/models/solr/promotion_program_line.py | |
| parent | 13b31fc3d89957d23582efb2c51ab143ca1d425a (diff) | |
| parent | 717cb3b43c729e265603b3df61234c0b430742a7 (diff) | |
Merged in change/feature/promotion-program (pull request #133)
Change/feature/promotion program
Diffstat (limited to 'indoteknik_custom/models/solr/promotion_program_line.py')
| -rw-r--r-- | indoteknik_custom/models/solr/promotion_program_line.py | 69 |
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) |
