res_config.py 4.37 KB
Newer Older
François C. committed
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
# -*- coding: utf-8 -*-

from openerp import fields, models, api


class PurchaseConfigSettings(models.TransientModel):
    _inherit = 'stock.config.settings'

    def _get_consumption_calculation_method(self):
        return [
            ('moves', 'Moves (calculate consumption based on Stock Moves)'),
            ('history', 'Calculate consumption based on the Product History'),
        ]

    default_consumption_calculation_method = fields.Selection(
        _get_consumption_calculation_method,
        'Consumption Calculation Method', default='moves',
        default_model='product.template')
    default_calculation_range = fields.Integer(
        'Calculation Range in days', default=365,
        default_model='product.template', help="""This field is used if the"""
        """ selected method is based on Stock Moves."""
        """Number of days used for"""
        """ the calculation of the average consumption. For example: if you"""
        """ put 365, the calculation will be done on last year.""")
    default_display_range = fields.Integer(
        'Display Range in days', default=1,
        default_model='product.template', help="""Examples:
        1 -> Average Consumption per days
        7 -> Average Consumption per week
        30 -> Average Consumption per month""")
    module_product_history = fields.Boolean(
        "View product History",
        help="This will install product_history module")

    @api.onchange('default_consumption_calculation_method')
    def _onchange_default_consumption_calculation_method(self):
        if self.default_consumption_calculation_method == 'history':
            self.module_product_history = True

    @api.onchange('module_product_history')
    def _onchange_module_product_history(self):
        if not self.module_product_history:
            self.default_consumption_calculation_method = 'moves'

    @api.model
    def create(self, vals):
        if vals.get('default_display_range', False):
            self.env.cr.execute("""
                UPDATE product_template
                SET display_range=%i""" % (
                vals.get('default_display_range')))
        if vals.get('default_calculation_range', False):
            self.env.cr.execute("""
                UPDATE product_template
                SET calculation_range=%i""" % (
                vals.get('default_calculation_range')))
        if vals.get('default_number_of_periods', False):
            self.env.cr.execute("""
                UPDATE product_template
                SET number_of_periods=%i""" % (
                vals.get('default_number_of_periods')))
        if vals.get('default_consumption_calculation_method', False):
            self.env.cr.execute("""
                UPDATE product_template
                SET consumption_calculation_method='%s'""" % (
                vals.get('default_consumption_calculation_method')))
        return super(PurchaseConfigSettings, self).create(vals)

    @api.multi
    def write(self, vals):
        for config in self:
            if vals.get('default_display_range', False):
                self.env.cr.execute("""
                    UPDATE product_template
                    SET display_range=%i""" % (
                    vals.get('default_display_range')))
            if vals.get('default_calculation_range', False):
                self.env.cr.execute("""
                    UPDATE product_template
                    SET calculation_range=%i""" % (
                    vals.get('default_calculation_range')))
            if vals.get('default_number_of_periods', False):
                self.env.cr.execute("""
                    UPDATE product_template
                    SET number_of_periods=%i""" % (
                    vals.get('default_number_of_periods')))
            if vals.get('default_consumption_calculation_method', False):
                self.env.cr.execute("""
                    UPDATE product_template
                    SET consumption_calculation_method='%s'""" % (
                    vals.get('default_consumption_calculation_method')))
        return super(PurchaseConfigSettings, self).write(vals)

    @api.onchange('default_calculation_range')
    @api.multi
    def _onchange_default_calculation_range(self):
        for config in self:
            self.env.cr.execute("""
                UPDATE product_template
                SET calculation_range=%i""" % (
                config.default_calculation_range))