from odoo import _, fields, models class AccountMove(models.Model): _inherit = 'account.move' dn_loan_line_id = fields.Many2one( 'dn.loan.line', string='Loan Installment', help="Installment of the loan that generated this entry.", copy=False, readonly=True, index=True, ondelete='cascade', ) dn_loan_id = fields.Many2one( related='dn_loan_line_id.loan_id', string='Loan', store=True, index=True, ) is_loan_payment_move = fields.Boolean( string='Is Loan Payment', help="True for the entry that pays an installment, false for the " "reclassification and its reversal.", ) def _post(self, soft=True): """Close the loan once its last installment has been posted. Checking here rather than on the installment keeps the state correct no matter where the posting comes from: the confirm action, a manual post, or the scheduler honouring `auto_post`. """ posted = super()._post(soft=soft) loans = self.env['dn.loan'] for move in self: loan = move.dn_loan_id if not loan or loan.state != 'running': continue skip = loan.skip_until_date if all( line.is_payment_move_posted or (skip and line.date < skip) for line in loan.line_ids ): loans |= loan if loans: loans.write({'state': 'closed'}) for loan in loans: loan.message_post(body=_( "All installments are posted, the loan is now closed.")) return posted def action_open_dn_loan(self): self.ensure_one() return { 'type': 'ir.actions.act_window', 'name': _("Loan"), 'views': [(False, 'form')], 'res_model': 'dn.loan', 'res_id': self.dn_loan_id.id, }