<?php

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

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

$contactId = $_POST['contactId'];



if($contactId)
{
	//get the fields from the contact
	$contactReturnFields = array('_CallBackDate');
	$contactQuery = array('Id'=>$contactId);
	
    $contactdetails = $app->dsQuery("Contact",1,0,$contactQuery,$contactReturnFields);	
    
	if(count($contactdetails))
	{
		$callbackdate = $contactdetails[0]['_CallBackDate'];

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