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

$maindb     = "inwmainraw";
$trandb     = "inwtranraw";
$formname   = "Inward Register - Raw Material";
$file       = "inwregraw_fr.php";


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

if (isset($_POST["view"])) {
    $itemcode   = $_POST["itemcode"];
    $itemname   = $_POST["itemname"];
    $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)) {
//        $itemnamecond = " AND `itemname `='$itemname ' ";
//    }

    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");
    }

    $getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
                            AND `vouchdt` <= '$mtodate'  ")or die(mysqli_error($conn));
    $get = mysqli_num_rows($getpmain);
    include("includes/linkshead.php");
        $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
        .dtype{
            background-color: #cccccc;
        }
    </style>

    </head>
    <body>
        <div class="container">
             <?php include("includes/head_rep.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:5%">  Sr</th>
                                <th style="width:10%"> Voucher No</th>
                                <th style="width:10%"> Date</th>
                                <th style="width:10%;" class="text-right">Qty</th>
                                <th style="width:15%"  class="text-right">Total</th>
                                <th style="width:50%; padding-left:20px;"  class="">Remark</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $mtqty = 0;
                            $mgtot = 0;

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

                                $ctr++;
                                $url = "inwardraw.php?vouchno=$pmain[vouchno]&mode=edit";
                                Echo "<tr id='boldfont' class='clickable-row2' data-href='$url' data-target='_blank'>";
                                Echo "<td>$ctr</td>";
                                Echo "<td>$pmain[vouchno]</td>";
                                Echo "<td>$pmain[mydt]</td>";
                                Echo "<td class='text-right' >$pmain[qty]</td>";
                                Echo "<td class='text-right'>$pmain[total]</td>";
                                Echo "<td style='padding-left:20px;'>$pmain[remark] &nbsp;   &nbsp;  $pmain[remark2]</td>";
                                $mtqty = $pmain['qty'] + $mtqty;
                                $mgtot = $pmain['total'] + $mgtot;

                                Echo "</tr>";

//                                $tottopay = $tottopay + $pmain[total];
//                                $paidtot = $paidtot + $pmain[totpaid];
//                                $totnet = $totnet + $pmain[finalamt];
//                                if ($_POST["dtype"] == 2) {

//                                if ($_POST["dtype"] == 2) {

                                    Echo "<tr style='height:10px;'>";
                                    Echo "<td colspan='25'>";
                                    Echo "<table style='width:100%;' id='tabital' class='table table-condensed table-hover table-bordered'>";
                                    Echo "<th width='5%;'class='dtype' style='background-color:'>Sr</th>";
//                                    Echo "<th width='5%' class='dtype''>Barcode</th>";
                                    Echo "<th width='20%' class='dtype'>Itemname</th>";
                                    Echo "<th width='10%' class='dtype'>Color</th>";
                                    Echo "<th width='6%' class='numeric dtype'>Roll</th>";
                                    Echo "<th width='6%' class='numeric dtype'>Yard</th>";
                                    Echo "<th width='6%' class='numeric dtype'>Qty</th>";
                                    Echo "<th width='6%' class='numeric dtype'>Rate</th>";
                                    Echo "<th width='6%' class='numeric dtype'>Amount</th>";
                                    Echo "<tr>";

                                    $ctr2 = 0;
                                    $totqty = $totmtrs = $totyard = $totdisc = $totdiscamt = $totcgst = $totsgst = $totigst = $totamt = 0;


                                    $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]'")or die(mysqli_error($conn));
                                    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);

                                        $ctr2++;
                                        Echo "<tr>";
                                        Echo "<td>$ctr2</td>";
//                                        Echo "<td>$tran[barcode]</td>";
                                        Echo "<td>$item[name]</td>";
                                        Echo "<td>$color[name]</td>";
                                        Echo "<td class='numeric'>$tran[meters]</td>";
                                        Echo "<td class='numeric'>$tran[yard]</td>";
                                        Echo "<td class='numeric'>$tran[qty]</td>";
                                        Echo "<td class='numeric'>$tran[rate]</td>";
                                        Echo "<td class='numeric'>$tran[amount]</td>";
                                        Echo "</tr>";

                                        $totamt = $totamt + $tran['amount'];
                                        $tottaxable = $tottaxable + $tran['subtot'];
                                        $totqty = $totqty + $tran['qty'];
                                        $totyard = $totyard + $tran['yard'];
                                        $totmtrs = $totmtrs + $tran['meters'];
                                    }
                                    echo "<tr> ";
                                    $colr1 = "blue";

                                    echo "<td style='color:$colr1'</td>";
                                    echo "<td style='color:$colr1'</td>";
//                                    echo "<td style='color:$colr1'</td>";
                                    echo "<td style='color:$colr1' id='font1' class='text-right'><h5><b>Total</b></h5></td>";
                                    echo "<td style='color:$colr1' id='font1' class='text-right'><h5><b>$totmtrs</b></h5></td>";
                                    echo "<td style='color:$colr1' id='font1' class='text-right'><h5><b>$totyard</b></h5></td>";
                                    echo "<td style='color:$colr1' id='font1' class='text-right'><h5><b>$totqty</b></h5></td>";
//                                    echo "<td style='color:$colr1' id='font1'></td>";
//                                    echo "<td style='color:$colr1' id='font1'></td>";
                                    echo "<td style='color:$colr1' id='font1'></td>";
                                    echo "<td style='color:$colr1' id='font1' class='numeric'><h5><b>$totamt</b></h5></td>";
                                    echo "</tr>";

                                    Echo "</table>";
                                    Echo "</td>";
//                                }
//                                }
//                                if ($_POST["dtype"] == 2) {
//                                    echo "<tr> ";
//                                    $colr1 = "blue";
//                                    echo "<td style='color:$colr1;' align='right' id='font1' colspan='6' ><h5><b>Total :</b></h5></td>";
//                                    echo "<td style='color:$colr1;' align='right' id='font1'><h5><b>$topaytot1</b></h5></td>";
//                                    echo "<td style='color:$colr1;' align='center' id='font1'><h5><b>$totweight</b></h5></td>";
//                                    echo "<td style='color:$colr1;'  id='font1'><h5><b>$totpaid</b></h5></td>";
//                                    echo "</tr>";
//                                }
                            }

                            echo "<tr> ";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></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'>Grand Total    &nbsp;&nbsp;&nbsp;  $mgtot</td>";
                            
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

        <?php include("includes/linksfoot.php");
        ?>
    </body>
    </html>
    <?php
}
if (isset($_POST["excel"])) {
    $itemcode = $_POST["itemcode"];
    $itemname = $_POST["itemname"];
    $fdate = $_POST["fdate"];
    $tdate = $_POST["tdate"];

    $dtypecond = "";

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




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

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

    $getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
                            AND `vouchdt` <= '$mtodate' ")or die(mysqli_error($conn));
    $get = mysqli_num_rows($getpmain);

    $contents.="Sr.,Voucher no.,Date,Qty,Total,Remark\n";

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

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

        $ctr++;
        $pvouchno = $pmain['vouchno'];
        $pdate = $pmain['vouchdt'];
        $pqty = $pmain['qty'];
        $ptotal = $pmain['total'];
        $premark = $pmain['remark'] . $pmain['remark2'];

        $contents.=$ctr . ",";
        $contents.=$pvouchno . ",";
        $contents.=$pdate . ",";
        $contents.=$pqty . ",";
        $contents.=$ptotal . ",";
        $contents.=$premark . "\n";

        $mtqty = $pmain['qty'] + $mtqty;
        $mgtot = $pmain['total'] + $mgtot;

        $contents.="Sr.,Barcode,Itemname,Qty,Unit,Mrp,Rate,Amount,Msp\n";

        $ctr2 = 0;
        $tqty = 0;
        $tamt = 0;

        $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]' $itemcond $itemnamecond  ")or die(mysqli_error($conn));
        while ($tran = mysqli_fetch_array($gettran)) {

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

            $ctr2++;
            $pitemcode = $tran['itemcode'];
            $pitemname = $item['name'];
            $ptrqty = $tran['trqty'];
            $punit = $tran['unit'];
            $pmrp = $tran['mrp'];
            $prate = $tran['rate'];
            $pamount = $tran['amount'];
            $pmsp = $tran['msp'];

            $contents.=$ctr2 . ",";
            $contents.=$pitemcode . ",";
            $contents.=$pitemname . ",";
            $contents.=$ptrqty . ",";
            $contents.=$punit . ",";
            $contents.=$pmrp . ",";
            $contents.=$prate . ",";
            $contents.=$pamount . ",";
            $contents.=$pmsp . "\n";
        }
        $tqty = $tran['trqty'] + $tqty;
        $tamt = $tran['amount'] + $tamt;

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