<?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 = 'prdmain';
$trandb = 'prdtran';
$zroot = $_SESSION["zdbroot"];

$formname = 'Pump Meter Register';
$zroot = $_SESSION["zdir"];
$file = 'preadreg_fr.php';

$shift = $_POST["shift"];

if (!empty($shift)) {
    $shiftcond = " AND `shift`='$shift' ";
}

$mfromdate = trim($_POST["frmdate"]);
$mtodate = trim($_POST["todate"]);

if (empty($mfromdate)) {
    browse("From Date Cannot Be Empty");
    echo"<script>window.close();</script>";
}
if (empty($mtodate)) {
    browse("To Date Cannot Be Empty");
    echo"<script>window.close();</script>";
}
$mfdt = date("d-m-Y", strtotime($mfromdate));
$mtdt = date("d-m-Y", strtotime($mtodate));

$getpmain = mysqli_query($conn, "SELECT * FROM `prdmain` WHERE `vouchdt` >= '$mfromdate' AND `vouchdt` <= '$mtodate' $shiftcond")or die(mysqli_error($conn));
$get = mysqli_num_rows($getpmain);

if (isset($_POST["view"])) {
    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
        th{
            background: #4eb3c7;
        }
        .td1{
           text-align:right;
           /*color:black;*/
           font-size:12px;
           font-weight: bold;
        }
        .tr1{
           background: #98f9f0;
            text-align:right;
        }

        
    </style>
    <!--<script src="MyScript/item.js" type="text/javascript"></script>-->
    </head>
    <body>
        <div class="container">
            <?php include("includes/head_rep.php"); ?>
            <div class="container">
                <div class="row">
                    <div class="col-lg-12 pad">
                        <table style="background: white;width: 100%" class="table table-condensed table-hover table-bordered">
                            <thead class="mySearchTr">
                                <tr>
                                    <th style="width:15%">Sr</th>
                                    <th style="width:15%">Date</th>
                                    <th style="width:12%">Shift </th>
                                    <th style="width:58%" colspan="3">Staff</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $ctr = 0;
                                $totqty = 0;

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

                                    $mstaff = mysqli_query($conn, "SELECT * FROM accmast where accode = '$pmain[staffcode]' order by name") or die(mysqli_error($conn));
                                    $tsta = mysqli_fetch_array($mstaff);
                                    $staff = $tsta[name];

                                    $mshift = mysqli_query($conn, "SELECT `code`,`shift` FROM shift where `shift` = '$pmain[shift]' ") or die(mysqli_error($conn));
                                    $trow = mysqli_fetch_array($mshift);
                                    $shift1 = $trow[shift];

                                    $ctr++;
                                    Echo "<tr id='boldfont'>";
                                    Echo "<td>$ctr</td>";
                                    Echo "<td>$pmain[vouchdt]</td>";
                                    Echo "<td>$shift1</td>";
                                    Echo "<td colspan='3'>$staff</td>";
                                    Echo "</tr>";

                                    $totqty = $totqty + $pmain[totqty];


                                    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 style='width:5%;'>Sr</th>";
                                    Echo "<th style='width:15%;'>Pump</th>";
                                    Echo "<th style='width:25%;' class='text-center'>Product</th>";
                                    Echo "<th style='width:20%;' class='text-center'>Closing</th>";
                                    Echo "<th style='width:20%;' class='text-center'>Opening </th>";
                                    Echo "<th style='width:15%;' class='text-center'>Net Sale</th>";
                                    Echo "<tr>";


                                    $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]'")or die(mysqli_error($conn));
                                    $ctr2 = 0;
                                    $tclose = 0;
                                    $topen = 0;
                                    $tnet = 0;

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

                                        $getpd = mysqli_query($conn, "SELECT name,prodcode,rate FROM `product` WHERE `prodcode`= '$tran[prodcode]'") or die(mysqli_error($conn));
                                        $lpd = mysqli_fetch_array($getpd);


                                        $ctr2++;
                                        Echo "<tr>";
                                        Echo "<td>$ctr2</td>";
                                        Echo "<td>$tran[pumpno]</td>";
                                        Echo "<td>$lpd[name]</td>";
                                        Echo "<td class='text-right'>$tran[closing]</td>";
                                        Echo "<td class='text-right'>$tran[opening]</td>";
                                        Echo "<td class='text-right'>$tran[netsale]</td>";

                                        Echo "</tr>";

                                        $tclose = $tclose + $tran[closing];
                                        $topen = $topen + $tran[opening];
                                        $tnet = $tnet + $tran[netsale];
                                    }
                                    Echo "<tr>";
                                    Echo "<td class='text-right' colspan='3'><b>Total</b></td>";
                                    Echo "<td class='text-right'><b>$tclose</b></td>";
                                    Echo "<td class='text-right'><b>$topen</b></td>";
                                    Echo "<td class='text-right'><b>$tnet</b></td>";
                                    Echo "</tr>";

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

                                    $ttclose = $ttclose + $tclose;
                                    $ttopen = $ttopen + $topen;
                                    $ttnet = $ttnet + $tnet;
                                }

                                Echo "<tr class='tr1'>";
                                Echo "<td class='td1' colspan='3'><b>Total</b></td>";
                                Echo "<td style='td1'><b>$ttclose</b></td>";
                                Echo "<td style='td1'><b>$ttopen</b></td>";
                                Echo "<td style='td1'><b>$ttnet</b></td>";
                                Echo "</tr>";
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            <?php include("includes/linksfoot.php");
            ?>
    </body>
    </html>
    <?php
}

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

    $contents .= "Sr., Date, Shift, Staff\n";

    $ctr = 0;
    $totqty = 0;

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

        $mstaff = mysqli_query($conn, "SELECT * FROM accmast where accode = '$pmain[staffcode]'") or die(mysqli_error($conn));
        $tsta = mysqli_fetch_array($mstaff);
        $staff = $tsta[name];

        $mshift = mysqli_query($conn, "SELECT `code`,`shift` FROM shift where `shift` = '$pmain[shift]' ") or die(mysqli_error($conn));
        $trow = mysqli_fetch_array($mshift);
        $shift1 = $trow[shift];

        $ctr++;
        $contents .= "$ctr" . ",";
        $contents .= "$pmain[vouchdt]" . ",";
        $contents .= "$shift1" . ",";
        $contents .= "$staff" . "\n";

        $totqty = $totqty + $pmain[totqty];


        $contents .= "Sr., Pump, Product, Closing, Opening, Net Sale\n";

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

            $getpd = mysqli_query($conn, "SELECT name,prodcode,rate FROM `product` WHERE `prodcode`= '$tran[prodcode]'") or die(mysqli_error($conn));
            $lpd = mysqli_fetch_array($getpd);


            $ctr2++;
            $contents .= "$ctr2" . ",";
            $contents .= "$tran[pumpno]" . ",";
            $contents .= "$lpd[name]" . ",";
            $contents .= "$tran[closing]" . ",";
            $contents .= "$tran[opening]" . ",";
            $contents .= "$tran[netsale]" . "\n";

            $tclose = $tclose + $tran[closing];
            $topen = $topen + $tran[opening];
            $tnet = $tnet + $tran[netsale];
        }
        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "Total" . ",";
        $contents .= "$tclose" . ",";
        $contents .= "$topen" . ",";
        $contents .= "$tnet" . "\n";


        $ttclose = $ttclose + $tclose;
        $ttopen = $ttopen + $topen;
        $ttnet = $ttnet + $tnet;
    }

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