<?php
ob_start();
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');
$maindb = "invoice";
$file = "genbill.php";
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn,$zdb);

$monthno = $_POST["monthno"];
$billperiod = $_POST["billperiod"];
$invdate = $_POST["invdate"];
$party = $_POST["party"];
$gsttype = $_POST["gsttype"];
$cgstperc = $gsttype / 2;
$purcode = '30';
$source = 'SL';
$mfrmdate = trim($_POST["fdate"]);
$mtodate = trim($_POST["tdate"]);
$mfdt = date("d-m-Y", strtotime($mfrmdate));
$mtdt = date("d-m-Y", strtotime($mtodate));

$descrip = 'Full Month ( ' . $mfdt . '   to    ' . $mtdt . ' )';
$bperiod = '1';
$mbillcycle = "M";

$partycond = "";
if (!empty($party)) {
    $partycond = " and `accode`='$party'";
}

if (empty($_POST["fdate"])) {
    browse("From Date Cannot Be Empty");
    header("refresh:1; genbill.php");
}


if (empty($_POST["tdate"])) {
    browse("To Date Cannot Be Empty");
    header("refresh:1; genbill.php");
}


//browse($mfdt);
//browse($mtdt);
$cgstamt = 0;
$tnetamt = 0;
$ttaxable = 0;
if (isset($_POST["submit"])) {

    $chkquery = mysqli_query($conn,"Select vouchno,party from `invoice` where `monthno` = '$monthno' $partycond") or die(mysqli_error($conn));
//    print table($chkquery);
    if (!empty($chkquery)) {
        $noquery = mysqli_num_rows($chkquery);
    } else {
        $noquery = 0;
    }
//    browse($noquery);
    if ($noquery > 0) {
        mysqli_query($conn,"DELETE FROM `actran` where  accode = '$purcode' and `monthno` = '$monthno' and source = '$source'") or die(mysqli_error($conn));

        while ($fetquery = mysqli_fetch_array($chkquery)) {
            $mvouch = $fetquery['vouchno'];
            $maccode = $fetquery['party'];
            $mainqry = mysqli_query($conn,"SELECT party, sum(amount) as taxable,
                                   sum(totgst1) as totgst1, sum(totgst5) as totgst5, sum(totgst12) as totgst12, sum(totgst18) as totgst18,
                                   sum(totgst28) as totgst28, sum(cgst1) as cgst1,sum(sgst1) as sgst1,sum(cgst5) as cgst5,sum(sgst5) as sgst5,
                                   sum(cgst12) as cgst12, sum(sgst12) as sgst12, 
                                   sum(cgst18) as cgst18, sum(sgst18) as sgst18, sum(cgst28) as cgst28, sum(sgst28) as sgst28,
                                   sum(totcgst) as totcgst, sum(totsgst) as totsgst, sum(billamt) as billamt 
                                   FROM `challan` WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' and party = '$maccode' 
                                   and delv = 'D' and cashcred != 'C'")or die(mysqli_error($conn));
            $fmain = mysqli_fetch_array($mainqry);
            $tnetamt = $fmain['billamt'];

            mysqli_query($conn,"DELETE FROM `actran` where accode = '$maccode' and `monthno` = '$monthno' and source = '$source'") or die(mysqli_error($conn));

            if ($tnetamt > 0) {
                $minus_amt = ($tnetamt * -1);

                // sale part
                mysqli_query($conn,"INSERT INTO `actran`(date, vouchno,source, trnno, accode, grpcode, amount, monthno)
                            VALUES ('$invdate','$mvouch','$source', '001', '$purcode','005', '$tnetamt','$monthno')") or die(mysqli_error($conn));

                // party part
                mysqli_query($conn,"INSERT INTO `actran`(date, vouchno, source, trnno, accode, grpcode, amount, monthno)
                                VALUES ('$invdate','$mvouch','$source', '002', '$maccode','010', '$minus_amt','$monthno')") or die(mysqli_error($conn));
            }

            mysqli_query($conn,"DELETE FROM `invoice` where vouchno = '$mvouch'") or die(mysqli_error($conn));


            mysqli_query($conn,"INSERT INTO `invoice` (`vouchno`, `vouchdt`, `monthno`, `party`, `taxable`, `totgst1`,
                                                        `totgst5`, `totgst12`, `totgst18`, `totgst28`, `cgst1`, `sgst1`,
                                                        `cgst5`, `sgst5`, `cgst12`, `sgst12`, `cgst18`, `sgst18`, `cgst28`,
                                                        `sgst28`, `totcgst`, `totsgst`, `billamt`)
                                                VALUES('$mvouch', '$invdate', '$monthno', '$fmain[party]', '$fmain[taxable]', '$fmain[totgst1]',
                                                        '$fmain[totgst5]', '$fmain[totgst12]', '$fmain[totgst18]', '$fmain[totgst28]', '$fmain[cgst1]', '$fmain[sgst1]',
                                                        '$fmain[cgst5]', '$fmain[sgst5]', '$fmain[cgst12]', '$fmain[sgst12]', '$fmain[cgst18]', '$fmain[sgst18]', '$fmain[cgst28]',
                                                        '$fmain[sgst28]', '$fmain[totcgst]', '$fmain[totsgst]', '$tnetamt')")or die(mysqli_error($conn));
            mysqli_query($conn,"Update `challan` set `gstinvno`='$mvouch' where party = '$maccode' and month(vouchdt) = '$monthno'") or die(mysqli_error($conn));
        }
            browse('Invoice Generated Successfully');
    } else {

        $mainqry = mysqli_query($conn,"SELECT party, sum(amount) as taxable,
                                   sum(totgst1) as totgst1, sum(totgst5) as totgst5, sum(totgst12) as totgst12, sum(totgst18) as totgst18,
                                   sum(totgst28) as totgst28, sum(cgst1) as cgst1,sum(sgst1) as sgst1,sum(cgst5) as cgst5,sum(sgst5) as sgst5,
                                   sum(cgst12) as cgst12, sum(sgst12) as sgst12, 
                                   sum(cgst18) as cgst18, sum(sgst18) as sgst18, sum(cgst28) as cgst28, sum(sgst28) as sgst28,
                                   sum(totcgst) as totcgst, sum(totsgst) as totsgst, sum(billamt) as billamt 
                                   FROM `challan` WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' and gstflag = 'Y' 
                                   and delv = 'D' and cashcred != 'C' $partycond
                                   group by party order by party")or die(mysqli_error($conn));
//    print table($mainqry);
        if (!empty($mainqry)) {
            $qry = mysqli_num_rows($mainqry);
        } else {
            $qry = 0;
        }
        if ($qry <= 0) {
            browse("No. Record Found");
            header("refresh:1; genbill.php");
        } else {

            mysqli_query($conn,"DELETE FROM `actran` where  accode = '$purcode' and `monthno` = '$monthno' and source = '$source'") or die(mysqli_error($conn));

            while ($fmain = mysqli_fetch_array($mainqry)) {
                $maccode = $fmain['party'];
                $tnetamt = $fmain['billamt'];

//            $billno = $maccode . "-" . $myyear . "/" . $monthno . "/" . $billperiod;

                $gen1 = mysqli_query($conn,"SELECT MAX(vouchno) AS maxcode FROM `$maindb` where vouchno != '' LIMIT 0,1 ") or die(mysqli_error($conn));
                if (mysqli_num_rows($gen1) > 0) {
                    $vvouch = mysqli_fetch_assoc($gen1);
                    $qvouch = $vvouch["maxcode"] + 1;
                } else {
                    $qvouch = '1';
                }
                $mvouch = $qvouch;


//            mysqli_query($conn, "DELETE FROM `invoice` where billperiod = '$bperiod' and accode = '$maccode' and `monno` = '$monthno' ");
                mysqli_query($conn,"DELETE FROM `actran` where accode = '$maccode' and `monthno` = '$monthno' and source = '$source'") or die(mysqli_error($conn));

                if ($tnetamt > 0) {
                    $minus_amt = ($tnetamt * -1);

                    // sale part
                    mysqli_query($conn,"INSERT INTO `actran`(date, vouchno,source, trnno, accode, grpcode, amount, monthno)
                            VALUES ('$invdate','$mvouch','$source', '001', '$purcode','005', '$tnetamt','$monthno')") or die(mysqli_error($conn));

                    // party part
                    mysqli_query($conn,"INSERT INTO `actran`(date, vouchno, source, trnno, accode, grpcode, amount, monthno)
                                VALUES ('$invdate','$mvouch','$source', '002', '$maccode','010', '$minus_amt','$monthno')") or die(mysqli_error($conn));
                }

                mysqli_query($conn,"INSERT INTO `invoice` (`vouchno`, `vouchdt`, `monthno`, `party`, `taxable`, `totgst1`,
                                                        `totgst5`, `totgst12`, `totgst18`, `totgst28`, `cgst1`, `sgst1`,
                                                        `cgst5`, `sgst5`, `cgst12`, `sgst12`, `cgst18`, `sgst18`, `cgst28`,
                                                        `sgst28`, `totcgst`, `totsgst`, `billamt`)
                                                VALUES('$mvouch', '$invdate', '$monthno', '$fmain[party]', '$fmain[taxable]', '$fmain[totgst1]',
                                                        '$fmain[totgst5]', '$fmain[totgst12]', '$fmain[totgst18]', '$fmain[totgst28]', '$fmain[cgst1]', '$fmain[sgst1]',
                                                        '$fmain[cgst5]', '$fmain[sgst5]', '$fmain[cgst12]', '$fmain[sgst12]', '$fmain[cgst18]', '$fmain[sgst18]', '$fmain[cgst28]',
                                                        '$fmain[sgst28]', '$fmain[totcgst]', '$fmain[totsgst]', '$tnetamt')")or die(mysqli_error($conn));
                mysqli_query($conn,"Update `challan` set `gstinvno`='$mvouch' where party = '$maccode' and month(vouchdt) = '$monthno'") or die(mysqli_error($conn));
            }
            browse('Invoice Generated Successfully');
        }
    }
    header("refresh:1; $file");
//}
    ?>
    </body>   
    </html>  
    <?php
}

            