<?php

ini_set('include_path', ini_get('include_path') . ':' . $_SERVER['DOCUMENT_ROOT'] . '/admin/includes/');
include_once 'doba/DobaOrderInfo.php';
include_once 'doba/DobaOrders.php';
$tempData = array('orders_id' => 12345, 'first_name' => 'Candice', 'last_name' => 'Johnson', 'address' => '4958 W 495 S', 'city' => 'Orem', 'state' => 'UT', 'postal' => '84097', 'country' => 'USA', 'items' => array());
$dpo = new DobaOrderInfo();
echo "<ul>";
echo "<li>\"" . $dpo->orders_id($tempData['orders_id']) . "\" should be \"" . $tempData['orders_id'] . "\"</li>";
echo "<li>\"" . $dpo->first_name($tempData['first_name']) . "\" should be \"" . $tempData['first_name'] . "\"</li>";
echo "<li>\"" . $dpo->last_name($tempData['last_name']) . "\" should be \"" . $tempData['last_name'] . "\"</li>";
echo "<li>\"" . $dpo->address($tempData['address']) . "\" should be \"" . $tempData['address'] . "\"</li>";
echo "<li>\"" . $dpo->city($tempData['city']) . "\" should be \"" . $tempData['city'] . "\"</li>";
echo "<li>\"" . $dpo->state($tempData['state']) . "\" should be \"" . $tempData['state'] . "\"</li>";
echo "<li>\"" . $dpo->postal($tempData['postal']) . "\" should be \"" . $tempData['postal'] . "\"</li>";
echo "<li>\"" . $dpo->country($tempData['country']) . "\" should be \"" . $tempData['country'] . "\"</li>";
echo "</ul>";
echo "<p>Done</p>";
$dpoAry = new DobaOrders();
echo "<p>" . ($dpoAry->orderExists($tempData['orders_id']) ? 'Order added' : 'Order not added') . "</p>";
$dpoAry->addOrder($dpo);
echo "<p>" . ($dpoAry->orderExists($tempData['orders_id']) ? 'Order added' : 'Order not added') . "</p>";
 /**
  * 
  * @return DobaOrders object
  * @param $data XML string
  */
 function parseGetOrderDetailResponse($data)
 {
     $orderList = new DobaOrders();
     $p = new XMLParser($data);
     $orderDetails = $p->getOutput();
     if ($orderDetails['dce']['response']['outcome'] == 'Success') {
         $tempDobaOrderArray = new DobaOrders();
         if (array_key_exists('order', $orderDetails['dce']['response']['orders'])) {
             //only one order;
             $tempPO = $orderDetails['dce']['response']['orders']['order']['order_id'];
             $tmpName = DobaOrdersAPI::name_to_parts($orderD['dce']['response']['orders']['order']['ship_name']);
             if (array_key_exists('supplier_order', $orderDetails['dce']['response']['orders']['order']['supplier_orders'])) {
                 //Only 1 supplier_order;
                 $ord = $orderDetails['dce']['response']['orders']['order']['supplier_orders']['supplier_order'];
                 $tempDobaOrderArray = DobaOrdersAPI::findItems($ord);
                 foreach ($tempDobaOrderArray as $currOrder) {
                     $currOrder->po_number($ord['order_id']);
                     $currOrder->max_expected_total($ord['order_total']);
                     $currOrder->first_name($tmpName['FirstName']);
                     $currOrder->last_name($tmpName['LastName']);
                     $currOrder->street($ord['ship_street']);
                     $currOrder->city($ord['ship_city']);
                     $currOrder->postal($ord['ship_postal']);
                     $currOrder->country($ord['ship_country']);
                     $orderList->addOrder($currOrder);
                 }
             } else {
                 //What does multiple supplier_order mean???
             }
             return $orderList;
         } else {
             foreach ($orderDetails['dce']['response']['orders'] as $ord) {
                 $tempPO = $orderDetails['dce']['response']['orders']['order']['order_id'];
                 $tmpName = DobaOrdersAPI::name_to_parts($orderD['dce']['response']['orders']['order']['ship_name']);
                 if (array_key_exists('supplier_order', $orderDetails['dce']['response']['orders']['order']['supplier_orders'])) {
                     //Only 1 supplier_order;
                     $ord = $orderDetails['dce']['response']['orders']['order']['supplier_orders']['supplier_order'];
                     $tempDobaOrderArray = DobaOrdersAPI::findItems($ord);
                     foreach ($tempDobaOrderArray as $currOrder) {
                         $currOrder->po_number($ord['order_id']);
                         $currOrder->max_expected_total($ord['order_total']);
                         $currOrder->first_name($tmpName['FirstName']);
                         $currOrder->last_name($tmpName['LastName']);
                         $currOrder->street($ord['ship_street']);
                         $currOrder->city($ord['ship_city']);
                         $currOrder->postal($ord['ship_postal']);
                         $currOrder->country($ord['ship_country']);
                         $orderList->addOrder($currOrder);
                     }
                 } else {
                     //What does multiple supplier_order mean???
                 }
             }
             return $orderList;
         }
     } else {
         return $orderDetails['dce']['response']['outcome'];
     }
 }
    /**
     * Pulls orders by sent to Doba status
     * @static method
     * @return DobaOrders
     * @param $status string (all, new, submitted, unsubmitted)
     */
    function loadOrders($status)
    {
        $do = new DobaOrders();
        $orders = array();
        $sql = 'select 
					' . TABLE_ORDERS . '.orders_id as ponumber, 
					' . TABLE_ORDERS . '.delivery_name as name,
					' . TABLE_ORDERS . '.delivery_street_address as address1,
					' . TABLE_ORDERS . '.delivery_city as city,
					' . TABLE_ORDERS . '.delivery_state as state,
					' . TABLE_ORDERS . '.delivery_postcode as postal,
					' . TABLE_ORDERS . '.delivery_country as country,
					' . TABLE_ORDERS_PRODUCTS . '.products_id as item_id,
					' . TABLE_ORDERS_PRODUCTS . '.products_quantity as quantity,
					' . TABLE_ORDERS_PRODUCTS . '.final_price as max_expected_total,
					DobaLog.doba_log_id,
					DobaLog.api_response
				from 
					' . TABLE_ORDERS . '
					join ' . TABLE_ORDERS_PRODUCTS . ' on ' . TABLE_ORDERS_PRODUCTS . '.orders_id=' . TABLE_ORDERS . '.orders_id 
					left join DobaLog on DobaLog.local_id=' . TABLE_ORDERS . '.orders_id and DobaLog.datatype="order"';
        if ($status == 'new') {
            $sql .= ' where DobaLog.doba_log_id is NULL';
        } else {
            if ($status == 'submitted') {
                $sql .= ' where DobaLog.api_response is not NULL';
            } else {
                if ($status == 'unsubmitted') {
                    $sql .= ' where DobaLog.api_response is NULL';
                }
            }
        }
        $orders_query = tep_db_query($sql);
        while ($o = tep_db_fetch_array($orders_query)) {
            $i = new DobaOrderInfo();
            $i->po_number($o['ponumber']);
            $name = $i->name_to_parts($o['name']);
            $i->first_name($name['FirstName']);
            $i->last_name($name['LastName']);
            $i->address1($o['address1']);
            $i->city($o['city']);
            $i->state($o['state']);
            $i->postal($o['postal']);
            $i->country($o['country']);
            $i->item_id($o['item_id']);
            $i->quantity($o['quantity']);
            $i->max_expected_total($o['max_expected_total']);
            $do->addOrder($i);
        }
        return $do;
    }
ini_set('include_path', ini_get('include_path') . ':' . $_SERVER['DOCUMENT_ROOT'] . '/admin/includes/');
include_once 'doba/DobaOrderInfo.php';
include_once 'doba/DobaOrders.php';
include_once 'doba/DobaOrderFile.php';
$do = new DobaOrders();
$dboi = new DobaOrderInfo();
$dof = new DobaOrderFile();
$tempData = array('orders_id' => 12345, 'first_name' => 'Candice', 'last_name' => 'Johnson', 'address' => '4958 W 495 S', 'address2' => 'none', 'city' => 'Orem', 'state' => 'UT', 'postal' => '84097', 'country' => 'USA', 'items_id' => '25', 'quantity' => '30', 'max' => '1025');
for ($i = 0; $i < 10; $i++) {
    $temp = new DobaOrderInfo();
    $temp->po_number($i);
    $temp->first_name($tempData['first_name']);
    $temp->last_name($tempData['last_name']);
    $temp->address1($tempData['address']);
    $temp->address2($tempData['address2']);
    $temp->city($tempData['city']);
    $temp->state($tempData['state']);
    $temp->postal($tempData['postal']);
    $temp->country($tempData['country']);
    $temp->item_id($tempData['items_id']);
    $dboi->quantity($tempData['quantity']);
    $dboi->max_expected_total($tempData['max']);
    $do->addOrder($temp);
}
/*for ($i = 0; $i < 10; $i++)
{
	$do->addOrder($dboi);
}*/
echo "<pre>";
$dof->processData($do);
echo "</pre>";