<?php

include_once ('config.php');

$config = array(
    'ServiceURL' => SERVICE_URL . "/Orders/2011-01-01",
    'ProxyHost' => null,
    'ProxyPort' => -1,
    'MaxErrorRetry' => 3,
);

$service = new MarketplaceWebServiceOrders_Client(
        AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, APPLICATION_NAME, APPLICATION_VERSION, $config);


//$count	=	1;
$nexttokenorder = "";
$nexttokenorderitem = "";
$master_array = array();

function getOrderList($noofdays, $status = "Unshipped", $totalcalls = 0) {
    global $nexttokenorder, $master_array, $service;
    $request = new MarketplaceWebServiceOrders_Model_ListOrdersRequest();
    $request->setSellerId(MERCHANT_ID);
    $today = date('Y-m-d H:i:s');
    if ($noofdays == 1)
        $last15 = date('Y-m-d H:i:s', strtotime($today . ' - ' . $noofdays . 'day'));
    else
        $last15 = date('Y-m-d H:i:s', strtotime($today . ' - ' . $noofdays . 'days'));

    $request->setCreatedAfter(new DateTime($last15, new DateTimeZone('UTC')));

    $marketplaceIdList = new MarketplaceWebServiceOrders_Model_MarketplaceIdList();
    $marketplaceIdList->setId(array(MARKETPLACE_ID));
    $request->setMarketplaceId($marketplaceIdList);

    //$orderstatuslist = new MarketplaceWebServiceOrders_Model_OrderStatusList();
    //$orderstatuslist->setStatus(array("Unshipped","PartiallyShipped"));
    //$request->setOrderStatus($orderstatuslist);
    invokeListOrders($service, $request);
    $totalcalls++;

    //echo "next token order is ".$nexttokenorder;
    while (!empty($nexttokenorder)) {
        $request = new MarketplaceWebServiceOrders_Model_ListOrdersByNextTokenRequest();
        $request->setSellerId(MERCHANT_ID);
        $request->setNextToken($nexttokenorder);
        invokeListOrdersByNextToken($service, $request);
        $totalcalls++;
        if ($totalcalls >= 6)
            sleep(60);
    }
    return $master_array;
}

function fetchOrderItems($orderid) {
    global $nexttokenorderitem, $details, $service;
    $request = new MarketplaceWebServiceOrders_Model_ListOrderItemsRequest();
    $request->setSellerId(MERCHANT_ID);
    $request->setAmazonOrderId($orderid);
    sleep(2);
    invokeListOrderItems($service, $request, $orderid);

    //echo "next token order is ".$nexttokenorder;
    while (!empty($nexttokenorderitem)) {
        sleep(2);
        $request = new MarketplaceWebServiceOrders_Model_ListOrderItemsByNextTokenRequest();
        $request->setSellerId(MERCHANT_ID);
        $request->setNextToken($nexttokenorderitem);
        invokeListOrderItemsByNextToken($service, $request);
    }
}

$items = array();

function invokeListOrders(MarketplaceWebServiceOrders_Interface $service, $request) {
    //$master_array = array();
    global $nexttokenorder, $master_array, $items;
    try {
        $response = $service->listOrders($request);
        //print_r($response);
//		die;
        // echo ("Service Response\n");
        // echo ("=============================================================================\n");
        // echo("        ListOrdersResponse\n");
        if ($response->isSetListOrdersResult()) {
            //   echo("            ListOrdersResult\n");
            $listOrdersResult = $response->getListOrdersResult();
            if ($listOrdersResult->isSetNextToken()) {
                // echo("                NextToken\n");
                // echo("                    " . $listOrdersResult->getNextToken() . "\n");
                $nexttokenorder = $listOrdersResult->getNextToken();
            }
            if ($listOrdersResult->isSetCreatedBefore()) {
                //echo("                CreatedBefore\n");
                //echo("                    " . $listOrdersResult->getCreatedBefore() . "\n");
            }

            if ($listOrdersResult->isSetLastUpdatedBefore()) {
                //echo("                LastUpdatedBefore\n");
                //echo("                    " . $listOrdersResult->getLastUpdatedBefore() . "\n");
            }
            if ($listOrdersResult->isSetOrders()) {
                //echo("                Orders\n");
                $orders = $listOrdersResult->getOrders();
                $orderList = $orders->getOrder();

                print_r($orderList);
                die;

                foreach ($orderList as $order) {

                    $inside_array = array();
                    //echo("                    Order\n");
                    if ($order->isSetAmazonOrderId()) {
                        $order_id = $order->getAmazonOrderId();
                        $inside_array['order_id'] = $order_id;
                        //array_push($inside_array, $order_id);
                    }
                    $seller_order_id = $order->getSellerOrderId();
                    $inside_array['seller_order_id'] = $seller_order_id;
                    //array_push($inside_array, $seller_order_id);

                    $sale_date = $order->getPurchaseDate();
                    $inside_array['sale_date'] = $sale_date;
                    //array_push($inside_array, $sale_date);

                    $last_updated_date = $order->getLastUpdateDate();
                    $inside_array['last_updated_date'] = $last_updated_date;
                    //array_push($inside_array, $last_updated_date);

                    $order_status = $order->getOrderStatus();
                    $inside_array['order_status'] = $order_status;
                    //array_push($inside_array, $order_status);

                    $fullfillment_channel = $order->getFulfillmentChannel();
                    $inside_array['fullfillment_channel'] = $fullfillment_channel;
                    //array_push($inside_array, $fullfillment_channel);

                    $sales_channel = $order->getSalesChannel();
                    $inside_array['sales_channel'] = $sales_channel;
                    //array_push($inside_array, $sales_channel);

                    $order_channel = $order->getOrderChannel();
                    $inside_array['order_channel'] = $order_channel;
                    //array_push($inside_array, $order_channel);

                    $ship_service_level = $order->getShipServiceLevel();
                    $inside_array['Shipping_service_level'] = $ship_service_level;
                    //array_push($inside_array, $ship_service_level);

                    if ($order->isSetShippingAddress()) {

                        $shippingAddress = $order->getShippingAddress();

                        $ship_name = $shippingAddress->getName();
                        $inside_array['ship_name'] = $ship_name;
                        //array_push($inside_array, $ship_name);

                        $shipping_address1 = $shippingAddress->getAddressLine1();
                        $inside_array['shipping_address1'] = $shipping_address1;
                        //array_push($inside_array, $shipping_address1);

                        $shipping_address2 = $shippingAddress->getAddressLine2();
                        $inside_array['shipping_address2'] = $shipping_address2;
                        //array_push($inside_array, $shipping_address2);

                        $shipping_address3 = $shippingAddress->getAddressLine3();
                        $inside_array['shipping_address3'] = $shipping_address3;
                        //array_push($inside_array, $shipping_address3);

                        $shipping_city = $shippingAddress->getCity();
                        $inside_array['shipping_city'] = $shipping_city;
                        //array_push($inside_array, $shipping_city);

                        $shipping_country = $shippingAddress->getCounty();
                        $inside_array['shipping_country'] = $shipping_country;
                        //array_push($inside_array, $shipping_country);

                        $district = $shippingAddress->getDistrict();
                        $inside_array['district'] = $district;
                        //array_push($inside_array, $district);

                        $region = $shippingAddress->getStateOrRegion();
                        $inside_array['region'] = $region;
                        //array_push($inside_array, $region);

                        $postal_code = $shippingAddress->getPostalCode();
                        $inside_array['postal_code'] = $postal_code;
                        //array_push($inside_array, $postal_code);

                        $country_code = $shippingAddress->getCountryCode();
                        $inside_array['country_code'] = $country_code;
                        //array_push($inside_array, $country_code);

                        $phone = $shippingAddress->getPhone();
                        $inside_array['phone'] = $phone;
                        //array_push($inside_array, $phone);
                    }

                    if ($order->isSetOrderTotal()) {

                        $orderTotal = $order->getOrderTotal();

                        $currency_code = $orderTotal->getCurrencyCode();
                        $inside_array['currency_code'] = $currency_code;
                        //array_push($inside_array, $currency_code);

                        $order_total = $orderTotal->getAmount();
                        $inside_array['Amout'] = $order_total;
                        //array_push($inside_array, $orderTotal);
                    }

                    $shipped_items_qty = $order->getNumberOfItemsShipped();
                    $inside_array['shipped_items_qty'] = $shipped_items_qty;
                    //array_push($inside_array, $shipped_items_qty);

                    $unshiiped_items_qty = $order->getNumberOfItemsUnshipped();
                    $inside_array['unshiiped_items_qty'] = $unshiiped_items_qty;
                    //array_push($inside_array, $unshiiped_items_qty);

                    if ($order->isSetPaymentExecutionDetail()) {

                        $paymentExecutionDetail = $order->getPaymentExecutionDetail();
                        $paymentExecutionDetailItemList = $paymentExecutionDetail->getPaymentExecutionDetailItem();
                        foreach ($paymentExecutionDetailItemList as $paymentExecutionDetailItem) {

                            if ($paymentExecutionDetailItem->isSetPayment()) {
                                $payment = $paymentExecutionDetailItem->getPayment();
                                if ($payment->isSetCurrencyCode()) {

                                    //   echo("                                    CurrencyCode\n");
                                    //   echo("                                        " . $payment->getCurrencyCode() . "\n");
                                }

                                if ($payment->isSetAmount()) {

                                    // echo("                                    Amount\n");
                                    // echo("                                        " . $payment->getAmount() . "\n");
                                }
                            }

                            if ($paymentExecutionDetailItem->isSetPaymentMethod()) {

                                //echo("                                PaymentMethod\n");
                                //echo("                                    " . $paymentExecutionDetailItem->getPaymentMethod() . "\n");
                            }
                        }
                    }

                    $payment_method = $order->getPaymentMethod();
                    $inside_array['payment_method'] = $payment_method;
                    //array_push($inside_array, $payment_method);

                    if ($order->isSetMarketplaceId()) {

                        //echo("                        MarketplaceId\n");
                        //echo("                            " . $order->getMarketplaceId() . "\n");
                    }
                    $buyer_email = $order->getBuyerEmail();
                    $inside_array['buyer_email'] = $buyer_email;
                    //array_push($inside_array, $buyer_email);

                    $buyer_name = $order->getBuyerName();
                    $inside_array['buyer_name'] = $buyer_name;
                    //array_push($inside_array, $buyer_name);

                    $shipment_service_level_category = $order->getShipmentServiceLevelCategory();
                    $inside_array['shipment_service_level_category'] = $shipment_service_level_category;
                    //array_push($inside_array, $shipment_service_level_category);

                    $shipped_by_amazon_tfm = $order->getShippedByAmazonTFM();
                    $inside_array['shipped_by_amazon_tfm'] = $shipped_by_amazon_tfm;
                    //array_push($inside_array, $shipped_by_amazon_tfm);

                    $tfm_shipment_status = $order->getTFMShipmentStatus();
                    $inside_array['tfm_shipment_status'] = $tfm_shipment_status;

                    $items = array();

                    if ($inside_array['order_status'] == 'Shipped') {
                        fetchOrderItems($order_id);
                        $inside_array['orderitems'] = $items;
                    }

                    array_push($master_array, $inside_array);
                }
            }
        }
        if ($response->isSetResponseMetadata()) {
            //echo("            ResponseMetadata\n");
            $responseMetadata = $response->getResponseMetadata();
            if ($responseMetadata->isSetRequestId()) {
                // echo("                RequestId\n");
                // echo("                    " . $responseMetadata->getRequestId() . "\n");
            }
        }
        //echo("            ResponseHeaderMetadata: " . $response->getResponseHeaderMetadata() . "\n");
    } catch (MarketplaceWebServiceOrders_Exception $ex) {
        echo("Caught Exception: " . $ex->getMessage() . "\n");
        echo("Response Status Code: " . $ex->getStatusCode() . "\n");
        echo("Error Code: " . $ex->getErrorCode() . "\n");
        echo("Error Type: " . $ex->getErrorType() . "\n");
        echo("Request ID: " . $ex->getRequestId() . "\n");
        echo("XML: " . $ex->getXML() . "\n");
        echo("ResponseHeaderMetadata: " . $ex->getResponseHeaderMetadata() . "\n");
    }

    return $master_array;
}

/////////////////////*************************************************///////////////////////
function invokeListOrdersByNextToken(MarketplaceWebServiceOrders_Interface $service, $request) {
    global $nexttokenorder, $master_array, $items;
    $nexttokenorder = "";

    try {
        $response = $service->listOrdersByNextToken($request);
        //print_r($response);
        // echo ("Service Response\n");
        // echo ("=============================================================================\n");
        // echo("        ListOrdersResponse\n");
        if ($response->isSetListOrdersByNextTokenResult()) {
            //   echo("            ListOrdersResult\n");
            $listOrdersResult = $response->getListOrdersByNextTokenResult();
            if ($listOrdersResult->isSetNextToken()) {
                // echo("                NextToken\n");
                // echo("                    " . $listOrdersResult->getNextToken() . "\n");
                $nexttokenorder = $listOrdersResult->getNextToken();
            }
            if ($listOrdersResult->isSetCreatedBefore()) {
                //echo("                CreatedBefore\n");
                //echo("                    " . $listOrdersResult->getCreatedBefore() . "\n");
            }
            if ($listOrdersResult->isSetLastUpdatedBefore()) {
                //echo("                LastUpdatedBefore\n");
                //echo("                    " . $listOrdersResult->getLastUpdatedBefore() . "\n");
            }
            if ($listOrdersResult->isSetOrders()) {
                //echo("                Orders\n");
                $orders = $listOrdersResult->getOrders();
                $orderList = $orders->getOrder();

                foreach ($orderList as $order) {
                    $inside_array = array();
                    //echo("                    Order\n");
                    if ($order->isSetAmazonOrderId()) {
                        $order_id = $order->getAmazonOrderId();
                        $inside_array['order_id'] = $order_id;
                        //array_push($inside_array, $order_id);
                    }
                    $seller_order_id = $order->getSellerOrderId();
                    $inside_array['seller_order_id'] = $seller_order_id;
                    //array_push($inside_array, $seller_order_id);

                    $sale_date = $order->getPurchaseDate();
                    $inside_array['sale_date'] = $sale_date;
                    //array_push($inside_array, $sale_date);

                    $last_updated_date = $order->getLastUpdateDate();
                    $inside_array['last_updated_date'] = $last_updated_date;
                    //array_push($inside_array, $last_updated_date);

                    $order_status = $order->getOrderStatus();
                    $inside_array['order_status'] = $order_status;
                    //array_push($inside_array, $order_status);

                    $fullfillment_channel = $order->getFulfillmentChannel();
                    $inside_array['fullfillment_channel'] = $fullfillment_channel;
                    //array_push($inside_array, $fullfillment_channel);

                    $sales_channel = $order->getSalesChannel();
                    $inside_array['sales_channel'] = $sales_channel;
                    //array_push($inside_array, $sales_channel);

                    $order_channel = $order->getOrderChannel();
                    $inside_array['order_channel'] = $order_channel;
                    //array_push($inside_array, $order_channel);

                    $ship_service_level = $order->getShipServiceLevel();
                    $inside_array['Shipping_service_level'] = $ship_service_level;
                    //array_push($inside_array, $ship_service_level);


                    if ($order->isSetShippingAddress()) {

                        $shippingAddress = $order->getShippingAddress();

                        $ship_name = $shippingAddress->getName();
                        $inside_array['ship_name'] = $ship_name;
                        //array_push($inside_array, $ship_name);

                        $shipping_address1 = $shippingAddress->getAddressLine1();
                        $inside_array['shipping_address1'] = $shipping_address1;
                        //array_push($inside_array, $shipping_address1);

                        $shipping_address2 = $shippingAddress->getAddressLine2();
                        $inside_array['shipping_address2'] = $shipping_address2;
                        //array_push($inside_array, $shipping_address2);

                        $shipping_address3 = $shippingAddress->getAddressLine3();
                        $inside_array['shipping_address3'] = $shipping_address3;
                        //array_push($inside_array, $shipping_address3);

                        $shipping_city = $shippingAddress->getCity();
                        $inside_array['shipping_city'] = $shipping_city;
                        //array_push($inside_array, $shipping_city);

                        $shipping_country = $shippingAddress->getCounty();
                        $inside_array['shipping_country'] = $shipping_country;
                        //array_push($inside_array, $shipping_country);

                        $district = $shippingAddress->getDistrict();
                        $inside_array['district'] = $district;
                        //array_push($inside_array, $district);

                        $region = $shippingAddress->getStateOrRegion();
                        $inside_array['region'] = $region;
                        //array_push($inside_array, $region);

                        $postal_code = $shippingAddress->getPostalCode();
                        $inside_array['postal_code'] = $postal_code;
                        //array_push($inside_array, $postal_code);

                        $country_code = $shippingAddress->getCountryCode();
                        $inside_array['country_code'] = $country_code;
                        //array_push($inside_array, $country_code);

                        $phone = $shippingAddress->getPhone();
                        $inside_array['phone'] = $phone;
                        //array_push($inside_array, $phone);
                    }

                    if ($order->isSetOrderTotal()) {

                        $orderTotal = $order->getOrderTotal();

                        $currency_code = $orderTotal->getCurrencyCode();
                        $inside_array['currency_code'] = $currency_code;
                        //array_push($inside_array, $currency_code);

                        $order_total = $orderTotal->getAmount();
                        $inside_array['Amout'] = $order_total;
                        //array_push($inside_array, $orderTotal);
                    }

                    $shipped_items_qty = $order->getNumberOfItemsShipped();
                    $inside_array['shipped_items_qty'] = $shipped_items_qty;
                    //array_push($inside_array, $shipped_items_qty);

                    $unshiiped_items_qty = $order->getNumberOfItemsUnshipped();
                    $inside_array['unshiiped_items_qty'] = $unshiiped_items_qty;
                    //array_push($inside_array, $unshiiped_items_qty);

                    if ($order->isSetPaymentExecutionDetail()) {

                        $paymentExecutionDetail = $order->getPaymentExecutionDetail();
                        $paymentExecutionDetailItemList = $paymentExecutionDetail->getPaymentExecutionDetailItem();
                        foreach ($paymentExecutionDetailItemList as $paymentExecutionDetailItem) {

                            if ($paymentExecutionDetailItem->isSetPayment()) {
                                $payment = $paymentExecutionDetailItem->getPayment();
                                if ($payment->isSetCurrencyCode()) {

                                    //   echo("                                    CurrencyCode\n");
                                    //   echo("                                        " . $payment->getCurrencyCode() . "\n");
                                }

                                if ($payment->isSetAmount()) {
                                    // echo("                                    Amount\n");
                                    // echo("                                        " . $payment->getAmount() . "\n");
                                }
                            }

                            if ($paymentExecutionDetailItem->isSetPaymentMethod()) {
                                //echo("                                PaymentMethod\n");
                                //echo("                                    " . $paymentExecutionDetailItem->getPaymentMethod() . "\n");
                            }
                        }
                    }
                    $payment_method = $order->getPaymentMethod();
                    $inside_array['payment_method'] = $payment_method;
                    //array_push($inside_array, $payment_method);

                    if ($order->isSetMarketplaceId()) {

                        //echo("                        MarketplaceId\n");
                        //echo("                            " . $order->getMarketplaceId() . "\n");
                    }
                    $buyer_email = $order->getBuyerEmail();
                    $inside_array['buyer_email'] = $buyer_email;
                    //array_push($inside_array, $buyer_email);

                    $buyer_name = $order->getBuyerName();
                    $inside_array['buyer_name'] = $buyer_name;
                    //array_push($inside_array, $buyer_name);

                    $shipment_service_level_category = $order->getShipmentServiceLevelCategory();
                    $inside_array['shipment_service_level_category'] = $shipment_service_level_category;
                    //array_push($inside_array, $shipment_service_level_category);

                    $shipped_by_amazon_tfm = $order->getShippedByAmazonTFM();
                    $inside_array['shipped_by_amazon_tfm'] = $shipped_by_amazon_tfm;
                    //array_push($inside_array, $shipped_by_amazon_tfm);

                    $tfm_shipment_status = $order->getTFMShipmentStatus();
                    $inside_array['tfm_shipment_status'] = $tfm_shipment_status;

                    $items = array();
                    if ($inside_array['order_status'] == 'Unshipped') {
                        fetchOrderItems($order_id);
                        $inside_array['orderitems'] = $items;
                    }

                    array_push($master_array, $inside_array);
                }
            }
            /* if ($listOrdersResult->isSetNextToken()) {
              $next_token_replay = $listOrdersResult->getNextToken();
              $request = new MarketplaceWebServiceOrders_Model_ListOrdersByNextTokenRequest();
              $request->setSellerId(MERCHANT_ID);
              $request->setNextToken($next_token_replay);
              invokeListOrdersByNextToken($service, $request);
              } */
        }
        if ($response->isSetResponseMetadata()) {
            //echo("            ResponseMetadata\n");
            $responseMetadata = $response->getResponseMetadata();
            if ($responseMetadata->isSetRequestId()) {
                // echo("                RequestId\n");
                // echo("                    " . $responseMetadata->getRequestId() . "\n");
            }
        }
        //echo("            ResponseHeaderMetadata: " . $response->getResponseHeaderMetadata() . "\n");
    } catch (MarketplaceWebServiceOrders_Exception $ex) {

        echo("Caught Exception: " . $ex->getMessage() . "\n");
        echo("Response Status Code: " . $ex->getStatusCode() . "\n");
        echo("Error Code: " . $ex->getErrorCode() . "\n");
        echo("Error Type: " . $ex->getErrorType() . "\n");
        echo("Request ID: " . $ex->getRequestId() . "\n");
        echo("XML: " . $ex->getXML() . "\n");
        echo("ResponseHeaderMetadata: " . $ex->getResponseHeaderMetadata() . "\n");
    }
    return $master_array;
}

function invokeListOrderItems(MarketplaceWebServiceOrders_Interface $service, $request, $orderid) {
    global $nexttokenorderitem, $items;
    $nexttokenorderitem = "";
    try {
        $response = $service->listOrderItems($request);
        //print_r($response);
        // echo ("Service Response\n");
        //echo ("=============================================================================\n");
        // echo("        ListOrderItemsResponse\n");
        if ($response->isSetListOrderItemsResult()) {
            // echo("            ListOrderItemsResult\n");
            $listOrderItemsResult = $response->getListOrderItemsResult();
            if ($listOrderItemsResult->isSetNextToken()) {
                // echo("                NextToken\n");
                $nexttokenorderitem = $listOrderItemsResult->getNextToken();
            }
            if ($listOrderItemsResult->isSetAmazonOrderId()) {
                // echo("                AmazonOrderId\n");
                // echo("                    " . $listOrderItemsResult->getAmazonOrderId() . "\n");
            }
            if ($listOrderItemsResult->isSetOrderItems()) {
                //echo("                OrderItems\n");
                $orderItems = $listOrderItemsResult->getOrderItems();
                $orderItemList = $orderItems->getOrderItem();
                foreach ($orderItemList as $orderItem) {
                    $inside_array = array();
                    if ($orderItem->isSetASIN()) {

                        $asin = $orderItem->getASIN();
                        $inside_array['Asin'] = $asin;
                        //array_push($inside_array,$asin);
                    }
                    if ($orderItem->isSetSellerSKU()) {
                        $SellerSKU = $orderItem->getSellerSKU();
                        $inside_array['SellerSKU'] = $SellerSKU;
                        //array_push($inside_array,$SellerSKU);
                    }
                    if ($orderItem->isSetOrderItemId()) {
                        $OrderItemId = $orderItem->getOrderItemId();
                        $inside_array['OrderItemId'] = $OrderItemId;
                        //array_push($inside_array,$OrderItemId);
                    }
                    if ($orderItem->isSetTitle()) {
                        $Title = $orderItem->getTitle();
                        $inside_array['Title'] = $Title;
                        //array_push($inside_array,$Title);
                    }
                    if ($orderItem->isSetQuantityOrdered()) {
                        $QuantityOrdered = $orderItem->getQuantityOrdered();
                        $inside_array['QuantityOrdered'] = $QuantityOrdered;
                        //array_push($inside_array,$QuantityOrdered);
                    }
                    if ($orderItem->isSetQuantityShipped()) {
                        $QuantityShipped = $orderItem->getQuantityShipped();
                        $inside_array['QuantityShipped'] = $QuantityShipped;
                        //array_push($inside_array,$QuantityShipped);
                    }
                    if ($orderItem->isSetItemPrice()) {


                        $itemPrice = $orderItem->getItemPrice();
                        if ($itemPrice->isSetCurrencyCode()) {
                            $CurrencyCode = $itemPrice->getCurrencyCode();
                            $inside_array['CurrencyCode'] = $CurrencyCode;
                            //array_push($inside_array,$CurrencyCode);
                        }
                        if ($itemPrice->isSetAmount()) {
                            $Amount = $itemPrice->getAmount();
                            $inside_array['Price'] = $Amount;
                            //array_push($inside_array,$Amount);
                        }
                    }
                    if ($orderItem->isSetShippingPrice()) {
                        //echo("                        ShippingPrice\n");
                        $shippingPrice = $orderItem->getShippingPrice();
                        if ($shippingPrice->isSetCurrencyCode()) {
                            // echo("                            CurrencyCode\n");
                            // echo("                                " . $shippingPrice->getCurrencyCode() . "\n");
                        }
                        if ($shippingPrice->isSetAmount()) {
                            //echo("                            Amount\n");
                            $shippingprice = $shippingPrice->getAmount();
                            $inside_array['shippingprice'] = $shippingprice;
                        }
                    }
                    if ($orderItem->isSetGiftWrapPrice()) {
                        //echo("                        GiftWrapPrice\n");
                        $giftWrapPrice = $orderItem->getGiftWrapPrice();
                        if ($giftWrapPrice->isSetCurrencyCode()) {
                            //echo("                            CurrencyCode\n");
                            //echo("                                " . $giftWrapPrice->getCurrencyCode() . "\n");
                        }
                        if ($giftWrapPrice->isSetAmount()) {
                            //echo("                            Amount\n");
                            $giftwrapprice = $giftWrapPrice->getAmount();
                            $inside_array['giftwrapprice'] = $giftwrapprice;
                        }
                    }
                    if ($orderItem->isSetItemTax()) {
                        //echo("                        ItemTax\n");
                        $itemTax = $orderItem->getItemTax();
                        if ($itemTax->isSetCurrencyCode()) {
                            //echo("                            CurrencyCode\n");
                            //echo("                                " . $itemTax->getCurrencyCode() . "\n");
                        }
                        if ($itemTax->isSetAmount()) {
                            //echo("                            Amount\n");
                            $ItemTax = $itemTax->getAmount();
                            $inside_array['ItemTax'] = $ItemTax;
                        }
                    }
                    if ($orderItem->isSetShippingTax()) {
                        //echo("                        ShippingTax\n");
                        $shippingTax = $orderItem->getShippingTax();
                        if ($shippingTax->isSetCurrencyCode()) {
                            //echo("                            CurrencyCode\n");
                            //echo("                                " . $shippingTax->getCurrencyCode() . "\n");
                        }
                        if ($shippingTax->isSetAmount()) {
                            //echo("                            Amount\n");
                            $ShippingTax = $shippingTax->getAmount();
                            $inside_array['ShippingTax'] = $ShippingTax;
                        }
                    }
                    if ($orderItem->isSetGiftWrapTax()) {
                        //echo("                        GiftWrapTax\n");
                        $giftWrapTax = $orderItem->getGiftWrapTax();
                        if ($giftWrapTax->isSetCurrencyCode()) {
                            //echo("                            CurrencyCode\n");
                            //echo("                                " . $giftWrapTax->getCurrencyCode() . "\n");
                        }
                        if ($giftWrapTax->isSetAmount()) {
                            //echo("                            Amount\n");
                            $GiftWrapTax = $giftWrapTax->getAmount();
                            $inside_array['GiftWrapTax'] = $GiftWrapTax;
                        }
                    }
                    if ($orderItem->isSetShippingDiscount()) {
                        //echo("                        ShippingDiscount\n");
                        $shippingDiscount = $orderItem->getShippingDiscount();
                        if ($shippingDiscount->isSetCurrencyCode()) {
                            //echo("                            CurrencyCode\n");
                            //echo("                                " . $shippingDiscount->getCurrencyCode() . "\n");
                        }
                        if ($shippingDiscount->isSetAmount()) {
                            //echo("                            Amount\n");
                            $ShippingDiscount = $shippingDiscount->getAmount();
                            $inside_array['ShippingDiscount'] = $ShippingDiscount;
                        }
                    }
                    if ($orderItem->isSetPromotionDiscount()) {
                        //echo("                        PromotionDiscount\n");
                        $promotionDiscount = $orderItem->getPromotionDiscount();
                        if ($promotionDiscount->isSetCurrencyCode()) {
                            //echo("                            CurrencyCode\n");
                            //echo("                                " . $promotionDiscount->getCurrencyCode() . "\n");
                        }
                        if ($promotionDiscount->isSetAmount()) {
                            //echo("                            Amount\n");
                            $PromotionDiscount = $promotionDiscount->getAmount();
                            $inside_array['PromotionDiscount'] = $PromotionDiscount;
                        }
                    }
                    if ($orderItem->isSetPromotionIds()) {
                        //echo("                        PromotionIds\n");
                        $promotionIds = $orderItem->getPromotionIds();
                        $promotionIdList = $promotionIds->getPromotionId();
                        foreach ($promotionIdList as $promotionId) {

                            $PromotionId = $promotionId;
                            //array_push($inside_array,$PromotionId);
                        }
                    }
                    if ($orderItem->isSetCODFee()) {
                        //echo("                        CODFee\n");
                        $CODFee = $orderItem->getCODFee();
                        if ($CODFee->isSetCurrencyCode()) {
                            //echo("                            CurrencyCode\n");
                            //echo("                                " . $CODFee->getCurrencyCode() . "\n");
                        }
                        if ($CODFee->isSetAmount()) {
                            //echo("                            Amount\n");
                            $CODFee = $CODFee->getAmount();
                            $inside_array['CODFee'] = $CODFee;
                        }
                    }
                    if ($orderItem->isSetCODFeeDiscount()) {
                        //echo("                        CODFeeDiscount\n");
                        $CODFeeDiscount = $orderItem->getCODFeeDiscount();
                        if ($CODFeeDiscount->isSetCurrencyCode()) {
                            // echo("                            CurrencyCode\n");
                            //echo("                                " . $CODFeeDiscount->getCurrencyCode() . "\n");
                        }
                        if ($CODFeeDiscount->isSetAmount()) {
                            //echo("                            Amount\n");
                            //echo("                                " . $CODFeeDiscount->getAmount() . "\n");
                        }
                    }
                    if ($orderItem->isSetGiftMessageText()) {
                        $GiftMessageText = $orderItem->getGiftMessageText();
                        $inside_array['GiftMessageText'] = $GiftMessageText;
                        //array_push($inside_array,$GiftMessageText);
                    }
                    if ($orderItem->isSetGiftWrapLevel()) {
                        $GiftWrapLevel = $orderItem->getGiftWrapLevel();
                        $inside_array['GiftWrapLevel'] = $GiftWrapLevel;
                        //array_push($inside_array,$GiftWrapLevel);
                    }
                    if ($orderItem->isSetInvoiceData()) {
                        //echo("                        InvoiceData\n");
                        $invoiceData = $orderItem->getInvoiceData();
                        if ($invoiceData->isSetInvoiceRequirement()) {
                            $InvoiceRequirement = $invoiceData->getInvoiceRequirement();
                            $inside_array['InvoiceRequirement'] = $InvoiceRequirement;
                            //array_push($inside_array,$InvoiceRequirement);
                        }
                        if ($invoiceData->isSetBuyerSelectedInvoiceCategory()) {
                            $BuyerSelectedInvoiceCategory = $invoiceData->getBuyerSelectedInvoiceCategory();
                            $inside_array['BuyerSelectedInvoiceCategory'] = $BuyerSelectedInvoiceCategory;
                            //array_push($inside_array,$BuyerSelectedInvoiceCategory);
                        }
                        if ($invoiceData->isSetInvoiceTitle()) {
                            $InvoiceTitle = $invoiceData->getInvoiceTitle();
                            $inside_array['InvoiceTitle'] = $InvoiceTitle;
                            //array_push($inside_array,$InvoiceTitle);
                        }
                        if ($invoiceData->isSetInvoiceInformation()) {
                            $InvoiceInformation = $invoiceData->getInvoiceInformation();
                            $inside_array['InvoiceInformation'] = $InvoiceInformation;
                            //array_push($inside_array,$InvoiceInformation);
                        }
                    }

                    //print_r($inside_array);
                    array_push($items, $inside_array);
                    //print_r($details);
                }
            }
        }
        if ($response->isSetResponseMetadata()) {
            //echo("            ResponseMetadata\n");
            $responseMetadata = $response->getResponseMetadata();
            if ($responseMetadata->isSetRequestId()) {
                // echo("                RequestId\n");
                // echo("                    " . $responseMetadata->getRequestId() . "\n");
            }
        }
        //echo("            ResponseHeaderMetadata: " . $response->getResponseHeaderMetadata() . "\n");
    } catch (MarketplaceWebServiceOrders_Exception $ex) {
        //echo("Caught Exception: " . $ex->getMessage() . "\n");
        //echo("Response Status Code: " . $ex->getStatusCode() . "\n");
        //echo("Error Code: " . $ex->getErrorCode() . "\n");
        //echo("Error Type: " . $ex->getErrorType() . "\n");
        //echo("Request ID: " . $ex->getRequestId() . "\n");
        //echo("XML: " . $ex->getXML() . "\n");
        // echo("ResponseHeaderMetadata: " . $ex->getResponseHeaderMetadata() . "\n");
    }
}

function invokeListOrderItemsByNextToken(MarketplaceWebServiceOrders_Interface $service, $request) {
    global $nexttokenorderitem, $items;
    $nexttokenorderitem = "";
    try {
        $response = $service->listOrderItemsByNextToken($request);
        //print_r($response);
        // echo ("Service Response\n");
        //echo ("=============================================================================\n");
        // echo("        ListOrderItemsResponse\n");
        if ($response->isSetListOrderItemsResult()) {
            // echo("            ListOrderItemsResult\n");
            $listOrderItemsResult = $response->getListOrderItemsResult();
            if ($listOrderItemsResult->isSetNextToken()) {
                // echo("                NextToken\n");
                $nexttokenorderitem = $listOrderItemsResult->getNextToken();
            }
            if ($listOrderItemsResult->isSetAmazonOrderId()) {
                // echo("                AmazonOrderId\n");
                // echo("                    " . $listOrderItemsResult->getAmazonOrderId() . "\n");
            }
            if ($listOrderItemsResult->isSetOrderItems()) {
                //echo("                OrderItems\n");
                $orderItems = $listOrderItemsResult->getOrderItems();
                $orderItemList = $orderItems->getOrderItem();
                foreach ($orderItemList as $orderItem) {
                    $inside_array = array();
                    if ($orderItem->isSetASIN()) {

                        $asin = $orderItem->getASIN();
                        $inside_array['Asin'] = $asin;
                        //array_push($inside_array,$asin);
                    }
                    if ($orderItem->isSetSellerSKU()) {
                        $SellerSKU = $orderItem->getSellerSKU();
                        $inside_array['SellerSKU'] = $SellerSKU;
                        //array_push($inside_array,$SellerSKU);
                    }
                    if ($orderItem->isSetOrderItemId()) {
                        $OrderItemId = $orderItem->getOrderItemId();
                        $inside_array['OrderItemId'] = $OrderItemId;
                        //array_push($inside_array,$OrderItemId);
                    }
                    if ($orderItem->isSetTitle()) {
                        $Title = $orderItem->getTitle();
                        $inside_array['Title'] = $Title;
                        //array_push($inside_array,$Title);
                    }
                    if ($orderItem->isSetQuantityOrdered()) {
                        $QuantityOrdered = $orderItem->getQuantityOrdered();
                        $inside_array['QuantityOrdered'] = $QuantityOrdered;
                        //array_push($inside_array,$QuantityOrdered);
                    }
                    if ($orderItem->isSetQuantityShipped()) {
                        $QuantityShipped = $orderItem->getQuantityShipped();
                        $inside_array['QuantityShipped'] = $QuantityShipped;
                        //array_push($inside_array,$QuantityShipped);
                    }
                    if ($orderItem->isSetItemPrice()) {


                        $itemPrice = $orderItem->getItemPrice();
                        if ($itemPrice->isSetCurrencyCode()) {
                            $CurrencyCode = $itemPrice->getCurrencyCode();
                            $inside_array['CurrencyCode'] = $CurrencyCode;
                            //array_push($inside_array,$CurrencyCode);
                        }
                        if ($itemPrice->isSetAmount()) {
                            $Amount = $itemPrice->getAmount();
                            $inside_array['Price'] = $Amount;
                            //array_push($inside_array,$Amount);
                        }
                    }
                    if ($orderItem->isSetShippingPrice()) {
                        //echo("                        ShippingPrice\n");
                        $shippingPrice = $orderItem->getShippingPrice();
                        if ($shippingPrice->isSetCurrencyCode()) {
                            // echo("                            CurrencyCode\n");
                            // echo("                                " . $shippingPrice->getCurrencyCode() . "\n");
                        }
                        if ($shippingPrice->isSetAmount()) {
                            //echo("                            Amount\n");
                            $shippingprice = $shippingPrice->getAmount();
                            $inside_array['shippingprice'] = $shippingprice;
                        }
                    }
                    if ($orderItem->isSetGiftWrapPrice()) {
                        //echo("                        GiftWrapPrice\n");
                        $giftWrapPrice = $orderItem->getGiftWrapPrice();
                        if ($giftWrapPrice->isSetCurrencyCode()) {
                            //echo("                            CurrencyCode\n");
                            //echo("                                " . $giftWrapPrice->getCurrencyCode() . "\n");
                        }
                        if ($giftWrapPrice->isSetAmount()) {
                            //echo("                            Amount\n");
                            $giftwrapprice = $giftWrapPrice->getAmount();
                            $inside_array['giftwrapprice'] = $giftwrapprice;
                        }
                    }
                    if ($orderItem->isSetItemTax()) {
                        //echo("                        ItemTax\n");
                        $itemTax = $orderItem->getItemTax();
                        if ($itemTax->isSetCurrencyCode()) {
                            //echo("                            CurrencyCode\n");
                            //echo("                                " . $itemTax->getCurrencyCode() . "\n");
                        }
                        if ($itemTax->isSetAmount()) {
                            //echo("                            Amount\n");
                            $ItemTax = $itemTax->getAmount();
                            $inside_array['ItemTax'] = $ItemTax;
                        }
                    }
                    if ($orderItem->isSetShippingTax()) {
                        //echo("                        ShippingTax\n");
                        $shippingTax = $orderItem->getShippingTax();
                        if ($shippingTax->isSetCurrencyCode()) {
                            //echo("                            CurrencyCode\n");
                            //echo("                                " . $shippingTax->getCurrencyCode() . "\n");
                        }
                        if ($shippingTax->isSetAmount()) {
                            //echo("                            Amount\n");
                            $ShippingTax = $shippingTax->getAmount();
                            $inside_array['ShippingTax'] = $ShippingTax;
                        }
                    }
                    if ($orderItem->isSetGiftWrapTax()) {
                        //echo("                        GiftWrapTax\n");
                        $giftWrapTax = $orderItem->getGiftWrapTax();
                        if ($giftWrapTax->isSetCurrencyCode()) {
                            //echo("                            CurrencyCode\n");
                            //echo("                                " . $giftWrapTax->getCurrencyCode() . "\n");
                        }
                        if ($giftWrapTax->isSetAmount()) {
                            //echo("                            Amount\n");
                            $GiftWrapTax = $giftWrapTax->getAmount();
                            $inside_array['GiftWrapTax'] = $GiftWrapTax;
                        }
                    }
                    if ($orderItem->isSetShippingDiscount()) {
                        //echo("                        ShippingDiscount\n");
                        $shippingDiscount = $orderItem->getShippingDiscount();
                        if ($shippingDiscount->isSetCurrencyCode()) {
                            //echo("                            CurrencyCode\n");
                            //echo("                                " . $shippingDiscount->getCurrencyCode() . "\n");
                        }
                        if ($shippingDiscount->isSetAmount()) {
                            //echo("                            Amount\n");
                            $ShippingDiscount = $shippingDiscount->getAmount();
                            $inside_array['ShippingDiscount'] = $ShippingDiscount;
                        }
                    }
                    if ($orderItem->isSetPromotionDiscount()) {
                        //echo("                        PromotionDiscount\n");
                        $promotionDiscount = $orderItem->getPromotionDiscount();
                        if ($promotionDiscount->isSetCurrencyCode()) {
                            //echo("                            CurrencyCode\n");
                            //echo("                                " . $promotionDiscount->getCurrencyCode() . "\n");
                        }
                        if ($promotionDiscount->isSetAmount()) {
                            //echo("                            Amount\n");
                            $PromotionDiscount = $promotionDiscount->getAmount();
                            $inside_array['PromotionDiscount'] = $PromotionDiscount;
                        }
                    }
                    if ($orderItem->isSetPromotionIds()) {
                        //echo("                        PromotionIds\n");
                        $promotionIds = $orderItem->getPromotionIds();
                        $promotionIdList = $promotionIds->getPromotionId();
                        foreach ($promotionIdList as $promotionId) {

                            $PromotionId = $promotionId;
                            //array_push($inside_array,$PromotionId);
                        }
                    }
                    if ($orderItem->isSetCODFee()) {
                        //echo("                        CODFee\n");
                        $CODFee = $orderItem->getCODFee();
                        if ($CODFee->isSetCurrencyCode()) {
                            //echo("                            CurrencyCode\n");
                            //echo("                                " . $CODFee->getCurrencyCode() . "\n");
                        }
                        if ($CODFee->isSetAmount()) {
                            //echo("                            Amount\n");
                            $CODFee = $CODFee->getAmount();
                            $inside_array['CODFee'] = $CODFee;
                        }
                    }
                    if ($orderItem->isSetCODFeeDiscount()) {
                        //echo("                        CODFeeDiscount\n");
                        $CODFeeDiscount = $orderItem->getCODFeeDiscount();
                        if ($CODFeeDiscount->isSetCurrencyCode()) {
                            // echo("                            CurrencyCode\n");
                            //echo("                                " . $CODFeeDiscount->getCurrencyCode() . "\n");
                        }
                        if ($CODFeeDiscount->isSetAmount()) {
                            //echo("                            Amount\n");
                            //echo("                                " . $CODFeeDiscount->getAmount() . "\n");
                        }
                    }
                    if ($orderItem->isSetGiftMessageText()) {
                        $GiftMessageText = $orderItem->getGiftMessageText();
                        $inside_array['GiftMessageText'] = $GiftMessageText;
                        //array_push($inside_array,$GiftMessageText);
                    }
                    if ($orderItem->isSetGiftWrapLevel()) {
                        $GiftWrapLevel = $orderItem->getGiftWrapLevel();
                        $inside_array['GiftWrapLevel'] = $GiftWrapLevel;
                        //array_push($inside_array,$GiftWrapLevel);
                    }
                    if ($orderItem->isSetInvoiceData()) {
                        //echo("                        InvoiceData\n");
                        $invoiceData = $orderItem->getInvoiceData();
                        if ($invoiceData->isSetInvoiceRequirement()) {
                            $InvoiceRequirement = $invoiceData->getInvoiceRequirement();
                            $inside_array['InvoiceRequirement'] = $InvoiceRequirement;
                            //array_push($inside_array,$InvoiceRequirement);
                        }
                        if ($invoiceData->isSetBuyerSelectedInvoiceCategory()) {
                            $BuyerSelectedInvoiceCategory = $invoiceData->getBuyerSelectedInvoiceCategory();
                            $inside_array['BuyerSelectedInvoiceCategory'] = $BuyerSelectedInvoiceCategory;
                            //array_push($inside_array,$BuyerSelectedInvoiceCategory);
                        }
                        if ($invoiceData->isSetInvoiceTitle()) {
                            $InvoiceTitle = $invoiceData->getInvoiceTitle();
                            $inside_array['InvoiceTitle'] = $InvoiceTitle;
                            //array_push($inside_array,$InvoiceTitle);
                        }
                        if ($invoiceData->isSetInvoiceInformation()) {
                            $InvoiceInformation = $invoiceData->getInvoiceInformation();
                            $inside_array['InvoiceInformation'] = $InvoiceInformation;
                            //array_push($inside_array,$InvoiceInformation);
                        }
                    }

                    //print_r($inside_array);
                    array_push($items, $inside_array);
                    //print_r($details);
                }
            }
        }
        if ($response->isSetResponseMetadata()) {
            //echo("            ResponseMetadata\n");
            $responseMetadata = $response->getResponseMetadata();
            if ($responseMetadata->isSetRequestId()) {
                // echo("                RequestId\n");
                // echo("                    " . $responseMetadata->getRequestId() . "\n");
            }
        }
        //echo("            ResponseHeaderMetadata: " . $response->getResponseHeaderMetadata() . "\n");
    } catch (MarketplaceWebServiceOrders_Exception $ex) {
        //echo("Caught Exception: " . $ex->getMessage() . "\n");
        //echo("Response Status Code: " . $ex->getStatusCode() . "\n");
        //echo("Error Code: " . $ex->getErrorCode() . "\n");
        //echo("Error Type: " . $ex->getErrorType() . "\n");
        //echo("Request ID: " . $ex->getRequestId() . "\n");
        //echo("XML: " . $ex->getXML() . "\n");
        // echo("ResponseHeaderMetadata: " . $ex->getResponseHeaderMetadata() . "\n");
    }
}

function prepare_shipment($order_id, $trackingno) {


    /* $xml_request='<?xml version="1.0" encoding="UTF-8"?>
      <AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amzn-envelope.xsd">
      <Header>
      <DocumentVersion>1.01</DocumentVersion>
      <MerchantIdentifier>'.MERCHANT_ID.'</MerchantIdentifier>
      </Header>
      <MessageType>OrderFulfillment</MessageType>
      <Message>
      <MessageID>1</MessageID>
      <OrderFulfillment>
      <AmazonOrderID>'.$order_id.'</AmazonOrderID>
      <FulfillmentDate>'.date('Y-m-d').'</FulfillmentDate>
      <FulfillmentData>
      <CarrierCode>Other</CarrierCode>
      <CarrierName>China Post</CarrierName>
      <ShipperTrackingNumber>'.$trackingno.'</ShipperTrackingNumber>
      </FulfillmentData>
      </OrderFulfillment>
      </Message>
      </AmazonEnvelope>'; */

    date_default_timezone_set("UTC");
    $d1 = date("Y-m-d H:i:s", time());
    $d2 = date("Y-m-d", strtotime($d1 . ' -7 hours')) . "T" . date("H:i:s", strtotime($d1 . ' -7 hours'));
    //$d2 = date("Y-m-d")."T". date("H:i:s");
    //echo $d2;
    $data = "order-id\torder-item-id\tquantity\tship-date\tcarrier-code\tcarrier-name\ttracking-number\tship-method\n";
    $data.="$order_id\t\t\t" . $d2 . "\tOther\tChina Post\t$trackingno\t\n";

    $filename = (time() * 1000) . ".txt";

    file_put_contents($filename, $data);



    return $filename;
}

function submitFeed($feed, $feedtype) {

    //echo "feed is ".$feed;

    $config = array(
        'ServiceURL' => SERVICE_URL,
        'ProxyHost' => null,
        'ProxyPort' => -1,
        'MaxErrorRetry' => 3,
    );

    $service = new MarketplaceWebService_Client(
            AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, $config, APPLICATION_NAME, APPLICATION_VERSION);

    $marketplaceIdArray = array("Id" => MARKETPLACE_ID);

    //$feed = file_get_contents("result_all.txt");

    $feedHandle = @fopen('php://temp', 'rw+');
    fwrite($feedHandle, $feed);
    rewind($feedHandle);

    $request = new MarketplaceWebService_Model_SubmitFeedRequest();
    $request->setMerchant(MERCHANT_ID);
    $request->setMarketplaceIdList($marketplaceIdArray);

    $request->setFeedType($feedtype);
    $request->setContentMd5(base64_encode(md5(stream_get_contents($feedHandle), true)));
    rewind($feedHandle);
    $request->setPurgeAndReplace(false);
    $request->setFeedContent($feedHandle);

    $submit_feed_id = invokeSubmitFeed($service, $request);

    @fclose($feedHandle);

    return $submit_feed_id;
}

function invokeSubmitFeed(MarketplaceWebService_Interface $service, $request) {
    try {
        $response = $service->submitFeed($request);



        if ($response->isSetSubmitFeedResult()) {

            $submitFeedResult = $response->getSubmitFeedResult();
            if ($submitFeedResult->isSetFeedSubmissionInfo()) {

                $feedSubmissionInfo = $submitFeedResult->getFeedSubmissionInfo();
                if ($feedSubmissionInfo->isSetFeedSubmissionId()) {
                    return $feedSubmissionInfo->getFeedSubmissionId();
                }
                if ($feedSubmissionInfo->isSetFeedType()) {
                    
                }
                if ($feedSubmissionInfo->isSetSubmittedDate()) {
                    
                }
                if ($feedSubmissionInfo->isSetFeedProcessingStatus()) {
                    
                }
                if ($feedSubmissionInfo->isSetStartedProcessingDate()) {
                    
                }
                if ($feedSubmissionInfo->isSetCompletedProcessingDate()) {
                    
                }
            }
        }
        if ($response->isSetResponseMetadata()) {
            
        }
    } catch (MarketplaceWebService_Exception $ex) {
        echo("Caught Exception: " . $ex->getMessage() . "\n");
        echo("Response Status Code: " . $ex->getStatusCode() . "\n");
        echo("Error Code: " . $ex->getErrorCode() . "\n");
        echo("Error Type: " . $ex->getErrorType() . "\n");
        echo("Request ID: " . $ex->getRequestId() . "\n");
        echo("XML: " . $ex->getXML() . "\n");
        echo("ResponseHeaderMetadata: " . $ex->getResponseHeaderMetadata() . "\n");
    }
    return null;
}

////////////////////////////////////////////////////

function amazon_send_file($localFile, $type = "_POST_FLAT_FILE_PRICEANDQUANTITYONLY_UPDATE_DATA_") {

    $feed = file_get_contents($localFile);

    return submitFeed($feed, $type);
}
