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

$maindb = "item";
$formname = "Item Print";
$file = "item_print.php";

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

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

    $namecond = "";
    $gstcond = "";

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

    $getitem = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `name` !='' $namecond $gstcond ORDER BY NAME")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">
                <?php include("includes/head_rep.php"); ?>
                <div class="col-lg-12 pad">
                    <table class="table table-condensed table-bordered">
                        <tr class="mySearchTr">
                            <th width="5%">Sr</th>
                            <th width="20%">Item Name</th>
                            <th width="25%">Description</th>
                            <th width="10%">Hsncode</th>
                            <th width="10%" style="text-align:right">GST %</th>
                            <th width="10%">Unit</th>
                            <th width="10%" style="text-align: right">Rate</th>
                        </tr>
                        </tr>
                        <?php
                        $ctr = 1;
                        while ($pmain = mysqli_fetch_array($getitem)) {

                            $code = $pmain['code'];
                            $name = $pmain['name'];
                            $description = $pmain['description'];
                            $hsncode = $pmain['hsncode'];
                            $gstperc = $pmain['gstperc'];
                            $unit = $pmain['unit'];
                            $rate = $pmain['rate'];

                            Echo "<tr style='background-color:white'>";
                            Echo "<td>$ctr</td>";
                            Echo "<td>$name</td>";
                            Echo "<td>$description</td>";
                            Echo "<td>$hsncode</td>";
                            Echo "<td class='text-right'>$gstperc%</td>";
                            Echo "<td>$unit</td>";
                            Echo "<td align='right'>$rate</td>";
                            echo "</tr>";

                            $ctr++;
                        }
                        ?>
                    </table>
                </div>
            </div>
        </div>

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

if (isset($_POST["excel"])) {
    $name = $_POST['name'];
    $gstperc = $_POST['gstperc'];

    $namecond = "";
    $gstcond = "";

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

    $getitem = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `name` !='' $namecond $gstcond ORDER BY NAME")or die(mysqli_error($conn));
    $contents .="Sr,Item Name,Description,Hsncode,GST,Unit,Rate\n";
                            
                        $ctr = 1;
                        while ($pmain = mysqli_fetch_array($getitem)) {

                            $code = $pmain['code'];
                            $name = $pmain['name'];
                            $description = $pmain['description'];
                            $hsncode = $pmain['hsncode'];
                            $gstperc = $pmain['gstperc'];
                            $unit = $pmain['unit'];
                            $rate = $pmain['rate'];

                            $contents .="$ctr".",";
                            $contents .="$name".",";
                            $contents .="$description".",";
                            $contents .="$hsncode".",";
                            $contents .="$gstperc".",";
                            $contents .="$unit".",";
                            $contents .="$rate"."\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;
    }
}
                        



//if (isset($_POST["excel"])) {
//    $name = $_POST['name'];
//    $gstperc = $_POST['gstperc'];
//
//    $namecond = "";
//    $gstcond = "";
//
//    if (!empty($name)) {
//        $namecond = " AND `code`='$name' ";
//    }
//    if (!empty($gstperc)) {
//        $gstcond = " AND `gstperc` LIKE '%$gstperc%' ";
//    }
//
//    $getitem = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `name` !='' $namecond $gstcond ORDER BY NAME")or die(mysqli_error($conn));
//
//    $contents.="Sr, ItemName, HsnCode, GST%, Unit, CostPerUnit, CostPerKg\n";
//
//    $ctr = 0;
//    while ($pmain = mysqli_fetch_array($getitem)) {
//        $ctr++;
//        
//        $contents.=$ctr . ",";
//        $contents.=$pmain[name] . ",";
//        $contents.=$pmain[hsncode] . ",";
//        $contents.=$pmain[gstperc] . ",";
//        $contents.=$pmain[unit] . ",";
//        $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;
//    }
//}
?>
