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

$maindb = "outmainraw";
$trandb = "outtranraw";
$formname = "Outard Register - Raw Material";
$file = "outregraw_fr.php";


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

$itemcode = $_POST["itemcode"];
$itemname = $_POST["itemname"];
$color = $_POST["color"];
$fdate = $_POST["fdate"];
$tdate = $_POST["tdate"];

$dtypecond = "";
$mscopecond = "";

$mfrmdate = trim($_POST["fdate"]);
$mtodate = trim($_POST["tdate"]);

if (!empty($itemcode)) {
    $itemcond = " AND `itemcode`='$itemcode' ";
}
if (!empty($itemname)) {
    $namecond = " AND `item`='$itemname' ";
}
if (!empty($color)) {
    $colorcond = " AND `color`='$color' ";
}

if (empty($_POST["fdate"])) {
    browse("From Date Cannot Be Empty");
    header("refresh:1; inwregraw.php");
}
if (empty($_POST["tdate"])) {
    browse("To Date Cannot Be Empty");
    header("refresh:1; inwregraw.php");
}

$gettran = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`,'%d-%m-%Y') as mydt FROM `$trandb` WHERE `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate'
                                           $namecond $colorcond order by vouchno ") or die(mysqli_error($conn));

$get = mysqli_num_rows($gettran);
$mfdt = date("d-m-Y", strtotime($mfrmdate));
$mtdt = date("d-m-Y", strtotime($mtodate));
if (isset($_POST["view"])) {
    include("includes/linkshead.php");
?>
    <style type="text/css">
        #tabital {
            font-style: italic;
        }

        #font1 {
            font-style: italic;
        }

        #boldfont {
            font-weight: bold;
        }

        .dtype {
            background-color: #cccccc;
        }@media print {
            
            .table-bordered>thead>tr>th{
                border: 1px solid black !important;
            }
            .table-bordered>tbody>tr>td{
                border: 1px solid black !important;
            }
            .table-bordered>tbody>tr>th{
                border: 1px solid black !important;
            }
        }
    </style>

    </head>

    <body>
        <div class="container">
            <?php include("includes/head_rep_kaccha.php"); ?>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table style="background: white;width: 100%" class="table table-condensed table-hover">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:4%"> Sr</th>
                                <th style="width:8%"> Voucher</th>
                                <th style="width:8%"> Date</th>
                                <th style="width:23%"> Itemname</th>
                                <th style="width:10%"> Color</th>
                                <th style="width:10%"> Lot</th>
                                <th style="width:10%;text-align:right"> Qty</th>
                                <th style="width:10%;text-align:right"> Rate</th>
                                <th style="width:10%;text-align:right"> Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $totamt = 0;
                            $totqty = 0;


                            $ctr2 = 0;

                            while ($tran = mysqli_fetch_array($gettran)) {

                                $getitem = mysqli_query($conn, "SELECT `code`,`name` FROM `rawmat` WHERE `code`='$tran[item]'") or die(mysqli_error($conn));
                                $item = mysqli_fetch_array($getitem);

                                $getcolor = mysqli_query($conn, "SELECT `code`,`name` FROM `color` WHERE `code`='$tran[color]'") or die(mysqli_error($conn));
                                $color = mysqli_fetch_array($getcolor);
                                $ctr++;
                                $ctr2++;
                                echo "<tr id='boldfont'>";
                                echo "<td>$ctr2</td>";
                                echo "<td>$tran[vouchno]</td>";
                                echo "<td>$tran[mydt]</td>";
                                echo "<td>$item[name]</td>";
                                echo "<td>$color[name]</td>";
                                echo "<td>$tran[lot]</td>";
                                echo "<td class='text-right'>$tran[qty]</td>";
                                echo "<td class='text-right'>$tran[rate]</td>";
                                echo "<td class='text-right'>$tran[amount]</td>";
                                echo "</tr>";

                                $totamt = $totamt + $tran['amount'];
                                $totqty = $totqty + $tran['qty'];
                            }

                            echo "<tr> ";
                            echo "<td colspan='5' class='text-right'  style='color: red;font-weight:bold;font-size:18px'>Grand Total : -</td>";
                            echo "<td  class='text-right'  style='color: red;font-weight:bold;font-size:18px'></td>";
                            echo "<td  class='text-right'  style='color: red;font-weight:bold;font-size:18px'>$totqty</td>";
                            echo "<td  class='text-right'  style='color: red;font-weight:bold;font-size:18px'></td>";
                            echo "<td colspan='2' class='text-right' style='color: red;font-weight:bold;font-size:18px'> $totamt</td>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

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

    </html>
<?php
}
if (isset($_POST["excel"])) {

    include("includes/head_excel_rep.php");

    $contents .= "Sr, Voucher, Date, Itemname, Color, Lot, Qty, Rate, Amount\n";
    $ctr = 0;
    $totamt = 0;
    $totqty = 0;

    $ctr2 = 0;

    while ($tran = mysqli_fetch_array($gettran)) {

        $getitem = mysqli_query($conn, "SELECT `code`,`name` FROM `rawmat` WHERE `code`='$tran[item]'") or die(mysqli_error($conn));
        $item = mysqli_fetch_array($getitem);

        $getcolor = mysqli_query($conn, "SELECT `code`,`name` FROM `color` WHERE `code`='$tran[color]'") or die(mysqli_error($conn));
        $color = mysqli_fetch_array($getcolor);
        $ctr++;
        $ctr2++;

        $contents .= "$ctr2" . ",";
        $contents .= "$tran[vouchno]" . ",";
        $contents .= "$tran[mydt]" . ",";
        $contents .= "$item[name]" . ",";
        $contents .= "$color[name]" . ",";
        $contents .= "$tran[lot]" . ",";
        $contents .= "$tran[qty]" . ",";
        $contents .= "$tran[rate]" . ",";
        $contents .= "$tran[amount]" . "\n";

        $totamt = $totamt + $tran['amount'];
        $totqty = $totqty + $tran['qty'];
    }

    $contents .= "" . ",,,,";
    $contents .= "Grand Total" . ",,";
    $contents .= "$totqty" . ",,";
    $contents .= "$totamt" . "\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;
    }
}
?>