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

$maindb = 'inwmfmain';
$trandb = 'inwmftran';
$formname = 'Inward Raw Material Register';
$file = 'minwreg_fr.php';

$entfile = "inward_mi.php";
$location = $_POST["location"];
$godown = $_POST["godown"];
$item = $_POST["item"];


$dtype = $_POST["dtype"];

if (!empty($godown)) {
    $godowncond = " AND `godown` = '$godown' ";
}

if (!empty($location)) {
    $locationcond = " AND `location` = '$location'";
}

$itemcond = "";
if (!empty($item)) {
    $itemcond = " AND b.`itemcode` = '$item' ";
}


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

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

$mfdt = date("d-m-Y", strtotime($mfrmdate));
$mtdt = date("d-m-Y", strtotime($mtodate));


$getpmain = mysqli_query($conn, "SELECT a.vouchno,a.vouchdt,a.godown, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt, b.srno, b.itemcode, b.qty, b.unit, b.location FROM $maindb a, $trandb b WHERE a.`vouchdt` >= '$mfrmdate' 
                                    AND a.`vouchdt` <= '$mtodate' AND a.vouchno = b.vouchno $locationcond $godowncond $itemcond")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; 
        }
        .colr{
            background: #26A69A;
            font-size: 17px;
            color:white;

        }

    </style>
    </head>
    <body>
        <div class="container">
            <?php include("includes/head_rep.php"); ?>
            <div class="row">
                <div class="col-lg-12 pad">
                    <div class="container-fluid">

                        <table style="background: white;width: 100%" class="table table-condensed table-hover table-bordered">
                            <thead class="mySearchTr">
                                <tr>
                                    <th style="width:5%;">Sr</th>
                                    <th style="width:5%;">Voucher</th>
                                    <th style="width:10%;">Date</th>
                                    <th style="width:10%;">Godown</th>
                                    <th style="width:20%;">Item Name</th>
                                    <th style="width:10%;" class="text-right">Qty.</th>
                                    <th style="width:10%">Unit</th>
                                    <th style="width:15%">Location</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $ctr = 0;

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

                                    $ggodw = mysqli_query($conn, "SELECT `code`,`name` FROM `godown` where `code` = '$pmain[godown]'") or die(mysqli_error($conn));
                                    $sgodw = mysqli_fetch_array($ggodw);
                                    $godw = "$sgodw[name]";

                                    $gloc = mysqli_query($conn, "SELECT `code`,`name` FROM `location` where `code` = '$pmain[location]'") or die(mysqli_error($conn));
                                    $floc = mysqli_fetch_array($gloc);
                                    $loc = "$floc[name]";

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


                                    $ctr++;
                                    echo "<tr class=' clickable-row2' data-href='$entfile?mode=edit&vouchno=$pmain[vouchno]' [target]='_blank'>";
                                    Echo"<td>$ctr</td>";
                                    Echo"<td>$pmain[vouchno]</td>";
                                    Echo"<td>$pmain[mydt]</td>";
                                    Echo"<td>$godw</td>";
                                    Echo"<td>$showitem[name]</td>";
                                    Echo"<td class='text-right'>$pmain[qty]</td>";
                                    Echo"<td>$pmain[unit]</td>";
                                    Echo"<td>$loc</td>";
                                    Echo"</tr>";


                                    $tqty = $tqty + $pmain[qty];
                                }
                                Echo "<tr>";
                                Echo "<td class='colr' colspan='4'></td>";
                                Echo "<td class='text-right colr'><b>Total</b></td>";
                                Echo "<td class='text-right colr'><b>".number_format($tqty,2)."</b></td>";
                                Echo "<td class='colr' colspan='3'></td>";
                                Echo "</tr>";
                                ?>
                            </tbody>
                        </table>
                    </div> 
                </div> 
            </div>
        </div>
        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}
if (isset($_POST["excel"])) {

    $contents .= "Sr,Voucher,Date,Godown,Itemname,Qty.,Unit,Location\n";
    $ctr = 0;

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

        $ggodw = mysqli_query($conn, "SELECT `code`,`name` FROM `godown` where `code` = '$pmain[godown]'") or die(mysqli_error($conn));
        $sgodw = mysqli_fetch_array($ggodw);
        $godw = "$sgodw[name]";

        $gloc = mysqli_query($conn, "SELECT `code`,`name` FROM `location` where `code` = '$pmain[location]'") or die(mysqli_error($conn));
        $floc = mysqli_fetch_array($gloc);
        $loc = "$floc[name]";

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


        $ctr++;
        $contents .= "$ctr" . ",";
        $contents .= "$pmain[vouchno]" . ",";
        $contents .= "$pmain[mydt]" . ",";
        $contents .= "$godw" . ",";
        $contents .= "$showitem[name]" . ",";
        $contents .= "$pmain[qty]" . ",";
        $contents .= "$pmain[unit]" . ",";
        $contents .= "$loc" . "\n";

        $tqty = $tqty + $pmain[qty];
    }
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "Total" . ",";
    $contents .= "$tqty" . ",";
    $contents .= "" . ",";
    $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;
    }
}
?>