from trytond.model import ModelSQL, ModelView, fields
from trytond.pool import PoolMeta
from trytond.pyson import Eval

__all__ = [
    'PhysicalTradeIFRS',
    ]
__metaclass__ = PoolMeta


class PhysicalTradeIFRS(ModelSQL, ModelView):
    'Physical Trade - IFRS Adjustment'
    __name__ = 'account.physical_trade_ifrs'

    date = fields.Date('Date', required=True)
    comment = fields.Text('Comment', required=True)
    currency = fields.Many2One('currency.currency', 'Currency', required=True)
    currency_digits = fields.Function(
        fields.Integer('Currency Digits'),
        'on_change_with_currency_digits')
    amount = fields.Numeric(
        'Amount',
        digits=(16, Eval('currency_digits', 2)),
        depends=['currency_digits'],
        required=True)

    @fields.depends('currency')
    def on_change_with_currency_digits(self, name=None):
        if self.currency:
            return self.currency.digits
        return 2
