import base64 from .. import api_controller from odoo import http from odoo.http import request class ProductApi(api_controller.ApiController): @http.route('/api/product/search', auth='public', methods=['GET']) def search_product(self, **kw): self.authenticate(kw) 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] 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) if product_template.image_128 else '', '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 self.response(data)