'How to create an Invoice and invoice lines via code - Odoo15

Good day, I am tiring to create an invoice with a custom module, the below code gives me an error.

....... invoice = self.with_context(pricelist_context).create_invoice(product_data,inv_data) Exception

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File "C:\Program Files\Odoo\15\server\odoo\http.py", line 644, in _handle_exception return super(JsonRequest, self)._handle_exception(exception) File "C:\Program Files\Odoo\15\server\odoo\http.py", line 302, in _handle_exception raise exception.with_traceback(None) from new_cause TypeError: create_invoice() takes 1 positional argument but 3 were given

def create_invoice(self):
    
        product_id = self.product_id
        
        product_data = [{'product_id': product_id}]
        if self.medical_aid_company_id:
            price = product_id._get_medical_aid_price(self.medical_aid_company_id)
            if price:
                product_data[0]['price_unit'] = price

        for rec in self.billable_line_ids:
            product_data.append({
                'product_id': rec.product_id,
                'quantity': rec.qty,
            })
        inv_data = {
            'consultation_id': self.id,
            'partner': self.patient_id,
        }

        pricelist_context = {}
        if self.pricelist_id:
            pricelist_context = {'set_pricelist_id': self.pricelist_id.id}
        invoice = self.with_context(pricelist_context).create_invoice(product_data,inv_data)
        self.invoice_id = invoice.id


Sources

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

Source: Stack Overflow

Solution Source