diff options
| author | Rafi Zadanly <zadanlyr@gmail.com> | 2023-08-25 15:11:00 +0700 |
|---|---|---|
| committer | Rafi Zadanly <zadanlyr@gmail.com> | 2023-08-25 15:11:00 +0700 |
| commit | a9a3c3e8945dd11a0d81f64a5629876397a1e51d (patch) | |
| tree | e7d8850e7868b3f83e29f19bc37bcc9730922a4e | |
| parent | 8424fbdfd66c6eca58d546c256d57a61e258e930 (diff) | |
Update sync solr function
| -rwxr-xr-x | indoteknik_custom/models/__init__.py | 2 | ||||
| -rw-r--r-- | indoteknik_custom/models/product_pricelist.py | 123 | ||||
| -rwxr-xr-x | indoteknik_custom/models/product_template.py | 128 | ||||
| -rw-r--r-- | indoteknik_custom/models/solr/__init__.py | 5 | ||||
| -rw-r--r-- | indoteknik_custom/models/solr/apache_solr.py (renamed from indoteknik_custom/models/apache_solr.py) | 33 | ||||
| -rw-r--r-- | indoteknik_custom/models/solr/product_pricelist_item.py | 30 | ||||
| -rw-r--r-- | indoteknik_custom/models/solr/product_product.py | 121 | ||||
| -rw-r--r-- | indoteknik_custom/models/solr/product_template.py | 158 | ||||
| -rw-r--r-- | indoteknik_custom/models/solr/website_categories_homepage.py | 57 | ||||
| -rw-r--r-- | indoteknik_custom/models/website_categories_homepage.py | 30 | ||||
| -rw-r--r-- | indoteknik_custom/views/product_product.xml | 8 | ||||
| -rwxr-xr-x | indoteknik_custom/views/product_template.xml | 14 | ||||
| -rw-r--r-- | indoteknik_custom/views/website_categories_homepage.xml | 6 |
13 files changed, 429 insertions, 286 deletions
diff --git a/indoteknik_custom/models/__init__.py b/indoteknik_custom/models/__init__.py index 255a12f6..2bf3be9d 100755 --- a/indoteknik_custom/models/__init__.py +++ b/indoteknik_custom/models/__init__.py @@ -62,7 +62,6 @@ from . import wati_contact from . import uangmuka_penjualan from . import uangmuka_pembelian from . import automatic_purchase -from . import apache_solr from . import raja_ongkir from . import procurement_monitoring_detail from . import brand_vendor @@ -81,3 +80,4 @@ from . import account_general_ledger from . import account_report_financial from . import account_report_general_ledger from . import price_group +from . import solr
\ No newline at end of file diff --git a/indoteknik_custom/models/product_pricelist.py b/indoteknik_custom/models/product_pricelist.py index 73eebdcf..a55e3fdd 100644 --- a/indoteknik_custom/models/product_pricelist.py +++ b/indoteknik_custom/models/product_pricelist.py @@ -1,8 +1,6 @@ -from odoo import models, fields, api -from odoo.exceptions import UserError +from odoo import models, fields from datetime import datetime import logging -import time _logger = logging.getLogger(__name__) class ProductPricelist(models.Model): @@ -30,121 +28,4 @@ class ProductPricelist(models.Model): class ProductPricelistItem(models.Model): _inherit = 'product.pricelist.item' - manufacture_id = fields.Many2one('x_manufactures', string='Manufacture') - - @api.constrains('applied_on', 'product_id', 'base', 'base_pricelist_id', 'price_discount') - def _constrains_related_solr_field(self): - self.update_template_solr() - self.update_variant_solr() - - def update_template_solr(self): - price_excl_after_disc = price_excl = discount = tax = 0 - flashsale_data = tier1 = tier2 = tier3 = {} - - template = self.product_id.product_tmpl_id - if template.product_variant_count > 1: - for variant in template.product_variant_ids: - if price_excl_after_disc == 0 or variant._get_website_price_after_disc_and_tax() < price_excl_after_disc: - price_excl = variant._get_website_price_exclude_tax() - price_excl_after_disc = variant._get_website_price_after_disc_and_tax() - discount = variant._get_website_disc(0) - tax = variant._get_website_tax() - flashsale_data = variant._get_flashsale_price() - # add price tiering for base price, discount, and price after discount (tier 1 - 3) - tier1 = variant._get_pricelist_tier1() - tier2 = variant._get_pricelist_tier2() - tier3 = variant._get_pricelist_tier3() - else: - price_excl_after_disc = price_excl_after_disc - price_excl = price_excl - discount = discount - tax = tax - flashsale_data = flashsale_data - tier1 = tier1 - tier2 = tier2 - tier3 = tier3 - else: - price_excl = template.product_variant_id._get_website_price_exclude_tax() - discount = template.product_variant_id._get_website_disc(0) - price_excl_after_disc = template.product_variant_id._get_website_price_after_disc_and_tax() - tax = template.product_variant_id._get_website_tax() - flashsale_data = template.product_variant_id._get_flashsale_price() - tier1 = template.product_variant_id._get_pricelist_tier1() - tier2 = template.product_variant_id._get_pricelist_tier2() - tier3 = template.product_variant_id._get_pricelist_tier3() - - solr_model = self.env['apache.solr'] - solr = solr_model.connect('product') - document = solr_model.get_single_doc('product', template.id) - document.update({ - 'id': template.id, - 'flashsale_id_i': flashsale_data['flashsale_id'] or 0, - 'flashsale_tag_s': flashsale_data['flashsale_tag'] or '', - 'flashsale_name_s': flashsale_data['flashsale_name'] or '', - 'flashsale_base_price_f': flashsale_data['flashsale_base_price'] or 0, - 'flashsale_discount_f': flashsale_data['flashsale_discount'] or 0, - 'flashsale_price_f': flashsale_data['flashsale_price'] or 0, - 'price_f': price_excl, - 'discount_f': discount, - 'price_discount_f': price_excl_after_disc, - 'tax_f': tax, - 'discount_tier1_f': tier1['discount_tier1'] or 0, - 'price_tier1_f': tier1['price_tier1'] or 0, - 'discount_tier2_f': tier2['discount_tier2'] or 0, - 'price_tier2_f': tier2['price_tier2'] or 0, - 'discount_tier3_f': tier3['discount_tier3'] or 0, - 'price_tier3_f': tier3['price_tier3'] or 0 - }) - solr.add([document]) - template.change_solr_data('Ada perubahan pada harga product') - - def update_variant_solr(self): - price_excl_after_disc = price_excl = discount = tax = 0 - flashsale_data = tier1 = tier2 = tier3 = {} - - product = self.product_id - for variant in product: - if price_excl_after_disc == 0 or variant._get_website_price_after_disc_and_tax() < price_excl_after_disc: - price_excl = variant._get_website_price_exclude_tax() - price_excl_after_disc = variant._get_website_price_after_disc_and_tax() - discount = variant._get_website_disc(0) - tax = variant._get_website_tax() - flashsale_data = variant._get_flashsale_price() - # add price tiering for base price, discount, and price after discount (tier 1 - 3) - tier1 = variant._get_pricelist_tier1() - tier2 = variant._get_pricelist_tier2() - tier3 = variant._get_pricelist_tier3() - else: - price_excl_after_disc = price_excl_after_disc - price_excl = price_excl - discount = discount - tax = tax - flashsale_data = flashsale_data - tier1 = tier1 - tier2 = tier2 - tier3 = tier3 - - solr_model = self.env['apache.solr'] - solr = solr_model.connect('variants') - document = solr_model.get_single_doc('variants', product.id) - document.update({ - 'id': product.id, - 'flashsale_id_i': flashsale_data['flashsale_id'] or 0, - 'flashsale_tag_s': flashsale_data['flashsale_tag'] or '', - 'flashsale_name_s': flashsale_data['flashsale_name'] or '', - 'flashsale_base_price_f': flashsale_data['flashsale_base_price'] or 0, - 'flashsale_discount_f': flashsale_data['flashsale_discount'] or 0, - 'flashsale_price_f': flashsale_data['flashsale_price'] or 0, - 'price_f': price_excl, - 'discount_f': discount, - 'price_discount_f': price_excl_after_disc, - 'tax_f': tax, - 'discount_tier1_f': tier1['discount_tier1'] or 0, - 'price_tier1_f': tier1['price_tier1'] or 0, - 'discount_tier2_f': tier2['discount_tier2'] or 0, - 'price_tier2_f': tier2['price_tier2'] or 0, - 'discount_tier3_f': tier3['discount_tier3'] or 0, - 'price_tier3_f': tier3['price_tier3'] or 0 - }) - solr.add([document]) - product.change_solr_data('Ada perubahan pada harga product')
\ No newline at end of file + manufacture_id = fields.Many2one('x_manufactures', string='Manufacture')
\ No newline at end of file diff --git a/indoteknik_custom/models/product_template.py b/indoteknik_custom/models/product_template.py index 3caa3125..f72fa763 100755 --- a/indoteknik_custom/models/product_template.py +++ b/indoteknik_custom/models/product_template.py @@ -1,6 +1,6 @@ from odoo import fields, models, api -from datetime import datetime, timedelta, timezone -from odoo.exceptions import AccessError, UserError, ValidationError +from datetime import datetime, timedelta +from odoo.exceptions import UserError import logging import requests import json @@ -50,13 +50,6 @@ class ProductTemplate(models.Model): is_new_product = fields.Boolean(string='Produk Baru', help='Centang jika ingin ditammpilkan di website sebagai segment Produk Baru') seq_new_product = fields.Integer(string='Seq New Product', help='Urutan Sequence New Product') - last_update_solr = fields.Datetime(string='Last Update Solr') - desc_update_solr = fields.Char(string='Desc Update Solr') - - def change_solr_data(self, desc): - current_time = datetime.utcnow() - self.desc_update_solr = desc - self.last_update_solr = current_time def _compute_virtual_rating(self): for product in self: @@ -251,62 +244,6 @@ class ProductTemplate(models.Model): } self.env['token.storage'].create([values]) return values - - @api.constrains( - 'name', - 'default_code', - 'weight', - 'x_manufacture', - 'public_categ_ids', - 'product_variant_ids.display_name', - 'product_variant_ids.default_code', - 'search_rank', - 'search_rank_weekly', - 'image_1920' - ) - def _sync_product_template_to_solr(self, limit=500): - template = self - if not template.active or template.type != 'product': - return - - variant_names = ', '.join([x.display_name or '' for x in template.product_variant_ids]) - variant_codes = ', '.join([x.default_code or '' for x in template.product_variant_ids]) - - category_id = 0 - category_name = '' - for category in template.public_categ_ids: - category_id, category_name = category.id, category.name - break - - solr_model = self.env['apache.solr'] - solr = solr_model.connect('product') - document = solr_model.get_single_doc('product', template.id) - document.update({ - 'id': template.id, - 'display_name_s': template.display_name, - 'name_s': template.name, - 'default_code_s': template.default_code or '', - 'product_rating_f': template.virtual_rating, - 'product_id_i': template.id, - 'image_s': self.env['ir.attachment'].api_image('product.template', 'image_512', template.id), - 'variant_total_i': template.product_variant_count, - 'stock_total_f': template.qty_stock_vendor, - 'weight_f': template.weight, - 'manufacture_id_i': template.x_manufacture.id or 0, - 'manufacture_name_s': template.x_manufacture.x_name or '', - 'manufacture_name': template.x_manufacture.x_name or '', - 'image_promotion_1_s': self.env['ir.attachment'].api_image('x_manufactures', 'image_promotion_1', template.x_manufacture.id), - 'image_promotion_2_s': self.env['ir.attachment'].api_image('x_manufactures', 'image_promotion_2', template.x_manufacture.id), - 'variants_name_t': variant_names, - 'variants_code_t': variant_codes, - 'search_rank_i': template.search_rank, - 'search_rank_weekly_i': template.search_rank_weekly, - 'category_id_i': category_id, - 'category_name_s': category_name, - 'category_name': category_name, - }) - solr.add([document]) - self.change_solr_data('Perubahan pada data product') class ProductProduct(models.Model): _inherit = "product.product" @@ -329,13 +266,6 @@ class ProductProduct(models.Model): qty_onhand_bandengan = fields.Float(string='Qty Incoming Bandengan', compute='_get_qty_onhand_bandengan') qty_incoming_bandengan = fields.Float(string='Qty Incoming Bandengan', compute='_get_qty_incoming_bandengan') sla_version = fields.Integer(string="SLA Version", default=0) - last_update_solr = fields.Datetime(string='Last Update Solr') - desc_update_solr = fields.Char(string='Desc Update Solr') - - def change_solr_data(self, desc): - current_time = datetime.now() - self.desc_update_solr = desc - self.last_update_solr = current_time def _get_qty_incoming_bandengan(self): for product in self: @@ -380,56 +310,4 @@ class ProductProduct(models.Model): ('end_date', '>=', current_time) ], limit=1) return pricelist - - @api.constrains( - 'name', - 'default_code', - 'virtual_rating', - 'weight', - 'x_manufacture', - 'public_categ_ids', - 'product_tmpl_id.default_code', - 'product_tmpl_id.image_1920', - 'search_rank', - 'search_rank_weekly', - 'image_1920' - ) - def _sync_variants_to_solr(self): - variant = self - if not variant.product_tmpl_id.active and variant.product_tmpl_id.type != 'product': - return - - category_id = 0 - category_name = '' - for category in variant.product_tmpl_id.public_categ_ids: - category_id = category.id - category_name = category.name - break - - solr_model = self.env['apache.solr'] - solr = solr_model.connect('variants') - document = solr_model.get_single_doc('variants', variant.id) - document.update({ - 'id': variant.id, - 'display_name_s': variant.display_name, - 'name_s': variant.name, - 'default_code_s': variant.default_code or '', - 'product_rating_f': variant.product_tmpl_id.virtual_rating, - 'product_id_i': variant.id, - 'template_id_i': variant.product_tmpl_id.id, - 'image_s': self.env['ir.attachment'].api_image('product.template', 'image_512', variant.product_tmpl_id.id), - 'stock_total_f': variant.qty_stock_vendor, - 'weight_f': variant.product_tmpl_id.weight, - 'manufacture_id_i': variant.product_tmpl_id.x_manufacture.id or 0, - 'manufacture_name_s': variant.product_tmpl_id.x_manufacture.x_name or '', - 'manufacture_name': variant.product_tmpl_id.x_manufacture.x_name or '', - 'image_promotion_1_s': self.env['ir.attachment'].api_image('x_manufactures', 'image_promotion_1', variant.product_tmpl_id.x_manufacture.id), - 'image_promotion_2_s': self.env['ir.attachment'].api_image('x_manufactures', 'image_promotion_2', variant.product_tmpl_id.x_manufacture.id), - 'category_id_i': category_id, - 'category_name_s': category_name, - 'category_name': category_name, - 'search_rank_i': variant.product_tmpl_id.search_rank, - 'search_rank_weekly_i': variant.product_tmpl_id.search_rank_weekly, - }) - solr.add([document]) - self.change_solr_data('Perubahan pada data product')
\ No newline at end of file +
\ No newline at end of file diff --git a/indoteknik_custom/models/solr/__init__.py b/indoteknik_custom/models/solr/__init__.py new file mode 100644 index 00000000..450e1dae --- /dev/null +++ b/indoteknik_custom/models/solr/__init__.py @@ -0,0 +1,5 @@ +from . import apache_solr +from . import product_pricelist_item +from . import product_product +from . import product_template +from . import website_categories_homepage
\ No newline at end of file diff --git a/indoteknik_custom/models/apache_solr.py b/indoteknik_custom/models/solr/apache_solr.py index bd4dd3dc..cb44e7ed 100644 --- a/indoteknik_custom/models/apache_solr.py +++ b/indoteknik_custom/models/solr/apache_solr.py @@ -16,12 +16,6 @@ class ApacheSolr(models.Model): _name = 'apache.solr' _order = 'id desc' - def get_single_doc(self, schema, id): - try: - return self.connect(schema).search(f'id:{id}').docs[0] - except: - return {} - def connect(self, schema): env = config.get('solr_env', 'development') @@ -33,6 +27,33 @@ class ApacheSolr(models.Model): return pysolr.Solr(url + schema, always_commit=True, timeout=30) + def get_single_doc(self, schema, id): + try: + return self.connect(schema).search(f'id:{id}').docs[0] + except: + return {} + + def get_docs(self, schema, ids): + ids_query = ' OR '.join(ids) + return self.connect(schema).search(f'id:({ids_query})').docs + + def clean_key_docs(self, docs): + cleaned_docs = [] + for doc in docs: + new_dict = self.clean_key_doc(doc) + cleaned_docs.append(new_dict) + return cleaned_docs + + def clean_key_doc(self, doc): + new_dict = {} + for key, value in doc.items(): + parts = key.split('_') + cleaned_parts = [part for part in parts if part not in ['s', 'i', 'f', 'd', 'b', 'l', 't', 'p']] + cleaned_key = '_'.join(cleaned_parts) + new_dict[cleaned_key] = value + return new_dict + + def _update_stock_product_to_solr(self, limit=10000): current_time = datetime.now() delta_time = current_time - timedelta(days=3) diff --git a/indoteknik_custom/models/solr/product_pricelist_item.py b/indoteknik_custom/models/solr/product_pricelist_item.py new file mode 100644 index 00000000..6ab2f588 --- /dev/null +++ b/indoteknik_custom/models/solr/product_pricelist_item.py @@ -0,0 +1,30 @@ +from odoo import models, api + + +class ProductPricelistItem(models.Model): + _inherit = 'product.pricelist.item' + + @api.constrains('applied_on', 'product_id', 'base', 'base_pricelist_id', 'price_discount') + def _constrains_related_solr_field(self): + self.update_template_solr() + self.update_variant_solr() + + def update_template_solr(self): + updated_template_ids = [] + for rec in self: + template = rec.product_id.product_tmpl_id + if template.id in updated_template_ids: + continue + + template._sync_price_to_solr() + updated_template_ids.append(template.id) + + def update_variant_solr(self): + updated_product_ids = [] + for rec in self: + product = rec.product_id + if product.id in updated_product_ids: + continue + + product._sync_price_to_solr() + updated_product_ids.append(product.id)
\ No newline at end of file diff --git a/indoteknik_custom/models/solr/product_product.py b/indoteknik_custom/models/solr/product_product.py new file mode 100644 index 00000000..5090b8d5 --- /dev/null +++ b/indoteknik_custom/models/solr/product_product.py @@ -0,0 +1,121 @@ +from odoo import models, fields +from datetime import datetime + + +class ProductProduct(models.Model): + _inherit = 'product.product' + + last_update_solr = fields.Datetime(string='Last Update Solr') + desc_update_solr = fields.Char(string='Desc Update Solr') + + def change_solr_data(self, desc): + self.desc_update_solr = desc + self.last_update_solr = datetime.utcnow() + + def solr(self): + return self.env['apache.solr'].connect('variants') + + def _sync_variants_to_solr(self): + solr_model = self.env['apache.solr'] + + for variant in self: + if not variant.product_tmpl_id.active and variant.product_tmpl_id.type != 'product': + continue + + category_id = 0 + category_name = '' + for category in variant.product_tmpl_id.public_categ_ids: + category_id = category.id + category_name = category.name + break + + document = solr_model.get_single_doc('variants', variant.id) + document.update({ + 'id': variant.id, + 'display_name_s': variant.display_name, + 'name_s': variant.name, + 'default_code_s': variant.default_code or '', + 'product_rating_f': variant.product_tmpl_id.virtual_rating, + 'product_id_i': variant.id, + 'template_id_i': variant.product_tmpl_id.id, + 'image_s': self.env['ir.attachment'].api_image('product.template', 'image_512', variant.product_tmpl_id.id), + 'stock_total_f': variant.qty_stock_vendor, + 'weight_f': variant.product_tmpl_id.weight, + 'manufacture_id_i': variant.product_tmpl_id.x_manufacture.id or 0, + 'manufacture_name_s': variant.product_tmpl_id.x_manufacture.x_name or '', + 'manufacture_name': variant.product_tmpl_id.x_manufacture.x_name or '', + 'image_promotion_1_s': self.env['ir.attachment'].api_image('x_manufactures', 'image_promotion_1', variant.product_tmpl_id.x_manufacture.id), + 'image_promotion_2_s': self.env['ir.attachment'].api_image('x_manufactures', 'image_promotion_2', variant.product_tmpl_id.x_manufacture.id), + 'category_id_i': category_id, + 'category_name_s': category_name, + 'category_name': category_name, + 'search_rank_i': variant.product_tmpl_id.search_rank, + 'search_rank_weekly_i': variant.product_tmpl_id.search_rank_weekly, + 'has_product_info_b': True + }) + self.solr().add([document]) + variant.change_solr_data('Perubahan pada data product') + + if not document.get('has_price_info_b'): + variant._sync_price_to_solr() + + self.solr().commit() + + def _sync_price_to_solr(self): + solr_model = self.env['apache.solr'] + + for variant in self: + price_excl_after_disc = price_excl = discount = tax = 0 + flashsale_data = tier1 = tier2 = tier3 = {} + + if price_excl_after_disc == 0 or variant._get_website_price_after_disc_and_tax() < price_excl_after_disc: + price_excl = variant._get_website_price_exclude_tax() + price_excl_after_disc = variant._get_website_price_after_disc_and_tax() + discount = variant._get_website_disc(0) + tax = variant._get_website_tax() + flashsale_data = variant._get_flashsale_price() + # add price tiering for base price, discount, and price after discount (tier 1 - 3) + tier1 = variant._get_pricelist_tier1() + tier2 = variant._get_pricelist_tier2() + tier3 = variant._get_pricelist_tier3() + + document = solr_model.get_single_doc('variants', variant.id) + document.update({ + 'id': variant.id, + 'flashsale_id_i': flashsale_data.get('flashsale_id', 0), + 'flashsale_tag_s': flashsale_data.get('flashsale_tag', ''), + 'flashsale_name_s': flashsale_data.get('flashsale_name', ''), + 'flashsale_base_price_f': flashsale_data.get('flashsale_base_price', 0), + 'flashsale_discount_f': flashsale_data.get('flashsale_discount', 0), + 'flashsale_price_f': flashsale_data.get('flashsale_price', 0), + 'price_f': price_excl, + 'discount_f': discount, + 'price_discount_f': price_excl_after_disc, + 'tax_f': tax, + 'discount_tier1_f': tier1.get('discount_tier1', 0), + 'price_tier1_f': tier1.get('price_tier1', 0), + 'discount_tier2_f': tier2.get('discount_tier2', 0), + 'price_tier2_f': tier2.get('price_tier2', 0), + 'discount_tier3_f': tier3.get('discount_tier3', 0), + 'price_tier3_f': tier3.get('price_tier3', 0), + 'has_price_info_b': True + }) + self.solr().add([document]) + variant.change_solr_data('Ada perubahan pada harga product') + + if not document.get('has_product_info_b'): + variant._sync_variants_to_solr() + + self.solr().commit() + + def sync_to_solr(self): + product_ids = self.env.context.get('active_ids', []) + products = self.search([('id', 'in', product_ids)]) + + updated_template_ids = [] + for product in products: + template = product.product_tmpl_id + if template.id in updated_template_ids: + continue + template._sync_product_template_to_solr() + updated_template_ids.append(template.id)
\ No newline at end of file diff --git a/indoteknik_custom/models/solr/product_template.py b/indoteknik_custom/models/solr/product_template.py new file mode 100644 index 00000000..6089adda --- /dev/null +++ b/indoteknik_custom/models/solr/product_template.py @@ -0,0 +1,158 @@ +from odoo import models, fields, api +from datetime import datetime + + +class ProductTemplate(models.Model): + _inherit = "product.template" + + last_update_solr = fields.Datetime(string='Last Update Solr') + desc_update_solr = fields.Char(string='Desc Update Solr') + + def change_solr_data(self, desc): + self.desc_update_solr = desc + self.last_update_solr = datetime.utcnow() + + def solr(self): + return self.env['apache.solr'].connect('product') + + @api.constrains('active') + def _sync_active_template_solr(self): + for template in self: + if not template.active or template.type != 'product': + self.solr().delete(template.id) + + variant_ids = [x.id for x in template.product_variant_ids] + template.product_variant_ids.solr().delete(variant_ids) + + self.solr().commit() + else: + template._sync_product_template_to_solr() + template._sync_price_to_solr() + + products = self.env['product.product'].search([('product_tmpl_id', '=', template.id), ('active', 'in', [True, False])]) + products._sync_variants_to_solr() + + @api.constrains( + 'name', + 'default_code', + 'weight', + 'x_manufacture', + 'public_categ_ids', + 'search_rank', + 'search_rank_weekly', + 'image_1920' + ) + def _sync_product_template_to_solr(self): + solr_model = self.env['apache.solr'] + + for template in self: + if not template.active or template.type != 'product': + continue + + variant_names = ', '.join([x.display_name or '' for x in template.product_variant_ids]) + variant_codes = ', '.join([x.default_code or '' for x in template.product_variant_ids]) + + category_id = 0 + category_name = '' + for category in template.public_categ_ids: + category_id, category_name = category.id, category.name + break + + document = solr_model.get_single_doc('product', template.id) + document.update({ + 'id': template.id, + 'display_name_s': template.display_name, + 'name_s': template.name, + 'default_code_s': template.default_code or '', + 'product_rating_f': template.virtual_rating, + 'product_id_i': template.id, + 'image_s': self.env['ir.attachment'].api_image('product.template', 'image_512', template.id), + 'variant_total_i': template.product_variant_count, + 'stock_total_f': template.qty_stock_vendor, + 'weight_f': template.weight, + 'manufacture_id_i': template.x_manufacture.id or 0, + 'manufacture_name_s': template.x_manufacture.x_name or '', + 'manufacture_name': template.x_manufacture.x_name or '', + 'image_promotion_1_s': self.env['ir.attachment'].api_image('x_manufactures', 'image_promotion_1', template.x_manufacture.id), + 'image_promotion_2_s': self.env['ir.attachment'].api_image('x_manufactures', 'image_promotion_2', template.x_manufacture.id), + 'variants_name_t': variant_names, + 'variants_code_t': variant_codes, + 'search_rank_i': template.search_rank, + 'search_rank_weekly_i': template.search_rank_weekly, + 'category_id_i': category_id, + 'category_name_s': category_name, + 'category_name': category_name, + 'has_product_info_b': True + }) + self.solr().add([document]) + template.product_variant_ids._sync_variants_to_solr() + self.change_solr_data('Perubahan pada data product') + + if not document.get('has_price_info_b'): + template._sync_price_to_solr() + + self.solr().commit() + + def _sync_price_to_solr(self): + solr_model = self.env['apache.solr'] + solr = self.solr() + + for template in self: + price_excl_after_disc = price_excl = discount = tax = 0 + flashsale_data = tier1 = tier2 = tier3 = {} + + for variant in template.product_variant_ids: + if price_excl_after_disc == 0 or variant._get_website_price_after_disc_and_tax() < price_excl_after_disc: + price_excl = variant._get_website_price_exclude_tax() + price_excl_after_disc = variant._get_website_price_after_disc_and_tax() + discount = variant._get_website_disc(0) + tax = variant._get_website_tax() + flashsale_data = variant._get_flashsale_price() + # add price tiering for base price, discount, and price after discount (tier 1 - 3) + tier1 = variant._get_pricelist_tier1() + tier2 = variant._get_pricelist_tier2() + tier3 = variant._get_pricelist_tier3() + + if template.product_variant_count == 1: + price_excl = template.product_variant_id._get_website_price_exclude_tax() + discount = template.product_variant_id._get_website_disc(0) + price_excl_after_disc = template.product_variant_id._get_website_price_after_disc_and_tax() + tax = template.product_variant_id._get_website_tax() + flashsale_data = template.product_variant_id._get_flashsale_price() + tier1 = template.product_variant_id._get_pricelist_tier1() + tier2 = template.product_variant_id._get_pricelist_tier2() + tier3 = template.product_variant_id._get_pricelist_tier3() + + document = solr_model.get_single_doc('product', template.id) + document.update({ + 'id': template.id, + 'flashsale_id_i': flashsale_data.get('flashsale_id', 0), + 'flashsale_tag_s': flashsale_data.get('flashsale_tag', ''), + 'flashsale_name_s': flashsale_data.get('flashsale_name', ''), + 'flashsale_base_price_f': flashsale_data.get('flashsale_base_price', 0), + 'flashsale_discount_f': flashsale_data.get('flashsale_discount', 0), + 'flashsale_price_f': flashsale_data.get('flashsale_price', 0), + 'price_f': price_excl, + 'discount_f': discount, + 'price_discount_f': price_excl_after_disc, + 'tax_f': tax, + 'discount_tier1_f': tier1.get('discount_tier1', 0), + 'price_tier1_f': tier1.get('price_tier1', 0), + 'discount_tier2_f': tier2.get('discount_tier2', 0), + 'price_tier2_f': tier2.get('price_tier2', 0), + 'discount_tier3_f': tier3.get('discount_tier3', 0), + 'price_tier3_f': tier3.get('price_tier3', 0), + 'has_price_info_b':True + }) + self.solr().add([document]) + template.change_solr_data('Ada perubahan pada harga product') + + if not document.get('has_product_info_b'): + template._sync_product_template_to_solr() + + self.solr().commit() + + def sync_to_solr(self): + template_ids = self.env.context.get('active_ids', []) + templates = self.search([('id', 'in', template_ids)]) + templates._sync_product_template_to_solr()
\ No newline at end of file diff --git a/indoteknik_custom/models/solr/website_categories_homepage.py b/indoteknik_custom/models/solr/website_categories_homepage.py new file mode 100644 index 00000000..59e7f32f --- /dev/null +++ b/indoteknik_custom/models/solr/website_categories_homepage.py @@ -0,0 +1,57 @@ +from odoo import models, fields, api +from datetime import datetime +import json + + +class WebsiteCategoriesHomepage(models.Model): + _inherit = 'website.categories.homepage' + + last_update_solr = fields.Datetime('Last Update Solr') + + def solr(self): + return self.env['apache.solr'].connect('product_category_homepage') + + def update_last_update_solr(self): + self.last_update_solr = datetime.utcnow() + + @api.constrains('status') + def _sync_status_category_homepage_solr(self): + for rec in self: + if rec.status == 'tayang': + rec._sync_category_homepage_to_solr() + else: + self.solr().delete(rec.id) + self.solr().commit() + + @api.constrains('category_id', 'image', 'url', 'sequence', 'product_ids') + def _sync_category_homepage_to_solr(self): + solr_model = self.env['apache.solr'] + + for category in self: + document = solr_model.get_single_doc('product_category_homepage', category.id) + products = [self.env['product.template'].api_single_response(x) for x in category.product_ids] + + document.update({ + 'id': category.id, + 'category_id_i': category.category_id.id, + 'name_s': category.category_id.name, + 'image_s': self.env['ir.attachment'].api_image('website.categories.homepage', 'image', category.id), + 'sequence_i': category.sequence or '', + 'url_s': category.url or '', + 'products_s': json.dumps(products, indent=None), + }) + self.solr().add([document]) + category.update_last_update_solr() + + self.solr().commit() + + def sync_to_solr(self): + category_ids = self.env.context.get('active_ids', []) + categories = self.search([('id', 'in', category_ids)]) + categories._sync_category_homepage_to_solr() + + def unlink(self): + res = super(WebsiteCategoriesHomepage, self).unlink() + self.solr().delete([x.id for x in self]) + self.solr().commit() + return res
\ No newline at end of file diff --git a/indoteknik_custom/models/website_categories_homepage.py b/indoteknik_custom/models/website_categories_homepage.py index 2f5b3d92..04632960 100644 --- a/indoteknik_custom/models/website_categories_homepage.py +++ b/indoteknik_custom/models/website_categories_homepage.py @@ -1,5 +1,4 @@ -from odoo import fields, models, api -import json +from odoo import fields, models class WebsiteCategoriesHomepage(models.Model): @@ -15,30 +14,3 @@ class WebsiteCategoriesHomepage(models.Model): ('tidak_tayang', 'Tidak Tayang') ], string='Status') product_ids = fields.Many2many('product.template', string='Product category') - - - @api.constrains('category_id', 'image', 'url', 'sequence', 'status', 'product_ids') - def _sync_category_homepage_to_solr(self): - solr_model = self.env['apache.solr'] - solr = solr_model.connect('product_category_homepage') - - for category in self: - document = solr_model.get_single_doc(solr, category.id) - result = [self.env['product.template'].api_single_response(x) for x in category.product_ids] - products = json.dumps(result, indent=None) - - document.update({ - 'id': category.id, - 'category_id_i': category.category_id.id, - 'name_s': category.category_id.name, - 'image_s': self.env['ir.attachment'].api_image('website.categories.homepage', 'image', category.id), - 'sequence_i': category.sequence or '', - 'url_s': category.url or '', - 'products_s': products, - }) - solr.add([document]) - - def sync_to_solr(self): - categories_ids = self.env.context.get('active_ids', []) - categories = self.env['website.categories.homepage'].search([('id', 'in', categories_ids)]) - categories._sync_category_homepage_to_solr() diff --git a/indoteknik_custom/views/product_product.xml b/indoteknik_custom/views/product_product.xml index d3ef3e15..3a10b4e0 100644 --- a/indoteknik_custom/views/product_product.xml +++ b/indoteknik_custom/views/product_product.xml @@ -12,5 +12,13 @@ </field> </field> </record> + + <record id="ir_actions_server_product_product_sync_to_solr" model="ir.actions.server"> + <field name="name">Sync to solr</field> + <field name="model_id" ref="product.model_product_product"/> + <field name="binding_model_id" ref="product.model_product_product"/> + <field name="state">code</field> + <field name="code">model.sync_to_solr()</field> + </record> </data> </odoo>
\ No newline at end of file diff --git a/indoteknik_custom/views/product_template.xml b/indoteknik_custom/views/product_template.xml index 82b7ce4d..bbbcc4e1 100755 --- a/indoteknik_custom/views/product_template.xml +++ b/indoteknik_custom/views/product_template.xml @@ -9,11 +9,13 @@ <field name="categ_id" position="after"> <field name="web_tax_id"/> <field name="x_manufacture"/> - <field name="desc_update_solr"/> - <field name="last_update_solr"/> <field name="x_model_product"/> <field name="x_studio_field_tGhJR" widget="many2many_tags"/> </field> + <field name="uom_po_id" position="after"> + <field name="desc_update_solr" readonly="1" /> + <field name="last_update_solr" readonly="1" /> + </field> <page name="inventory" position="after"> <page string="Marketplace" name="marketplace"> <group> @@ -74,5 +76,13 @@ </field> </field> </record> + + <record id="ir_actions_server_product_template_sync_to_solr" model="ir.actions.server"> + <field name="name">Sync to solr</field> + <field name="model_id" ref="product.model_product_template"/> + <field name="binding_model_id" ref="product.model_product_template"/> + <field name="state">code</field> + <field name="code">model.sync_to_solr()</field> + </record> </data> </odoo>
\ No newline at end of file diff --git a/indoteknik_custom/views/website_categories_homepage.xml b/indoteknik_custom/views/website_categories_homepage.xml index 0a3f684b..bd64f201 100644 --- a/indoteknik_custom/views/website_categories_homepage.xml +++ b/indoteknik_custom/views/website_categories_homepage.xml @@ -16,6 +16,7 @@ <field name="category_id"/> <field name="url"/> <field name="status"/> + <field name="last_update_solr"/> </tree> </field> </record> @@ -33,6 +34,7 @@ <field name="image" widget="image"/> <field name="url"/> <field name="status"/> + <field name="last_update_solr" readonly="1" /> </group> </group> <notebook> @@ -49,8 +51,8 @@ </field> </record> - <record id="website_categories_homepage_ir_actions_server" model="ir.actions.server"> - <field name="name">Sync To Solr</field> + <record id="ir_actions_server_website_categories_homepage_sync_to_solr" model="ir.actions.server"> + <field name="name">Sync to solr</field> <field name="model_id" ref="indoteknik_custom.model_website_categories_homepage"/> <field name="binding_model_id" ref="indoteknik_custom.model_website_categories_homepage"/> <field name="state">code</field> |
