<?php
session_start();
require_once '../system/conn.php';
if(isset($_GET['q'])){
    $date=explode('-',date("Y-m-d", strtotime("last day of previous month")));
    $month=$date[1];
    $year=$date[0];
    
    $delete=$conn->query(" DELETE FROM `admin_salary_attendance` WHERE `month`='$month' AND `year`='$year' ");
    if($delete){
        $delete_sa = $conn->query(" DELETE FROM `admin_emp_salary` WHERE `month`='$month' AND `year`='$year' ");
        if($delete_sa){
          $_SESSION['delete']="Data deleted successfully . Click generate for re-regenerate";
          echo "<script>window.location.assign('gross-sheet.php')</script>";
        } 
    }
    
    $selectss=$conn->query(" SELECT * FROM `attendance` WHERE `month`='$month' and `year`='$year' ");
    while($rowss=$selectss->fetch_assoc()){
      
      $select_site=$conn->query(" SELECT `name` from `site` where id='".$rowss['site']."' ");
      $fetch_site=$select_site->fetch_assoc();
      $att_detail=$fetch_site['name']."-".$rowss['attendance'];
        
        $s_s=$conn->query(" SELECT `ot` FROM `emp_details` where `e_id`='".$rowss['emp_id']."' and `branch`='".$rowss['branch']."' ");
        $r_r=$s_s->fetch_assoc();
        $ot_status=$r_r['ot'];
        
          $select=$conn->query(" SELECT * FROM `admin_salary_attendance` WHERE `month`='".$rowss['month']."' and `year`='".$rowss['year']."' and `emp_id`='".$rowss['emp_id']."' and `branch`='".$rowss['branch']."' ");
          $row=$select->fetch_assoc();
          $count=$select->num_rows;
          
          if($count>0){
            
            $new_ta = $row['t_a']+$rowss['attendance'];
            $new_ba = $row['b_a']+$rowss['attendance'];
            $manual_advance = $row['maual_advance']+$rowss['advance_deduction'];
            $spc_allow = $row['spc_allow']+$rowss['spc_allow'];
            $new_details = $row['details'].", ".$att_detail;
            
            if($ot_status=="")
            {
              $o_a=0;
              if($new_ba<26)
              {
                $b_a=$new_ba;
              } else {
                $b_a=26;
              }
            } else {
              if($rowss['only_ot']=="yes")
              {
                $b_a=$row['b_a'];
                $o_a=$row['o_a']+$rowss['attendance'];
              } else {
                if($new_ba<26) {
                  $b_a=$new_ba;
                  $o_a=$row['o_a'];
                } else {
                  $b_a=26;
                  $extra_oa=$new_ba-26;
                  $o_a=$row['o_a']+$extra_oa;
                }
              }
            }
            $update=$conn->query("UPDATE `admin_salary_attendance` SET `t_a`='$new_ta',`b_a`='$b_a',`o_a`='$o_a',`block`='".$rowss['block']."',`cheque`='".$rowss['check']."',`maual_advance`='$manual_advance',`spc_allow`='$spc_allow',`details`='$new_details' where `emp_id`='".$rowss['emp_id']."'");
            if($update){
              $success="Successfull";
            }
        } else {
            $attendance=$rowss['attendance'];
            if($ot_status=="")
            {
            $o_a=0;
                if($attendance<26)
                {
                    $b_a=$attendance;
                } else {
                    $b_a=26;
                }
            }
            else
            {
                if($rowss['only_ot']=="yes")
                {
                    $b_a=0;
                    $o_a=$attendance;
                } else {
                    if($attendance<26)
                    {
                        $b_a=$attendance;
                        $o_a=0;
                    } else {
                        $b_a=26;
                        $o_a=$attendance-26;
                    }
                }
            } 

            $insert=$conn->query("INSERT INTO `admin_salary_attendance`(`emp_id`, `month`, `year`, `t_a`, `b_a`, `o_a`, `block`, `cheque` , `branch`, `maual_advance`, `spc_allow`, `details`) VALUES ('".$rowss['emp_id']."','".$rowss['month']."','".$rowss['year']."','$attendance','$b_a','$o_a','".$rowss['block']."','".$rowss['check']."','".$rowss['branch']."','".$rowss['advance_deduction']."','".$rowss['spc_allow']."','$att_detail')");
            //print_r($insert);exit;
            if($insert){
                $success="Successfull";
            }
        }
    }
}
?>