1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
from .. import controller
from odoo import http
from odoo.http import request
class Voucher(controller.Controller):
prefix = '/api/v1/'
@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')
source = kw.get('source')
visibility = ['public']
parameter = []
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)
vouchers = vouchers.res_format()
checkout = cart.get_user_checkout(user_id, source=source)
for voucher in vouchers:
apply_status = ''
products = checkout['products']
min_purchase_amount = voucher['min_purchase_amount']
can_apply = False
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:
has_match_manufacture = True
if product['has_flashsale']:
continue
purchase_amt = price * quantity
discount_amt = (price - price_discount) * quantity
subtotal += purchase_amt - discount_amt
has_flashsale_products = any(product['has_flashsale'] for product in products)
if not has_match_manufacture:
apply_status = 'UM' # Unqualified Manufacture
elif subtotal < min_purchase_amount:
apply_status = 'MPA' # Minimum Purchase Amount
if has_flashsale_products:
apply_status += '-HF' # Has Flashsale
else:
can_apply = True
if subtotal < min_purchase_amount:
difference_to_apply = min_purchase_amount - subtotal
obj_voucher = request.env['voucher'].browse(voucher['id'])
discount_voucher = obj_voucher.calculate_discount(subtotal)
voucher['can_apply'] = can_apply
voucher['apply_status'] = apply_status
voucher['has_flashsale_products'] = has_flashsale_products
voucher['discount_voucher'] = discount_voucher
voucher['difference_to_apply'] = difference_to_apply
sorted_vouchers = sorted(vouchers, key=lambda x: x['can_apply'], reverse=True)
return self.response(sorted_vouchers)
|