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

$maindb = "contra";
$formname = 'Contra Register';
$file = "contrareg_fr.php";

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

$frominit = $_POST["frominit"];
$toinit = $_POST["toinit"];

$mfrmdate = trim($_POST["fromdt"]);
$mtodate = trim($_POST["todt"]);


$sonnamecond = "";


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

if (!empty($frominit)) {
    $frominitcond = " AND `frombc`='$frominit ' ";
}
if (!empty($toinit)) {
    $toinitcond = " AND `tobc`='$toinit ' ";
}



$getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y')as vouchdt,DATE_FORMAT(`chqdate`, '%d-%m-%Y')as chqdate
                                    FROM $maindb WHERE vouchno != ''
                                    AND `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' 
                                    $frominitcond $toinitcond ") or die(mysqli_error($conn));
$get = mysqli_num_rows($getpmain);

include("includes/linkshead.php");
if (isset($_POST["view"])) {
?>
    <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;width: 100%" class="table table-condensed table-hover">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:4%">Voucher</th>
                                <th style="width:7%">Date</th>
                                <th style="width:12%;">From </th>
                                <th style="width:12%;">To</th>
                                <th style="width:6%;">Cheque No.</th>
                                <th style="width:7%;">Cheque Date</th>
                                <th style="width:5%;text-align:right">Amount</th>
                                <th style="width:15%;">Narration</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $sontot = 0;
                            $dautot = 0;

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

                                $getbank = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode` = '$pmain[frombc]'");
                                $banknm = mysqli_fetch_array($getbank);

                                $getcash = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode` = '$pmain[tobc]'");
                                $cash = mysqli_fetch_array($getcash);

                                $ctr++;
                                echo "<tr id='boldfont'>";
                                echo "<td>$pmain[vouchno]</td>";
                                echo "<td>$pmain[vouchdt]</td>";
                                echo "<td>$banknm[name]</td>";
                                echo "<td>$cash[name]</td>";
                                echo "<td>$pmain[chqno]</td>";
                                echo "<td>$pmain[chqdate]</td>";
                                echo "<td style='text-align:right;'>$pmain[amount]</td>";
                                echo "<td>$pmain[narration]   $pmain[narration2] </td>";

                                $sontot = $pmain['amount'] + $sontot;

                                $sonamt = number_format($sontot, 2);

                                echo "</tr>";
                            }
                            echo "<tr id='boldfont'>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td style='text-align:right;'>Total</td>";
                            echo "<td style='text-align:right;'>$sonamt</td>";
                            echo "<td></td>";

                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

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

    </html>
<?php
}
if (isset($_POST["excel"])) {

    $contents .= "Voucher,Date,Father Name,Son Name,Amount,Father Name,Daugther Name,Amount,English Date,Nikah Location\n";

    $ctr = 0;
    $sontot = 0;
    $dautot = 0;

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

        $time = date('h:i A', strtotime($pmain['engtime']));

        $address = $pmain['add1'] . $pmain['add2'] . $pmain['add3'];

        $ctr++;


        $contents .= "$pmain[vouchno]" . ",";
        $contents .= "$pmain[vouchdt]" . ",";
        $contents .= "$pmain[memno]" . "$pmain[memname]" . ",";
        $contents .= "$pmain[sonno]" . "$pmain[sonname]" . ",";
        $contents .= "$pmain[sonamt]" . ",";
        $contents .= "$pmain[fathno]" . "$pmain[fathname]" . ",";
        $contents .= "$pmain[dauno]" . "$pmain[dauname]" . ",";
        $contents .= "$pmain[dauamt]" . ",";
        $contents .= "$pmain[englishdt]" . "$pmain[engday]" . "$time" . ",";
        $contents .= "$address" . "\n";

        $sontot = $pmain['sonamt'] + $sontot;
        $dautot = $pmain['dauamt'] + $dautot;
    }
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "Total" . ",";
    $contents .= "$sontot" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "$dautot" . ",";
    $contents .= "" . ",";
    $contents .= "" . "\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;
    }
}
?>