<?php

$instance_url = "http://{site_url}/rest/v11";
$username = "admin";
$password = "password";

//Login - POST /oauth2/token
$auth_url = $instance_url . "/oauth2/token";

$oauth2_token_arguments = array(
    "grant_type" => "password",
    //client id - default is sugar.
    //It is recommended to create your own in Admin > OAuth Keys
    "client_id" => "sugar",
    "client_secret" => "",
    "username" => $username,
    "password" => $password,
    //platform type - default is base.
    //It is recommend to change the platform to a custom name such as "custom_api" to avoid authentication conflicts.
    "platform" => "base"
);

$auth_request = curl_init($auth_url);
curl_setopt($auth_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($auth_request, CURLOPT_HEADER, false);
curl_setopt($auth_request, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($auth_request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($auth_request, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($auth_request, CURLOPT_HTTPHEADER, array(
    "Content-Type: application/json"
));

//convert arguments to json
$json_arguments = json_encode($oauth2_token_arguments);
curl_setopt($auth_request, CURLOPT_POSTFIELDS, $json_arguments);

//execute request
$oauth2_token_response = curl_exec($auth_request);

//decode oauth2 response to get token
$oauth2_token_response_obj = json_decode($oauth2_token_response);
$oauth_token = $oauth2_token_response_obj->access_token;


//Create Records - POST /<module>
$url = $instance_url . "/Quotes";
//Set up the Record details
$DateTime = new DateTime();
//Expected Close Date in 1 Month
$DateTime->add(new DateInterval("P1M"));
//Quote Record
$quote = array(
    'name' => 'Test Quote',
    'quote_stage' => 'Draft',
    'date_quote_expected_closed' => $DateTime->format(DateTime::ISO8601),
    //Create Product Bundles
    'product_bundles' => array(
        'create' => array(
            array(
                "name" => "Product Bundle 1",
                "bundle_stage" => "Draft",
                "currency_id" => ":-99",
                "base_rate" => "1.0",
                "shipping" => "0.00",
                "products" => array(
                    //Create Product in Bundle 1
                    "create" => array(
                        array(
                            "tax_class" => "Taxable",
                            "quantity" => 1000.00,
                            "name" => "Test Product 1",
                            "description" => "My Test Product",
                            "mft_part_num" => "mft100012021",
                            "cost_price" =>  "100.00",
                            "list_price" =>  "200.00",
                            "discount_price" =>  "175.00",
                            "discount_amount" =>  "0.00",
                            "discount_select" =>  0,
                            "product_template_id" =>  "",
                            "type_id"  =>  "",
                            "status"  =>  "Quotes"
                        )
                    )
                ),
                //Create Product Bundle Note in Bundle 1
                "product_bundle_notes" => array(
                    "create" => array(
                        array(
                            "description" => "Free shipping",
                            "position" => 1
                        )
                    )
                )
            ),
            array(
                "name" => "Product Bundle 2",
                "bundle_stage" => "Draft",
                "currency_id" => ":-99",
                "base_rate" => "1.0",
                "shipping" => "25.00",
                "products" => array(
                    //Create Products in Bundle 2
                    "create" => array(
                        array(
                            "quantity" => 1000.00,
                            "name" => "Test Product 2",
                            "description" => "My Other Product",
                            "mft_part_num" => "mft100012234",
                            "cost_price" =>  "150.00",
                            "list_price" =>  "300.00",
                            "discount_price" =>  "275.00",
                            "discount_amount" =>  "0.00",
                            "discount_select" =>  0,
                            "product_template_id" =>  "",
                            "type_id"  =>  "",
                            "status"  =>  "Quotes"
                        ),
                        array(
                            "quantity" => 500.00,
                            "name" => "Test Product 3",
                            "description" => "My Other Other Product",
                            "mft_part_num" => "mft100012123",
                            "cost_price" =>  "10.00",
                            "list_price" =>  "500.00",
                            "discount_price" =>  "400.00",
                            "discount_amount" =>  "0.00",
                            "discount_select" =>  0,
                            "product_template_id" =>  "",
                            "type_id"  =>  "",
                            "status"  =>  "Quotes"
                        )
                    )
                )
            )
        ),
    )
);

$curl_request = curl_init($url);
curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($curl_request, CURLOPT_HEADER, false);
curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($curl_request, CURLOPT_HTTPHEADER, array(
    "Content-Type: application/json",
    "oauth-token: {$oauth_token}"
));

//convert arguments to json
$json_arguments = json_encode($quote);
curl_setopt($curl_request, CURLOPT_POSTFIELDS, $json_arguments);
//execute request
$curl_response = curl_exec($curl_request);
//decode json
$createdQuote = json_decode($curl_response);

//display the created record
print_r($createdQuote);
curl_close($curl_request);

//Get Related ProductBundles
$url = $instance_url."/Quotes/".$createdQuote->id."/link/product_bundles";

$curl_request = curl_init($url);
curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($curl_request, CURLOPT_HEADER, false);
curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($curl_request, CURLOPT_HTTPHEADER, array(
    "Content-Type: application/json",
    "oauth-token: {$oauth_token}"
));

//execute request
$curl_response = curl_exec($curl_request);
//decode json
$relatedBundles = json_decode($curl_response,true);

//display the related bundles
print_r($relatedBundles['records']);
curl_close($curl_request);


//Create a new ProductBundle
$url = $instance_url . "/ProductBundles";
$productBundle = array(
    "name" => "Product Bundle 3",
    "bundle_stage" => "Draft",
    "currency_id" => ":-99",
    "base_rate" => "1.0",
    "shipping" => "0.00",
    "products" => array(
        //Create Product in Bundle 3
        "create" => array(
            array(
                "tax_class" => "Taxable",
                "quantity" => 100.00,
                "name" => "Test Product 3",
                "description" => "Test Product 3",
                "mft_part_num" => "mft100012021",
                "cost_price" =>  "100.00",
                "list_price" =>  "250.00",
                "discount_price" =>  "175.00",
                "discount_amount" =>  "0.00",
                "discount_select" =>  0,
                "product_template_id" =>  "",
                "type_id"  =>  "",
                "status"  =>  "Quotes",
                "position" => 0
            )
        )
    ),
    //Create Product Bundle Note in Bundle 3
    "product_bundle_notes" => array(
        "create" => array(
            array(
                "description" => "Free shipping",
                "position" => 1
            )
        )
    )
);
$curl_request = curl_init($url);
curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($curl_request, CURLOPT_HEADER, false);
curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($curl_request, CURLOPT_HTTPHEADER, array(
    "Content-Type: application/json",
    "oauth-token: {$oauth_token}"
));

//convert arguments to json
$json_arguments = json_encode($productBundle);
curl_setopt($curl_request, CURLOPT_POSTFIELDS, $json_arguments);
//execute request
$curl_response = curl_exec($curl_request);
//decode json
$createdBundle = json_decode($curl_response);

//display the created record
print_r($createdBundle);
curl_close($curl_request);

//Add Bundle to Previously Created Quote
//PUT to /Quotes/<record_id>
$url = $instance_url . "/Quotes/".$createdQuote->id;
$quote = array(
    'product_bundles' => array(
        'delete' => array(
            $relatedBundles['records'][0]['id']
        ),
        'add' => array(
            $createdBundle->id
        )
    )
);

$curl_request = curl_init($url);
curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($curl_request, CURLOPT_HEADER, false);
curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($curl_request, CURLOPT_HTTPHEADER, array(
    "Content-Type: application/json",
    "oauth-token: {$oauth_token}"
));

//convert arguments to json
$json_arguments = json_encode($quote);
curl_setopt($curl_request, CURLOPT_POSTFIELDS, $json_arguments);
//PUT Request
curl_setopt($curl_request, CURLOPT_CUSTOMREQUEST, "PUT");
//execute request
$curl_response = curl_exec($curl_request);
//decode json
$updatedQuote = json_decode($curl_response);

//display the updated quote record
print_r($updatedQuote);
curl_close($curl_request);

//Get Related ProductBundles
$url = $instance_url."/Quotes/".$createdQuote->id."/link/product_bundles";

$curl_request = curl_init($url);
curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
curl_setopt($curl_request, CURLOPT_HEADER, false);
curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($curl_request, CURLOPT_HTTPHEADER, array(
    "Content-Type: application/json",
    "oauth-token: {$oauth_token}"
));

//execute request
$curl_response = curl_exec($curl_request);
//decode json
$relatedBundles = json_decode($curl_response,true);
//display the related bundles
print_r($relatedBundles['records']);
curl_close($curl_request);