<?php
include("../../db.php");
header('Content-Type: application/json');

$session = $_GET['session'];

if (isset($_GET['session']) && !empty($_GET['session'])){


    $date = date("Y-m-d");


    $paymode = $db->getRows("select distinct(payment_mode) from receipt where payment_mode!='' and udate='$date' ");

    //$paymode = $db->getRows("select distinct(payment_mode) from receipt ");

    $collection_array = array(); $tempary = [];

    foreach ($paymode as $mode) {

        $amount = $db->getVal("select sum(amount) from receipt where payment_mode='$mode[payment_mode]' and udate='$date' ");

        if ($amount == null){
            $amount = 0;
        }
        $tempary[$mode['payment_mode']] += $amount;

        // $collection_array[] = array("payment_mode" => $mode["payment_mode"], "amount" => number_format($amount, 2), "amount_number" => $amount);

    }

    $hostelpay = $db->getRows("select distinct(payment_mode) from hostel_receipt where payment_mode!='' and udate='$date' ");
    foreach($hostelpay as $mode){
        $amount = $db->getVal("select sum(amount) from hostel_receipt where payment_mode='$mode[payment_mode]' and udate='$date' ");
        $tempary[$mode['payment_mode']] += $amount;
    }
    
    $enquirypay = $db->getRows("select distinct(payment_mode) from enquiry_receipt where payment_mode!='' and udate='$date' ");
    foreach($enquirypay as $mode){
        $amount = $db->getVal("select sum(amount) from enquiry_receipt where payment_mode='$mode[payment_mode]' and udate='$date' ");
        $tempary[$mode['payment_mode']] += $amount;
    }
    
    $inventorypay = $db->getRows("select distinct(payment_mode) from inventory_receipt where payment_mode!='' and issue_date like '$date%' ");
    foreach($inventorypay as $mode){
        $amount = $db->getVal("select sum(total_amount) from inventory_receipt where payment_mode='$mode[payment_mode]' and issue_date like '$date%' ");
        $tempary[$mode['payment_mode']] += $amount;
    }
    
    $transportpay = $db->getRows("select distinct(payment_mode) from transport_receipt where payment_mode!='' and udate='$date' ");
    foreach($transportpay as $mode){
        $amount = $db->getVal("select sum(amount) from transport_receipt where payment_mode='$mode[payment_mode]' and udate='$date' ");
        $tempary[$mode['payment_mode']] += $amount;
    }
    
    foreach($tempary as $key=>$val){
        // array_push($dataPoints,["label"=>$key, "y"=>$val]);
        $collection_array[] = array("payment_mode" => $key, "amount" => number_format($val, 2), "amount_number" => $val);
    }

///

    $fdate = date('Y')."-".date('m')."-".date('d');
    $tdate = date('Y-m-d', strtotime("+1 day"));
    $conwhere = "and (r.date between '$fdate' and '$tdate')";

    $cla= $db->getRows("select class from class");

    $total_student=0;
    $total_absent = 0;
    $tf=0;
    foreach($cla as $cl){
        $total_student+= $db->getVal("SELECT count(idno) FROM student WHERE class='$cl[class]' AND school_id='1' and session='$session' and issue_TC=0 and status=1 ");

        $total_absent+=$db->getVal("select count(*) from attandenc as r INNER join student as s on s.idno=r.idno where s.class='$cl[class]' $conwhere and r.type='2' ") ;

    }
    $total_present = $total_student - $total_absent;



////////////////////////////////////////////////////////


    //$fdate = date('Y')."-".date('m')."-".date('d');
    //$todate = date('Y-m-d', strtotime("+1 day"));

    $records = $db->getRows("SELECT * FROM staff WHERE session='$session' and status='1' ");
    $total_teachers = count($records);
    $teacher_present = 0;
    $i=0;

    foreach($records as $rec){
        $i++;
        $result = $db->getRow("Select * from attendance_new where teacher_id='$rec[idno]' and (timestamp>='$fdate' and timestamp<='$tdate') and session='$session' and att_type='IN' ");
        if($result['att_type']=="IN"){
            $teacher_present++;
        }
    }

    $teacher_absent = $total_teachers-$teacher_present;

    $income = $db->getVal("select sum(amount) from receipt where udate='$date' ");

    $expense = $db->getVal("SELECT sum(amount) FROM transaction where school_id='1' and type='withdraw' and status=1 and udate like '%$date%' ");

    $reception = $db->getVal("select sum(amount) from enquiry_receipt where udate='$date' ");
    
    $hostel = $db->getVal("select sum(amount) from hostel_receipt where udate='$date' ");
    
    $transportfee = $db->getVal("select sum(amount) from transport_receipt where udate='$date' ");
    
    $inventoryfee = $db->getVal("select sum(total_amount) from inventory_receipt where issue_date like '$date%' ");

$total_collection = number_format($income + $hostel + $reception+$transportfee+$inventoryfee,2);

$discount = $db->getVal("select sum(discount_amount) from discount_detail where date like '".$date."%' ");

    $data = array("status" => 1, "income" => number_format($income, 2), "expense" => number_format($expense, 2), "hostel" => number_format($hostel, 2),
        "registration" => number_format($reception,2), "transport" => number_format($transportfee,2), "inventory"   => number_format($inventoryfee,2),
         "total_collection" => $total_collection, "discount" => number_format($discount,2), "collections" => $collection_array, "total_student" => $total_student,
        "total_absent" => $total_absent, "total_present" => $total_present,
        "total_teacher" => $total_teachers, "teacher_absent" => $teacher_absent, "teacher_present" => $teacher_present);

    echo json_encode($data);

}else{

    $data = array("status" => 0, "message" => "Session not found error..!");

    echo json_encode($data);

}

