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
|
from datetime import datetime
from . import controller
from odoo import http
from odoo.http import request
class ProductController(controller.Controller):
prefix_url = '/api/product'
# TODO: separate function for get manufacture and promotion by product_id
@http.route(prefix_url + '/search', auth='public', methods=['GET'])
def search_product(self, **kw):
self.authenticate(kw)
base_url = request.env['ir.config_parameter'].sudo().get_param('web.base.url')
query = kw.get('query')
if not query:
return self.response(code=400, description='Field query is required')
query = '%' + query.replace(' ', '%') + '%'
domain = [
('sale_ok', '=', True),
'|',
('default_code', 'ilike', query),
('name', 'ilike', query)
]
manufactures = kw.get('manufactures')
if manufactures:
manufactures = [int(x) for x in manufactures.split(',')]
domain.append(('x_manufacture', 'in', manufactures))
categories = kw.get('categories')
if categories:
categories = [int(x) for x in categories.split(',')]
domain.append(('public_categ_ids', 'child_of', categories))
product_variants = request.env['product.product'].search(domain)
product_variant_ids = [x.id for x in product_variants]
domain = [('product_variant_ids', 'in', product_variant_ids)]
product_templates = self.search_with_api_params('product.template', kw, domain)
data = {
'total_records': len(request.env['product.template'].search(domain)),
'products': []
}
for product_template in product_templates:
discount_price = 0
price = product_template.web_price
if price > 0:
if product_template.taxes_id:
if not product_template.taxes_id.price_include:
price += (price * product_template.taxes_id.amount / 100)
else:
price += (price * 11 / 100)
promotion = self.get_promotion_by_product(product_template)
if len(promotion) > 0:
discount_price = price - (price * promotion['discount_percentage'] / 100)
manufacture = self.get_manufacture_by_product(product_template)
stock = self.get_stock_by_product(product_template)
# TODO: remove price and discount_price if old indoteknik not use
data['products'].append({
'id': product_template.id,
'image': base_url + 'api/image/product.template/image_128/' + str(product_template.id) if product_template.image_128 else '',
'name': product_template.name,
# 'lowest_price': request.env['product.pricelist'].get_lowest_product_variant_price(product_template.id, 4),
'price': price,
'discount_price': discount_price,
'total_variant': len(product_template.product_variant_ids),
'stock': stock,
'manufacture': manufacture,
'promotion': promotion,
})
return self.response(data)
@http.route(prefix_url + '/flash_sale', auth='public', methods=['GET'])
def get_flash_sale_product(self, **kw):
self.authenticate(kw)
base_url = request.env['ir.config_parameter'].sudo().get_param('web.base.url')
product_pricelist_default = request.env['ir.config_parameter'].sudo().get_param('product.pricelist.default')
active_flash_sale = request.env['product.pricelist'].get_active_flash_sale()
data = {}
if active_flash_sale:
flash_sale = {
'banner': base_url + 'api/image/product.pricelist/banner/' + str(active_flash_sale.id) if active_flash_sale.banner else '',
'duration': round((active_flash_sale.end_date - datetime.now()).total_seconds()),
'products': []
}
product_pricelist_item = request.env['product.pricelist.item'].search([('pricelist_id', '=', active_flash_sale.id)])
product_variant_ids = [x.product_id.id for x in product_pricelist_item]
product_templates = self.search_with_api_params('product.template', kw, [('product_variant_ids', 'in', product_variant_ids)])
for product in product_templates:
flash_sale['products'].append({
'id': product.id,
'name': product.name,
'image': base_url + 'api/image/product.template/image_128/' + str(product.id) if product.image_128 else '',
'lowest_price': request.env['product.pricelist'].get_lowest_product_variant_price(product.id, int(product_pricelist_default)),
'stock': self.get_stock_by_product(product),
'total_variant': len(product.product_variant_ids),
'manufacture': self.get_manufacture_by_product(product),
'promotion': self.get_promotion_by_product(product),
})
data.update({'flash_sale': flash_sale})
else:
return self.response(code=404, description='Data not found')
return self.response(data)
def get_stock_by_product(self, product_template: object):
stock = 0
for product_variant in product_template.product_variant_ids:
stock += product_variant.qty_stock_vendor
return stock
def get_manufacture_by_product(self, product_template: object):
manufacture = {}
if product_template.x_manufacture:
manufacture.update({
'id': product_template.x_manufacture.id,
'name': product_template.x_manufacture.x_name,
})
return manufacture
def get_promotion_by_product(self, product_template: object):
base_url = request.env['ir.config_parameter'].sudo().get_param('web.base.url')
promotion = {}
if product_template.x_manufacture:
domain = [
('rule_products_domain', 'ilike', product_template.x_manufacture.x_name),
('active', '=', True)
]
coupon_program = request.env['coupon.program'].search(domain, limit=1)
if coupon_program:
icon_1 = (base_url + 'api/image/coupon.program/x_studio_field_Ifopn/' + str(coupon_program.id)) if coupon_program.x_studio_field_Ifopn else ''
icon_2 = (base_url + 'api/image/coupon.program/x_studio_field_2Ul77/' + str(coupon_program.id)) if coupon_program.x_studio_field_2Ul77 else ''
promotion.update({
'name': coupon_program.name,
'discount_percentage': coupon_program.discount_percentage,
'icon_1': icon_1,
'icon_2': icon_2
})
return promotion
|