<?php
include_once('vtwsclib/Vtiger/WSClient.php');
// Vtiger license open source/cloud
// Vtiger url
//User
//Key
//fetch vtifger ticket
//fetch vtiger contacts
//create contact in Vtiger
//Status
//open source demo
	$vtiger_lic = 1; //1= open source, 2 = cloud
	$url = "http://iamdemo.com/vision4/amoln/vtiger/webservice.php";
	$user = "admin";
	$key = "RYTQy9BCYR7EKKSS";

/*	
	$vtiger_lic = 2; //1= open source, 2 = cloud
	$url = 'https://soft95.od2.vtiger.com/webservice.php';
	$user = "amolvision@outlook.com"; //"admin";
	$key = "S5T2eIUrixpjokC"; //"gvzgVel9Fu8AHcSn";
*/
/* 
	$vtiger_lic = 2; //1= open source, 2 = cloud
	$url = 'https://soft95.od2.vtiger.com/webservice.php';
	$user = "tejas@gmail.com";
	$key = "HrJi6gvOb86sfWsA"; */

/*//clients credential
// Password   Jaspal@707 
	$vtiger_lic = 1; //1= open source, 2 = cloud
	$url = 'https://securecrm.biz/educationalliance/webservice.php';
	$user = "Admin";
	$key ="4p1mlwvcslsqnabu";

*/
 


 $moduleidx	=	array();
 if($vtiger_lic==1){
	$moduleidx["Campaigns"] = "1x";
	$moduleidx["Vendors"] = "2x";
	$moduleidx["Faq"] = "3x";
	$moduleidx["Quotes"] = "4x";
	$moduleidx["PurchaseOrder"] = "5x";
	$moduleidx["SalesOrder"] = "6x";
	$moduleidx["Invoice"] = "7x";
	$moduleidx["PriceBooks"] = "8x";
	$moduleidx["Calendar"] = "9x";
	$moduleidx["Leads"] = "10x";
	$moduleidx["Accounts"] = "11x";
	$moduleidx["Contacts"] = "12x";
	$moduleidx["Potentials"] = "13x";
	$moduleidx["Products"] = "14x";
	$moduleidx["Documents"] = "15x";
	$moduleidx["Emails"] = "16x";
	$moduleidx["HelpDesk"] = "17x";
	$moduleidx["Events"] = "18x";
	$moduleidx["Users"] = "19x";
	$moduleidx["Services"] = "24x";
	$moduleidx["ServiceContracts"] = "25x";
	$moduleidx["PBXManager"] = "26x";
	$moduleidx["Assets"] = "27x";
	$moduleidx["ModComments"] = "28x";
	$moduleidx["SMSNotifier"] = "29x";
	$moduleidx["ProjectMilestone"] = "30x";
	$moduleidx["ProjectTask"] = "31x";
	$moduleidx["Project"] = "32x";
	$moduleidx["Groups"] = "20x";
	$moduleidx["Currency"] = "21x";
	$moduleidx["DocumentFolders"] = "22x";
	$moduleidx["CompanyDetails"] = "23x";
	$moduleidx["LineItem"] = "33x";
	$moduleidx["Tax"] = "34x";
	$moduleidx["ProductTaxes"] = "35x";
 }else if($vtiger_lic==2){
	$moduleidx["Calendar"] = "1x";
	$moduleidx["Leads"] = "2x";
	$moduleidx["Accounts"] = "3x";
	$moduleidx["Contacts"] = "4x";
	$moduleidx["Potentials"] = "5x";
	$moduleidx["Products"] = "6x";
	$moduleidx["Documents"] = "7x";
	$moduleidx["Emails"] = "8x";
	$moduleidx["Faq"] = "10x";
	$moduleidx["Vendors"] = "11x";
	$moduleidx["PriceBooks"] = "12x";
	$moduleidx["Quotes"] = "13x";
	$moduleidx["PurchaseOrder"] = "14x";
	$moduleidx["SalesOrder"] = "15x";
	$moduleidx["Invoice"] = "16x";
	$moduleidx["Campaigns"] = "17x";
	$moduleidx["Events"] = "18x";
	$moduleidx["Users"] = "19x";
	$moduleidx["ServiceContracts"] = "23x";
	$moduleidx["PBXManager"] = "24x";
	$moduleidx["Services"] = "25x";
	$moduleidx["Assets"] = "27x";
	$moduleidx["ModComments"] = "28x";
	$moduleidx["ProjectMilestone"] = "29x";
	$moduleidx["ProjectTask"] = "30x";
	$moduleidx["Project"] = "31x";
	$moduleidx["EmailCampaigns"] = "37x";
	$moduleidx["SLA"] = "38x";
	$moduleidx["Cases"] = "39x";
	$moduleidx["Olark"] = "40x";
	$moduleidx["PrintTemplates"] = "41x";
	$moduleidx["EventForms"] = "42x";
	$moduleidx["Groups"] = "20x";
	$moduleidx["Currency"] = "21x";
	$moduleidx["DocumentFolders"] = "22x";
	$moduleidx["CompanyDetails"] = "26x";
	$moduleidx["LineItem"] = "33x";
	$moduleidx["Tax"] = "34x";
	$moduleidx["ProductTaxes"] = "35x";
 }else{ 
	die("Set Vtiger License");
 }
 
 
 
 
 
 
$client = new Vtiger_WSClient($url);
$login = $client->doLogin($user, $key);
if(!$login) echo 'Login Failed';
else { 
    //echo 'Login Successful';
	$modules = $client->doListTypes();
	
	##List of modules
	/*foreach($modules as $modulename => $moduleinfo) {
		echo "ModuleName: $modulename\n<BR>";
	}
	*/
	
	
	## Describe single module by name
	$module_ticket = $client->doDescribe('Potentials');
	
	echo '<pre>';print_r($module_ticket); echo '</pre>'; 
	/**/
	## Describe All modules	
	/*foreach($modules as $modulename => $moduleinfo) {
		'<br/><br/>modulename :'.$module = $moduleinfo['name'];
		$describe = $client->doDescribe($module);
		//echo '<br/> createable:'.$cancreate = $describe['createable'];
		//echo '<br/> updateable:'.$canupdate = $describe['updateable'];
		//echo '<br/> deleteable:'.$candelete = $describe['deleteable'];
		//echo '<br/> retrieveable:'.$canread = $describe['retrieveable'];
		//echo '<br/> fields:'.$fields = $describe['fields'];
		//echo '<br/>Module id: '.$describe['idPrefix'].' / modulename :'.$module.'<br/>'; 
		
		//echo '$moduleidx["'.$module.'"] = "'.$describe['idPrefix'].'x";<br/>';
		
		//if($modulename=='HelpDesk' ){
			echo '<pre>';print_r($describe); echo '</pre>'; 
		//}
	}*/
	
	
	##Creating new records in the module can be done using this method. 
	##doCreate(module, valuesmap)
	
	/**************************************************************************
		=> Here used only mandatory fields if extra foelds require then refer $describe Array 
		
		-Leads
			$arr = array('firstname'=>'tejas','lastname'=>'JADHAV', 'company'=>'Vision pro'); 
		
		-Contacts 
			$arr = array('lastname'=>'Test description')
		
		-Cases  
			$arr = array("description"=>"descriptiondescription","title"=>"titletitle","casestatus"=>"New","casepriority"=>"Urgent","contact_id"=>$moduleidx["Contacts"]."247");
		
		-Project 
			$arr = array("projectname"=>"ProjectNName","assigned_user_id"=>$moduleidx["Users"]."6");
		
		-Accounts(organization) 
			$arr = array("accountname"=>"MY ACCOUNT NAME","assigned_user_id"=>$moduleidx["Users"]."6");
		
		-Assets  
			$arr = array("assetname"=>"Asset Name","serialnumber"=>"123456789","product"=>$moduleidx["Products"]."268","assetstatus"=>"In Service","assigned_user_id"=>$moduleidx["Users"]."6","dateinservice"=>"19-08-2016","datesold"=>"19-08-2016","account"=>$moduleidx["Accounts"]."45");
		
		-Vendors
			$arr = array("vendorname"=>"Vendor Name","assigned_user_id"=>$moduleidx["Users"]."6");
		
		-Products
			$arr = array("productname"=>"product Name","assigned_user_id"=>$moduleidx["Users"]."6");	
		
		-Services
			$arr = array("servicename"=>"servicename Name","assigned_user_id"=>$moduleidx["Users"]."6");	
		
		-ServiceContracts
			$arr = array("subject"=>"subject Name","assigned_user_id"=>$moduleidx["Users"]."6");	
		
		-Faq
			$arr = array("question"=>"question Name","faq_answer"=>"faq_answer","faqstatus"=>"faqstatus");	
		
		-Campaigns
			$arr = array("campaignname"=>"campaignname Name","closingdate"=>"19-08-2016","assigned_user_id"=>$moduleidx["Users"]."6");
		
		-Groups
			$arr = array("groupname"=>"groupname Name","description"=>"description");
			
		-Potentials (Opportunities)
			$arr = array("potentialname"=>"potentialname Name","closingdate"=>"30-08-2016","sales_stage"=>"Prospecting","assigned_user_id"=>$moduleidx["Users"]."6");
		//mm-dd-yyyy	
	/********************************************************************/
	$arr = array("potentialname"=>"potentialname","closingdate"=>"09-09-2003","sales_stage"=>"Prospecting","assigned_user_id"=>"19x1");
		
	$module = 'Potentials';
	$record = $client->doCreate($module,$arr);
	if($record) {
		echo $recordid = $client->getRecordId($record['id']);
	}
	
	
	##Rerive record  doRetrieve(record)
	##record – identifier having the format <moduleid>'x'<recordid>
	// Retrieve Contacts record with id 110
	/*$record = $moduleidx["Contacts"].'247';
	$recordInfo = $client->doRetrieve($record);
	if($recordInfo) {
		$lastname = $recordInfo['lastname'];
		echo '<pre>';print_r($recordInfo);
	}
	*/
	
	##doQuery(query)
	##query – SELECT query to use for fetching the records.
	// Retrieve Accounts(organization) matching name 'vtiger'

	//Accounts , Contacts , Cases{HelpDesk} , Assets , Vendors , Products , Services , ServiceContracts ,Faq, Campaigns , Leads, Project, Invoice, PurchaseOrder, PriceBooks, ProjectTask, ProjectMilestone ,Potentials
	/*$query = "SELECT * FROM    Potentials "; 	 
	$records = $client->doQuery($query);
	if($records) {
		$columns = $client->getResultColumns($records);
		foreach($records as $record) {
			// Process record information
			echo '<pre>'; print_r($record); echo '</pre>';
		}
	}*/
	
	
	
	
	##doInvoke(method, params, type)
	
	
	
	##lastError()
	##Map containing (code, message) if the last operation failed or else false
	// Retrieve Accounts matching name 'vtiger'
	/*$query = "SELECT * FROM Accounts WHERE WRONGQUERY";
	$records = $client->doQuery($query);*/
	$wasError= $client->lastError();
	if($wasError) {
		echo $wasError['code'].':'.$wasError['message'];
	}
	
	
	##toJSON(inputString)
	
	##toJSONString(input)
	
	
	
}//login success
?>