odooodoo-17

Odoo 17: Create invoice for sales order via API (the right way)


I have created an Odoo sales order with the following payload (for test reasons I use the same Odoo product id for all order lines) and the sale.order model:

{
    "company_id": 1,
    "name": "10000",
    "partner_id": 17,
    "partner_invoice_id": 22,
    "partner_shipping_id": 23,
    "client_order_ref": "10000",
    "carrier_id": 2,
    "order_line":
    [
        [
            0,
            0,
            {
                "product_id": 414,
                "display_name": "Product #2",
                "name_short": "Product #2",
                "name": "Product #2",
                "product_uom_qty": 1,
                "price_unit": 103.36,
                "purchase_price": 40,
                "price_tax": 19.64
            }
        ],
        [
            0,
            0,
            {
                "product_id": 414,
                "display_name": "Demo Product with Variants",
                "name_short": "Demo Product with Variants",
                "name": "Demo Product with Variants",
                "product_uom_qty": 1,
                "price_unit": 84.03,
                "purchase_price": 40,
                "price_tax": 15.97
            }
        ],
        [
            0,
            0,
            {
                "product_id": 414,
                "display_name": "Shipping Costs",
                "name_short": "Shipping Costs",
                "name": "Shipping Costs",
                "product_uom_qty": 1,
                "price_unit": 8.4,
                "purchase_price": 40,
                "price_tax": 0
            }
        ]
    ]
}

After this I tried to create the invoice for my sales order, but the method action_invoice_generate seems depricated. I then proceeded to create an invoice manually with the following payload and the account.move model. The invoice_origin is from my created sales order.

{
    "partner_id": 22,
    "move_type": "out_invoice",
    "invoice_date": "2024-04-19",
    "invoice_line_ids": [...],
    "invoice_origin": "10000"
}

Now I have created the invoice and in my invoice the sales order is referenced.

When I reopen my sales order the button Create Invoice is still available and the invoice is not shown.

What is the best way to create an invoice for my sales order via API?

Idea: I could add the id of my manually created invoice to the invoice_ids list in the sale.order model and set the invoice_status manually, but whats with the fields invoice_count, amount_invoiced etc.?


Solution

  • At first: the button is shown on base of the invoice status, which is computed by some "complex" code, depending how many apps/modules you've installed. So in the end it depends very much on the product settings.

    And secondly you could use sale.order._create_invoices(), which is creating invoices for orders. It will only create invoices if there is something to invoice, which is dependent on the product settings and other factors.

    The mentioned method should not be useable by the XML or JSON RPC interface. But you could easily and should cautiously make it available by calling it from a interface useable method, implemented by yourself.