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

$maindb = "dipmain";
$trandb = "dipmain";
$formname = "Dip Reading - Register";
$file = "dreadreg.php";

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

$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 *,DATE_FORMAT(`vouchdt`, '%d-%m-%Y')as mydt FROM $maindb WHERE vouchdt >= '$mfromdate'
                                         AND vouchdt <= '$mtodate' $shiftcond")or die(mysqli_error($conn));

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:14px;
           font-weight: bold;
        }
        .tr1{
           background: #98f9f0;
        }

    </style>
    <!--<script src="MyScript/item.js" type="text/javascript"></script>-->
    </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 table-bordered">
                            <thead class="mySearchTr">
                                <tr>
                                    <th style="width:25%">Sr</th>
                                    <th style="width:25%">Date</th>
                                    <th style="width:25%">Shift</th>
                                    <th style="width:25%">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 * FROM shift where shift = '$pmain[shift]'") or die(mysqli_error($conn));
                                    $trow = mysqli_fetch_array($mshift);
                                    $shift = $trow[shift];

                                    $ctr++;
                                    Echo "<tr id='boldfont'>";
                                    Echo "<table style='width:100%;' id='tabital' class='table table-condensed table-hover table-bordered'>";
                                    Echo "<td style='width:25%'>$ctr</td>";
                                    Echo "<td style='width:25%''>$pmain[mydt]</td>";
                                    Echo "<td style='width:25%''>$shift</td>";
                                    Echo "<td style='width:25%''>$staff</td>";
                                    ECHO "</table>";
                                    Echo "</tr>";

                                    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:10%;' rowspan='2'>Tank</th>";
                                    Echo "<th style='width:15%;' rowspan='2'>Product</th>";
                                    Echo "<th style='width:20%;' rowspan='2' colspan='2' class='text-center'>Opening</th>";
                                    Echo "<th style='width:20%;' rowspan='2' colspan='2' class='text-center'>Adding</th>";
                                    Echo "<th style='width:20%;' rowspan='2' colspan='2' class='text-center'>Closing</th>";
                                    Echo "<th style='width:15%;'>Sale</th>";
                                    Echo "<tr>";

                                    Echo "<tr>";
                                    Echo "<td colspan='25'>";
                                    Echo "<table style='width:100%;' id='tabital' class='table table-condensed  table-bordered'>";
                                    Echo "<th style='width:10%;'></th>";
                                    Echo "<th style='width:15%;'></th>";
                                    Echo "<th style='width:10%;text-align:right'>Dip</th>";
                                    Echo "<th style='width:10%;text-align:right'>Litres</th>";
                                    Echo "<th style='width:10%;text-align:right'>Dip</th>";
                                    Echo "<th style='width:10%;text-align:right'>Litres</th>";
                                    Echo "<th style='width:10%;text-align:right'>Dip</th>";
                                    Echo "<th style='width:10%;text-align:right'>Litres</th>";
                                    Echo "<th style='width:15%;text-align:right'>Litres</th>";
                                    Echo "<tr>";


                                    $gettran = mysqli_query($conn, "SELECT * FROM `diptran` WHERE vouchno = '$pmain[vouchno]' ")or die(mysqli_error($conn));
                                    $topdp = 0;
                                    $topltrs = 0;
                                    $tadddip = 0;
                                    $tadltrs = 0;
                                    $tclosedip = 0;
                                    $tcloseltrs = 0;
                                    $tsaleltrs = 0;
                                    while ($fettran = mysqli_fetch_array($gettran)) {

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

                                        $ctr2++;
                                        Echo "<tr>";
                                        Echo "<td>$fettran[tankno]</td>";
                                        Echo "<td>$lpd[name]</td>";
                                        Echo "<td style='text-align:right'>$fettran[opendip]</td>";
                                        Echo "<td style='text-align:right'>$fettran[openltrs]</td>";
                                        Echo "<td style='text-align:right'>$fettran[adddip]</td>";
                                        Echo "<td style='text-align:right'>$fettran[addltrs]</td>";
                                        Echo "<td style='text-align:right'>$fettran[closedip]</td>";
                                        Echo "<td style='text-align:right'>$fettran[closeltrs]</td>";
                                        Echo "<td style='text-align:right'>$fettran[saleltrs]</td>";
                                        Echo "</tr>";

                                        $topdp = $topdp + $fettran[opendip];
                                        $topltrs = $topltrs + $fettran[openltrs];
                                        $tadddip = $tadddip + $fettran[adddip];
                                        $tadltrs = $taddltrs + $fettran[addltrs];
                                        $tclosedip = $tclosedip + $fettran[closedip];
                                        $tcloseltrs = $tcloseltrs + $fettran[closeltrs];
                                        $tsaleltrs = $tsaleltrs + $fettran[saleltrs];
                                    }

                                    Echo "<tr>";
                                    Echo "<td></td>";
                                    Echo "<td class='td1'>Total</td>";
                                    Echo "<td class='td1'>$topdp</td>";
                                    Echo "<td class='td1'>$topltrs</td>";
                                    Echo "<td class='td1'>$tadddip</td>";
                                    Echo "<td class='td1'>$tadltrs</td>";
                                    Echo "<td class='td1'>$tclosedip</td>";
                                    Echo "<td class='td1'>$tcloseltrs</td>";
                                    Echo "<td class='td1'>$tsaleltrs</td>";
                                    Echo "</tr>";

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

                                    $totopnltrs = $totopnltrs + $topltrs;
                                    $totaddltrs = $totaddltrs + $tadltrs;
                                    $totcloseltrs = $totcloseltrs + $tcloseltrs;
                                    $totsaleltrs = $totsaleltrs + $tsaleltrs;
                                }



                                Echo "<tr class='tr1'>";
                                Echo "<td colspan='3' style='text-align:right;color:black;font-size:14px'><b>Total</b></td>";
                                Echo "<td style='text-align:right;color:black;font-size:14px'><b>$totopnltrs</b></td>";
                                Echo "<td></td>";
                                Echo "<td style='text-align:right;color:black;font-size:14px'><b>$totaddltrs</b></td>";
                                Echo "<td></td>";
                                Echo "<td style='text-align:right;color:black;font-size:14px'><b>$totcloseltrs</b></td>";
                                Echo "<td style='text-align:right;color:black;font-size:14px'><b>$totsaleltrs</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]' order by name") or die(mysqli_error($conn));
        $tsta = mysqli_fetch_array($mstaff);
        $staff = $tsta[name];

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

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


        $contents .= "Tank,Product,Opening, , Adding , ,Closing, ,Sale\n";



        $contents .= ",,Dip,Litres,Dip,Litres,Dip,Litres,Litres\n";



        $gettran = mysqli_query($conn, "SELECT * FROM `diptran` WHERE vouchno = '$pmain[vouchno]' ")or die(mysqli_error($conn));
        $topdp = 0;
        $topltrs = 0;
        $tadddip = 0;
        $tadltrs = 0;
        $tclosedip = 0;
        $tcloseltrs = 0;
        $tsaleltrs = 0;
        while ($fettran = mysqli_fetch_array($gettran)) {

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

            $ctr2++;
            $contents .= "$fettran[tankno]" . ",";
            $contents .= "$lpd[name]" . ",";
            $contents .= "$fettran[opendip]" . ",";
            $contents .= "$fettran[openltrs]" . ",";
            $contents .= "$fettran[adddip]" . ",";
            $contents .= "$fettran[addltrs]" . ",";
            $contents .= "$fettran[closedip]" . ",";
            $contents .= "$fettran[closeltrs]" . ",";
            $contents .= "$fettran[saleltrs]" . "\n";


            $topdp = $topdp + $fettran[opendip];
            $topltrs = $topltrs + $fettran[openltrs];
            $tadddip = $tadddip + $fettran[adddip];
            $tadltrs = $taddltrs + $fettran[addltrs];
            $tclosedip = $tclosedip + $fettran[closedip];
            $tcloseltrs = $tcloseltrs + $fettran[closeltrs];
            $tsaleltrs = $tsaleltrs + $fettran[saleltrs];
        }

        $contents .= "" . ",";
        $contents .= "Total" . ",";
        $contents .= "$topdp" . ",";
        $contents .= "$topltrs" . ",";
        $contents .= "$tadddip" . ",";
        $contents .= "$tadltrs" . ",";
        $contents .= "$tclosedip" . ",";
        $contents .= "$tcloseltrs" . ",";
        $contents .= "$tsaleltrs" . "\n";


        $totopnltrs = $totopnltrs + $topltrs;
        $totaddltrs = $totaddltrs + $tadltrs;
        $totcloseltrs = $totcloseltrs + $tcloseltrs;
        $totsaleltrs = $totsaleltrs + $tsaleltrs;
    }



    $contents .= "Total" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "$totopnltrs" . ",";
    $contents .= "" . ",";
    $contents .= "$totaddltrs" . ",";
    $contents .= "" . ",";
    $contents .= "$totcloseltrs" . ",";
    $contents .= "$totsaleltrs" . "\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;
    }
}
?>