From 507bcbe0482f33c6305549b554463ab6d45cdc17 Mon Sep 17 00:00:00 2001 From: stephanchrst Date: Fri, 31 May 2024 09:30:19 +0700 Subject: add paid invoice api --- fixco_api/models/invoice.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 fixco_api/models/invoice.py (limited to 'fixco_api/models/invoice.py') diff --git a/fixco_api/models/invoice.py b/fixco_api/models/invoice.py new file mode 100644 index 0000000..354d3ea --- /dev/null +++ b/fixco_api/models/invoice.py @@ -0,0 +1,29 @@ +from odoo import models + + +class Invoice(models.Model): + _inherit = 'account.move' + + def _register_payment_automatically(self): + payment_model = self.env['account.payment'] + + for invoice in self: + if invoice.state == 'posted' and invoice.amount_residual > 0: + payment_vals = { + 'payment_type': 'inbound', + 'partner_type': 'customer', + 'partner_id': invoice.partner_id.id, + 'amount': invoice.amount_residual, + 'date': invoice.invoice_date, + 'journal_id': self.env['account.journal'].search([('type', '=', 'bank')], limit=1).id, + 'payment_method_id': self.env.ref('account.account_payment_method_manual_in').id, + 'ref': invoice.name, + # 'communication': invoice.name, + } + payment = payment_model.create(payment_vals) + payment.action_post() + + # Reconcile payment with invoice + (invoice.line_ids + payment.move_id.line_ids) \ + .filtered(lambda line: line.account_id.reconcile) \ + .reconcile() -- cgit v1.2.3