Files
dn20/accountant_cn/wizard/dn_loan_close_wizard.py
2026-11-17 17:57:08 +08:00

44 lines
1.9 KiB
Python

from odoo import _, fields, models
from odoo.tools import format_date
class DnLoanCloseWizard(models.TransientModel):
_name = 'dn.loan.close.wizard'
_description = 'Close Loan Wizard'
loan_id = fields.Many2one('dn.loan', string='Loan', required=True,
ondelete='cascade')
currency_id = fields.Many2one(related='loan_id.currency_id')
date = fields.Date(
string='Close Date', required=True, default=fields.Date.context_today,
help="Draft entries dated after this day are deleted, because the loan "
"will not run past it. Entries already posted are kept: they are "
"in the ledger and can only be reversed, not removed.",
)
def action_save(self):
"""Stop the loan: drop the future drafts, keep what is already booked.
Only draft entries are removed. A posted entry may have been reported
already, so deleting it silently would misstate a closed period; it has
to be reversed deliberately, by whoever owns the ledger.
"""
self.ensure_one()
dropped = self.loan_id.move_ids.filtered(
lambda m: m.state == 'draft' and m.date > self.date)
kept = self.loan_id.move_ids.filtered(
lambda m: m.state == 'posted' and m.date > self.date)
dropped.unlink()
self.loan_id.state = 'closed'
body = _("Closed on the %(date)s.", date=format_date(self.env, self.date))
if dropped:
body += " " + _("%(count)s draft entries scheduled after that date "
"were removed.", count=len(dropped))
if kept:
body += " " + _("%(count)s posted entries after that date were kept "
"and must be reversed manually if needed.",
count=len(kept))
self.loan_id.message_post(body=body)
return {'type': 'ir.actions.act_window_close'}