Current File : /home/inlingua/www/icentex/leave_tracker/leaves.main.php
<?php
ob_start();
session_start();
//error_reporting(-1);
date_default_timezone_set('Asia/Calcutta');
// Copyright, Panalink Infotech Limited. Created on: 17-03-2011

// Include the database connection details
require('../dbConnection.php');

// Include common functions
require('../commonFunc.php');

// Include common functions
require('../globalVars.php');
require("../class.phpmailer.php");

// Setup table name, configuration and data entry form
$tableName = "leaves";
$tableConf = $tableName . ".conf.php";
$dataForm = $tableName . ".form.php";
$viewForm = $tableName . ".view.php";

// Include table configuration details
require($tableConf);

function DateArray($dat1,$dat2)
{


$dateMonthYearArr = array();
$fromDateTS = strtotime($dat1);
$toDateTS = strtotime($dat2);

for ($currentDateTS = $fromDateTS; $currentDateTS <= $toDateTS; $currentDateTS += (60 * 60 * 24)) {
// use date() and $currentDateTS to format the dates in between
$currentDateStr = date("Y-m-d",$currentDateTS);
$dateMonthYearArr[] = $currentDateStr;
//print $currentDateStr.<br />;
}
return $arraystr=implode("#",$dateMonthYearArr);
}





// Include the table handler class
require('../dbHandler.class.php');
$dbObject = new dbHandler();
$dbObject->setTableName($tableName);
$dbObject->setDbFieldList($dbFieldList);
$dbObject->setKeyFieldName("id");
$dbObject->connect();

// Retrieve parameters from request
$dbObject->getDbFieldsFromRequest();

$queryLWC="select count(id) as tot from leaves where status='waiting' && timesheet_user_id='".$_SESSION['id']."'";
$resultLWC=mysqli_query($conn,$queryLWC);
$numLWC=mysqli_fetch_array($resultLWC);
////////////////////////////////////////Tomail array///////////////////////////////////////////////////////////
$tomail_array=array();
//$query_tomail = "SELECT email,name FROM timesheet_user where recNo ='235' ";
//$query_tomail = "SELECT email,name FROM timesheet_user where  recNo='273' || recNo='259'";
$query_tomail = "SELECT email,name FROM timesheet_user where  recNo='273'";
$tomail_exec=mysqli_query($conn,$query_tomail);

while($result_tomail=mysqli_fetch_array($tomail_exec))
{
array_push($tomail_array,$result_tomail['email']);
}

array_push($tomail_array,"nandini@inlinguanewdelhi.com");
//array_push($tomail_array,"aditti@inlinguanewdelhi.com");
//array_push($tomail_array,"vikasumrao@sify.com");

//////////////////////////////////////////to mail array///////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$query_mail = "SELECT email,name FROM timesheet_user where recNo='".$_SESSION['id']."'  and usertype='user' and status='Active'";
$mail_exec=mysqli_query($conn,$query_mail);
$result_mail=mysqli_fetch_array($mail_exec);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$currentMode = "";
$formPrompt = "";
$succFailMesg = "";
$showResult = "N";

$action = $_REQUEST['aC'];
if (($action == "")  || ($action == "sA"))
{	// Set 'Add' mode in dataform
	$currentMode = "Apply"; $succFailMesg = "<b>Add details</b>&nbsp;&nbsp;&nbsp;&nbsp;[<a href='javascript:history.back()'>Back</a>]<br>";
}
else if ($action == "Apply")
{	$currentMode = "Apply"; 

$reqFields = array("fromDate|From Date","toDate|To Date","noofdays|No Of Days");
	
	$blankFields = "";
	foreach ($reqFields as $reqField)
	{	list($field,$prompt) = explode('|',$reqField);
		if ($_REQUEST[$field] == "") { $blankFields .= "'". $prompt . "', ";}
	} 

	// Perform check for duplicate entry
	if($dbObject->Exists("id", $_REQUEST["id"]))
	{	$succFailMesg = "<b>Could not add details, record with this key exists.</b><br>";	
		$showResult = "N"; // Show dataentry form again
	}
	elseif ($blankFields != "")
	{	
	
	$succFailMesg = "<font color='#da0000'><b>No values entered for $blankFields please enter or select value</b><br></font>";	
		$showResult = "N"; // Show dataentry form again
	}
	else
	{	//$today = date("Y-m-d");
	    $instructor=$_SESSION['id'];
	    $dbObject->setDbField("timesheet_user_id",$instructor);
		
		$dbObject->setDbField("reasons",addslashes($_REQUEST['reasons']));
		
		$dbObject->setDbField("classAffected",addslashes($_REQUEST['classAffected']));
		
		$dbObject->setDbField("subByWhom",addslashes($_REQUEST['subByWhom']));
		
		$dbObject->setDbField("compensated",addslashes($_REQUEST['compensated']));
		
		$leaveAppliedDate=date("Y-m-d H:i:s");
	    $dbObject->setDbField("leaveAppliedDate",$leaveAppliedDate);
		
	    $dbObject->setDbField("waitingDate",$leaveAppliedDate);
		
		$strD=DateArray($_REQUEST['fromDate'],$_REQUEST['toDate']);
		$dbObject->setDbField("date_str",$strD);
		
		if($_REQUEST['classAffected']=="" || $_REQUEST['IsclassesSubstituted']=='Other' )
		{
		
		$dbObject->setDbField("IsclassesSubstituted",'');
		}		
		
		$result=$dbObject->Save();
		if($result>0){
		
		$succFailMesg = "<b>Details added successfully</b><br>";
////////////////////////////////Mail function///////////////////////////////////////////		
		

$message=$result_mail['name']." has applied for Leave.<br>";

$fromdate1=explode("-",$_REQUEST['fromDate']);
$todate1=explode("-",$_REQUEST['toDate']);
$fdate1= date("d F Y", mktime(0, 0, 0, $fromdate1[1], $fromdate1[2], $fromdate1[0]));
$tdate1= date("d F Y", mktime(0, 0, 0, $todate1[1], $todate1[2], $todate1[0]));
$IsclassesSubstituted=$_REQUEST['IsclassesSubstituted'];
$emergencyLeave=$_REQUEST['emergencyLeave'];
////////////No==compensated/////
if($emergencyLeave=='Yes' && $IsclassesSubstituted=='No')
{
$addmsg='<br><b>Note :</b> "The following leaves are emergency off and compensated and  not applicable for salary and/or leave deduction"';
}
else if($emergencyLeave=='Yes' && $IsclassesSubstituted=='Yes')
{
$addmsg='<br><b>Note :</b> "The following leaves are emergency off and substituted and  applicable for salary and/or leave deduction"';
}
else 
{
$addmsg='';
}
$message.="The details are as follows: <br>Period - From : ".$fdate1." to ".$tdate1." <br>";

$message.="No. Of Days :".$_REQUEST['noofdays']."<br>Reason :".$_REQUEST['reasons']."<br>";
$message.=$addmsg;
$message .="<br><br>Regards,<br>iCentex Administrator";
//$fromEmail=$result_mail['email'];
//$fromEmail="mttd@inlinguanewdelhi.com";
$fromEmail= "icentex@inlinguanewdelhi.in";
$fromName=$result_mail['name'];

$to=$tomail_array;

$subject="Leave Applied-".$fdate1." to ".$tdate1."-".$result_mail['name'];

            $domain = "inlinguanewdelhi.in";

		    $smtpHost = "mail.$domain";

		    $smtpUser = "admin@$domain";

		    $smtpPassword = "5B&vp7pgm7flABvi";
			

$mail = new PHPMailer();

//$mail->IsSMTP();              // set mailer to use SMTP
//$mail->IsMail();
//$mail->Host = $smtpHost;  // specify main and backup server
//$mail->SMTPAuth = true;     // turn on SMTP authentication
//$mail->Username = $smtpUser;  // SMTP username
//$mail->Password = $smtpPassword; // SMTP password
if($mail_type=="S")
        {
	    
	$mail->IsSMTP();              // set mailer to use SMTP
        
        }
	
        if($mail_type=="N")
        {
	$mail->IsMail();
        }
        
        if($mail_type=="S")
        {
	$mail->Host = $smtpHost;  // specify main and backup server
	$mail->SMTPAuth = true;     // turn on SMTP authentication
	$mail->Username = $smtpUser;  // SMTP username
	$mail->Password = $smtpPassword; // SMTP password
        }

$mail->From = $fromEmail;
$mail->FromName = $fromName;
foreach ($to as $toAddress) 
{
	 //$toAddress;
	$mail->AddAddress($toAddress, "");
}

$mail->IsHTML(true);               // set email format to HTML
$mail->Subject = $subject;
$mail->Body    = $message;
//$mail->AddReplyTo("umraovikas@gmail.com","");
//$mail->AddAttachment($path);
		
if(!$mail->Send())
{
    echo "Message could not be sent on mailid: <font color='red'>'".$to."'</font><br>";
	echo "Please check the emailId.<br>";
    //echo "Mailer Error: " . $mail->ErrorInfo;
    //exit;
}
////////////////////////////////////////////Mail function//////////////////////////////////////////////////////////////	

$message1="Dear ".$result_mail['name'].",<br><br>You have successfully applied for leave.<br>";

$message1.="The details are as follows: <br>Period - From : ".$fdate1." to ".$tdate1." <br>";

$message1.="No. Of Days :".$_REQUEST['noofdays']."<br>Reason :".$_REQUEST['reasons']."<br>";
$message1.=$addmsg;

$message1.='<br><br>If you wish to make any changes, please cancel your leave and re-apply.';

$message1 .="<br><br>Thanking You<br>iCentex Administrator";

//$fromEmail1="mttd@inlinguanewdelhi.com";

//$fromEmail1="vikasumrao@sify.com";

$fromEmail1="icentex@inlinguanewdelhi.in";

$to1=$result_mail['email'];

$mail1 = new PHPMailer();
//$mail1->IsMail();
//$mail1->IsSMTP(); 
//$mail1->Host = $smtpHost;  // specify main and backup server
//$mail1->SMTPAuth = true;     // turn on SMTP authentication
//$mail1->Username = $smtpUser;  // SMTP username
//$mail1->Password = $smtpPassword; // SMTP password

if($mail_type=="S")
        {
	    
	$mail1->IsSMTP();              // set mailer to use SMTP
        
        }
	
        if($mail_type=="N")
        {
	$mail1->IsMail();
        }
        
        if($mail_type=="S")
        {
	$mail1->Host = $smtpHost;  // specify main and backup server
	$mail1->SMTPAuth = true;     // turn on SMTP authentication
	$mail1->Username = $smtpUser;  // SMTP username
	$mail1->Password = $smtpPassword; // SMTP password
        }

$mail1->From = $fromEmail1;
$mail1->FromName = "Inlingua Leave Admin";
$mail1->AddAddress($to1, "");
$mail1->IsHTML(true);               // set email format to HTML
$mail1->Subject = $subject;
$mail1->Body    = $message1;

if(!$mail1->Send())
{
    echo "Message could not be sent on mailid: <font color='red'>'".$to1."'</font><br>";
	echo "Please check the emailId.<br>";
    //echo "Mailer Error: " . $mail1->ErrorInfo;
    //exit;
}

		
		$showResult = "Y"; // Goto result part in dataform
		$st="waiting";
		
		
		
	}
	else{
		$succFailMesg = "<b>Could not retrieve details. Contact system administrator</b><br>";
		$showResult = "Y"; // Goto result part in dataform
		$st="waiting";
		
	}
	
	}
}
else if ($action == "sM")
{	// Set 'Update' mode in dataform
	$currentMode = "Update"; $succFailMesg = "<b>Cancel details</b>&nbsp;&nbsp;&nbsp;&nbsp;[<a href='javascript:history.back()'>Back</a>]<br>";

	// Retrieve record first
	if ($dbObject->Retrieve("id",$_REQUEST["kV"]))
	{ ; }
	else { $succFailMesg = "<b>Could not retrieve details. Contact system administrator</b><br>"; }

}
else if ($action == "Update")
{	$currentMode = "Update"; 
	$showResult = "Y"; // Goto result part in dataform
	
	
	
	 //$instructor=$_SESSION['id'];
	   // $dbObject->setDbField("timesheet_user_id",$instructor);
	   
	   
		
		$disapproveSelfDate=date("Y-m-d H:i:s");
	    $dbObject->setDbField("disapproveSelfDate",$disapproveSelfDate);
		
		//$strD=DateArray($_REQUEST['fromDate'],$_REQUEST['toDate']);
		$dbObject->setDbField("status",'calSelf');
	
	if ($dbObject->Update("id",$_REQUEST["kV"]))
	{	
	////////////////////////////////Mail function///////////////////////////////////////////	
	$query_leaves = "SELECT fromDate,toDate, noofdays FROM leaves where id='".$_REQUEST['kV']."' ";
	$leaves_exec=mysqli_query($conn,$query_leaves);
	$result_leaves=mysqli_fetch_array($leaves_exec);	
	$message=$result_mail['name']." has canceled his/her applied leave from  : ".$fdate1." to ".$tdate1."<br>";
	$fromdate1=explode("-",$result_leaves['fromDate']);
	$todate1=explode("-",$result_leaves['toDate']);
	$fdate1= date("d F Y", mktime(0, 0, 0, $fromdate1[1], $fromdate1[2], $fromdate1[0]));
	$tdate1= date("d F Y", mktime(0, 0, 0, $todate1[1], $todate1[2], $todate1[0]));
	//$message.="The details are as follows: <br>Period - From : ".$fdate1." to ".$tdate1." <br>";
	//$message.="No. Of Days :".$result_leaves['noofdays']."<br>";
	$message=$result_mail['name']." has canceled his/her applied leave from  : ".$fdate1." to ".$tdate1."<br><br>";
	$message .="Thanking You<br>".$result_mail['name'];
	$fromEmail=$result_mail['email'];
        // $fromEmail="mttd@inlinguanewdelhi.com";
	$fromName=$result_mail['name'];
	$tomail_array=array();
        //$to=array("aditti@inlinguanewdelhi.com","apok@nd.inlingua.in","hrd@inlinguanewdelhi.com","amttd@inlinguanewdelhi.com");
	//$to=array("nandini@inlinguanewdelhi.com","apok@nd.inlingua.in","hrd@inlinguanewdelhi.com","amttd@inlinguanewdelhi.com");
	array_push($tomail_array,"nandini@inlinguanewdelhi.com");
	array_push($tomail_array,"service@inlinguanewdelhi.com");
	array_push($tomail_array,"hrd@inlinguanewdelhi.com");
	array_push($tomail_array,"amttd@inlinguanewdelhi.com");
	
	
	array_push($tomail_array,$fromEmail);
	//echo '<pre>'; print_r($tomail_array);
	echo $subject="Leave self canceled-".$fdate1." to ".$tdate1."-".$result_mail['name'];
	
	       $domain = "inlinguanewdelhi.in";

		    $smtpHost = "mail.$domain";

		    $smtpUser = "admin@$domain";

		    $smtpPassword = "5B&vp7pgm7flABvi";
	
	$mail = new PHPMailer();
	$mail_type=="S";
	//$mail->IsSMTP();              // set mailer to use SMTP
	//$mail->IsMail();
	//$mail->Host = $smtpHost;  // specify main and backup server
	//$mail->SMTPAuth = true;     // turn on SMTP authentication
	//$mail->Username = $smtpUser;  // SMTP username
	//$mail->Password = $smtpPassword; // SMTP password
	
        if($mail_type=="S")
        {
	    
	$mail->IsSMTP();              // set mailer to use SMTP
        
        }
	
        if($mail_type=="N")
        {
	$mail->IsMail();
        }
        
        if($mail_type=="S")
        {
	$mail->Host = $smtpHost;  // specify main and backup server
	$mail->SMTPAuth = true;     // turn on SMTP authentication
	$mail->Username = $smtpUser;  // SMTP username
	$mail->Password = $smtpPassword; // SMTP password
	
        }  	
	$fromEmail= "icentex@inlinguanewdelhi.in";
	$mail->From = $fromEmail;
	$mail->FromName = $fromName;
	foreach ($tomail_array as $toAddress) 
	{
	 //echo $toAddress;
		$mail->AddAddress($toAddress, "");
	}

	$mail->IsHTML(true);               // set email format to HTML
	$mail->Subject = $subject;
	$mail->Body    = $message;
	//$mail->AddReplyTo("umraovikas@gmail.com","");
	//$mail->AddAttachment($path);
		
	if(!$mail->Send())
	{
        echo "Message could not be sent on mailid: <font color='red'>'".$to."'</font><br>";
	echo "Please check the emailId.<br>";
        //echo "Mailer Error: " . $mail->ErrorInfo;
        //exit;
	}
	
////////////////////////////////////////////Mail function//////////////////////////////////////////////////////////////	
	$succFailMesg = "<b>Details modified successfully</b><br>"; 
	$st="calSelf";
	
	
	}
	else
	{	$succFailMesg = "<b>Could not modify details. Contact system administrator</b><br>" ; }
}
else if ($action == "sD")
{	$currentMode = "Delete"; $succFailMesg = "<b>Delete details</b>&nbsp;&nbsp;&nbsp;&nbsp;[<a href='javascript:history.back()'>Back</a>]<br>";
	// Retrieve record first
	if ($dbObject->Retrieve("id",$_REQUEST["kV"]))
	{ ; }
	else { $succFailMesg = "<b>Could not retrieve details. Contact system administrator</b><br>"; }
}
else if ($action == "Delete")
{	$showResult = "Y"; // Goto result part in dataform
	
	if ($dbObject->Delete("id",$_REQUEST["kV"]))
	{	$succFailMesg = "<b>Details deleted successfully</b><br>"; }
	else
	{	$succFailMesg = "<b>Could not delete details. Contact System administrator</b><br>" ; }
}
else if ($action == "sV")
{	// Set 'View' mode 
	$currentMode = "View"; $succFailMesg = "<b>View details(<a href='javascript:history.back();'>Back</a>)</b><br>";
	$dataForm = $viewForm;
	
	// Retrieve record first
	if ($dbObject->Retrieve("id",$_REQUEST["kV"]))
	{ ; }
	else { $succFailMesg = "<b>Could not retrieve details. Contact system administrator</b><br>"; }

}
else
{	echo "Nothing to do !";
	return;
}

// Load the data entry or  view entry form
require($dataForm);

$dbObject->CloseConnection();
return;


?>