2018-02-26 12:17:24 +01:00
|
|
|
# Copyright 2018 Creu Blanca
|
|
|
|
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
|
|
|
|
2021-02-23 11:00:15 +01:00
|
|
|
from odoo import _, api, fields, models
|
2018-02-26 12:17:24 +01:00
|
|
|
from odoo.exceptions import UserError
|
|
|
|
|
|
|
|
|
|
|
|
class AccountLoan(models.TransientModel):
|
2021-02-23 11:00:15 +01:00
|
|
|
_name = "account.loan.pay.amount"
|
|
|
|
_description = "Loan pay amount"
|
2018-02-26 12:17:24 +01:00
|
|
|
|
2022-01-10 15:07:49 +01:00
|
|
|
loan_id = fields.Many2one(
|
|
|
|
"account.loan",
|
|
|
|
required=True,
|
|
|
|
readonly=True,
|
|
|
|
)
|
2018-02-26 12:17:24 +01:00
|
|
|
currency_id = fields.Many2one(
|
2021-02-23 11:00:15 +01:00
|
|
|
"res.currency", related="loan_id.currency_id", readonly=True
|
2018-02-26 12:17:24 +01:00
|
|
|
)
|
2022-01-10 15:07:49 +01:00
|
|
|
cancel_loan = fields.Boolean(
|
|
|
|
default=False,
|
|
|
|
)
|
2018-02-26 12:17:24 +01:00
|
|
|
date = fields.Date(required=True, default=fields.Date.today())
|
|
|
|
amount = fields.Monetary(
|
2022-01-10 15:07:49 +01:00
|
|
|
currency_field="currency_id",
|
|
|
|
string="Amount to reduce from Principal",
|
2018-02-26 12:17:24 +01:00
|
|
|
)
|
2021-02-23 11:00:15 +01:00
|
|
|
fees = fields.Monetary(currency_field="currency_id", string="Bank fees")
|
2018-02-26 12:17:24 +01:00
|
|
|
|
2021-02-23 11:00:15 +01:00
|
|
|
@api.onchange("cancel_loan")
|
2018-02-26 12:17:24 +01:00
|
|
|
def _onchange_cancel_loan(self):
|
|
|
|
if self.cancel_loan:
|
2021-02-23 11:00:15 +01:00
|
|
|
self.amount = max(
|
2021-02-23 12:15:36 +01:00
|
|
|
self.loan_id.line_ids.filtered(lambda r: not r.move_ids).mapped(
|
|
|
|
"pending_principal_amount"
|
|
|
|
)
|
2018-02-26 12:17:24 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
def new_line_vals(self, sequence):
|
|
|
|
return {
|
2021-02-23 11:00:15 +01:00
|
|
|
"loan_id": self.loan_id.id,
|
|
|
|
"sequence": sequence,
|
|
|
|
"payment_amount": self.amount + self.fees,
|
|
|
|
"rate": 0,
|
|
|
|
"interests_amount": self.fees,
|
|
|
|
"date": self.date,
|
2018-02-26 12:17:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
self.ensure_one()
|
|
|
|
if self.loan_id.is_leasing:
|
|
|
|
if self.loan_id.line_ids.filtered(
|
2021-02-23 12:15:36 +01:00
|
|
|
lambda r: r.date <= self.date and not r.move_ids
|
2018-02-26 12:17:24 +01:00
|
|
|
):
|
2021-02-23 11:00:15 +01:00
|
|
|
raise UserError(_("Some invoices are not created"))
|
2018-02-26 12:17:24 +01:00
|
|
|
if self.loan_id.line_ids.filtered(
|
2021-02-23 12:15:36 +01:00
|
|
|
lambda r: r.date > self.date and r.move_ids
|
2018-02-26 12:17:24 +01:00
|
|
|
):
|
2021-02-23 11:00:15 +01:00
|
|
|
raise UserError(_("Some future invoices already exists"))
|
2023-09-15 12:28:54 +02:00
|
|
|
else:
|
|
|
|
if self.loan_id.line_ids.filtered(
|
|
|
|
lambda r: r.date < self.date and not r.move_ids
|
|
|
|
):
|
|
|
|
raise UserError(_("Some moves are not created"))
|
|
|
|
if self.loan_id.line_ids.filtered(
|
|
|
|
lambda r: r.date > self.date and r.move_ids
|
|
|
|
):
|
|
|
|
raise UserError(_("Some future moves already exists"))
|
2021-02-23 11:00:15 +01:00
|
|
|
lines = self.loan_id.line_ids.filtered(lambda r: r.date > self.date).sorted(
|
|
|
|
"sequence", reverse=True
|
|
|
|
)
|
|
|
|
sequence = min(lines.mapped("sequence"))
|
2018-02-26 12:17:24 +01:00
|
|
|
for line in lines:
|
|
|
|
line.sequence += 1
|
2023-09-15 10:04:47 +02:00
|
|
|
line.flush_recordset()
|
2018-02-26 12:17:24 +01:00
|
|
|
old_line = lines.filtered(lambda r: r.sequence == sequence + 1)
|
|
|
|
pending = old_line.pending_principal_amount
|
|
|
|
if self.loan_id.currency_id.compare_amounts(self.amount, pending) == 1:
|
2021-02-23 11:00:15 +01:00
|
|
|
raise UserError(_("Amount cannot be bigger than debt"))
|
2018-02-26 12:17:24 +01:00
|
|
|
if self.loan_id.currency_id.compare_amounts(self.amount, 0) <= 0:
|
2021-02-23 11:00:15 +01:00
|
|
|
raise UserError(_("Amount cannot be less than zero"))
|
2018-02-26 12:17:24 +01:00
|
|
|
self.loan_id.periods += 1
|
|
|
|
self.loan_id.fixed_periods = self.loan_id.periods - sequence
|
|
|
|
self.loan_id.fixed_loan_amount = pending - self.amount
|
2021-02-23 11:00:15 +01:00
|
|
|
new_line = self.env["account.loan.line"].create(self.new_line_vals(sequence))
|
2018-02-26 12:17:24 +01:00
|
|
|
new_line.long_term_pending_principal_amount = (
|
2021-02-23 11:00:15 +01:00
|
|
|
old_line.long_term_pending_principal_amount
|
|
|
|
)
|
2018-02-26 12:17:24 +01:00
|
|
|
amount = self.loan_id.loan_amount
|
2021-02-23 11:00:15 +01:00
|
|
|
for line in self.loan_id.line_ids.sorted("sequence"):
|
2018-02-26 12:17:24 +01:00
|
|
|
if line.move_ids:
|
|
|
|
amount = line.final_pending_principal_amount
|
|
|
|
else:
|
|
|
|
line.pending_principal_amount = amount
|
|
|
|
if line.sequence != sequence:
|
|
|
|
line.rate = self.loan_id.rate_period
|
2023-09-15 10:04:47 +02:00
|
|
|
line._check_amount()
|
2018-02-26 12:17:24 +01:00
|
|
|
amount -= line.payment_amount - line.interests_amount
|
|
|
|
if self.loan_id.long_term_loan_account_id:
|
2023-09-15 10:04:47 +02:00
|
|
|
self.loan_id._check_long_term_principal_amount()
|
2018-02-26 12:17:24 +01:00
|
|
|
if self.loan_id.currency_id.compare_amounts(pending, self.amount) == 0:
|
2021-02-23 11:00:15 +01:00
|
|
|
self.loan_id.write({"state": "cancelled"})
|
2018-02-26 12:17:24 +01:00
|
|
|
return new_line.view_process_values()
|