简体   繁体   中英

Odoo creating an invoice with Web Service API (PHP)

I am trying to create an invoice on Odoo using Odoo Web Service API (via PHP) wich 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 it\n- creation/update: a mandatory field is not correctly set\n\n[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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM