This question has been flagged
2 Replies
17185 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);
Avatar
Discard
Best Answer

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.

Avatar
Discard
Author

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.

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

Author

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.

Best Answer

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.

Avatar
Discard

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