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

$maindb = "itemgrp";
$formname = "Item Group List";
$file = "itemgrplst.php";

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

if (isset($_POST["view"])) {
    $code = $_POST[code];
    $name = $_POST[name];

    $codecond = "";
    $namecond = "";

    if (!empty($code)) {
        $codecond = " AND `code` LIKE '%$code%' ";
    }

    if (!empty($name)) {
        $namecond = " AND `name` LIKE '%$name%' ";
    }

    $getpmain = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `code`!='' $namecond $codecond order by name ")or die(mysqli_error($conn));
    $getno = mysqli_num_rows($getpmain);


    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">
                    <?php
                    require("includes/dbconnect.php");
                    $comp = mysqli_query($conn, "SELECT * FROM `company`") or die(mysql_errno());
                    $zcomp = mysqli_fetch_assoc($comp);
                    echo "<font size='4px' ><b>$zcomp[name]</font></b></font><br />";
                    echo "<font size='2px' ><b> $zcomp[address]. </b></font><br />";
//              echo "<b><font >$formname &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font size='3px'>Date : ".date("M-d-Y") . "</b><br />";
                    echo "<b>" . "Total  - $getno</b><br />";
//              echo "<br />";
                    ?>
                </div>

                <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-6">
                    <label class="label-control marg">Print - </label>
                    <label class="label-control marg"><?php Echo $formname; ?></label>
                    <div class="col-lg-1 pull-right" >
                        <button id='cancel' type="reset" class="btn btn-primary" style="background-color:red;" onClick="window.location.href = '<?php echo $file; ?>'"> 
                            Go Back
                        </button>
                    </div>
                </div>

            </div>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table class="table table-condensed table-hover table-bordered">
                        <tr  style="background-color: #05B2EA">
                            <th>Sr</th>
                            <th>Code</th> 
                            <th>Name</th>
                            <th>Order for Invoice</th>
                        </tr>
                        <?php
                        $ctr = 1;
                        while ($pmain = mysqli_fetch_array($getpmain)) {

                            Echo "<tr>";
                            Echo "<td>$ctr</td>";
                            Echo "<td>$pmain[code]</td>";
                            Echo "<td>$pmain[name]</td>";
                            Echo "<td>$pmain[orderinv]</td>";
                            Echo "</tr>";
                            $ctr++;
                        }
                        ?>
                    </table>
                </div>
            </div>
        </div>

        <?php include("includes/linksfoot.php"); ?>
    </body>
    </html>
    <?
}
if (isset($_POST["excel"])) {
    $code = $_POST['code'];
    $name = $_POST['name'];

    $codecond = "";
    $namecond = "";

    if (!empty($code)) {
        $codecond = " AND `code` LIKE '%$code%' ";
    }

    if (!empty($name)) {
        $namecond = " AND `name` LIKE '%$name%' ";
    }

    $getpmain = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `code`!='' $namecond $codecond order by name ")or die(mysqli_error($conn));
    $getno = mysqli_num_rows($getpmain);

    require("includes/dbconnect.php");
    $comp = mysqli_query($conn, "SELECT * FROM `company`") or die(mysql_errno());
    $zcomp = mysqli_fetch_assoc($comp);
//                    echo "<font size='4px' ><b>$zcomp[name]</font></b></font><br />";
//                    echo "<font size='2px' ><b> $zcomp[address]. </b></font><br />";
////              echo "<b><font >$formname &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<font size='3px'>Date : ".date("M-d-Y") . "</b><br />";
//                    echo "<b>" . "Total  - $getno</b><br />";
//              echo "<br />";
    $contents.="Sr.,Code,Name\n";

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

        $contents.=$ctr . ",";
        $contents.=$pmain[code] . ",";
        $contents.=$pmain[name] . "\n";
        $contents.=$pmain[orderinv] . "\n";
        $ctr++;
    }
    $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;
    }
}
?>
