diff options
| author | Azka Nathan <darizkyfaz@gmail.com> | 2024-02-07 10:45:51 +0700 |
|---|---|---|
| committer | Azka Nathan <darizkyfaz@gmail.com> | 2024-02-07 10:45:51 +0700 |
| commit | f6be42d37a363b86f4a9ec71ccb38c78cbe2d887 (patch) | |
| tree | 9c297e776f67e35671012e94d3c64007bd5090d7 /indoteknik_custom/models/solr/promotion_program_line.py | |
| parent | edb3c1c80931078d40a8f56149aeca9efdcdc07d (diff) | |
| parent | 29a9ec94f1ad131f398cf119a03a7b927a4c6cba (diff) | |
Merge branch 'production' into purchasing-job
# Conflicts:
# indoteknik_custom/__manifest__.py
# indoteknik_custom/models/__init__.py
# indoteknik_custom/models/automatic_purchase.py
# indoteknik_custom/models/purchase_order_line.py
# indoteknik_custom/security/ir.model.access.csv
# indoteknik_custom/views/automatic_purchase.xml
# indoteknik_custom/views/purchase_order.xml
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) |
