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

$maindb = "item";
$formname = "Item List";
$file = "itemlst.php";

$zseries = $_SESSION['user_series'];
$myear = $_SESSION["tit_" . $dbname];
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);
$usertype = $_SESSION["usertype"];


$name = $_POST['name'];
$itemgrp = $_POST['itemgrp'];
$size = $_POST['size'];
$hsncode = $_POST['hsncode'];
$gstperc = $_POST['gstperc'];

$namecond = "";
$grpcond = "";
$sizecond = "";
$hsncond = "";
$gstcond = "";

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

if (!empty($itemgrp)) {
    $grpcond = " AND `itemgrp` = '$itemgrp' ";
}
if (!empty($size)) {
    $sizecond = " AND `size` = '$size' ";
}

if (!empty($hsncode)) {
    $hsncond = " AND `hsncode` = '$hsncode' ";
}

if (!empty($gstperc)) {
    $gstcond = " AND `taxperc` = '$gstperc' ";
}

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

if (isset($_POST["view"])) {
    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 id="" class="table table-condensed table-hover table-bordered">
                        <tr style="background-color: #05B2EA">
                            <th>Sr</th>
                            <th>Name</th>
                            <th>Size</th>
                            <th>Unit</th>
                            <th>Group</th>
                            <th>Ord for Invoice</th>
                            <th>Name Print</th>
                            <th>Name for Custom Inv</th>
                            <th>Name for Party Inv</th>
                            <th>Tax %</th>
                            <th>Hsncode</th>
                        </tr>
                        <?php
                        $ctr = 1;
                        while ($pmain = mysqli_fetch_array($getpmain)) {
                            mysqli_select_db($conn, $zdb);

                            $getgroup1 = mysqli_query($conn, "SELECT  *  FROM `itemgrp` where code = '$pmain[itemgrp]'") or die(mysqli_error($conn));
                            $grop = mysqli_fetch_array($getgroup1);
                            $groupname = $grop['name'];
                            echo "<tr>";
                            echo "<td>$ctr</td>";
                            echo "<td>$pmain[name]</td>";
                            echo "<td>$pmain[size]</td>";
                            echo "<td>$pmain[unit]</td>";
                            echo "<td>$groupname</td>";
                            echo "<td>$pmain[levelno]</td>";
                            echo "<td>$pmain[nameprin]</td>";
                            echo "<td>$pmain[namecstmprin]</td>";
                            echo "<td>$pmain[nameprtyprin]</td>";
                            echo "<td>$pmain[taxperc]</td>";
                            echo "<td>$pmain[hsncode]</td>";
                            echo "</tr>";
                            $ctr++;
                        }
                        ?>
                    </table>
                </div>
            </div>
        </div>

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

    </html>
<?
}

if (isset($_POST["excel"])) {
    
                    require("includes/dbconnect.php");
                    $comp = mysqli_query($conn, "SELECT * FROM `company`") or die(mysql_errno());
                    $zcomp = mysqli_fetch_assoc($comp);
                    $contents .= "Total - $getno \n";
                    
                    $contents .= "Sr., Name, Size, Unit, Group, Order For Invoice, Name Print, Name for Customer Invoice, Name for Party Invoice, Tac %, HSN CODE \n";
                            
                        $ctr = 1;
                        while ($pmain = mysqli_fetch_array($getpmain)) {
                            mysqli_select_db($conn, $zdb);

                            $getgroup1 = mysqli_query($conn, "SELECT  *  FROM `itemgrp` where code = '$pmain[itemgrp]'") or die(mysqli_error($conn));
                            $grop = mysqli_fetch_array($getgroup1);
                            $groupname = $grop['name'];

                            $contents .= "$ctr" . ",";
                            $contents .= "$pmain[name]" . ",";
                            $contents .= "$pmain[size]" . ",";
                            $contents .= "$pmain[unit]" . ",";
                            $contents .= "$groupname" . ",";
                            $contents .= "$pmain[levelno]" . ",";
                            $contents .= "$pmain[nameprin]" . ",";
                            $contents .= "$pmain[namecstmprin]" . ",";
                            $contents .= "$pmain[nameprtyprin]" . ",";
                            $contents .= "$pmain[taxperc]" . ",";
                            $contents .= "$pmain[hsncode]" . "\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;
    }
}
?>