I am using https://www.npmjs.com/package/odoo-xmlrpc to create invoices in odoo 10. I can create an invoice, but i am unable to add invoice line_items to the invoice.
//connect to odoo
var odoo = new Odoo({
url: "xxx",
port: 443,
db: "xxx",
username: 'apiuser123456',
password: 'xxx'
});
odoo.connect(function (err) {
if (err) { return console.log(err); }
console.log('Connected to Odoo server.');
var inParams = [];
inParams.push({
'partner_id': 119,
'account_id': 162,
'invoice_line': [(0, '', { 'account_id': 162, 'name': "AAA", 'quantity': parseFloat('3'), 'price_unit': parseFloat('5') })]
});
var params = [];
params.push(inParams);
odoo.execute_kw('account.invoice', 'create', params, function (err2, value2) {
if (err2) { return console.log(err2); }
});
});
this code actually creates an invoice in odoo, but it does not add any line. I assume that the invoice_line attribute is somehow wrong but I fail to find the problem. I would have expected to see one line in the invoice with a total amount of 15 Eur.
Finally this is how i solved it:
1- adding the invoice_line_tax_ids as lined out above:
invoiceLines = [
{ 'account_id': 162, 'name': "Product A", 'quantity': 1, 'price_unit': 17, 'product_id': 1 },
{ 'product_id': 1, 'account_id': 162, 'name': "Product B", 'quantity': 1, 'price_unit': 17 }
]
-------------------
// partnerId is an id of an existing partner in odoo
function createInvoiceObj(partnerId, lineEntries) {
inParams = [];
params = [];
inParams.push({
'partner_id': partnerId,
'account_id': 162,
'invoice_line_ids': lineEntries
});
params.push(inParams);
odoo.execute_kw('account.invoice', 'create', params, function (err, invoiceId) {
if (err) { throws('error during execution createInvoiceObj() ' + err); }
return createTax(invoiceId);
});
}
2- create tax entries for each line (find lineIds for invoice first)
function createTax(invoiceId) {
var inParams = [];
var params = [];
inParams.push([['invoice_id', '=', invoiceId]]);
params.push(inParams);
odoo.execute_kw('account.invoice.line', 'search', params, function (err, invoiceLineIds) {
if (err) { return console.log(err); }
invoiceLineId = invoiceLineIds[0];
inParams = [];
inParams.push(invoiceLineIds);
inParams.push({ 'invoice_line_tax_ids': [[6, 0, [12]]] })
params = [];
params.push(inParams);
odoo.execute_kw('account.invoice.line', 'write', params, function (err, value) {
if (err) { return console.log('Error during execution ' + err); }
return compute_taxes(invoiceId)
});
});
}
3- trigger tax calculation on invoice
function compute_taxes(invoiceId) {
inParams = [];
params = [];
inParams.push(invoiceId);
params.push(inParams);
odoo.execute_kw('account.invoice', 'compute_taxes', params, function (err, result) {
if (err) { return console.log(err); }
return invoice_open(invoiceId)
});
}
so the magix fix at the end was to call the compute_taxes function. However, i tried to take the next step and automatically confirm the invoice, but this still fails. I used the invoice_open workflow, but this shows no result:
function invoice_open(invoiceId) {
var params = [];
params.push(invoiceId);
odoo.exec_workflow('account.invoice', 'invoice_open', params, function (err, value) {
if (err) { return console.log("Error during execution " + err); }
console.log('Result invoice_open: ' + value);
});
}
this function always returns false
but with no error message. The invoice state is not transsitioned to "confirmed" either.