<?php
  session_start();
  if(!$_SESSION['id']){
    header('location:index.php');
  }
   require_once '../system/conn.php';
   
   
    $date1=date('Y-m-d');
    $date=date('Y-m', strtotime(date('Y-m')." -1 month"));
    $date=explode('-',$date);
    $month=$date[1];
    $monthNum  = $month;
    $dateObj   = DateTime::createFromFormat('!m', $monthNum);
    $monthName = $dateObj->format('F');
    $year=$date[0];
   
   
    $p_date=date('Y-m', strtotime(date('Y-m')." -2 month"));
    $p_date1=explode('-',$p_date);
    $p_month=$p_date1[1];
    $p_monthNum  = $p_month;
    $p_dateObj   = DateTime::createFromFormat('!m', $p_monthNum);
    $p_monthName = $p_dateObj->format('F');
    $p_year=$p_date1[0];
   
    $insert=" INSERT IGNORE INTO `prv_emp_salary` select * from `emp_salary` where `month` NOT IN ('$month') OR `year` NOT IN ('$year') ";
    $statement = $conn->prepare($insert)->execute();
    if($statement){
      $insert1=" INSERT IGNORE INTO `prv_attendance` select * from `attendance` where `month` NOT IN ('$month') OR `year` NOT IN ('$year') ";
      //print_r($insert);exit;
      $statement1 = $conn->prepare($insert1)->execute();
      if($statement1){
         $insert2=" INSERT IGNORE INTO `prv_salary_attendance` select * from `salary_attendance` where `month` NOT IN ('$month') OR `year` NOT IN ('$year') ";
          $statement2 = $conn->prepare($insert2)->execute();
          if($statement2){
          
          $insert3=" INSERT IGNORE INTO `prv_ot_details` select * from `ot_details` where `month` NOT IN ('$month') OR `year` NOT IN ('$year') ";
          $statement3 = $conn->prepare($insert3)->execute();
          
          if($statement3){
          
          
          
          
        $delete=$conn->query(" DELETE FROM `emp_salary` WHERE `month` NOT IN ('$month') OR `year` NOT IN ('$year') ");
        if($delete){
          $delete1=$conn->query(" DELETE FROM `attendance` WHERE `month` NOT IN ('$month') OR `year` NOT IN ('$year') ");
          if($delete){
          $delete2=$conn->query(" DELETE FROM `salary_attendance` WHERE `month` NOT IN ('$month') OR `year` NOT IN ('$year') ");
        
           if($delete2){
           $delete3=$conn->query(" DELETE FROM `ot_details` WHERE `month` NOT IN ('$month') OR `year` NOT IN ('$year') ");
        
                if($delete3){
                    $_SESSION['delete']="Archive data successfully generate.";
                    echo "<script>window.location.assign('archive.php')</script>";
                  } else {
                    $_SESSION['delete']="Archive data not generate successfully.";
                    echo "<script>window.location.assign('archive.php')</script>"; 
                  } 
          }                  
                  
           }
         }
         
         
         
          }
         
        }
      }
    
    
   }

?>