summaryrefslogtreecommitdiff
path: root/indoteknik_api/controllers/api_v1
diff options
context:
space:
mode:
authorRafi Zadanly <zadanlyr@gmail.com>2023-07-20 10:43:13 +0700
committerRafi Zadanly <zadanlyr@gmail.com>2023-07-20 10:43:13 +0700
commited231d1cfa4e78b8f98c5406f88c1f985c0225a7 (patch)
tree601335e1c5594142c0dc21e490d1cf8015bb3c44 /indoteknik_api/controllers/api_v1
parent7b76facc6b7fdbff83a98a1b3251f1d3dad48937 (diff)
Add response on vouccher api
- can apply - discount voucher - difference to apply
Diffstat (limited to 'indoteknik_api/controllers/api_v1')
-rw-r--r--indoteknik_api/controllers/api_v1/voucher.py36
1 files changed, 32 insertions, 4 deletions
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/<user_id>/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)