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


//browse("$_POST[dtype]");
$cond = "";
$ccond = "";
$cocond = "";
$stcond = "";

if (!empty($_POST["rawmatname"])) {
    $pname = trim($_POST["rawmatname"]);
    $cond = "AND `name` LIKE '%$pname%'";
}
//
if (!empty($_POST["type"])) {
    $itgrp = trim($_POST["type"]);
    $cocond = "AND `type` = '$itgrp'";
}
//    if (!empty($_POST["company"])) {
//        $comp = trim($_POST["company"]);
//        $stcond = "AND `compcode` = '$comp'";
//    }

$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);
if (isset($_POST["view"])) {

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

    </head>

    <body>
        <div class="container">
            <div class="col-lg-2"></div>
            <div class="col-lg-8">
                <?php include("includes/head_rep.php");
                ?>
                <div class="row">
                    <div class="pad">
                        <table class="table table-condensed table-bordered">
                            <tr class="mySearchTr">
                                <th>Sr</th>
                                <th>Name</th>
                                <!--<th>Group</th>-->
                                <th>Type</th>
                                <!--<th>Unit</th>-->
                                <!--<th>HSN Code</th>-->
                                <th style="text-align: right">Rate</th>
                                <!--<th style="text-align: right">GST%</th>-->
                            </tr>
                            <tbody>
                                <?php
                                $ctr = 1;
                                while ($pmain = mysqli_fetch_array($get)) {

                                    if ($pmain["type"] == "A") {
                                        $type = "ACCESSORY";
                                    } elseif ($pmain["type"] == "F") {
                                        $type = "FABRIC";
                                    } else {
                                        $type = "OTHER";
                                    }

                                    //                                $rawmatgrp = mysqli_query($conn, "SELECT * FROM rawmatgrp WHERE code='$pmain[rawmatgrp]' ")or die(mysqli_error($conn));
                                    //                                $getit = mysqli_fetch_array($rawmatgrp);


                                    echo "<tr>";
                                    echo "<td>$ctr</td>";
                                    //                                Echo "<td>$pmain[code]</td>";
                                    echo "<td>$pmain[name]</td>";
                                    //                                Echo "<td> $getco[rawmatgrp]</td>";
                                    echo "<td> $type</td>";
                                    //                                Echo "<td>$pmain[unit]</td>";
                                    //                                Echo "<td>$pmain[hsncode]</td>";
                                    echo "<td align='right'>$pmain[rate]</td>";
                                    //                                Echo "<td align='right'>$pmain[gstperc]</td>";
                                    echo "</tr>";
                                    $ctr++;
                                }
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        </div>

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

    </html>
<?php
}

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

    $contents = "Sr,Name,Type,Rate\n";

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

        if ($pmain["type"] == "A") {
            $type = "ACCESSORY";
        } elseif ($pmain["type"] == "F") {
            $type = "FABRIC";
        } else {
            $type = "OTHER";
        }

        $contents .= "$ctr,$pmain[name],$type,$pmain[rate]\n";
        $ctr++;
    }

    $contents = strip_tags($contents);

    header("Content-Disposition: attachment; filename=$formname" . "_Detail" . date('d-m-Y') . ".csv");
    print $contents;
}
?>