diff options
Diffstat (limited to 'indoteknik_api/controllers/api_v1')
| -rw-r--r-- | indoteknik_api/controllers/api_v1/cart.py | 21 | ||||
| -rw-r--r-- | indoteknik_api/controllers/api_v1/product_variant.py | 4 | ||||
| -rw-r--r-- | indoteknik_api/controllers/api_v1/sale_order.py | 51 |
3 files changed, 41 insertions, 35 deletions
diff --git a/indoteknik_api/controllers/api_v1/cart.py b/indoteknik_api/controllers/api_v1/cart.py index 6faac27f..2243ec0f 100644 --- a/indoteknik_api/controllers/api_v1/cart.py +++ b/indoteknik_api/controllers/api_v1/cart.py @@ -31,7 +31,7 @@ class Cart(controller.Controller): return self.response(carts) @http.route(PREFIX_USER + 'cart/create-or-update', auth='public', methods=['POST', 'OPTIONS'], csrf=False) - @controller.Controller.must_authorized() + @controller.Controller.must_authorized(private=True, private_key='user_id') def create_or_update_cart(self, user_id, **kw): # Convert input values to appropriate types user_id = int(user_id) @@ -54,19 +54,29 @@ class Cart(controller.Controller): user_query = ('user_id', '=', user_id) website_user_cart.search([user_query, ('source', '=', 'buy')]).unlink() - # Prepare query to find existing cart entry for the product - query = [user_query, ('product_id', '=', product_id), ('source', '=', 'add_to_cart')] + # Prepare query to find existing cart entry + query = [user_query, ('source', '=', 'add_to_cart')] + if product_id: + query.append(('product_id', '=', product_id)) + elif program_line_id: + query.append(('program_line_id', '=', program_line_id)) + cart = website_user_cart.search(query, limit=1) - result = {} + data_to_update = { 'qty': qty, 'is_selected': is_selected, - 'program_line_id': program_line_id + 'program_line_id': program_line_id, + 'product_id': product_id } + if program_line_id: + data_to_update['product_id'] = False + if source: data_to_update['source'] = source + result = {} if cart and source in (None, 'add_to_cart'): # Update existing cart entry cart.write(data_to_update) @@ -75,7 +85,6 @@ class Cart(controller.Controller): # Create a new cart entry if it doesn't exist create = website_user_cart.create({ 'user_id': user_id, - 'product_id': product_id, **data_to_update }) result['id'] = create.id diff --git a/indoteknik_api/controllers/api_v1/product_variant.py b/indoteknik_api/controllers/api_v1/product_variant.py index 8de4669e..fce2edf0 100644 --- a/indoteknik_api/controllers/api_v1/product_variant.py +++ b/indoteknik_api/controllers/api_v1/product_variant.py @@ -21,7 +21,7 @@ class ProductVariant(controller.Controller): return self.response(data) - @http.route(prefix + 'product_variant/<product_id>/promotions', auth='public', methods=['GET', 'OPTIONS']) + @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) @@ -29,6 +29,6 @@ class ProductVariant(controller.Controller): 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) + program_lines = program_lines.formats(user=user_data) return self.response(program_lines) diff --git a/indoteknik_api/controllers/api_v1/sale_order.py b/indoteknik_api/controllers/api_v1/sale_order.py index 2c6958d8..ef4c2688 100644 --- a/indoteknik_api/controllers/api_v1/sale_order.py +++ b/indoteknik_api/controllers/api_v1/sale_order.py @@ -295,47 +295,44 @@ class SaleOrder(controller.Controller): if params['value']['type'] == 'sale_order': parameters['approval_status'] = 'pengajuan1' sale_order = request.env['sale.order'].create([parameters]) - parameters = [] user_id = params['value']['user_id'] user_cart = request.env['website.user.cart'] source = params['value']['source'] - products = user_cart.get_product_by_user(user_id=user_id, selected=True, source=source) - for product in products: - price_unit = product['price']['price'] - price_discount =product['price']['discount_percentage'] - order_line = { - 'company_id': 1, - 'order_id': sale_order.id, - 'price_unit': price_unit, - 'discount': price_discount, - 'product_id': product['id'], - 'product_uom_qty': product['quantity'] - } - - if product['program']: - order_line.update({ - 'program_line_id': product['program']['id'] + carts = user_cart.get_product_by_user(user_id=user_id, selected=True, source=source) + + order_lines = [] + promotions = [] + for cart in carts: + if cart['cart_type'] == 'product': + order_lines.append({ + 'company_id': 1, + 'order_id': sale_order.id, + 'price_unit': cart['price']['price'], + 'discount': cart['price']['discount_percentage'], + 'product_id': cart['id'], + 'product_uom_qty': cart['quantity'] + }) + elif cart['cart_type'] == 'promotion': + promotions.append({ + 'order_id': sale_order.id, + 'program_line_id': cart['id'], + 'quantity': cart['quantity'] }) - parameters.append(order_line) - # if product['program'] and product['program']['type']['value'] != 'special_price': - # total_qty += sum(x['quantity'] for x in product['program']['items']) - # price_unit = product['subtotal'] / total_qty - # price_discount = 0 - - request.env['sale.order.line'].create(parameters) + request.env['sale.order.line'].create(order_lines) + request.env['sale.order.promotion'].create(promotions) - if any(x['program'] for x in products): + if len(promotions) > 0: sale_order.apply_promotion_program() voucher_code = params['value']['voucher'] voucher = request.env['voucher'].search([('code', '=', voucher_code)]) - if voucher: + if voucher and len(promotions) == 0: sale_order.voucher_id = voucher.id sale_order.apply_voucher() - cart_ids = [x['cart_id'] for x in products] + cart_ids = [x['cart_id'] for x in carts] # user_cart.browse(cart_ids).unlink() return self.response({ 'id': sale_order.id, |
