简体   繁体   中英

Create an odoo order and invoice it via external API

I'm trying to integrate an third party application with odoo, the application should create the order and confirm it, to generate the invoice, the order creation works perfectly, when executing the create_invoices nothing shows and no errors appears.


require_once('ripcord/ripcord.php');

$url = "http://127.0.0.1:8069";
$db = "odoo";
$password = "";
$username = "";

$common = ripcord::client("$url/xmlrpc/2/common");
$uid = $common->authenticate($db, $username, $password, array());
$models = ripcord::client("$url/xmlrpc/2/object");



$order_data = array(
    'partner_id' => 12, // Customer ID 
    'date_order' => date('Y-m-d'), // Order Date 
    // 'pricelist_id' => 1, // Price List ID 
    'state' => 'draft', // Order Status (draft) 

    'order_line' => array(array(0, 0, array( // Order Line Items 

        'product_id' => 2, // Product ID 
        'name' => 'Product Name', // Product Name  
        'product_uom_qty' => 5.00, // Quantity of Product  
        'price_unit' => 10.00, // Unit Price of Product  

    )))
);

$order_id = $models->execute_kw($db, $uid, $password, 'sale.order', 'create', array($order_data));

// Confirm Order and Create Invoice

if ($order_id) {

    $confirmOrder = $models->execute_kw($db, $uid, $password, 'sale.order', 'action_confirm', array($order_id));

    if ($confirmOrder) {

        $invoiceCreate = $models->execute_kw(
            $db,
            $uid,
            $password,
            'sale.advance.payment.inv',
            'create_invoices',
            array(
                'sale_order_ids' =>
                array(
                    0 =>
                    array(
                        0 => 6,
                        1 => false,
                        2 =>
                        array(
                            0 => $order_id,
                        ),
                    ),
                ),
                'advance_payment_method' => 'delivered',
                'deduct_down_payments' => true,
                'product_id' => false,
                'fixed_amount' => 0,
                'amount' => 0,
                'deposit_account_id' => false,
                'deposit_taxes_id' =>
                array(
                    0 =>
                    array(
                        0 => 6,
                        1 => false,
                        2 => array(),
                    ),
                ),
            )
            
            ,
            array(
                "context" => array(
                    'active_model' => "sale.order",
                    "allowed_company_ids" => array(1),
                    'active_id' => $order_id, "active_ids" => array($order_id)
                )
            )
        );

        if ($invoiceCreate) {
            
            echo "Order and Invoice #$invoiceCreate for Order #$order_id created successfully!\n";
        } else {
            echo "Error creating invoice!";
        }
    } else {
        echo "Error confirming order!";
    }
} else {
    echo "Error creating order!";
}

When calling create_invoices , Odoo will consider the first parameter as ids

You can change the log_level to debug to the function call details:

DEBUG demo16 odoo.api: call sale.advance.payment.inv('s', 'a', 'l', 'e', '_', 'o', 'r', 'd', 'e', 'r', '_', 'i', 'd', 's').create_invoices('advance_payment_method', 'deduct_down_payments', 'product_id', 'fixed_amount', 'amount', 'deposit_account_id', 'deposit_taxes_id')

To fix the issue you can call the create function to get a wizard record id then call the create_invoices

Example:

$wizard_ids = $models->execute_kw(
    $db,
    $uid,
    $password,
    'sale.advance.payment.inv',
    'create',
    array(array(
        'sale_order_ids' => array($order_id),
    )),
    array(
        "context" => array(
            'active_model' => "sale.order",
            "allowed_company_ids" => array(1),
            'active_id' => $order_id, "active_ids" => array($order_id)
        )
    )
);
    
$invoiceCreate = $models->execute_kw($db, $uid, $password, 'sale.advance.payment.inv', 'create_invoices', array($wizard_ids));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM