summaryrefslogtreecommitdiff
path: root/indoteknik_api/controllers/api_v1/voucher.py
blob: 3c056ecd44edac6c6fb4c30b409f026dae0b9077 (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
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
from .. import controller
from odoo import http
from odoo.http import request
from bs4 import BeautifulSoup


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 = int(kw.get('user_id', 0))
        source = kw.get('source')
        visibility = ['public']

        parameter = []
        if code:
            visibility.append('private')
            parameter += [('code', '=', code)]
        user_pricelist = request.env.context.get('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)
        checkout = cart.get_user_checkout(user_id, source=source)
        products = checkout['products']

        user = request.env['res.users'].search([('id', '=', user_id)], limit=1)
        if not user:
            return self.response([])

        order_line = []
        for product in products:
            order_line.append({
                'product_id': request.env['product.product'].browse(product['id']),
                'price': product['price']['price'],
                'discount': product['price']['discount_percentage'],
                'qty': product['quantity'],
                'subtotal': product['subtotal']
            })
        
        results = []
        for voucher in vouchers:
            if voucher.limit > 0 and voucher.count_order >= voucher.limit:
                continue

            partner_voucher_orders = []
            for order in voucher.order_ids:
                if order.partner_id.id == user.partner_id.id:
                    partner_voucher_orders.append(order)
            
            if voucher.limit_user > 0 and len(partner_voucher_orders) >= voucher.limit_user:
                continue
            
            voucher_info = voucher.apply(order_line)
            voucher_discount = voucher_info['discount']['all']

            valid_order = voucher_info['valid_order']

            can_apply = True if valid_order and voucher_discount > 0 else False

            voucher_res = voucher.format()
            voucher_res['can_apply'] = can_apply
            voucher_res['discount_voucher'] = voucher_discount

            cleaned_tnc = BeautifulSoup(voucher.terms_conditions or '', "html.parser").get_text()
            voucher_res['terms_conditions'] = voucher.terms_conditions if cleaned_tnc else voucher.generate_tnc()

            results.append(voucher_res)
        
        sorted_results = sorted(results, key=lambda x: x['can_apply'], reverse=True)

        return self.response(sorted_results)