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

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];

$myyear = $_SESSION[$myyear];
$zyear = $myyear;
$zsdate = getzdates($zyear, 'S');
$zedate = getzdates($zyear, 'E');

mysqli_select_db($conn, $zdb);

$zcomp = $_SESSION["comp_" . $dbname];

$maindb = 'salemain';
$trandb = 'saletran';

$zroot = $_SESSION["zdbroot"];

$para = $_GET['para'];
if ($para == "M") {
    $formname = "Invoice - Multiple";
} else {
    $formname = "Invoice - Single";
}

$zroot = $_SESSION["zdir"];
$file = 'invoice.php';

if (isset($_POST["view"])) {

    $dbillno = "";
    $imon = "";
    $iyear = "";

    $stdate = '2012-07-01';      // date - service tax applicable
    $rtit1 = '';
    $rtit2 = '';

    $mcono = $zcomp;

    $qrycomp = mysqli_query($conn, "SELECT * from $zroot.company WHERE code = '$mcono' ") or die(mysqli_error($conn));
    $fetcomp = mysqli_fetch_array($qrycomp);

    $mgstno = $fetcomp['gstno'];
    $tdsperc = $fetcomp['tdsperc'];

    $zadd1 = trim($fetcomp['hoadd1']) . ' ' . trim($fetcomp['hoadd2']);
    $zadd2 = trim($fetcomp['hoadd3']) . ' ' . trim($fetcomp['email']);

    $mrepprin = trim($fetcomp['invprin']);
    $mbankdtl = trim($fetcomp['bankdtl']);
    $mpaymode = trim($fetcomp['paymode']);
    $mlatepay = trim($fetcomp['latepay']);

    $zbankname = trim($fetcomp['bankname']);
    $zbranch = trim($fetcomp['branchname']);
    $zbankcode = trim($fetcomp['ifccode']);
    $zacno = trim($fetcomp['acno']);

    $qryyear = mysqli_query($conn, "SELECT * from $zroot.compyear WHERE cono = '$mcono' and finyear = '$zyear' ") or die(mysqli_error($conn));
    $fetyear = mysqli_fetch_array($qryyear);
    $stopperc = $fetcomp['stonop'];

    $vmon = $_POST["month"];
    IF (EMPTY($vmon)) {
        browse('Month Cannot Be Empty');
        echo "<script>
          window.close();
          </script>";
    } else {
        $rtit1 = 'Specific Month    ' . vmon;
    }

    $period = $_POST["period"];

    IF (EMPTY($period)) {
        browse('Period Cannot Be Empty');
        echo "<script>
          window.close();
          </script>";
    } ELSE {
        $rtit1 = 'Specific Period   ' . $period;
    }

    if (empty($_POST["todate"])) {
        browse("Invoice Date Cannot Be Empty");
        echo "<script>
          window.close();
          </script>";
    }

    $mtodate = $_POST["todate"];
    $mbilldate = $_POST["todate"];
    $mtdt = date("d-m-Y", strtotime($mtodate));

    $ddate = $mtodate;

    $mmonth = $vmon;
    $mperiod = $period;

    $rtit1 = '';
    $cucond = "";

    $iname = trim($_POST["account"]);

    if (!empty($iname)) {
        $rtit1 = 'Specific Party     ' . $iname;


        $qryacc = mysqli_query($conn, "select * from accmast where accode = '$iname' ") or die(mysqli_error($conn));
        $noacc = mysqli_num_rows($qryacc);
        // $accname = mysqli_fetch_array($qryacc);

        if ($noacc == 0) {
            browse('Party Does Not Exist');
            echo "<script>
          window.close();
          </script>";
        } else {
            $fetacc = mysqli_fetch_array('$qryacc');
            $cucode = $fetacc['custcode'];
        }

        $frmaccode = $cucode;
        $toaccode = $cucode;
    }

    $frmcode = $_POST["frmcode"];

    IF (!EMPTY($frmcode)) {
        $qryacc = mysqli_query($conn, "select * from accmast where custcode = '$frmcode' ") or die(mysqli_error($conn));
        $noacc = mysqli_num_rows($qryacc);

        if ($noacc == 0) {
            browse('Invalid From Customer Code');
            echo "<script>
          window.close();
          </script>";
        } else {
            $fetacc = mysqli_fetch_array('$qryacc');
            $frmaccode = $fetacc['accode'];
        }
    }

    $tocode = $_POST["tocode"];

    IF (!EMPTY($tocode)) {
        $qryacc = mysqli_query($conn, "select * from accmast where custcode = '$tocode' ") or die(mysqli_error($conn));
        $noacc = mysqli_num_rows($qryacc);

        if ($noacc == 0) {
            browse('Invalid From Customer Code');
            echo "<script>
          window.close();
          </script>";
        } else {
            $fetacc = mysqli_fetch_array('$qryacc');
            $toaccode = $fetacc['accode'];
        }
    }

    $frmcust = $frmcode;
    $tocust = $tocode;

    IF (!EMPTY($frmcust) && !EMPTY($tocust)) {
        $cucond = " and custcode >= '$frmcust' and custcode <= '$tocust' ";
    }

    $vcond = '';
    $mvehno = $_POST["vehno"];
    if (!empty($mvehno)) {
        $rtit1 = 'Specific Vehicle     ' . $mvehno;
        $vcond = " and vehicleno = '$mvehno' ";
    }

    $mdaydate = $_POST["daydate"];
    IF (!EMPTY($mdaydate)) {
        if ($mdaydate >= $zsdate && $mdaydate <= $zedate) {
            
        } else {
            browse('Date Does Not Belong To Financial Year');
            echo "<script>
          window.close();
          </script>";
        }
    } ELSE {
        IF ($mperiod == 8) {
            browse('Date Cannot Be Empty');
            echo "<script>
          window.close();
          </script>";
        }
    }

//  build date condition string and period string

    $datecond = '';
    $puccond = '';
    $mfromdate = '0000-00-00';
    $mtodate = '0000-00-00';


    if ($mmonth > 3) {
        $myear = left($zsdate, 4);
    } else {
        $myear = left($zedate, 4);
    }
    $arrdate = $myear . '-' . $mmonth . '-01';

//    browse($myear);

    $qrymon = mysqli_query($conn, "SELECT * from $zroot.month WHERE moncode = '$mmonth' ") or die(mysqli_error($conn));
    $fetmon = mysqli_fetch_array($qrymon);
    $mper = $fetmon['monname'];

    $mdate = $arrdate;
    $mondate1 = date('Y-m-d', strtotime($mdate . ' . 1 months'));
    $mondate = date('Y-m-d', strtotime($mondate1 . ' - 1 days'));

    $chkdate = $mondate;

    $dmonth = date('F', strtotime($mdate));

    $mhf = $myear . '-' . $mmonth . '-01';

    $ehf1 = date('Y-m-d', strtotime($mhf . ' . 1 months'));
    $ehf = date('Y-m-d', strtotime($ehf1 . ' - 1 days'));

    $mmd = date("d", $ehf);

    $stdate1 = left($mondate, 8) . '01';
    $stdate2 = left($mondate, 8) . '08';
    $stdate3 = left($mondate, 8) . '16';
    $stdate4 = left($mondate, 8) . '23';

    $onedate = left($mondate, 8) . '07';
    $twodate = left($mondate, 8) . '15';
    $thredate = left($mondate, 8) . '22';

    $oneten = left($mondate, 8) . '10';
    $twoten = left($mondate, 8) . '20';


    $arrdate = $myear . '-' . $mmonth . '-01';
    $mper = date('F', strtotime($arrdate));

    $half = $mperiod - 1;

    switch ($mperiod) {
        case "1":
            $datecond = " and month(vouchdt)  = '$mmonth' ";
            $puccond = " and month(trandate) = '$mmonth' ";
            $period = $mper;
            $dispmon = $dmonth;
            $bperiod = 1;
            $chkdate = $mondate;
            $mtodate = $todate;
            $mbillcycle = "M";
            break;

        case "2";
            $mfromdate = $myear . '-' . $mmonth . '-01';
            $mtodate = $myear . '-' . $mmonth . '-15';

            $period = '1st Half  ( ' . $mfromdate . ' - ' . $mtodate . ' )';
            $datecond = " and vouchdt >= '$mfromdate'  and vouchdt <= '$mtodate' ";
            $puccond = " and trandate >= '$mfromdate' and trandate <= '$mtodate' ";
            $dispmon = left($dmonth, 3) . ' 01-15';
            $bperiod = 2;
            $chkdate = $twodate;
            $mbillcycle = "H";
            break;

        case "3";

            $mfromdate = $myear . '-' . $mmonth . '-16';
            $to1 = date('Y-m-d', strtotime($mmonth . ' . 1 months'));
            $mtodate = date('Y-m-d', strtotime($to1 . ' - 1 days'));

            $period = '2nd Half  ( ' . $mfromdate . ' - ' . $mtodate . ' )';
            $datecond = " and vouchdt >= '$mfromdate'  and vouchdt <= '$mtodate' ";
            $puccond = " and trandate >= '$mfromdate' and trandate <= '$mtodate' ";
            $dispmon = left($dmonth, 3) . ' 16-' . $mmd;
            $bperiod = 2;
            $chkdate = $mondate;
            $mbillcycle = "H";
            break;

        case "4";
            $mfromdate = $myear . '-' . $mmonth . '-01';
            $mtodate = $myear . '-' . $mmonth . '-07';
            $period = '1st Wk  ( ' . $mfromdate . ' - ' . $mtodate . ' )';
            $datecond = " and vouchdt >= '$mfromdate'  and vouchdt <= '$mtodate' ";
            $puccond = " and trandate >= '$mfromdate' and trandate <= '$mtodate' ";
            $dispmon = left($dmonth, 3) . ' 01-07';
            $bperiod = 3;
            $chkdate = $onedate;
            $mbillcycle = "W";
            break;

        case "5";
            $mfromdate = $myear . '-' . $mmonth . '-08';
            $mtodate = $myear . '-' . $mmonth . '-15';
            $period = '2nd Wk  ( ' . $mfromdate . ' - ' . $mtodate . ' )';
            $datecond = " and vouchdt >= '$mfromdate'  and vouchdt <= '$mtodate' ";
            $puccond = "";
            $dispmon = left($dmonth, 3) . ' 08-15';
            $bperiod = 3;
            $chkdate = $twodate;
            $mbillcycle = "W";
            break;

        case "6";
            $mfromdate = $myear . '-' . $mmonth . '-16';
            $mtodate = $myear . '-' . $mmonth . '-22';
            $period = '3rd Wk  ( ' . $mfromdate . ' - ' . $mtodate . ' )';
            $datecond = " and vouchdt >= '$mfromdate'  and vouchdt <= '$mtodate' ";
            $puccond = "";
            $dispmon = left($dmonth, 3) . ' 16-22';
            $bperiod = 3;
            $chkdate = $thredate;
            $mbillcycle = "W";
            break;

        case "7";

            $mfromdate = $myear . '-' . $mmonth . '-23';
            $mhalf = $myear . '-' . $mmonth . '-01';

            $ehf1 = date('Y-m-d', strtotime($mhalf . ' . 1 months'));
            $ehalf = date('Y-m-d', strtotime($ehf1 . ' - 1 days'));
            $mtodate = $ehalf;
            $period = '4th Wk  ( ' . $mfromdate . ' - ' . $mtodate . ' )';
            $datecond = " and vouchdt >= '$mfromdate'  and vouchdt <= '$mtodate' ";
            $puccond = "";
            $dispmon = left($dmonth, 3) . ' 23-' . $mmd;
            $bperiod = 3;
            $chkdate = $mondate;
            $mbillcycle = "W";
            break;

        case "8";

            $cdatef = date('Y-m-d', strtotime($mdate));
            $cdatet = date('Y-m-d', strtotime($chkdate));

            if (($mdaydate >= $cdatef) && ($mdaydate <= $cdatet)) {
                
            } else {
                browse('Date Does Not Belong To The Month Selected');
                echo "<script>
          window.close();
          </script>";
            }

            $mfromdate = $mdaydate;
            $mtodate = $mdaydate;
            $period = '( Daily ) - ' . $mtodate;

            $datecond = " and vouchdt >= '$mfromdate'  and vouchdt <= '$mtodate' ";
            $puccond = "";
            $dispmon = $mdaydate;
            $bperiod = 4;
            $chkdate = $mdaydate;
            $mbillcycle = "D";
            break;

        case "9";
            $mfromdate = $myear . '-' . $mmonth . '-01';
            $mtodate = $myear . '-' . $mmonth . '-10';

            $period = 'First Ten Days ( ' . $mfromdate . ' - ' . $mtodate . ' )';
            $datecond = " and vouchdt >= '$mfromdate'  and vouchdt <= '$mtodate' ";
            $puccond = " and trandate >= '$mfromdate' and trandate <= '$mtodate' ";

            $dispmon = left($dmonth, 3) . ' 01-10';
            $bperiod = 5;
            $chkdate = $mondate;
            $mbillcycle = "T";
            break;

        case "10";
            $mfromdate = $myear . '-' . $mmonth . '-11';
            $mtodate = $myear . '-' . $mmonth . '-20';

            $period = 'Mid Ten Days ( ' . $mfromdate . ' - ' . $mtodate . ' )';
            $datecond = " and vouchdt >= '$mfromdate'  and vouchdt <= '$mtodate' ";
            $puccond = " and trandate >= '$mfromdate' and trandate <= '$mtodate' ";

            $dispmon = left($dmonth, 3) . ' 11-20';
            $bperiod = 5;
            $chkdate = $oneten;
            $mbillcycle = "T";
            break;

        case "11";
            $mfromdate = $myear . '-' . $mmonth . '-21';
            $mhalf = $myear . '-' . $mmonth . '-01';

            $ehf1 = date('Y-m-d', strtotime($mhalf . ' . 1 months'));
            $ehalf = date('Y-m-d', strtotime($ehf1 . ' - 1 days'));
            $mtodate = $ehalf;

            $period = 'Last Ten Days ( ' . dtoc(mfromdate) . ' - ' . dtoc(mtodate) . ' )';
            $datecond = " and vouchdt >= '$mfromdate'  and vouchdt <= '$mtodate' ";
            $puccond = " and trandate >= '$mfromdate' and trandate <= '$mtodate' ";

            $dispmon = left($dmonth, 3) . ' 16-' . $mmd;
            $bperiod = 5;
            $chkdate = $towten;
            $mbillcycle = "T";
            break;
    }


    IF ($mtodate > date("Y-m-d")) {
        browse('Last Date Of Invoice Period Is Greater Than Current Date');
        echo "<script>
          window.close();
          </script>";
    }

    $minvdt = $myear . '-' . $mmonth . '-01';

    switch ($mperiod) {
        case "3":
            $minvdt = $myear . '-' . $mmonth . '-16';
        case "5":
            $minvdt = $myear . '-' . $mmonth . '-08';
        case "6":
            $minvdt = $myear . '-' . $mmonth . '-16';
        case "7":
            $minvdt = $myear . '-' . $mmonth . '-23';
        case "8":
            $minvdt = $mdaydate;
        case "9":
            $minvdt = $myear . '-' . $mmonth . '-01';
        case "10":
            $minvdt = $myear . '-' . $mmonth . '-11';
        case "11":
            $minvdt = $myear . '-' . $mmonth . '-21';
    }

    $mcond = "";

    $half = $mperiod - 1;


// ********* end of date condition string


    $cctr = 0;

    $qrymon = mysqli_query($conn, "SELECT * from accmast WHERE custcode >= '$frmcust' AND custcode <= '$tocust' AND billcycle != '$mbillcycle' ") or die(mysqli_error($conn));
    IF (mysqli_num_rows($qrymon) == 0) {
        $fetmon = mysqli_fetch_array($qrymon);
        $mcust = $fetmon['custcode'];

        browse('Customer Code : ' . $mcust . '  Mismatch Billing Cycle Selected');
        echo "<script>
          window.close();
          </script>";
    }

    $ccyc = mysqli_query($conn, "SELECT * from accmast WHERE billcycle = '$mbillcycle' AND dailyprint != 1 ") or die(mysqli_error($conn));

    mysqli_query($conn, "drop temporary table if exists cmain ") or die(mysqli_error($conn));
    mysqli_query($conn, "create temporary table cmain 
                                SELECT *, party as accode, vehicleno as vehno from salemain WHERE party != ''  $datecond $cucond $vcond and party in 
                                       (select accode from accmast WHERE billcycle = '$mbillcycle' AND dailyprint != 1 ) ") or die(mysqli_error($conn));

    $cmaintmp = mysqli_query($conn, "SELECT * from cmain group by accode, vehno order by accode, vehno ") or die(mysqli_error($conn));

    IF (mysqli_num_rows($cmaintmp) == 0) {
        browse('Record Not Found');

        while ($rw = mysqli_fetch_array($ccyc)) {
            $maccode = $rw['accode'];
            $acode = $maccode;

            // ********* invoice part
            include ('delinvoice.php');

            switch ($mperiod) {
                case "1":
                    mysqli_query($conn, "DELETE FROM `invoice` WHERE `month` = '$mmonth' AND `accode` = '$acode' ") or die(mysqli_error($conn));
                case "2":
                    mysqli_query($conn, "DELETE FROM `invoice` WHERE `month` = '$mmonth' AND `accode` = '$acode' 
                                                and half != 2 and half != 5 and half != 6 and half != 10 ") or die(mysqli_error($conn));

                    delinv($mmonth, $acode, 7, " and day(invdate) <= 15 ");

                case "3":
                    mysqli_query($conn, "DELETE FROM `invoice` WHERE `month` = '$mmonth' AND `accode` = '$acode' and 
                                         half != 1 and half != 3 and half != 4  and half != 8 ") or die(mysqli_error($conn));

                    delinv($mmonth, $acode, 7, " and day(invdate) > 15 ");

                case "4":
                    mysqli_query($conn, "DELETE FROM `invoice` WHERE `month` = '$mmonth' AND `accode` = '$acode' and 
                                         half != 2 and half != 4 and half != 5  and half != 9 and half != 10 ") or die(mysqli_error($conn));

                    delinv($mmonth, $acode, 7, " and day(invdate) <= 7 ");

                case "5":
                    mysqli_query($conn, "DELETE FROM `invoice` WHERE `month` = '$mmonth' AND `accode` = '$acode' and 
                                         half != 2 and half != 4 and half != 5  and half != 9 and half != 10 ") or die(mysqli_error($conn));

                    delinv($mmonth, $acode, 7, " and day(invdate) > 7 and day(invdate) < 16  ");

                case "6":
                    mysqli_query($conn, "DELETE FROM `invoice` WHERE `month` = '$mmonth' AND `accode` = '$acode' and 
                                         half != 1 and half != 3 and half != 4  and half != 6 and half != 8 ") or die(mysqli_error($conn));

                    delinv($mmonth, $acode, 7, " and day(invdate) > 15 and day(invdate) < 23  ");

                case "7":
                    mysqli_query($conn, "DELETE FROM `invoice` WHERE `month` = '$mmonth' AND `accode` = '$acode' and 
                                         half != 1 and half != 3 and half != 4  and half != 5 and half != 8 and half != 9 ") or die(mysqli_error($conn));

                    delinv($mmonth, $acode, 7, " and day(invdate) > 22  ");

                case "8":
                    delinv($mmonth, $acode, 0, "");

                    IF ((mdaydate >= stdate1) && ($mdaydate <= onedate)) {
                        delinv($mmonth, $acode, 1, "");
                        delinv($mmonth, $acode, 3, "");
                    } elseif (($mdaydate >= $stdate3) && ($mdaydate <= $twodate)) {
                        delinv($mmonth, $acode, 1, "");
                        delinv($mmonth, $acode, 4, "");
                    } elseif (($mdaydate >= $stdate3) && ($mdaydate <= $thredate)) {
                        delinv($mmonth, $acode, 2, "");
                        delinv($mmonth, $acode, 5, "");
                    } else {
                        delinv($mmonth, $acode, 2, "");
                        delinv($mmonth, $acode, 6, "");
                    }

                    delinv($mmonth, $acode, 7, " and invdate = '$mdaydate' ");

                case "9":
                    mysqli_query($conn, "DELETE FROM `invoice` WHERE `month` = '$mmonth' AND `accode` = '$acode' and 
                                         half != 2 and half != 5 and half != 6  and half != 9 and half != 10 ") or die(mysqli_error($conn));

                    delinv($mmonth, $acode, 7, " and day(invdate) <= 11  ");

                case "10":
                    mysqli_query($conn, "DELETE FROM `invoice` WHERE `month` = '$mmonth' AND `accode` = '$acode' and 
                                         half != 3 and half != 6 and half != 8  and half != 10 ") or die(mysqli_error($conn));

                    delinv($mmonth, $acode, 7, " and day(invdate) > 11 and DAY(invdate) < 20 ");

                case "11":
                    mysqli_query($conn, "DELETE FROM `invoice` WHERE `month` = '$mmonth' AND `accode` = '$acode' and 
                                         half != 1 and half != 3 and half != 4 and half != 8  and half != 9 ") or die(mysqli_error($conn));

                    delinv($mmonth, $acode, 7, " and day(invdate) < 21 ");
            }

            // ********* actran part

            switch ($mperiod) {
                case "1":
                    delro(13, 20, $acode, " month(date) = '$mmonth' ");
                case "2":
                    delro(13, 14, $acode, " month(date) = '$mmonth' ");
                    delro(16, 17, $acode, " month(date) = '$mmonth' ");
                    delro(20, 20, $acode, "  day(date) <= 15 ");
                case "3":
                    delro(13, 14, $acode, " month(date) = '$mmonth' ");
                    delro(15, 15, $acode, " month(date) = '$mmonth' ");
                    delro(18, 19, $acode, " month(date) = '$mmonth' ");
                    delro(20, 20, $acode, "  day(date) >= 16 ");
                case "4":
                    delro(13, 14, $acode, " month(date) = '$mmonth' ");
                    delro(16, 16, $acode, " month(date) = '$mmonth' ");
                    delro(20, 20, $acode, "  day(date) <= 7 ");
                case "5":
                    delro(13, 14, $acode, " month(date) = '$mmonth' ");
                    delro(17, 17, $acode, " month(date) = '$mmonth' ");
                    delro(20, 20, $acode, " day(date) >= 8 and day(date) <= 15  ");

                case "6":
                    delro(13, 13, $acode, " month(date) = '$mmonth' ");
                    delro(15, 15, $acode, " month(date) = '$mmonth' ");
                    delro(18, 18, $acode, " month(date) = '$mmonth' ");
                    delro(20, 20, $acode, " day(date) >= 16 and day(date) <= 22 ");

                case "7":
                    delro(13, 13, $acode, " month(date) = '$mmonth' ");
                    delro(15, 15, $acode, " month(date) = '$mmonth' ");
                    delro(19, 19, $acode, " month(date) = '$mmonth' ");
                    delro(20, 20, $acode, " day(date) >= 23 ");

                case "8":
                    delro(13, 13, $acode, " month(date) = '$mmonth' ");
                    IF ((mdaydate >= stdate1) && ($mdaydate <= onedate)) {
                        delro(14, 14, $acode, " month(date) = '$mmonth' ");
                        delro(16, 16, $acode, " month(date) = '$mmonth' ");
                    } elseif (($mdaydate >= $stdate3) && ($mdaydate <= $twodate)) {
                        delro(14, 14, $acode, " month(date) = '$mmonth' ");
                        delro(17, 17, $acode, " month(date) = '$mmonth' ");
                    } elseif (($mdaydate >= $stdate3) && ($mdaydate <= $thredate)) {
                        delro(15, 15, $acode, " month(date) = '$mmonth' ");
                        delro(18, 18, $acode, " month(date) = '$mmonth' ");
                    } else {
                        delro(15, 15, $acode, " month(date) = '$mmonth' ");
                        delro(19, 19, $acode, " month(date) = '$mmonth' ");
                    }

                    delro(20, 20, $acode, " date = '$mdaydate' ");
                case "9":
                    delro(13, 14, acode, " mont(date) = mmonth ");
                    delro(16, 17, acode, " mont(date) = mmonth ");
                    delro(21, 21, acode, " mont(date) = mmonth ");
                    delro(20, 20, acode, " day(date) <= 10 ");
                case "10":
                    delro(13, 15, acode, " mont(date) = mmonth ");
                    delro(17, 18, acode, " mont(date) = mmonth ");
                    delro(21, 21, acode, " mont(date) = mmonth ");
                    delro(20, 20, acode, " day(date) >= 11 and day(date) <= 20 ");
                case "11":
                    delro(13, 13, acode, " mont(date) = mmonth ");
                    delro(15, 15, acode, " mont(date) = mmonth ");
                    delro(18, 19, acode, " mont(date) = mmonth ");
                    delro(23, 23, acode, " mont(date) = mmonth ");
                    delro(20, 20, acode, " day(date) >= 21 ");
            }
        }
        echo "<script>
          window.close();
          </script>";
    }

    mysqli_query($conn, "drop temporary table if exists ctran ") or die(mysqli_error($conn));
    mysqli_query($conn, "create temporary table ctran 
                                SELECT a.*, ' ' as actype, b.srno, b.prodcode, b.qty, b.unit, b.rate, b.amount as amt,
                                      b.diesdisc as tdiscdies from cmain a, saletran b     
                                      where a.vouchno = b.vouchno order by a.vouchno ") or die(mysqli_error($conn));


    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
        th{
            background: #6082BF;
        }
    </style>

    <?php

    $ctr = 0;
    $totqty = 0;
    $postinvno = $_POST['invno'];

    IF ($postinvno != 0) {
        $minvno = $postinvno - 1 ;
    }

    include ('delinvoice.php');

    while ($pmain = mysqli_fetch_array($cmaintmp)) {

        $accode = $pmain['accode'];
        $acode = $accode;
        $custcode = $pmain['custcode'];
        $vehno = $pmain['vehno'];

        switch ($mperiod) {
            case "1":

                mysqli_query($conn, "DELETE FROM `invoice` WHERE `month` = '$mmonth' AND `accode` = '$acode' ") or die(mysqli_error($conn));
                break;
            case "2":
                mysqli_query($conn, "DELETE FROM `invoice` WHERE `month` = '$mmonth' AND `accode` = '$acode' and
                                                half != 2 and half != 5 and half != 6 ") or die(mysqli_error($conn));

                delinv($mmonth, $acode, 7, " and day(invdate) <= 15 ");
                break;

            case "3":
                mysqli_query($conn, "DELETE FROM `invoice` WHERE `month` = '$mmonth' AND `accode` = '$acode' and 
                                         half != 1 and half != 3 and half != 4  and half != 8 ") or die(mysqli_error($conn));

                delinv($mmonth, $acode, 7, " and day(invdate) > 15 ");
                break;

            case "4":
                mysqli_query($conn, "DELETE FROM `invoice` WHERE `month` = '$mmonth' AND `accode` = '$acode' and 
                                         half != 2 and half != 4 and half != 5  and half != 9 and half != 10 ") or die(mysqli_error($conn));

                delinv($mmonth, $acode, 7, " and day(invdate) <= 7 ");
                break;

            case "5":
                mysqli_query($conn, "DELETE FROM `invoice` WHERE `month` = '$mmonth' AND `accode` = '$acode' and 
                                         half != 2 and half != 4 and half != 5  and half != 9 and half != 10 ") or die(mysqli_error($conn));

                delinv($mmonth, $acode, 7, " and day(invdate) > 7 and day(invdate) < 16  ");
                break;

            case "6":
                mysqli_query($conn, "DELETE FROM `invoice` WHERE `month` = '$mmonth' AND `accode` = '$acode' and 
                                         half != 1 and half != 3 and half != 4  and half != 6 and half != 8 ") or die(mysqli_error($conn));

                delinv($mmonth, $acode, 7, " and day(invdate) > 15 and day(invdate) < 23  ");
                break;

            case "7":
                mysqli_query($conn, "DELETE FROM `invoice` WHERE `month` = '$mmonth' AND `accode` = '$acode' and 
                                         half != 1 and half != 3 and half != 4  and half != 5 and half != 8 and half != 9 ") or die(mysqli_error($conn));

                delinv($mmonth, $acode, 7, " and day(invdate) > 22  ");
                break;

            case "8":
                delinv($mmonth, $acode, 0, "");

                IF (($mdaydate >= $stdate1) && ($mdaydate <= $onedate)) {
                    delinv($mmonth, $acode, 1, "");
                    delinv($mmonth, $acode, 3, "");
                } elseif (($mdaydate >= $stdate3) && ($mdaydate <= $twodate)) {
                    delinv($mmonth, $acode, 1, "");
                    delinv($mmonth, $acode, 4, "");
                } elseif (($mdaydate >= $stdate3) && ($mdaydate <= $thredate)) {
                    delinv($mmonth, $acode, 2, "");
                    delinv($mmonth, $acode, 5, "");
                } else {
                    delinv($mmonth, $acode, 2, "");
                    delinv($mmonth, $acode, 6, "");
                }

                delinv($mmonth, $acode, 7, " and invdate = '$mdaydate' ");
                break;

            case "9":
                mysqli_query($conn, "DELETE FROM `invoice` WHERE `month` = '$mmonth' AND `accode` = '$acode' and 
                                         half != 2 and half != 5 and half != 6  and half != 9 and half != 10 ") or die(mysqli_error($conn));

                delinv($mmonth, $acode, 7, " and day(invdate) <= 11  ");
                break;

            case "10":
                mysqli_query($conn, "DELETE FROM `invoice` WHERE `month` = '$mmonth' AND `accode` = '$acode' and 
                                         half != 3 and half != 6 and half != 8  and half != 10 ") or die(mysqli_error($conn));

                delinv($mmonth, $acode, 7, " and day(invdate) > 11 and DAY(invdate) < 20 ");
                break;

            case "11":
                mysqli_query($conn, "DELETE FROM `invoice` WHERE `month` = '$mmonth' AND `accode` = '$acode' and 
                                         half != 1 and half != 3 and half != 4 and half != 8  and half != 9 ") or die(mysqli_error($conn));

                delinv($mmonth, $acode, 7, " and day(invdate) < 21 ");
                break;
        }

        // ********* actran part
        switch ($mperiod) {
            case "1":
                delro(13, 20, $acode, " month(date) = '$mmonth' ");
                break;
            case "2":
                delro(13, 14, $acode, " month(date) = '$mmonth' ");
                delro(16, 17, $acode, " month(date) = '$mmonth' ");
                delro(20, 20, $acode, "  day(date) <= 15 ");
                break;
            case "3":
                delro(13, 14, $acode, " month(date) = '$mmonth' ");
                delro(15, 15, $acode, " month(date) = '$mmonth' ");
                delro(18, 19, $acode, " month(date) = '$mmonth' ");
                delro(20, 20, $acode, "  day(date) >= 16 ");
                break;
            case "4":
                delro(13, 14, $acode, " month(date) = '$mmonth' ");
                delro(16, 16, $acode, " month(date) = '$mmonth' ");
                delro(20, 20, $acode, "  day(date) <= 7 ");
                break;
            case "5":
                delro(13, 14, $acode, " month(date) = '$mmonth' ");
                delro(17, 17, $acode, " month(date) = '$mmonth' ");
                delro(20, 20, $acode, " day(date) >= 8 and day(date) <= 15  ");
                break;

            case "6":
                delro(13, 13, $acode, " month(date) = '$mmonth' ");
                delro(15, 15, $acode, " month(date) = '$mmonth' ");
                delro(18, 18, $acode, " month(date) = '$mmonth' ");
                delro(20, 20, $acode, " day(date) >= 16 and day(date) <= 22 ");
                break;

            case "7":
                delro(13, 13, $acode, " month(date) = '$mmonth' ");
                delro(15, 15, $acode, " month(date) = '$mmonth' ");
                delro(19, 19, $acode, " month(date) = '$mmonth' ");
                delro(20, 20, $acode, " day(date) >= 23 ");
                break;

            case "8":
                delro(13, 13, $acode, " month(date) = '$mmonth' ");
                IF (($mdaydate >= $stdate1) && ($mdaydate <= $onedate)) {
                    delro(14, 14, $acode, " month(date) = '$mmonth' ");
                    delro(16, 16, $acode, " month(date) = '$mmonth' ");
                } elseif (($mdaydate >= $stdate3) && ($mdaydate <= $twodate)) {
                    delro(14, 14, $acode, " month(date) = '$mmonth' ");
                    delro(17, 17, $acode, " month(date) = '$mmonth' ");
                } elseif (($mdaydate >= $stdate3) && ($mdaydate <= $thredate)) {
                    delro(15, 15, $acode, " month(date) = '$mmonth' ");
                    delro(18, 18, $acode, " month(date) = '$mmonth' ");
                } else {
                    delro(15, 15, $acode, " month(date) = '$mmonth' ");
                    delro(19, 19, $acode, " month(date) = '$mmonth' ");
                }

                delro(20, 20, $acode, " date = '$mdaydate' ");
                break;
            case "9":
                delro(13, 14, $acode, " mont(date) = $mmonth ");
                delro(16, 17, $acode, " mont(date) = $mmonth ");
                delro(21, 21, $acode, " mont(date) = $mmonth ");
                delro(20, 20, $acode, " day(date) <= 10 ");
                break;
            case "10":
                delro(13, 15, $acode, " mont(date) = $mmonth ");
                delro(17, 18, $acode, " mont(date) = $mmonth ");
                delro(21, 21, $acode, " mont(date) = $mmonth ");
                delro(20, 20, $acode, " day(date) >= 11 and day(date) <= 20 ");
                break;
            case "11":
                delro(13, 13, $acode, " mont(date) = $mmonth ");
                delro(15, 15, $acode, " mont(date) = $mmonth ");
                delro(18, 19, $acode, " mont(date) = $mmonth ");
                delro(23, 23, $acode, " mont(date) = $mmonth ");
                delro(20, 20, $acode, " day(date) >= 21 ");
                break;
        }

        $maccode = $accode;
        $mcustcode = $custcode;
        $mvehno = $vehno;

        $accqry = mysqli_query($conn, "SELECT * from accmast where accode = '$accode' ") or die(mysqli_error($conn));
        $acctmp = mysqli_fetch_array($accqry);

        $opflag = $acctmp['apply'];
        $arrear = $acctmp['arrears'];
        $opchg = $acctmp['charges'];
        $oparr = $acctmp['oparr'];
        $tcsperc = $acctmp['tcsperc'];
        $tdsapply = $acctmp['tdsapply'];
        $diesmast = $acctmp['discdies'];

//        browse($custcode);
//        browse($opchg);
//        browse($opflag);

        $acode = $maccode;
        $paccode = $maccode;

        $subqry = mysqli_query($conn, "SELECT sum(amt) as sub, sum(tdiscdies) as discdies from ctran where accode = '$accode' and vehno = '$vehno' ") or die(mysqli_error($conn));
        $subtmp = mysqli_fetch_array($subqry);
        $sub = $subtmp['sub'];
        $discdies = $subtmp['discdies'];

        $tot = $sub - $discdies;

        IF ($mperiod == 8) {
            $tcsamt = 0;
        } else {
            $tcsamt = $tot * ($tcsperc / 100);
        }

        IF ($opflag == "Y") {
            IF (($sub * ($opchg / 100)) < $zminopchg) {
                $oper = $zminopchg;
            } ELSE {
                $oper = $sub * ($opchg / 100);
            }
        } ELSE {
            $oper = 0;
        }

        IF ($chkdate >= $stdate && $oper > 0) {
            $stopchgs = $oper * ($stopperc / 100);
        } ELSE {
            $stopchgs = 0;
            $stopperc = 0;
        }

        $sot = $sub + $oper + $stopchgs - $discdies + $tcsamt;
        $rsot = ROUND($sot, 0);

        $rof = $sot - $rsot;
        $grd = $rsot;

        IF ($tdsapply == "Y") {
            $tdsamt = $grd * ($tdsperc / 100);
        } ELSE {
            $tdsamt = 0;
        }

        
        IF ($postinvno == 0) {
            $minvno = $custcode . ' - ' . $mmonth . ' / ' . $myear;
            
            IF ($mperiod >= 4 && $mperiod <= 7) {
                $mxtra = $mperiod - 3;
                $minvno = $custcode . ' - ' . $mmonth . ' / ' . $myear . '/' + $mxtra;
            }

            IF ($mperiod == 8) {
                $daysh = date('d', strtotime($mdaydate));
                $minvno = $custcode . ' - ' .$mmonth . ' / ' . $myear . '/' . $daysh;
            }

            IF ($mperiod >= 9 && mperiod <= 11) {
                $mxtra = $mperiod - 8;
                $minvno = $custcode . ' - ' . $mmonth . ' / ' . $myear . '/' . $mxtra;
            }

            $dinvno = $minvno;
        } else {
            $minvno = $minvno + 1;
            $dinvno = $custcode . ' - ' + $mmonth . ' / ' . $myear . '/' . $minvno;

            IF ($mperiod >= 4 && $mperiod <= 7) {
                $mxtra = $mperiod - 3;
                $dinvno = $custcode . ' - ' + $mmonth . ' / ' . $myear . '/' + $mxtra;
            }

            IF ($mperiod == 8) {
                $daysh = date('d', strtotime($mdaydate));
                $dinvno = $custcode . ' - ' + $mmonth . ' / ' . $myear . '/' . $minvno . '/' . $daysh;
            }

            IF ($mperiod >= 9 && $mperiod <= 11) {
                $mxtra = $mperiod - 8;
                $dinvno = $custcode . ' - ' + $mmonth . ' / ' . $myear . '/' . $mxtra;
            }
        }
        $hpara = 'Y';
        include("invoice_print.php");
    }
}
?>
