summaryrefslogtreecommitdiff
path: root/indoteknik_api/controllers/product_controller.py
diff options
context:
space:
mode:
authorIT Fixcomart <it@fixcomart.co.id>2022-10-05 15:19:27 +0700
committerIT Fixcomart <it@fixcomart.co.id>2022-10-05 15:19:27 +0700
commit14cb30c3f2fb8b15baaf32ab9fca7bb9bda73845 (patch)
treee4039e13e934db6ee148f766882bb82af8db1d7a /indoteknik_api/controllers/product_controller.py
parentd1bc570eae2818bc4b535840f2eb3061b99ca98b (diff)
Update struktur API and fitur API flash sale
Diffstat (limited to 'indoteknik_api/controllers/product_controller.py')
-rw-r--r--indoteknik_api/controllers/product_controller.py142
1 files changed, 142 insertions, 0 deletions
diff --git a/indoteknik_api/controllers/product_controller.py b/indoteknik_api/controllers/product_controller.py
new file mode 100644
index 00000000..a127b790
--- /dev/null
+++ b/indoteknik_api/controllers/product_controller.py
@@ -0,0 +1,142 @@
+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': (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})
+ 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