Odoo creating an invoice with Web Service API (PHP)

Question:

I am trying to create an invoice on Odoo using Odoo Web Service API (via PHP) which is based on XML-RPC using the code example in the official API documentation. I have succeeded to create a customer using the code provided in the documentation :

$id = $models->execute_kw($db, $uid, $password,
    'res.partner', 'create',
    array(array('name'=>"New Partner"))
);

But I can’t create an invoice using the code sample provided:

$client = $models->execute_kw(
    $db, $uid, $password,
    'res.partner', 'search_read',
    array(array(array('customer', '=', true))),
    array(
        'limit' => 1,
        'fields' => array(
            'property_account_receivable_id',
            'property_payment_term_id',
            'property_account_position_id'
        )))[0];
$invoice_id = $models->execute_kw(
    $db, $uid, $password,
    'account.invoice', 'create', array(array(
        'partner_id' => $client['id'],
        'account_id' => $client['property_account_receivable_id'][0],
        'invoice_line_ids' => array(array(0, false, array('name' => "AAA")))
    )));

when I run the code I get an error response:

array(2) {
  ["faultCode"]=>
  int(1)
  ["faultString"]=>
  string(989) "Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/odoo/service/wsgi_server.py", line 56, in xmlrpc_return
    result = odoo.http.dispatch_rpc(service, method, params)
  File "/usr/lib/python2.7/dist-packages/odoo/http.py", line 118, in dispatch_rpc
    result = dispatch(method, params)
  File "/usr/lib/python2.7/dist-packages/odoo/service/model.py", line 38, in dispatch
    res = fn(db, uid, *params)
  File "/usr/lib/python2.7/dist-packages/odoo/service/model.py", line 157, in execute_kw
    return execute(db, uid, obj, method, *args, **kw or {})
  File "/usr/lib/python2.7/dist-packages/odoo/service/model.py", line 143, in wrapper
    raise ValidationError(msg)
ValidationError: ('The operation cannot be completed, probably due to the following:n- deletion: you may be trying to delete a record while other records still reference itn- creation/update: a mandatory field is not correctly setnn[object with reference: price_unit - price.unit]', None)
"
}

Though it creates the invoice if I change this line into an empty array:

'invoice_line_ids' => array(array(0, false, array('name' => "AAA")))

But then I don’t know how to add the invoice lines.

Asked By: Badr

||

Answers:

For invoice lines(account.invoice.line) there are many required fields like name(description), qty etc.In your example you have passed only name.

Try passing all required fields.

Answered By: Amal
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.