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


class ProductTemplate(models.Model):
    _inherit = 'product.template'
    
    def api_single_response(self, product_template, with_detail=''):
        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)
        data = {
            'id': product_template.id,
            'image': self.env['ir.attachment'].api_image('product.template', 'image_128', product_template.id),
            'code': product_template.default_code or '',
            'name': product_template.name,
            'lowest_price': self.env['product.pricelist'].get_lowest_product_variant_price(product_template, product_pricelist_default_discount_id),
            'variant_total': len(product_template.product_variant_ids),
            'stock_total': product_template.qty_stock_vendor,
            'weight': product_template.weight,
            'manufacture': self.api_manufacture(product_template),
            'categories': self.api_categories(product_template),
        }
        
        if with_detail != '':
            data_with_detail = {
                'image': self.env['ir.attachment'].api_image('product.template', 'image_512', product_template.id),
                'display_name': product_template.display_name,
                'variants': [self.env['product.product'].api_single_response(variant) for variant in product_template.product_variant_ids],
                'description': product_template.website_description or '',
            }
            data.update(data_with_detail)
            
        if with_detail == 'SOLR':
            is_image_found = self.env['ir.attachment'].is_found('product.template', 'image_128', product_template.id)
            rate = 0
            if data['lowest_price']['price'] > 0:
                rate += 1
            if product_template.have_promotion_program:
                rate += 1
            if is_image_found:
                rate += 1
            if product_template.website_description:
                rate += 1
            if product_template.qty_stock_vendor > 0:
                rate += 1
            data_with_detail = {
                'solr_flag': product_template.solr_flag,
                'product_rating': rate,
                'search_rank': product_template.search_rank,
                'search_rank_weekly': product_template.search_rank_weekly
            }
            data.update(data_with_detail)
        return data
    
    def v2_api_single_response(self, product_template, with_detail=''):
        data = {
            'id': product_template.id,
            'image': self.env['ir.attachment'].api_image('product.template', 'image_128', product_template.id),
            'code': product_template.default_code or '',
            'name': product_template.name,
            'variant_total': len(product_template.product_variant_ids),
            'stock_total': product_template.qty_stock_vendor,
            'weight': product_template.weight,
            'manufacture': self.api_manufacture(product_template),
            'categories': self.api_categories(product_template),
        }
        
        if with_detail != '':
            variants = [self.env['product.product'].v2_api_single_response(variant) for variant in product_template.product_variant_ids]
            lowest_price = variants[0]['price']
            for variant in variants:
                if variant["price"]["price_discount"] < lowest_price["price_discount"]:
                    lowest_price = variant['price']
            template_pricelist = product_template._get_active_flash_sale()
            data_with_detail = {
                'flash_sale': {
                    'remaining_time': template_pricelist._remaining_time_in_second() if template_pricelist else 0,
                    'tag': template_pricelist.flashsale_tag if template_pricelist else None
                },
                'lowest_price': lowest_price,
                'image': self.env['ir.attachment'].api_image('product.template', 'image_512', product_template.id),
                'display_name': product_template.display_name,
                'variants': variants,
                'description': product_template.website_description or '',
            }
            data.update(data_with_detail)
            
        if with_detail == 'SOLR':
            is_image_found = self.env['ir.attachment'].is_found('product.template', 'image_128', product_template.id)
            rate = 0
            if data['lowest_price']['price'] > 0:
                rate += 1
            if product_template.have_promotion_program:
                rate += 1
            if is_image_found:
                rate += 1
            if product_template.website_description:
                rate += 1
            if product_template.qty_stock_vendor > 0:
                rate += 1
            data_with_detail = {
                'solr_flag': product_template.solr_flag,
                'product_rating': rate,
                'search_rank': product_template.search_rank,
                'search_rank_weekly': product_template.search_rank_weekly
            }
            data.update(data_with_detail)
        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 api_categories(self, product_template):
        if product_template.public_categ_ids:
            categories = []
            for category in product_template.public_categ_ids:
                categories.append({
                    'id': category.id,
                    'name': category.name
                })
            return categories
        return []