Odoo Help


How to register payments automatically trough python code?

on 9/29/15, 7:40 AM 2,252 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!

Vysakh Bt
On 3/14/18, 7:59 AM

Got the answer ?

About This Community

This platform 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.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

2 follower(s)


Asked: 9/29/15, 7:40 AM
Seen: 2252 times
Last updated: 3/14/18, 7:59 AM