Jay247
Jay247

Reputation: 25

Error creating invoice from custom module (Odoo 13)

I am trying to create an invoice from a custom object but I am getting errors from on validation .When I post, i get the following error: "

ValueError: Wrong value for 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 and current_reading are custom fields i created. I am aware that Odoo automatically creates values for line_ids from invoice_line_ids if line_ids is not provided, so I am really stuck about this error.

Here's my code for creating the invoice:

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'})

Any help given will be appreciated. Thanks

Upvotes: 0

Views: 1139

Answers (1)

If you want to create invoices, in the lines you should not use the debit and credit fields since these are calculated, as it is a product line, you should not use display_type, since the line_section type is treated as an annotation and not as a price calculation line.

In the invoice data, when linking the lines 'invoice_line_ids': inv_line_ids an instruction must be specified to process the lines in your case it would be as follows 'invoice_line_ids': (0, 0, inv_line_ids) for greater information visit this page.

Upvotes: 2

Related Questions