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 variant_solr_flag_to_solr(self, limit=500): variant_products = self.search([('solr_flag', '=', 2)], limit=limit) for product in variant_products: product.product_tmpl_id._create_solr_queue('_sync_price_to_solr') product.solr_flag = 1 def _sync_variants_to_solr(self): solr_model = self.env['apache.solr'] for variant in self: 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, 'attributes': [x.name for x in variant.product_template_attribute_value_ids], 'has_product_info_b': True, 'publish_b': variant.product_tmpl_id.active and variant.product_tmpl_id.type == 'product', }) self.solr().add(docs=[document], softCommit=True) 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 = {} 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() tax = variant._get_website_tax() discount = variant._get_website_disc(0) flashsale_data = variant._get_flashsale_price() price_excl_v2 = variant._v2_get_website_price_exclude_tax() price_excl_after_disc_v2 = variant._v2_get_website_price_after_disc_and_tax() tax_v2 = variant._v2_get_website_tax() 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_start_date_s": flashsale_data.get('flashsale_start_date', ''), "flashsale_end_date_s": flashsale_data.get('flashsale_end_date', ''), "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, "price_v2_f": price_excl_v2, "price_discount_v2_f": price_excl_after_disc_v2, "tax_v2_f": tax_v2, }) for tier_number in [1, 2, 3, '1_v2', '2_v2', '3_v2', '4_v2', '5_v2']: tier = variant._get_pricelist_tier(tier_number) document.update({ f"discount_tier{tier_number}_f": tier.get(f'discount_tier{tier_number}', 0), f"price_tier{tier_number}_f": tier.get(f'price_tier{tier_number}', 0), }) # for tier_number in [1, 2, 3, '1_v2', '2_v2', '3_v2', '4_v2', '5_v2']: # tier = tier_data[tier_number] document.update({"has_price_info_b": True}) self.solr().add(docs=[document], softCommit=True) 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.context.get('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, 'attributes': [x.name for x in product.product_template_attribute_value_ids], 'is_flashsale': True if flashsale else False } 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