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

$maindb = "product";
$formname = "Product List";
$file = "product_print.php";

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

$item = $_POST[item];
$itemgrp = $_POST[itemgrp];

$itemcond = '';
$itmgrpcond = '';

if (!empty($item)) {
    $itemcond = " AND `prodcode`='$item' ";
}

if (!empty($itemgrp)) {
    $itmgrpcond = " AND `code`='$itemgrp' ";
}



$getpmain = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE code !='' $itemcond $itmgrpcond ")or die(mysqli_error($conn));
if (isset($_POST["view"])) {

    include("includes/linkshead.php");
    ?>  
    </head>
    <body>
        <div class="container table-responsive" style="width: 60%;">
            <?php include("includes/head_rep.php"); ?>            
            <div class="row">
                <div class="col-lg-12 pad">
                    <table class="table table-condensed table-bordered table-hover">
                        <tr class="mySearchTr">
                            <th>Sr</th>
                            <th>Product Code & Name</th>
                            <th>Group Name</th>
                            <th>Cost</th>
                            <th>Rate</th>
                            <th>Min.Stock</th>
                        </tr>
                        <?php
                        $ctr = 1;
                        while ($pmain = mysqli_fetch_array($getpmain)) {
                            $getprod= mysqli_query($conn, "SELECT name FROM `prodgrp` WHERE code = $pmain[prodgrp]")or die(mysqli_error($conn));
                            $prodgrp= mysqli_fetch_array($getprod);
                            Echo "<tr>";
                            Echo "<td>$ctr</td>";
                            Echo "<td>$pmain[prodcode] $pmain[name]</td>";
                            Echo "<td>$prodgrp[name]</td>";  
                            Echo "<td>$pmain[cost]</td>";
                            Echo "<td>$pmain[rate]</td>";
                            Echo "<td>$pmain[minstock]</td>";
                            Echo "</tr>";
                            $ctr++;
                            }
//                        }
                        ?>
                    </table>
                </div>
            </div>
        </div>

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

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

    
                             $contents .= "Sr,Product Code & Name,Group Name,Cost,Rate,Min.Stock\n";
                          
                        $ctr = 1;
                        while ($pmain = mysqli_fetch_array($getpmain)) {
                            $getprod= mysqli_query($conn, "SELECT name FROM `prodgrp` WHERE code = $pmain[prodgrp]")or die(mysqli_error($conn));
                            $prodgrp= mysqli_fetch_array($getprod);
                            
                            $contents .= "$ctr" . ",";
                            $contents .= "$pmain[prodcode] $pmain[name]" . ",";
                            $contents .= "$prodgrp[name]" . ",";
                            $contents .= "$pmain[cost]" . ",";
                            $contents .= "$pmain[rate]" . ",";
                            $contents .= "$pmain[minstock]" . "\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;
    }
}
?>
