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

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

$maindb = 'packing';
$grid = 'packing_print.php';
$formname = 'Packing Material Listing';

if (isset($_POST["view"])) {

    $cond = "";

    if (!empty($_POST["itemname"])) {
        $pname = trim($_POST["itemname"]);
        $cond = "AND `name` LIKE '%$pname%'";
    }

    $get = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `name`!='' $cond  ORDER by `name`") or die(mysqli_error($conn));

    $getno = mysqli_num_rows($get);

    include("includes/linkshead.php");
    ?>

    </head>
    <body>
        <div class="container">
            <?php include('includes/head_rep.php') ?>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table class="table table-condensed table-bordered ">
                        <tr class="mySearchTr">
                            <th>Sr</th>
                            <th>Packing Material Name</th>
                            <th>Hsncode</th>
                            <th style="text-align: right">GST %</th>
                            <th>Unit</th>
                            <th style="text-align: right">Cost Per Unit</th>
                        </tr>
                        <tbody>
                            <?php
                            $ctr = 1;
                            while ($pmain = mysqli_fetch_array($get)) {

                                $name = $pmain['name'];
                                $hsncode = $pmain['hsncode'];
                                $gstperc = $pmain['gstperc'];
                                $unit = $pmain['unit'];
                                $cost = $pmain['cost'];
                                
                                Echo "<tr>";
                                Echo "<td>$ctr</td>";
                                Echo "<td>$name</td>";
                                Echo "<td>$hsncode</td>";
                                Echo "<td class='text-right'>$gstperc%</td>";
                                Echo "<td>$unit</td>";
                                Echo "<td align='right'>$cost</td>";
                                echo "</tr>";
                                $ctr++;
                            }
                            ?>
                        </tbody>    
                    </table>
                </div>
            </div>
        </div>
    </div>

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

    $cond = "";

    if (!empty($_POST["itemname"])) {
        $pname = trim($_POST["itemname"]);
        $cond = "AND `name` LIKE '%$pname%'";
    }

    $get = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `name`!='' $cond $cocond $stcond ORDER by `name`") or die(mysqli_error($conn));
    $getno = mysqli_num_rows($get);



    $contents .= "Sr.,Packing Material Name,Hsncode,GST %,Unit,Cost Per Unit\n";

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


        $contents .= $ctr . ",";
        $contents .= $pmain['name'] . ",";
        $contents .= $pmain['hsncode'] . ",";
        $contents .= $pmain['gstperc'] . '%' . ",";
        $contents .= $pmain['unit'] . ",";
        $contents .= $pmain['cost'] . "\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;
    }
}
?>