从自定义模块 odoo 13 创建发票行
create invoice line from custom module odoo 13
我创建了一个自定义模块并创建了一个按钮来创建发票,发票已成功创建,但问题是发票行 ID 只创建一行而忽略另一行。这是我的代码
def create_invoice(self):
invoice = self.env['account.move'].create({
'type': 'out_invoice',
'state': 'draft',
'journal_id': 1,
'partner_id': self.partner_id.id,
'amount_untaxed': self.package_price,
'ref': self.name,
'invoice_origin': self.name,
'invoice_line_ids': [(0, 0, self._prepare_invoice_line())]
})
return invoice
def _prepare_invoice_line(self):
for order in self:
for line in order.additional_services_ids:
res = {
'product_id': line.service_id.id,
'quantity': line.quantity,
'name': 'pro',
'discount': 0,
'price_unit': line.price_unit,
}
return res
Odoo many2many 领域很痛苦。
你的invoice_line_ids
中应该有这种数据结构
[
(0,0,[invoice_line_1]),
(0,0, [invoice_line_2]),
...
]
所以像这样修改你的代码是可行的:
def create_invoice(self):
invoice = self.env['account.move'].create({
'type': 'out_invoice',
'state': 'draft',
'journal_id': 1,
'partner_id': self.partner_id.id,
'amount_untaxed': self.package_price,
'ref': self.name,
'invoice_origin': self.name,
'invoice_line_ids': [(0, 0, line) for line in self._prepare_invoice_line()]
})
return invoice
def _prepare_invoice_line(self):
res = []
for order in self:
for line in order.additional_services_ids:
res.append({
'product_id': line.service_id.id,
'quantity': line.quantity,
'name': 'pro',
'discount': 0,
'price_unit': line.price_unit,
})
return res
我不知道这是一个错误还是设计的问题,但我已经为此苦苦思索了很多次。
--- 编辑 ---
_prepare_invoice_line 方法也有问题。它只返回一行而不是行列表。
要创建多行,只需使用 Odoo
one2many 在 data 上执行 loop 创建数据的语法。
示例代码:
def create_invoice(self):
invoice = self.env['account.move'].create({
'type': 'out_invoice',
'state': 'draft',
'journal_id': 1,
'partner_id': self.partner_id.id,
'amount_untaxed': self.package_price,
'ref': self.name,
'invoice_origin': self.name,
'invoice_line_ids': [(0, 0, {
'product_id': line.service_id.id,
'quantity': line.quantity,
'name': 'pro',
'discount': 0,
'price_unit': line.price_unit
}) for line in self.additional_services_ids] # Multiple Invoice Line Create code here
})
return invoice
(0, 0, { values }) link to a new record that needs to be created with the given values dictionary
(1, ID, { values }) update the linked record with id = ID (write values on it)
(2, ID) remove and delete the linked record with id = ID (calls unlink on ID, that will delete the object completely, and the link to it as well)
(3, ID) cut the link to the linked record with id = ID (delete the relationship between the two objects but does not delete the target object itself)
(4, ID) link to existing record with id = ID (adds a relationship)
(5, 0, 0) unlink all (like using (3, ID) for all linked records)
(6, 0, [IDs]) replace the list of linked IDs (like using (5) then (4, ID) for each ID in the list of IDs)
我创建了一个自定义模块并创建了一个按钮来创建发票,发票已成功创建,但问题是发票行 ID 只创建一行而忽略另一行。这是我的代码
def create_invoice(self):
invoice = self.env['account.move'].create({
'type': 'out_invoice',
'state': 'draft',
'journal_id': 1,
'partner_id': self.partner_id.id,
'amount_untaxed': self.package_price,
'ref': self.name,
'invoice_origin': self.name,
'invoice_line_ids': [(0, 0, self._prepare_invoice_line())]
})
return invoice
def _prepare_invoice_line(self):
for order in self:
for line in order.additional_services_ids:
res = {
'product_id': line.service_id.id,
'quantity': line.quantity,
'name': 'pro',
'discount': 0,
'price_unit': line.price_unit,
}
return res
Odoo many2many 领域很痛苦。
你的invoice_line_ids
中应该有这种数据结构[
(0,0,[invoice_line_1]),
(0,0, [invoice_line_2]),
...
]
所以像这样修改你的代码是可行的:
def create_invoice(self):
invoice = self.env['account.move'].create({
'type': 'out_invoice',
'state': 'draft',
'journal_id': 1,
'partner_id': self.partner_id.id,
'amount_untaxed': self.package_price,
'ref': self.name,
'invoice_origin': self.name,
'invoice_line_ids': [(0, 0, line) for line in self._prepare_invoice_line()]
})
return invoice
def _prepare_invoice_line(self):
res = []
for order in self:
for line in order.additional_services_ids:
res.append({
'product_id': line.service_id.id,
'quantity': line.quantity,
'name': 'pro',
'discount': 0,
'price_unit': line.price_unit,
})
return res
我不知道这是一个错误还是设计的问题,但我已经为此苦苦思索了很多次。
--- 编辑 --- _prepare_invoice_line 方法也有问题。它只返回一行而不是行列表。
要创建多行,只需使用 Odoo
one2many 在 data 上执行 loop 创建数据的语法。
示例代码:
def create_invoice(self):
invoice = self.env['account.move'].create({
'type': 'out_invoice',
'state': 'draft',
'journal_id': 1,
'partner_id': self.partner_id.id,
'amount_untaxed': self.package_price,
'ref': self.name,
'invoice_origin': self.name,
'invoice_line_ids': [(0, 0, {
'product_id': line.service_id.id,
'quantity': line.quantity,
'name': 'pro',
'discount': 0,
'price_unit': line.price_unit
}) for line in self.additional_services_ids] # Multiple Invoice Line Create code here
})
return invoice
(0, 0, { values }) link to a new record that needs to be created with the given values dictionary
(1, ID, { values }) update the linked record with id = ID (write values on it)
(2, ID) remove and delete the linked record with id = ID (calls unlink on ID, that will delete the object completely, and the link to it as well)
(3, ID) cut the link to the linked record with id = ID (delete the relationship between the two objects but does not delete the target object itself)
(4, ID) link to existing record with id = ID (adds a relationship)
(5, 0, 0) unlink all (like using (3, ID) for all linked records)
(6, 0, [IDs]) replace the list of linked IDs (like using (5) then (4, ID) for each ID in the list of IDs)