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: document = solr_model.get_doc('product', template.id) flashsale_data = {} for variant in template.product_variant_ids: variant_flashsale = variant._get_flashsale_price() variant_flashsale_price = variant_flashsale.get('flashsale_price', 0) flashsale_data_price = flashsale_data.get('flashsale_price', 0) if flashsale_data_price == 0 or (variant_flashsale_price != 0 and variant_flashsale_price < flashsale_data_price): flashsale_data = variant_flashsale 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() tier1 = variant._get_pricelist_tier1() tier2 = variant._get_pricelist_tier2() tier3 = variant._get_pricelist_tier3() 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.product_variant_ids._sync_price_to_solr() 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