From bc6bc85f455c4b8bc9f73b779b521faa5fcdcf96 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Thu, 15 Jun 2023 16:48:29 +0700 Subject: Update user cart model and API --- indoteknik_api/controllers/api_v1/cart.py | 36 +++++++++++++------------ indoteknik_api/controllers/api_v1/sale_order.py | 20 ++++++++++++++ indoteknik_custom/__manifest__.py | 3 --- indoteknik_custom/models/website_user_cart.py | 25 +++++++++++++++-- indoteknik_custom/security/ir.model.access.csv | 3 --- indoteknik_custom/views/website_user_cart.xml | 4 +++ 6 files changed, 66 insertions(+), 25 deletions(-) diff --git a/indoteknik_api/controllers/api_v1/cart.py b/indoteknik_api/controllers/api_v1/cart.py index a8628432..0d54ecbc 100644 --- a/indoteknik_api/controllers/api_v1/cart.py +++ b/indoteknik_api/controllers/api_v1/cart.py @@ -5,32 +5,30 @@ from odoo.http import request class Cart(controller.Controller): prefix = '/api/v1/' + PREFIX_USER = prefix + 'user//' - @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_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') data = { 'product_total': request.env['website.user.cart'].search_count(query), - 'products': [] + 'products': [cart.get_product() for cart in carts] } - 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/create-or-update', auth='public', methods=['POST', 'OPTIONS'], csrf=False) @controller.Controller.must_authorized() - def create_or_update_cart(self, **kw): - user_id = int(kw.get('user_id', 0)) + 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 = int(kw.get('is_selected', False)) + program_line_id = int(kw.get('program_line_id', 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') @@ -38,22 +36,26 @@ class Cart(controller.Controller): 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/sale_order.py b/indoteknik_api/controllers/api_v1/sale_order.py index 35361ba4..9df710ec 100644 --- a/indoteknik_api/controllers/api_v1/sale_order.py +++ b/indoteknik_api/controllers/api_v1/sale_order.py @@ -218,6 +218,26 @@ class SaleOrder(controller.Controller): data = sale_order.id return self.response(data) + @http.route(prefix + 'user//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'] for x in products) + total_discount = sum((x['price']['price'] - x['price']['price_discount']) for x in products) + subtotal = total_purchase - total_discount + tax = round(subtotal * 0.11) + grand_total = subtotal + tax + result = { + 'total_purchase': total_purchase, + 'total_discount': total_discount, + 'subtotal': subtotal, + 'tax': tax, + 'grand_total': round(grand_total), + '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): diff --git a/indoteknik_custom/__manifest__.py b/indoteknik_custom/__manifest__.py index f21fab0c..7b029a05 100755 --- a/indoteknik_custom/__manifest__.py +++ b/indoteknik_custom/__manifest__.py @@ -74,15 +74,12 @@ 'views/procurement_monitoring_detail.xml', 'views/product_product.xml', 'views/brand_vendor.xml', -<<<<<<< HEAD 'views/promotion_program.xml', 'views/promotion_program_line.xml', 'views/promotion_program_free_item.xml', 'views/promotion_program_keyword.xml', -======= 'views/requisition.xml', 'views/landedcost.xml', ->>>>>>> 24649f8e939484759ef34e5e68f251d951f63c02 'report/report.xml', 'report/report_banner_banner.xml', 'report/report_banner_banner2.xml', diff --git a/indoteknik_custom/models/website_user_cart.py b/indoteknik_custom/models/website_user_cart.py index 8046469f..dcd9fa5a 100644 --- a/indoteknik_custom/models/website_user_cart.py +++ b/indoteknik_custom/models/website_user_cart.py @@ -5,6 +5,27 @@ class WebsiteUserCart(models.Model): _name = 'website.user.cart' _rec_name = 'user_id' - user_id = fields.Many2one('res.users', string='User', help="User ID yang terdaftar di table res.users") - product_id = fields.Many2one('product.product', string='Product', help="Product yang terdaftar di table product.product") + user_id = fields.Many2one('res.users', string='User') + product_id = fields.Many2one('product.product', string='Product') + program_line_id = fields.Many2one('promotion.program.line', string='Program', help="Apply program") qty = fields.Float(string='Quantity', digits='Product Unit of Measure') + is_selected = fields.Boolean(string='Selected?', digits='Is selected to process checkout') + + def get_product(self): + product_product = self.env['product.product'] + product = product_product.v2_api_single_response(self.product_id) + product['quantity'] = self.qty + product['subtotal'] = self.qty * product['price']['price_discount'] + product['selected'] = self.is_selected + return product + + def get_product_by_user(self, user_id, selected = False): + user_id = int(user_id) + parameters = [('user_id', '=', user_id)] + if selected: + parameters.append(('is_selected', '=', True)) + carts = self.search(parameters) + products = [] + for cart in carts: + products.append(cart.get_product()) + return products \ No newline at end of file diff --git a/indoteknik_custom/security/ir.model.access.csv b/indoteknik_custom/security/ir.model.access.csv index 3d23529f..c00f5b76 100755 --- a/indoteknik_custom/security/ir.model.access.csv +++ b/indoteknik_custom/security/ir.model.access.csv @@ -49,13 +49,10 @@ access_group_partner,access.group.partner,model_group_partner,,1,1,1,1 access_procurement_monitoring_detail,access.procurement.monitoring.detail,model_procurement_monitoring_detail,,1,1,1,1 access_rajaongkir_kurir,access.rajaongkir.kurir,model_rajaongkir_kurir,,1,1,1,1 access_brand_vendor,access.brand.vendor,model_brand_vendor,,1,1,1,1 -<<<<<<< HEAD access_promotion_program,access.promotion.program,model_promotion_program,,1,1,1,1 access_promotion_program_line,access.promotion.program.line,model_promotion_program_line,,1,1,1,1 access_promotion_program_free_item,access.promotion.program.free_item,model_promotion_program_free_item,,1,1,1,1 access_promotion_program_keyword,access.promotion.program.keyword,model_promotion_program_keyword,,1,1,1,1 -======= access_requisition,access.requisition,model_requisition,,1,1,1,1 access_requisition_line,access.requisition.line,model_requisition_line,,1,1,1,1 access_requisition_purchase_match,access.requisition.purchase.match,model_requisition_purchase_match,,1,1,1,1 ->>>>>>> 24649f8e939484759ef34e5e68f251d951f63c02 diff --git a/indoteknik_custom/views/website_user_cart.xml b/indoteknik_custom/views/website_user_cart.xml index 890d801c..fbd08acb 100755 --- a/indoteknik_custom/views/website_user_cart.xml +++ b/indoteknik_custom/views/website_user_cart.xml @@ -13,7 +13,9 @@ + + @@ -28,7 +30,9 @@ + + -- cgit v1.2.3