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

$maindb = "item";
$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 `code` = '$item' ";
}

$getpmain = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `code` != '' $subgrpcond $subnamecond $kargnamecond $itemgrpcond $itemcond ")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:18%">Item Name</th>
                                <th style="width:18%">Sub Name</th>
                                <th style="width:11%">Group Name</th>
                                <th style="width:11%">SubGroup Name</th>
                                <th style="width:11%">Karigar</th>
                                <th style="width:11%"class='numeric'>Cost</th>
                                <th style="width:11%"class='numeric'>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[subname]</td>";
                                Echo "<td style='width:5%'>$grpit[name]</td>";
                                Echo "<td style='width:5%'>$fitemsub[name]</td>";
                                Echo "<td style='width:5%' >$pmain[kargname]</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.,Item Name,Sub Name,Group Name,Sub Group,Karigar,Cost,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++;
        $pcode = $pmain[code];
        $pname = $pmain[name];
        $psubname = $pmain[subname];
        $pgrpitname = $grpit[name];
        $psubgrpname = $fitemsub[name];
        $pkargname = $pmain[kargname];
        $pcost = $pmain[cost];
        $prate = $pmain[rate];
        $pmsp = $pmain[msp];
        $pmrp = $pmain[mrp];
        $pminimum = $pmain[minimum];

        $contents.=$ctr . ",";
//        $contents.=$pcode . ",";
        $contents.=$pname . ",";
        $contents.=$psubname . ",";
        $contents.=$pgrpitname . ",";
        $contents.=$psubgrpname . ",";
        $contents.=$pkargname . ",";
        $contents.=$pcost . ",";
        $contents.=$prate . "\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;
    }
}
?>