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

$maindb = "stkmain";
$trandb = "stktran";
$formname = "Stock Reading - Register";
$file = "sreadreg_fr.php";

//browse ('Work In Progress!!!!');
$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 * FROM $maindb ")or die(mysqli_error($conn));
//WHERE vouchdt >= '$mfromdate' AND vouchdt <= '$mtodate' $shiftcond

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="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: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 "<td>$ctr</td>";
                                    Echo "<td>$pmain[vouchdt]</td>";
                                    Echo "<td>$shift</td>";
                                    Echo "<td>$staff</td>";
                                    Echo "</tr>";

                                    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>Sr.</th>";
                                    Echo "<th>Prodcode</th>";
                                    Echo "<th>Product</th>";
                                    Echo "<th>Sale</th>";
                                    Echo "<th>rate</th>";
                                    Echo "<tr>";



                                    $gettran = mysqli_query($conn, "SELECT * FROM `stktran` WHERE vouchno = '$pmain[vouchno]' ")or die(mysqli_error($conn));
                                    $ctr2 = 0;
                                    $totsale = 0;
                                    $totrate = 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>$ctr2</td>";
                                        Echo "<td>$fettran[prodcode]</td>";
                                        Echo "<td>$lpd[name]</td>";
                                        Echo "<td style='text-align:right'>$fettran[sale]</td>";
                                        Echo "<td style='text-align:right'>$fettran[rate]</td>";
                                        Echo "</tr>";

                                        $totsale = $totsale + $fettran[sale];
                                        $totrate = $totrate + $fettran[rate];
                                    }

                                    Echo "<tr>";
                                    Echo "<td colspan='3' class='text-right'>Total</td>";
                                    Echo "<td style='text-align:right'>$totsale </td>";
                                    Echo "<td style='text-align:right'>$totrate </td>";
                                    Echo "</tr>";
                                    Echo "</table>"; 
                                

                                    $ttotsale = $ttotsale + $totsale;
                                    $ttotrate = $ttotrate + $totrate;
                                }



                                Echo "<tr class='tr1'>";
                                Echo "<td colspan='2' class='td1' style='width:72%'><b>Total</b></td>";
                                Echo "<td style='width:10%' class='td1'><b>$ttotsale</b></td>";
                                Echo "<td style='width:16%' class='td1'><b>$ttotrate</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 .= "Sr., Prodcode, Product, Sale, Rate\n";

        $gettran = mysqli_query($conn, "SELECT * FROM `stktran` WHERE vouchno = '$pmain[vouchno]' ")or die(mysqli_error($conn));
        $ctr2 = 0;
        $totsale = 0;
        $totrate = 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 .= "$ctr2" . ",";
            $contents .= "$fettran[prodcode]" . ",";
            $contents .= "$lpd[name]" . ",";
            $contents .= "$fettran[sale]" . ",";
            $contents .= "$fettran[rate]" . "\n";

            $totsale = $totsale + $fettran[sale];
            $totrate = $totrate + $fettran[rate];
        }

        $contents .= "" . ",";
        $contents .= "" . ",";
        $contents .= "Total" . ",";
        $contents .= "$totsale" . ",";
        $contents .= "$totrate" . "\n";

        $ttotsale = $ttotsale + $totsale;
        $ttotrate = $ttotrate + $totrate;
    }



    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "Grand Total" . ",";
    $contents .= "$ttotsale" . ",";
    $contents .= "$ttotrate" . ",";

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