<?php

include_once('./apiFunctions.php');

/*
//ini
$contactID = 0;
$billToLocationID = 1;
$shipToLocationID = 1;
$soldToLocationID = 1;
*/

//similate data from quotes
$userEmailFromEitQuotes = 'russel@overdrive.co.za';
$nameFromEitQuotes = "Jess";
$eitQuoteNumber = 278995455;
// $eitBillToAddress = "26 Tech Valley Drive, Suite 2, East Greenbush, NY, 12061, United States";
$eitBillToAddress = "Test Hares Avenue, Woodstock, 7925, Cape Town Western Cape, South Africa, 7925";
// $eitBillToAddress = "6 Begonia Street, Milerton";
$eitShipToAddress = "6 Begonia Street, Milerton";
$clientName = "1ForTheRoad";
// $clientName = "2Way Systems";
$comment = "test comment" . date("Y/m/d");


$apiFunction = new APIFunctions();

//if the whole address is bigger than 50 chars which is the max per address field in autotask we need to break it up 
$eitBillToAddress1 = "";
$eitBillToAddress2 = "";
if (strlen($eitBillToAddress) > 50) {
   $eitBillToAddress1 = substr($eitBillToAddress, 0, 47);
   $eitBillToAddress2 = substr($eitBillToAddress, 48);
}

$apiFunction->print_rr($eitBillToAddress1);
$apiFunction->print_rr($eitBillToAddress2);


// $result = $apiFunction->createQuoteLocation($eitBillToAddress1, $eitBillToAddress2);
// $apiFunction->print_rr($result);

// $eitBillToAddressTokens = explode(",", $eitBillToAddress);
// $apiFunction->print_rr($eitBillToAddressTokens);

// $rstBillToPhysicalLocation1 = $apiFunction->customQuery("QuoteLocation", 'Address1', null, 'Contains', $eitBillToAddressTokens[0]);
// $apiFunction->print_rr($rstBillToPhysicalLocation1);

die;

$result = $apiFunction->generateAutoTaskQuote($userEmailFromEitQuotes, $nameFromEitQuotes, $eitQuoteNumber, $eitBillToAddress, $eitShipToAddress, $clientName, $comment);
$apiFunction->print_rr($result);



/*

$eitBillToAddressTokens = explode(",", $eitBillToAddress);
// $apiFunction->print_rr($eitBillToAddressTokens);

//get all locations that match first eit address token 
//depending on how people entered address in eit quotes we may need to check in both Address1 and Address2 fields for match
$rstBillToPhysicalLocation1 = $apiFunction->customQuery("QuoteLocation", 'Address1', null, 'Contains', $eitBillToAddressTokens[0]);
// $apiFunction->print_rr($rstBillToPhysicalLocation1);

$physicalLocation = "";
$physicalLocationID = 0;
$physicalLocationArr = array();
if (isset($rstBillToPhysicalLocation1->EntityResults->Entity)) { //check if first query result contains data else make second query to check if Address2 field contains eitaddress
   // $apiFunction->print_rr("first query success");
   $physicalLocation = $rstBillToPhysicalLocation1->EntityResults->Entity;
} else {
   // $apiFunction->print_rr("run second query");
   $rstBillToPhysicalLocation2 = $apiFunction->customQuery("QuoteLocation", 'Address2', null, 'Contains', $eitBillToAddressTokens[0]);
   if (isset($rstBillToPhysicalLocation2->EntityResults->Entity)) {
      $physicalLocation = $rstBillToPhysicalLocation2->EntityResults->Entity;
   } else { //if both result in no data then create new location and get id
      $apiFunction->print_rr("create new location and get id");
      //get and set new physicalLocationID
      // $physicalLocationID = 0;
   }
}

//only get here if there is unfiltered address data
//in here we will filter and check the data and create a new location after filtering and checking is concluded and the address doesn't exist
if ($physicalLocationID === 0) {
   if (isset($physicalLocation)) {
      if (gettype($physicalLocation) === "array") { //if it's an array there will be more than one address else there will only be one and we need to get the matching address id
         // $apiFunction->print_rr("test2");

         for ($i = 0; $i < count($physicalLocation); $i++) {
            // $apiFunction->print_rr("test3");
            // $apiFunction->print_rr($physicalLocation[$i]->Address1);
            if (strpos($eitBillToAddress, $physicalLocation[$i]->Address1) !== false) {
               array_push($physicalLocationArr, $physicalLocation[$i]->id);
            } else { //if eit address string doesn't contain adresses from autotask then create new location and get id
               $apiFunction->print_rr("create new location and get id");
            }
         }
      } elseif (gettype($physicalLocation) === "object") {
         // $apiFunction->print_rr("test5");
         if (strpos($eitBillToAddress, $physicalLocation->Address1) !== false) {
            array_push($physicalLocationArr, $physicalLocation->id);
         } else { //if eit address string doesn't contain adresses from autotask then create new location and get id
            $apiFunction->print_rr("create new location and get id");
         }
      }

      if (!empty($physicalLocationArr)) {
         $billToLocationID = $physicalLocationArr[0];
         $shipToLocationID = $physicalLocationArr[0];
         $soldToLocationID = $physicalLocationArr[0];
      }
   }
}

//get OwnerResourceId based on user creating quote's name/email
$rstUserName = $apiFunction->customQuery("Resource", 'FirstName', null, 'Like', $nameFromEitQuotes);
// $rstUserEmail = $apiFunction->customQuery("Resource", 'Email', null, 'Like', $userEmailFromEITQuotes);
$ownerResourceID = $rstUserName->EntityResults->Entity->id;

//check if client exists using their name, can't use ID cause the id's in quotes and autotask are not the same
$clientCheckResult = $apiFunction->queryClient($clientName);
// $apiFunction->print_rr($clientCheckResult);

if ($clientCheckResult["blnResult"] === true) { //if client exists create opportunity and quote, quoteitem(s) linked to it

   //get contact info of client using clientID
   $contactCheckResult = $apiFunction->customQuery("Contact", 'AccountID', null, 'Equals', $clientCheckResult['clientID']);

   if (gettype($contactCheckResult->EntityResults->Entity) === "array") { //use first contact if there is more than one for that specific client
      $contactID = $contactCheckResult->EntityResults->Entity[0]->id;
   } else if (gettype($contactCheckResult->EntityResults->Entity) === "object") { //if its only one, use that one
      $contactID = $contactCheckResult->EntityResults->Entity->id;
   }

   //create opportunity
   $opportunityCreateResult = $apiFunction->createOpportunity($ownerResourceID, $clientCheckResult['clientID'], $eitQuoteNumber, $contactID);
   $apiFunction->print_rr($opportunityCreateResult);

   if ($opportunityCreateResult["blnError"] !== 1) { //if there are not errors in creating opportunity create quote

      //create quote
      $quoteCreateResult = $apiFunction->createQuote($opportunityCreateResult["createdOpportunityID"], $eitQuoteNumber, $contactID, $billToLocationID, $shipToLocationID, $soldToLocationID, $comment);
      $apiFunction->print_rr($quoteCreateResult);

      if ($quoteCreateResult["blnError"] !== 1) {
         print_r('create quote items');
      } else {
         $apiFunction->print_rr($quoteCreateResult["result"]);
      }
   } else {  //if there are errors in creating opportunity display error message
      $apiFunction->print_rr($opportunityCreateResult["result"]);
   }
} else { //if client doesn't exist 
   print_r('client does not exist');
}

*/