Search code examples
odooodoo-11

How to register a payment to an invoice in Odoo 11 via XML RPC


I'm using the Odoo External API (in python: odoorpc) to facilitate communication between a customer app (used to place orders) and my Odoo Online instance. I'm currently not able to register a payment using XML RPC.

I have successfully created sales orders and I can create and validate invoices for them. The last step is to register a payment (which are handled completely outside of Odoo, so I just have to register them for accounting purposes), but I can't get it to work.

create invoice

x = odoo.execute_kw('sale.order', 'action_invoice_create', [[sales_order]], {'context': {'active_ids': sales_order}})[0]

validate invoice

odoo.execute_kw('account.invoice', 'action_invoice_open', [[x]], {})

register payment (i found this code in another topic)

mod = odoo.env['account.payment']
id = mod.create({'amount': 32, 'payment_date': '2018-12-25 00:00:01', 'payment_type': 'inbound', 'payment_method_id': 2, 'journal_id': 8, 'currency_id': 1, 'partner_id': 853} )
mod.browse(id).invoice_ids = [x]
mod.browse(id).post()

register payment second attempt:

odoo.execute_kw('account.payment', 'action_validate_invoice_payment', [[288],{
        "active_id":x,
        "active_ids":[x],
        "active_model": "account.invoice",
        "default_invoice_ids":[x],
        "journal_type":"sale",
        "search_disable_custom_filters": True,
        "type": "out_invoice",
        "tz": False
    }])

I'm getting this error on both attempts to register my payment.

RPCError: local variable 'sequence_code' referenced before assignment


Solution

  • The error states, that a variable isn't set properly: The code can be found here

            if rec.payment_type == 'transfer':
                sequence_code = 'account.payment.transfer'
            else:
                if rec.partner_type == 'customer':
                    if rec.payment_type == 'inbound':
                        sequence_code = 'account.payment.customer.invoice'
                    if rec.payment_type == 'outbound':
                        sequence_code = 'account.payment.customer.refund'
                if rec.partner_type == 'supplier':
                    if rec.payment_type == 'inbound':
                        sequence_code = 'account.payment.supplier.refund'
                    if rec.payment_type == 'outbound':
                        sequence_code = 'account.payment.supplier.invoice'
            rec.name = self.env['ir.sequence'].with_context(ir_sequence_date=rec.payment_date).next_by_code(sequence_code)
    

    rec is the payment you've created with mod.create() and the error belongs to the call of post(). You've forgotten to set a partner_type for the payment, which should be customer in your case.

    Maybe there will be more errors, but your specific problem should be solved by setting partner_type.