This question has been flagged
1 Reply
9174 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({
'amount':amount,
'account_id':invoice.account_id.id,
'partner_id':partner_id,
'type':invoice.type in ('out_invoice','out_refund') and 'receipt' or 'payment',
'journal_id':invoice.journal_id.id
})
for line in invoice.move_id.line_id:
if line.debit > 0.0:
account_voucher.env['account.voucher.line'].create({
'name':invoice.number,
'voucher_id':account_voucher.id,
'move_line_id':line.id,
'amount_unreconciled':abs(line.debit),
'amount_original':abs(line.debit),
'amount':abs(line.debit),
'account_id':invoice.account_id.id,
'partner_id':partner_id,
'type': 'cr',
})
account_voucher.signal_workflow('proforma_voucher')

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 = {}
context_dict.update(invoice._context)
context_dict.update(test['context'])
account_voucher = invoice.env['account.voucher'].with_context(context_dict).new({'amount':amount,'journal_id':self.env['account.journal'].search([('code','=','BNK2')]).id})
account_voucher.button_proforma_voucher()

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!


Avatar
Discard
Best Answer

Got the answer ?

Avatar
Discard