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

ini_set("display_errors", 1);
error_reporting(E_ERROR);

$maindb = "item";
$trandb = "prodsize";
// $trandb = "itemtran";
$formname = "Item Print";
$file = "itemprint.php";

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

$itemgrp = $_POST['itemgrp'];
$item = $_POST['item'];
$subgrp = $_POST['subgrp'];
$subname = $_POST['subname'];
$kargname = $_POST['kargname'];

$itemgrpcond = "";
$itemcond = "";
$subgrpcond = "";
$subnamecond = "";
$kargnamecond = "";

if (!empty($subgrp)) {
    $subgrpcond = " AND `itemsubgrp`='$subgrp' ";
}
if (!empty($subname)) {
    $subnamecond = " AND `subname`='$subname' ";
}
if (!empty($kargname)) {
    $kargnamecond = " AND `kargname`='$kargname' ";
}
if (!empty($itemgrp)) {
    $itemgrpcond = " AND `itemgrp`='$itemgrp' ";
}
if (!empty($item)) {
    $itemcond = " AND a.`code` = '$item' ";
}

$getpmain = mysqli_query($conn, "SELECT a.*, b.* FROM `$maindb` a LEFT JOIN $trandb b ON a.code = b.code 
                                 WHERE a.`code` != '' $itemcond $itemgrpcond ") or die(mysqli_error($conn));

if (isset($_POST["view"])) {
    include("includes/linkshead.php");
?>
    <script src="MyScript/item.js" type="text/javascript"></script>
    </head>

    <body>
        <div class="container">
            <?php include("includes/head_rep.php");
            ?>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table style="width:100%;background: white" class="table table-condensed table-bordered">
                        <thead>
                            <tr style="height:10px;" class="mySearchTr">
                                <th style="width:5%">Sr</th>
                                <th style="width:10%">Name</th>
                                <th style="width:10%">Size</th>
                                <th style="width:20%">Item Group</th>
                                <th style="width:10%">HSN Code</th>
                                <th style="width:11%" class='numeric'>GST %</th>
                                <th style="width:11%" class='numeric'>Purchase Rate</th>
                                <th style="width:11%" class='numeric'>Selling Rate</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            while ($pmain = mysqli_fetch_array($getpmain)) {

                                $getitemgrp = mysqli_query($conn, "SELECT * FROM `itemgrp` WHERE `code`='$pmain[itemgrp]'") or die(mysqli_error($conn));
                                $grpit = mysqli_fetch_array($getitemgrp);

                                $finditemsub = mysqli_query($conn, "SELECT * FROM `itemsubgrp` WHERE `code`='$pmain[itemsubgrp]'") or die(mysqli_error($conn));
                                $fitemsub = mysqli_fetch_array($finditemsub);

                                //while ($itemgrp = mysqli_fetch_object($getitemgrp)) {
                                $ctr++;
                                echo "<tr>";
                                echo "<td style='width:5%'>$ctr</td>";
                                //Echo "<td style='width:5%'>$pmain[code]</td>";
                                echo "<td style='width:5%'>$pmain[name]</td>";
                                echo "<td style='width:5%'>$pmain[size]</td>";
                                echo "<td style='width:5%'>$grpit[name]</td>";
                                echo "<td style='width:5%'>$pmain[hsncode]</td>";
                                echo "<td style='width:5%' class='numeric'>$pmain[gstperc]</td>";
                                echo "<td style='width:5%' class='numeric'>$pmain[cost]</td>";
                                echo "<td style='width:5%' class='numeric'>$pmain[rate]</td>";

                                echo "</tr>";

                                //                            if ($_POST["rtype"] == 2) {
                                //                                Echo "<tr style='height:10px;'>";
                                //                                Echo "<td colspan='25'>";
                                //                                Echo "<table style='width:100%;' class='table table-condensed table-hover'>";
                                //                                Echo "<th style='background-color: #cccccc; width:5%'>Sr</th>";
                                //                                Echo "<th style='background-color: #cccccc; width:10%'>Godown</th>";
                                //                                Echo "<th style='background-color: #cccccc; width:10%'>Batch</th>";
                                //                                Echo "<th style='background-color: #cccccc; width:15%'>Qty.</th>";
                                //                                Echo "<tr>";
                                //
                                //
                                //                                $ctr2 = 0;
                                //                                $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `code`='$pmain[code]'")or die(mysqli_error($conn));
                                //                                while ($tran = mysqli_fetch_array($gettran)) {
                                //
                                //                                    $ctr2++;
                                //                                    Echo "<tr>";
                                //                                    Echo "<td style='width:5%'>$ctr2</td>";
                                //                                    Echo "<td style='width:10%'>$tran[godown]</td>";
                                //                                    Echo "<td style='width:10%'>$tran[batch]</td>";
                                //                                    Echo "<td style='width:15%' class='numeric' text-align:left>$tran[qty]</td>";
                                //                                    Echo "</tr>";
                                //                                }
                                //                                Echo "</table>";
                                //                                Echo "</td>";
                                //                            }
                            }
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

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

    </html>
<?php
}


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

    $contents .= "Sr, Name, Size, Item Group, HSN Code, GST %, Purchase Rate, Selling Rate \n";

    $ctr = 0;
    while ($pmain = mysqli_fetch_array($getpmain)) {

        $getitemgrp = mysqli_query($conn, "SELECT * FROM `itemgrp` WHERE `code`='$pmain[itemgrp]'") or die(mysqli_error($conn));
        $grpit = mysqli_fetch_array($getitemgrp);

        $finditemsub = mysqli_query($conn, "SELECT * FROM `itemsubgrp` WHERE `code`='$pmain[itemsubgrp]'") or die(mysqli_error($conn));
        $fitemsub = mysqli_fetch_array($finditemsub);

        $ctr++;
        $contents .= "$ctr" . ",";
        $contents .= "$pmain[name]". ",";
        $contents .= "$pmain[size]". ",";
        $contents .= "$grpit[name]". ",";
        $contents .= "$pmain[hsncode]". ",";
        $contents .= "$pmain[gstperc]". ",";
        $contents .= "$pmain[cost]". ",";
        $contents .= "$pmain[rate]". "\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;
    }
}
?>