<?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','_TotalTravelCost0','_ChurchName','_ContactTownState','_Numberofdaysofhotelneeded','_Milagedistancebothways','_ShowAmountindollars','_TotalHotelCost0','_TotalTravelCost0','_ShowDiscount','_TotalShowCost','_Subtotal1','_Subtotal2','_Subtotal3','_Subtotal4','_Halfofshowcost','_ShowNotes','_SizeofChurchonanAverageWeekend','_Howlonghaveyouworkedthere','_youeverbroughtinachildrensministryspeaker','_Doyouhaveanyannuallargeevents','_WhatTypeofEventsDoYouDo','_DoYouDoLargeEvents','_DoyouhaveUpwardSports','_Whatisyourdenominationalaffiliation','_Howdoesyourchurchmakedecisions');
	$contactQuery = array('Id'=>$contactId);
	
    $contactdetails = $app->dsQuery("Contact",1,0,$contactQuery,$contactReturnFields);	
    file_put_contents(dirname(__FILE__) . '/logs/contact-overwrites-opportunity.txt', $contactdetails, FILE_APPEND);
    
	if(count($contactdetails))
	{
		$callbackdate = $contactdetails[0]['_CallBackDate'];
		$totaltravelcost = $contactdetails[0]['_TotalTravelCost0'];
		$churchname = $contactdetails[0]['_ChurchName'];
		$contacttownstate = $contactdetails[0]['_ContactTownState'];
		$numberofdaysofhotelneeded = $contactdetails[0]['_Numberofdaysofhotelneeded'];
		$milagedistancebothways = $contactdetails[0]['_Milagedistancebothways'];
		$showamountindollars = $contactdetails[0]['_ShowAmountindollars'];
		$totalhotelCost = $contactdetails[0]['_TotalHotelCost0'];
		$totaltravelCost = $contactdetails[0]['_TotalTravelCost0'];
		$showdiscount = $contactdetails[0]['_ShowDiscount'];
		$totalshowCost = $contactdetails[0]['_TotalShowCost'];
		$subtotal1 = $contactdetails[0]['_Subtotal1'];
		$subtotal2 = $contactdetails[0]['_Subtotal2'];
		$subtotal3 = $contactdetails[0]['_Subtotal3'];
		$subtotal4 = $contactdetails[0]['_Subtotal4'];
		$halfofshowcost = $contactdetails[0]['_Halfofshowcost'];
		$shownotes = $contactdetails[0]['_ShowNotes'];
		$sizeofchurchonanaverageweekend = $contactdetails[0]['_SizeofChurchonanAverageWeekend'];
		$howlonghaveyouworkedthere = $contactdetails[0]['_Howlonghaveyouworkedthere'];
		$youeverbroughtinachildrensministryspeaker = $contactdetails[0]['_youeverbroughtinachildrensministryspeaker'];
		$doyouhaveanyannuallargeevents = $contactdetails[0]['_Doyouhaveanyannuallargeevents'];
		$whattypeofeventsdoyoudo = $contactdetails[0]['_WhatTypeofEventsDoYouDo'];
		$doyoudolargeevents = $contactdetails[0]['_DoYouDoLargeEvents'];
		$doyouhaveupwardsports = $contactdetails[0]['_DoyouhaveUpwardSports'];
		$whatisyourdenominationalaffiliation = $contactdetails[0]['_Whatisyourdenominationalaffiliation'];
		$howdoesyourchurchmakedecisions = $contactdetails[0]['_Howdoesyourchurchmakedecisions'];
		
		$saveString = "[" . date("Y-m-d H:i:s") . "] contactId: " . $contactId . " callbackdate: " . $callbackdate. " totaltravelcost: " . $totaltravelcost. "\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,'_TotalTravelCost'=>$totaltravelcost,'_ChurchName'=>$churchname,'_ContactTownState'=>$contacttownstate,'_Numberofdaysofhotelneeded'=>$numberofdaysofhotelneeded,'_Milagedistancebothways'=>$milagedistancebothways,'_ShowAmountindollars'=>$showamountindollars,'_TotalHotelCost'=>$totalhotelCost,'_TotalTravelCost'=>$totaltravelCost,'_ShowDiscount'=>$showdiscount,'_TotalShowCost'=>$totalshowCost,'_Subtotal1'=>$subtotal1,'_Subtotal2'=>$subtotal2,'_Subtotal3'=>$subtotal3,'_Subtotal4'=>$subtotal4,'_Halfofshowcost'=>$halfofshowcost'_ShowNotes'=>$shownotes,'_SizeofChurchonanAverageWeekend'=>$sizeofchurchonanaverageweekend,'_Howlonghaveyouworkedthere'=>$howlonghaveyouworkedthere,'_youeverbroughtinachildrensministryspeaker'=>$youeverbroughtinachildrensministryspeaker,'_Doyouhaveanyannuallargeevents'=>$doyouhaveanyannuallargeevents,'WhatTypeOfEventsDoYouDo'=>$whattypeofeventsdoyoudo,'DoYouDoLargeEvents'=>$doyoudolargeevents,'_DoyouhaveUpwardSports'=>$doyouhaveupwardsports,'_Whatisyourdenominationalaffiliation'=>$whatisyourdenominationalaffiliation,'_Howdoesyourchurchmakedecisions'=>$howdoesyourchurchmakedecisions);
				$app->dsUpdate("Lead", $opportunityId, $opportunitydata);
			}
		}
		
	}
}