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

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

$maindb = 'production';
$trandb = 'prodraw';
$trandb2 = 'prodrel';

$formname = 'Purchase Return Register';
$zroot = $_SESSION["zdir"];
$file = 'prodreg_fr.php';

if (isset($_POST["view"])) {
    $product = $_POST["product"];
    $godown = $_POST["godown"];
    $checkedby = $_POST["checkedby"];
    $weightby = $_POST["weightby"];
    $dtype = $_POST["dtype"];

    if (!empty($product)) {
        $prodcond = " AND `product` = '$product' ";
    }
    if (!empty($godown)) {
        $godowncond = " AND `godown` = '$godown' ";
    }
    if (!empty(checkedby)) {
        $chkbycond = " AND `checkedby` = '$checkedby '";
    }
    if (!empty(weightby)) {
        $weightcond = " AND `weightby` = '$weightby '";
    }

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

    $mfrmdate = trim($_POST["frmdt"]);
    $mtodate = trim($_POST["todt"]);

    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));


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

    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
        .colr{
            background: #26A69A;
            font-size: 17px;
        }
        .dth{
            background:#BABFC2;
        }
    </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-bordered">
                            <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:25%;">Product</th>
                                    <th style="width:15%;">Godown</th>
                                    <th style="width:15%;">Checked By</th>
                                    <th style="width:15%;">Weight By</th>
                                    <th style="width:10%;" class="text-right">Total Qty</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $ctr = 0;
                                $totamt = 0;

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

                                    $getfinprod = mysqli_query($conn, "SELECT `code`,`name` FROM `finprod` WHERE `code`= '$pmain[product]'") or die(mysqli_error($conn));
                                    $finprod = mysqli_fetch_array($getfinprod);

                                    $getgodown = mysqli_query($conn, "SELECT * FROM `godown` WHERE `code`= '$pmain[godown]'") or die(mysqli_error($conn));
                                    $getgd = mysqli_fetch_array($getgodown);

                                    $getchk = mysqli_query($conn, "SELECT accode,name FROM `accmast` where `grpcode`='040' and `accode`='$pmain[checkedby]' ")or die(mysqli_error($conn));
                                    $mchk = mysqli_fetch_array($getchk);

                                    $getwei = mysqli_query($conn, "SELECT accode,name FROM `accmast` where `grpcode`='040' and `accode`='$pmain[weightby]' ")or die(mysqli_error($conn));
                                    $mwei = mysqli_fetch_array($getwei);

                                    $ctr++;
                                    Echo"<tr>";
                                    Echo"<td>$ctr</td>";
                                    Echo"<td>$pmain[voucher]</td>";
                                    Echo"<td>$pmain[mydt]</td>";
                                    Echo"<td>$finprod[name]</td>";
                                    Echo"<td>$getgd[gcode]</td>";
                                    Echo"<td>$mchk[name]</td>";
                                    Echo"<td>$mwei[name]</td>";
                                    Echo"<td class='text-right'>$pmain[totactpcs]</td>";
                                    Echo"</tr>";

                                    $totamt = $totamt + $pmain[totactpcs];

                                    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-bordered'>";
                                        Echo "<th style='width:5%;' class='dth'>Sr</th>";
                                        Echo "<th style='width:8%;' class='dth'>Product Code</th>";
                                        Echo "<th style='width:29%;' class='dth'>Name</th>";
                                        Echo "<th style='width:5%;' class='dth'>Net Wt.</th>";
                                        Echo "<th style='width:8%;' class='text-right dth'>Approx. Pcs.</th>";
                                        Echo "<th style='width:10%;' class='text-right dth'>Consumption in Kg.</th>";
                                        Echo "<th style='width:5%;' class='text-right dth'>Actual pcs.</th>";
                                        Echo "<th style='width:10%;' class='text-right dth'>Approved By</th>";
                                        Echo "</tr>";


                                        $gettran = mysqli_query($conn, "SELECT * FROM `$trandb2` WHERE `vouchno`='$pmain[vouchno]'")or die(mysqli_error($conn));
                                        $ctr2 = 0;
                                        $totapprox = 0;
                                        $totcon = 0;
                                        $totact = 0;
                                        
                                        while ($tran = mysqli_fetch_array($gettran)) {
                                            
                                          $gapprov = mysqli_query($conn, "SELECT accode,name FROM `accmast` where `grpcode`='040' order by name")or die(mysqli_error($conn));
                                          $fapprov = mysqli_fetch_array($gapprov);  

                                            $ctr2++;
                                            Echo "<tr>";
                                            Echo "<td>$ctr2</td>";
                                            Echo "<td>$tran[prodcode]</td>";
                                            Echo "<td>$tran[prodname]</td>";
                                            Echo "<td>$tran[netwt]</td>";
                                            Echo "<td class='text-right'>$tran[approxpcs]</td>";
                                            Echo "<td class='text-right'>$tran[conskg]</td>";
                                            Echo "<td class='text-right'>$tran[actpcs]</td>";
                                            Echo "<td class='text-right'>$fapprov[name]</td>";
                                            Echo "</tr>";

                                            $totapprox = $totapprox + $tran[approxpcs];
                                            $totcon = $totcon + $tran[conskg];
                                            $totact = $totact + $tran[actpcs];
                                            
                                            
                                        }
                                        Echo "<tr>";
                                        Echo "<td class='dth'></td>";
                                        Echo "<td class='dth'></td>";
                                        Echo "<td class='text-right dth'>Total</td>";
                                        Echo "<td class='text-right dth'></td>";
                                        Echo "<td class='text-right dth'>$totapprox</td>";
                                        Echo "<td class='text-right dth'>$totcon</td>";
                                        Echo "<td class='text-right dth'>$totact</td>";
                                        Echo "<td class='text-right dth'></td>";
                                        Echo "</tr>";

                                        Echo "</table>";
                                        Echo "</td>";
                                    }
                                }

                                Echo "<tr>";
                                Echo "<td class='colr'></td>";
                                Echo "<td class='colr'></td>";
                                Echo "<td class='colr'></td>";
                                Echo "<td class='colr'></td>";
                                Echo "<td class='colr'></td>";
                                Echo "<td class='colr'></td>";
                                Echo "<td class='text-right colr'><b>Total</b></td>";
                                Echo "<td class='text-right colr'><b>$totamt</b></td>";
                                Echo "</tr>";
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            <?php include("includes/linksfoot.php");
            ?>
    </body>
    </html>
    <?php
}

if (isset($_POST["excel"])) {
    $product = $_POST["product"];
    $godown = $_POST["godown"];
    $checkedby = $_POST["checkedby"];
    $weightby = $_POST["weightby"];
    $dtype = $_POST["dtype"];

    if (!empty($product)) {
        $prodcond = " AND `product` = '$product' ";
    }
    if (!empty($godown)) {
        $godowncond = " AND `godown` = '$godown' ";
    }
    if (!empty(checkedby)) {
        $chkbycond = " AND `checkedby` = '$checkedby '";
    }
    if (!empty(weightby)) {
        $weightcond = " AND `weightby` = '$weightby '";
    }

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

    $mfrmdate = trim($_POST["frmdt"]);
    $mtodate = trim($_POST["todt"]);

    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));


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

    $contents.="Sr, Voucher, Date, Product, Godown, Checked By, Weight By, Total Qty \n";
                       
                                $ctr = 0;
                                $totamt = 0;

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

                                    $getfinprod = mysqli_query($conn, "SELECT `code`,`name` FROM `finprod` WHERE `code`= '$pmain[product]'") or die(mysqli_error($conn));
                                    $finprod = mysqli_fetch_array($getfinprod);

                                    $getgodown = mysqli_query($conn, "SELECT * FROM `godown` WHERE `code`= '$pmain[godown]'") or die(mysqli_error($conn));
                                    $getgd = mysqli_fetch_array($getgodown);

                                    $getchk = mysqli_query($conn, "SELECT accode,name FROM `accmast` where `grpcode`='040' and `accode`='$pmain[checkedby]' ")or die(mysqli_error($conn));
                                    $mchk = mysqli_fetch_array($getchk);

                                    $getwei = mysqli_query($conn, "SELECT accode,name FROM `accmast` where `grpcode`='040' and `accode`='$pmain[weightby]' ")or die(mysqli_error($conn));
                                    $mwei = mysqli_fetch_array($getwei);

                                    $ctr++;
                                    $contents.= $ctr . ",";
                                    $contents.= $pmain[voucher] . ",";
                                    $contents.= $pmain[mydt] . ",";
                                    $contents.= $finprod[name] . ",";
                                    $contents.= $getgd[gcode] . ",";
                                    $contents.= $mchk[name] . ",";
                                    $contents.= $mwei[name] . ",";
                                    $contents.= $pmain[totactpcs] . "\n";

                                    $totamt = $totamt + $pmain[totactpcs];

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

                                        $contents.= "Sr,Product Code, Name, Net Wt., Approx. Pcs., Consumption in Kg., Actual pcs, Approved By \n";

                                        $gettran = mysqli_query($conn, "SELECT * FROM `$trandb2` WHERE `vouchno`='$pmain[vouchno]'")or die(mysqli_error($conn));
                                        $ctr2 = 0;
                                        $totapprox = 0;
                                        $totcon = 0;
                                        $totact = 0;
                                        
                                        while ($tran = mysqli_fetch_array($gettran)) {
                                            
                                          $gapprov = mysqli_query($conn, "SELECT accode,name FROM `accmast` where `grpcode`='040' order by name")or die(mysqli_error($conn));
                                          $fapprov = mysqli_fetch_array($gapprov);  

                                            $ctr2++;
                                            $contents.= $ctr2 . ",";
                                            $contents.= $tran[prodcode] . ",";
                                            $contents.= $tran[prodname] . ",";
                                            $contents.= $tran[netwt] . ",";
                                            $contents.= $tran[approxpcs] . ",";
                                            $contents.= $tran[conskg] . ",";
                                            $contents.= $tran[actpcs] . ",";
                                            $contents.= $fapprov[name] . "\n";

                                            $totapprox = $totapprox + $tran[approxpcs];
                                            $totcon = $totcon + $tran[conskg];
                                            $totact = $totact + $tran[actpcs];
                                            
                                            
                                        }
                                        $contents.= "" . ",";
                                        $contents.= "" . ",";
                                        $contents.= "Total" . ",";
                                        $contents.= "" . ",";
                                        $contents.= $totapprox . ",";
                                        $contents.= $totcon . ",";
                                        $contents.= $totact . ",";
                                        $contents.= "" . ",";
                                    }
                                }
                                
                                $contents.= "" . ",";
                                $contents.= "" . ",";
                                $contents.= "" . ",";
                                $contents.= "" . ",";
                                $contents.= "" . ",";
                                $contents.= "" . ",";
                                $contents.= "Total" . ",";
                                $contents.= $totamt . ",";
 

    $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;
    }
}
?>