diff options
Diffstat (limited to 'indoteknik_custom/models/solr')
| -rw-r--r-- | indoteknik_custom/models/solr/__init__.py | 6 | ||||
| -rw-r--r-- | indoteknik_custom/models/solr/apache_solr.py | 285 | ||||
| -rw-r--r-- | indoteknik_custom/models/solr/apache_solr_queue.py | 69 | ||||
| -rw-r--r-- | indoteknik_custom/models/solr/product_pricelist_item.py | 30 | ||||
| -rw-r--r-- | indoteknik_custom/models/solr/product_product.py | 168 | ||||
| -rw-r--r-- | indoteknik_custom/models/solr/product_template.py | 233 | ||||
| -rw-r--r-- | indoteknik_custom/models/solr/website_categories_homepage.py | 81 |
7 files changed, 872 insertions, 0 deletions
diff --git a/indoteknik_custom/models/solr/__init__.py b/indoteknik_custom/models/solr/__init__.py new file mode 100644 index 00000000..45ef4299 --- /dev/null +++ b/indoteknik_custom/models/solr/__init__.py @@ -0,0 +1,6 @@ +from . import apache_solr +from . import apache_solr_queue +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/solr/apache_solr.py b/indoteknik_custom/models/solr/apache_solr.py new file mode 100644 index 00000000..518280ac --- /dev/null +++ b/indoteknik_custom/models/solr/apache_solr.py @@ -0,0 +1,285 @@ +from odoo import models, api, fields +from odoo.exceptions import UserError +from datetime import datetime, timedelta +import logging +import pysolr +import time +from odoo.tools.config import config + +_logger = logging.getLogger(__name__) +_solr = pysolr.Solr('http://10.148.0.5:8983/solr/product/', always_commit=True, timeout=30) +_variants_solr = pysolr.Solr('http://10.148.0.5:8983/solr/variants/', always_commit=True, timeout=30) +# _solr = pysolr.Solr('http://34.101.189.218:8983/solr/product/', always_commit=True, timeout=30) # for development only + + +class ApacheSolr(models.Model): + _name = 'apache.solr' + _order = 'id desc' + + def connect(self, schema): + env = config.get('solr_env', 'development') + + url = '' + if env == 'development': + url = 'http://192.168.23.5:8983/solr/' + elif env == 'production': + url = 'http://10.148.0.5:8983/solr/' + + return pysolr.Solr(url + schema, always_commit=True, timeout=30) + + def get_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) + + current_time = current_time.strftime('%Y-%m-%d %H:%M:%S') + delta_time = delta_time.strftime('%Y-%m-%d %H:%M:%S') + + query = [('__last_update', '<', delta_time),] + # query = [('product_variant_id.id', '=', 204903)] + stocks = self.env['stock.vendor'].search(query, limit=limit) + documents = [] + for stock in stocks: + stock_total = int(stock.product_variant_id.product_tmpl_id.qty_stock_vendor) + # dict_stock = dict({"set": stock_total}) + document = { + "id": int(stock.product_variant_id.product_tmpl_id.id), + "stock_total_f": stock_total + } + documents.append(document) + _logger.info("[SYNC_PRODUCT_STOCK_TO_SOLR] Success Set to solr product %s" % stock.product_variant_id.product_tmpl_id.id) + _solr.add(documents, fieldUpdates={'stock_total_f':'set'}) + + def _update_rating_product_to_solr(self, limit=1000): + current_time = datetime.now() + delta_time = current_time - timedelta(days=30) + + current_time = current_time.strftime('%Y-%m-%d %H:%M:%S') + delta_time = delta_time.strftime('%Y-%m-%d %H:%M:%S') + query = [ + '&','&', + ('type', '=', 'product'), + ('active', '=', True), + '|', + ('last_calculate_rating', '=', False), + ('last_calculate_rating', '<', delta_time), + ] + # query = [('id', '=', 97942)] + templates = self.env['product.template'].search(query, limit=limit) + documents=[] + for template in templates: + rating = int(template.virtual_rating) + # dict_rating = dict({'set':rating}) + document = { + 'id':template.id, + 'product_rating_f':rating + } + documents.append(document) + template.last_calculate_rating = current_time + _logger.info("[SYNC_PRODUCT_RATING_TO_SOLR] Success Set to solr product %s" % template.id) + _logger.info(documents) + _solr.add(documents, fieldUpdates={'product_rating_f':'set'}) + + def _sync_product_to_solr(self, limit=500): + start_time = time.time() + _logger.info('run sync to solr...') + query = ["&","&",("type","=","product"),("active","=",True),"|",("solr_flag","=",0),("solr_flag","=",2)] + # query = [('id', '=', 97942)] + + templates = self.env['product.template'].search(query, limit=limit) + documents = [] + counter = 0 + for template in templates: + template_time = time.time() + counter += 1 + price_excl_after_disc = price_excl = discount = tax = 0 + variants_name = variants_code = '' + flashsale_data = tier1 = tier2 = tier3 = {} + 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 + variants_name += variant.display_name or ''+', ' + variants_code += variant.default_code or ''+', ' + else: + variants_name = template.display_name + 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() + + category_id = '' + category_name = '' + for category in template.public_categ_ids: + category_id = category.id + category_name = category.name + + document = { + '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), + 'price_f': price_excl, + 'discount_f': discount, + 'price_discount_f': price_excl_after_disc, + 'tax_f': tax, + '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), + 'category_id_i': category_id or 0, + 'category_name_s': category_name or '', + 'category_name': category_name or '', + 'variants_name_t': variants_name, + 'variants_code_t': variants_code, + 'search_rank_i': template.search_rank, + 'search_rank_weekly_i': template.search_rank_weekly, + '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, + '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 + } + documents.append(document) + template.solr_flag = 1 + # add counter for monitoring + _logger.info('[SYNC_PRODUCT_TO_SOLR] {}/{} {:.6f}'.format(counter, limit, time.time() - template_time)) + _logger.info('[SYNC_PRODUCT_TO_SOLR] Success add to solr product %s' % template.id) + _solr.add(documents) + end_time = time.time() + _logger.info("[SYNC_PRODUCT_TO_SOLR] Finish task add to solr. Time taken: {:.6f} seconds".format(end_time - start_time)) + + def _test_product_price(self, product_id=228178): + product = self.env['product.product'].search([('id', '=', product_id)], limit=1) + _logger.info('price incl tax: %s' % product._get_website_price_include_tax()) + _logger.info('price excl tax: %s' % product._get_website_price_exclude_tax()) + _logger.info('discount : %s' % product._get_website_disc(0)) + _logger.info('price after discount : %s' % product._get_website_price_after_disc()) + _logger.info('price excl after discount : %s' % product._get_website_price_after_disc_and_tax()) + _logger.info('tax : %s' % product._get_website_tax()) + + def _sync_variants_to_solr(self, limit=500): + start_time = time.time() + _logger.info('run sync to solr variants...') + query = ["&","&","&",("product_tmpl_id.active","=",True),("product_tmpl_id.type","=","product"),("active","=",True),"|",("solr_flag","=",0),("solr_flag","=",2)] + # query = [('id', 'in', [184622, 204903])] + + variants = self.env['product.product'].search(query, limit=limit) + documents = [] + counter = 0 + for variant in variants: + template_time = time.time() + counter += 1 + + price_excl_after_disc = price_excl = discount = tax = 0 + 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() + + category_id = '' + category_name = '' + for category in variant.product_tmpl_id.public_categ_ids: + category_id = category.id + category_name = category.name + + document = { + '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), + 'price_f': price_excl, + 'discount_f': discount, + 'price_discount_f': price_excl_after_disc, + 'tax_f': tax, + '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 or 0, + 'category_name_s': category_name or '', + 'category_name': category_name or '', + 'search_rank_i': variant.product_tmpl_id.search_rank, + 'search_rank_weekly_i': variant.product_tmpl_id.search_rank_weekly, + 'flashsale_id_i': flashsale_data['flashsale_id'] or 0, + '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, + } + documents.append(document) + variant.solr_flag = 1 + # add counter for monitoring + _logger.info('[SYNC_VARIANTS_TO_SOLR] {}/{} {:.6f}'.format(counter, limit, time.time() - template_time)) + _logger.info('[SYNC_VARIANTS_TO_SOLR] Success add to solr variants %s' % variant.id) + _variants_solr.add(documents) + end_time = time.time() + _logger.info("[SYNC_VARIANTS_TO_SOLR] Finish task add to solr. Time taken: {:.6f} seconds".format(end_time - start_time)) diff --git a/indoteknik_custom/models/solr/apache_solr_queue.py b/indoteknik_custom/models/solr/apache_solr_queue.py new file mode 100644 index 00000000..eb5a99a8 --- /dev/null +++ b/indoteknik_custom/models/solr/apache_solr_queue.py @@ -0,0 +1,69 @@ +from odoo import models, fields +from datetime import datetime +import logging + + +_logger = logging.getLogger(__name__) + +class ApacheSolrQueue(models.Model): + _name = 'apache.solr.queue' + + display_name = fields.Char('Display Name', compute="_compute_display_name") + res_model = fields.Char('Resource Model') + res_id = fields.Integer('Resource ID') + function_name = fields.Char('Function Name') + execute_status = fields.Selection([ + ('success', 'Success'), + ('failed', 'Failed'), + ('not_found', 'Record not found') + ], 'Execute Status') + execute_date = fields.Datetime('Execute Date') + + def _compute_display_name(self): + for rec in self: + try: + res_model = rec.res_model + res_id = int(rec.res_id) + model_instance = self.env[res_model].browse(res_id) + rec.display_name = model_instance.display_name or '' + except: + rec.display_name = '' + + def process_queue_item(self, limit=100): + records = self.search([('execute_status', '=', False)], order='create_date asc', limit=limit) + for rec in records: + rec.execute_queue() + + def execute_queue(self): + for rec in self: + try: + res_model = rec.res_model + res_id = int(rec.res_id) + function_name = rec.function_name + _logger.info(f'Execute Queue: {res_model}.{function_name}() -> {res_id}') + + domain = [('id', '=', res_id)] + if res_model in ['product.template']: + domain.append(('active', 'in', [True, False])) + + model_instance = self.env[res_model].search(domain) + if model_instance: + getattr(model_instance, function_name)() + rec.execute_status = 'success' + else: + rec.execute_status = 'not_found' + except: + rec.execute_status = 'failed' + rec.execute_date = datetime.utcnow() + self.env.cr.commit() + + def create_unique(self, payload={}): + count = self.search_count([ + ('res_model', '=', payload['res_model']), + ('res_id', '=', payload['res_id']), + ('function_name', '=', payload['function_name']), + ('execute_status', '=', False) + ]) + if count == 0: + self.create(payload) + 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..48ee9daa --- /dev/null +++ b/indoteknik_custom/models/solr/product_product.py @@ -0,0 +1,168 @@ +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 action_sync_to_solr(self): + product_ids = self.env.context.get('active_ids', []) + products = self.search([('id', 'in', product_ids)]) + for product in products: + product.product_tmpl_id._create_solr_queue('_sync_product_template_to_solr') + + 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_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() + + 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_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() + + def _sync_delete_solr(self): + for rec in self: + self.solr().delete(rec.id) + + def solr_results(self): + solr_model = self.env['apache.solr'] + pricelist = self.env.user_pricelist + price_tier = pricelist.get_tier_name() + + results = [] + for product in self: + doc = solr_model.get_doc('variants', product.id) + if len(doc) == 0: continue + + discount_key = 'discount_f' + price_discount_key = 'price_discount_f' + if price_tier: + discount_key = f'discount_{price_tier}_f' + price_discount_key = f'price_{price_tier}_f' + + flashsale = product._get_active_flash_sale() + if flashsale: + discount_key = 'flashsale_discount_f' + price_discount_key = 'flashsale_price_f' + + result = { + 'id': doc.get('id'), + 'parent': { + 'id': doc.get('template_id_i'), + 'name': doc.get('name_s'), + 'image': doc.get('image_s'), + }, + 'code': doc.get('default_code_s'), + 'name': doc.get('display_name_s'), + 'price': { + 'price': doc.get('price_f'), + 'discount_percentage': doc.get(discount_key), + 'price_discount': doc.get(price_discount_key) + }, + 'stock': doc.get('stock_total_f'), + 'weight': doc.get('weight_f'), + 'manufacture': None + } + + manufacture_id = doc.get('manufacture_id_i') + if manufacture_id: + result['manufacture'] = { + 'id': manufacture_id, + 'name': doc.get('manufacture_name_s'), + 'image_promotion_1': doc.get('image_promotion_1_s'), + 'image_promotion_2': doc.get('image_promotion_2_s'), + } + + results.append(result) + + return results
\ 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..6ae0bec2 --- /dev/null +++ b/indoteknik_custom/models/solr/product_template.py @@ -0,0 +1,233 @@ +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') + + def _create_solr_queue(self, function_name): + for rec in self: + self.env['apache.solr.queue'].create_unique({ + 'res_model': self._name, + 'res_id': rec.id, + 'function_name': function_name + }) + + @api.constrains('active') + def _create_solr_queue_sync_active(self): + self._create_solr_queue('_sync_active_template_solr') + + @api.constrains('name', 'default_code', 'weight', 'x_manufacture', 'public_categ_ids', 'search_rank', 'search_rank_weekly', 'image_1920') + def _create_solr_queue_sync_product_template(self): + self._create_solr_queue('_sync_product_template_to_solr') + + def action_sync_to_solr(self): + template_ids = self.env.context.get('active_ids', []) + templates = self.search([('id', 'in', template_ids)]) + templates._create_solr_queue('_sync_product_template_to_solr') + + def _sync_active_template_solr(self): + for template in self: + if not template.active or template.type != 'product': + template._sync_delete_solr() + 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() + + 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_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_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_delete_solr(self): + for rec in self: + self.solr().delete(rec.id) + for variant in rec.product_variant_ids: + variant._sync_delete_solr() + self.solr().optimize() + self.solr().commit() + + def solr_results(self, detail=False): + solr_model = self.env['apache.solr'] + pricelist = self.env.user_pricelist + price_tier = pricelist.get_tier_name() + + results = [] + for template in self: + doc = solr_model.get_doc('product', template.id) + if len(doc) == 0: continue + + discount_key = 'discount_f' + price_discount_key = 'price_discount_f' + if price_tier: + discount_key = f'discount_{price_tier}_f' + price_discount_key = f'price_{price_tier}_f' + + flashsale = template._get_active_flash_sale() + if flashsale: + discount_key = 'flashsale_discount_f' + price_discount_key = 'flashsale_price_f' + + result = { + 'id': doc.get('id'), + 'image': doc.get('image_s'), + 'code': doc.get('default_code_s'), + 'display_name': doc.get('display_name_s'), + 'name': doc.get('name_s'), + 'variant_total': doc.get('variant_total_i'), + 'weight': doc.get('weight_f'), + 'manufacture': None, + 'categories': [], + 'flash_sale': { + 'remaining_time': flashsale._remaining_time_in_second() or 0, + 'tag': flashsale.flashsale_tag or None + }, + 'lowest_price': { + 'price': doc.get('price_f'), + 'discount_percentage': doc.get(discount_key), + 'price_discount': doc.get(price_discount_key) + } + } + + manufacture_id = doc.get('manufacture_id_i') + if manufacture_id: + result['manufacture'] = { + 'id': manufacture_id, + 'name': doc.get('manufacture_name_s'), + 'image_promotion_1': doc.get('image_promotion_1_s'), + 'image_promotion_2': doc.get('image_promotion_2_s'), + } + + category_id = doc.get('category_id_i') + if category_id: + result['categories'] = [{ + 'id': category_id, + 'name': doc.get('category_name_s'), + }] + + if detail == True: + result['variants'] = template.product_variant_ids.solr_results() + result['description'] = template.website_description or '' + + results.append(result) + + return results 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..1b517ae6 --- /dev/null +++ b/indoteknik_custom/models/solr/website_categories_homepage.py @@ -0,0 +1,81 @@ +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() + + def _create_solr_queue(self, function_name): + for rec in self: + self.env['apache.solr.queue'].create_unique({ + 'res_model': self._name, + 'res_id': rec.id, + 'function_name': function_name + }) + + @api.constrains('status') + def _create_solr_queue_sync_status(self): + self._create_solr_queue('_sync_status_category_homepage_solr') + + @api.constrains('category_id', 'image', 'url', 'sequence', 'product_ids') + def _create_solr_queue_sync_category_homepage(self): + self._create_solr_queue('_sync_category_homepage_to_solr') + + def action_sync_to_solr(self): + category_ids = self.env.context.get('active_ids', []) + categories = self.search([('id', 'in', category_ids)]) + categories._create_solr_queue('_sync_category_homepage_to_solr') + + def unlink(self): + res = super(WebsiteCategoriesHomepage, self).unlink() + for rec in self: + self.env['apache.solr.queue'].create_unique({ + 'res_model': self._name, + 'res_id': rec.id, + 'function_name': '_sync_delete_solr' + }) + return res + + def _sync_status_category_homepage_solr(self): + for rec in self: + if rec.status == 'tayang': + rec._sync_category_homepage_to_solr() + else: + rec._sync_delete_solr() + + def _sync_category_homepage_to_solr(self): + solr_model = self.env['apache.solr'] + + for category in self: + if category.status == 'tidak_tayang': + continue + + document = solr_model.get_doc('product_category_homepage', category.id) + 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 '', + 'product_ids': [x.id for x in category.product_ids] + }) + self.solr().add([document]) + category.update_last_update_solr() + + self.solr().commit() + + def _sync_delete_solr(self): + for rec in self: + self.solr().delete(rec.id) + self.solr().optimize() + self.solr().commit()
\ No newline at end of file |
