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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
from odoo import models
from odoo.http import request
import math
class ProductProduct(models.Model):
_inherit = 'product.product'
def api_single_response(self, product_product):
product_pricelist_default_discount_id = self.env['ir.config_parameter'].get_param('product.pricelist.default_discount_id')
product_pricelist_default_discount_id = int(product_pricelist_default_discount_id)
product_template = product_product.product_tmpl_id
stock = product_product.qty_stock_vendor
stock = stock if stock > 0 else 1
data = {
'id': product_product.id,
'parent': {
'id': product_template.id,
'name': product_template.name,
'image': self.env['ir.attachment'].api_image('product.template', 'image_256', product_template.id),
},
'code': product_product.default_code or '',
'name': product_product.display_name,
'price': self.env['product.pricelist'].compute_price(product_pricelist_default_discount_id, product_product.id),
'stock': stock,
'weight': product_product.weight,
'attributes': [x.name for x in product_product.product_template_attribute_value_ids],
'manufacture' : self.api_manufacture(product_product)
}
return data
def v2_api_single_response(self, product_product):
product_template = product_product.product_tmpl_id
data = {
'id': product_product.id,
'parent': {
'id': product_template.id,
'name': product_template.name,
'image': self.env['ir.attachment'].api_image('product.template', 'image_256', product_template.id),
},
'code': product_product.default_code or '',
'name': product_product.display_name,
'price': product_product.calculate_website_price(),
'stock': product_product.qty_stock_vendor,
'weight': product_product.weight,
'attributes': [x.name for x in product_product.product_template_attribute_value_ids],
'manufacture' : self.api_manufacture(product_product)
}
return data
def has_active_program(self):
program_line = self.env['promotion.program.line']
product_promotions = program_line.get_active_promotions(self.id)
return True if len(product_promotions) > 0 else False
def calculate_website_price(self):
pricelist = self.env.user_pricelist
config = self.env['ir.config_parameter']
product_pricelist_tier1 = int(config.get_param('product.pricelist.tier1'))
product_pricelist_tier2 = int(config.get_param('product.pricelist.tier2'))
product_pricelist_tier3 = int(config.get_param('product.pricelist.tier3'))
discount_percentage = self._get_website_disc(0)
price_discount = self._get_website_price_after_disc_and_tax()
price_tier = False
pricelists = {
'tier1': self._get_pricelist_tier1,
'tier2': self._get_pricelist_tier2,
'tier3': self._get_pricelist_tier3,
}
pricelist_id = pricelist.id if pricelist else False
if pricelist_id == product_pricelist_tier1: price_tier = 'tier1'
if pricelist_id == product_pricelist_tier2: price_tier = 'tier2'
if pricelist_id == product_pricelist_tier3: price_tier = 'tier3'
if price_tier:
price = pricelists[price_tier]()
discount_key = 'discount_%s' % price_tier
price_key = 'price_%s' % price_tier
if price[discount_key] > 0: discount_percentage = price[discount_key]
if price[price_key] > 0: price_discount = price[price_key]
return {
'price': self._get_website_price_exclude_tax(),
'discount_percentage': discount_percentage,
'price_discount': price_discount
}
def api_manufacture(self, product_template):
if product_template.x_manufacture:
manufacture = product_template.x_manufacture
return {
'id': manufacture.id,
'name': manufacture.x_name,
'image_promotion_1': self.env['ir.attachment'].api_image('x_manufactures', 'image_promotion_1', manufacture.id),
'image_promotion_2': self.env['ir.attachment'].api_image('x_manufactures', 'image_promotion_2', manufacture.id),
}
return {}
def _is_have_flashsale(self):
active_flashsale = self.env['product.pricelist'].get_active_flash_sale()
for pricelist in active_flashsale:
found_product = self.env['product.pricelist.item'].search([('pricelist_id', '=', pricelist.id), ('product_id', '=', self.id)])
if found_product:
return True
return False
def _get_website_price_include_tax(self):
default_pricelist_id = int(1)
query = [
('pricelist_id.id', '=', default_pricelist_id),
('product_id.id', '=', self.id)
]
pl_item1 = self.env['product.pricelist.item'].search(query, limit=1)
retValue = pl_item1.fixed_price
retValue = math.floor(retValue)
return retValue
def _get_website_price_exclude_tax(self):
default_divide_tax = float(1.11)
price_incl = self._get_website_price_include_tax()
res = price_incl / default_divide_tax
return math.floor(res)
def _get_website_disc(self, partner_id):
default_discount_id = int(4)
# compile partner
partner = self.env['res.partner'].search([('id', '=', partner_id)], limit=1)
if partner:
if not partner.parent_id: # it means this is a parent
default_discount_id = partner.custom_pricelist_id
else: # it means this is NOT parent
default_discount_id = partner.parent_id.custom_pricelist_id
query = [
('pricelist_id.id', '=', default_discount_id),
('product_id.id', '=', self.id)
]
pl_item2 = self.env['product.pricelist.item'].search(query, limit=1)
retValue = pl_item2.price_discount
return retValue
def _get_website_price_after_disc_and_tax(self):
default_divide_tax = float(1.11)
price_after_disc = self._get_website_price_after_disc()
res = price_after_disc / default_divide_tax
res = math.ceil(res)
return res
def _get_website_price_after_disc(self):
discount = self._get_website_disc(0)
price_incl = self._get_website_price_include_tax()
res = 0
if discount > 0:
res = price_incl - (price_incl * discount / 100)
else:
res = price_incl
return math.floor(res)
def _get_website_tax(self):
default_percent_tax = float(11)
price_after_disc = self._get_website_price_after_disc_and_tax()
res = price_after_disc * default_percent_tax / 100
return math.floor(res)
def _get_pricelist_tier1(self):
product_pricelist_tier1 = int(self.env['ir.config_parameter'].get_param('product.pricelist.tier1'))
default_divide_tax = float(1.11)
base_price = discount = price = 0
pricelist_item = self.env['product.pricelist.item'].search([
('pricelist_id', '=', product_pricelist_tier1),
('product_id', '=', self.id)
], limit=1)
if pricelist_item:
# base_price = self._get_website_price_exclude_tax()
base_price_incl = self._get_website_price_include_tax()
discount = pricelist_item.price_discount
price = base_price_incl - (base_price_incl * discount / 100)
price = price / default_divide_tax
price = math.floor(price)
data = {
# 'base_price_tier1': base_price or 0,
'discount_tier1': discount or 0,
'price_tier1': price or 0
}
return data
def _get_pricelist_tier2(self):
product_pricelist_tier2 = int(self.env['ir.config_parameter'].get_param('product.pricelist.tier2'))
default_divide_tax = float(1.11)
base_price = discount = price = 0
pricelist_item = self.env['product.pricelist.item'].search([
('pricelist_id', '=', product_pricelist_tier2),
('product_id', '=', self.id)
], limit=1)
if pricelist_item:
# base_price = self._get_website_price_exclude_tax()
base_price_incl = self._get_website_price_include_tax()
discount = pricelist_item.price_discount
price = base_price_incl - (base_price_incl * discount / 100)
price = price / default_divide_tax
price = math.floor(price)
data = {
# 'base_price_tier2': base_price or 0,
'discount_tier2': discount or 0,
'price_tier2': price or 0
}
return data
def _get_pricelist_tier3(self):
product_pricelist_tier3 = int(self.env['ir.config_parameter'].get_param('product.pricelist.tier3'))
default_divide_tax = float(1.11)
base_price = discount = price = 0
pricelist_item = self.env['product.pricelist.item'].search([
('pricelist_id', '=', product_pricelist_tier3),
('product_id', '=', self.id)
], limit=1)
if pricelist_item:
# base_price = self._get_website_price_exclude_tax()
base_price_incl = self._get_website_price_include_tax()
discount = pricelist_item.price_discount
price = base_price_incl - (base_price_incl * discount / 100)
price = price / default_divide_tax
price = math.floor(price)
data = {
# 'base_price_tier3': base_price or 0,
'discount_tier3': discount or 0,
'price_tier3': price or 0
}
return data
def _get_flashsale_price(self):
# must get active pricelist
active_flash_sale = self.env['product.pricelist'].get_active_flash_sale()
flashsale_id = 0
flashsale_name = ''
# loop pricelist items
base_price = discount = price_flashsale = 0
for pricelist in active_flash_sale:
query = [
('pricelist_id', '=', pricelist.id),
('product_id', '=', self.id),
]
pricelist_items = self.env['product.pricelist.item'].search(query, limit=1)
for item in pricelist_items:
flashsale_id = pricelist.id
flashsale_name = pricelist.name
base_price = self._get_website_price_exclude_tax()
if item.price_discount > 0:
discount = item.price_discount
# base_item = self.env['product.pricelist.item'].search([('pricelist_id', '=', item.base_pricelist_id.id), ('product_id', '=', self.id)], limit=1)
price_flashsale = base_price - (base_price * discount // 100)
elif item.fixed_price > 0:
price_flashsale = item.fixed_price # ask darren for include or exclude
discount = (base_price - price_flashsale) // base_price * 100
data = {
'flashsale_id': flashsale_id,
'flashsale_name': flashsale_name,
'flashsale_base_price': base_price,
'flashsale_discount': discount,
'flashsale_price': price_flashsale
}
return data
|