2
0
account-financial-tools/account_loan/wizards/account_loan_post.py

94 lines
3.1 KiB
Python
Raw Normal View History

2018-02-26 12:17:24 +01:00
# Part of Odoo. See LICENSE file for full copyright and licensing details.
2023-09-15 10:04:47 +02:00
from odoo import Command, _, api, fields, models
2018-02-26 12:17:24 +01:00
from odoo.exceptions import UserError
class AccountLoanPost(models.TransientModel):
_name = "account.loan.post"
_description = "Loan post"
2018-02-26 12:17:24 +01:00
@api.model
def _default_journal_id(self):
loan_id = self.env.context.get("default_loan_id")
2018-02-26 12:17:24 +01:00
if loan_id:
return self.env["account.loan"].browse(loan_id).journal_id.id
2018-02-26 12:17:24 +01:00
@api.model
def _default_account_id(self):
loan_id = self.env.context.get("default_loan_id")
2018-02-26 12:17:24 +01:00
if loan_id:
loan = self.env["account.loan"].browse(loan_id)
2018-02-26 12:17:24 +01:00
if loan.is_leasing:
return loan.leased_asset_account_id.id
else:
2022-01-10 15:07:49 +01:00
return loan.partner_id.with_company(
loan.company_id
2018-02-26 12:17:24 +01:00
).property_account_receivable_id.id
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
journal_id = fields.Many2one(
"account.journal", required=True, default=lambda r: r._default_journal_id()
2018-02-26 12:17:24 +01:00
)
account_id = fields.Many2one(
"account.account", required=True, default=lambda r: r._default_account_id()
2018-02-26 12:17:24 +01:00
)
def move_line_vals(self):
res = list()
2022-01-10 15:07:49 +01:00
partner = self.loan_id.partner_id.with_company(self.loan_id.company_id)
2018-02-26 12:17:24 +01:00
line = self.loan_id.line_ids.filtered(lambda r: r.sequence == 1)
res.append(
{
"account_id": self.account_id.id,
"partner_id": partner.id,
"credit": 0,
"debit": line.pending_principal_amount,
}
)
if line.pending_principal_amount - line.long_term_pending_principal_amount > 0:
res.append(
{
"account_id": self.loan_id.short_term_loan_account_id.id,
"credit": (
line.pending_principal_amount
- line.long_term_pending_principal_amount
),
"debit": 0,
}
)
2018-02-26 12:17:24 +01:00
if (
line.long_term_pending_principal_amount > 0
and self.loan_id.long_term_loan_account_id
2018-02-26 12:17:24 +01:00
):
res.append(
{
"account_id": self.loan_id.long_term_loan_account_id.id,
"credit": line.long_term_pending_principal_amount,
"debit": 0,
}
)
2018-02-26 12:17:24 +01:00
return res
def move_vals(self):
return {
"loan_id": self.loan_id.id,
"date": self.loan_id.start_date,
"ref": self.loan_id.name,
"journal_id": self.journal_id.id,
2023-09-15 10:04:47 +02:00
"line_ids": [Command.create(vals) for vals in self.move_line_vals()],
2018-02-26 12:17:24 +01:00
}
def run(self):
self.ensure_one()
if self.loan_id.state != "draft":
raise UserError(_("Only loans in draft state can be posted"))
2018-02-26 12:17:24 +01:00
self.loan_id.post()
move = self.env["account.move"].create(self.move_vals())
move.action_post()