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

$maindb = "contra";
//$trandb = "invtran";
$formname = "Contra Register";
$file = "contreg_fr.php";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);


$bcfrom = $_POST['cmbfrom'];
$bcto = $_POST['cmbto'];
$chqno = $_POST['chqno'];
$chqdate = $_POST['chqdt'];
if (!empty($bcfrom)) {
    $cmbfromcond = " AND `frombc`='$bcfrom' ";
}
if (!empty($bcto)) {
    $cmbtocond = " AND `tobc`='$bcto' ";
}
//browse($cmbtocond);
if (!empty($chqno)) {
    $chqnocond = " AND `chqno`='$chqno' ";
}

if (!empty($chqdate)) {
    $chqdatecond = " AND `chqdate`='$chqdate' ";
}

if (!empty($frmdt)) {
    $frmdtcond = " AND `frmdt`='$frmdt' ";
}

if (!empty($todt)) {
    $todtcond = " AND `todt`='$todt' ";
}

if (empty($_POST["frmdt"])) {
    browse("From Date Cannot Be Empty");
    header("refresh:1; contreg.php");
}
if (empty($_POST["todt"])) {
    browse("To Date Cannot Be Empty");
    header("refresh:1; contreg.php");
}
$mfrmdate = trim($_POST["frmdt"]);
$mtodate = trim($_POST["todt"]);


$getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt, DATE_FORMAT(`chqdate`, '%d-%m-%Y') as chqdate 
                             FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' 
                            $cmbfromcond $cmbtocond $chqnocond $chqdatecond")or die(mysqli_error($conn));

$getno = mysqli_num_rows($getpmain);

if (isset($_POST["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">
            <?php include("includes/head_rep.php"); ?>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table style="background: white" class="table table-condensed table-hover table-bordered">
                        <thead>
                            <tr class="mySearchTr">
                                <th style="width:4%">Sr</th>
                                <th style="width:7%">Voucher No.</th>
                                <th style="width:6%">Date</th>
                                <th style="width:15%">From</th>
                                <th style="width:15%">To</th>
                                <th style="width:7%">Cheque No.</th>
                                <th style="width:8%">Cheque Date</th>
                                <th style="width:8%" class="text-right">Amount</th>
                                <th style="width:20%">Narration</th>

                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $totamt = 0;

                            while ($pmain = mysqli_fetch_array($getpmain)) {
//                                $getbranch = mysqli_query($conn, "SELECT * FROM `branch` WHERE `code`='$pmain[branch]'")or die(mysqli_error($conn));
//                                $mbranch = mysqli_fetch_array($getbranch);

                                $gbfrom = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode`='$pmain[frombc]'")or die(mysqli_error($conn));
                                $bfrom = mysqli_fetch_array($gbfrom);

                                $gbto = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode`='$pmain[tobc]'")or die(mysqli_error($conn));
                                $bto = mysqli_fetch_array($gbto);

                                $ctr++;
                                Echo "<tr id='boldfont'>";
                                Echo "<td>$ctr</td>";
                                Echo "<td style>$pmain[vouchno]</td>";
                                Echo "<td>$pmain[mydt]</td>";
                                Echo "<td>$bfrom[name]</td>";
                                Echo "<td>$bto[name]</td>";
                                Echo "<td>$pmain[chqno]</td>";
                                Echo "<td>$pmain[chqdate]</td>";
                                Echo "<td class='text-right'>$pmain[amount]</td>";
                                Echo "<td>$pmain[narr1]  $pmain[narr2]</td>";
                                Echo "</tr>";

                                $totamt = $totamt + $pmain[amount];
                            }
                            $colr = "green";
                            echo "<tr> ";
                            echo "<td style='color:$colr;' colspan='7' class='text-right'><h4><b>Grand Total</b></h4></td>";
                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totamt</b></h4></td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}
if (isset($_POST["excel"])) {

    $contents .= "Sr., Vouchno, Date, From ,To, Cheque No., Cheque Date, Amount, Narration\n";

    $ctr = 0;
    $totamt = 0;

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

        $gbfrom = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode`='$pmain[frombc]'")or die(mysqli_error($conn));
        $bfrom = mysqli_fetch_array($gbfrom);

        $gbto = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode`='$pmain[tobc]'")or die(mysqli_error($conn));
        $bto = mysqli_fetch_array($gbto);

        $ctr++;
        $contents .= $ctr . ",";
        $contents .= $pmain['vouchno'] . ",";
        $contents .= $pmain['mydt'] . ",";
        $contents .= $bfrom['name'] . ",";
        $contents .= $bto['name'] . ",";
        $contents .= $pmain['chqno'] . ",";
        $contents .= $pmain['chqdate'] . ",";
        $contents .= $pmain['amount'] . ",";
        $contents .= $pmain['narr1'] . " " . $pmain['narr2'] . "\n";


        $totamt = $totamt + $pmain['amount'];
    }
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= "Grand Total" . ",";
    $contents .= $totamt . "\n";

    $contents = strip_tags($contents);

if ($_POST["dtype"] == 1) {
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;
}
}
?>

