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.

0

Sample code for Applying payment to Invoice via XML-RPC (PHP) ?

By
Antoni
on 5/10/13, 3:59 PM 8,253 views

I am using PHP to communicate with the XML-RPC API. I have been able to successfully create and validate an invoice but I am running into a problem applying a payment to an invoice.

Does anyone know how this can be done through the API? Or have a code sample I could reference?

Thanks

Here is the code I am using. It doesnt show the actual xml-rpc calls but it shows the fields. This is using a custom handler I wrote.

echo "LOGIN<BR /><BR />";
$conn = new openerp_client($user, $password, $dbname, $server_url);

//search for company in partner model to get id
echo "SEARCH<br /><br />";
$data = array(
    0 => array('field' => 'name', 'operator' => '=', 'value' => 'Antoni\'s Company 2', 'type' => 'string')
);
$conn->search('res.partner', $data);

$partner_id = $conn->response['last_record_id'][0];

//search for company in res.comapny model to get id
echo "SEARCH<br /><br />";
$data = array(
    0 => array('field' => 'name', 'operator' => '=', 'value' => 'Antoni\'s Company 2', 'type' => 'string')
);
$conn->search('res.company', $data);

$company_id = $conn->response['last_record_id'][0];

//create the invoice
echo "INVOICE<BR /><BR />";

$data = array(
    0 => array('field' => 'partner_id', 'value' => $partner_id, 'type' => 'int'),
    1 => array('field' => 'date_invoice', 'value' => date('Y-m-d'), 'type' => 'string'),
    2 => array('field' => 'date_due', 'value' => date('Y-m-d'), 'type' => 'string'),
    3 => array('field' => 'account_id', 'value' => 40, 'type' => 'int'),
    4 => array('field' => 'journal_id', 'value' => 11, 'type' => 'int'),
    6 => array('field' => 'company_id', 'value' => $company_id, 'type' => 'int'),
    7 => array('field' => 'period_id', 'value' => 4, 'type' => 'int')
);

$conn->put('account.invoice', 'create', $data);
$invoice_id = $conn->response['last_record_id'];

//create the invoice lines
echo "CREATE INVOICE LINES<BR /><BR />";
$data = array(
    0 => array('field' => 'invoice_id', 'value' => $invoice_id, 'type' => 'int'),
    1 => array('field' => 'product_id', 'value' => 49, 'type' => 'int'),
    2 => array('field' => 'name', 'value' => '[ADPT] USB Adapter', 'type' => 'string'),
    3 => array('field' => 'price_unit', 'value' => 18.0, 'type' => 'double'),
    4 => array('field' => 'quantity', 'value' => 2.0, 'type' => 'double'),
    5 => array('field' => 'account_id', 'value' => 51, 'type' => 'int')
);

$conn->put('account.invoice.line', 'create', $data);

//validate the invoice
echo "VALIDATE<BR /><BR />";
$conn->workflow('account.invoice', 'invoice_open', $invoice_id);

//get move_id from invoice
echo "SEARCH INVOICE<BR /><BR />";
$data = array(
    0 => array('field' =>'id', 'operator' => '=', 'value' => $invoice_id, 'type' => 'int')
);
$conn->search('account.invoice', $data);

echo "GET INVOICE<BR /><BR />";
$fields = array();
$conn->get('account.invoice', $fields);

echo $move_id = $conn->response['last_record_id'][0]['move_id']->me['array'][0]->me['int'];

//create voucher
echo "Create<br /><br />";

$data = array(
    0 => array('field' => 'partner_id', 'value' => 166, 'type' => 'int'),
    1 => array('field' => 'company_id', 'value' => 1, 'type' => 'int'),
    2 => array('field' => 'type', 'value' => 'receipt', 'type' => 'string'),
    3 => array('field' => 'account_id', 'value' => 57, 'type' => 'int'),
    4 => array('field' => 'period_id', 'value' => 5, 'type' => 'int'),
    5 => array('field' => 'journal_id', 'value' => 17, 'type' => 'int'),
    6 => array('field' => 'amount', 'value' => 12.0, 'type' => 'double'),
    7 => array('field' => 'move_id', 'value' => $move_id, 'type' => 'int'),
    8 => array('field' => 'currency_id', 'value' => 1, 'type' => 'int'),
    9 => array('field' => 'date', 'value' => date('Y-m-d'), 'type' => 'string')
);
$conn->put('account.voucher', 'create', $data);
$voucher_id = $conn->response['last_record_id'];

//get move_line_id
$data = array(
    0 => array('field' => 'move_id', 'operator' => '=', 'value' => $move_id, 'type' => 'int'),
    1 => array('field' => 'name', 'operator' => '=', 'value' => '/', 'type' => 'string')
);
$conn->search('account.move.line', $data);

$fields = array();
$conn->get('account.move.line', $fields);

$move_line_id = $conn->response['last_record_id'][0]['id']->me['int'];

$invoice_name = $conn->response['last_record_id'][0]['invoice']->me['array'][1]->me['string'];

//create voucher line
echo "Create Voucher Line<br /><br />";
$data = array(
    0 => array('field' => 'partner_id', 'value' => 166, 'type' => 'int'),
    1 => array('field' => 'company_id', 'value' => 1, 'type' => 'int'),
    2 => array('field' => 'voucher_id', 'value' => $voucher_id, 'type' => 'int'),
    3 => array('field' => 'amount', 'value' => 12.0, 'type' => 'double'),
    4 => array('field' => 'type', 'value' => 'cr', 'type' => 'string'),
    5 => array('field' => 'name', 'value' => $invoice_name, 'type' => 'string'),
    6 => array('field' => 'account_id', 'value' => 40, 'type' => 'int'),
    7 => array('field' => 'move_line_id', 'value' => $move_line_id, 'type' => 'int')
);

$conn->put('account.voucher.line','create',$data);

//Post voucher
echo "Post Voucher<BR /><BR />";
$conn->workflow('account.voucher', 'proforma_voucher', $voucher_id);
1

Francesco OpenCode

--Francesco OpenCode--
3608
| 5 7 9
Grottaglie, Italy
--Francesco OpenCode--

Italian Odoo (OpenERP) Modules Developer LINKEDIN: http://www.linkedin.com/in/francescoapruzzese

Francesco OpenCode
On 5/11/13, 5:33 AM

If you want, you can use my script hosted in github: https://github.com/OpenCode/openerp-php-connector

If you read index.php page you can find examples for alla the feature. You must use create to create an invoice and call_function to validate it.

Hi Francesco, Thanks for replying. I have already figured out how to create and invoice and validate one. I am looking to make payments to an invoice and reconcile and invoice. Would you happen to have any code samples on that? Thanks.

Antoni
on 5/13/13, 11:15 AM

A solution is to study the openerp workflow for the invoice and call the some functions with PHP

Francesco OpenCode
on 5/13/13, 11:36 AM

I tried that as well. I did the following: created an invoice, added lines to the invoice, validated the invoice. Then created a voucher with the move_id related to the invoice got the move_line_id related to the invoice/lines. But the payment does not show up in the invoice and the voucher cannot be posted. So I am still missing something. I update the question with the code I have already. It uses a class I wrote but will give you an idea of the fields I am using. Thanks for your help.

Antoni
on 5/13/13, 11:41 AM
0
Nicholas Linn
On 5/16/13, 5:32 PM

I haven't had a chance to try it but I dug this up earlier and bookmarked it:

http://forum.openerp.com/forum/topic32306.html

Hope it helps.

This link is now dead. I've put the essence of the link content into this gist: https://gist.github.com/judgej/38d5bcba3c8f5e3f6fa5

Jason Judge
on 8/29/14, 11:12 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: 5/10/13, 3:59 PM
Seen: 8253 times
Last updated: 3/16/15, 8:10 AM