summaryrefslogtreecommitdiff
path: root/indoteknik_api/controllers/api_v1/promotion.py
blob: 991a3eb9ed029111e1d9a5ec6acc1fe0b3a4b61a (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
114
115
116
117
118
119
120
121
122
123
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 + '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),
        ])
        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))
        ])
        pricelist = self.user_pricelist()
        data = []
        for line in program_lines:
            product = request.env['product.product'].v2_api_single_response(line.product_id, pricelist=pricelist)
            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': 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)
                }
            })
            price = product['lowest_price']['price']
            if line.discount_type == 'percentage':
                product['lowest_price']['discount_percentage'] = line.discount_amount
                product['lowest_price']['price_discount'] = price - (price * line.discount_amount / 100)
            if line.discount_type == 'fixed_price':
                product['lowest_price']['price_discount'] = line.discount_amount
                product['lowest_price']['discount_percentage'] = round((price - line.discount_amount) / price * 100)

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


    @http.route(prefix + 'promotion/product/<id>', auth='public', methods=['GET', 'OPTIONS'])
    def v1_get_promotion_by_product_id(self, id):
        id = int(id)
        current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        product = request.env['product.template'].search([('id', '=', id)])
        variant_ids = [variant.id for variant in product.product_variant_ids]
        program_lines = request.env['promotion.program.line'].search([
            ('program_id.start_time', '<=', current_time),
            ('program_id.end_time', '>=', current_time),
            ('product_id.id', 'in', variant_ids)
        ])

        data = []
        ir_attachment = request.env['ir.attachment']
        for line in program_lines:
            data.append({
                'name': line.name,
                'image': ir_attachment.api_image('promotion.program.line', 'image', line.id),
                'type': line.promotion_type,
                'minimum_purchase_qty': line.minimum_purchase_qty,
                'applies_multiply': line.applies_multiply,
                'limit_qty': line.limit_qty,
                'limit_qty_user': line.limit_qty_user,
                'limit_qty_transaction': line.limit_qty_transaction,
            })
        return self.response(data)