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

$maindb = "trfmain";
$trandb = "trftran";
$formname = "Transfer - Register";
$file = "stktrnrg.php";

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

$prodname = $_POST["prodname"];
$deptfrom = $_POST["fromdept"];
$deptto = $_POST["todept"];

if (!empty($prodname)) {
    $prodcond = " AND `prodcode`='$prodname' ";
}
if (!empty($deptfrom)) {
    $fromcond = " AND `fromdept`='$deptfrom' ";
}
if (!empty($deptto)) {
    $tocond = " AND `todept`='$deptto' ";
}

$mfromdate = trim($_POST["fromdate"]);
$mtodate = trim($_POST["todate"]);

if (empty($mfromdate)) {
    browse("From Date Cannot Be Empty");
    echo"<script>window.close();</script>";
}
if (empty($mtodate)) {
    browse("To Date Cannot Be Empty");
    echo"<script>window.close();</script>";
}
$mfdt = date("d-m-Y", strtotime($mfromdate));
$mtdt = date("d-m-Y", strtotime($mtodate));


$getpmain = mysqli_query($conn, "SELECT * FROM $maindb  WHERE vouchdt >= '$mfromdate' AND vouchdt <= '$mtodate' $fromcond $tocond ") or die(mysqli_error($conn)); 

if (isset($_POST["view"])) {

    include("includes/linkshead.php");
?>
    <style type="text/css">
        #tabital {
            font-style: italic;
        }

        #font1 {
            font-style: italic;
        }

        #boldfont {
            font-weight: bold;
        }

        th {
            background: #6082BF;
        }
    </style>
    <!--<script src="MyScript/item.js" type="text/javascript"></script>-->
    </head>

    <body>
        <div class="container">
            <?php include("includes/head_rep.php"); ?>
            <div class="container">
                <div class="row">
                    <div class="col-lg-12 pad">
                        <table style="background: white;width: 100%" class="table table-condensed table-hover table-bordered">
                            <thead class="mySearchTr">
                                <tr>
                                    <th style="width:10%">Sr. </th>
                                    <th style="width:10%">Voucher </th>
                                    <th style="width:10%">Date</th>
                                    <th style="width:20%">From Dept.</th>
                                    <th style="width:20%">To Dept.</th>
                                    <th style="width:20%">Shift</th>
                                    <th style="width:20%">Total</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $ctr = 0;
                                $totqty = 0;

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

                                    $mown = mysqli_query($conn, "SELECT * FROM dept where code = $pmain[fromdept]") or die(mysqli_error($conn));
                                    $trow = mysqli_fetch_array($mown);
                                    $fromdate = $trow['name'];

                                    $mown1 = mysqli_query($conn, "SELECT * FROM dept where code = $pmain[todept]") or die(mysqli_error($conn));
                                    $trow1 = mysqli_fetch_array($mown1);
                                    $todate = $trow1['name'];

                                    $mshift = mysqli_query($conn, "SELECT * FROM shift where shift = '$pmain[shift]'") or die(mysqli_error($conn));
                                    $trow = mysqli_fetch_array($mshift);
                                    $shift = $trow['shift'];

                                    $ctr++;
                                    echo "<tr id='boldfont'>";
                                    echo "<table style='width:100%;' id='tabital' class='table table-condensed table-hover table-bordered'>";
                                    echo "<td style='width:10%'>$ctr</td>";
                                    echo "<td style='width:10%'>$pmain[vouchno]</td>";
                                    echo "<td style='width:10%'>$pmain[vouchdt]</td>";
                                    echo "<td style='width:20%'>$fromdate</td>";
                                    echo "<td style='width:20%'>$todate</td>";
                                    echo "<td style='width:20%'>$shift</td>";
                                    echo "<td style='width:20%' class='text-right'><b>$pmain[totamount]</b></td>";
                                    echo "</tr>";

                                    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 "<th style='width:7%;'>Sr. </th>";
                                    echo "<th style='width:10%;'>Prod.Code</th>";
                                    echo "<th style='width:25%;'>Product Name</th>";
                                    echo "<th style='width:13%;'>Qty.</th>";
                                    echo "<th style='width:15%;'>Unit</th>";
                                    echo "<th style='width:15%;'>Rate</th>";
                                    echo "<th style='width:15%;'>Amount</th>";
                                    echo "<tr>";



                                    $gettran = mysqli_query($conn, "SELECT * FROM `trftran` WHERE vouchno = '$pmain[vouchno]' $prodcond ") or die(mysqli_error($conn));
                                    while ($fettran = mysqli_fetch_array($gettran)) {

                                        $getpd = mysqli_query($conn, "SELECT name,prodcode,rate FROM `product` WHERE `prodcode`= '$fettran[prodcode]'") or die(mysqli_error($conn));
                                        $lpd = mysqli_fetch_array($getpd);

                                        $ctr2++;
                                        echo "<tr>";
                                        echo "<td>$ctr2</td>";
                                        echo "<td>$fettran[prodcode]</td>";
                                        echo "<td>$lpd[name]</td>";
                                        echo "<td style='text-align:right'>$fettran[qty]</td>";
                                        echo "<td style='text-align:right'>$fettran[unit]</td>";
                                        echo "<td style='text-align:right'>$fettran[rate]</td>";
                                        echo "<td style='text-align:right'>$fettran[amount]</td>";
                                        echo "</tr>";

                                        $totqty = $totqty + $fettran['qty'];
                                        $totrate = $totrate + $fettran['rate'];
                                        $totamt = $totamt + $fettran['amount'];
                                    }

                                    echo "<tr>";
                                    echo "<td colspan='3' class='text-right'><b>Total</b></td>";
                                    echo "<td class='text-right'><b></b>$totqty</td>";
                                    echo "<td class='text-right'><b></b></td>";
                                    echo "<td class='text-right'><b>$totrate</b></td>";
                                    echo "<td class='text-right'><b>$totamt</b></td>";
                                    echo "</tr>";
                                    echo "</table>";
                                    $ttotamt = $ttotamt + $pmain['totamount'];
                                }



                                echo "<tr>";
                                echo "<td colspan='5' class='text-right'><b>Grand Total</b></td>";
                                echo "<td style='text-align:right'><b>$ttotamt</b></td>";
                                echo "</table>";
                                echo "</tr>";
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            <?php include("includes/linksfoot.php");
            ?>
    </body>

    </html>
<?php
}

if (isset($_POST["excel"])) {

    $contents .= "Sr., Voucher, Date, From Dept., To Dept., Shift, Total\n";

    $ctr = 0;
    $totqty = 0;

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

        $mown = mysqli_query($conn, "SELECT * FROM dept where code = $pmain[fromdept]") or die(mysqli_error($conn));
        $trow = mysqli_fetch_array($mown);
        $fromdate = $trow['name'];

        $mown1 = mysqli_query($conn, "SELECT * FROM dept where code = $pmain[todept]") or die(mysqli_error($conn));
        $trow1 = mysqli_fetch_array($mown1);
        $todate = $trow1['name'];

        $mshift = mysqli_query($conn, "SELECT * FROM shift where shift = '$pmain[shift]'") or die(mysqli_error($conn));
        $trow = mysqli_fetch_array($mshift);
        $shift = $trow['shift'];

        $ctr++;
        $contents .= "$ctr" . ",";
        $contents .= "$pmain[vouchno]" . ",";
        $contents .= "$pmain[vouchdt]" . ",";
        $contents .= "$fromdate" . ",";
        $contents .= "$todate" . ",";
        $contents .= "$shift" . ",";
        $contents .= "$pmain[totamount]" . "\n";

        $contents .= "Sr., Prod.Code, Product Name, Qty., Unit, Rate, Amount\n";

        $gettran = mysqli_query($conn, "SELECT * FROM `trftran` WHERE vouchno = '$pmain[vouchno]' ") or die(mysqli_error($conn));
        while ($fettran = mysqli_fetch_array($gettran)) {

            $getpd = mysqli_query($conn, "SELECT name,prodcode,rate FROM `product` WHERE `prodcode`= '$fettran[prodcode]'") or die(mysqli_error($conn));
            $lpd = mysqli_fetch_array($getpd);

            $ctr2++;
            $contents .= "$ctr2" . ",";
            $contents .= "$fettran[prodcode]" . ",";
            $contents .= "$lpd[name]" . ",";
            $contents .= "$fettran[qty]" . ",";
            $contents .= "$fettran[unit]" . ",";
            $contents .= "$fettran[rate]" . ",";
            $contents .= "$fettran[amount]" . "\n";

            $totqty = $totqty + $fettran['qty'];
            $totrate = $totrate + $fettran['rate'];
            $totamt = $totamt + $fettran['amount'];
        }

        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "Total" . ",";
        $contents .= "$totqty" . ",";
        $contents .= "" . ",";
        $contents .= "$totrate" . ",";
        $contents .= "$totamt" . "\n";

        $ttotamt = $ttotamt + $pmain['totamount'];
    }

    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "Grand Total" . ",";
    $contents .= "$ttotamt" . "\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;
    }
}
?>