如何通过节点中的odoo-xmlrpc在odoo中创建发票?
How to create an invoice in odoo via odoo-xmlrpc in node?
我正在使用 https://www.npmjs.com/package/odoo-xmlrpc 在 odoo 10 中创建发票。我可以创建发票,但我无法将发票 line_items 添加到发票中。
//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); }
});
});
此代码实际上在 odoo 中创建了一个发票,但它没有添加任何行。我假设 invoice_line 属性在某种程度上是错误的,但我没有找到问题所在。我本以为会在发票中看到一行,总金额为 15 欧元。
One2many 和 Many2many 字段可以用魔法三元组或更好的那些三元组的列表来设置。由于您在这里使用 JavaScript,我建议使用列表三元组 -> [MagicNumber, ID or nothing, List of IDs, nothing or values dictionary]
所以发票行应该是
'invoice_line_ids': [[0, 0, {'field1': value1, 'fieldn': valuen}],
[0, 0, {'field1': value1, 'fieldn': valuen}]]
这将添加两个全新创建的发票行。
Many2many 字段也是如此,例如发票行的税收
'invoice_line_tax_ids': [[6, 0, [12]]]
这会将 ID 为 12 的税添加到 many2many 关系中。只需提供多个 ID 即可添加更多关系。
小提示:那些神奇的三元组可以在官方文档中找到,也可以直接在 BaseModel.write()
上的文档字符串代码中找到
最后我是这样解决的:
1- 添加上面列出的 invoice_line_tax_ids:
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- 为每一行创建税务条目(首先找到发票的 lineIds)
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- 触发发票税金计算
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)
});
}
所以最后的 magix 修复是调用 compute_taxes 函数。但是,我尝试采取下一步并自动确认发票,但这仍然失败。我使用了 invoice_open 工作流程,但这显示没有结果:
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);
});
}
此函数总是 returns false
但没有错误消息。发票状态也未转换为 "confirmed"。
我正在使用 https://www.npmjs.com/package/odoo-xmlrpc 在 odoo 10 中创建发票。我可以创建发票,但我无法将发票 line_items 添加到发票中。
//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); }
});
});
此代码实际上在 odoo 中创建了一个发票,但它没有添加任何行。我假设 invoice_line 属性在某种程度上是错误的,但我没有找到问题所在。我本以为会在发票中看到一行,总金额为 15 欧元。
One2many 和 Many2many 字段可以用魔法三元组或更好的那些三元组的列表来设置。由于您在这里使用 JavaScript,我建议使用列表三元组 -> [MagicNumber, ID or nothing, List of IDs, nothing or values dictionary]
所以发票行应该是
'invoice_line_ids': [[0, 0, {'field1': value1, 'fieldn': valuen}],
[0, 0, {'field1': value1, 'fieldn': valuen}]]
这将添加两个全新创建的发票行。
Many2many 字段也是如此,例如发票行的税收
'invoice_line_tax_ids': [[6, 0, [12]]]
这会将 ID 为 12 的税添加到 many2many 关系中。只需提供多个 ID 即可添加更多关系。
小提示:那些神奇的三元组可以在官方文档中找到,也可以直接在 BaseModel.write()
最后我是这样解决的:
1- 添加上面列出的 invoice_line_tax_ids:
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- 为每一行创建税务条目(首先找到发票的 lineIds)
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- 触发发票税金计算
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)
});
}
所以最后的 magix 修复是调用 compute_taxes 函数。但是,我尝试采取下一步并自动确认发票,但这仍然失败。我使用了 invoice_open 工作流程,但这显示没有结果:
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);
});
}
此函数总是 returns false
但没有错误消息。发票状态也未转换为 "confirmed"。