# -*- coding: utf-8 -*-

#    Copyright 2018 Youssef El Ouahby <youssef@yaltik.com>
#    Copyright 2018 Fabien Bourgeois <fabien@yaltik.com>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

""" Grant File Amount Management"""

from odoo import models, fields, api, _


class GrantFileAmountLine(models.Model):
    """ Grant File Amount Line Management """
    _name = 'grant.file.amount.line'
    _sql_constraints = [('grant_file_amount_line_uniq',
                         'UNIQUE (file_id, name)',
                         _('An amount line had already been named like that for '
                           'this grant file'))]

    file_id = fields.Many2one('grant.file', required=True, index=True,
                              auto_join=True, string='Grant file',
                              ondelete='cascade')
    currency_id = fields.Many2one(related='file_id.currency_id')
    file_requested_amount = fields.Monetary(related='file_id.requested_amount')
    file_notified_amount = fields.Monetary(related='file_id.notified_amount')
    file_received_amount = fields.Monetary(related='file_id.received_amount')
    name = fields.Char(required=True)
    requested_amount = fields.Monetary()
    notified_amount = fields.Monetary()
    received_amount = fields.Monetary()
    remaining_amount = fields.Monetary(compute='_compute_remaining_amount')

    @api.depends('notified_amount', 'received_amount')
    def _compute_remaining_amount(self):
        """ Computes remaining amount """
        for line in self:
            line.remaining_amount = line.notified_amount - line.received_amount