<?php
	//Connects to your Database
	include "../db_connection/db_connection.php";
	include "../config/config.php";		
	$page_id	=	base64_decode(base64_decode(base64_decode($_GET['page_id']))); //THIS IS PAGE ID                    
	//dept_name,e_name,dob,e_id,mob,add,resume,bg,mob1.mob2,remak,of_add,co_add,
	//bnk_name,brn_name,ach_name,acno,ifsc
if($page_id==1) //this is for basic details add page	
{ 		
		$PATH_resume	= "../employee_resumes/";
		
		$dept_nm='';
		$dept_ids	=	$_POST['dept_name'];
		for($i = 0; $i < count($dept_ids); $i++)
		{
			$dept_nm .= $dept_ids[$i].",";
		} 
		$dept_name 	= 	rtrim($dept_nm,",");
		$e_name		=	$_POST['e_name'];
		$dob		=	$_POST['dob'];
		$bg			=	$_POST['bg'];
		$e_id		=	$_POST['e_id'];
		$mob1		=	$_POST['mob1'];
		$mob2		=	$_POST['mob2'];
		$per_nm		=	$_POST['per_nm'];
		$mob3		=	$_POST['mob3'];
		$relan		=	$_POST['relan'];

		$of_add		=	$_POST['of_add'];
		$co_add		=	$_POST['co_add'];

		$bnk_name	=	$_POST['bnk_name'];
		$brn_name	=	$_POST['brn_name'];
		$ach_name	=	$_POST['ach_name'];
		$acno		=	$_POST['acno'];
		$ifsc		=	$_POST['ifsc'];
		
		$remak		=	$_POST['remak'];		
		//echo "zz".$remak."<br>";
		
	$SLCT = "SELECT  * FROM emp_det ORDER BY emp_det_id ASC";
	//echo $SLCT;
	$QURY = mysqli_query($connect,$SLCT);
	if(mysqli_num_rows($QURY)> 0)
	{
		$SLCT_sk = "SELECT  MAX(emp_det_id) as max_ids FROM emp_det ORDER BY emp_det_id ASC";
		$QURY_sk = mysqli_query($connect,$SLCT_sk);  
		$FTCH_sk = mysqli_fetch_object($QURY_sk);
		$max_id = $FTCH_sk->max_ids + 1;

		$resume = substr($_FILES["resume"]["name"], strrpos($_FILES["resume"]["name"], '.') + 1);
		$res_ext="R.".$max_id.".".$resume;
		if(move_uploaded_file($_FILES["resume"]["tmp_name"], "$PATH_resume".$res_ext)){ /*echo "succes";*/ }

	}
	else
	{
		$resume = substr($_FILES["resume"]["name"], strrpos($_FILES["resume"]["name"], '.') + 1);
		$res_ext="R.1.".$resume;
		if(move_uploaded_file($_FILES["resume"]["tmp_name"], "$PATH_resume".$res_ext)){ /*echo "succes";*/ }
	}	

		$INSRT		= "INSERT INTO emp_det(`master_dept_id`,`histry_mas_dept_id`, `name`, `dob`,`blood_gp`, `email_id`, `mobile`,`mobile2`,`cont_per_name`,`cont_per_mobile`,`cont_relan`, `add`,`res_add`,`bank_name`, `branch_name`, `ac_name`, `ac_no`, `ifsc_code`, `resume_ext`,`remark_s`, `post_dt`) VALUES
		 ('$dept_name','$dept_name','$e_name','$dob','$bg','$e_id','$mob1','$mob2','$per_nm','$mob3','$relan','$of_add','$co_add','$bnk_name','$brn_name','$ach_name','$acno','$ifsc','$res_ext','$remak','$cur_time')";
		//echo $INSRT;
		$QURY		= mysqli_query($connect,$INSRT);	

		$SLCT_md = "SELECT  MAX(emp_det_id) as emp_mx FROM emp_det ORDER BY emp_det_id ASC";
		$QURY_md = mysqli_query($connect,$SLCT_md);  
		$FTCH_md = mysqli_fetch_object($QURY_md);
		$emp_mx = $FTCH_md->emp_mx;

		//fmd, tod, sal_amt,rem
		$fmd		=	$_POST['fmd'];
		$tod		=	$_POST['tod'];
		$sal_amt	=	$_POST['sal_amt'];
		$rem		=	$_POST['rem'];

		if($fmd!='' && $sal_amt!='')
		{				
			$INSRT1		= "INSERT INTO emp_sal_det(`emp_det_id`, `from_date`, `to_date`, `sal_amt`, `remarks`, `post_dt`) VALUES
			 ('$emp_mx','$fmd','$tod','$sal_amt','$rem','$cur_time')";
			//echo $INSRT1;
			$QURY1		= mysqli_query($connect,$INSRT1);	
		}
		$tot_cn = $_POST['tot_cn'];
		for($i=1; $i<$tot_cn; $i++)
		{
			if(isset($_POST['fmdate'][$i]) && $_POST['fmdate'][$i]!='' && isset($_POST['cost_vl'][$i]) && $_POST['cost_vl'][$i]!='')
			{
				$cat_id = $_POST['cat_id'][$i];
				$fmdate = $_POST['fmdate'][$i];
				$cost_vl = $_POST['cost_vl'][$i];
				
				$INSRT_d	= "INSERT INTO emp_cad_det(`emp_det_id`, `master_cad_cat_id`,`fm_date`, `cat_hrs`,`post_dt`) VALUES('$emp_mx','$cat_id','$fmdate','$cost_vl','$cur_time')";		
				$QURY_d		= mysqli_query($connect,$INSRT_d);
			}
		}

?>
		<script type="text/javascript">
            //alert("Data inserted successfully");
            //history.back();
            window.location='../employee_list.php';
        </script>
<?php
}
else if($page_id==2) //this is for basic details list page	
{
	$emp_id	= base64_decode(base64_decode(base64_decode($_GET['emp_id'])));
	
	if($_GET['statu_s']=="active")
	{
		$UPDATE = "UPDATE emp_det SET statu_s = 'inactive',update_dt = '$cur_time' WHERE emp_det_id = '$emp_id'";
		$QRY    = mysqli_query($connect,$UPDATE); 
	}
	else if($_GET['statu_s']=="inactive")
	{
		$UPDATE = "UPDATE emp_det SET statu_s = 'active',update_dt = '$cur_time' WHERE emp_det_id = '$emp_id'";
		$QRY    = mysqli_query($connect,$UPDATE); 
	}
	?>
  			<script type="text/javascript">
    		//alert("Data inserted successfully");
    		//history.back();
   			window.location='../employee_list.php';
  			</script>
   <?php
}
else if($page_id==3) //this is for basic details list page	
{
		
	$PATH_resume	= "../employee_resumes/";
	
	$emp_id		=	$_POST['emp_id']; 
	$SLCTse = "SELECT histry_mas_dept_id FROM emp_det WHERE emp_det_id ='$emp_id' ";		
	$QURYse = mysqli_query($connect,$SLCTse);
	$FTCHse = mysqli_fetch_object($QURYse);
	$hi_dept = $FTCHse->histry_mas_dept_id;
	$dept_nm	=	'';
	$dept_ids	=	$_POST['dept_name'];
	for($i = 0; $i < count($dept_ids); $i++)
	{
		$dept_nm .= $dept_ids[$i].",";
		
		$SLCTse = "SELECT * FROM emp_det WHERE emp_det_id ='$emp_id' AND histry_mas_dept_id REGEXP(CONCAT('(^|,)(',$dept_ids[$i],')(,|$)'))";		
		$QURYse = mysqli_query($connect,$SLCTse);
		if(mysqli_num_rows($QURYse)>0)
		{
		}
		else
		{
			$hi_dept = $hi_dept.",".$dept_ids[$i];
		}	
	}
	$dept_name 	= 	rtrim($dept_nm,",");
	$hst_dept_name = rtrim($hi_dept,",");
	$e_name		=	$_POST['e_name'];
	$dob		=	$_POST['dob'];
	$bg			=	$_POST['bg'];
	$e_id		=	$_POST['e_id'];
	$mob1		=	$_POST['mob1'];
	$mob2		=	$_POST['mob2'];
	$of_add		=	$_POST['of_add'];
	$co_add		=	$_POST['co_add'];
	
	$per_nm		=	$_POST['per_nm'];
	$mob3		=	$_POST['mob3'];
	$relan		=	$_POST['relan'];
	
	$bnk_name	=	$_POST['bnk_name'];
	$brn_name	=	$_POST['brn_name'];
	$ach_name	=	$_POST['ach_name'];
	$acno		=	$_POST['acno'];
	$ifsc		=	$_POST['ifsc'];
	
	$remak		=	$_POST['remak'];	
	
	if($_FILES["resume"]["name"]!='')
	{
		$resume = substr($_FILES["resume"]["name"], strrpos($_FILES["resume"]["name"], '.') + 1);
		$res_ext="R.".$emp_id.".".$resume;
		if(move_uploaded_file($_FILES["resume"]["tmp_name"], "$PATH_resume".$res_ext)){ /*echo "succes";*/ }	

		$UPDATE = "UPDATE emp_det SET `master_dept_id`='$dept_name', `histry_mas_dept_id`='$hst_dept_name', `name`='$e_name', `dob`='$dob',`blood_gp`='$bg', `email_id`='$e_id', `mobile`='$mob1',`mobile2`='$mob2', `cont_per_name`='$per_nm',`cont_per_mobile`='$mob3',`cont_relan`='$relan',`add`='$of_add',`res_add`='$co_add',`bank_name`='$bnk_name', `branch_name`='$brn_name', `ac_name`='$ach_name', `ac_no`='$acno', `ifsc_code`='$ifsc', `resume_ext`='$res_ext',`remark_s`='$remak',update_dt = '$cur_time' WHERE emp_det_id = '$emp_id'";
		//echo $UPDATE."aa";
		$QRY    = mysqli_query($connect,$UPDATE); 
	}
	else
	{
		$UPDATE = "UPDATE emp_det SET `master_dept_id`='$dept_name', `histry_mas_dept_id`='$hst_dept_name', `name`='$e_name', `dob`='$dob',`blood_gp`='$bg', `email_id`='$e_id', `mobile`='$mob1',`mobile2`='$mob2', `cont_per_name`='$per_nm', `cont_per_mobile`='$mob3', `cont_relan`='$relan', `add`='$of_add',`res_add`='$co_add',`bank_name`='$bnk_name', `branch_name`='$brn_name', `ac_name`='$ach_name', `ac_no`='$acno', `ifsc_code`='$ifsc',`remark_s`='$remak',update_dt = '$cur_time' WHERE emp_det_id = '$emp_id'";
		//echo $UPDATE;
		$QRY    = mysqli_query($connect,$UPDATE); 
	}

	$lp_vl = $_POST['sal_loop'];
	if($lp_vl!=0)
	{
		for($i=1;$i<$lp_vl;$i++)
		{
			if(isset($_POST['tod_'.$i]) && $_POST['tod_'.$i]!='' && isset($_POST['fmd_'.$i]) && $_POST['fmd_'.$i]!='')
			{
				$sal_det	=	$_POST['sal_det_'.$i];	
				$fmd1		=	$_POST['fmd_'.$i];
				$tod1		=	$_POST['tod_'.$i];
				$sal_amt1	=	$_POST['sal_amt_'.$i];
				$rem1		=	$_POST['rem_'.$i];

				$UPDATE1 = "UPDATE emp_sal_det SET from_date='$fmd1', to_date='$tod1', `sal_amt`='$sal_amt1', `remarks`='$rem1', `upd_status`='yes',update_dt = '$cur_time' WHERE emp_det_id = '$emp_id' AND emp_sal_det_id='$sal_det'";
				$QRY1    = mysqli_query($connect,$UPDATE1); 
			}
			else if(isset($_POST['tod_'.$i]) && $_POST['tod_'.$i]!='' && isset($_POST['fmd_'.$i]) && $_POST['fmd_'.$i]=='')
			{
				$sal_det	=	$_POST['sal_det_'.$i];
				$fmd1		=	'';	
				$tod1		=	$_POST['tod_'.$i];
				$sal_amt1	=	$_POST['sal_amt_'.$i];
				$rem1		=	$_POST['rem_'.$i];

				$UPDATE1 = "UPDATE emp_sal_det SET from_date='$fmd1', to_date='$tod1', `sal_amt`='$sal_amt1', `remarks`='$rem1', `upd_status`='yes',update_dt = '$cur_time' WHERE emp_det_id = '$emp_id' AND emp_sal_det_id='$sal_det'";
				$QRY1    = mysqli_query($connect,$UPDATE1); 
			}
			else if(isset($_POST['tod_'.$i]) && $_POST['tod_'.$i]=='' && isset($_POST['fmd_'.$i]) && $_POST['fmd_'.$i]!='')
			{
				$sal_det	=	$_POST['sal_det_'.$i];	
				$fmd1		=	$_POST['fmd_'.$i];
				$tod1		=	'';
				$sal_amt1	=	$_POST['sal_amt_'.$i];
				$rem1		=	$_POST['rem_'.$i];

				$UPDATE1 = "UPDATE emp_sal_det SET from_date='$fmd1', to_date='$tod1', `sal_amt`='$sal_amt1', `remarks`='$rem1', `upd_status`='yes',update_dt = '$cur_time' WHERE emp_det_id = '$emp_id' AND emp_sal_det_id='$sal_det'";
				$QRY1    = mysqli_query($connect,$UPDATE1); 
			}
		}
	}

	if(isset($_POST['fmd']))
	{
		//fmd, tod, sal_amt,rem
		$fmd		=	$_POST['fmd'];
		$tod		=	$_POST['tod'];
		$sal_amt	=	$_POST['sal_amt'];
		$rem		=	$_POST['rem'];

		$INSRT		= "INSERT INTO emp_sal_det(`emp_det_id`, `from_date`, `to_date`, `sal_amt`, `remarks`, `post_dt`) VALUES('$emp_id','$fmd','$tod','$sal_amt','$rem','$cur_time')";
		$QURY		= mysqli_query($connect,$INSRT);
	}
	$tot_cn = $_POST['tot_cn'];
	for($i=1; $i<$tot_cn; $i++)
	{
		if(isset($_POST['fmdate'][$i]) && $_POST['fmdate'][$i]!='' && isset($_POST['cost_vl'][$i]) && $_POST['cost_vl'][$i]!='')
		{
			$cat_id = $_POST['cat_id'][$i];
			$fmdate = $_POST['fmdate'][$i];
			$cost_vl = $_POST['cost_vl'][$i];

			$SLCT_cat_check = "SELECT * FROM emp_cad_det WHERE master_cad_cat_id = '$cat_id' AND emp_det_id = '$emp_id' AND updated = 'no'";
			$QURY_cat_check = mysqli_query($connect,$SLCT_cat_check);
			if(mysqli_num_rows($QURY_cat_check)>0)
			{
				$SLCT = "SELECT * FROM emp_cad_det WHERE master_cad_cat_id = '$cat_id' AND emp_det_id = '$emp_id' AND updated = 'no' AND fm_date != ''";
				$QURY = mysqli_query($connect,$SLCT);
				if(mysqli_num_rows($QURY)>0) //this condition for checking the category id is exist in the table or not
				{
					$FTCH = mysqli_fetch_object($QURY);
					if($FTCH->cat_hrs != 0) 
					{ 
						$ex_cost_vl = $FTCH->cat_hrs ; 
					}
					else
					{ 
						$ex_cost_vl = ""; 
					}

					if( $ex_cost_vl != $cost_vl || $FTCH->fm_date != $fmdate) //this condition for checking the existing value and new values
					{
						$todate = date('d-m-Y',(strtotime ( '-1 day' , strtotime ( $fmdate) ) ));
						$todate =  date("d-m-Y", strtotime($todate));
						$UPDATE_dt = "UPDATE emp_cad_det SET `to_date`='$todate', `updated` = 'yes',`update_dt` = '$cur_time' WHERE updated = 'no' AND master_cad_cat_id = '$cat_id' AND emp_det_id = '$emp_id'";
						$QRY_dt    = mysqli_query($connect,$UPDATE_dt);
						$INSRT_d	= "INSERT INTO emp_cad_det(`emp_det_id`, `master_cad_cat_id`,`fm_date`, `cat_hrs`,`post_dt`) VALUES('$emp_id','$cat_id','$fmdate','$cost_vl','$cur_time')";	
						$QURY_d		= mysqli_query($connect,$INSRT_d);
					}
				}
				else
				{
					if($fmdate!='')
					{
						$UPDATE_dt = "UPDATE emp_cad_det SET `fm_date`='$fmdate', `cat_hrs`='$cost_vl', `update_dt` = '$cur_time' WHERE updated = 'no' AND master_cad_cat_id = '$cat_id' AND emp_det_id = '$emp_id'";
						$QRY_dt    = mysqli_query($connect,$UPDATE_dt);
					}
				}
			}
			else
			{
				$INSRT_d	= "INSERT INTO emp_cad_det(`emp_det_id`, `master_cad_cat_id`,`fm_date`, `cat_hrs`,`post_dt`) VALUES('$emp_id','$cat_id','$fmdate','$cost_vl','$cur_time')";	
				$QURY_d		= mysqli_query($connect,$INSRT_d);
			}
		}
	}
	?>
  			<script type="text/javascript">
    		//alert("Data inserted successfully");
    		//history.back();
   			window.location='../employee_list.php';
  			</script>
   <?php
}
?>

