<?php
session_start();
// ============
include("../../php_crud/my_class.php");
$obj=new my_class();
// ============
extract($_POST);
$branchid = $_SESSION['LOGIN_BRANCH'];
$year =date("Y");

if($opr=='entry'){

$leave_date=explode(',',$leave_dates);
$days=(count($leave_date));
$lv_setting= $obj->Details_By_Cond("hr_emp_leave_setting","_br_id='$branchid' AND _status='1' AND _id='$leave_id'");
$leave=$lv_setting['_days'];
$column= "sum(_days) as dayCn";
$lv_entry= $obj->View_column_details_By_Cond("hr_emp_leave_entry",$column,"_br_id='$branchid' AND _emp_id='$emp_id' AND _lv_id='$leave_id' AND _status='A' AND EXTRACT(YEAR FROM _entry_date)='$year'");
$emp_lv_days=$lv_entry['dayCn'];
$remaining=$leave-$emp_lv_days;

 if($remaining>=$days){ 




		$form_data=array(
			
			'_br_id'=>$branchid,
			'_emp_id'=>$emp_id,
			'_lv_id'=>$leave_id,
			'_reason'=>$reason,
			'_days'=>$days,
			'_pay_type'=>$pay_type,
			'_entry_by'=>$_SESSION['LOGIN_USERID'],
			'_entry_date'=>date("Y:m:d h:i:s"),
			'_status'=>'A'
			
		);



 $entry=$obj->Insert_Data("hr_emp_leave_entry", $form_data);
}

else{
	echo "Your Selected Days have exceeded your Remaining Leave Days";
}

if($entry){

for($i=0;$i<$days;$i++){
$time = strtotime($leave_date[$i]);

$lv_date = date('Y-m-d',$time);


		$form_data_date=array(
			
			'_pid'=>$entry,
			'_date'=>$lv_date,
			'_status'=>'A'	
		);

		$entry_date=$obj->Insert_Data("hr_emp_leave_dates", $form_data_date);
    }

    if($entry_date){
    	echo "Success";
    }
    else{
    	echo "Faild";
    }
}
  
}


if($opr=='entry_leave_with_pay'){

$leave_date=explode(',',$leave_dates);
$days=(count($leave_date));
$lv_setting= $obj->Details_By_Cond("hr_emp_leave_setting","_br_id='$branchid' AND _status='1' AND _id='$leave_id'");
$leave=$lv_setting['_days'];
$column= "sum(_days) as dayCn";
$lv_entry= $obj->View_column_details_By_Cond("hr_emp_leave_entry",$column,"_br_id='$branchid' AND _emp_id='$emp_id' AND _lv_id='$leave_id' AND _status='A' AND EXTRACT(YEAR FROM _entry_date)='$year'");
$emp_lv_days=$lv_entry['dayCn'];

    for ($i = 0; $i < $days; $i++) {
        $time = strtotime($leave_date[$i]);

        $lv_date = date('Y-m-d', $time);

        $lv_chek= $obj->View_column_details_By_Cond("`hr_emp_leave_entry` a  JOIN `hr_emp_leave_dates` b ON a.`_id`=b.`_pid`","COUNT(a._id) cid","a.`_emp_id`='$emp_id' AND b.`_date`=DATE('$lv_date') AND a.`_br_id`='$branchid'");
        $emp_lv_enty_check_Count=$lv_chek['cid'];
        if($emp_lv_enty_check_Count!=0)
        {
            echo "Date: $lv_date is Alread Exist, Sorry try again...";exit;
        }



    }

$remaining=$leave-$emp_lv_days;



    if ($remaining >= $days) {

        $form_data = array(

            '_br_id' => $branchid,
            '_emp_id' => $emp_id,
            '_lv_id' => $leave_id,
            '_reason' => $remark,
            '_days' => $days,
            '_pay_type' => '1',
            '_entry_by' => $_SESSION['LOGIN_USERID'],
            '_entry_date' => date("Y:m:d h:i:s"),
            '_status' => 'A'

        );

        $entry = $obj->Insert_Data_By_Cond("hr_emp_leave_entry", $form_data, "");


        if ($entry) {

            for ($i = 0; $i < $days; $i++) {
                $time = strtotime($leave_date[$i]);

                $lv_date = date('Y-m-d', $time);


                $form_data_date = array(

                    '_pid' => $entry,
                    '_date' => $lv_date,
                    '_status' => 'A'
                );

                $entry_date = $obj->Insert_Data("hr_emp_leave_dates", $form_data_date);
            }

            if ($entry_date) {
                echo "Success";
            } else {
                echo "Faild";
            }
        }

    } else {
        echo "Your Selected Days have exceeded your Remaining Leave Days";
    }


}

if($opr=='entry_leave_with_pay2'){

    $leave_date=explode(',',$leave_dates);
    $days=(count($leave_date));
    $trowt = $obj->View_colmn_By_Cond("hr_emp_monthly_attandce","_leave","id='$empUniId'");
    $leave= $trowt[0]["_leave"];
    $TotalLv=$leave+$days;
    $empUniId;
    $lv_setting= $obj->Details_By_Cond("hr_emp_leave_setting","_br_id='$branchid' AND _status='1' AND _id='$leave_id'");
    $leave=$lv_setting['_days'];
    $column= "sum(_days) as dayCn";
    $lv_entry= $obj->View_column_details_By_Cond("hr_emp_leave_entry",$column,"_br_id='$branchid' AND _emp_id='$emp_id' AND _lv_id='$leave_id' AND _status='A' AND EXTRACT(YEAR FROM _entry_date)='$year'");
    $emp_lv_days=$lv_entry['dayCn'];

    for ($i = 0; $i < $days; $i++) {
        $time = strtotime($leave_date[$i]);

        $lv_date = date('Y-m-d', $time);

        $lv_chek= $obj->View_column_details_By_Cond("`hr_emp_leave_entry` a  JOIN `hr_emp_leave_dates` b ON a.`_id`=b.`_pid`","COUNT(a._id) cid","a.`_emp_id`='$emp_id' AND b.`_date`=DATE('$lv_date') AND a.`_br_id`='$branchid'");
        $emp_lv_enty_check_Count=$lv_chek['cid'];
        if($emp_lv_enty_check_Count!=0)
        {
            echo "Date: $lv_date is Alread Exist. Sorry try again...";exit;
        }



    }

    $remaining=$leave-$emp_lv_days;



    if ($remaining >= $days) {

        $form_data = array(

            '_br_id' => $branchid,
            '_emp_id' => $emp_id,
            '_lv_id' => $leave_id,
            '_reason' => $remark,
            '_days' => $days,
            '_pay_type' => '1',
            '_entry_by' => $_SESSION['LOGIN_USERID'],
            '_entry_date' => date("Y:m:d h:i:s"),
            '_status' => 'A'

        );

        $entry = $obj->Insert_Data_By_Cond("hr_emp_leave_entry", $form_data, "");


        if ($entry) {

            for ($i = 0; $i < $days; $i++) {
                $time = strtotime($leave_date[$i]);

                $lv_date = date('Y-m-d', $time);


                $form_data_date = array(

                    '_pid' => $entry,
                    '_date' => $lv_date,
                    '_status' => 'A'
                );

                $entry_date = $obj->Insert_Data("hr_emp_leave_dates", $form_data_date);
            }

            if ($entry_date) {
                echo "Success";
            } else {
                echo "Faild";
            }
        }
        $form_data=array(

            '_leave'=>$TotalLv
        );

        $obj->Update_Data("hr_emp_monthly_attandce", $form_data,"id='$empUniId'");

    } else {
        echo "Your Selected Days have exceeded your Remaining Leave Days";
    }


}


if($opr=='entry_leave_without_pay'){

$leave_date=explode(',',$leave_dates);
$days=(count($leave_date));
$lv_setting= $obj->Details_By_Cond("hr_emp_leave_setting","_br_id='$branchid' AND _status='1' AND _id='$leave_id'");
$leave=$lv_setting['_days'];
$column= "sum(_days) as dayCn";
$lv_entry= $obj->View_column_details_By_Cond("hr_emp_leave_entry",$column,"_br_id='$branchid' AND _emp_id='$emp_id' AND _lv_id='$leave_id' AND _status='A' AND EXTRACT(YEAR FROM _entry_date)='$year'");
$emp_lv_days=$lv_entry['dayCn'];
$remaining=$leave-$emp_lv_days;

 if($remaining>=$days){ 

		$form_data=array(
			
			'_br_id'=>$branchid,
			'_emp_id'=>$emp_id,
			'_lv_id'=>$leave_id,
			'_reason'=>$remark,
			'_days'=>$days,
			'_pay_type'=>'0',
			'_entry_by'=>$_SESSION['LOGIN_USERID'],
			'_entry_date'=>date("Y:m:d h:i:s"),
			'_status'=>'A'
			
		);


 $entry=$obj->Insert_Data_By_Cond("hr_emp_leave_entry", $form_data,"");
}

else{
	echo "Your Selected Days have exceeded your Remaining Leave Days";
}

if($entry){

for($i=0;$i<$days;$i++){
$time = strtotime($leave_date[$i]);

$lv_date = date('Y-m-d',$time);


		$form_data_date=array(
			
			'_pid'=>$entry,
			'_date'=>$lv_date,
			'_status'=>'A'
		);

		$entry_date=$obj->Insert_Data("hr_emp_leave_dates", $form_data_date);
    }

    if($entry_date){
	    	echo "Success";
	    }

	    else{
	    	echo "Faild";
	    }
}
  
}

if($opr=='update_leave_head'){

		$form_data=array(
			
			'_br_id'=>$branchid,
			'_name'=>$leave_head,
			'_allow'=>$allow,
			'_days'=>$leave_days,
			'_update_by'=>$_SESSION['LOGIN_USERID'],
			'_last_update'=>date("Y:m:d h:i:s")
			
		);

 $update=$obj->Update_Data("hr_emp_leave_setting", $form_data,"_id='$id'");
 if($update){
 	echo "leave head updated";
 }
 else{
 	echo "Error";
 }
}



if($opr=='delete'){

		$form_data=array(
			'_status_crud'=>'D'
			
		);

 $Delete=$obj->Update_Data("hr_emp_leave_setting", $form_data,"_id='$id'");
 if($Delete){
 	echo "leave head deleted";
 }
 else{
 	echo "Error";
 }
}


if($operation=='delete'){
	$form_data=array(
			'_status'=>'D'	
		);
$Delete=$obj->Update_Data("hr_emp_leave_entry", $form_data,"_id='$id'");

if($Delete){
	echo "Entry Deleted";

$form_data_dt=array(
			'_status'=>'D'	
		);

$Delete=$obj->Update_Data("hr_emp_leave_dates", $form_data_dt,"_pid='$id'");
}
else{

	echo "Error";
}

}

if($opr=='delete_date'){

	$form_data1=array(
			'_status'=>'D'	
		);
$Delete=$obj->Update_Data("hr_emp_leave_dates",$form_data1,"_id='$id'");

if($Delete){
	echo "Entry Deleted";

 $t_days=$obj->Total_Count_By_Cond("hr_emp_leave_dates","_pid='$pid' AND _status='A'");

$form_data_dt=array(
			'_days'=>$t_days	
		);

$update=$obj->Update_Data("hr_emp_leave_entry",$form_data_dt,"_id='$pid'");
}
else{

	echo "Error";
}

}

?>