<?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 = 'item';
$grid = 'item_print.php';
$formname = 'Raw Material Listing';

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

    $cond = "";
    $ccond = "";
    $cocond = "";
    $stcond = "";

    if (!empty($_POST["itemname"])) {
        $pname = trim($_POST["itemname"]);
        $cond = "AND `code` = '$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-bordered table-condensed">
                        <tr class="mySearchTr">
                            <th width="5%">Sr</th>
                            <th width="35%">Raw Material Name</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">Cost Per Unit</th>
                            <th width="10%" style="text-align: right">Cost Per Kg.</th>
                        </tr>
                        <tbody>
                            <?php
                            $ctr = 1;
                            while ($pmain = mysqli_fetch_array($get)) {

                                $code = $pmain['code'];
                                $name = $pmain['name'];
                                $hsncode = $pmain['hsncode'];
                                $gstperc = $pmain['gstperc'];
                                $unit = $pmain['unit'];
                                $cost = $pmain['cost'];
                                $rate = $pmain['rate'];
                                
                                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 "<td align='right'>$rate</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 `code` = '$pname'";
    }

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


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

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

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