<?php
    session_start();
    if(!$_SESSION['id']){
        header('location:index.php');
    }
   include('system/conn.php');
   
   if(isset($_POST["submit"]))
   {
    $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 `admin_prv_ot_details` select * from `admin_ot_details` where `month` NOT IN ('$month','$p_month') AND `year` NOT IN ('$year','$p_year') ";
    $statement = $conn->prepare($insert)->execute();

    if($statement){
        $delete=$conn->query(" DELETE FROM `admin_ot_details` WHERE `month` NOT IN ('$month','$p_month') AND `year` NOT IN ('$year','$p_year') ");
        if($delete){
            $_SESSION['s_sa']="Archive ot data successfully generate.";
            echo "<script>window.location.assign('archive.php')</script>";
          } else {
            $_SESSION['s_sa']="Archive ot data not generate successfully.";
            echo "<script>window.location.assign('archive.php')</script>"; 
          }
    }
    
   }

?>