'How can I create an invoice with invoice lines in odoo

I am trying to create an invoice with an invoice line from a new model when a button is clicked(generate_appraisal_fee button), the invoice creates successfully but without the invoice line and it does not give any error that I can point too. I cannot figure out what is wrong. Here is my code.


    def _prepare_invoice(self):
      
        self.ensure_one()
        
        journal = self.env['account.move'].with_context(default_move_type='out_invoice')._get_default_journal()
        appraisal_acct = self.env.user.company_id.appraisal_account
        if not journal:
            raise UserError(_('Please define an accounting sales journal for the company %s (%s).') % (
            self.env.user.company_id.name, self.env.user.company_id.id))
        
        invoice_vals = {
            'ref': self.application_id or '',
            'move_type': 'out_invoice',
            'narration': self.appraisal_comment,
           
            'user_id': self.env.user.id,
            'invoice_user_id': self.env.user.id,
            
            'partner_id': self.customer.id,
            
            'partner_bank_id': self.env.user.company_id.partner_id.bank_ids[:1].id,
            'journal_id': journal.id,  # company comes from the journal
            'invoice_origin': self.name,
          
            'payment_reference': self.application_id,
            
            'invoice_line_ids': [((0, 0, {
                # 'product_id': 15,
                'name': 'Appraisal Fee',
                'account_id': appraisal_acct.id,
                'quantity': 1.0,
                'price_unit': self.appraisal_amount,
                'analytic_account_id': False,
                'partner_id': self.customer.id,
              }))],
            'company_id': self.env.user.company_id.id,
            'investment_id': self.id
        }
        return invoice_vals

def generate_appraisal_fee(self):
      _logger.info("Generate Appraisal fee")
      appraisal_acct = self.env.user.company_id.appraisal_account
      _logger.info("Appraisal Account set on config: %s", appraisal_acct.name)
      inv_vals = []
      inv_vals_lines = []
      for rec in self:
        if not rec.appraisal_amount:
          raise UserError(_("Please fill in an Appraisal amount for this record!"))
        inv_vals_list = rec._prepare_invoice()
  
        inv_vals.append(inv_vals_list)

      _logger.info("Inv vals: %s", inv_vals)

      moves = self.env['account.move'].sudo().with_context(default_move_type='out_invoice').create(inv_vals)
      if moves:
        _logger.info("Moves Created")
       
      return moves

I don't know what I am not getting right as everything seems to be in place and I do not get any errors



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source