summaryrefslogtreecommitdiff
path: root/indoteknik_api/models/product_pricelist.py
blob: e0debf3863b1c3c8662d925c620ee0970551e018 (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
from odoo import models
from pytz import timezone
from datetime import datetime


class ProductPricelist(models.Model):
    _inherit = 'product.pricelist'
    
    def get_product_price(self, pricelist_id: int, product_id: int):
        price = 0
        discounts = []
        is_compute_formula = True
        while is_compute_formula:
            pricelist = self.env['product.pricelist.item'].search([
                ('pricelist_id', '=', pricelist_id),
                ('product_id', '=', product_id)
            ], limit=1)
            if pricelist:
                if pricelist.compute_price == 'formula':
                    pricelist_id = pricelist.base_pricelist_id.id
                    discounts.append(pricelist.price_discount)
                else:
                    price = pricelist.fixed_price
                    is_compute_formula = False
            else:
                is_compute_formula = False
        return price, discounts

    def compute_price(self, pricelist_id: int, product_id: int):
        """
        Compute price with tax, discount formula, and fixed_price
        @param pricelist_id: id of product.pricelist
        @param product_id: id of product.product
        @return: returns price value from pricelist.
        """
        is_flash_sale_product = self.is_flash_sale_product(product_id)
        if is_flash_sale_product:
            pricelist_id = is_flash_sale_product

        price, discounts = self.get_product_price(pricelist_id, product_id)
        if price == 0:
            product_pricelist_default_price_id = self.env['ir.config_parameter'].get_param('product.pricelist.default_price_id')
            price, discounts = self.get_product_price(int(product_pricelist_default_price_id), product_id)
        
        price_discount = price
        discount_percentage = 0
        if price > 0:
            product = self.env['product.product'].browse(product_id)
            if product:
                product = product.product_tmpl_id
                if product.web_tax_id and not product.web_tax_id.price_include:
                    price *= (100 + product.web_tax_id.amount) / 100
                    
            price_discount = price
            for discount in discounts:
                price_discount *= (100 - discount) / 100

            if len(discounts) > 0:
                discount_percentage = (price - price_discount) / price * 100
            
        return {
            'price': price,
            'price_discount': price_discount,
            'discount_percentage': float("%.2f" % discount_percentage)
        }

    def get_lowest_product_variant_price(self, product_template: dict, pricelist_id: int):
        """
        @param product_template: dict of product_template
        @param pricelist_id: id of pricelist which have default price
        @return price: object
        """
        product_pricelist_default_price_id = self.env['ir.config_parameter'].get_param('product.pricelist.default_price_id')
        product_pricelist_default_price_id = int(product_pricelist_default_price_id)
        product_variant_ids = [x.id for x in product_template.product_variant_ids]
        product = self.env['product.pricelist.item'].search([
            ('pricelist_id', '=', product_pricelist_default_price_id),
            ('product_id', 'in', product_variant_ids)
        ], order='fixed_price asc', limit=1)
        product_id = 0
        if product:
            product_id = product.product_id.id
        return self.compute_price(pricelist_id, product_id)

    def get_active_flash_sale(self):
        """
        Check whether have active flash sale in range of date
        @return: returns pricelist: object
        """
        current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        pricelist = self.search([
            ('is_flash_sale', '=', True),
            ('start_date', '<=', current_time),
            ('end_date', '>=', current_time)
        ], limit=1, order='start_date asc')
        return pricelist

    def get_is_show_program_flash_sale(self, is_show_program):
        """
        Check whether have active flash sale in range of date
        @return: returns pricelist: object
        """

        if is_show_program == 'true':
            is_show_program = True
        else:
            is_show_program = False

        current_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        pricelist = self.search([
            ('is_flash_sale', '=', True),
            ('is_show_program', '=', is_show_program),
            ('start_date', '<=', current_time),
            ('end_date', '>=', current_time)
        ], order='number asc')
        return pricelist
    
    def is_flash_sale_product(self, product_id: int):
        """
        Check whether product is flash sale.
        @param product_id: id of product.product
        @return: returns pricelist_id: int, if product is flash sale.
        """
        pricelist = self.get_active_flash_sale()
        if pricelist:
            pricelist_product_ids = [x.product_id.id for x in pricelist.item_ids]
            if product_id in pricelist_product_ids:
                return pricelist.id
        return False


class ProductPricelistItem(models.Model):
    _inherit = 'product.pricelist.item'

    def api_single_response(self, item):
        data = {
            'id': item.product_id.id,
            'parent': {
                'id': item.product_id.product_tmpl_id.id,
                'name': item.product_id.product_tmpl_id.name,
                'image': self.env['ir.attachment'].api_image('product.template', 'image_256', item.product_id.product_tmpl_id.id)
            },
            'code': item.product_id.default_code or item.product_tmpl_id.default_code or '',
            'name': item.product_id.display_name,
            'price':{
                'price': item.product_id._get_website_price_exclude_tax(),
                'discount': item.product_id._get_website_disc(0),
                'price_discount': item.product_id._get_website_price_after_disc_and_tax()
            },
            'stock': item.product_id.qty_stock_vendor,
            'weight': item.product_id.weight,
            'attributes': [x.name for x in item.product_id.product_template_attribute_value_ids],
            'manufacture': item.product_id.api_manufacture(item.product_id)
        }
        return data