<?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];

if (isset($_REQUEST['vouchdt'])) {
    $vouchdt = $_REQUEST['vouchdt'];
    $y = $_REQUEST['y'];
    $mon = $_REQUEST['mon'];
    $day = $_REQUEST['day'];
    if ($mon < 10) {
        $mon = '0' . $mon;
    }
    if ($day < 10) {
        $day = '0' . $day;
    }

    $fdate1 = $y . '-' . $mon . '-' . '01';
    $tdate1 = $y . '-' . $mon . '-' . '07';

    $fdate2 = $y . '-' . $mon . '-' . '08';
    $tdate2 = $y . '-' . $mon . '-' . '15';

    $fdate3 = $y . '-' . $mon . '-' . '16';
    $tdate3 = $y . '-' . $mon . '-' . '22';

    
    $nmondate = date('Y-m-d', strtotime($vouchdt . ' + 1 months'));
    $time1 = strtotime($nmondate);
    $ncmonno = date("m", $time1);

    $ncyearno = date("Y", $time1);


    $ndate = $ncyearno . '-' . $ncmonno . '-01';

    IF ($monno == 12) {
        $fdate4 = $y . '-' . $mon . '-' . '23';
        $tdate4 = $y . '-' . $mon . '-' . '31';
    } else {
        $fdate4 = $y . '-' . $mon . '-23';
        $tdate4 = date('Y-m-d', strtotime($ndate . ' - 1 days'));
    }


    $lmondate = date('Y-m-d', strtotime($vouchdt . ' - 1 months'));
    $ltime1 = strtotime($lmondate);
    $lcmonno = date("m", $ltime1);
    $lcyearno = date("Y", $ltime1);

    $fdate0 = $lcyearno . '-' . $lcmonno . '-23';
    $tdate0 = date('Y-m-d', strtotime($fdate1 . ' - 1 days'));
    $elimit = 1000;


//***************  monthly
    $fmdate1 = $fdate1;
    $tmdate1 = date('Y-m-d', strtotime($ndate . ' - 1 days'));

    $fmdate0 = $lcyearno . '-' . $lcmonno . '-01';
    $tmdate0 = $tdate0;


//***************  half monthly

    $f15date1 = $fdate1;
    $t15date1 = $y . '-' . $mon . '-15';

    $f15date2 = $y . '-' . $mon . '-16';
    $t15date2 = date('Y-m-d', strtotime($ndate . ' - 1 days'));

    $f15date0 = $lcyearno . '-' . $lcmonno . '-16';
    $t15date0 = $tdate0;


//***************  ten days
    $f10date1 = $fdate1;
    $t10date1 = $y . '-' . $mon . '-10';
//
    $f10date2 = $y . '-' . $mon . '-10';
    $t10date2 = $y . '-' . $mon . '-20';
//
    $f10date3 = $y . '-' . $mon . '-21';
    $t10date3 = date('Y-m-d', strtotime($ndate . ' - 1 days'));
//
    $f10date0 = $lcyearno . '-' . $lcmonno . '-21';
    $t10date0 = $tdate0;

//************************************
//
//

    mysqli_query($conn, "Create temporary table if not exists `folwblk`  SELECT accode, custcode, name, billcycle, credtype, openbal, dc,
                                SPACE(30) as cyclehead, 0000000000.00 as prelast, SPACE(1) as folblk, 
                                0000000000.00 as lastbill, 0000000000.00 as recd, 0000000000.00 as balance FROM accmast 
                                WHERE grpcode = '010' AND custcode > 0 AND accode != '49' AND credtype = 'S' ORDER BY custcode") or die(mysqli_error($conn));

    $getquery = mysqli_query($conn, "SELECT * FROM `folwblk` ORDER BY custcode") or die(mysqli_error($conn));
    $xtlamt = $xramt = $tillamt = $pxtlamt = $pxramt = $ptillamt = 0;


    while ($fetquery = mysqli_fetch_array($getquery)) {
        $maccode = $fetquery['accode'];
        $mbcycle = $fetquery['billcycle'];
        $mcredtype = $fetquery['credtype'];
        $mopenbal = $fetquery['openbal'];
        $mdc = $fetquery['dc'];

        switch ($mbcycle) {
            case "W":
                $mperiod = 1;
                switch ($vouchdt) {
                    case $vouchdt >= $fdate1 && $vouchdt <= $tdate1:
                        $mperiod = 1;
                        $frdate = $fdate1;
                        $foldate = $vouchdt;
                        $tilldate = $tdate0;
//                        $pretilldt = date('Y-m-d', strtotime($fdate0 . ' - 1 days'));

                        break;

                    case $vouchdt >= $fdate2 && $vouchdt <= $tdate2:
                        $mperiod = 2;
                        $frdate = date('Y-m-d', strtotime($tdate1 . ' + 1 days'));
                        $foldate = $vouchdt;
                        $tilldate = $tdate1;
//                        $pretilldt = date('Y-m-d', strtotime($fdate1 . ' - 1 days'));

                        break;

                    case $vouchdt >= $fdate3 && $vouchdt <= $tdate3:
                        $mperiod = 3;
                        $frdate = date('Y-m-d', strtotime($tdate2 . ' + 1 days'));
                        $foldate = $vouchdt;
                        $tilldate = $tdate2;
//                        $pretilldt = date('Y-m-d', strtotime($fdate1 . ' - 1 days'));

                        break;

                    case $vouchdt >= $fdate4 && $vouchdt <= $tdate4:
                        $mperiod = 4;
                        $frdate = date('Y-m-d', strtotime($tdate3 . ' + 1 days'));
                        $foldate = $vouchdt;
                        $tilldate = $tdate3;
//                        $pretilldt = date('Y-m-d', strtotime($fdate1 . ' - 1 days'));

                        break;
                }
            case "D":
                $tilldate = date('Y-m-d', strtotime($vouchdt . ' - 1 days'));
                $frdate = date('Y-m-d', strtotime($tilldate . ' + 1 days'));
                $foldate = $vouchdt;
//                $pretilldt = date('Y-m-d', strtotime($tilldate . ' - 1 days'));
                break;

            case "M":
                $tilldate = $tmdate0;
                $frdate = date('Y-m-d', strtotime($tilldate . ' + 1 days'));
                $foldate = $vouchdt;
//                $pretilldt = date('Y-m-d', strtotime($tmdate0 . ' - 1 months'));
                break;
            case "H":
                $graced = 7;
                switch ($vouchdt) {
                    case $vouchdt >= $f15date1 && $vouchdt <= $t15date1:
                        $frdate = $f15date1;
//                        $todate = $t15date0;

                        $foldate = date('Y-m-d', strtotime($f15date1 . ' +  ' . $graced . ' days'));

                        $tilldate = $t15date0;
//                        $pretilldt = date('Y-m-d', strtotime($f15date0 . ' - 1 days'));

                        break;

                    case $vouchdt >= $f15date2 && $vouchdt <= $t15date2:
                        $frdate = $f15date2;
//                        $todate = $t15date1;

                        $foldate = date('Y-m-d', strtotime($f15date2 . ' +  ' . $graced . ' days'));

                        $tilldate = $f15date1;
//                        $pretilldt = date('Y-m-d', strtotime($f15date1 . ' - 1 days'));

                        break;

                    default:
                        break;
                }
                break;

            case "T":
                $graced = 5;
                switch ($vouchdt) {
                    case $vouchdt >= $f10date1 && $vouchdt <= $t10date1:
                        $frdate = $f10date1;

                        $foldate = date('Y-m-d', strtotime($f10date1 . ' +  ' . $graced . ' days'));

                        $tilldate = $t10date0;
//                        $pretilldt = date('Y-m-d', strtotime($tilldate . ' - 10 days'));

                        break;

                    case $vouchdt >= $f10date2 && $vouchdt <= $t10date2:
                        $frdate = $f10date2;

                        $foldate = date('Y-m-d', strtotime($f10date2 . ' +  ' . $graced . ' days'));

                        $tilldate = $t10date1;
//                        $pretilldt = date('Y-m-d', strtotime($tilldate . ' - 10 days'));
                        break;

                    case $vouchdt >= $f10date3 && $vouchdt <= $t10date3:
                        $frdate = $f10date3;

                        $foldate = date('Y-m-d', strtotime($f10date3 . ' +  ' . $graced . ' days'));

                        $tilldate = $t10date2;
//                        $pretilldt = date('Y-m-d', strtotime($tilldate . ' - 10 days'));
                        break;

                    default:
                        break;
                }
            default:
                break;
        }

        if ($mdc == "D") {
            $xxmopen = $mopenbal * -1;
        } else {
            $xxmopen = $mopenbal;
        }

        $getbal = mysqli_query($conn, "SELECT SUM(amount) as amt FROM `actran` WHERE accode = '$maccode' AND 
                                                           `date` <= '$tilldate' and `retdate` = '0000-00-00'") or die(mysqli_error($conn));
        $getamt = mysqli_fetch_array($getbal);
        $xtlamt = $getamt['amt'];

        $tillamt = $xxmopen + $xtlamt;


        $getbal3 = mysqli_query($conn, "SELECT * FROM `actran` WHERE accode = '$maccode' AND  amount > 0 and
                                                           `date` >= '$frdate' and date <= '$foldate' and `retdate` = '0000-00-00'") or die(mysqli_error($conn));
        $getamt3 = mysqli_fetch_array($getbal3);
        $xramt = $getamt3['amt'];

        $prelast = $tillamt * -1;
        $recd = $xramt;

        switch ($mbcycle) {
            case "W":
                switch ($vouchdt) {
                    case $vouchdt >= $fdate1 && $vouchdt <= $tdate1:
                        if ($day > 3) {
                            $folblk = "Y";
                        } else {
                            $folblk = "N";
                        }
                        break;
                    case $vouchdt >= $fdate2 && $vouchdt <= $tdate2:
                        if ($day > 10) {
                            $folblk = "Y";
                        } else {
                            $folblk = "N";
                        }
                        break;
                    case $vouchdt >= $fdate3 && $vouchdt <= $tdate3:
                        if ($day > 18) {
                            $folblk = "Y";
                        } else {
                            $folblk = "N";
                        }
                        break;
                    case $vouchdt >= $fdate4 && $vouchdt <= $tdate4:
                        if ($day > 25) {
                            $folblk = "Y";
                        } else {
                            $folblk = "N";
                        }
                        break;
                }
                break;
            case "M":
                if ($day > 7) {
                    $folblk = "Y";
                } else {
                    $folblk = "N";
                }
                break;

            case "H":
                switch ($vouchdt) {
                    case $vouchdt >= $f15date1 && $vouchdt <= $t15date1:
                        if ($day > 7) {
                            $folblk = "Y";
                        } else {
                            $folblk = "N";
                        }
                        break;

                    case $vouchdt >= $f15date2 && $vouchdt <= $t15date2:
                        if ($day > 22) {
                            $folblk = "Y";
                        } else {
                            $folblk = "N";
                        }
                        break;

                    default:
                        break;
                }

                break;

            case "T":
                switch ($vouchdt) {
                    case $vouchdt >= $f10date1 && $vouchdt <= $t10date1:
                        if ($day > 3) {
                            $folblk = "Y";
                        } else {
                            $folblk = "N";
                        }
                        break;

                    case $vouchdt >= $f10date2 && $vouchdt <= $t10date2:
                        if ($day > 13) {
                            $folblk = "Y";
                        } else {
                            $folblk = "N";
                        }
                        break;

                    case $vouchdt >= $f10date3 && $vouchdt <= $t10date3:
                        if ($day > 23) {
                            $folblk = "Y";
                        } else {
                            $folblk = "N";
                        }
                        break;


                    default:
                        break;
                }

                break;
            default:
                $folblk = "Y";
                break;
        }
                $balance = $prelast - $recd;
        mysqli_query($conn, "update folwblk set prelast = '$prelast', recd='$recd', balance='$balance',folblk='$folblk' where accode = '$maccode'") or die(mysqli_error($conn));
        
    }
    
    $finquery = mysqli_query($conn, "Select * from folwblk where balance >= $elimit and balance > 0 and folblk = 'Y' order by custcode") or die(mysqli_error($conn));
    $ctr = 0;
    while ($row = mysqli_fetch_array($finquery)) {
        $ctr++;
        echo "<tr style='color:green'>";
        echo "<td>$ctr</td>";
        echo "<td>$row[custcode]</td>";
        echo "<td>$row[name]</td>";
        echo "<td class='text-right'>$row[balance]</td>";
        echo "</tr>";
    }




//SELECT &mdata
//REPLACE ALL balance WITH prelast - recd
//
//SELECT * from &mdata WHERE balance >= elimit AND balance > 0 AND folblk = "Y" INTO CURSOR folwblk
}
        