summaryrefslogtreecommitdiff
path: root/fixco_api/models/invoice.py
blob: 354d3ea28cdd96f7ca2138a877752b18adf8cea4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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()