// include in all Avalara Scripts
require '../Credentials.php';
// where service URL, account, license key are set
$STDIN = fopen('php://stdin', 'r');
$client = new TaxServiceSoap('Development');
$request = new ReconcileTaxHistoryRequest();
echo "Enter Start Date (yyyy-mm-dd):";
$input = rtrim(fgets($STDIN));
$request->setStartDate($input);
echo "Enter End Date (yyyy-mm-dd):";
$input = rtrim(fgets($STDIN));
$request->setEndDate($input);
$request->setCompanyCode('<Your Company Code Here>');
// Dashboard Company Code
$request->setDocStatus(DocStatus::$Committed);
$request->setLastDocCode("0");
$request->setPageSize(1000);
$request->setDocType(DocumentType::$SalesInvoice);
try {
    $result = $client->reconcileTaxHistory($request);
    echo 'ReconcileTaxHistory ResultCode is: ' . $result->getResultCode() . "\n";
    if ($result->getResultCode() != SeverityLevel::$Success) {
        foreach ($result->getMessages() as $msg) {
            echo $msg->getName() . ": " . $msg->getSummary() . "\n";
        }
    } else {
        echo "Committed Documents Dated Between " . $request->getStartDate() . " and " . $request->getEndDate() . ":\n";
        foreach ($result->getGetTaxResults() as $getRes) {
            echo "     Invoice Number: " . $getRes->getDocCode() . " Invoice Amount: " . $getRes->getTotalAmount();
            echo "  Total Taxes on Invoice: " . $getRes->getTotalTax() . "\n";
        }
 function testReconcileTaxHistory()
 {
     global $client;
     $dateTime = new DateTime();
     $docCode = "PHPReconcile" . date_format($dateTime, "dmyGis");
     $getTaxRequest = $this->CreateTaxRequest($docCode);
     $getTaxRequest->setCommit(true);
     $getTaxResult = $client->getTax($getTaxRequest);
     $this->assertEqual(SeverityLevel::$Success, $getTaxResult->getResultCode());
     $this->assertEqual(DocStatus::$Committed, $getTaxResult->getDocStatus());
     $reconcileTaxHistoryRequest = new ReconcileTaxHistoryRequest();
     $reconcileTaxHistoryRequest->setCompanyCode("DEFAULT");
     //request.setReconciled(false);
     $reconcileTaxHistoryRequest->setStartDate(date_format($dateTime, "Y-m-d"));
     $reconcileTaxHistoryRequest->setEndDate(date_format($dateTime, "Y-m-d"));
     $reconcileTaxHistoryRequest->setDocStatus(DocStatus::$Committed);
     $reconcileTaxHistoryRequest->setLastDocId("0");
     $reconcileTaxHistoryRequest->setLastDocCode("0");
     $reconcileTaxHistoryRequest->setPageSize(1000);
     $reconcileTaxHistoryRequest->setDocType(DocumentType::$SalesInvoice);
     //Calling reconHistory Method
     $reconcileTaxHistoryResult = $client->reconcileTaxHistory($reconcileTaxHistoryRequest);
     $this->assertEqual(SeverityLevel::$Success, $reconcileTaxHistoryResult->getResultCode());
     $taxResults = $reconcileTaxHistoryResult->getGetTaxResults();
     $this->assertTrue(count($taxResults) > 0, "Expected > 0 reconcile records");
     $this->assertTrue($reconcileTaxHistoryResult->getRecordCount() >= count($taxResults), "RecordCount has to be equal to or more than number of records fetched");
     $found = false;
     /*do
     	        {
     	            foreach ($taxResults as $taxResult)
     	            {
     	                $this->assertEqual(DocStatus::$Committed, $taxResult->getDocStatus());                
     	                	
     	                if (strcmp($taxResult->getDocCode(),$getTaxRequest->getDocCode()) == 0)
     	                {
     	                    $found = true;
     	                }
     	            }
     	
     	            $reconcileTaxHistoryRequest->setLastDocCode($reconcileTaxHistoryResult->getLastDocCode());
     	            $reconcileTaxHistoryResult = $client->reconcileTaxHistory($reconcileTaxHistoryRequest);	            
     	            $taxResults = $reconcileTaxHistoryResult->getGetTaxResults();	            
     	        }
     	        while ( count($taxResults) > 0);*/
     //$this->assertTrue($found,"ReconcileCommittedTest doc not found");
     //Cancel Tax
     $cancelRequest = new CancelTaxRequest();
     $cancelRequest->setCompanyCode($getTaxRequest->getCompanyCode());
     $cancelRequest->setDocCode($getTaxRequest->getDocCode());
     $cancelRequest->setDocType($getTaxRequest->getDocType());
     $cancelRequest->setCancelCode(CancelCode::$DocDeleted);
     $cancelResult = $client->cancelTax($cancelRequest);
     $this->assertEqual(SeverityLevel::$Success, $cancelResult->getResultCode());
 }