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

_logger = logging.getLogger(__name__)


class StockVendor(models.Model):
    _name = "stock.vendor"
    _description = "Stock Vendor"
    _rec_name = "product_variant_id"

    product_variant_id = fields.Many2one("product.product", string="Product Variant")
    quantity = fields.Integer(string="Quantity")
    cache_reset_status = fields.Selection([
        ('reset', 'Reset'),
        ('done', 'Done')
    ], string="Cache Reset")

    def cache_reset(self):
        stocks = self.env['stock.vendor'].search([
            ('cache_reset_status', '=', 'reset'),
        ])

        templates = []
        for stock in stocks:
            templates.append(stock.product_variant_id.product_tmpl_id)
            # stock.cache_reset_status = 'done'
        templates = list(dict.fromkeys(templates))

        for template in templates:
            if template.solr_flag == 1:
                template.solr_flag = 2
                _logger.info('Update Solr Flag to 2 %s' % template.name)

    def update_product_template(self):
        updated_virtual_qty_product_template = self.env['ir.config_parameter'].search([('key', '=', 'updated_virtual_qty_product_template')], limit=1)
        updated_virtual_qty_product_template_value = int(updated_virtual_qty_product_template.value)

        limit = 10000
        query = [('active', '=', True), ('type', '=', 'product')]
        templates = self.env['product.template'].search(query, limit=limit, offset=updated_virtual_qty_product_template_value)
        template_count = self.env['product.template'].search_count(query)

        if (updated_virtual_qty_product_template_value + limit) > template_count:
            updated_virtual_qty_product_template.value = '0'
        else:
            updated_virtual_qty_product_template.value = str(limit + updated_virtual_qty_product_template_value)

        for template in templates:
            template.virtual_qty = template.qty_stock_vendor or 0
            _logger.info("Update Stock Product Template %s : %s" % (template.id, template.virtual_qty))

    # @api.onchange('quantity')
    # def update_solr_flag(self):
    #     for stock in self:
    #         if stock.product_variant_id.product_tmpl_id.solr_flag == 1:
    #             stock.product_variant_id.product_tmpl_id.solr_flag = 2