summaryrefslogtreecommitdiff
path: root/indoteknik_api/controllers/api_v1/voucher.py
diff options
context:
space:
mode:
Diffstat (limited to 'indoteknik_api/controllers/api_v1/voucher.py')
-rw-r--r--indoteknik_api/controllers/api_v1/voucher.py69
1 files changed, 62 insertions, 7 deletions
diff --git a/indoteknik_api/controllers/api_v1/voucher.py b/indoteknik_api/controllers/api_v1/voucher.py
index 0990a1a0..a6a88cad 100644
--- a/indoteknik_api/controllers/api_v1/voucher.py
+++ b/indoteknik_api/controllers/api_v1/voucher.py
@@ -2,21 +2,76 @@ from .. import controller
from odoo import http
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')
- visibility = 'public'
+ user_id = kw.get('user_id')
+ visibility = ['public']
parameter = []
if code:
- visibility = 'private'
+ 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', '=', visibility)]
+ 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:
+ 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 = True
+ difference_to_apply = 0
+
+ manufacture_ids = voucher['manufacture_ids']
+ 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
+ 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['apply_status'] = apply_status
+ voucher['discount_voucher'] = discount_voucher
+ voucher['difference_to_apply'] = difference_to_apply
+
+ return self.response(vouchers)