summaryrefslogtreecommitdiff
path: root/indoteknik_api/models/product_product.py
blob: c867eaa03c4d099cd05adc69f6b795c28610bf85 (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
from odoo import models
import math


class ProductProduct(models.Model):
    _inherit = 'product.product'
    
    def api_single_response(self, product_product):
        product_pricelist_default_discount_id = self.env['ir.config_parameter'].get_param('product.pricelist.default_discount_id')
        product_pricelist_default_discount_id = int(product_pricelist_default_discount_id)
        product_template = product_product.product_tmpl_id
        data = {
            'id': product_product.id,
            'parent': {
                'id': product_template.id,
                'name': product_template.name,
                'image': self.env['ir.attachment'].api_image('product.template', 'image_256', product_template.id),
            },
            'code': product_product.default_code or '',
            'name': product_product.display_name,
            'price': self.env['product.pricelist'].compute_price(product_pricelist_default_discount_id, product_product.id),
            'stock': product_product.qty_stock_vendor,
            'weight': product_product.weight,
            'attributes': [x.name for x in product_product.product_template_attribute_value_ids],
            'manufacture' : self.api_manufacture(product_product)
        }
        return data
    
    def v2_api_single_response(self, product_product):
        product_pricelist_default_discount_id = self.env['ir.config_parameter'].get_param('product.pricelist.default_discount_id')
        product_pricelist_default_discount_id = int(product_pricelist_default_discount_id)
        product_template = product_product.product_tmpl_id
        data = {
            'id': product_product.id,
            'parent': {
                'id': product_template.id,
                'name': product_template.name,
                'image': self.env['ir.attachment'].api_image('product.template', 'image_256', product_template.id),
            },
            'code': product_product.default_code or '',
            'name': product_product.display_name,
            'price': {
                'price': product_product._get_website_price_exclude_tax(),
                'discount_percentage': product_product._get_website_disc(0),
                'price_discount': product_product._get_website_price_after_disc_and_tax()
            },
            'stock': product_product.qty_stock_vendor,
            'weight': product_product.weight,
            'attributes': [x.name for x in product_product.product_template_attribute_value_ids],
            'manufacture' : self.api_manufacture(product_product)
        }
        return data

    def api_manufacture(self, product_template):
        if product_template.x_manufacture:
            manufacture = product_template.x_manufacture
            return {
                'id': manufacture.id,
                'name': manufacture.x_name,
                'image_promotion_1': self.env['ir.attachment'].api_image('x_manufactures', 'image_promotion_1', manufacture.id),
                'image_promotion_2': self.env['ir.attachment'].api_image('x_manufactures', 'image_promotion_2', manufacture.id),
            }
        return {}
    
    def _get_website_price_include_tax(self):
        default_pricelist_id = int(1)

        query = [
            ('pricelist_id.id', '=', default_pricelist_id),
            ('product_id.id', '=', self.id)
        ]
        pl_item1 = self.env['product.pricelist.item'].search(query, limit=1)

        retValue = pl_item1.fixed_price
        retValue = math.floor(retValue)
        return retValue
    
    def _get_website_price_exclude_tax(self):
        default_divide_tax = float(1.11)
        price_incl = self._get_website_price_include_tax()
        res = price_incl / default_divide_tax
        return math.floor(res)
    
    def _get_website_disc(self, partner_id):
        default_discount_id = int(4)

        # compile partner
        partner = self.env['res.partner'].search([('id', '=', partner_id)], limit=1)
        
        if partner:
            if not partner.parent_id: # it means this is a parent
                default_discount_id = partner.custom_pricelist_id
            else: # it means this is NOT parent
                default_discount_id = partner.parent_id.custom_pricelist_id
        query = [
            ('pricelist_id.id', '=', default_discount_id),
            ('product_id.id', '=', self.id)
        ]
        
        pl_item2 = self.env['product.pricelist.item'].search(query, limit=1)

        retValue = pl_item2.price_discount
        return retValue
    
    def _get_website_price_after_disc_and_tax(self):
        default_divide_tax = float(1.11)
        price_after_disc = self._get_website_price_after_disc()
        res = price_after_disc / default_divide_tax
        res = math.floor(res)
        return res
    
    def _get_website_price_after_disc(self):
        discount = self._get_website_disc(0)
        price_incl = self._get_website_price_include_tax()
        res = 0
        if discount > 0:
            res = price_incl - (price_incl * discount / 100)
        else:
            res = price_incl
        return math.floor(res)
    
    def _get_website_tax(self):
        default_percent_tax = float(11)
        price_after_disc = self._get_website_price_after_disc_and_tax()
        res = price_after_disc * default_percent_tax / 100
        return math.floor(res)