<?php

	require_once("../include/config.php");	
	require_once("../include/startup.php");

$obj  			= new Client();
$factory_obj   = new ClientFactories();
$branch_obj  	= new ClientBranches();
$contact_obj  	= new ClientContacts();
$objinfo  		= new AllTables();


if($_POST['SUBMIT'])
{ 
//echo"<pre>";
			//print_r($_POST);
			//die();
			
	 //Information
	 	$memberid 									= $_POST['txtmemberid'];
	 	$NewProduct['memberid']					= $memberid;
	 	$NewProduct['companyname']				= sanitize($_POST['txtcompanyname'],SQL);
	 	
	 	//Address		
		$NewProduct['Addline1'] 					= sanitize($_POST['Addline1'],SQL);
	 	$NewProduct['Addline2'] 					= sanitize($_POST['txtAddline1'],SQL);
	 	$NewProduct['head_CityName'] 				= sanitize($_POST['txthead_CityName1'],SQL);
	 	$state 											= explode("-",sanitize($_POST['txtstate'],SQL));
	 	$NewProduct['head_Stateid1'] 				= $state[0];
	 	$NewProduct['head_StateidName'] 			= $state[1];
	 	$Country											= explode("-",sanitize($_POST['txtcountry'],SQL));
	 	$NewProduct['Country'] 						= $Country[1];
		$NewProduct['head_Pin'] 					= sanitize($_POST['txtpincode1'],SQL);
		
	
	   //Contact Information
		$NewProduct['PersonName1'] 	      		= sanitize($_POST['txtclientname_1'],SQL);
	 	$NewProduct['head_TelNo'] 	      		= sanitize($_POST['txthead_TelNo_1'],SQL);
		$NewProduct['head_mobNo'] 	     		 	= sanitize($_POST['head_mobNo_1'],SQL);
		$NewProduct['head_Email'] 	      		= sanitize($_POST['head_Email_1'],SQL);
	
	
	   //Information
	 	$NewProduct['gstapplicable']					= $_POST['txtgstapplicable'];
	 	$NewProduct['gstno'] 							= sanitize($_POST['txtgstno1'],SQL);
	 	$NewProduct['Tan_No'] 							= sanitize($_POST['txttanno'],SQL);
	 	$NewProduct['panno']								= sanitize($_POST['txtpanno'],SQL);
	 	$NewProduct['memberapprovalstatus']			= sanitize($_POST['txtstatus'],SQL);
		//echo"<pre>";
				//print_r($NewProduct)
		 $addresult     = $obj -> AddClient($NewProduct);
 
 		$sql 				= "select * from mstclients where memberid = $memberid order by clientcompanyid desc limit 0,1 ";
 		$sqllist 		= $obj -> getClient('','',$sql);
 		$companyid		= $sqllist[0]['clientcompanyid'];
 		
 		$contact_count = $_POST['entered_contact_count'];
		$factory_count = $_POST['entered_factory_count'];
		$branch_count  = $_POST['entered_branch_count'];
		if($_POST['txtclientname_1'] != '')
		{
			for($j=1;$j<=$contact_count;$j++)
			{ 	
				$contact['clientcompanyid'] 				= $companyid;
				$contact['contact_parson_name1'] 					= sanitize($_POST['txtclientname_'.$j],SQL);
				$contact['contact_telno'] 			= sanitize($_POST['txthead_TelNo_'.$j],SQL);
			
				$contact['contactfaxno'] 						= sanitize($_POST['head_FaxNo_'.$j],SQL);
				$contact['contactmob'] 					= sanitize($_POST['head_mobNo_'.$j],SQL);
				
				//$contact['factorystate'] 					= $_POST['txtfactstate_'.$i];
				//$contact['factorytelno'] 					= trim(sanitize($_POST['txtfacttel_'.$i],SQL));
				//$contact['factoryfaxno'] 					= trim(sanitize($_POST['txtfactfax_'.$i],SQL));
				//$contact['factoryemail'] 					= trim(sanitize($_POST['txtfactemail_'.$i],SQL));
				$contact['contactemail'] 					= sanitize($_POST['head_Email_'.$j],SQL);
				$contact['contactwebaddress'] 					= sanitize($_POST['txtWebsite_'.$j],SQL);
				//$test				= $_POST['txtclientname_'.$j];
//echo"<pre>";
			//	print_r($contact);
			//	echo $contact;
				//die();
				$addcontact     = $contact_obj -> AddClientContacts($contact);
				//$addcontact = $objinfo-> AddInfo($contact,"client_contacts");
			}	
			
			
		}
//$test				= $_POST['txtclientname_1'];
	
				//echo $test;
				//echo "test";
				//die();
		if($_POST['txtfactadd_1'] != '')
		{
			for($i=1;$i<=$factory_count;$i++)
			{ 	
				$factarray['clientcompanyid'] 				= $companyid;
				$factarray['factoryaddress'] 					= trim(sanitize($_POST['txtfactadd_'.$i],SQL));
				$factarray['factoryaddressline2'] 			= trim(sanitize($_POST['txtfactadd2_'.$i],SQL));
			
				$factarray['factorycity'] 						= trim(sanitize($_POST['txtfactcity_'.$i],SQL));
				$factarray['factorypincode'] 					= trim(sanitize($_POST['txtfactpin_'.$i],SQL));
				
				//$factarray['factorystate'] 					= $_POST['txtfactstate_'.$i];
				//$factarray['factorytelno'] 					= trim(sanitize($_POST['txtfacttel_'.$i],SQL));
				//$factarray['factoryfaxno'] 					= trim(sanitize($_POST['txtfactfax_'.$i],SQL));
				//$factarray['factoryemail'] 					= trim(sanitize($_POST['txtfactemail_'.$i],SQL));
				$factarray['factorygstno'] 					= trim(sanitize($_POST['txtfactgstno_'.$i],SQL));
				//print_r($factarray);
				
				$addfactory     = $factory_obj -> AddClientFactories($factarray);
			}	
		}
		
		if($_POST['txtbranchadd_1'] != '')
		{		
			for($i=1;$i<=$branch_count;$i++) 
			{
				$brancharray['clientcompanyid'] 			= $companyid;
				$brancharray['branchaddress'] 			= trim(sanitize($_POST['txtbranchadd_'.$i],SQL));
				$brancharray['branchaddressline2'] 		= trim(sanitize($_POST['txtbranchadd2_'.$i],SQL));
				//$brancharray['branchaddressline3'] 	= trim(sanitize($_POST['txtbranchadd3_'.$i],SQL));
				$brancharray['branchcity'] 				= trim(sanitize($_POST['txtbranchcity_'.$i],SQL));
				$brancharray['branchpincode'] 			= trim(sanitize($_POST['txtbranchpin_'.$i],SQL));
				//$branchstateinfo								= explode('-',$_POST['txtbranchstate_'.$i]);
				//$branchstateid									= $branchstateinfo[0];
				//$branchstatename								= $branchstateinfo[1];	
				//$brancharray['branchstateid'] 			= $branchstateid;	
				////$brancharray['branchstate'] 				= $branchstatename;
			//	$brancharray['branchtelno'] 				= trim(sanitize($_POST['txtbranchtel_'.$i],SQL));
		//brancharray['branchfaxno'] 				= trim(sanitize($_POST['txtbranchfax_'.$i],SQL));
			//	$brancharray['branchemail'] 				= trim(sanitize($_POST['txtbranchemail_'.$i],SQL));
				$brancharray['branchgstno'] 				= trim(sanitize($_POST['txtbranch_gstno_'.$i],SQL));
				 
				$add_branch = $branch_obj -> AddClientBranches($brancharray);
			 } 
		}		
				if($addresult)
		 		{
					$redirect_url = DOC_ROOT."admin/viewclient.php";
					header("Location: $redirect_url");
	    			exit;
				 }
	
}
?>