Odoo Help

Welcome!

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.

2

How to apply payment to Invoice via XML-RPC?

By
Carlos Castillo
on 11/28/13, 5:10 AM 8,486 views

I'm trying to import invoices from our website into OpenERP via XML-RPC and PHP.

I managed to create an invoice an validate it. The problem comes when applying payment to the invoice: I'm not sure what's the proper way to "pay" that invoice.

The steps I've followed till now are:

  1. Create invoice
  2. Create invoice lines
  3. Open Invoice
  4. Create Voucher related to Invoice's move_id
  5. Create Voucher line

The entries are correctly created, however the payment stays in 'draft' state and never shows up at the invoice.

I've seen that the next step in the account.invoice workflow is "Paid", but I can't make it work. The requirement for this transition is "test_paid()" and the trigger model is account.move.line, but I don't know exactly how to trigger this action.

I've also tried with account.voucher workflow, but "proforma_voucher" action has no effect on the draft voucher

Any ideas about what can be wrong? Am I missing something?

Thanks

2
Carlos Castillo
On 12/10/13, 6:51 AM

Thank you Boniface, that's the solution.

I figured it out yesterday at the end, the problem i had was that the account used in voucher didn't match with the default account of the Journal.

So, at the end, the workflow i used works like this (just insert and pay one invoice, if you want to pay all the partner's open invoices can be done the way Boniface said):

  1. Create invoice
  2. Create invoice lines
  3. Open Invoice (account.invoice workflow, action: invoice_open)
  4. Opening invoice will create an account.move related with the invoice
  5. Create Voucher specifying amount of the payment (matching invoice's amount), journal, account and period
  6. Create Voucher line with "move_line_id" matching Invoice's account.move.line Id
  7. Post payment (via account.voucher workflow, action: proforma_voucher)

In my case, this worked perfectly.

Hope this will help

I have tried for days this and had no success in registering the payment. i opened this threat http://help.openerp.com/question/44048/register-payment-using-xmlrpc/

Vlad Janicek
on 2/16/14, 8:24 PM
1
Aron Lorinc
On 11/23/15, 11:26 AM

To add a hopefully "it just works" solution:

if not invoice.state == "open":
return
payable_amount = ... # The amount you want to pay
voucher = self.env["account.voucher"].create({
"name": "",
"amount": payable_amount,
"journal_id": self.env["account.journal"].search([("type", "=", "bank")], limit=1).id,
"account_id": invoice.partner_id.property_account_receivable.id,
"period_id": self.env["account.voucher"]._get_period(),
"partner_id": invoice.partner_id.id,
"type": "receipt"
})
voucher_line = self.env["account.voucher.line"].create({
"name": "",
"payment_option": "without_writeoff",
"amount": payable_amount,
"voucher_id": voucher.id,
"partner_id": invoice.partner_id.id,
"account_id": invoice.partner_id.property_account_receivable.id,
"type": "cr",
"move_line_id": invoice.move_id.line_id[0].id,
})
voucher.signal_workflow("proforma_voucher")
1
Ferdinand Gassauer
On 8/18/14, 1:32 AM

I suggest to create a python function (module) which is called via XMLRPC  - XMLRPC is much to slow

create and open invoice
create and post payment move
reconcile
        for inv in invoice_ids: 
            invoice_id = inv.id
            wf_service.trg_validate(uid, 'account.invoice', invoice_id, 'invoice_open', cr)
            for inv_open in invoice_obj.browse(cr, uid, [invoice_id] , context):
                invoices.append(inv_open.number)
                # payment
                journal_id = journal_obj.search(cr, uid, [('code','=', order_vals['pay_method'])])[0]
                journal = journal_obj.browse(cr, uid, [journal_id], context)[0]
                move_val = {
                    'partner_id' : inv_open.partner_id.id,
                    'date'       : inv_open.date_invoice,
                    'period_id'  : inv_open.period_id.id,
                    'journal_id' : journal_id,
                    'ref' : order_vals['pay_ref']
                }
                move_id =  move_obj.create(cr, uid, move_val, context)
                line_val = {
                    'account_id' : inv_open.account_id.id,
                    'credit'     : inv_open.residual,
                    'move_id'    : move_id,
                    'name'       : order_vals['pay_ref']
                }  
                line_val.update(move_val)
                line_id = move_line_obj.create(cr, uid, line_val, context)
                reconcile_lines = [line_id]
                line_val = {
                    'account_id' : journal.default_debit_account_id.id,
                    'debit'      : inv_open.residual,
                    'move_id'    : move_id,
                    'name'       : order_vals['pay_ref']
                }  
                line_val.update(move_val)
                line_id = move_line_obj.create(cr, uid, line_val, context)
                move_obj.button_validate(cr, uid, [move_id], context=context)

                inv_move_line_id = move_line_obj.search(cr, uid, [('move_id','=',inv_open.move_id.id), ('account_id','=', inv_open.account_id.id) ])[0]
                reconcile_lines.append(inv_move_line_id)
                move_line_obj.reconcile(cr, uid, reconcile_lines)

                invoice_obj.invoice_print(cr, uid, [inv_open.id], 'account.invoice', context)

This worked for me, Thanks.. Just need to change 3 things i have changed below for invoice_id in invoice_ids: # invoice_id = inv.id wf_service.trg_validate(uid, 'account.invoice', invoice_id, 'invoice_open', cr) for inv_open in invoice_obj.browse(cr, uid, [invoice_id] , context): #invoices.append(inv_open.number) # payment journal_id = journal_obj.search(cr, uid, [('code','=', order_vals['pay_method'])])[0]

Bino
on 10/27/15, 10:09 AM
0
Alkivi SAS
On 5/2/14, 12:31 PM

Hi guys,

I've been facing the same issue here, and here is the code that is currently working for us. Any feedback are appreciated :) Transaction object is a custom one, but contains the journal_id related to the bank account and an amount.

    invoice = self.browse(cr, uid, ids[0], context=context)
    move = invoice.move_id

    # First part, create voucher
    account = transaction.journal_id.default_credit_account_id or transaction.journal_id.default_debit_account_id
    period_id = self.pool.get('account.voucher')._get_period(cr, uid)
    partner_id = self.pool.get('res.partner')._find_accounting_partner(invoice.partner_id).id,

    voucher_data = {
        'partner_id': partner_id,
        'amount': abs(transaction.amount),
        'journal_id': transaction.journal_id.id,
        'period_id': period_id,
        'account_id': account.id,
        'type': invoice.type in ('out_invoice','out_refund') and 'receipt' or 'payment',
        'reference' : invoice.name,
    }

    _logger.debug('voucher_data')
    _logger.debug(voucher_data)

    voucher_id = self.pool.get('account.voucher').create(cr, uid, voucher_data, context=context)
    _logger.debug('test')
    _logger.debug(voucher_id)

    # Equivalent to workflow proform
    self.pool.get('account.voucher').write(cr, uid, [voucher_id], {'state':'draft'}, context=context)

    # Need to create basic account.voucher.line according to the type of invoice need to check stuff ...
    double_check = 0
    for move_line in invoice.move_id.line_id:
        # According to invoice type
        if invoice.type in ('out_invoice','out_refund'):
            if move_line.debit > 0.0:
                line_data = {
                    'name': invoice.number,
                    'voucher_id' : voucher_id,
                    'move_line_id' : move_line.id,
                    'account_id' : invoice.account_id.id,
                    'partner_id' : partner_id,
                    'amount_unreconciled': abs(move_line.debit),
                    'amount_original': abs(move_line.debit),
                    'amount': abs(move_line.debit),
                    'type': 'cr',
                }
                _logger.debug('line_data')
                _logger.debug(line_data)

                line_id = self.pool.get('account.voucher.line').create(cr, uid, line_data, context=context)
                double_check += 1
        else:
            if move_line.credit > 0.0:
                line_data = {
                    'name': invoice.number,
                    'voucher_id' : voucher_id,
                    'move_line_id' : move_line.id,
                    'account_id' : invoice.account_id.id,
                    'partner_id' : partner_id,
                    'amount_unreconciled': abs(move_line.credit),
                    'amount_original': abs(move_line.credit),
                    'amount': abs(move_line.credit),
                    'type': 'dr',
                }
                _logger.debug('line_data')
                _logger.debug(line_data)

                line_id = self.pool.get('account.voucher.line').create(cr, uid, line_data, context=context)
                double_check += 1

    # Cautious check to see if we did ok
    if double_check == 0:
        _logger.warning(invoice)
        _logger.warning(voucher_id)
        raise osv.except_osv(_("Warning"), _("I did not create any voucher line"))
    elif double_check > 1:
        _logger.warning(invoice)
        _logger.warning(voucher_id)
        raise osv.except_osv(_("Warning"), _("I created multiple voucher line ??"))


    # Where the magic happen
    self.pool.get('account.voucher').button_proforma_voucher(cr, uid, [voucher_id], context=context)
0
Jaakko Komulainen
On 11/28/13, 6:50 AM

You need to reconcile the invoice from the voucher view. If the voucher has same partner as open invoice, it should offer you to reconcile the payment amount from that partners open invoice(s). After fully reconciled, the invoice moves to paid state and a journal entry is generated.

Hope this helps.

Thanks for the answer. The problem is that i need to do everything via XML-RPC, How could I reconcile the invoice that way?

Carlos Castillo
on 11/28/13, 8:26 AM

I haven't looked into automatic reconciliation myself, but I guess you could try to use the openerp's reconcile tool at accounting/periodic processing/reconciliation/automatic reconciliation via XML-RPC.

Jaakko Komulainen
on 11/28/13, 8:56 AM

I figured out how to solve this. I had a client request to integrate openerp with another php based payment platform. Please check this answer http://stackoverflow.com/questions/16238044/openerp-7-api-invoice-validation-and-payment/20471961#20471961

hope this helps you.

Boniface Irungu
on 12/9/13, 10:03 AM

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

3 follower(s)

Stats

Asked: 11/28/13, 5:10 AM
Seen: 8486 times
Last updated: 11/23/15, 11:26 AM