<?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"];

$file = 'inwreg_fr.php';

$supplier = $_POST["supplier"];
$p = $_GET["p"];
$ptype = $_POST["ptype"];
$pchtype = $_POST["pchtype"];
$dtype = $_POST["dtype"];
$gsttype = $_POST["gsttype"];

$formname = 'Inward Register';

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


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

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; outreg.php");
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; outreg.php");
    }
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
}

if (!empty($gsttype)) {
    if ($gsttype == 1) {
        $gstcond = "AND `oms` = '1'";
    } else {
        $gstcond = "AND `oms` = ''";
    }
} else {
    $gstcond = "";
}
//browse(1);
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 `inwmain` WHERE `vouchdt` >= '$mfrmdate' 
                                        AND `vouchdt` <= '$mtodate'")or die(mysqli_error($conn));

            $getno2 = mysqli_num_rows($getpmain2);

                ?>
                <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>Inward 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>
                        <?php if ($ptype != "") { ?>
                            <table width="100%" class="table" style="background-color: #98F9F0">
                                <tr><td width="7%" style="font-size:14px">Pay Type : </td>
                                    <td width="93%" style="font-size:14px"><b><?php echo $paytype; ?></b></td></tr>
                            </table>
                        <?php } ?>
                        <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="4%;">Voucher</th>
                                    <th width="6%;">Date</th>
                                    <th width="20%;" class="text-right">Total Qty</th>
                                    <th width="20%;" class="text-right">Total</th>
                                    <th width="67%;">Remarks</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $pctr = 0;
                                $grossamt = 0;

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

                                    $pctr++;
                                    Echo"<tr data-href='inward.php?mode=edit&vouchno=$pmain[vouchno]' class='clickable-row2' [target]='_blank'>";
                                    Echo"<td>$pctr</td>";
                                    Echo"<td>$pmain[vouchno]</td>";
                                    Echo"<td>$pmain[vouchdt]</td>";
                                    Echo"<td class='text-right'>$pmain[totalqty]</td>";
                                    Echo"<td class='text-right'>$pmain[grossamt]</td>";
                                    Echo"<td>$pmain[rem1] $pmain[rem2]</td>";
                                    Echo"</tr>";

                                    $grossamt = $grossamt + $pmain['grossamt'];

                                    if ($_POST["dtype"] == 2) {

                                        Echo "<tr style='height:10px;'>";
                                        Echo "<td colspan='25'>";
                                        Echo "<table style='width:100%;' id='tabital' class='table table-condensed table-hover table-bordered'>";
                                        Echo "<thead>";
                                        Echo "<tr class='' style='background-color:#BABFC2'>";
                                        Echo "<th style='width:5%;'>Sr</th>";
                                        Echo "<th style='width:65%;' class=''>Item</th>";
                                        Echo "<th style='width:10%;' class='text-right'>Qty.</th>";
                                        Echo "<th style='width:10%;' class='text-right'>Rate</th>";
                                        Echo "<th style='width:10%;' class='text-right'>Amount</th>";
                                        Echo "</tr>";
                                        Echo "<thead>";
                                        Echo "<tr>";


                                        $ctr2 = 0;
                                        $pctr2 = 0;
                                        $pcartqty = $pqty = $ptotqty = $ptotamount = $ptotdiscount = $ptotvat = $ptotal = 0;

                                        $gettran = mysqli_query($conn, "SELECT * FROM `inwtran` WHERE `vouchno`='$pmain[vouchno]' order by srno") or die(mysqli_error($conn));
                                        while ($tran = mysqli_fetch_array($gettran)) {

                                            $getitem = mysqli_query($conn, "SELECT `code`,`name` FROM `product` WHERE `code`='$tran[itemcode]'") or die(mysqli_error($conn));
                                            $showitem = mysqli_fetch_array($getitem);

                                            $ctr2++;
                                            Echo "<tr>";
                                            Echo "<td>$ctr2</td>";
                                            Echo "<td>$showitem[name]</td>";
                                            Echo "<td class='text-right'>$tran[qty]</td>";
                                            Echo "<td class='text-right'>$tran[cost]</td>";
                                            Echo "<td class='text-right'>$tran[amount]</td>";
                                            Echo "</tr>";

                                            $pcartqty = $pcartqty + $tran['cartqty'];
                                            $ptotdiscount = $ptotdiscount + $tran['discamt'];
                                            $ptotvat = $ptotvat + $tran['vat'];
                                            $ptotal = $ptotal + $tran['total'];
                                            $ptotqty = $ptotqty + $tran['totqty'];
                                            $pqty = $pqty + $tran['qty'];
                                            $ptotamount = $ptotamount + $tran['amount'];
                                        }

                                        Echo "<tr style='background-color:#d8e0e6'>";
                                        Echo "<td></td>";
                                        Echo "<td>Total</td>";
                                        Echo "<td class='text-right'>$pqty</td>";
                                        Echo "<td class='text-right'>$prate</td>";
                                        Echo "<td class='text-right'>$ptotamount</td>";
                                        Echo "</tr>";
                                        Echo "</table>";
                                    }
                                }
                                Echo "<tr style='background-color: #98F9F0'>";
                                Echo "<td colspan='3'></td>";
                                Echo "<td class='text-right'><b>Total</b></td>";
                                Echo "<td class='numeric text-right'><b>" . number_format($grossamt, 2) . "</b></td>";
                                Echo "<td></td>";
                                Echo "</tr>";
                                ?>
                            </tbody>
                        </table>
                    </div> 
                </div>

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

    $contents ."Sr, Voucher, Date, Total Qty, Total, Remarks\n";

                                $pctr = 0;
                                $grossamt = 0;

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

                                    $pctr++;
                                    $contents .="$pctr" . ",";
                                    $contents .="$pmain[vouchno]" . ",";
                                    $contents .="$pmain[vouchdt]" . ",";
                                    $contents .="$pmain[totalqty]" . ",";
                                    $contents .="$pmain[grossamt]" . ",";
                                    $contents .="$pmain[rem1]" . "$pmain[rem2]" . "\n";

                                    $grossamt = $grossamt + $pmain['grossamt'];

                                    if ($_POST["dtype"] == 2) {

                                        $contents .="Sr, Item, Qty., Rate, Amount\n";

                                        $ctr2 = 0;
                                        $pctr2 = 0;
                                        $pcartqty = $pqty = $ptotqty = $ptotamount = $ptotdiscount = $ptotvat = $ptotal = 0;

                                        $gettran = mysqli_query($conn, "SELECT * FROM `inwtran` WHERE `vouchno`='$pmain[vouchno]' order by srno") or die(mysqli_error($conn));
                                        while ($tran = mysqli_fetch_array($gettran)) {

                                            $getitem = mysqli_query($conn, "SELECT `code`,`name` FROM `product` WHERE `code`='$tran[itemcode]'") or die(mysqli_error($conn));
                                            $showitem = mysqli_fetch_array($getitem);

                                            $ctr2++;
                                            $contents .="$ctr2" . ",";
                                            $contents .="$showitem[name]" . ",";
                                            $contents .="$tran[qty]" . ",";
                                            $contents .="$tran[cost]" . ",";
                                            $contents .="$tran[amount]" . "\n";

                                            $pcartqty = $pcartqty + $tran['cartqty'];
                                            $ptotdiscount = $ptotdiscount + $tran['discamt'];
                                            $ptotvat = $ptotvat + $tran['vat'];
                                            $ptotal = $ptotal + $tran['total'];
                                            $ptotqty = $ptotqty + $tran['totqty'];
                                            $pqty = $pqty + $tran['qty'];
                                            $ptotamount = $ptotamount + $tran['amount'];
                                        }

                                        $contents .="" . ",";
                                        $contents .="Total" . ",";
                                        $contents .="$pqty" . ",";
                                        $contents .="$prate" . ",";
                                        $contents .="$ptotamount" . "\n";
                                    }
                                }
                                $contents .="" . ",,,";
                                $contents .="Total" . ",";
                                $contents .="$grossamt" . "\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;
    }
}
?>