<?php

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

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

//$_POST['contactid'] = 510;
//$_POST['stageid']   = 26;

$contactId = $_POST['contactid'];
$stageId   = $_POST['stageid'];

if($contactId)
{
	$returnFields  = array('Id', 'ContactID', 'CreatedBy', 'StageID', 'StatusID', 'UserID');
	$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('StageID'  => $stageId);
			$app->dsUpdate("Lead", $opportunityId, $opportunitydata);
		}
	}
}