<?php 

include 'CrmAuth.php';
include 'CrmExecuteSoap.php';
include "CrmAuthenticationHeader.php";


$url = "https://subway.crm.dynamics.com/";
$username = "acorrea@subway.com.pa";
$password = "Password11";

$crmAuth = new CrmAuth ();
$authHeader = $crmAuth->GetHeaderOnline ( $username, $password, $url );

function WhoAmI($authHeader, $url) {
	$xml = "<s:Body>
		<Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\">
			<request i:type=\"c:WhoAmIRequest\" xmlns:b=\"http://schemas.microsoft.com/xrm/2011/Contracts\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:c=\"http://schemas.microsoft.com/crm/2011/Contracts\">
				<b:Parameters xmlns:d=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\"/>
				<b:RequestId i:nil=\"true\"/>
				<b:RequestName>WhoAmI</b:RequestName>
			</request>
		</Execute>
	</s:Body>";
	
	$executeSoap = new CrmExecuteSoap ();
	$response = $executeSoap->ExecuteSOAPRequest ( $authHeader, $xml, $url );
	
	$responsedom = new DomDocument ();
	$responsedom->loadXML ( $response );
	
	$values = $responsedom->getElementsbyTagName ( "KeyValuePairOfstringanyType" );
	
	foreach ( $values as $value ) {
		if ($value->firstChild->textContent == "UserId") {
			return $value->lastChild->textContent;
		}
	}
	
	return null;
}

function CrmGetAccountName($authHeader, $id, $url) {
	$xml = "<s:Body>
		<Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">
			<request i:type=\"a:RetrieveRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\">
				<a:Parameters xmlns:b=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">
					<a:KeyValuePairOfstringanyType>
						<b:key>Target</b:key>
						<b:value i:type=\"a:EntityReference\">
							<a:Id>" . $id . "</a:Id>
							<a:LogicalName>systemuser</a:LogicalName>
							<a:Name i:nil=\"true\" />
						</b:value>
					</a:KeyValuePairOfstringanyType>
					<a:KeyValuePairOfstringanyType>
						<b:key>ColumnSet</b:key>
						<b:value i:type=\"a:ColumnSet\">
							<a:AllColumns>false</a:AllColumns>
							<a:Columns xmlns:c=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">
								<c:string>firstname</c:string>
								<c:string>lastname</c:string>
							</a:Columns>
						</b:value>
					</a:KeyValuePairOfstringanyType>
				</a:Parameters>
				<a:RequestId i:nil=\"true\" />
				<a:RequestName>Retrieve</a:RequestName>
			</request>
		</Execute>
	</s:Body>";
	
	$executeSoap = new CrmExecuteSoap ();
	$response = $executeSoap->ExecuteSOAPRequest ( $authHeader, $xml, $url );
	$responsedom = new DomDocument ();
	$responsedom->loadXML ( $response );
	$firstname = "";
	$lastname = "";
	$values = $responsedom->getElementsbyTagName ( "KeyValuePairOfstringanyType" );
	foreach ( $values as $value ) {
		if ($value->firstChild->textContent == "firstname") {
			$firstname = $value->lastChild->textContent;
		}
		
		if ($value->firstChild->textContent == "lastname") {
			$lastname = $value->lastChild->textContent;
		}
	}
  echo  $firstname . " -" . $lastname;
	return $firstname . " " . $lastname;
}




function CrmGetContactDataByCard($authHeader, $id, $url) {

$xml="
<s:Body>
    <Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">
      <request i:type=\"a:RetrieveMultipleRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\">
        <a:Parameters xmlns:b=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">
          <a:KeyValuePairOfstringanyType>
            <b:key>Query</b:key>
            <b:value i:type=\"a:QueryExpression\">
              <a:ColumnSet>
                <a:AllColumns>true</a:AllColumns>
              </a:ColumnSet>
              <a:Criteria>
                <a:Conditions>
                  <a:ConditionExpression>
                    <a:AttributeName>ccs_subwaycardid</a:AttributeName>
                    <a:Operator>Equal</a:Operator>
                    <a:Values xmlns:c=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">
                      <c:anyType i:type=\"d:string\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\">".$id."</c:anyType>
                    </a:Values>
                  </a:ConditionExpression>
                </a:Conditions>
                <a:FilterOperator>And</a:FilterOperator>
                <a:Filters />
                <a:IsQuickFindFilter>false</a:IsQuickFindFilter>
              </a:Criteria>
              <a:Distinct>false</a:Distinct>
              <a:EntityName>contact</a:EntityName>
              <a:LinkEntities />
              <a:Orders />
              <a:PageInfo>
                <a:Count>0</a:Count>
                <a:PageNumber>0</a:PageNumber>
                <a:PagingCookie i:nil=\"true\" />
                <a:ReturnTotalRecordCount>false</a:ReturnTotalRecordCount>
              </a:PageInfo>
              <a:NoLock>false</a:NoLock>
            </b:value>
          </a:KeyValuePairOfstringanyType>
        </a:Parameters>
        <a:RequestId i:nil=\"true\" />
        <a:RequestName>RetrieveMultiple</a:RequestName>
      </request>
    </Execute>
  </s:Body>
";
	$executeSoap = new CrmExecuteSoap ();
	$response = $executeSoap->ExecuteSOAPRequest ( $authHeader, $xml, $url );
	$responsedom = new DomDocument ();
	$responsedom->loadXML ( $response );
  var_dump($response);
	$result=array();
	$values = $responsedom->getElementsbyTagName ( "KeyValuePairOfstringanyType" );
	foreach ( $values as $value ) {
		$result[$value->firstChild->textContent]=$value->lastChild->textContent;
	}
	
	return $result;
}

function CrmGetAccountDataByGuid($authHeader, $id, $url) {
	$xml = "<s:Body>
		<Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">
			<request i:type=\"a:RetrieveRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\">
				<a:Parameters xmlns:b=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">
					<a:KeyValuePairOfstringanyType>
						<b:key>Target</b:key>
						<b:value i:type=\"a:EntityReference\">
							<a:Id>".$id."</a:Id>
							<a:LogicalName>systemuser</a:LogicalName>
							<a:Name i:nil=\"true\" />
						</b:value>
					</a:KeyValuePairOfstringanyType>
					<a:KeyValuePairOfstringanyType>
						<b:key>ColumnSet</b:key>
						<b:value i:type=\"a:ColumnSet\">
							<a:AllColumns>true</a:AllColumns>
						</b:value>
					</a:KeyValuePairOfstringanyType>
				</a:Parameters>
				<a:RequestId i:nil=\"true\" />
				<a:RequestName>Retrieve</a:RequestName>
			</request>
		</Execute>
	</s:Body>
";
	
	$executeSoap = new CrmExecuteSoap ();
	$response = $executeSoap->ExecuteSOAPRequest ( $authHeader, $xml, $url );
	$responsedom = new DomDocument ();
	$responsedom->loadXML ( $response );
	$result=array();
	$values = $responsedom->getElementsbyTagName ( "KeyValuePairOfstringanyType" );
	foreach ( $values as $value ) {
		$result[$value->firstChild->textContent]=$value->lastChild->textContent;
	}
	return $result;
}

function CrmGetAccountDataByGuid2($authHeader, $id, $url) {
	$xml = "<s:Body>
		<Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">
			<request i:type=\"a:RetrieveRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\">
				<a:Parameters xmlns:b=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">
					<a:KeyValuePairOfstringanyType>
						<b:key>Target</b:key>
						<b:value i:type=\"a:EntityReference\">
							<a:Id>".$id."</a:Id>
							<a:LogicalName>ccs_transactions</a:LogicalName>
							<a:Name i:nil=\"true\" />
						</b:value>
					</a:KeyValuePairOfstringanyType>
					<a:KeyValuePairOfstringanyType>
						<b:key>ColumnSet</b:key>
						<b:value i:type=\"a:ColumnSet\">
							<a:AllColumns>true</a:AllColumns>
						</b:value>
					</a:KeyValuePairOfstringanyType>
				</a:Parameters>
				<a:RequestId i:nil=\"true\" />
				<a:RequestName>Retrieve</a:RequestName>
			</request>
		</Execute>
	</s:Body>
";
	
	$executeSoap = new CrmExecuteSoap ();
	$response = $executeSoap->ExecuteSOAPRequest ( $authHeader, $xml, $url );
	$responsedom = new DomDocument ();
	$responsedom->loadXML ( $response );
	$result=array();
	$values = $responsedom->getElementsbyTagName ( "KeyValuePairOfstringanyType" );
	foreach ( $values as $value ) {
		$result[$value->firstChild->textContent]=$value->lastChild->textContent;
	}
	return $result;
}



function CrmGetTransactions($authHeader, $id, $url) {

$xml="
<s:Body>
    <Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">
      <request i:type=\"a:RetrieveMultipleRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\">
        <a:Parameters xmlns:b=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">
          <a:KeyValuePairOfstringanyType>
            <b:key>Query</b:key>
            <b:value i:type=\"a:QueryExpression\">
              <a:ColumnSet>
                <a:AllColumns>true</a:AllColumns>
              </a:ColumnSet>
              <a:Criteria>
                <a:Conditions>
                  <a:ConditionExpression>
                    <a:AttributeName>ccs_cliente</a:AttributeName>
                    <a:Operator>Equal</a:Operator>
                    <a:Values xmlns:c=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">
                      <c:anyType i:type=\"d:string\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\">".$id."</c:anyType>
                    </a:Values>
                  </a:ConditionExpression>


                  <a:ConditionExpression>
                    <a:AttributeName>ccs_tipodetransaccion</a:AttributeName>
                    <a:Operator>Equal</a:Operator>
                    <a:Values xmlns:c=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">
                      <c:anyType i:type=\"d:string\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\">2</c:anyType>
                    </a:Values>
                  </a:ConditionExpression>


                  <a:ConditionExpression>
                    <a:AttributeName>ccs_fechayhora</a:AttributeName>
                    <a:Operator>OnOrAfter</a:Operator>
                    <a:Values xmlns:c=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">
                      <c:anyType i:type=\"d:date\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\">2016-12-03T10:30:00Z</c:anyType>
                    </a:Values>
                  </a:ConditionExpression>







                </a:Conditions>
                <a:FilterOperator>And</a:FilterOperator>
                <a:Filters />
                <a:IsQuickFindFilter>false</a:IsQuickFindFilter>
              </a:Criteria>
              <a:Distinct>false</a:Distinct>
              <a:EntityName>ccs_transactions</a:EntityName>
              <a:LinkEntities />
              
               <a:Orders>
                <a:OrderExpression>
                  <a:AttributeName>ccs_fechayhora</a:AttributeName>
                  <a:OrderType>Descending</a:OrderType>
                </a:OrderExpression>
              </a:Orders>
              <a:PageInfo>
                <a:Count>0</a:Count>
                <a:PageNumber>0</a:PageNumber>
                <a:PagingCookie i:nil=\"true\" />
                <a:ReturnTotalRecordCount>false</a:ReturnTotalRecordCount>
              </a:PageInfo>
              <a:NoLock>false</a:NoLock>
            </b:value>
          </a:KeyValuePairOfstringanyType>
        </a:Parameters>
        <a:RequestId i:nil=\"true\" />
        <a:RequestName>RetrieveMultiple</a:RequestName>
      </request>
    </Execute>
  </s:Body>
";
	$executeSoap = new CrmExecuteSoap ();
	$response = $executeSoap->ExecuteSOAPRequest ( $authHeader, $xml, $url );
	//var_dump($response);
	$responsedom = new DomDocument ();
	$responsedom->loadXML ( $response );
	$result=array();


$counter=-1;


		$values = $responsedom->getElementsbyTagName ( "KeyValuePairOfstringanyType" );
		
		 foreach ( $values as $value ) {
			 	if($value->firstChild->textContent!="EntityCollection"){
			 		if($value->firstChild->textContent=="statecode"){$counter++;}
			 	
			 	$result[$counter][$value->firstChild->textContent]=$value->lastChild->textContent;
			 	
			 }
		 }

	


	
	return $result;
}




function CrmGetMovements($authHeader, $id, $url) {

$xml="
<s:Body>
    <Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">
      <request i:type=\"a:RetrieveMultipleRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\">
        <a:Parameters xmlns:b=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">
          <a:KeyValuePairOfstringanyType>
            <b:key>Query</b:key>
            <b:value i:type=\"a:QueryExpression\">
              <a:ColumnSet>
                <a:AllColumns>true</a:AllColumns>
              </a:ColumnSet>
              <a:Criteria>
                <a:Conditions>
                  <a:ConditionExpression>
                    <a:AttributeName>ccs_cliente</a:AttributeName>
                    <a:Operator>Equal</a:Operator>
                    <a:Values xmlns:c=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">
                      <c:anyType i:type=\"d:string\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\">".$id."</c:anyType>
                    </a:Values>
                  </a:ConditionExpression>



                  <a:ConditionExpression>
                    <a:AttributeName>ccs_fechadelmovimiento</a:AttributeName>
                    <a:Operator>OnOrAfter</a:Operator>
                    <a:Values xmlns:c=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">
                      <c:anyType i:type=\"d:date\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\">2016-12-03T10:30:00Z</c:anyType>
                    </a:Values>
                  </a:ConditionExpression>


                </a:Conditions>
                <a:FilterOperator>And</a:FilterOperator>
                <a:Filters />
                <a:IsQuickFindFilter>false</a:IsQuickFindFilter>
              </a:Criteria>
              <a:Distinct>false</a:Distinct>
              <a:EntityName>ccs_movimientodeproductos</a:EntityName>
              <a:LinkEntities />
              
               <a:Orders>
                <a:OrderExpression>
                  <a:AttributeName>ccs_fechadelmovimiento</a:AttributeName>
                  <a:OrderType>Descending</a:OrderType>
                </a:OrderExpression>
              </a:Orders>
              <a:PageInfo>
                <a:Count>0</a:Count>
                <a:PageNumber>0</a:PageNumber>
                <a:PagingCookie i:nil=\"true\" />
                <a:ReturnTotalRecordCount>false</a:ReturnTotalRecordCount>
              </a:PageInfo>
              <a:NoLock>false</a:NoLock>
            </b:value>
          </a:KeyValuePairOfstringanyType>
        </a:Parameters>
        <a:RequestId i:nil=\"true\" />
        <a:RequestName>RetrieveMultiple</a:RequestName>
      </request>
    </Execute>
  </s:Body>
";
	$executeSoap = new CrmExecuteSoap ();
	$response = $executeSoap->ExecuteSOAPRequest ( $authHeader, $xml, $url );
	
	$responsedom = new DomDocument ();
	
	$responsedom->loadXML ( $response );
	
	
	$result=array();


$counter=-1;


		$values = $responsedom->getElementsbyTagName ( "KeyValuePairOfstringanyType" );
		
		 foreach ( $values as $value ) {
			 	if($value->firstChild->textContent!="EntityCollection"){
			 		if($value->firstChild->textContent=="ccs_monto"){$counter++;}
			 	
			 	$result[$counter][$value->firstChild->textContent]=$value->lastChild->textContent;
			 	
			 }
		 }

	


	
	return $result;
}




function CrmGetBonifications($authHeader, $id, $url) {

$xml="
<s:Body>
    <Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">
      <request i:type=\"a:RetrieveMultipleRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\">
        <a:Parameters xmlns:b=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">
          <a:KeyValuePairOfstringanyType>
            <b:key>Query</b:key>
            <b:value i:type=\"a:QueryExpression\">
              <a:ColumnSet>
                <a:AllColumns>true</a:AllColumns>
              </a:ColumnSet>
              <a:Criteria>
                <a:Conditions>
                  <a:ConditionExpression>
                    <a:AttributeName>ccs_cliente</a:AttributeName>
                    <a:Operator>Equal</a:Operator>
                    <a:Values xmlns:c=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">
                      <c:anyType i:type=\"d:string\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\">".$id."</c:anyType>
                    </a:Values>
                  </a:ConditionExpression>


                  <a:ConditionExpression>
                    <a:AttributeName>ccs_tipodetransaccion</a:AttributeName>
                    <a:Operator>NotEqual</a:Operator>
                    <a:Values xmlns:c=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">
                      <c:anyType i:type=\"d:string\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\">2</c:anyType>
                    </a:Values>
                  </a:ConditionExpression>


                  <a:ConditionExpression>
                    <a:AttributeName>ccs_fechayhora</a:AttributeName>
                    <a:Operator>OnOrAfter</a:Operator>
                    <a:Values xmlns:c=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">
                      <c:anyType i:type=\"d:date\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\">2016-12-03T10:30:00Z</c:anyType>
                    </a:Values>
                  </a:ConditionExpression>





                  <a:ConditionExpression>
                    <a:AttributeName>ccs_puntosasignados</a:AttributeName>
                    <a:Operator>Equal</a:Operator>
                    <a:Values xmlns:c=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">
                      <c:anyType i:type=\"d:string\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\">true</c:anyType>
                    </a:Values>
                  </a:ConditionExpression>




                </a:Conditions>
                <a:FilterOperator>And</a:FilterOperator>
                <a:Filters />
                <a:IsQuickFindFilter>false</a:IsQuickFindFilter>
              </a:Criteria>
              <a:Distinct>false</a:Distinct>
              <a:EntityName>ccs_transactions</a:EntityName>
              <a:LinkEntities />
              
               <a:Orders>
                <a:OrderExpression>
                  <a:AttributeName>ccs_fechayhora</a:AttributeName>
                  <a:OrderType>Descending</a:OrderType>
                </a:OrderExpression>
              </a:Orders>
              <a:PageInfo>
                <a:Count>0</a:Count>
                <a:PageNumber>0</a:PageNumber>
                <a:PagingCookie i:nil=\"true\" />
                <a:ReturnTotalRecordCount>false</a:ReturnTotalRecordCount>
              </a:PageInfo>
              <a:NoLock>false</a:NoLock>
            </b:value>
          </a:KeyValuePairOfstringanyType>
        </a:Parameters>
        <a:RequestId i:nil=\"true\" />
        <a:RequestName>RetrieveMultiple</a:RequestName>
      </request>
    </Execute>
  </s:Body>
";
	$executeSoap = new CrmExecuteSoap ();
	$response = $executeSoap->ExecuteSOAPRequest ( $authHeader, $xml, $url );
	//var_dump($response);
	$responsedom = new DomDocument ();
	$responsedom->loadXML ( $response );
	$result=array();


$counter=-1;


		$values = $responsedom->getElementsbyTagName ( "KeyValuePairOfstringanyType" );
		
		 foreach ( $values as $value ) {
			 	if($value->firstChild->textContent!="EntityCollection"){
			 		if($value->firstChild->textContent=="statecode"){$counter++;}
			 	
			 	$result[$counter][$value->firstChild->textContent]=$value->lastChild->textContent;
			 	
			 }
		 }

	


	
	return $result;
}



function CrmGetProduct($authHeader, $id, $url) {

$xml="
<s:Body>
    <Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">
      <request i:type=\"a:RetrieveMultipleRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\">
        <a:Parameters xmlns:b=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">
          <a:KeyValuePairOfstringanyType>
            <b:key>Query</b:key>
            <b:value i:type=\"a:QueryExpression\">
              <a:ColumnSet>
                <a:AllColumns>true</a:AllColumns>
              </a:ColumnSet>
              <a:Criteria>
                <a:Conditions>
                  <a:ConditionExpression>
                    <a:AttributeName>productid</a:AttributeName>
                    <a:Operator>Equal</a:Operator>
                    <a:Values xmlns:c=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">
                      <c:anyType i:type=\"d:string\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\">".$id."</c:anyType>
                    </a:Values>
                  </a:ConditionExpression>

                </a:Conditions>
                <a:FilterOperator>And</a:FilterOperator>
                <a:Filters />
                <a:IsQuickFindFilter>false</a:IsQuickFindFilter>
              </a:Criteria>
              <a:Distinct>false</a:Distinct>
              <a:EntityName>product</a:EntityName>
              <a:LinkEntities />
              
               <a:Orders/>
              <a:PageInfo>
                <a:Count>0</a:Count>
                <a:PageNumber>0</a:PageNumber>
                <a:PagingCookie i:nil=\"true\" />
                <a:ReturnTotalRecordCount>false</a:ReturnTotalRecordCount>
              </a:PageInfo>
              <a:NoLock>false</a:NoLock>
            </b:value>
          </a:KeyValuePairOfstringanyType>
        </a:Parameters>
        <a:RequestId i:nil=\"true\" />
        <a:RequestName>RetrieveMultiple</a:RequestName>
      </request>
    </Execute>
  </s:Body>
";
	$executeSoap = new CrmExecuteSoap ();
	$response = $executeSoap->ExecuteSOAPRequest ( $authHeader, $xml, $url );
	//var_dump($response);
	$responsedom = new DomDocument ();
	$responsedom->loadXML ( $response );
	$result=array();


$counter=-1;


		$values = $responsedom->getElementsbyTagName ( "KeyValuePairOfstringanyType" );
		
		 foreach ( $values as $value ) {
			 	if($value->firstChild->textContent!="EntityCollection"){
			 		if($value->firstChild->textContent=="statecode"){$counter++;}
			 	
			 	$result[$counter][$value->firstChild->textContent]=$value->lastChild->textContent;
			 	
			 }
		 }

	


	
	return $result;
}



function CrmGetBodega($authHeader, $id, $url) {

$xml="
<s:Body>
    <Execute xmlns=\"http://schemas.microsoft.com/xrm/2011/Contracts/Services\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">
      <request i:type=\"a:RetrieveMultipleRequest\" xmlns:a=\"http://schemas.microsoft.com/xrm/2011/Contracts\">
        <a:Parameters xmlns:b=\"http://schemas.datacontract.org/2004/07/System.Collections.Generic\">
          <a:KeyValuePairOfstringanyType>
            <b:key>Query</b:key>
            <b:value i:type=\"a:QueryExpression\">
              <a:ColumnSet>
                <a:AllColumns>true</a:AllColumns>
              </a:ColumnSet>
              <a:Criteria>
                <a:Conditions>
                  <a:ConditionExpression>
                    <a:AttributeName>ccs_bodegaid</a:AttributeName>
                    <a:Operator>Equal</a:Operator>
                    <a:Values xmlns:c=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">
                      <c:anyType i:type=\"d:string\" xmlns:d=\"http://www.w3.org/2001/XMLSchema\">".$id."</c:anyType>
                    </a:Values>
                  </a:ConditionExpression>

                </a:Conditions>
                <a:FilterOperator>And</a:FilterOperator>
                <a:Filters />
                <a:IsQuickFindFilter>false</a:IsQuickFindFilter>
              </a:Criteria>
              <a:Distinct>false</a:Distinct>
              <a:EntityName>ccs_bodega</a:EntityName>
              <a:LinkEntities />
              
               <a:Orders/>
              <a:PageInfo>
                <a:Count>0</a:Count>
                <a:PageNumber>0</a:PageNumber>
                <a:PagingCookie i:nil=\"true\" />
                <a:ReturnTotalRecordCount>false</a:ReturnTotalRecordCount>
              </a:PageInfo>
              <a:NoLock>false</a:NoLock>
            </b:value>
          </a:KeyValuePairOfstringanyType>
        </a:Parameters>
        <a:RequestId i:nil=\"true\" />
        <a:RequestName>RetrieveMultiple</a:RequestName>
      </request>
    </Execute>
  </s:Body>
";
	$executeSoap = new CrmExecuteSoap ();
	$response = $executeSoap->ExecuteSOAPRequest ( $authHeader, $xml, $url );
	//var_dump($response);
	$responsedom = new DomDocument ();
	$responsedom->loadXML ( $response );
	$result=array();


$counter=-1;


		$values = $responsedom->getElementsbyTagName ( "KeyValuePairOfstringanyType" );
		
		 foreach ( $values as $value ) {
			 	if($value->firstChild->textContent!="EntityCollection"){
			 		if($value->firstChild->textContent=="statecode"){$counter++;}
			 	
			 	$result[$counter][$value->firstChild->textContent]=$value->lastChild->textContent;
			 	
			 }
		 }

	


	
	return $result;
}





 ?>