From a9a3c3e8945dd11a0d81f64a5629876397a1e51d Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Fri, 25 Aug 2023 15:11:00 +0700 Subject: Update sync solr function --- indoteknik_custom/models/__init__.py | 2 +- indoteknik_custom/models/apache_solr.py | 295 ------------------- indoteknik_custom/models/product_pricelist.py | 123 +------- indoteknik_custom/models/product_template.py | 128 +-------- indoteknik_custom/models/solr/__init__.py | 5 + indoteknik_custom/models/solr/apache_solr.py | 316 +++++++++++++++++++++ .../models/solr/product_pricelist_item.py | 30 ++ indoteknik_custom/models/solr/product_product.py | 121 ++++++++ indoteknik_custom/models/solr/product_template.py | 158 +++++++++++ .../models/solr/website_categories_homepage.py | 57 ++++ .../models/website_categories_homepage.py | 30 +- indoteknik_custom/views/product_product.xml | 8 + indoteknik_custom/views/product_template.xml | 14 +- .../views/website_categories_homepage.xml | 6 +- 14 files changed, 718 insertions(+), 575 deletions(-) delete mode 100644 indoteknik_custom/models/apache_solr.py create mode 100644 indoteknik_custom/models/solr/__init__.py create mode 100644 indoteknik_custom/models/solr/apache_solr.py create mode 100644 indoteknik_custom/models/solr/product_pricelist_item.py create mode 100644 indoteknik_custom/models/solr/product_product.py create mode 100644 indoteknik_custom/models/solr/product_template.py create mode 100644 indoteknik_custom/models/solr/website_categories_homepage.py 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/apache_solr.py b/indoteknik_custom/models/apache_solr.py deleted file mode 100644 index bd4dd3dc..00000000 --- a/indoteknik_custom/models/apache_solr.py +++ /dev/null @@ -1,295 +0,0 @@ -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 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') - - 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 _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_template_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)] - - templates = self.env['product.template'].search(query, limit=limit) - documents = [] - counter = 0 - for template in templates: - template_time = time.time() - counter += 1 - variants_name = variants_code = '' - if template.product_variant_count > 1: - for variant in template.product_variant_ids: - variants_name += variant.display_name or ''+', ' - variants_code += variant.default_code or ''+', ' - - category_id = '' - category_name = '' - for category in template.public_categ_ids: - category_id = category.id - category_name = category.name - - document = { - # Start Product Template - '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': variants_name, - 'variants_code_t': variants_code, - 'search_rank_i': template.search_rank, - 'search_rank_weekly_i': template.search_rank_weekly, - 'category_id_i': category_id or 0, - 'category_name_s': category_name or '', - 'category_name': category_name or '', - # End Product Template - } - documents.append(document) - template.solr_flag = 1 - _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 _sync_product_pricelist_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)] - - 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() - - document = { - # Start Product Pricelist - '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 - # End Product Pricelist - } - 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/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/solr/apache_solr.py b/indoteknik_custom/models/solr/apache_solr.py new file mode 100644 index 00000000..cb44e7ed --- /dev/null +++ b/indoteknik_custom/models/solr/apache_solr.py @@ -0,0 +1,316 @@ +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_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) + + 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_template_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)] + + templates = self.env['product.template'].search(query, limit=limit) + documents = [] + counter = 0 + for template in templates: + template_time = time.time() + counter += 1 + variants_name = variants_code = '' + if template.product_variant_count > 1: + for variant in template.product_variant_ids: + variants_name += variant.display_name or ''+', ' + variants_code += variant.default_code or ''+', ' + + category_id = '' + category_name = '' + for category in template.public_categ_ids: + category_id = category.id + category_name = category.name + + document = { + # Start Product Template + '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': variants_name, + 'variants_code_t': variants_code, + 'search_rank_i': template.search_rank, + 'search_rank_weekly_i': template.search_rank_weekly, + 'category_id_i': category_id or 0, + 'category_name_s': category_name or '', + 'category_name': category_name or '', + # End Product Template + } + documents.append(document) + template.solr_flag = 1 + _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 _sync_product_pricelist_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)] + + 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() + + document = { + # Start Product Pricelist + '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 + # End Product Pricelist + } + 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/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 @@ + + + Sync to solr + + + code + model.sync_to_solr() + \ 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 @@ - - + + + + @@ -74,5 +76,13 @@ + + + Sync to solr + + + code + model.sync_to_solr() + \ 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 @@ + @@ -33,6 +34,7 @@ + @@ -49,8 +51,8 @@ - - Sync To Solr + + Sync to solr code -- cgit v1.2.3