<?php
ob_start();
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];
mysqli_select_db($conn, $zdb);
$zroot = $_SESSION["zdbroot"];
$zcomp = $_SESSION["comp_" . $dbname];
$zyear = $myyear;

$file = 'gstbnkreg_fr.php';


$formname = 'GST in Bank Register';
$gstcond = '';
$gsttype = $_POST['gsttype'];

if (!empty($dtype)) {
    $dtypecond = " AND `dtype`= '$dtype' ";
}

if (!empty($gsttype)) {
    $gstcond = " AND `gstperc`= '$gsttype' ";
}

if (($_GET["fdt"]) != '') {
    $mfdt = $_GET["fdt"];
    $mtdt = $_GET["tdt"];
    $mfrmdate = date("y-m-d", strtotime($mfdt));
    $mtodate = date("y-m-d", strtotime($mtdt));
    $dtype = $_GET["rpt"];
} else {
    $mfrmdate = trim($_POST["frmdt"]);
    $mtodate = trim($_POST["todt"]);

    if (empty($_POST["frmdt"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; salereg.php");
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; salereg.php");
    }
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
}

if (isset($_POST["view"]) || ($_GET["view"])) {
    include("includes/linkshead.php");
?>
    <style type="text/css">
        #tabital {
            font-style: italic;
        }

        #font1 {
            font-style: italic;
        }

        #boldfont {
            font-weight: bold;
        }
    </style>
    </head>

    <body>


        <div class="container-fluid">
            <?php
            include("includes/head_rep2.php");

            $getpmain2 = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `bankpay` WHERE `vouchdt` >= '$mfrmdate' 
                                        AND `vouchdt` <= '$mtodate' $gstcond order by vouchdt,vouchno") or die(mysqli_error($conn));


            $getno2 = mysqli_num_rows($getpmain2);


            if ($getno2 > 0) {
            ?>
                <div class="row">
                    <div class="col-lg-12 pad">
                        <table class="table table-condesed table-bordered">
                            <tr class="mySearchTr">
                                <!--9221292520-->
                                <th width="100%" class="h4" style="color:white"><b>GST in Bank Register</b></th>
                            </tr>
                            <tr>
                                <td style="font-size: 15px;padding:5px" class="HDFont" colspan="2">
                                    Total No. of Record :&nbsp;&nbsp;<b><?php echo $getno2; ?></b>
                                </td>
                            </tr>
                        </table>
                        <table style="background: white ;width: 100%" class="table table-condensed table-striped table-hover table-bordered">
                            <thead class="mySearchTr">
                                <tr>
                                    <th width="3%;">Sr</th>
                                    <th width="7%;">Date</th>
                                    <th width="3%;">Voucher</th>
                                    <th width="15%;">Party Name</th>
                                    <th width="6%;" class="text-right">Pre GST</th>
                                    <th width="6%;" class="text-right">GST %</th>
                                    <th width="6%;" class="text-right">GST Amount</th>
                                    <th width="6%;" class="text-right">Amount</th>
                                    <th width="6%;">Bank Name</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $pctr = 0;
                                $totamt = $totpre = $totgst =0;

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

                                    $getparty = mysqli_query($conn, "SELECT `accode`,`name` FROM accmast WHERE `accode`='$pmain[party]' ") or die(mysqli_error($conn));
                                    $fetparty = mysqli_fetch_object($getparty);
                                    $partyname = $fetparty->name;

                                    $getbank = mysqli_query($conn, "SELECT `accode`,`name` FROM accmast WHERE `accode`='$pmain[bankname]' ") or die(mysqli_error($conn));
                                    $fetbank = mysqli_fetch_object($getbank);
                                    $bankname = $fetbank->name;

                                    $voucher = $pmain['vouchno'];
                                    $mydt = $pmain['mydt'];
                                    $pregst = $pmain['pregst'];
                                    $gstperc = $pmain['gstperc'];
                                    $gst = $pmain['gst'];
                                    $amount = $pmain['amount'];

                                    $pctr++;
                                    echo "<tr data-href='bankpay.php?mode=edit&vouchno=$voucher' style='background-color: lightyellow' class='clickable-row2' [target]='_blank'>";
                                    echo "<td>$pctr</td>";
                                    echo "<td>$mydt</td>";
                                    echo "<td>$voucher</td>";
                                    echo "<td>$partyname</td>";
                                    echo "<td class='text-right'>$pregst</td>";
                                    echo "<td class='text-right'>$gstperc</td>";
                                    echo "<td class='text-right'>$gst</td>";
                                    echo "<td class='text-right'>$amount</td>";
                                    echo "<td class='text-right'>$bankname</td>";
                                    echo "</tr>";

                                    $totpre += $pregst;
                                    $totgst += $gst;
                                    $totamt += $amount;
                                }

                                echo "<tr style='background-color: #98F9F0'>";
                                echo "<td class='text-right' colspan='4'><b>Total</b></td>";
                                echo "<td class='numeric text-right'><b>$totpre</b></td>";
                                echo "<td class='numeric text-right'><b></b></td>";
                                echo "<td class='numeric text-right'><b>$totgst</b></td>";
                                echo "<td class='numeric text-right'><b>$totamt</b></td>";
                                echo "<td class='numeric text-right'><b></b></td>";
                                echo "</tr>";
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>

            <?php
            }
            ?>
        </div>
        <?php include("includes/linksfoot.php");
        ?>
    </body>

    </html>
<?php
}

if (isset($_POST["excel"]) || ($_GET["excel"])) {
    
            $getpmain2 = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `bankpay` WHERE `vouchdt` >= '$mfrmdate' 
                                        AND `vouchdt` <= '$mtodate' $gstcond order by vouchdt,vouchno") or die(mysqli_error($conn));


            $getno2 = mysqli_num_rows($getpmain2);


            if ($getno2 > 0) {
            
            $contents = "GST in Bank Register \n";
            $contents .= "Total No. of Record : $getno2 \n";
                                
                                    $contents .= "Sr., Date, Voucher, Party Name, Pre GST, GST %, GST Amount, Amount, Bank Name \n";
                                    
                                $pctr = 0;
                                $totamt = $totpre = $totgst =0;

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

                                    $getparty = mysqli_query($conn, "SELECT `accode`,`name` FROM accmast WHERE `accode`='$pmain[party]' ") or die(mysqli_error($conn));
                                    $fetparty = mysqli_fetch_object($getparty);
                                    $partyname = $fetparty->name;

                                    $getbank = mysqli_query($conn, "SELECT `accode`,`name` FROM accmast WHERE `accode`='$pmain[bankname]' ") or die(mysqli_error($conn));
                                    $fetbank = mysqli_fetch_object($getbank);
                                    $bankname = $fetbank->name;

                                    $voucher = $pmain['vouchno'];
                                    $mydt = $pmain['mydt'];
                                    $pregst = $pmain['pregst'];
                                    $gstperc = $pmain['gstperc'];
                                    $gst = $pmain['gst'];
                                    $amount = $pmain['amount'];

                                    $pctr++;

                                    $contents .= "$pctr" . ",";
                                    $contents .= "$mydt" . ",";
                                    $contents .= "$voucher" . ",";
                                    $contents .= "$partyname" . ",";
                                    $contents .= "$pregst" . ",";
                                    $contents .= "$gstperc" . ",";
                                    $contents .= "$gst" . ",";
                                    $contents .= "$amount" . ",";
                                    $contents .= "$bankname" . "\n";
                                    
                                    $totpre += $pregst;
                                    $totgst += $gst;
                                    $totamt += $amount;
                                    }
                                    
                                    $contents .= "" . ",,,";
                                    $contents .= "Total" . ",";
                                    $contents .= "$totpre" . ",";
                                    $contents .= "" . ",";
                                    $contents .= "$totgst" . ",";
                                    $contents .= "$totamt" . ",";
                                    $contents .= "" . "\n";
                                
            }
            
    $contents = strip_tags($contents);

    if ($_POST["dtype"] == 1 || $_POST["dtype"] == 3) {
        header("Content-Disposition: attachment;filename = $formname" . "_Summary" . date('d-m-Y') . ".csv");
        print $contents;
    } else {
        header("Content-Disposition: attachment;filename = $formname" . "_Detail" . date('d-m-Y') . ".csv");
        print $contents;
    }
}
?>