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

$maindb = "item";
$trandb = "itemsub";
$formname = "Item Print";
$file = "itemprint.php";

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

if (isset($_POST["view"])) {
    $itemgrp = $_POST[itemgrp];
    $item = $_POST[item];

    $itemgrpcond = "";
    $itemcond = "";

    if (!empty($itemgrp)) {
        $itemgrpcond = " AND `itemgrp`='$itemgrp' ";
    }

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

    $getpmain = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `code` != '' $itemgrpcond $itemcond ")or die(mysqli_error($conn));

    include("includes/linkshead.php");
    ?>
    <script src="MyScript/item.js" type="text/javascript"></script>
    </head>
    <body>
        <div class="container">
            <div class="row">
                <div class="col-lg-6">
                    <label class="label-control marg">Date - </label>
                    <label class="label-control marg"><?php Echo Date("d-m-Y"); ?></label>
                </div>
                <div class="col-lg-5">
                    <label class="label-control marg">Print - </label>
                    <label class="label-control marg"><?php Echo $formname; ?></label>
                </div>
                <div class="col-lg-1">
                    <a onclick="window.close()"  class="btn btn-primary">GO Back</a>
                </div>
            </div>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table style="width:100%;background: white" class="table table-condensed table-hover">
                        <thead>
                            <tr style="height:10px;background-color: #78909C;">
                                <th style="width:5%">Sr</th>
                                <th style="width:11%">Item Code</th>
                                <th style="width:18%">Item Name</th>
                                <th style="width:11%">Group Name</th>
                                <th style="width:11%"class='numeric'>Cost</th>
                                <th style="width:11%"class='numeric'>Rate</th>
                                <th style="width:11%"class='numeric'>MSP</th>
                                <th style="width:11%"class='numeric'>MRP</th>
                                <th style="width:11%"class='numeric'>Min.Stock</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);
//                            $srcunit = mysqli_query($conn, "SELECT `code`,`name` FROM `unit` where `code`='$pmain[unit]'")or die(mysqli_error($conn));
//                            $sunit = mysqli_fetch_array($srcunit);
//                            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%'>$grpit[name]</td>";
                                Echo "<td style='width:5%' class='numeric'>$pmain[cost]</td>";
                                Echo "<td style='width:5%' class='numeric'>$pmain[rate]</td>";
                                Echo "<td style='width:5%' class='numeric'>$pmain[msp]</td>";
                                Echo "<td style='width:5%' class='numeric'>$pmain[mrp]</td>";
                                Echo "<td style='width:5%' class='numeric'>$pmain[minimum]</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"])) {
    $itemgrp = $_POST[itemgrp];
    $item = $_POST[item];

    $itemgrpcond = "";
    $itemcond = "";

    if (!empty($itemgrp)) {
        $itemgrpcond = " AND `itemgrp`='$itemgrp' ";
    }

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

    $getpmain = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `code` != '' $itemgrpcond $itemcond ")or die(mysqli_error($conn));

    $contents.="Sr.,Item Code,Item Name,Group Name,Cost,Rate,MSP,MRP,Min. Stock\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);

        $ctr++;
        $pcode = $pmain[code];
        $pname = $pmain[name];
        $pgrpitname = $grpit[name];
        $pcost = $pmain[cost];
        $prate = $pmain[rate];
        $pmsp = $pmain[msp];
        $pmrp = $pmain[mrp];
        $pminimum = $pmain[minimum];

        $contents.=$ctr . ",";
        $contents.=$pcode . ",";
        $contents.=$pname . ",";
        $contents.=$pgrpitname . ",";
        $contents.=$pcost . ",";
        $contents.=$prate . ",";
        $contents.=$pmsp . ",";
        $contents.=$pmrp . ",";
        $contents.=$pminimum . "\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;
    }
}
?>