<?php
if(!isset($_SESSION)) 
{ 
session_start(); 
} 
if(!isset($_SESSION['is_login']) ){
header("location: login.php");
?>
<script>
window.location="login.php";
</script>
<?php
}

// $permission  = explode(',',$_SESSION['permission_id']);
// if(!in_array(19,$permission) && !in_array(1,$permission)  ){

// $op['is_success'] = false;
// $op['data'] 	  = "عذرا انت غير مخول للوصل الى هذه الصفحة";
// echo json_encode($op);
// exit;

// }

date_default_timezone_set($_SESSION['SYSTimezone']);

if (isset($_POST['type']) || isset($_GET['type'])){
		require("db_connect.php");

		
		if( isset($_GET['type']) && ($_GET['type'] == "load")){
			// DB table to use
			// show All products for specific category
			$cond_y = isset($_GET['y'])? ' AND W.year_id_fk ='.$_GET['y']:'';
			$cond_m = isset($_GET['m'])? ' AND W.month_id_fk ='.$_GET['m']:'';
			$cond_d = isset($_GET['d'])? ' AND W.day_id_fk ='.$_GET['d']:'';
			$cond_id = isset($_GET['id'])? ' AND W.employe_id_fk ='.$_GET['id']:'';
			
			$cond   = $cond_id . ' ' .$cond_y . ' ' . $cond_m . ' ' . $cond_d;
			

			$cond_y1 = isset($_GET['y'])? ' AND WH1.year_id_fk ='.$_GET['y']:'';
			$cond_m1 = isset($_GET['m'])? ' AND WH1.month_id_fk ='.$_GET['m']:'';
			$cond_d1 = isset($_GET['d'])? ' AND WH1.day_id_fk ='.$_GET['d']:'';
			$cond_id1 = isset($_GET['id'])? ' AND WH1.employe_id_fk ='.$_GET['id']:'';
			
			$cond1   = $cond_id1 . ' ' .$cond_y1 . ' ' . $cond_m1 . ' ' . $cond_d1;
			


			


			$table  =  '(

							SELECT
					
									work_id,
									time_in,
									time_out,
									hours,
									W.hour_price,
									hour_transaction,
									is_subtracted,
									work_note,
									day_id_fk,
									month_id_fk,
									year_id_fk,
									employe_id_fk,
									W.created_at,
									employe_full_name,
									year_no,
									month_name,

									(COALESCE((SELECT sum(hours) FROM work_hours WH1  	
					        			WHERE   WH1.is_deleted =0  AND WH1.hour_transaction = "Attend" '.$cond1.' ),0)
									) AS attend_hours,

									(COALESCE((SELECT sum(hours) FROM work_hours WH1  	
									        	WHERE WH1.is_deleted =0   AND WH1.hour_transaction = "Absent" '.$cond1.'),0)
									) AS absent_hours,

									(COALESCE((SELECT sum(hours) FROM work_hours WH1  	
									        	WHERE WH1.is_deleted =0  AND WH1.hour_transaction = "Absent With Permission" '.$cond1.'),0)
									) AS absent_hours_with_permission,
									
									(COALESCE((SELECT sum(hours) FROM work_hours WH1  	
									        	WHERE WH1.is_deleted =0   AND WH1.hour_transaction = "Sick With Permission" '.$cond1.'),0)
									) AS sick_hours_with_permission,

									(COALESCE((SELECT sum(hours) FROM work_hours WH1  	
									        	WHERE WH1.is_deleted =0   AND WH1.is_subtracted = 1  '.$cond1.'),0)
									) AS absent_subtracted_hours,
									
									(COALESCE((SELECT sum(hours) FROM work_hours WH1  	
									        	WHERE WH1.is_deleted =0   AND WH1.hour_transaction = "Extra Hours" '.$cond1.'),0)
									) AS extra_hours,

									(COALESCE((SELECT sum(hours) FROM work_hours WH1  	
									        	WHERE WH1.is_deleted =0   AND WH1.hour_transaction = "Reward" '.$cond1.'),0)
									) AS rewards,


									(COALESCE((SELECT sum(hours) FROM work_hours WH1  	
									        	WHERE WH1.is_deleted =0   AND WH1.hour_transaction = "Penalty" '.$cond1.'),0)
									) AS penalty,
									

									(COALESCE((SELECT sum(hours) FROM work_hours WH1  	
									        	WHERE WH1.is_deleted =0   AND WH1.hour_transaction = "Salary" '.$cond_y1.' '.$cond_m1.' '.$cond_id1.'),0)
									) AS salary,

									(COALESCE((SELECT sum(hours) FROM work_hours WH1  	
					        		WHERE   WH1.is_deleted =0   AND WH1.hour_transaction = "Loan" '.$cond_y1.' '.$cond_m1.' '.$cond_id1.'),0)
									) AS loan,

									(COALESCE((SELECT sum(hours) FROM work_hours WH1  	
									        	WHERE  WH1.is_deleted =0   AND WH1.hour_transaction = "Refund" '.$cond_y1.' '.$cond_m1.' '.$cond_id1.'),0)
									) AS refund,

									(

									


								    (	COALESCE((SELECT sum(hours ) FROM work_hours WH1  	
										        	WHERE  WH1.is_deleted =0  AND WH1.hour_transaction = "Attend" '.$cond1.'),0)
									*
										(	(COALESCE((SELECT sum(hours) FROM work_hours WH1  	
										        	WHERE WH1.is_deleted =0  AND WH1.hour_transaction = "Salary" '.$cond_y1.' '.$cond_m1.' '.$cond_id1.'),0)
											) / 240
										)
									)

									+
									
									(	COALESCE((SELECT sum(hours * 1.3333 ) FROM work_hours WH1  	
										        	WHERE  WH1.is_deleted =0  AND WH1.hour_transaction = "Extra Hours" '.$cond1.'),0)
									*
										(	(COALESCE((SELECT sum(hours) FROM work_hours WH1  	
										        	WHERE WH1.is_deleted =0  AND WH1.hour_transaction = "Salary" '.$cond_y1.' '.$cond_m1.''.$cond_id1.'),0)
											) / 240
										)
									)
									
									+
									(COALESCE((SELECT sum(hours) FROM work_hours WH1  	
									        	WHERE WH1.is_deleted =0 AND WH1.hour_transaction = "Reward" '.$cond1.'),0)
									) 
									-
									(COALESCE((SELECT sum(hours) FROM work_hours WH1  	
									        	WHERE WH1.is_deleted =0  AND WH1.hour_transaction = "Penalty" '.$cond1.'),0)
									) 

									-
									(COALESCE((SELECT sum(hours) FROM work_hours WH1  	
									        	WHERE WH1.is_deleted =0  AND WH1.hour_transaction = "Refund" '.$cond_y1.' '.$cond_m1.''.$cond_id1.'),0)
									) 
									-
									(		
										(COALESCE((SELECT sum(hours ) FROM work_hours WH1  	
									        	WHERE WH1.is_deleted =0  AND WH1.is_subtracted = 1 '.$cond1.'),0)
										)

										*
											
										(	(COALESCE((SELECT sum(hours) FROM work_hours WH1  	
								        	WHERE WH1.is_deleted =0  AND WH1.hour_transaction = "Salary" '.$cond_y1.' '.$cond_m1.' '.$cond_id1.'),0)
											) / 240
										)

											
									)


									) AS real_salary

									
									
								FROM
									work_hours  W
									LEFT JOIN employe E ON (W.employe_id_fk = E.employe_id)
									LEFT JOIN years Y ON (W.year_id_fk = Y.year_id)
									LEFT JOIN months M ON (W.month_id_fk = M.month_id)
									
					
					
							WHERE
								( W.is_deleted = 0  '.$cond.') 
		
				) AS ALLTBL';
			// Table's primary key
			// echo $table;
			$primaryKey = 'work_id';
			$columns = array(
					    array( 'db' => 'work_id', 'dt' => 0 ),
					    array( 'db' => 'employe_full_name', 'dt' => 1 ),
					    array( 'db' => 'year_no', 'dt' => 2),
					    array( 'db' => 'month_name', 'dt' => 3 ),
					    array( 'db' => 'day_id_fk', 'dt' => 4 ),
					    array( 'db' => 'time_in', 'dt' => 5 ),
						array( 'db' => 'time_out', 'dt' => 6 ),
					  	array( 'db' => 'hours', 'dt' => 7 ),
					  	array( 'db' => 'hour_transaction', 'dt' => 8 ),
					  	array( 'db' => 'work_note', 'dt' => 9 ),
					  	array( 'db' => 'created_at', 'dt' => 10 , 'formatter'=> function($d){
							return date('Y-m-d',strtotime($d));
							 } ),
					  	array( 'db' => 'is_subtracted', 'dt' => 11 ),
					    array( 'db' => 'year_id_fk', 'dt' => 12 ),

						array( 'db' => 'month_id_fk', 'dt' => 13 ),
						array( 'db' => 'employe_id_fk', 'dt' => 14 ),
						
						array( 'db' => 'attend_hours', 'dt' => 15 ),
						array( 'db' => 'absent_hours', 'dt' => 16 ),
						array( 'db' => 'absent_hours_with_permission', 'dt' => 17 ),
						array( 'db' => 'sick_hours_with_permission', 'dt' => 18 ),
						array( 'db' => 'absent_subtracted_hours', 'dt' => 19 ),
						array( 'db' => 'extra_hours', 'dt' => 20 ),
						array( 'db' => 'rewards', 'dt' => 21 ),
						array( 'db' => 'penalty', 'dt' => 22 ),
						array( 'db' => 'salary', 'dt' => 23 ),
						array( 'db' => 'real_salary', 'dt' => 24 ),
						array( 'db' => 'loan', 'dt' => 25 ),
						array( 'db' => 'refund', 'dt' => 26 ),
							  	
			);
			// SQL server connection information
			$sql_details = array(
			    'user' => $mysql_user,
			    'pass' => $mysql_password,
			    'db'   => $mysql_db,
			    'host' => $mysql_server
			);
			require( 'ssp.php' );
			echo json_encode(
			    SSP::complex( $_GET, $sql_details, $table, $primaryKey, $columns ,null, '' )
			);
			exit;
		}

		

		if ($_POST['type']=="show_salary_board") {

			$year_id 		= $_POST['year_id'];
			$month_id 		= $_POST['month_id'];
			$employe_id 	= $_POST['employe_id'];
			
			if($year_id > 0 && $year_id != NULL && $year_id !=''){
				$cond_y =  ' AND W.year_id_fk ='.$year_id ;
			}else{
				$cond_y =  '';
			}

			if($month_id > 0 && $month_id != NULL && $month_id !=''){
				$cond_m = ' AND W.month_id_fk ='.$month_id;
			}else{
				$cond_m = '';
			}

			if($employe_id > 0 && $employe_id != NULL && $employe_id !=''){
				$cond_id = ' AND W.employe_id_fk ='.$employe_id;
			}else{
				$cond_id = '';
			}

			
			
			$cond   = $cond_y . ' ' . $cond_m . ' ' . $cond_id;

			


			$res = $db->query('
								SELECT
					
									work_id,
									employe_full_name,
									hour_transaction,
									hours AS salary,
									
									year_no,
									month_name,
                                    
                                    COALESCE((SELECT sum(hours ) FROM work_hours WH1  	
										        	WHERE WH1.year_id_fk = W.year_id_fk AND WH1.month_id_fk = W.month_id_fk AND WH1.employe_id_fk = W.employe_id_fk AND  WH1.is_deleted =0  AND WH1.hour_transaction = "Attend" ),0) AS attend_hours,

									COALESCE((SELECT sum(hours ) FROM work_hours WH1  	
										        	WHERE WH1.year_id_fk = W.year_id_fk AND WH1.month_id_fk = W.month_id_fk AND WH1.employe_id_fk = W.employe_id_fk AND  WH1.is_deleted =0  AND WH1.hour_transaction = "Absent" ),0) AS absent_hours,

									COALESCE((SELECT sum(hours ) FROM work_hours WH1  	
										        	WHERE WH1.year_id_fk = W.year_id_fk AND WH1.month_id_fk = W.month_id_fk AND WH1.employe_id_fk = W.employe_id_fk AND  WH1.is_deleted =0  AND WH1.hour_transaction = "Absent With Permission" ),0) AS perm_hours,

									COALESCE((SELECT sum(hours ) FROM work_hours WH1  	
										        	WHERE WH1.year_id_fk = W.year_id_fk AND WH1.month_id_fk = W.month_id_fk AND WH1.employe_id_fk = W.employe_id_fk AND  WH1.is_deleted =0  AND WH1.hour_transaction = "Sick With Permission" ),0) AS sick_hours,


									COALESCE((SELECT sum(hours  ) FROM work_hours WH1  	
										        	WHERE WH1.year_id_fk = W.year_id_fk AND WH1.month_id_fk = W.month_id_fk AND WH1.employe_id_fk = W.employe_id_fk AND  WH1.is_deleted =0  AND WH1.hour_transaction = "Extra Hours" ),0) AS extra_hours,

									(COALESCE((SELECT sum(hours) FROM work_hours WH1  	
									        	WHERE WH1.year_id_fk = W.year_id_fk AND WH1.month_id_fk = W.month_id_fk AND WH1.employe_id_fk = W.employe_id_fk AND  WH1.is_deleted =0 AND WH1.hour_transaction = "Reward" ),0)
									)  AS rewards,


									(COALESCE((SELECT sum(hours) FROM work_hours WH1  	
									        	WHERE WH1.year_id_fk = W.year_id_fk AND WH1.month_id_fk = W.month_id_fk AND WH1.employe_id_fk = W.employe_id_fk AND  WH1.is_deleted =0  AND WH1.hour_transaction = "Penalty" ),0)
									)  AS penalty,

									(COALESCE((SELECT sum(hours ) FROM work_hours WH1  	
									        	WHERE WH1.year_id_fk = W.year_id_fk AND WH1.month_id_fk = W.month_id_fk AND WH1.employe_id_fk = W.employe_id_fk AND  WH1.is_deleted =0  AND WH1.is_subtracted = 1 ),0)
									) as absent_subtracted,

									(COALESCE((SELECT sum(hours) FROM work_hours WH1  	
								        	WHERE WH1.year_id_fk = W.year_id_fk AND WH1.month_id_fk = W.month_id_fk AND WH1.employe_id_fk = W.employe_id_fk AND  WH1.is_deleted =0  AND WH1.hour_transaction = "Loan" ),0)
									) AS loan,

									(COALESCE((SELECT sum(hours) FROM work_hours WH1  	
								        	WHERE WH1.year_id_fk = W.year_id_fk AND WH1.month_id_fk = W.month_id_fk AND WH1.employe_id_fk = W.employe_id_fk AND  WH1.is_deleted =0  AND WH1.hour_transaction = "Refund" ),0)
									) AS refund,

                                    
									(

									


								    (	COALESCE((SELECT sum(hours ) FROM work_hours WH1  	
										        	WHERE WH1.year_id_fk = W.year_id_fk AND WH1.month_id_fk = W.month_id_fk AND WH1.employe_id_fk = W.employe_id_fk AND  WH1.is_deleted =0  AND WH1.hour_transaction = "Attend" ),0)
									*
										(	(COALESCE((SELECT sum(hours) FROM work_hours WH1  	
										        	WHERE WH1.year_id_fk = W.year_id_fk AND WH1.month_id_fk = W.month_id_fk AND WH1.employe_id_fk = W.employe_id_fk AND  WH1.is_deleted =0  AND WH1.hour_transaction = "Salary" ),0)
											) / 240
										)
									)

									+
									
									(	COALESCE((SELECT sum(hours * 1.3333 ) FROM work_hours WH1  	
										        	WHERE WH1.year_id_fk = W.year_id_fk AND WH1.month_id_fk = W.month_id_fk AND WH1.employe_id_fk = W.employe_id_fk AND  WH1.is_deleted =0  AND WH1.hour_transaction = "Extra Hours" ),0)
									*
										(	(COALESCE((SELECT sum(hours) FROM work_hours WH1  	
										        	WHERE WH1.year_id_fk = W.year_id_fk AND WH1.month_id_fk = W.month_id_fk AND WH1.employe_id_fk = W.employe_id_fk AND  WH1.is_deleted =0  AND WH1.hour_transaction = "Salary" ),0)
											) / 240
										)
									)
									
									+
									(COALESCE((SELECT sum(hours) FROM work_hours WH1  	
									        	WHERE WH1.year_id_fk = W.year_id_fk AND WH1.month_id_fk = W.month_id_fk AND WH1.employe_id_fk = W.employe_id_fk AND  WH1.is_deleted =0 AND WH1.hour_transaction = "Reward" ),0)
									) 
									-
									(COALESCE((SELECT sum(hours) FROM work_hours WH1  	
									        	WHERE WH1.year_id_fk = W.year_id_fk AND WH1.month_id_fk = W.month_id_fk AND WH1.employe_id_fk = W.employe_id_fk AND  WH1.is_deleted =0  AND WH1.hour_transaction = "Penalty" ),0)
									) 

									-
									(COALESCE((SELECT sum(hours) FROM work_hours WH1  	
									        	WHERE WH1.year_id_fk = W.year_id_fk AND WH1.month_id_fk = W.month_id_fk AND WH1.employe_id_fk = W.employe_id_fk AND  WH1.is_deleted =0  AND WH1.hour_transaction = "Refund" ),0)
									) 
									-
									(		
										(COALESCE((SELECT sum(hours ) FROM work_hours WH1  	
									        	WHERE WH1.year_id_fk = W.year_id_fk AND WH1.month_id_fk = W.month_id_fk AND WH1.employe_id_fk = W.employe_id_fk AND  WH1.is_deleted =0  AND WH1.is_subtracted = 1 ),0)
										)

										*
											
										(	(COALESCE((SELECT sum(hours) FROM work_hours WH1  	
								        	WHERE WH1.year_id_fk = W.year_id_fk AND WH1.month_id_fk = W.month_id_fk AND WH1.employe_id_fk = W.employe_id_fk AND  WH1.is_deleted =0  AND WH1.hour_transaction = "Salary" ),0)
											) / 240
										)

											
									)


									) AS real_salary
                                    
                                   



									
									
								FROM
									work_hours  W
									LEFT JOIN employe E ON (W.employe_id_fk = E.employe_id)
									LEFT JOIN years Y ON (W.year_id_fk = Y.year_id)
									LEFT JOIN months M ON (W.month_id_fk = M.month_id)
									
					
					
							WHERE
								( W.is_deleted = 0  AND W.hour_transaction ="Salary" '.$cond.') ORDER BY year_id_fk DESC, month_id_fk ASC ,employe_id_fk ASC
									');

		
			if ($res) {
				//$row = $res->fetch_all();
				//echo json_encode($row);

				$op  = array('invoice'=>array(),'details'=>array());
				while ($row = $res->fetch_assoc()) {
				   
					$result['work_id']  			= $row['work_id'];
					$result['employe_full_name']  	= $row['employe_full_name'];
					$result['year_no']  			= $row['year_no'] ;
					$result['month_name']  			= $row['month_name'];
					$result['salary']  				= $row['salary'];
					$result['real_salary']  		= $row['real_salary'];
					$result['attend_hours'] 		= $row['attend_hours'];
					$result['absent_hours'] 		= $row['absent_hours'];
					$result['perm_hours'] 			= $row['perm_hours'];
					$result['sick_hours'] 			= $row['sick_hours'];
					$result['extra_hours'] 			= $row['extra_hours'];
					$result['rewards'] 				= $row['rewards'];
					$result['penalty'] 				= $row['penalty'];
					$result['loan'] 				= $row['loan'];
					$result['refund'] 				= $row['refund'];
					$result['absent_subtracted'] 	= $row['absent_subtracted'];
					
					$result['total_hours'] 			= $row['attend_hours'] + $row['extra_hours'] * 1.333 - $row['absent_subtracted'];
					
					
					array_push($op['invoice'],$result);
						
				}		
				echo json_encode($op);
				exit;
			}else{
				echo 0;
				exit;
			}
		}



		if ($_POST['type']=="employe_details") {

			$employe_id_fk 		= $_POST['employe_id_fk'];
			$year_id_fk 		= $_POST['year_id_fk'];
			$month_id_fk 		= $_POST['month_id_fk'];
			$day_id_fk 			= $_POST['day_id_fk'];
			$trans 				= $_POST['trans'];
			
			$cond_e  			= ' AND W.employe_id_fk ='.$employe_id_fk ;
			$cond_y 			=  '';
			$cond_m 			=  '';
			$cond_d 			=  '';

			if( $year_id_fk != '' && $year_id_fk != NULL && $year_id_fk > 0){
				$cond_y =  ' AND W.year_id_fk ='.$year_id_fk ;
			}

			if( $month_id_fk != '' && $month_id_fk != NULL && $month_id_fk > 0){
				$cond_m =  ' AND W.month_id_fk ='.$month_id_fk ;
			}

			if( $day_id_fk != '' && $day_id_fk != NULL && $day_id_fk > 0){
				$cond_m =  ' AND W.day_id_fk ='.$day_id_fk ;
			}

			
			
			$cond   = $cond_y . ' ' . $cond_m . ' ' . $cond_d . ' ' . $cond_e ;

			

		

			$res = $db->query('SELECT
					
									work_id,
									time_in,
									time_out,
									hours,
									W.hour_price,
									hour_transaction,
									is_subtracted,
									work_note,
									day_id_fk,
									month_id_fk,
									year_id_fk,
									employe_id_fk,
									W.created_at,
									employe_full_name  

									
									
								FROM
									work_hours  W
									LEFT JOIN employe E ON (W.employe_id_fk = E.employe_id)
									
									
								WHERE
									( W.is_deleted = 0  '.$cond.') ORDER BY day_id_fk

											');

		
			if ($res) {
				//$row = $res->fetch_all();
				//echo json_encode($row);

				$op  = array('invoice'=>array(),'details'=>array());
				while ($row = $res->fetch_assoc()) {
				   
					$result['work_id']  			= $row['work_id'];
					$result['time_in']  			= $row['time_in'];
					$result['time_out']  			= $row['time_out'] ;
					$result['hours']  				= $row['hours'];
					$result['hour_price']  			= $row['hour_price'];
					$result['hour_transaction']  	= $row['hour_transaction'];
					$result['is_subtracted'] 		= $row['is_subtracted'];
					$result['work_note'] 			= $row['work_note'];
					$result['day_id_fk'] 			= $row['day_id_fk'];
					$result['month_id_fk'] 			= $row['month_id_fk'];
					$result['year_id_fk'] 			= $row['year_id_fk'];
					$result['employe_id_fk'] 		= $row['employe_id_fk'];
					$result['employe_full_name'] 	= $row['employe_full_name'];
					$result['created_at'] 			= date('Y-m-d',strtotime($row['created_at']));
					
					
					
					array_push($op['invoice'],$result);
						
				}		
				echo json_encode($op);
				exit;
			}else{
				echo $db->error;
				exit;
			}
		}


		if ($_POST['type']=="get_edit_item") {

			$id 		= $_POST['id'];
			
			

			$res = $db->query("SELECT work_id,
									time_in,
									time_out,
									hours,
									W.hour_price,
									hour_transaction,
									is_subtracted,
									work_note,
									day_id_fk,
									month_id_fk,
									year_id_fk,
									employe_id_fk
									  
									

									FROM work_hours  W
										
								 	WHERE W.work_id='$id'  AND W.is_deleted = 0  ");

		
			if ($res) {
				//$row = $res->fetch_all();
				//echo json_encode($row);

				$op  = array();
				while ($row = $res->fetch_assoc()) {
				   
					$result['work_id']  		= $row['work_id'];
					$result['time_in']  		= $row['time_in'];
					
					$result['time_out']  		= $row['time_out'];
					$result['hours']  			= $row['hours'];
					$result['hour_transaction'] = $row['hour_transaction'];
					$result['is_subtracted'] 	= $row['is_subtracted'];
					$result['work_note']  		= $row['work_note'];
					$result['day_id_fk']  		= $row['day_id_fk'];
					$result['month_id_fk']  	= $row['month_id_fk'];
					$result['year_id_fk']  		= $row['year_id_fk'];
					$result['employe_id_fk']  	= $row['employe_id_fk'];
					
					
					array_push($op,$result);
						
				}		
				echo json_encode($op);
				exit;
			}else{
				echo 0;
				exit;
			}
		}

		

		if ($_POST['type'] == "update") {
      		
      		$id					=addslashes($_POST["id"]);
			$year_id_fk			=addslashes($_POST["year_id_fk"]);
			$month_id_fk		=addslashes($_POST["month_id_fk"]);
			$day_id_fk			=addslashes($_POST["day_id_fk"]);
			$hours				=addslashes($_POST["hours"]);
			$time_in			=addslashes($_POST["time_in"]);
			$time_out			=addslashes($_POST["time_out"]);
			$is_subtracted		=addslashes($_POST["is_subtracted"]);
			$work_note			=addslashes($_POST["work_note"]);
			$hour_transaction	=addslashes($_POST["trans"]);
			$employe_id_fk		=addslashes($_POST["employe_id_fk"]);

			// $hour_transaction   ='';

			switch ($hour_transaction) {
				

				case 'Attend':
					

					$res = $db->query("SELECT * FROM work_hours WHERE is_deleted=0 AND employe_id_fk = '$employe_id_fk' AND year_id_fk = '$year_id_fk' AND month_id_fk = '$month_id_fk' AND day_id_fk = '$day_id_fk' AND hour_transaction = '$hour_transaction' AND work_id != '$id'");
					if (mysqli_num_rows($res)>0) {
							$op['is_success'] = false;
							$op['data'] 	  = "خطأ لقد تم تسجيل حضور الموظف لهذا اليوم مسبقا";
							echo json_encode($op);
							exit;
					}
					break;

				

				case 'Salary':
					
					$res = $db->query("SELECT * FROM work_hours WHERE is_deleted=0 AND employe_id_fk = '$employe_id_fk' AND year_id_fk = '$year_id_fk' AND month_id_fk = '$month_id_fk' AND hour_transaction = '$hour_transaction' AND work_id != '$id'");

					if (mysqli_num_rows($res)>0) {
							$op['is_success'] = false;
							$op['data'] 	  = "خطأ لقد تم تحديد الراتب لهذا الشهر مسبقا الرجاء قم بتحريره لانه لايمكن اضافة اكثر من راتب لنفس الشهر";
							echo json_encode($op);
							exit;
					}

					break;
				
				
			}
			

			$delete = 0;
			$reg_date = date("Y-m-d h:i:s", time());
			$lastaction = "Edit work action to employee";

			$sql ="UPDATE work_hours SET 	
										   year_id_fk 		= '$year_id_fk',
										   month_id_fk 		= '$month_id_fk',
										   day_id_fk 		= '$day_id_fk',
										   hours 			= '$hours',
										   time_in 			= '$time_in',
										   time_out 		= '$time_out',
										   is_subtracted 	= '$is_subtracted',
										   work_note 		= '$work_note',
										  
										   updated_at 		= '$reg_date',
										   last_action 		= '$lastaction',
										   last_action_by 	= '".$_SESSION['user_id']."'
									WHERE work_id = '$id'";

				$res = $db->query($sql);

					if ($res) {

						$op['is_success'] = true;
						$op['data'] 	  = "تم عملية التحديث بنجاح";
						echo json_encode($op);
						exit;
					}else{
						$op['is_success'] = false;
						$op['data'] 	  = "هناك خطأ : لم تتم عملية التحديث بنجاح الرجاء اعادة تحميل الصفحة والمحاولة من جديد";
						echo json_encode($op);
						exit;
					}
			

		}



		if ($_POST['type']=="delete") {

			$id=addslashes($_POST["obj_id"]);
			$reg_date = date("Y-m-d h:i:s", time());
			$lastaction = "Delete Work hours";

			$op  = array('is_success'=>false, 'data' => '');

			

			

	  		$res = $db->query("UPDATE work_hours SET is_deleted = 1 ,deleted_at = '$reg_date',last_action='$lastaction',last_action_by='".$_SESSION['user_id']."' WHERE work_id=$id");
		    	if ($res){
		        		$op['is_success'] = true;
						$op['data'] 	  = "تمت عملية الحذف بنجاح";
						echo json_encode($op);
						exit;
		     	}else{
		      			$op['is_success'] = false;
						$op['data'] 	  = "حصل خطأ : السجل في امان";
						echo json_encode($op);
						exit;
		     	}
		}

		if ($_POST['type'] == "create") {
      		
      		$employe_id_fk		=addslashes($_POST["employe_id_fk"]);
			$year_id_fk			=addslashes($_POST["year_id_fk"]);
			$month_id_fk		=addslashes($_POST["month_id_fk"]);
			$day_id_fk			=addslashes($_POST["day_id_fk"]);
			$hours				=addslashes($_POST["hours"]);
			$time_in			=addslashes($_POST["time_in"]);
			$time_out			=addslashes($_POST["time_out"]);
			$is_subtracted		=addslashes($_POST["is_subtracted"]);
			$work_note			=addslashes($_POST["work_note"]);
			$trans				=addslashes($_POST["trans"]);

			$hour_transaction   ='';

			switch ($trans) {
				case 'absent':
					$hour_transaction   ='Absent';
					break;

				case 'absent_with_per':
					$hour_transaction   ='Absent With Permission';
					break;

				case 'sick':
					$hour_transaction   ='Sick With Permission';
					break;

				case 'attend':
					$hour_transaction   ='Attend';
					$hours				='8';

					$res = $db->query("SELECT * FROM work_hours WHERE is_deleted=0 AND employe_id_fk = '$employe_id_fk' AND year_id_fk = '$year_id_fk' AND month_id_fk = '$month_id_fk' AND day_id_fk = '$day_id_fk' AND hour_transaction = '$hour_transaction'");
					if (mysqli_num_rows($res)>0) {
							$op['is_success'] = false;
							$op['data'] 	  = "خطأ لقد تم تسجيل حضور الموظف لهذا اليوم مسبقا";
							echo json_encode($op);
							exit;
					}
					break;

				case 'extra':
					$hour_transaction   ='Extra Hours';
					break;

				case 'reward':
					$hour_transaction   ='Reward';
					break;

				case 'penalty':
					$hour_transaction   ='Penalty';
					break;

				case 'salary':
					$hour_transaction   ='Salary';

					$res = $db->query("SELECT * FROM work_hours WHERE is_deleted=0 AND employe_id_fk = '$employe_id_fk' AND year_id_fk = '$year_id_fk' AND month_id_fk = '$month_id_fk' AND hour_transaction = '$hour_transaction'");
					if (mysqli_num_rows($res)>0) {
							$op['is_success'] = false;
							$op['data'] 	  = "خطأ لقد تم تحديد الراتب لهذا الشهر مسبقا الرجاء قم بتحريره لانه لايمكن اضافة اكثر من راتب لنفس الشهر";
							echo json_encode($op);
							exit;
					}

					break;
				
				
			}
			

			

				$delete = 0;
				$reg_date = date("Y-m-d h:i:s", time());
				$lastaction = "Add new work action to employee";

				$sql ="INSERT INTO work_hours (employe_id_fk,
											   year_id_fk,
											   month_id_fk,
											   day_id_fk,
											   hours,
											   time_in,
											   time_out,
											   is_subtracted,
											   work_note,
											   hour_transaction,
											   created_by,
											   created_at,
											   updated_at,
											   last_action,
											   last_action_by)
										VALUES('$employe_id_fk',
											   '$year_id_fk',
											   '$month_id_fk',
											   '$day_id_fk',
											   '$hours',
											   '$time_in',
											   '$time_out',
											   '$is_subtracted',
											   '$work_note',
											   '$hour_transaction',
											   '".$_SESSION['user_id']."',
											   '$reg_date',
											   '$reg_date',
											   '$lastaction',
											   '".$_SESSION['user_id']."')";

				$res = $db->query($sql);

					if ($res) {

						$op['is_success'] = true;
						$op['data'] 	  = "تم الاضافة بنجاح";
						echo json_encode($op);
						exit;
					}else{
						$op['is_success'] = false;
						$op['data'] 	  = "هناك خطأ : لم يتم عملية الاضافة بنجاح الرجاء اعادة تحميل الصفحة والمحاولة من جديد";
						echo json_encode($op);
						exit;
					}
			

		}
	}
	else{
		header("Location:../");
		exit;
	}
?>
