From 6f898864f5e825a560cc92f2e1f1973defafba51 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Thu, 20 Jul 2023 09:09:31 +0700 Subject: Update voucher api can apply to public voucher --- indoteknik_api/controllers/api_v1/voucher.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'indoteknik_api/controllers/api_v1/voucher.py') diff --git a/indoteknik_api/controllers/api_v1/voucher.py b/indoteknik_api/controllers/api_v1/voucher.py index 0990a1a0..f948183e 100644 --- a/indoteknik_api/controllers/api_v1/voucher.py +++ b/indoteknik_api/controllers/api_v1/voucher.py @@ -2,6 +2,7 @@ from .. import controller from odoo import http from odoo.http import request + class Voucher(controller.Controller): prefix = '/api/v1/' @@ -9,14 +10,14 @@ class Voucher(controller.Controller): @controller.Controller.must_authorized() def get_vouchers(self, **kw): code = kw.get('code') - visibility = 'public' + visibility = ['public'] parameter = [] if code: - visibility = 'private' + visibility.append('private') parameter += [('code', '=', code)] - parameter += [('visibility', '=', visibility)] + parameter += [('visibility', 'in', visibility)] vouchers = request.env['voucher'].get_active_voucher(parameter) data = vouchers.res_format() return self.response(data) -- cgit v1.2.3 From ed231d1cfa4e78b8f98c5406f88c1f985c0225a7 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Thu, 20 Jul 2023 10:43:13 +0700 Subject: Add response on vouccher api - can apply - discount voucher - difference to apply --- indoteknik_api/controllers/api_v1/voucher.py | 36 ++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) (limited to 'indoteknik_api/controllers/api_v1/voucher.py') diff --git a/indoteknik_api/controllers/api_v1/voucher.py b/indoteknik_api/controllers/api_v1/voucher.py index f948183e..5cdafba4 100644 --- a/indoteknik_api/controllers/api_v1/voucher.py +++ b/indoteknik_api/controllers/api_v1/voucher.py @@ -6,10 +6,12 @@ from odoo.http import request class Voucher(controller.Controller): prefix = '/api/v1/' - @http.route(prefix + 'voucher', auth='public', methods=['GET', 'OPTIONS']) - @controller.Controller.must_authorized() + @http.route(prefix + 'user//voucher', auth='public', methods=['GET', 'OPTIONS']) + @controller.Controller.must_authorized(private=True, private_key='user_id') def get_vouchers(self, **kw): + cart = request.env['website.user.cart'] code = kw.get('code') + user_id = kw.get('user_id') visibility = ['public'] parameter = [] @@ -19,5 +21,31 @@ class Voucher(controller.Controller): parameter += [('visibility', 'in', visibility)] vouchers = request.env['voucher'].get_active_voucher(parameter) - data = vouchers.res_format() - return self.response(data) + vouchers = vouchers.res_format() + checkout = cart.get_user_checkout(user_id) + + for voucher in vouchers: + subtotal = checkout['subtotal'] + min_purchase_amount = voucher['min_purchase_amount'] + max_discount_amount = voucher['max_discount_amount'] + discount_type = voucher['discount_type'] + discount_amount = voucher['discount_amount'] + can_apply = subtotal >= min_purchase_amount + difference_to_apply = 0 + if not can_apply: + difference_to_apply = min_purchase_amount - subtotal + + discount_voucher = 0 + if discount_type == 'fixed_price': + discount_voucher = discount_amount + if discount_type == 'percentage': + discount_voucher = subtotal * discount_amount / 100 + + if max_discount_amount > 0 and discount_voucher > max_discount_amount: + discount_voucher = max_discount_amount + + voucher['can_apply'] = can_apply + voucher['discount_voucher'] = discount_voucher + voucher['difference_to_apply'] = difference_to_apply + + return self.response(vouchers) -- cgit v1.2.3 From da2389775171d288b96958651a45f07865a2e014 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Fri, 21 Jul 2023 11:06:58 +0700 Subject: Update voucher use manufacture rules --- indoteknik_api/controllers/api_v1/voucher.py | 29 +++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) (limited to 'indoteknik_api/controllers/api_v1/voucher.py') diff --git a/indoteknik_api/controllers/api_v1/voucher.py b/indoteknik_api/controllers/api_v1/voucher.py index 5cdafba4..96c52e1e 100644 --- a/indoteknik_api/controllers/api_v1/voucher.py +++ b/indoteknik_api/controllers/api_v1/voucher.py @@ -25,14 +25,36 @@ class Voucher(controller.Controller): checkout = cart.get_user_checkout(user_id) for voucher in vouchers: - subtotal = checkout['subtotal'] + apply_status = '' + products = checkout['products'] min_purchase_amount = voucher['min_purchase_amount'] max_discount_amount = voucher['max_discount_amount'] discount_type = voucher['discount_type'] discount_amount = voucher['discount_amount'] - can_apply = subtotal >= min_purchase_amount + can_apply = True difference_to_apply = 0 - if not can_apply: + + manufacture_ids = [x['id'] for x in voucher['manufactures']] + subtotal = 0 + has_match_manufacture = False + for product in products: + price = product['price']['price'] + price_discount = product['price']['price_discount'] + quantity = product['quantity'] + manufacture_id = product['manufacture']['id'] or False + + if len(manufacture_ids) == 0 or manufacture_id in manufacture_ids: + purchase_amt = price * quantity + discount_amt = (price - price_discount) * quantity + subtotal += purchase_amt - discount_amt + has_match_manufacture = True + + if not has_match_manufacture: + can_apply = False + apply_status = 'UM' + elif subtotal < min_purchase_amount: + can_apply = False + apply_status = 'MPA' difference_to_apply = min_purchase_amount - subtotal discount_voucher = 0 @@ -45,6 +67,7 @@ class Voucher(controller.Controller): discount_voucher = max_discount_amount voucher['can_apply'] = can_apply + voucher['apply_status'] = apply_status voucher['discount_voucher'] = discount_voucher voucher['difference_to_apply'] = difference_to_apply -- cgit v1.2.3 From f58e8b41012f692b7995026b9869d7c07763b250 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Fri, 21 Jul 2023 11:20:34 +0700 Subject: Update voucher manufacture response --- indoteknik_api/controllers/api_v1/voucher.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'indoteknik_api/controllers/api_v1/voucher.py') diff --git a/indoteknik_api/controllers/api_v1/voucher.py b/indoteknik_api/controllers/api_v1/voucher.py index 96c52e1e..454961da 100644 --- a/indoteknik_api/controllers/api_v1/voucher.py +++ b/indoteknik_api/controllers/api_v1/voucher.py @@ -34,7 +34,7 @@ class Voucher(controller.Controller): can_apply = True difference_to_apply = 0 - manufacture_ids = [x['id'] for x in voucher['manufactures']] + manufacture_ids = voucher['manufacture_ids'] subtotal = 0 has_match_manufacture = False for product in products: @@ -42,7 +42,7 @@ class Voucher(controller.Controller): price_discount = product['price']['price_discount'] quantity = product['quantity'] manufacture_id = product['manufacture']['id'] or False - + if len(manufacture_ids) == 0 or manufacture_id in manufacture_ids: purchase_amt = price * quantity discount_amt = (price - price_discount) * quantity -- cgit v1.2.3 From bc0c9e782140d82dc2147afb4a049c37141b081a Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Fri, 21 Jul 2023 17:03:15 +0700 Subject: Add exclude voucher pricelist API --- indoteknik_api/controllers/api_v1/voucher.py | 3 +++ 1 file changed, 3 insertions(+) (limited to 'indoteknik_api/controllers/api_v1/voucher.py') diff --git a/indoteknik_api/controllers/api_v1/voucher.py b/indoteknik_api/controllers/api_v1/voucher.py index 454961da..a6a88cad 100644 --- a/indoteknik_api/controllers/api_v1/voucher.py +++ b/indoteknik_api/controllers/api_v1/voucher.py @@ -18,6 +18,9 @@ class Voucher(controller.Controller): if code: visibility.append('private') parameter += [('code', '=', code)] + user_pricelist = request.env.user_pricelist + if user_pricelist: + parameter += [('excl_pricelist_ids', 'not in', [user_pricelist.id])] parameter += [('visibility', 'in', visibility)] vouchers = request.env['voucher'].get_active_voucher(parameter) -- cgit v1.2.3