product.py 4.37 KB
Newer Older
1 2
# This file is part of Tryton.  The COPYRIGHT file at the top level of this
# repository contains the full copyright notices and license terms.
3
from trytond import backend
4 5
from trytond.model import fields
from trytond.pyson import Eval
6
from trytond.pool import PoolMeta, Pool
7 8
from trytond.modules.account_product.product import (
    account_used, template_property)
9

10
__all__ = ['Category', 'CategoryAccount', 'Template', 'Product']
11 12


13
class Category(metaclass=PoolMeta):
14
    __name__ = 'product.category'
15
    account_depreciation = fields.MultiValue(fields.Many2One('account.account',
16 17
            'Account Depreciation', domain=[
                ('kind', '=', 'other'),
18
                ('company', '=', Eval('context', {}).get('company', -1)),
19 20 21
                ],
            states={
                'invisible': (~Eval('context', {}).get('company')
22 23 24 25
                    | Eval('account_parent')
                    | ~Eval('accounting', False)),
                },
            depends=['account_parent', 'accounting']))
26
    account_asset = fields.MultiValue(fields.Many2One('account.account',
27 28 29
            'Account Asset',
            domain=[
                ('kind', '=', 'expense'),
30
                ('company', '=', Eval('context', {}).get('company', -1)),
31 32 33
                ],
            states={
                'invisible': (~Eval('context', {}).get('company')
34 35 36 37
                    | Eval('account_parent')
                    | ~Eval('accounting', False)),
                },
            depends=['account_parent', 'accounting']))
38

39 40 41 42 43 44 45
    @classmethod
    def multivalue_model(cls, field):
        pool = Pool()
        if field in {'account_depreciation', 'account_asset'}:
            return pool.get('product.category.account')
        return super(Category, cls).multivalue_model(field)

46 47 48 49 50 51 52 53 54 55
    @property
    @account_used('account_depreciation')
    def account_depreciation_used(self):
        pass

    @property
    @account_used('account_asset')
    def account_asset_used(self):
        pass

56

57
class CategoryAccount(metaclass=PoolMeta):
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    __name__ = 'product.category.account'
    account_depreciation = fields.Many2One(
        'account.account', "Account Depreciation",
        domain=[
            ('kind', '=', 'other'),
            ('company', '=', Eval('company', -1)),
            ],
        depends=['company'])
    account_asset = fields.Many2One(
        'account.account', "Account Asset",
        domain=[
            ('kind', '=', 'expense'),
            ('company', '=', Eval('company', -1)),
            ],
        depends=['company'])

    @classmethod
    def __register__(cls, module_name):
        TableHandler = backend.get('TableHandler')
        exist = TableHandler.table_exist(cls._table)
        if exist:
79
            table = cls.__table_handler__(module_name)
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
            exist &= (table.column_exist('account_depreciation')
                and table.column_exist('account_asset'))

        super(CategoryAccount, cls).__register__(module_name)

        if not exist:
            # Re-migration
            cls._migrate_property([], [], [])

    @classmethod
    def _migrate_property(cls, field_names, value_names, fields):
        field_names.extend(['account_depreciation', 'account_asset'])
        value_names.extend(['account_depreciation', 'account_asset'])
        super(CategoryAccount, cls)._migrate_property(
            field_names, value_names, fields)

96

97
class Template(metaclass=PoolMeta):
98 99 100 101 102
    __name__ = 'product.template'
    depreciable = fields.Boolean('Depreciable', states={
            'readonly': ~Eval('active', True),
            'invisible': Eval('type', '') != 'assets',
            }, depends=['active', 'type'])
103 104 105 106 107 108 109 110 111 112
    depreciation_duration = fields.Integer(
        "Depreciation Duration",
        states={
            'readonly': ~Eval('active', True),
            'invisible': (~Eval('depreciable')
                | (Eval('type', '') != 'assets')),
            },
        depends=['active', 'depreciable', 'type'],
        help='In months')

113
    @property
114
    @account_used('account_depreciation', 'account_category')
115 116 117 118
    def account_depreciation_used(self):
        pass

    @property
119
    @account_used('account_asset', 'account_category')
120 121 122
    def account_asset_used(self):
        pass

123

124
class Product(metaclass=PoolMeta):
125
    __name__ = 'product.product'
126 127 128

    account_depreciation_used = template_property('account_depreciation_used')
    account_asset_used = template_property('account_asset_used')