from datetime import datetime, date
from odoo import models, fields, api


class SetParametersContract(models.Model):
    _name = 'set.parameters.contract'
    _rec_name = 'set_contract_name'

    request_object = fields.Many2one(comodel_name='request.object', string='請求對象', required = True)
    # request_program = fields.Selection(selection=[(1, '留遊學-長期'), (2, '留遊學-短期'), (3, '工作方案'), (4, '住宿')], string='方案', required = True)
    set_contract_name = fields.Char(string='合約名稱', required = True)
    contract_period = fields.Selection(selection=[(1, '每月請求'), (2, '一次請求半年'), (3, '一次請求一年'), (4, '其他')],string='合約類別', required = True)
    contract_start_date = fields.Date(string='合約開始日')
    contract_end_date = fields.Date(string='合約結束日')
    # currency_id = fields.Many2one('res.currency', string='貨幣', required = True)
    product = fields.Many2one('product.product', string='產品', required = True)
    set_parameters_contract_ids = fields.One2many(comodel_name='set.parameters.contract.line', inverse_name='set_parameters_contract_id', string='明細')
    set_contract_code = fields.One2many(comodel_name='request.summary', inverse_name='contract_name', string='合約名稱')
    start_date = fields.Date(string='開始預定日')
    study_period = fields.Char(string='修業期間')


class SetParametersContractLine(models.Model):
    _name = 'set.parameters.contract.line'

    set_parameters_contract_id = fields.Many2one(comodel_name='set.parameters.contract', string='請求書合約主檔')
    installments = fields.Selection(selection=[(1, '第一期'), (2, '第二期'), (3, '第三期'), (4, '第四期')], string='期數')
    remarks = fields.Char(string='備註')
    fixed_amount = fields.Integer(string='固定金額')
    tuition = fields.Integer(string='學費')
    admission_fee = fields.Integer(string='入學金')
    percentage = fields.Float(string='百分比')
    price_total = fields.Integer(string='金額', compute='compute_price_total')
    other_receipts = fields.Integer(string='其他收款')

    @api.onchange('fixed_amount','tuition','admission_fee','percentage','other_receipts')
    def compute_price_total(self):
        for line in self:
            if line.fixed_amount != 0 and line.tuition == 0 and line.admission_fee == 0:
                line.price_total = line.fixed_amount + line.other_receipts
            elif line.tuition != 0 and line.admission_fee != 0 and line.percentage != 0:
                line.price_total = ((line.tuition + line.admission_fee) * line.percentage/100) + line.other_receipts
            elif line.tuition != 0 and line.percentage != 0:
                line.price_total = (line.tuition * line.percentage/100) + line.other_receipts