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

$maindb = "dammain";
$trandb = "damtran";
$formname = "Damage Register";
$file = "damreg_fr.php";

$zroot = 'rajcomputers_msp_root';

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

    $itemname = $_POST["itemname"];
    $dtype = $_POST["dtype"];

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

    if (!empty($itemname)) {
        $itemcond = " AND b.`itemcode`='$itemname' ";
    }

    if (empty($_POST["fdate"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; purreg.php");
    }
    if (empty($_POST["tdate"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; purreg.php");
    }
    $getpmain = mysqli_query($conn, "SELECT a.*, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt,b.trqty,b.rate,b.amount,b.itemcode,b.size 
                                     FROM `$maindb` a, `$trandb` b WHERE a.vouchno = b.vouchno 
                                     AND a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` <= '$mtodate' $itemcond  ") or die(mysqli_error($conn));

    $get = mysqli_num_rows($getpmain);

    $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;
        }@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-bordered">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:5%"> Sr</th>
                                <th style="width:6%"> Voucher </th>
                                <th style="width:7%"> Date</th>
                                <th style="width:10%"> Itemname</th>
                                <th style="width:10%"> Size</th>
                                <th style="width:10%;" class="text-right">Qty</th>
                                <th style="width:10%;" class="text-right">Rate</th>
                                <th style="width:10%;" class="text-right">Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $mtqty = 0;
                            $mgtot = 0;

                            while ($pmain = mysqli_fetch_array($getpmain)) {


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

                                $ctr++;
                                echo "<tr id='boldfont'>";
                                echo "<td>$ctr</td>";
                                echo "<td>$pmain[vouchno]</td>";
                                echo "<td>$pmain[mydt]</td>";
                                echo "<td>$item[name]</td>";
                                echo "<td>$pmain[size]</td>";
                                echo "<td class='text-right' >$pmain[trqty]</td>";
                                echo "<td class='text-right'>$pmain[rate]</td>";
                                echo "<td class='text-right'>$pmain[amount]</td>";

                                $mtqty = $pmain['trqty'] + $mtqty;
                                $mgtot = $pmain['amount'] + $mgtot;

                                echo "</tr>";
                            }

                            echo "<tr> ";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<th class='text-right' style='color: red;'>Grand Total</td>";
                            echo "<th class='text-right' style='color: red;'>$mtqty</td>";
                            echo "<td></td>";
                            echo "<th class='text-right' style='color: red;'>$mgtot</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, Size, Qty, Rate, Amount\n";

                            $ctr = 0;
                            $mtqty = 0;
                            $mgtot = 0;

                            while ($pmain = mysqli_fetch_array($getpmain)) {
                                $getitem  = mysqli_query($conn, "SELECT `code`,`name` FROM `item` WHERE `code`='$pmain[itemcode]'") or die(mysqli_error($conn));
                                $item     = mysqli_fetch_array($getitem);

                                $ctr++;

                                $contents .="$ctr" . ",";
                                $contents .="$pmain[vouchno]" . ",";
                                $contents .="$pmain[mydt]" . ",";
                                $contents .="$item[name]" . ",";
                                $contents .="$pmain[size]" . ",";
                                $contents .="$pmain[trqty]" . ",";
                                $contents .="$pmain[rate]" . ",";
                                $contents .="$pmain[amount]" . "\n";

                                $mtqty = $pmain['trqty'] + $mtqty;
                                $mgtot = $pmain['amount'] + $mgtot;
                            }

                            $contents .="" . ",,,,";
                            $contents .="Grand Total" . ",";
                            $contents .="$mtqty" . ",,";
                            $contents .="$mgtot" . "\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;
    }
}

?>