<?php
include_once("../connection/setting_jaldhi_class.php");

class data
{
var $var_1;
var $var_2;
var $var_3;
var $var_4;
var $var_5;
var $var_6;
var $var_7;
var $var_8;
var $var_9;
var $var_10;
var $var_11;
var $var_12;
var $var_13;
var $var_14;
var $var_15;
var $var_16;
var $var_17;
var $var_18;
var $var_19;
var $var_20;
var $var_21;
var $var_22;
var $var_23;
var $var_24;
var $var_25;
var $var_26;
var $var_27;
var $var_28;
var $var_29;
var $var_30;
var $var_31;
var $var_32;
var $var_33;
var $var_34;
var $var_35;
var $var_36;
var $var_37;
var $var_38;
var $var_39;
var $var_40;
var $var_41;
var $var_42;
var $var_43;
var $var_44;
function getFun1(){return $this->var_1;}
function getFun2(){return $this->var_2;}
function getFun3(){return $this->var_3;}
function getFun4(){return $this->var_4;}
function getFun5(){return $this->var_5;}
function getFun6(){return $this->var_6;}
function getFun7(){return $this->var_7;}
function getFun8(){return $this->var_8;}
function getFun9(){return $this->var_9;}
function getFun10(){return $this->var_10;}
function getFun11(){return $this->var_11;}
function getFun12(){return $this->var_12;}
function getFun13(){return $this->var_13;}
function getFun14(){return $this->var_14;}
function getFun15(){return $this->var_15;}
function getFun16(){return $this->var_16;}
function getFun17(){return $this->var_17;}
function getFun18(){return $this->var_18;}
function getFun19(){return $this->var_19;}
function getFun20(){return $this->var_20;}
function getFun21(){return $this->var_21;}
function getFun22(){return $this->var_22;}
function getFun23(){return $this->var_23;}
function getFun24(){return $this->var_24;}
function getFun25(){return $this->var_25;}
function getFun26(){return $this->var_26;}
function getFun27(){return $this->var_27;}
function getFun28(){return $this->var_28;}
function getFun29(){return $this->var_29;}
function getFun30(){return $this->var_30;}
function getFun31(){return $this->var_31;}
function getFun32(){return $this->var_32;}
function getFun33(){return $this->var_33;}
function getFun34(){return $this->var_34;}
function getFun35(){return $this->var_35;}
function getFun36(){return $this->var_36;}
function getFun37(){return $this->var_37;}
function getFun38(){return $this->var_38;}
function getFun39(){return $this->var_39;}
function getFun40(){return $this->var_40;}
function getFun41(){return $this->var_41;}
function getFun42(){return $this->var_42;}
function getFun43(){return $this->var_43;}
function getFun44(){return $this->var_44;}

/*
* function used for connecting to the database
*/
function funConnect()
{
    $info = new clsSettings(); 
	$admin_site_server	= $info->admin_site;
	$admin_site_user	= $info->admin_siteuser;				
	$admin_site_passwd	= $info->admin_sitepass;
	$admin_site_db		= $info->admin_sitedatabase;
	
	$link = mysql_connect($admin_site_server, $admin_site_user, $admin_site_passwd)			
			or die ('I cannot connect to the database because: ' . mysql_error());
	
	/* checks to see if the connection actually works */ 			
	if (!$link)
	{ 
		$this->errno = 1; 
		return NULL; 
	}
	mysql_select_db($admin_site_db,$link) or die( "unable to select the database becoz of : " . mysql_error());	
	return $link;
}

function getLoginidWiseRecentFiveWorks()
{
	$connect = $this->funConnect();
	$sql = "select * from recent_work_master where LOGINID='".$_SESSION['uid']."' order by RECENT_WORKID desc limit 10";
	$res = mysql_query($sql);
	$rec = mysql_num_rows($res);
	$arr = array();
	if($rec > 0 )
	{
		while($rows = mysql_fetch_assoc($res))
		{
			$arr[] = array("work" => $rows['WORK'] , "datetime" => date("d M Y H:i",strtotime($rows['WORK_DATE'])));
		}
		return $arr;
	}
	else
	{
		return $arr;
	}
}


function getMainCompanyList()
{
	$this->funConnect();
	$query = mysql_query("select * from main_company where 1=1 order by COMPANY_NAME ");
	echo "<option value = '' >---Select from list---</option>";
	@$selCName = @$_REQUEST['selCName'];
	while($rows = mysql_fetch_assoc($query))
		{
		 $key= $rows['MCOMPANYID'];
		 $value= $rows['COMPANY_NAME'];
		 echo "<option value='".$key."' ";
		 if($key == @$selCName){ echo " selected";}
		 echo ">".$value."</option>";
		}
}

function getGenderList()
{
$state = array( 1 =>'Male' , 2 =>'Female');
$selGender = @$_REQUEST['selGender'];
echo "<option value=''>--Select from list--</option>";
	while ($array_cell = each($state)) 
	{
		$value = $array_cell['value'];
		$key = $array_cell['key'];
		
		echo "<option value=$key";
		if($key == $selGender){ echo " selected";}
		echo ">".$value."</option>";
		
	}
}

function getUserStatusList()
{
$state = array( 1 =>'Active' , 2 =>'In-Active');
$selStatus = @$_REQUEST['selStatus'];
echo "<option value=''>--Select from list--</option>";
	while ($array_cell = each($state)) 
	{
		$value = $array_cell['value'];
		$key = $array_cell['key'];
		
		echo "<option value=$key";
		if($key == $selStatus){ echo " selected";}
		echo ">".$value."</option>";
		
	}
}

function getUserStatusBasedOnID($id)
{
	if($id == 1){return 'Active';}
	if($id == 2){return 'In-Active';}
}

function getCompanyData($id,$field)
{
	$connect = $this->funConnect();
	$sql = "select ".$field." from main_company where MCOMPANYID=".$id;
	$res = mysql_query($sql);
	$rows = mysql_fetch_assoc($res);
	return $rows[$field];
}

//////////////////////////////////////////
////////////Form Code////////////////////
////////////////////////////////////////

function displayCompanyCreationList()
{
	$this->funConnect();
	echo '<div class="box-body table-responsive">';
	echo '<table id="company_list" class="table table-bordered table-striped">';
	echo '<thead>';
	echo '<tr>';
	echo '<th >Company ID</th>';
	echo '<th >Company Name</th>';
	echo '<th >Address</th>';	
	echo '<th >E-mail ID</th>';
	echo '<th >Phone No</th>';
	echo '<th >Details</th>';
	echo '</tr>';
	echo '</thead>';
	$sql = "select * from main_company where 1=1 order by COMPANY_NAME";
	$res = mysql_query($sql) or die(mysql_error());
	$rec = mysql_num_rows($res);
	$i = 1;
	if($rec == 0)
	{
		echo '<tbody>';
		echo '</tbody>';
	}
	else{
	echo '<tbody>';
		while($rows = mysql_fetch_assoc($res))
			{
				echo '<tr>';
				echo '<td >'.$rows['COMPANYID'].'</td>';
				echo '<td >'.$rows['COMPANY_NAME'].'</td>';
				echo '<td >'.$rows['ADDRESS'].'</td>';
				echo '<td >'.$rows['EMAIL_ID'].'</td>';
				echo '<td >'.$rows['PHONE_NO'].'</td>';
				echo '<td align="center" valign="middle" class="input-text"><a href="update_company_details.php?id='.$rows['MCOMPANYID'].'" title="Edit Details" ><i class="fa fa-edit "></i></a></td>';	
				echo '</tr>';
				$i++;
		}
	echo '</tbody>';
	}
	echo '</table></div>';
}

function insertCompanyDetails()
{
	$connect = $this->funConnect();
	$company_name		= addslashes(trim($_REQUEST['txtCName']));
	$address			= addslashes(trim($_REQUEST['txtAddress']));
	$company_id			= addslashes(trim($_REQUEST['txtCID']));
	$phone_no 			= addslashes(trim($_REQUEST['txtPhoneNo']));
	$emailid 			= addslashes(trim($_REQUEST['txtEmailid']));
	$txtShortName 		= addslashes(trim($_REQUEST['txtShortName']));
	$txtVendorCode 		= addslashes(trim($_REQUEST['txtVendorCode']));
	
	$msg = 0;
	mysql_query("Set Autocommit=0");
	mysql_query("START TRANSACTION");
	
	$sql = "insert into main_company (COMPANY_NAME, COMPANYID, ADDRESS, PHONE_NO, EMAIL_ID, SHORT_NAME, VENDOR_CODE) values ('".$company_name."','".$company_id."','".$address."','".$phone_no."','".$emailid."', '".$txtShortName."', '".$txtVendorCode."')";
	mysql_query($sql,$connect) or die(mysql_error());
	
	$sql1 = "insert into recent_work_master (LOGINID, WORK, WORK_DATE) values ('".$_SESSION['uid']."','Creating new company ".$company_name.".',NOW())";
	mysql_query($sql1,$connect) or die(mysql_error());
	
	if(mysql_error($connect))
	{
		$msg = 1;
		mysql_query("ROLLBACK");
	}
	else
	{
		mysql_query("commit"); 
	}
	return $msg; 
}

function viewCompanyRecords($id)
{
	$connect = $this->funConnect();
	$query = "SELECT * FROM main_company where MCOMPANYID=".$id;
	$result = mysql_query($query);
	while ($rows = mysql_fetch_assoc($result)) 
	{
		$this->var_1 = $rows['COMPANY_NAME'];
		$this->var_2 = $rows['COMPANYID'];
		$this->var_3 = $rows['ADDRESS'];
		$this->var_4 = $rows['PHONE_NO'];
		$this->var_5 = $rows['EMAIL_ID'];
		$this->var_6 = $rows['SHORT_NAME'];
		$this->var_7 = $rows['VENDOR_CODE'];
	}
}

function updateCompanyRecords()
{
	$connect				= $this->funConnect();
	$id = $_REQUEST['id'];
 	$company_name		= addslashes(trim($_REQUEST['txtCName']));
	$address			= addslashes(trim($_REQUEST['txtAddress']));
	$company_id			= addslashes(trim($_REQUEST['txtCID']));
	$phone_no 			= addslashes(trim($_REQUEST['txtPhoneNo']));
	$emailid 			= addslashes(trim($_REQUEST['txtEmailid']));
    $txtShortName 		= addslashes(trim($_REQUEST['txtShortName']));
	$txtVendorCode 		= addslashes(trim($_REQUEST['txtVendorCode']));
	
	mysql_query("Set Autocommit=0");
	mysql_query("START TRANSACTION");
		
	$msg = 0;
	$sql = "update main_company set COMPANY_NAME='".$company_name."',ADDRESS='".$address."',PHONE_NO='".$phone_no."',EMAIL_ID='".$emailid."',COMPANYID='".$company_id."',SHORT_NAME='".$txtShortName."', VENDOR_CODE='".$txtVendorCode."'  where MCOMPANYID='".$id."'";
	
	mysql_query($sql) or die(mysql_error());
	
	$sql1 = "insert into recent_work_master (LOGINID, WORK, WORK_DATE) values ('".$_SESSION['uid']."','Updating company ".$company_name.".',NOW())";
	mysql_query($sql1,$connect) or die(mysql_error());
	
	if(mysql_error($connect))
	{
		$msg = 1;
		mysql_query("ROLLBACK");
	}
	else
	{
		mysql_query("commit"); 
	}
	return $msg;
}

function displayAdminCreationList()
{
	$this->funConnect();
	echo '<div class="box-body table-responsive">';
	echo '<table id="admin_list" class="table table-bordered table-striped">';
	echo '<thead>';
	echo '<tr>';
	echo '<th align="left" valign="middle" width="10%"  >Company Name</th>';
	echo '<th align="left" valign="middle" width="10%"  >Name</th>';
	echo '<th align="left" valign="middle" width="15%"  >Address</th>';	
	echo '<th align="left" valign="middle" width="10%"  >E-mail ID</th>';
	echo '<th align="left" valign="middle" width="10%"  >User Name</th>';
	echo '<th align="left" valign="middle" width="10%"  >Password</th>';
	echo '<th align="left" valign="middle" width="10%"  >Status</th>';
	echo '<th align="center" valign="middle" width="5%" >Details</th>';
	echo '<th align="center" valign="middle" width="5%" >Pdf</th>';
	echo '</tr>';
	echo '</thead>';
	$sql = "select * from login where USER_TYPE='admin' order by CONTACT_PERSON,STATUS";
	$res = mysql_query($sql) or die(mysql_error());
	$rec = mysql_num_rows($res);
	$i = 1;
	if($rec == 0)
	{
		echo '<tbody>';
		echo '</tbody>';
	}
	else{
	echo '<tbody>';
		while($rows = mysql_fetch_assoc($res))
			{
				echo '<tr>';
				echo '<td align="left" valign="middle" class="input-text">'.$this->getCompanyData($rows['MCOMPANYID'],"COMPANY_NAME").'</td>';
				echo '<td align="left" valign="middle" class="input-text">'.$rows['CONTACT_PERSON'].'</td>';
				echo '<td align="left" valign="middle" class="input-text">'.$rows['ADDRESS'].'</td>';
				echo '<td align="left" valign="middle" class="input-text">'.$rows['EMAILID'].'</td>';
				echo '<td align="left" valign="middle" class="input-text">'.$rows['USERNAME'].'</td>';
				echo '<td align="left" valign="middle" class="input-text">'.$rows['PASSWORD'].'</td>';
				echo '<td align="left" valign="middle" class="input-text">'.$this->getUserStatusBasedOnID($rows['STATUS']).'</td>';
				echo '<td align="center" valign="middle" class="input-text"><a href="update_admin_details.php?id='.$rows['LOGINID'].'" title="Edit Details" ><i class="fa fa-edit "></i></a></td>';
				echo '<td align="center" valign="middle" class="input-text"><a title="Generate PDF for Audit Trail" href="#Pdf" onclick="getPdf('.$rows['LOGINID'].');" ><i class="fa fa-download"></i></a></td>';	
				echo '</tr>';
				$i++;
		}
	echo '</tbody>';
	}
	echo '</table></div>';
}

function insertAdminDetails()
{
	$connect = $this->funConnect();
	$company_id			= addslashes(trim($_REQUEST['selCName']));
	$c_person 			= addslashes(trim($_REQUEST['txtName']));
	$address			= addslashes(trim($_REQUEST['txtAddress']));	
	$phone_no 			= addslashes(trim($_REQUEST['txtPhoneNo']));
	$emailid 			= addslashes(trim($_REQUEST['txtEmailid']));
	$user_name 			= addslashes(trim($_REQUEST['txtUName']));
	$password 			= addslashes(trim($_REQUEST['txtPassword']));
	$status 			= addslashes(trim($_REQUEST['selStatus']));
	$selGender 			= addslashes(trim($_REQUEST['selGender']));
	
	$msg = 0;
	mysql_query("Set Autocommit=0");
	mysql_query("START TRANSACTION");
	
	$sql = "insert into login (MCOMPANYID,CONTACT_PERSON,ADDRESS,PHONE_NO,EMAILID,USER_TYPE,STATUS,USERNAME,PASSWORD,GENDER) values ('".$company_id."','".$c_person."','".$address."','".$phone_no."','".$emailid."','admin','".$status."','".$user_name."','".$password."','".$selGender."')";
	mysql_query($sql,$connect) or die(mysql_error());
	
	$sql1 = "select max(LOGINID) as max from login";
	$res1 = mysql_query($sql1) or die(mysql_error());
	$rows1 = mysql_fetch_assoc($res1);
	
	$sql2 = "insert into audit_trail (LOGINID, USERNAME, PASSWORD, DATE_ON) values ('".$rows1['max']."', '".$user_name."', '".$password."', '".date('Y-m-d',time())."')";
	mysql_query($sql2) or die(mysql_error());
	
	$sql3 = "insert into recent_work_master (LOGINID, WORK, WORK_DATE) values ('".$_SESSION['uid']."','New login created :  ".$c_person.".',NOW())";
	mysql_query($sql3,$connect) or die(mysql_error());
	
	if(mysql_error($connect))
	{
		$msg = 1;
		mysql_query("ROLLBACK");
	}
	else
	{
		mysql_query("commit"); 
	}
	return $msg; 
}

function viewAdminRecords($id)
{
	$connect = $this->funConnect();
	$query = "SELECT * FROM login where LOGINID=".$id;
	$result = mysql_query($query);
	while ($rows = mysql_fetch_assoc($result)) 
	{
		$this->var_1 = $rows['MCOMPANYID'];
		$this->var_2 = $rows['ADDRESS'];
		$this->var_3 = $rows['PHONE_NO'];
		$this->var_4 = $rows['EMAILID'];
		$this->var_5 = $rows['CONTACT_PERSON'];
		$this->var_6 = $rows['STATUS'];
		$this->var_7 = $rows['USERNAME'];
		$this->var_8 = $rows['PASSWORD'];
		$this->var_9 = $rows['GENDER'];
	}
}

function updateAdminRecords()
{
	$connect				= $this->funConnect();
	$id = $_REQUEST['id'];
 	$company_id			= addslashes(trim($_REQUEST['selCName']));
	$c_person 			= addslashes(trim($_REQUEST['txtName']));
	$address			= addslashes(trim($_REQUEST['txtAddress']));	
	$phone_no 			= addslashes(trim($_REQUEST['txtPhoneNo']));
	$emailid 			= addslashes(trim($_REQUEST['txtEmailid']));
	$user_name 			= addslashes(trim($_REQUEST['txtUName']));
	$password 			= addslashes(trim($_REQUEST['txtPassword']));
	$status 			= addslashes(trim($_REQUEST['selStatus']));
	$selGender 			= addslashes(trim($_REQUEST['selGender']));

	mysql_query("Set Autocommit=0");
	mysql_query("START TRANSACTION");
		
	$msg = 0;
	$sql = "update login set MCOMPANYID='".$company_id."',ADDRESS='".$address."',PHONE_NO='".$phone_no."',EMAILID='".$emailid."',CONTACT_PERSON='".$c_person."',STATUS='".$status."',USERNAME='".$user_name."',PASSWORD='".$password."',GENDER='".$selGender."'  where LOGINID='".$id."'";
	
	mysql_query($sql) or die(mysql_error());
	
	$sql2 = "insert into audit_trail (LOGINID, USERNAME, PASSWORD, DATE_ON) values ('".$id."', '".$user_name."', '".$password."', '".date('Y-m-d',time())."')";
	mysql_query($sql2) or die(mysql_error());
	
	$sql3 = "insert into recent_work_master (LOGINID, WORK, WORK_DATE) values ('".$_SESSION['uid']."','Login details updated :  ".$c_person.".',NOW())";
	mysql_query($sql3,$connect) or die(mysql_error());
		
	if(mysql_error($connect))
	{
		$msg = 1;
		mysql_query("ROLLBACK");
	}
	else
	{
		mysql_query("commit"); 
	}
	return $msg;
}




} // class data ends
?>