summaryrefslogtreecommitdiff
path: root/indoteknik_api
diff options
context:
space:
mode:
authorIT Fixcomart <it@fixcomart.co.id>2022-10-13 16:43:59 +0700
committerIT Fixcomart <it@fixcomart.co.id>2022-10-13 16:43:59 +0700
commit3a5b05bee24feaadc8d6b5fe078fd6cf56989ba7 (patch)
tree435617837217f4869c1eb5acb50b625da4331b31 /indoteknik_api
parent47be053bafe8e212e7c98332666819e65428c4e3 (diff)
parent6029c9e3a0b42a6faef373e6dd2f5bf57cef17a0 (diff)
Fix price and update product response API
Diffstat (limited to 'indoteknik_api')
-rw-r--r--indoteknik_api/controllers/__init__.py1
-rw-r--r--indoteknik_api/controllers/api_v1/category.py2
-rw-r--r--indoteknik_api/controllers/api_v1/manufacture.py2
-rw-r--r--indoteknik_api/controllers/api_v1/product.py17
-rw-r--r--indoteknik_api/controllers/product_controller.py151
-rw-r--r--indoteknik_api/models/product_pricelist.py3
-rw-r--r--indoteknik_api/models/product_template.py6
7 files changed, 17 insertions, 165 deletions
diff --git a/indoteknik_api/controllers/__init__.py b/indoteknik_api/controllers/__init__.py
index f56b2612..30664b92 100644
--- a/indoteknik_api/controllers/__init__.py
+++ b/indoteknik_api/controllers/__init__.py
@@ -1,3 +1,2 @@
from . import controller
-from . import product_controller
from . import api_v1 \ No newline at end of file
diff --git a/indoteknik_api/controllers/api_v1/category.py b/indoteknik_api/controllers/api_v1/category.py
index 4b5f558e..585e3eda 100644
--- a/indoteknik_api/controllers/api_v1/category.py
+++ b/indoteknik_api/controllers/api_v1/category.py
@@ -24,7 +24,7 @@ class Category(controller.Controller):
product_variants = request.env['product.product'].search([('x_manufacture', '=', int(manufacture_id))])
category_ids = [x.id for variant in product_variants for x in variant.public_categ_ids]
else:
- return self.response(code=400, description='data not found')
+ return self.response(code=400, description='page possible value is flash-sale, manufacture')
categories = request.env['product.public.category'].search([('id', 'in', category_ids)])
data = []
diff --git a/indoteknik_api/controllers/api_v1/manufacture.py b/indoteknik_api/controllers/api_v1/manufacture.py
index 3cb6ed0f..ba2e3be9 100644
--- a/indoteknik_api/controllers/api_v1/manufacture.py
+++ b/indoteknik_api/controllers/api_v1/manufacture.py
@@ -24,7 +24,7 @@ class Manufacture(controller.Controller):
product_variants = request.env['product.product'].search([('public_categ_ids', '=', int(category_id))])
manufacture_ids = [x.x_manufacture.id for x in product_variants]
else:
- return self.response(code=400, description='data not found')
+ return self.response(code=400, description='page possible value is flash-sale, category')
manufactures = request.env['x_manufactures'].search([('id', 'in', manufacture_ids)])
data = []
diff --git a/indoteknik_api/controllers/api_v1/product.py b/indoteknik_api/controllers/api_v1/product.py
index 068a54b6..493677fd 100644
--- a/indoteknik_api/controllers/api_v1/product.py
+++ b/indoteknik_api/controllers/api_v1/product.py
@@ -48,15 +48,16 @@ class Product(controller.Controller):
limit = int(kw.get('limit', 0))
offset = int(kw.get('offset', 0))
order = kw.get('order')
- if order == 'price-desc':
- order = 'web_price_sorting desc'
- elif order == 'price-asc':
- order = 'web_price_sorting asc'
+
+ orders = ['product_rating desc']
+ if order != 'price-asc':
+ orders.append('web_price_sorting desc')
+ if order == 'price-asc':
+ orders.append('web_price_sorting asc')
elif order == 'latest':
- order = 'create_date desc'
- else:
- order = 'product_rating desc'
- product_templates = request.env['product.template'].search(query, limit=limit, offset=offset, order=order)
+ orders.append('create_date desc')
+ orders = ','.join(orders)
+ product_templates = request.env['product.template'].search(query, limit=limit, offset=offset, order=orders)
data = {
'product_total': request.env['product.template'].search_count(query),
'products': [request.env['product.template'].api_single_response(x) for x in product_templates]
diff --git a/indoteknik_api/controllers/product_controller.py b/indoteknik_api/controllers/product_controller.py
deleted file mode 100644
index abb48f7d..00000000
--- a/indoteknik_api/controllers/product_controller.py
+++ /dev/null
@@ -1,151 +0,0 @@
-from datetime import datetime
-import logging
-from . import controller
-from odoo import http
-from odoo.http import request
-
-_logger = logging.getLogger(__name__)
-
-
-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()
- 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_filter('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.web_tax_id:
- if not product_template.web_tax_id.price_include:
- price += (price * product_template.web_tax_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):
- try:
- 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_filter('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')
- except Exception as e:
- _logger.info(self.prefix_url + '/flash_sale: ' + str(e))
- return self.response(code=500, description='Internal server error')
- 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 \ No newline at end of file
diff --git a/indoteknik_api/models/product_pricelist.py b/indoteknik_api/models/product_pricelist.py
index a82fea1d..4e7f6adf 100644
--- a/indoteknik_api/models/product_pricelist.py
+++ b/indoteknik_api/models/product_pricelist.py
@@ -41,7 +41,8 @@ class ProductPricelist(models.Model):
if price > 0:
product = self.env['product.product'].browse(product_id)
if product:
- if product.web_tax_id and product.web_tax_id.price_include:
+ product = product.product_tmpl_id
+ if product.web_tax_id and not product.web_tax_id.price_include:
price *= (100 + product.web_tax_id.amount) / 100
price_discount = price
diff --git a/indoteknik_api/models/product_template.py b/indoteknik_api/models/product_template.py
index 0cbf605f..aa35d922 100644
--- a/indoteknik_api/models/product_template.py
+++ b/indoteknik_api/models/product_template.py
@@ -16,6 +16,7 @@ class ProductTemplate(models.Model):
'lowest_price': self.env['product.pricelist'].get_lowest_product_variant_price(product_template, product_pricelist_default),
'variant_total': len(product_template.product_variant_ids),
'stock_total': product_template.qty_stock_vendor,
+ 'weight': product_template.weight,
'manufacture': self.api_manufacture(product_template),
'categories': self.api_categories(product_template),
}
@@ -29,9 +30,10 @@ class ProductTemplate(models.Model):
detail_data['variants'].append({
'id': variant.id,
'code': variant.default_code or '',
- 'name': variant.name,
+ 'name': variant.display_name,
'price': self.env['product.pricelist'].compute_price(product_pricelist_default, variant.id),
- 'stock': variant.qty_stock_vendor
+ 'stock': variant.qty_stock_vendor,
+ 'weight': variant.weight,
})
data.update(detail_data)
return data