<?php

require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');

$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);
$admin = $_SESSION["admin_" . $dbname];
$fseries = $_SESSION["society_" . $dbname];
$keycond = "and `series`= '$fseries'";
$zroot = $_SESSION["zdbroot"];

$zyear = right($zdb, 4);

$mfrdate = getzdates($zyear, 'S');
$mtodate = getzdates($zyear, 'E');

if (isset($_REQUEST['billcycle'])) {
    $billcycle = $_REQUEST['billcycle'];
    $billcode = $_REQUEST['billcode'];
    $member = $_REQUEST['member'];
    $yearno = $_REQUEST['yearno'];

    if (!empty($member)) {
        $memcond = " and `accode` = '$member'";
    }
//    $vouchdt = $_REQUEST[vouchdt];

//    switch ($billcycle) {
//        case 'M': if ($billcode < 4) {
//                $cdate = strtotime($mtodate);
//            } else {
//                $cdate = strtotime($mfrdate);
//            }
//
//            $chkdate = date("Y", $cdate);
//            break;
//        case 'BM': if ($billcode == 6) {
//                $cdate = strtotime($mtodate);
//            } else {
//                $cdate = strtotime($mfrdate);
//            }
//
//            $chkdate = date("Y", $cdate);
//            break;
//        case 'Q': if ($billcode == 4) {
//                $cdate = strtotime($mtodate);
//            } else {
//                $cdate = strtotime($mfrdate);
//            }
//
//            $chkdate = date("Y", $cdate);
//            break;
//        case 'H': if ($billcode == 2) {
//                $cdate = strtotime($mtodate);
//            } else {
//                $cdate = strtotime($mfrdate);
//            }
//
//            $chkdate = date("Y", $cdate);
//            break;
//        default: $chkdate = '';
//    }
    
    


    $gbilcode = mysqli_query($conn, "SELECT checkdate,descrip FROM $zroot.`billingcycle` Where `billcycle`='$billcycle' and `code` = '$billcode' ") or die(mysqli_error($conn));
    $fetbilcode = mysqli_fetch_array($gbilcode);
    $bdescrip = $fetbilcode['descrip'];

    $chkdt = $fetbilcode['checkdate'] . $yearno;
    $checkdate = date("Y-m-d", strtotime($chkdt));


    // current billcode
    $billexist = mysqli_query($conn, "SELECT * FROM $zdb.`billmain` Where `billcycle`='$billcycle' and `billcode` = '$billcode' and 
                             `society` = '$fseries' and yearno = '$yearno' $memcond") or die(mysqli_error($conn));
    $gbillexist = mysqli_num_rows($billexist);
    // billdate check
    $checkexist = mysqli_query($conn, "SELECT * FROM $zdb.`billmain` Where `billcycle`='$billcycle' and `trandate` > '$checkdate' and 
                              `society` = '$fseries' and yearno = '$yearno' $memcond") or die(mysqli_error($conn));
    $gcheckexist = mysqli_num_rows($checkexist);


    if ($gbillexist > 0) {
        $billcheck = 1;
    } else {
        $billcheck = '';
    }

    if ($gcheckexist > 0) {
        $check = 1;
    } else {
        $check = '';
    }


    $rows = array('descrip' => $bdescrip, 'chkdescrip' => $chkdescrip, 'billcheck' => $billcheck, 'check' => $check, 'checkdt' => $checkdate);

    header("Content-type: application\json");
    echo json_encode($rows);
}