summaryrefslogtreecommitdiff
path: root/indoteknik_custom/controllers/api/product.py
diff options
context:
space:
mode:
authorIT Fixcomart <it@fixcomart.co.id>2022-09-27 14:41:16 +0700
committerIT Fixcomart <it@fixcomart.co.id>2022-09-27 14:41:16 +0700
commit469dd515bacbd4f76265860a9d7c8c745cb5bcf0 (patch)
treee0e086990d2d2afeb87356af2ff81a518395f716 /indoteknik_custom/controllers/api/product.py
parentd057901e1b30dc24978ec345e4daec1d16abf117 (diff)
fix rest api response, image api, search product api
Diffstat (limited to 'indoteknik_custom/controllers/api/product.py')
-rw-r--r--indoteknik_custom/controllers/api/product.py96
1 files changed, 82 insertions, 14 deletions
diff --git a/indoteknik_custom/controllers/api/product.py b/indoteknik_custom/controllers/api/product.py
index 5c5a6c1a..0cd02e90 100644
--- a/indoteknik_custom/controllers/api/product.py
+++ b/indoteknik_custom/controllers/api/product.py
@@ -1,27 +1,95 @@
+import base64
+
from .. import api_controller
from odoo import http
from odoo.http import request
-import json
class ProductApi(api_controller.ApiController):
@http.route('/api/product/search', auth='public', methods=['GET'])
def search_product(self, **kw):
self.authenticate(kw)
- limit = kw.get('limit', 0)
- offset = kw.get('offset', 0)
- order = kw.get('order', '')
- domain = kw.get('domain', [])
- if domain:
- domain = json.loads(domain)
+ 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 = [v['id'] for v in product_variants]
- domain = [('product_variant_ids', 'in', product_variant_ids)]
- products = request.env['product.template'].search(domain, limit=int(limit), offset=int(offset), order=order)
- response = []
- for product in products:
- response.append({
- 'name': product.name
+
+ base_url = request.env['ir.config_parameter'].sudo().get_param('web.base.url')
+ 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:
+ stock = 0
+ for product_variant in product_template.product_variant_ids:
+ stock += product_variant.qty_stock_vendor
+
+ 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 = {}
+ manufacture = {}
+ if product_template.x_manufacture:
+ manufacture.update({
+ 'id': product_template.x_manufacture.id,
+ 'name': product_template.x_manufacture.x_name,
+ })
+ 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:
+ discount_price = price - (price * coupon_program.discount_percentage / 100)
+ 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
+ })
+
+ data['products'].append({
+ 'id': product_template.id,
+ 'image': base_url + 'api/image/product.template/image_128/' + str(product_template.id),
+ 'name': product_template.name,
+ 'price': price,
+ 'discount_price': discount_price,
+ 'total_variant': len(product_template.product_variant_ids),
+ 'stock': stock,
+ 'manufacture': manufacture,
+ 'promotion': promotion,
})
- return json.dumps(response)
+
+ return self.response(data)