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

	}	
}