summaryrefslogtreecommitdiff
path: root/indoteknik_api/controllers/api_v1/promotion.py
blob: 221f6e10af3e90e6a4b7ce8eb884b1aab2c3b4a2 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
from .. import controller
from odoo import http
from odoo.http import request
from datetime import datetime


class Promotion(controller.Controller):
    prefix = '/api/v1/'
    
    @http.route(prefix + 'program-line/<id>/stock', auth='public', methods=['GET', 'OPTIONS'])
    @controller.Controller.must_authorized()
    def get_promotion_stock(self, id):
        program_line = request.env['promotion.program.line'].browse(int(id))
        if not program_line.id:
            return self.response(code=400, description='program not found')
        
        user_data = self.verify_user_token()
        
        limit_qty = program_line._res_limit_qty()
        remaining_qty = program_line._get_remaining_qty(user_data)
        
        percent_remaining = 0
        if limit_qty['all'] > 0:
            percent_remaining = (limit_qty['all'] - remaining_qty['all']) / limit_qty['all'] * 100
            
        return self.response({
            'limit_qty': limit_qty,
            'remaining_qty': remaining_qty,
            'used_percentage': percent_remaining,
        })


    @http.route(prefix + 'promotion/<id>', auth='public', methods=['GET'])
    @controller.Controller.must_authorized()
    def get_promotion_by_id(self, **kw):
        base_url = request.env['ir.config_parameter'].get_param('web.base.url')
        id = kw.get('id')
        if not id:
            return self.response(code=400, description='id is required')

        data = {}
        id = int(id)
        coupon_program = request.env['coupon.program'].search([('id', '=', id)])
        if coupon_program:
            data = {
                'banner': base_url + 'api/image/coupon.program/x_studio_banner_promo/' + str(coupon_program.id) if coupon_program.x_studio_banner_promo else '',
                'image': base_url + 'api/image/coupon.program/x_studio_image_promo/' + str(coupon_program.id) if coupon_program.x_studio_image_promo else '',
                'name': coupon_program.name,
            }

        return self.response(data)


    @http.route(prefix + 'promotion/home', auth='public', methods=['GET', 'OPTIONS'])
    @controller.Controller.must_authorized()
    def v1_get_promotion_home(self):
        current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        programs = request.env['promotion.program'].search([
            ('start_time', '<=', current_time),
            ('end_time', '>=', current_time),
            ('program_line.display_on_homepage', '=', True)
        ])

        if not programs:
            return self.response(None)

        data = []
        for program in programs:
            data_program = {
                'id': program.id,
                'name': program.name,
                'banner': request.env['ir.attachment'].api_image('promotion.program', 'banner', program.id),
                'icon': request.env['ir.attachment'].api_image('promotion.program', 'icon', program.id)
            }
            data.append(data_program)

        return self.response(data)


    @http.route(prefix + 'promotion/home/<id>', auth='public', methods=['GET', 'OPTIONS'])
    @controller.Controller.must_authorized()
    def v1_get_promotion_home_detail(self, id):
        program_lines = request.env['promotion.program.line'].search([
            ('display_on_homepage', '=', True),
            ('promotion_type', '=', 'special_price'),
            ('program_id', '=', int(id))
        ])
        
        data = []
        for line in program_lines:
            product = request.env['product.product'].v2_api_single_response(line.product_id)
            product_template = line.product_id.product_tmpl_id

            product.update({
                'id': product['parent']['id'],
                'image': product['parent']['image'],
                'name': product['parent']['name'],
                'variant_total': len(product_template.product_variant_ids),
                'lowest_price': line.calculate_price(product['price']),
                'stock_total': product['stock'],
                'icon': {
                    'top': request.env['ir.attachment'].api_image('promotion.program', 'icon_top', line.program_id.id),
                    'bottom': request.env['ir.attachment'].api_image('promotion.program', 'icon_bottom', line.program_id.id)
                }
            })

            product.pop('parent', None)
            product.pop('price', None)
            product.pop('stock', None)
            data.append(product)

        return self.response(data)