# This file is part of Tradon. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.

from trytond.model import ModelSQL, ModelView, fields

__all__ = ['MarketIndex', 'MarketIndexRate', 'IndexTerm', 'InterestFormula']


class MarketIndex(ModelSQL, ModelView):
    'Market Index'
    __name__ = 'trade_finance.market_index'
    _rec_name = 'name'

    code = fields.Char('Code', required=True)
    name = fields.Char('Name', required=True)
    rates = fields.One2Many(
        'trade_finance.market_index_rate', 'index', 'Rates')
    active = fields.Boolean('Active')

    @staticmethod
    def default_active():
        return True


class MarketIndexRate(ModelSQL, ModelView):
    'Market Index Rate'
    __name__ = 'trade_finance.market_index_rate'

    index = fields.Many2One(
        'trade_finance.market_index', 'Index',
        required=True, ondelete='CASCADE')
    rate_date = fields.Date('Date', required=True)
    rate = fields.Numeric('Rate (%)', digits=(16, 6), required=True)


class IndexTerm(ModelSQL, ModelView):
    'Index Term'
    __name__ = 'trade_finance.index_term'
    _rec_name = 'name'

    code = fields.Char('Code', required=True)
    name = fields.Char('Name', required=True)
    days = fields.Integer('Days', required=True,
        help='Approximate number of days for this term (O/N=1, 1W=7, 1M=30, '
             '3M=90, 6M=180, 1Y=365)')
    active = fields.Boolean('Active')

    @staticmethod
    def default_active():
        return True


class InterestFormula(ModelSQL, ModelView):
    'Interest Calculation Formula'
    __name__ = 'trade_finance.interest_formula'
    _rec_name = 'name'

    code = fields.Char('Code', required=True)
    name = fields.Char('Name', required=True)
    active = fields.Boolean('Active')

    @staticmethod
    def default_active():
        return True
