<?php
	error_reporting(0);
	include("secure.php");
	include 'connection_arrivals.php';
	
	$method = $_POST['method'];
	$output ='';
	
	if ($method=="tssessions"){
		$id = $_POST['id'];
		$type = $_POST['type'];
		
		/*--Identify fortnight--*/
		$fixedfortnight = '2017-02-06';
		$fortnight=''; $tdate=''; $cdate= date('Y-m-d');
		for($i=0; $i<1200; $i++){
			$tdate = date('Y-m-d', strtotime($fixedfortnight." +".(14*$i)." days"));
			if($tdate >= $cdate ){
				$fortnight = $tdate;
				break;
			}
		}
		$fortnight = date('Y-m-d', strtotime($fortnight." -14 days"));
		if($type=='temp')$fortnight ='0000-00-00';
		//if($type=='gen')$fortnight = date('Y-m-d', strtotime($fortnight." +14 days"));
		
			
		$output ='';
		
		$sql0 = "SELECT * FROM ts_users WHERE id='".$id."'";
		$result0 = mysqli_query($con,$sql0);
			while($row = mysqli_fetch_array($result0 , MYSQLI_BOTH)){
			$output .= $row[3] . ",";
			$output .= $row[4] . ",";
			$output .= $row[6] . ",";
			$output .= $row[5] . ",";
		}
		
		$temp ='no';
		
		$sql = "SELECT S1, S2, S3, S4 FROM ts_sessions WHERE USERID='".$id."' AND SDATE>='".$fortnight."' AND TYPE='".$type."' ORDER BY CAST(DAYNO AS UNSIGNED)";
		
		$result = mysqli_query($con,$sql);
			while($row = mysqli_fetch_array($result , MYSQLI_BOTH)){
			$output .= $row[0] . ",";
			$output .= $row[1] . ",";
			$output .= $row[2] . ",";
			$output .= $row[3] . ",";
			$temp = 'yes';
		}
		if($temp =='no'){
			$fortnight1 ='0000-00-00';
			$type = 'temp';
			
			$sql ="SELECT S1, S2, S3, S4 FROM ts_sessions WHERE USERID='".$id."' AND SDATE='".$fortnight1."' AND TYPE='".$type."' ORDER BY CAST(DAYNO AS UNSIGNED)";
			$result = mysqli_query($con,$sql);
			while($row = mysqli_fetch_array($result , MYSQLI_BOTH)){
				$output .= $row[0] . ",";
				$output .= $row[1] . ",";
				$output .= $row[2] . ",";
				$output .= $row[3] . ",";
			}
		}
		$output .= date('d-m-Y', strtotime($fortnight." +0 days")) .',';
		$output .= date('d-m-Y', strtotime($fortnight." +1 days")) .',';
		$output .= date('d-m-Y', strtotime($fortnight." +2 days")) .',';
		$output .= date('d-m-Y', strtotime($fortnight." +3 days")) .',';
		$output .= date('d-m-Y', strtotime($fortnight." +4 days")) .',';
		$output .= date('d-m-Y', strtotime($fortnight." +5 days")) .',';
		$output .= date('d-m-Y', strtotime($fortnight." +6 days")) .',';
		$output .= date('d-m-Y', strtotime($fortnight." +7 days")) .',';
		$output .= date('d-m-Y', strtotime($fortnight." +8 days")) .',';
		$output .= date('d-m-Y', strtotime($fortnight." +9 days")) .',';
		$output .= date('d-m-Y', strtotime($fortnight." +10 days")) .',';
		$output .= date('d-m-Y', strtotime($fortnight." +11 days")) .',';
		$output .= date('d-m-Y', strtotime($fortnight." +12 days")) .',';
		$output .= date('d-m-Y', strtotime($fortnight." +13 days")) .',';
		//$output .= $check;
		echo json_encode($output.','.$sql);
	}
	
	//$method ="tssessions_io";
	if($method=="tssessions_io"){
	    
	    	$id = $_POST['id'];
		    $type = $_POST['type'];
		    $sarch_start = $_POST['sarch_start'];
		    $end_start = $_POST['end_start'];
		    
		    $sarch_start = strtotime($sarch_start);
		    
		    for($i=0; $i<=13; $i++){
		       
		        $search_date = date('Y-m-d',strtotime('+'.$i.' day',$sarch_start));
		        
		        $sql = "SELECT s.*,u.HOURLYCOST,u.POSITION FROM ts_sessions s INNER JOIN ts_users u ON u.ID = s.USERID WHERE u.ID ='".$id."' AND s.SDATE ='".$search_date."'";
//echo $sql.'<br>';
        		$result = mysqli_query($con,$sql);
        		if(mysqli_num_rows($result) ) {
        		    
        		   while($row = mysqli_fetch_array($result , MYSQLI_BOTH)){
            			$s1=$row[4];  $s2=$row[5];  $s3=$row[6];  $s4=$row[7]; $s5=$row[8]; $s6=$row[9]; $hrs =$row[10]; $cost =$row[15]; $pos =$row[16]; $type =$row[12]; $wage =$row[13]; $sta =$row[0];
            		}
            		
        		}else{
        		    
        		        $s1="00:00";  $s2="00:00";  $s3="00:00"; $s4="00:00"; $s5="00:00"; $s6="00:00"; $hrs ='0'; $cost ='0'; $type ='gen'; $wage ='0'; $sta ='0'; $pos ='0';
        		}
        		
        		 $date_val[] =['s1' => $s1,'s2' => $s2,'s3' =>$s3,'s4' => $s4,'s5' => $s5,'s6' => $s6,'hrs'=>$hrs,'cost' => $cost,'type' => $type,'wage' => $wage,'sta' => $sta,'position' => $pos];
        		
		    }
	    
	    	echo json_encode($date_val);
	}
	
	if($method=="updateasuser"){
	    
	        $id = $_POST['id'];
		    $staff = $_POST['staff'];
		    $wage = $_POST['wage'];
		    
		    $sql ="UPDATE ts_users SET POSITION = '".$staff."',HOURLYCOST = '".$wage."' WHERE ID = '".$id."'";
	        $result = mysqli_query($con,$sql);
	        echo json_encode('succes');
	}
	//$method="rosadduser_data";
	if($method=="rosadduser_data"){
	    

	$fromDate = $_SESSION['codd_fdate'];
	$toDate = $_SESSION['codd_tdate'];
	$locationName = $_SESSION['codd_location'];
	$user = $_SESSION['codd_user'];
	$groupname = $_SESSION['groupname'];
	$field_username = $_SESSION['field_username'];

	if (strlen($locationName)>2)$locationName = str_replace("_"," ", $locationName );
	
	if($locationName !='1'){
		
		$sqlz = "SELECT LOCATIONID,LOCATIONNAME FROM locationdetails WHERE LOCATIONNAME ='".$locationName."'";

	}else{

		if($groupname =='all'){

			$sqlz = "SELECT LOCATIONID,LOCATIONNAME FROM locationdetails";
		}else{
			
			$sqlz = "SELECT a.LOCATIONID,l.LOCATIONNAME FROM admin_group a INNER JOIN locationdetails l ON l.LOCATIONID = a.LOCATIONID WHERE a.GROUPNAME ='".$groupname."'";
		}
	}
		
		$resultz = mysqli_query($con,$sqlz);
		while($rowz = mysqli_fetch_array($resultz , MYSQLI_BOTH)){

			$LOCATIONID = $rowz[0];
			$LOCATIONNAME = $rowz[1];

			$MY_SQL = "select * from ts_users WHERE LOCATIONID = '".$LOCATIONID."'"; 
			//echo $MY_SQL.'<br>';
			$result = mysqli_query($con,$MY_SQL);
			while($rec = mysqli_fetch_array($result , MYSQLI_BOTH)){	

				$name = $rec[3].' '.$rec[4];
				$btn = '<a class="btn btn-warning" href="edit_roster_value.php?'.$rec[0].'?'.$name.'">Edit</a>';
				$date_val[] =['id' => $rec[0],'location' => $LOCATIONNAME,'fname' =>$rec[3],'sname' => $rec[4],'jobtype' => $rec[5],'button' => $btn];		
			
			}
		}
	        
		echo json_encode($date_val);
	}
	if($method=="get_user_data"){
	    
	        $id = $_POST['id'];
		    $sql ="SELECT HOURLYCOST,POSITION FROM ts_users WHERE ID = '".$id."'";
	        $result = mysqli_query($con,$sql);
	        while($row = mysqli_fetch_array($result , MYSQLI_BOTH)){
            		
            	$date_val[] =['wage' => $row[0],'position' => $row[1]];
        		
            }
            
            echo json_encode($date_val);
	}
	
	if($method=="get_userdata"){
	    
	        $id = $_SESSION['codd_did'];
		    $sql ="SELECT FIELD_USERNAME,FIELD_PASSWORD,ID FROM admin_users WHERE ID = '".$id."'";
	        $result = mysqli_query($con,$sql);
	        while($row = mysqli_fetch_array($result , MYSQLI_BOTH)){
            		
            	$date_val[] =['username' => $row[0],'pass' => $row[1],'id' => $row[2]];
        		
            }
            
            echo json_encode($date_val);
	}
	
	if($method=="save_user_data"){
	    
	        $id = $_POST['ids'];
		    $name = $_POST['name'];
		    $pass = $_POST['pass'];
		    
		    $sql ="UPDATE admin_users SET FIELD_USERNAME = '".$name."',FIELD_PASSWORD = '".$pass."' WHERE ID = '".$id."'";
	        $result = mysqli_query($con,$sql);
	        echo json_encode('succes');
	}
	
	if($method=="insertsession"){
	    
	    $s1 = $_POST['s1'];
		$s2 = $_POST['s2'];
		$s3 = $_POST['s3'];
		$s4 = $_POST['s4'];
		$s5 = $_POST['s5'];
		$s6 = $_POST['s6'];
		$user_id = $_POST['user_id'];
		$date = $_POST['date'];
		$wage = $_POST['wage'];
		$dayno = $_POST['dayno'];
		
		
		
		if($s1 !='00:00' && $s2 !='00:00'){
		    $s1_s2 = find_timediff($s1,$s2);
		}else{
		    $s1_s2 = 0;
		}
		
		if($s3 !='00:00' && $s4 !='00:00'){
		    $s3_s4 = find_timediff($s3,$s4);
		}else{
		    $s3_s4 = 0;
		}
		
		if($s5 !='00:00' && $s6 !='00:00'){
		    $s5_s6 = find_timediff($s5,$s6);
		}else{
		    $s5_s6 = 0;
		}
		
	    $hrstotal = $s1_s2 + $s3_s4 + $s5_s6;
	    
	    if($hrstotal !=0){
	    
	        $hrstotal = floor($hrstotal / 60).'.'.($hrstotal -   floor($hrstotal / 60) * 60);
	    }
	    $cost = $wage * $hrstotal;
	  
	    $sql = "INSERT INTO ts_sessions(USERID,SDATE,S1,S2,S3,S4,S5,S6,HOURS,COST,HourlyWage,DAYNO,TYPE) VALUES ('$user_id','$date','$s1','$s2','$s3','$s4','$s5','$s6','$hrstotal','$cost','$wage','$dayno','gen')";
		$result = mysqli_query($con,$sql);
	    
	    echo json_encode('success');
	}
	
	//$method=="updatesession";
	if($method=="updatesession"){
	    
	    $s1 = $_POST['s1'];
		$s2 = $_POST['s2'];
		$s3 = $_POST['s3'];
		$s4 = $_POST['s4'];
		$s5 = $_POST['s5'];
		$s6 = $_POST['s6'];
		$id = $_POST['id'];
		$wage = $_POST['wage'];
		
		
		
		if($s1 !='00:00' && $s2 !='00:00' && $s1 !='' && $s2 !=''){
		    $s1_s2 = find_timediff($s1,$s2);
		    $check_start_end = check_start_end($s1,$s2);
		    if($check_start_end =='0'){
			$s1 =''; $s2 ='';
		    }
		    
		}else{
		    $s1_s2 = 0;
		    $s1 =''; $s2 ='';
		}
		
		if($s3 !='00:00' && $s4 !='00:00' && $s3 !='' && $s4 !=''){
		    $s3_s4 = find_timediff($s3,$s4);
		    $check_start_end = check_start_end($s3,$s4);
		    if($check_start_end =='0'){
			$s3 =''; $s4 ='';
		    }
		    

		}else{
		    $s3_s4 = 0;
		    $s3 =''; $s4 ='';
		}
		
		if($s5 !='00:00' && $s6 !='00:00' && $s5 !='' && $s6 !=''){
		    $s5_s6 = find_timediff($s5,$s6);
		    $check_start_end = check_start_end($s5,$s6);
		    if($check_start_end =='0'){
			$s5 =''; $s6 ='';
		    }
		    
		}else{
		    $s5_s6 = 0;
		    $s5 =''; $s6 ='';
		}
		
	    $hrstotal = $s1_s2 + $s3_s4 + $s5_s6;
	    
	    if($hrstotal !=0){
	    
	        $hrstotal = floor($hrstotal / 60).'.'.($hrstotal -   floor($hrstotal / 60) * 60);
	    }
	    $cost = $wage * $hrstotal;
	  
	    $sql = "UPDATE ts_sessions SET S1='".$s1."', S2='".$s2."', S3='".$s3."', S4='".$s4."', S5='".$s5."', S6='".$s6."', HOURS='".$hrstotal."', COST='".$cost."',HourlyWage ='".$wage."'  WHERE ID='".$id."';";
		$result = mysqli_query($con,$sql);
	    
	    echo json_encode('success');
	}
	
	
	if ($method=="ts_supdate"){
		$id = $_POST['id'];
		$val = $_POST['val'];
		$type = $_POST['type'];
		
		$GROUPID = '8001';
		$LOCATIONID='';
		
		$uval = explode(",",$val);
		
		if(!isset($_SESSION)){
			session_start();
		}
		$bpuserid = $_SESSION['codd_user'];
		$locationname = $_SESSION['codd_location'];
		
		$locationname = str_replace("_"," ", $locationname);
		$sql0 = "SELECT LOCATIONID FROM locationdetails WHERE LOCATIONNAME='$locationname'";
		$result0 = mysqli_query($con,$sql0);
		while($row = mysqli_fetch_array($result0 , MYSQLI_BOTH)){
			$LOCATIONID=$row[0];
		}
		
		$first_name = $uval[0];
		$sur_name = $uval[1];
		$hourly_cost = $uval[2];
		$position = $uval[3];
		
		/*--Identify fortnight--*/
		$fixedfortnight = '2017-02-06';
		$fortnight=''; $tdate=''; $cdate= date('Y-m-d');
		for($i=0; $i<1200; $i++){
			$tdate = date('Y-m-d', strtotime($fixedfortnight." +".(14*$i)." days"));
			if($tdate >= $cdate ){
				$fortnight = $tdate;
				break;
			}
		}
		$fortnight = date('Y-m-d', strtotime($fortnight." -14 days"));
		$fortnight1 = date('Y-m-d', strtotime($fortnight." +1 days"));
		$fortnight2 = date('Y-m-d', strtotime($fortnight." +2 days"));
		$fortnight3 = date('Y-m-d', strtotime($fortnight." +3 days"));
		$fortnight4 = date('Y-m-d', strtotime($fortnight." +4 days"));
		$fortnight5 = date('Y-m-d', strtotime($fortnight." +5 days"));
		$fortnight6 = date('Y-m-d', strtotime($fortnight." +6 days"));
		$fortnight7 = date('Y-m-d', strtotime($fortnight." +7 days"));
		$fortnight8 = date('Y-m-d', strtotime($fortnight." +8 days"));
		$fortnight9 = date('Y-m-d', strtotime($fortnight." +8 days"));
		$fortnight10 = date('Y-m-d', strtotime($fortnight." +10 days"));
		$fortnight11 = date('Y-m-d', strtotime($fortnight." +11 days"));
		$fortnight12 = date('Y-m-d', strtotime($fortnight." +12 days"));
		$fortnight13 = date('Y-m-d', strtotime($fortnight." +13 days"));
		
		
		
		if($type=='temp'){
			$fortnight = '0000-00-00';
			$fortnight1 = '0000-00-00';
			$fortnight2 = '0000-00-00';
			$fortnight3 = '0000-00-00';
			$fortnight4 = '0000-00-00';
			$fortnight5 = '0000-00-00';
			$fortnight6 = '0000-00-00';
			$fortnight7 = '0000-00-00';
			$fortnight8 = '0000-00-00';
			$fortnight9 = '0000-00-00';
			$fortnight10 = '0000-00-00';
			$fortnight11 = '0000-00-00';
			$fortnight12 = '0000-00-00';
			$fortnight13 = '0000-00-00';
		}
		
		$dhours1 = (((strtotime($uval[5]) - strtotime($uval[4]))/60) + ((strtotime($uval[7]) - strtotime($uval[6]))/60))/60;
		$dhours2 = (((strtotime($uval[9]) - strtotime($uval[8]))/60) + ((strtotime($uval[11]) - strtotime($uval[10]))/60))/60;
		$dhours3 = (((strtotime($uval[13]) - strtotime($uval[12]))/60) + ((strtotime($uval[15]) - strtotime($uval[14]))/60))/60;
		$dhours4 = (((strtotime($uval[17]) - strtotime($uval[16]))/60) + ((strtotime($uval[19]) - strtotime($uval[18]))/60))/60;
		$dhours5 = (((strtotime($uval[21]) - strtotime($uval[20]))/60) + ((strtotime($uval[23]) - strtotime($uval[22]))/60))/60;
		$dhours6 = (((strtotime($uval[25]) - strtotime($uval[24]))/60) + ((strtotime($uval[27]) - strtotime($uval[26]))/60))/60;
		$dhours7 = (((strtotime($uval[29]) - strtotime($uval[28]))/60) + ((strtotime($uval[31]) - strtotime($uval[30]))/60))/60;
		$dhours8 = (((strtotime($uval[33]) - strtotime($uval[32]))/60) + ((strtotime($uval[35]) - strtotime($uval[34]))/60))/60;
		$dhours9 = (((strtotime($uval[37]) - strtotime($uval[36]))/60) + ((strtotime($uval[39]) - strtotime($uval[38]))/60))/60;
		$dhours10 = (((strtotime($uval[41]) - strtotime($uval[40]))/60) + ((strtotime($uval[43]) - strtotime($uval[42]))/60))/60;
		$dhours11 = (((strtotime($uval[45]) - strtotime($uval[44]))/60) + ((strtotime($uval[47]) - strtotime($uval[46]))/60))/60;
		$dhours12 = (((strtotime($uval[49]) - strtotime($uval[48]))/60) + ((strtotime($uval[51]) - strtotime($uval[50]))/60))/60;
		$dhours13 = (((strtotime($uval[53]) - strtotime($uval[52]))/60) + ((strtotime($uval[55]) - strtotime($uval[54]))/60))/60;
		$dhours14 = (((strtotime($uval[57]) - strtotime($uval[56]))/60) + ((strtotime($uval[59]) - strtotime($uval[58]))/60))/60;
		
		$cost1 = $hourly_cost * $dhours1;
		$cost2 = $hourly_cost * $dhours2;
		$cost3 = $hourly_cost * $dhours3;
		$cost4 = $hourly_cost * $dhours4;
		$cost5 = $hourly_cost * $dhours5;
		$cost6 = $hourly_cost * $dhours6;
		$cost7 = $hourly_cost * $dhours7;
		$cost8 = $hourly_cost * $dhours8;
		$cost9 = $hourly_cost * $dhours9;
		$cost10 = $hourly_cost * $dhours10;
		$cost11 = $hourly_cost * $dhours11;
		$cost12 = $hourly_cost * $dhours12;
		$cost13 = $hourly_cost * $dhours13;
		$cost14 = $hourly_cost * $dhours14;
		
		//bpuserid
		$bpfirstname =''; $bpsurname ='';
		$rs14='';
		
		if ($id=='0' && $bpuserid >0){
			
			$sql1 = "SELECT USERID, FIRSTNAME, SURNAME FROM users_".$LOCATIONID." WHERE USERID = '$bpuserid'";
			$result1 = mysqli_query($con,$sql1);
			while($row = mysqli_fetch_array($result1 , MYSQLI_BOTH)){
				$bpfirstname = $row[1];
				$bpsurname = $row[2];
			}
			
			$chkid =''; //AVOID DUBLICATE INSERT
			$sql2 = "SELECT BPUSERID FROM ts_users WHERE LOCATIONID='$LOCATIONID' AND FIRSTNAME='$bpfirstname' AND SURNAME='$bpsurname'";
			$result2 = mysqli_query($con,$sql2);
			while($row = mysqli_fetch_array($result2 , MYSQLI_BOTH)){
				$chkid = $row[0];
			}
			if($chkid==''){
				
				$sql3 = "INSERT INTO ts_users( GROUPID, LOCATIONID, FIRSTNAME, SURNAME, POSITION, HOURLYCOST, BPUSERID) VALUES ('$GROUPID','$LOCATIONID','$bpfirstname','$bpsurname','$position','$hourly_cost','$bpuserid')";
				$result3 = mysqli_query($con,$sql3);
				
				$sql4 = "SELECT ID FROM ts_users WHERE LOCATIONID='$LOCATIONID' AND FIRSTNAME='$bpfirstname' AND SURNAME='$bpsurname'";
				$result4 = mysqli_query($con,$sql4);
				while($row = mysqli_fetch_array($result4 , MYSQLI_BOTH)){
					$id = $row[0];
				}
				
				$sql5 = "INSERT INTO ts_sessions( USERID, SDATE, DAYNO, S1, S2, S3, S4, TYPE, HOURS, COST, BPUSERID) VALUES 
					('".$id."','".$fortnight."','0','".$uval[4]."','".$uval[5]."','".$uval[6]."','".$uval[7]."','".$type."','".$dhours1."','".$cost1."','".$bpuserid."')
					,('".$id."','".$fortnight."','1','".$uval[8]."','".$uval[9]."','".$uval[10]."','".$uval[11]."','".$type."','".$dhours2."','".$cost2."','".$bpuserid."')
					,('".$id."'