Odoo Help


This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


How to register payments automatically trough python code?

on 9/29/15, 7:40 AM 874 views

Task: Given a csv file with payments, the customer payments have to be registred in odoo. 

I am not using RPC calls, everything happens in an Import Wizard that I have created. The Wizard reads the file and searches for the invoices.

First attempt: After importing the file I have the invoice code and the paid amount for every invoice. I've read the post https://www.odoo.com/forum/help-1/question/how-to-apply-payment-to-invoice-via-xml-rpc-37795. My implementation looked like this:

            invoice = self.env['account.invoice'].search([('number','=',invoice_name)])

partner_id = self.env['res.partner']._find_accounting_partner(invoice.partner_id).id
account_voucher = invoice.env['account.voucher'].create({
'type':invoice.type in ('out_invoice','out_refund') and 'receipt' or 'payment',
for line in invoice.move_id.line_id:
if line.debit > 0.0:
'type': 'cr',

Problem: Although payments are registred, the behaviour is different from the standard payment behaviour. The credit field in the customer accounting tab is not affected which means that the whole process isn't complete. Additionally, I don't need to have so much information for the standard payment operation. If the button Register Payment is pressed in an invoice only the amount and the journal_id are needed. Everything else gets computed by the system.

Is there a way to simulate the standard behaviour. My thought was to get the context from the invoice_pay_customer function, open a new voucher model with this context, set the amount, set the journal_id and to press the button Register Payment. My code:

invoice = self.env['account.invoice'].search([('number','=',invoice_name)])
test = invoice.invoice_pay_customer()
context_dict = {}
account_voucher = invoice.env['account.voucher'].with_context(context_dict).new({'amount':amount,'journal_id':self.env['account.journal'].search([('code','=','BNK2')]).id})

If I use create instead of new then I would need a lot of mandatory fields, which normally should be computed by the system. If I use new then it's not the same as creating a new view. Nothing happens if the button is pressed. The code runs without errors. The context is not available in the new object. (Debug prints in account.voucher only have the standard tz, id, .... context)

Is there a way to improve this thought or should it be done in a different way?

I appreciate any help and feedback!

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)


Asked: 9/29/15, 7:40 AM
Seen: 874 times
Last updated: 10/1/15, 9:01 AM