summaryrefslogtreecommitdiff
path: root/indoteknik_api/controllers/api_v1
diff options
context:
space:
mode:
authorRafi Zadanly <zadanlyr@gmail.com>2023-07-18 13:59:18 +0700
committerRafi Zadanly <zadanlyr@gmail.com>2023-07-18 13:59:18 +0700
commit45fd501a53c6997bf74d5927d7c0eecf387caa51 (patch)
treefa20bdff3e95d943b0f4846b9746767f87f70f31 /indoteknik_api/controllers/api_v1
parent999725ea036840d74c7fdeebbd3aefac772bd8d3 (diff)
parentd418bd8dd84b91b9dc031819cfa9a2446e77acd2 (diff)
Merge branch 'origin/feature/promotion-program' into feature/voucher-cart
Diffstat (limited to 'indoteknik_api/controllers/api_v1')
-rw-r--r--indoteknik_api/controllers/api_v1/cart.py51
-rw-r--r--indoteknik_api/controllers/api_v1/product_variant.py12
-rw-r--r--indoteknik_api/controllers/api_v1/promotion.py70
-rw-r--r--indoteknik_api/controllers/api_v1/sale_order.py28
4 files changed, 136 insertions, 25 deletions
diff --git a/indoteknik_api/controllers/api_v1/cart.py b/indoteknik_api/controllers/api_v1/cart.py
index a8628432..035a40b7 100644
--- a/indoteknik_api/controllers/api_v1/cart.py
+++ b/indoteknik_api/controllers/api_v1/cart.py
@@ -5,55 +5,68 @@ from odoo.http import request
class Cart(controller.Controller):
prefix = '/api/v1/'
+ PREFIX_USER = prefix + 'user/<user_id>/'
- @http.route(prefix + 'cart', auth='public', methods=['GET'])
+ @http.route(PREFIX_USER + 'cart', auth='public', methods=['GET', 'OPTIONS'])
@controller.Controller.must_authorized()
- def get_cart_by_user_id(self, **kw):
- user_id = int(kw.get('user_id', 0))
+ def get_cart_by_user_id(self, user_id, **kw):
+ user_cart = request.env['website.user.cart']
+ user_id = int(user_id)
limit = int(kw.get('limit', 0))
offset = int(kw.get('offset', 0))
query = [('user_id', '=', user_id)]
- carts = request.env['website.user.cart'].search(query, limit=limit, offset=offset, order='create_date desc')
+ carts = user_cart.search(query, limit=limit, offset=offset, order='create_date desc')
data = {
- 'product_total': request.env['website.user.cart'].search_count(query),
- 'products': []
+ 'product_total': user_cart.search_count(query),
+ 'products': carts.get_products()
}
- for cart in carts:
- product = request.env['product.product'].api_single_response(cart.product_id)
- product['template_id'] = cart.product_id.product_tmpl_id.id
- product['quantity'] = cart.qty
- data['products'].append(product)
return self.response(data)
- @http.route(prefix + 'cart/create-or-update', auth='public', methods=['POST'], csrf=False)
+ @http.route(PREFIX_USER + 'cart/count', auth='public', methods=['GET', 'OPTIONS'])
@controller.Controller.must_authorized()
- def create_or_update_cart(self, **kw):
- user_id = int(kw.get('user_id', 0))
+ def get_cart_count_by_user_id(self, user_id, **kw):
+ user_id = int(user_id)
+ query = [('user_id', '=', user_id)]
+ carts = request.env['website.user.cart'].search_count(query)
+ return self.response(carts)
+
+ @http.route(PREFIX_USER + 'cart/create-or-update', auth='public', methods=['POST', 'OPTIONS'], csrf=False)
+ @controller.Controller.must_authorized()
+ def create_or_update_cart(self, user_id, **kw):
+ user_id = int(user_id)
product_id = int(kw.get('product_id', 0))
qty = int(kw.get('qty', 0))
+ is_selected = kw.get('selected', False)
+ program_line_id = int(kw.get('program_line_id', False))
+ if is_selected:
+ is_selected = True if is_selected == 'true' else False
if not user_id or not product_id or not qty:
return self.response(code=400, description='user_id, product_id and qty is required')
query = [('user_id', '=', user_id), ('product_id', '=', product_id)]
cart = request.env['website.user.cart'].search(query, limit=1)
result = {}
if cart:
- cart.write({'qty': qty})
+ data_to_update = {'qty': qty, 'is_selected': is_selected}
+ if program_line_id:
+ data_to_update['program_line_id'] = program_line_id
+ cart.write(data_to_update)
result['id'] = cart.id
else:
create = request.env['website.user.cart'].create({
'user_id': user_id,
'product_id': product_id,
- 'qty': qty
+ 'qty': qty,
+ 'is_selected': is_selected
})
result['id'] = create.id
return self.response(result)
- @http.route(prefix + 'cart', auth='public', methods=['DELETE'], csrf=False)
+ @http.route(PREFIX_USER + 'cart', auth='public', methods=['DELETE', 'OPTIONS'], csrf=False)
@controller.Controller.must_authorized()
- def delete_cart_by_user_id(self, **kw):
- user_id = int(kw.get('user_id', 0))
+ def delete_cart_by_user_id(self, user_id, **kw):
+ user_id = int(user_id)
query = [('user_id', '=', user_id)]
product_ids = kw.get('product_ids')
if product_ids:
diff --git a/indoteknik_api/controllers/api_v1/product_variant.py b/indoteknik_api/controllers/api_v1/product_variant.py
index 999ced6f..8de4669e 100644
--- a/indoteknik_api/controllers/api_v1/product_variant.py
+++ b/indoteknik_api/controllers/api_v1/product_variant.py
@@ -2,6 +2,7 @@ from .. import controller
from odoo import http
from odoo.http import request
+
class ProductVariant(controller.Controller):
prefix = '/api/v1/'
@@ -20,3 +21,14 @@ class ProductVariant(controller.Controller):
return self.response(data)
+ @http.route(prefix + 'product_variant/<product_id>/promotions', auth='public', methods=['GET', 'OPTIONS'])
+ @controller.Controller.must_authorized()
+ def get_product_variant_promotions(self, product_id):
+ product_id = int(product_id)
+ user_data = self.verify_user_token()
+
+ program_line = request.env['promotion.program.line']
+ program_lines = program_line.get_active_promotions(product_id)
+ program_lines = program_lines.res_format(user=user_data)
+
+ return self.response(program_lines)
diff --git a/indoteknik_api/controllers/api_v1/promotion.py b/indoteknik_api/controllers/api_v1/promotion.py
index b137fe2e..68a23ef2 100644
--- a/indoteknik_api/controllers/api_v1/promotion.py
+++ b/indoteknik_api/controllers/api_v1/promotion.py
@@ -1,12 +1,13 @@
from .. import controller
from odoo import http
from odoo.http import request
-import ast
+from datetime import datetime
class Promotion(controller.Controller):
prefix = '/api/v1/'
-
+
+
@http.route(prefix + 'promotion/<id>', auth='public', methods=['GET'])
@controller.Controller.must_authorized()
def get_promotion_by_id(self, **kw):
@@ -14,16 +15,75 @@ class Promotion(controller.Controller):
id = kw.get('id')
if not id:
return self.response(code=400, description='id is required')
-
+
data = {}
id = int(id)
- coupon_program = request.env['coupon.program'].search([('id', '=', id)])
+ coupon_program = request.env['coupon.program'].search(
+ [('id', '=', id)])
if coupon_program:
data = {
'banner': base_url + 'api/image/coupon.program/x_studio_banner_promo/' + str(coupon_program.id) if coupon_program.x_studio_banner_promo else '',
'image': base_url + 'api/image/coupon.program/x_studio_image_promo/' + str(coupon_program.id) if coupon_program.x_studio_image_promo else '',
'name': coupon_program.name,
}
+
+ return self.response(data)
+
+
+ @http.route(prefix + 'promotion/home', auth='public', methods=['GET', 'OPTIONS'])
+ @controller.Controller.must_authorized()
+ def v1_get_promotion_home(self):
+ current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
+ programs = request.env['promotion.program'].search([
+ ('start_time', '<=', current_time),
+ ('end_time', '>=', current_time),
+ ])
+ if not programs:
+ return self.response(None)
+
+ data = []
+ for program in programs:
+ data_program = {
+ 'id': program.id,
+ 'name': program.name,
+ 'banner': request.env['ir.attachment'].api_image('promotion.program', 'banner', program.id),
+ 'icon': request.env['ir.attachment'].api_image('promotion.program', 'icon', program.id)
+ }
+ data.append(data_program)
+
+ return self.response(data)
+
+
+ @http.route(prefix + 'promotion/home/<id>', auth='public', methods=['GET', 'OPTIONS'])
+ @controller.Controller.must_authorized()
+ def v1_get_promotion_home_detail(self, id):
+ program_lines = request.env['promotion.program.line'].search([
+ ('display_on_homepage', '=', True),
+ ('promotion_type', '=', 'special_price'),
+ ('program_id', '=', int(id))
+ ])
+ data = []
+ for line in program_lines:
+ product = request.env['product.product'].v2_api_single_response(line.product_id)
+ product_template = line.product_id.product_tmpl_id
+
+ product.update({
+ 'id': product['parent']['id'],
+ 'image': product['parent']['image'],
+ 'name': product['parent']['name'],
+ 'variant_total': len(product_template.product_variant_ids),
+ 'lowest_price': line.calculate_price(product['price']),
+ 'stock_total': product['stock'],
+ 'icon': {
+ 'top': request.env['ir.attachment'].api_image('promotion.program', 'icon_top', line.program_id.id),
+ 'bottom': request.env['ir.attachment'].api_image('promotion.program', 'icon_bottom', line.program_id.id)
+ }
+ })
+
+ product.pop('parent', None)
+ product.pop('price', None)
+ product.pop('stock', None)
+ data.append(product)
return self.response(data)
- \ No newline at end of file
+
diff --git a/indoteknik_api/controllers/api_v1/sale_order.py b/indoteknik_api/controllers/api_v1/sale_order.py
index 713b3bff..7c38d47d 100644
--- a/indoteknik_api/controllers/api_v1/sale_order.py
+++ b/indoteknik_api/controllers/api_v1/sale_order.py
@@ -231,7 +231,33 @@ class SaleOrder(controller.Controller):
sale_order.state = 'cancel'
data = sale_order.id
return self.response(data)
-
+
+ @http.route(prefix + 'user/<user_id>/sale_order/checkout', auth='public', method=['GET', 'OPTIONS'], csrf=False)
+ @controller.Controller.must_authorized(private=True, private_key='user_id')
+ def get_user_checkout_so(self, user_id):
+ cart = request.env['website.user.cart']
+ products = cart.get_product_by_user(user_id=user_id, selected=True)
+ total_purchase = sum(x['price']['price'] * x['quantity'] for x in products)
+ total_discount = sum((x['price']['price'] - x['price']['price_discount']) * x['quantity'] for x in products)
+ subtotal = total_purchase - total_discount
+ tax = round(subtotal * 0.11)
+ grand_total = subtotal + tax
+ total_weight = sum(x['weight'] * x['quantity'] for x in products)
+ result = {
+ 'total_purchase': total_purchase,
+ 'total_discount': total_discount,
+ 'subtotal': subtotal,
+ 'tax': tax,
+ 'grand_total': round(grand_total),
+ 'total_weight': {
+ 'kg': total_weight,
+ 'g': total_weight * 1000
+ },
+ 'has_product_without_weight': any(not product.get('weight') or product.get('weight') == 0 for product in products),
+ 'products': products
+ }
+ return self.response(result)
+
@http.route(PREFIX_PARTNER + 'sale_order/checkout', auth='public', method=['POST', 'OPTIONS'], csrf=False)
@controller.Controller.must_authorized(private=True, private_key='partner_id')
def create_partner_sale_order(self, **kw):