0xV3NOMx
Linux ip-172-26-7-228 5.4.0-1103-aws #111~18.04.1-Ubuntu SMP Tue May 23 20:04:10 UTC 2023 x86_64



Your IP : 18.219.150.195


Current Path : /var/www/html/oums/api/
Upload File :
Current File : /var/www/html/oums/api/saveAbLeave.php

<?php

include("sys_connect.php");
if(!isset($_POST["emplcode"]) || !isset($_POST["deptcode"]) 
|| !isset($_POST["attstatus"]) || !isset($_POST["fromdate"])
|| !isset($_POST["todate"]) || !isset($_POST["loguser"]) 
|| $_POST["emplcode"] == "" || $_POST["deptcode"] == ""
|| $_POST["attstatus"] == "" || $_POST["fromdate"] == ""
|| $_POST["todate"] == "" || $_POST["loguser"] == "" ){
    echo json_encode(array("msg" => "Invalid Params", "error_code" => -1));
    die();    
}

$emplcode = $_POST["emplcode"];
$deptcode = $_POST["deptcode"];
$attstatus = $_POST["attstatus"];
$fromdate = $_POST["fromdate"];
$todate = $_POST["todate"];
$loguser = $_POST["loguser"];

if($fromdate < date('Y-m-d') || $todate < date('Y-m-d')) {
    echo json_encode(array("msg" => "Dates should be greater than or equal to current date",
         "error_code" => -1));
    die();    
}

$begin = new DateTime($fromdate);
$end = new DateTime($todate);
$cur = new DateTime(date('Y-m-d'));
$cur->modify('+30 day'); 

if($begin > $cur || $end > $cur ) {
    echo json_encode(array("msg" => "Dates should be within 30 days.",
         "error_code" => -1));
    die();    
}

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



$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $end);

foreach ($period as $dt) {
    $date = $dt->format("Y-m-d");
    $query = "select * from attendance where fattndate = '{$date}' 
            and femplcode = '{$emplcode}'";
    $myres = mysqli_query($mycon, $query);
    $mycnt = mysqli_num_rows($myres);
    if($mycnt > 0) {
        echo json_encode(array("error_code" => -1, 
            'msg' => "Attendance marked for the date {$dt->format("d-m-Y")}. Modification not allowed" )); 
        break;
    }
    else {
        $query = "insert into attendance(fattndate, fdeptcode, femplcode, fattnstat, fupdtuser, fupdttime )
        values('{$date}', '{$deptcode}', '{$emplcode}', '{$attstatus}', '{$loguser}', now())";
        // var_dump($query);
        $res = mysqli_query($mycon, $query);
        if($res) {
            echo json_encode(array("error_code" => 0, 'msg' => "Updated Successfully." ));
        } else {
            echo json_encode(array("error_code" => -1, 'msg' => "Error While Updating" ));
        }
    }
}