<?php 
set_time_limit(10000);
include('../dbconnect.php');

function mnltopst($date, $time){

  $convert = new DateTime($date.' '.$time, new DateTimeZone('Asia/Manila'));
  
  
  $convert->setTimezone(new DateTimeZone('America/Los_Angeles'));
  
  $datetime = $convert->format('m/d/Y h:i A');
  
  
  return ($datetime);
  
  
}
function psttomnl($date, $time){
  
$convert = new DateTime($date.' '.$time, new DateTimeZone('America/Los_Angeles'));


$convert->setTimezone(new DateTimeZone('Asia/Manila'));

$datetime = $convert->format('m/d/Y h:i A');


return ($datetime);




}
if($_POST['subsched']){

  $dates = array(); 

    $start = $_POST['start'];
    $end = $_POST['end'];
    $pststart = $_POST['pststart'];
    $pstend = $_POST['pstend'];
    $weekfrom = $_POST['from'];
    $weekto = $_POST['to'];
    $teams = $_POST['team'];
    $employee = array();
    $allteams = array();
    $empteam = array();

    $yearto = substr($weekto, 0, 4);
    $yearfrom = substr($weekfrom, 0, 4);

    


    
   $year = substr($weekfrom, 0, 4);

   


  $from = substr($weekfrom, -2);
  $setfrom = ltrim($from, '0');
  $to = substr($weekto, -2);
  $setto = ltrim($to, '0');

  $week_start = new DateTime();
  $week_start->setISODate($yearfrom,$from);
  $week_start->modify('-1 days');
  $w1=  $week_start->format('Y-m-d');

  $week_end= new DateTime();
  $week_end->setISODate($yearto,$to);
  $week_end->format('Y-m-d');
  $week_end->modify('+6 days');
  $w2 = $week_end->format('Y-m-d');
    

  $period = new DatePeriod(
      new DateTime($w1),
      new DateInterval('P1D'),
      new DateTime($w2)
  );
  $x=0;
  foreach ($period as $periods) {
     $dates[$x] = $periods->format('Y-m-d');
    $x++;
  }
  


  $days = array();
  $weeks = array();
  $month = array();
  $date = array();
  $starttime = array();
    $endtime = array();
    $pststarttime = array();
    $pstendtime = array();

  $numberofweeks = abs(((int)$setto - (int)$setfrom)) + 1;

  for ($e=0; $e < $numberofweeks; $e++) {
      
    $starttime = array_merge($starttime, array($start[0],$start[1],$start[2],$start[3],$start[4],$start[5],$start[6]));
    $endtime = array_merge($endtime, array($end[0],$end[1],$end[2],$end[3],$end[4],$end[5],$end[6]));

    }

    for ($f=0; $f < $numberofweeks; $f++) {
        $pststarttime = array_merge($pststarttime, array($pststart[0],$pststart[1],$pststart[2],$pststart[3],$pststart[4],$pststart[5],$pststart[6]));
        $pstendtime = array_merge($pstendtime, array($pstend[0],$pstend[1],$pstend[2],$pstend[3],$pstend[4],$pstend[5],$pstend[6]));
        }
    



  //get date from week range


   
    $range = range($setfrom, $setto); 
    
    foreach ($range as $week_no) {
        $week_start = new DateTime();
        $week_start->setISODate($year, $week_no);
        $week_start->modify('-2 day');

        $seven_day_week = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
        $week = array();

        for ($i = 0; $i < 7; $i++) {
            $day = $seven_day_week[$i];
            $week[$day] = $week_start->format('Y-m-d');
            
            $week_start->modify('+1 day');

            $date[] = $week_start->format('Y-m-d');
            $month[] = $week_start->format('F');
            $weeks[] = $week_no;
            $days[] = $week_start->format('l');
     
            


        }


    }



    if($teams =='All'){


            //INSERT ORIGINAL ALL  per Team
 
        $allteam = "SELECT team FROM empdetails GROUP BY team";
        $allteamsql = mysqli_query($conn, $allteam);
        while($allteamrow= mysqli_fetch_assoc($allteamsql)){
             $allteams[] = $allteamrow['team'];

        }

        

       
        //get all emp insert all emp sched
        $allemp ="SELECT detailid,team FROM empdetails WHERE status = 'Active' ";
        $allempres = mysqli_query($conn, $allemp);
        while($allrow = mysqli_fetch_assoc($allempres)){
             $employee[] = $allrow['detailid'];
             $empteam[] = $allrow['team'];



        }


          for($g=0; $g < count($employee); $g++){
            for($f=0; $f < count($dates); $f++){

              $schedexist = "SELECT * FROM schedule WHERE empid='$employee[$g]' AND date='$date[$f]'";
              $schedexistres = mysqli_query($conn, $schedexist);
              $schedexrow = mysqli_fetch_assoc($schedexistres);


              
              $weeks = date('W', strtotime($dates[$f]));

              $gethrs = "SELECT * FROM schedule WHERE empid='$employee[$g]' AND week='$weeks' AND starttime !='OFF' AND starttime !='HOLIDAY OFF' LIMIT 1";
              $hrsres = mysqli_query($conn, $gethrs);
              $hrsrow = mysqli_fetch_assoc($hrsres);
              $hrs = $hrsrow['starttime'];

              



              if(!empty($schedexrow['idl'])){

               

                 $pstdate =date('Y-m-d', strtotime(mnltopst($dates[$f],$hrs))) ;

                  $allteam = "UPDATE `schedule` SET `starttime`='$starttime[$f]', `endtime` ='$endtime[$f]', `pststarttime` = '$pststarttime[$f]', `pstendtime` = '$pstendtime[$f]', `pstdate`='$pstdate' WHERE `empid`='$employee[$g]' AND `date`='$dates[$f]'";
              
              }else{

                  $allteam =  "UPDATE `schedule` SET `starttime`='$starttime[$f]', `endtime` ='$endtime[$f]', `pststarttime` = '$pststarttime[$f]', `pstendtime` = '$pstendtime[$f]' WHERE `empid`='$employee[$g]' AND `date`='$dates[$f]'";
              


              }

           








              if(mysqli_query($conn, $allteam)){

                  $alert = '<div class="alert alert-success alert-dismissible fade show" role="alert">
                 Employee schedule update successfully
                 <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                   <span aria-hidden="true">&times;</span>
                 </button>
               </div>';
              
             }else{

                 $alert = '<div class="alert alert-danger alert-dismissible fade show" role="alert">
                Theres error updating employee schedule
                 <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                   <span aria-hidden="true">&times;</span>
                 </button>
               </div>';


             }

  
          }



          }

            

            

            
        
        


    

    }else{

        $allemp ="SELECT detailid,team FROM empdetails WHERE status = 'Active' AND team= '$teams'";
        $allempres = mysqli_query($conn, $allemp);
        while($allrow = mysqli_fetch_assoc($allempres)){
             $employee[] = $allrow['detailid'];
          



        }

   
    




                for($f=0; $f < count($dates); $f++){
    
                    $years = date('Y', strtotime($dates[$f]));
    
                    $schedcode = $employee[$a].$dates[$f];
                     $allteam = "UPDATE schedule SET starttime='$starttime[$f]', endtime ='$endtime[$f]', pststarttime = '$pststarttime[$f]', pstendtime = '$pstendtime[$f]' WHERE team='$teams' AND date='$dates[$f]' AND indi='Original' ";

                   
                     if(mysqli_query($conn, $allteam)){

                        $alert = '<div class="alert alert-success alert-dismissible fade show" role="alert">
                       Employee schedule update successfully
                       <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                         <span aria-hidden="true">&times;</span>
                       </button>
                     </div>';
                    
                   }else{
    
                       $alert = '<div class="alert alert-danger alert-dismissible fade show" role="alert">
                      Theres error updating employee schedule
                       <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                         <span aria-hidden="true">&times;</span>
                       </button>
                     </div>';
    
    
                   }
    
        
                }

                for($g=0; $g < count($employee); $g++){

                  for($f=0; $f < count($dates); $f++){

                    $schedexist = "SELECT * FROM schedule WHERE empid='$employee[$g]' AND date='$dates[$f]'";
                    $schedexistres = mysqli_query($conn, $schedexist);
                    $schedexrow = mysqli_fetch_assoc($schedexistres);
      
      
                    
                    $weeks = date('W', strtotime($dates[$f]));
      
                    $gethrs = "SELECT * FROM schedule WHERE empid='$employee[$g]' AND week='$weeks' AND starttime !='OFF' AND starttime !='HOLIDAY OFF' LIMIT 1";
                    $hrsres = mysqli_query($conn, $gethrs);
                    $hrsrow = mysqli_fetch_assoc($hrsres);
                    $hrs = $hrsrow['starttime'];
      
                    
      
      
      
                    if(!empty($schedexrow['idl'])){
      
                     
      
                       $pstdate =date('Y-m-d', strtotime(mnltopst($dates[$f],$hrs))) ;
      
                        $allteam = "UPDATE `schedule` SET `starttime`='$starttime[$f]', `endtime` ='$endtime[$f]', `pststarttime` = '$pststarttime[$f]', `pstendtime` = '$pstendtime[$f]', `pstdate`='$pstdate' WHERE `empid`='$employee[$g]' AND `date`='$dates[$f]'";
                    
                    }else{
      
                        $allteam =  "UPDATE `schedule` SET `starttime`='$starttime[$f]', `endtime` ='$endtime[$f]', `pststarttime` = '$pststarttime[$f]', `pstendtime` = '$pstendtime[$f]' WHERE `empid`='$employee[$g]' AND `date`='$dates[$f]'";
                    
      
      
                    }

          
      


                  
                    if(mysqli_query($conn, $allteam)){

                        $alert = '<div class="alert alert-success alert-dismissible fade show" role="alert">
                      Employee schedule update successfully
                      <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                      </button>
                    </div>';
                    
                    }else{
      
                        $alert = '<div class="alert alert-danger alert-dismissible fade show" role="alert">
                        Theres error updating employee schedule
                        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                          <span aria-hidden="true">&times;</span>
                        </button>
                      </div>';
      
      
                    }
    
        
                  }
  


                }

    
                
    
                
            

            echo $alert;
  


    }








}

if($_POST['indisubsched']){
  $dates = array();
    $start = $_POST['indistart'];
    $end = $_POST['indiend'];
    $pststart = $_POST['indipststart'];
    $pstend = $_POST['indipstend'];
     $weekfrom = $_POST['indifrom'];
    $weekto = $_POST['indito'];
    $emp = $_POST['emp'];


    $yearto = substr($weekto, 0, 4);
    $yearfrom = substr($weekfrom, 0, 4);

    $year = substr($weekfrom, 0, 4);

   


    $from = substr($weekfrom, -2);
    $setfrom = ltrim($from, '0');
    $to = substr($weekto, -2);
    $setto = ltrim($to, '0');
  

    $week_start = new DateTime();
    $week_start->setISODate($yearfrom,$from);
    $week_start->modify('-1 days');
    $w1=  $week_start->format('Y-m-d');

    $week_end= new DateTime();
    $week_end->setISODate($yearto,$to);
    $week_end->format('Y-m-d');
    $week_end->modify('+6 days');
    $w2 = $week_end->format('Y-m-d');
      

    $period = new DatePeriod(
        new DateTime($w1),
        new DateInterval('P1D'),
        new DateTime($w2)
    );
    $x=0;
    foreach ($period as $periods) {
       $dates[$x] = $periods->format('Y-m-d');      
      $x++;
    }
    
  

  
  
    $days = array();
    $weeks = array();
    $month = array();
    $date = array();
    $starttime = array();
      $endtime = array();
      $pststarttime = array();
      $pstendtime = array();

      
  $numberofweeks = abs(((int)$setto - (int)$setfrom)) + 1;

  for ($e=0; $e < $numberofweeks; $e++) {
      
    $starttime = array_merge($starttime, array($start[0],$start[1],$start[2],$start[3],$start[4],$start[5],$start[6]));
    $endtime = array_merge($endtime, array($end[0],$end[1],$end[2],$end[3],$end[4],$end[5],$end[6]));

    }

    for ($f=0; $f < $numberofweeks; $f++) {
        $pststarttime = array_merge($pststarttime, array($pststart[0],$pststart[1],$pststart[2],$pststart[3],$pststart[4],$pststart[5],$pststart[6]));
        $pstendtime = array_merge($pstendtime, array($pstend[0],$pstend[1],$pstend[2],$pstend[3],$pstend[4],$pstend[5],$pstend[6]));
        }
    


      $allemp ="SELECT detailid,team FROM empdetails WHERE detailid='$emp'";
      $allempres = mysqli_query($conn, $allemp);
        $allrow = mysqli_fetch_assoc($allempres);
           $empteam = $allrow['team'];

           $range = range($setfrom, $setto); 
    
           foreach ($range as $week_no) {
               $week_start = new DateTime();
               $week_start->setISODate($year, $week_no);
               $week_start->modify('-1 day');
       
               $seven_day_week = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
               $week = array();
       
               for ($i = 0; $i < 7; $i++) {
                   $day = $seven_day_week[$i];
                   $week[$day] = $week_start->format('Y-m-d');
                   
                   
       
                   $date[] = $week_start->format('Y-m-d');
                   $month[] = $week_start->format('F');
                    $weeks[] = $week_no;
                   $days[] = $week_start->format('l');


                   $week_start->modify('+1 day');

            
                   
       
       
               }
       
       
           }
        



      

 
          



              for($f=0; $f < count($dates); $f++){
  
                  $years = date('Y', strtotime($dates[$f]));
                
  
                  $schedcode = $emp.$dates[$f];
                  $Ymddate = $dates[$f];

                  $schedexist = "SELECT * FROM schedule WHERE empid='$emp' AND date='$dates[$f]'";
                  $schedexistres = mysqli_query($conn, $schedexist);
                  $schedexrow = mysqli_fetch_assoc($schedexistres);
   
   
                  if(!empty($schedexrow['idl'])){
					  
					  if($starttime[$f] == 'OFF' || $starttime[$f] == 'HOLIDAY OFF'){
						  
						  $pstdate =date('Y-m-d', strtotime(mnltopst($Ymddate,'00:00'))) ;
						  
					  }else{
						  $pstdate =date('Y-m-d', strtotime(mnltopst($Ymddate,$starttime[$f]))) ;
						  
					  }

                     

                     $allteam = "UPDATE schedule SET starttime='$starttime[$f]', endtime ='$endtime[$f]', pststarttime = '$pststarttime[$f]', pstendtime = '$pstendtime[$f]', pstdate='$pstdate' WHERE empid='$emp'  AND date='$dates[$f]' ";
                  
                  }else{
   
                     $allteam =  "UPDATE schedule SET starttime='$starttime[$f]', endtime ='$endtime[$f]', pststarttime = '$pststarttime[$f]', pstendtime = '$pstendtime[$f]' WHERE empid='$emp' AND date='$dates[$f]' ";
                  
   
   
                  }

                 
   



                   

                   if(mysqli_query($conn, $allteam)){

                    $alert = '<div class="alert alert-success alert-dismissible fade show" role="alert">
                   Employee schedule update successfully
                   <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                     <span aria-hidden="true">&times;</span>
                   </button>
                 </div>';
                
               }else{

                   $alert = '<div class="alert alert-danger alert-dismissible fade show" role="alert">
                  Theres error updating employee schedule
                   <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                     <span aria-hidden="true">&times;</span>
                   </button>
                 </div>';


               }

              }

              echo $alert;
			  
			  mysqli_close($conn);
  
              
  
              
          
  
    

    








}

?>