summaryrefslogtreecommitdiff
path: root/indoteknik_api/controllers/api_v1/voucher.py
blob: 5cdafba4839a9e6972e7a576b791d34bbce0fd48 (plain)
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
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')
        visibility = ['public']

        parameter = []
        if code:
            visibility.append('private')
            parameter += [('code', '=', code)]

        parameter += [('visibility', 'in', visibility)]
        vouchers = request.env['voucher'].get_active_voucher(parameter)
        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)