<?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">Act.</th>
                                <th style="text-align: right">Cost</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[act_cost]</td>";
                                    Echo "<td align='right'>$pmain[cost]</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, Raw Material Name, Type, Rate\n";

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

        $ctr++;
        $pname = $pmain['name'];
        $hsncode = $pmain['hsncode'];
        $rate = $pmain['rate'];
        $gstperc = $pmain['gstperc'];


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

        $contents .= $ctr . ",";
        $contents .= $pname . ",";
        $contents .= $type . ",";
//        $contents .= $hsncode . ",";
        $contents .= $rate . "\n";
//        $contents .= $gstperc . "\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;
    }
}
?>