<?php

$r = array();
$r["a"] = array();
$r["t"] = array();

require_once __DIR__ . '/db_config.php';

if (isset($_POST["companyname"])) {
$con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD,DB_DATABASE) or die(mysql_error());

$companyname = $_POST['companyname'];
$date = $_POST['date'];
$atm = $_POST['atm'];

//$companyname ="zkitchen";
//$date="2020-01-26";
//$atmid='B00100023';

$query1="SELECT a.`location`,a.`state`,aa.emailid as checkin_id,aaa.emailid as checkout_id,aa.checkintime,aaa.checkouttime,a.atmid,
a.`sitename`,a.`atmaddress`,aa.facility_id,aa.date,aaa.facility_id_out,aaa.date,s.employeename as checkinperson,ss.employeename as checkoutperson,
    s.employeename,s.emailid
    FROM atmdetails a 
    LEFT JOIN attendance aa ON aa.facility_id=a.atmid AND aa.date='$date'
    LEFT JOIN attendance aaa ON aaa.facility_id_out=a.atmid AND aaa.date='$date'
    LEFT JOIN signupdetails ss ON aaa.emailid=ss.emailid
    LEFT JOIN signupdetails s ON  s.emailid=aa.emailid
    WHERE a.`companyname`='$companyname' AND a.atmid NOT IN ('Registered_address','Head Office') AND a.atmid='$atm'";
$result = mysqli_query($con,$query1);
if(!empty($result) && mysqli_num_rows($result) > 0){
    while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
        array_push($r["a"], $row);
    }
}

$query2="SELECT t.timesheetdate,t.starttime,t.endtime,t.activity,t.employeemailid,s.employeename
FROM atmdetails a 
LEFT JOIN timesheet t ON t.facility_id=a.atmid AND t.timesheetdate='$date'
LEFT JOIN signupdetails s ON s.emailid=t.employeemailid
WHERE s.`companyname`='$companyname' AND a.atmid NOT IN ('Registered_address','Head Office')
AND a.atmid='$atm'";

$result = mysqli_query($con,$query2);
if(!empty($result) && mysqli_num_rows($result) > 0){
    while ($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
        array_push($r["t"], $row);
    }
}
mysqli_close($con);               
echo json_encode($r);
}
?>


