from datetime import datetime from odoo import api, fields, models class ProductProduct(models.Model): _inherit = 'product.product' unpublished = fields.Boolean(string='Unpublished') 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_product_template_to_solr') product.product_tmpl_id._create_solr_queue('_sync_price_to_solr') product.solr_flag = 1 @api.constrains('active') def _constrains_active(self): for rec in self: if not rec.active: rec.unpublished = True def _sync_variants_to_solr(self): solr_model = self.env['apache.solr'] ir_attachment = self.env['ir.attachment'] 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 # Check if the product's inventory location is in ID 57 or 83 target_locations = [57, 83] stock_quant = self.env['stock.quant'].search([ ('product_id', 'in', variant.product_variant_ids.ids), ('location_id', 'in', target_locations), ]) is_in_bu = True if variant.qty_free_bandengan > 0 else False document = solr_model.get_doc('variants', variant.id) carousel = [ir_attachment.api_image('image.carousel', 'image', carousel.product_id.id) for carousel in variant.product_tmpl_id.image_carousel_lines], document.update({ 'id': variant.id, 'display_name_s': variant.display_name, 'name_s': variant.name, 'search_keywords_t': variant.display_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': ir_attachment.api_image('product.template', 'image_512', variant.product_tmpl_id.id), 'image_carousel_ss': [ir_attachment.api_image('image.carousel', 'image', carousel.id) for carousel in variant.product_tmpl_id.image_carousel_lines], 'image_mobile_s': ir_attachment.api_image('product.template', 'image_256', variant.product_tmpl_id.id), 'stock_total_f': variant.qty_free_bandengan, 'weight_f': variant.weight, 'has_magento_b': variant.has_magento, 'attribute_set_id_i': variant.product_tmpl_id.x_attribute_set_id or 0, 'attribute_set_name_s': variant.product_tmpl_id.x_attribute_set_name or '', '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': ir_attachment.api_image('x_manufactures', 'image_promotion_1', variant.product_tmpl_id.x_manufacture.id), 'image_promotion_2_s': ir_attachment.api_image('x_manufactures', 'image_promotion_2', variant.product_tmpl_id.x_manufacture.id), 'x_logo_manufacture_s': ir_attachment.api_image('x_manufactures', 'x_logo_manufacture', 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': not variant.unpublished, 'sni_b': variant.sni, 'tkdn_b': variant.tkdn, 'qty_sold_f': variant.qty_sold, "is_in_bu_b": is_in_bu, }) 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'] TIER_NUMBERS = ['1_v2', '2_v2', '3_v2', '4_v2', '5_v2'] for variant in self: flashsale_data = variant.with_context(price_for="web")._get_flashsale_price() flashsale_doc = { "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_doc = {} for tier_number in TIER_NUMBERS: tier = variant._get_pricelist_tier(tier_number) price_doc[f"discount_tier{tier_number}_f"] = tier.get(f'discount_tier{tier_number}', 0) price_doc[f"price_tier{tier_number}_f"] = tier.get(f'price_tier{tier_number}', 0) document = solr_model.get_doc('variants', variant.id) document['id'] = variant.id document.update(flashsale_doc) document.update(price_doc) 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 @api.constrains('unpublished') def _constrains_unpublished(self): for rec in self: if rec.product_variant_count == 1: rec.product_tmpl_id.unpublished = rec.unpublished rec.product_tmpl_id._create_solr_queue_sync_product_template()