<?php

include('library/src/isdk.php');

$app = new iSDK;
$app->cfgCon("ifconnection");

$contactId = $_POST['contactId'];



if($contactId)
{
	//get the latest lead for the contactId

	$returnFields  = array('_CallBackDate0','_TotalTravelCost');
	$query         = array('ContactID' => $contactId);	
	$opportunities = $app->dsQueryOrderBy("Lead",1,0,$query,$returnFields,'Id',false);
	
	if(count($opportunities))
	{
		$callbackdate = $opportunities[0]['_CallBackDate0'];
		$totaltravelcost = $opportunities[0]['_TotalTravelCost'];
		
		$saveString = "[" . date("Y-m-d H:i:s") . "] contactId: " . $contactId . " callbackdate: " . $callbackdate. " totaltravelcost: " . $totaltravelcost. "\r\n";
		file_put_contents(dirname(__FILE__) . '/logs/opportunity-overwrites-contact.txt', $saveString, FILE_APPEND);
		
		//deal with the contact
		$contactdata = array('_CallBackDate'=>$callbackdate,'_TotalTravelCost0'=>$totaltravelcost);
		$app->dsUpdate('Contact',$contactId,$contactdata);

	}	
}