summaryrefslogtreecommitdiff
path: root/indoteknik_custom/models/product_template.py
blob: a51f0600456f22bc6346770c1aa1a2179e17982b (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
from odoo import fields, models, api
from datetime import datetime, timedelta


class ProductTemplate(models.Model):
    _inherit = "product.template"

    x_studio_field_tGhJR = fields.Many2many('x_product_tags', string="Product Tags")
    x_manufacture = fields.Many2one(
        comodel_name="x_manufactures",
        string="Manufactures"
    )
    x_model_product = fields.Char(string="Model Produk")
    x_product_manufacture = fields.Many2one(
        comodel_name="x_manufactures",
        string="Manufacture"
    )
    x_lazada = fields.Text(string="Lazada")
    x_tokopedia = fields.Text(string="Tokopedia")
    web_tax_id = fields.Many2one('account.tax', string='Website Tax')
    web_price = fields.Float(
        'Web Price', compute='_compute_web_price',
        digits='Product Price', inverse='_set_product_lst_price',
        help="Web Price with pricelist_id = 1")
    qty_stock_vendor = fields.Float('QTY Stock Vendor', compute='_compute_qty_stock_vendor')
    have_promotion_program = fields.Boolean('Have Promotion Program', compute='_have_promotion_program', help="Punya promotion program gak?")
    product_rating = fields.Float('Product Rating', help="Digunakan untuk sorting product di website")
    last_calculate_rating = fields.Datetime("Last Calculate Rating")
    web_price_sorting = fields.Float('Web Price Sorting', help='Hanya digunakan untuk sorting di web, harga tidak berlaku')

    def _compute_qty_stock_vendor(self):
        for product_template in self:
            product_template.qty_stock_vendor = 0
            for product_variant in product_template.product_variant_ids:
                product_template.qty_stock_vendor += product_variant.qty_stock_vendor
                
    def _compute_web_price(self):
        for template in self:
            product = self.env['product.product'].search([('product_tmpl_id', '=', template.id)],limit=1)
            product_pricelist_item = self.env['product.pricelist.item'].search([('pricelist_id', '=', 1),('product_id', '=', product.id)],limit=1)
            template.web_price = product_pricelist_item.fixed_price

    def _have_promotion_program(self):
        for template in self:
            domain = [
                ('rule_products_domain', 'ilike', template.x_manufacture.x_name),
                ('active', '=', True)
            ]
            coupon_program = self.env['coupon.program'].search(domain, limit=1)
            if coupon_program:
                template.have_promotion_program = True
            else:
                template.have_promotion_program = False

    @api.model
    def _calculate_rating_product(self):
        current_time = datetime.now()
        delta_time = current_time - timedelta(days=30)

        current_time = current_time.strftime('%Y-%m-%d %H:%M:%S')
        delta_time = delta_time.strftime('%Y-%m-%d %H:%M:%S')

        products = self.env['product.template'].search([
            ('type', '=', 'product'),
            ('active', '=', True),
            ('last_calculate_rating', '!=', False),
        ], limit=5000)
        for product in products:
            print("Calculate Rating Product ", product)
            rate = 0
            if product.web_price:
                rate += 1
                product.web_price_sorting = product.web_price
            if product.have_promotion_program:
                rate += 1
            if product.image_128:
                rate += 1
            product.product_rating = rate
            product.last_calculate_rating = current_time

        products = self.env['product.template'].search([
            ('type', '=', 'product'),
            ('active', '=', True),
            ('last_calculate_rating', '<', delta_time),
        ], limit=5000)
        for product in products:
            print("Calculate Rating Product OutOfDate", product)
            rate = 0
            if product.web_price:
                rate += 1
                product.web_price_sorting = product.web_price
            if product.have_promotion_program:
                rate += 1
            if product.image_128:
                rate += 1
            product.product_rating = rate
            product.last_calculate_rating = current_time


class ProductProduct(models.Model):
    _inherit = "product.product"
    web_price = fields.Float(
        'Web Price', compute='_compute_web_price',
        digits='Product Price', inverse='_set_product_lst_price',
        help="Web Price with pricelist_id = 1")
    qty_stock_vendor = fields.Float(
        'Qty Stock Vendor', compute='_compute_stock_vendor',
        help="Stock Vendor")

    def _compute_web_price(self):
        for product in self:
            product_pricelist_item = self.env['product.pricelist.item'].search([('pricelist_id', '=', 1),('product_id', '=', product.id)],limit=1)
            product.web_price = product_pricelist_item.fixed_price

    def _compute_stock_vendor(self):
        for product in self:
            stock_vendor = self.env['stock.vendor'].search([('product_variant_id', '=', product.id)],limit=1)
            product.qty_stock_vendor = stock_vendor.quantity+product.qty_available