1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
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()
|