从自定义模块创建发票时出错 (Odoo 13)

Error creating invoice from custom module (Odoo 13)

我正在尝试从自定义对象创建发票,但我在验证时遇到错误。当我 post 时,我收到以下错误:“

ValueError:account.move.line_ids 的错误值:{'display_type':'line_section','name':'Phone Bill','product_id': 11783, 'product_uom_id': 19, 'current_reading': 66.0, 'current_date': datetime.date(2020, 11, 3), 'quantity': 17.0, 'price_unit': 565.0, 'account_id': 19, 'debit': 9605.0, 'credit': 0.0}

current_date 和 current_reading 是我创建的自定义字段。我知道如果未提供 line_ids,Odoo 会自动从 invoice_line_ids 创建 line_ids 的值,所以我真的被这个错误困住了。

这是我创建发票的代码:

class ReadingCorrection(models.TransientModel):
    _name = 'reading.upload.wizard'
    _description = 'Validate reading uploads'
  
    def validate_entry(self):
        active_ids = self._context.get('active_ids', []) or []
        company = self.env.user.company_id
        journal = self.env['account.move'].with_context(force_company=company.id, type='out_invoice')._get_default_journal()
        for reads in self.env['reading.upload'].browse(active_ids):
            if reads.reading >= reads.previous_reading: # and reads.state == 'draft':
                account = reads.product_id.product_tmpl_id._get_product_accounts()['income']
                if not account:
                    raise UserError(_('No account defined for product "%s".') % reads.product_id.name)
                invoice = {
                    'type': 'out_invoice',
                    'invoice_date':reads.read_date,
                    'narration': reads.remark,
                    'invoice_user_id': reads.current_user.id,
                    'partner_id': reads.meter_id.customer_id.id,
                    'journal_id': 1,#journal.id,
                    'currency_id': reads.meter_id.customer_id.currency_id.id,
                    'doc_type': 'bill',
                    'invoice_line_ids':[(0,0, {
                        'name': reads.product_id.name,
                        'product_id': reads.product_id.id,
                        'product_uom_id': reads.product_id.uom_id.id,
                        'current_reading': reads.reading,
                        'previous_reading': reads.previous_reading,
                        'current_date': reads.read_date,
                        'quantity': reads.reading - reads.previous_reading,
                        'price_unit': reads.product_id.product_tmpl_id.lst_price,
                        'account_id': account.id,
                    })]
                }
                moves = self.env['account.move'].with_context(default_type='out_invoice').create(invoice)
                #invoice = self.env['account.move'].sudo().create(invoice)
                reads.write({'state':'uploaded'})

如有任何帮助,我们将不胜感激。谢谢

如果你想创建发票,你不应该在行中使用 debitcredit 字段,因为这些是计算出来的,因为它是一个产品线,你不应该使用 display_type,因为 line_section 类型被视为注释而不是价格计算线。

在发票数据中,链接行时 'invoice_line_ids': inv_line_ids 必须指定一条指令来处理您的情况下的行,如下所示 'invoice_line_ids': (0, 0, inv_line_ids) 如需更多信息,请访问此 page.