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

$maindb = 'todo';
$trandb = "todotran";
$formname = "Bhatta Register";
$file = "todoreg_fr.php";

$zroot = $_SESSION["zdbroot"];

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

if (isset($_POST["view"])) {
    $acname = $_POST["acname"];
    $fdate = $_POST["fdate"];
    $tdate = $_POST["tdate"];


    $dtypecond = "";
    $mscopecond = "";

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

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


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


    if (!empty($acname)) {
        $acnamecond = " AND b.`staff`='$acname' ";
    }
//    if (!empty($tdate)) {
//        $fdatecond = " AND `fdate`='$fdate' ";
//    }
//
//    if (!empty($tdate)) {
//        $tdatecond = " AND `tdate`='$tdate' ";
//    }

    $getpmain = mysqli_query($conn, "SELECT a.*, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt,b.staff FROM `$maindb` a, `$trandb` b WHERE a.`vouchdt` >= '$mfrmdate' 
                            AND a.`vouchdt` <= '$mtodate'  $acnamecond group  by a.vouchno")or die(mysqli_error($conn));

    $get = mysqli_num_rows($getpmain);
//    browse($get);
    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
    </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-bordered table-condensed table-hover">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:5%">Sr</th>
                                <th style="width:10%">To Do No.</th>
                                <th style="width:10%">Date</th>
                                <th style="width:40%">Task</th>
                                <th style="width:10%">Done</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $totamt = 0;

                            while ($pmain = mysqli_fetch_array($getpmain)) {
                                $done = "";
                                if ($pmain[done] == 'Y') {
                                    $done = "Completed";
                                }
                                $ctr++;
                                Echo "<tr id='boldfont'>";
                                Echo "<td>$ctr</td>";
                                Echo "<td>$pmain[vouchno]</td>";
                                Echo "<td>$pmain[mydt]</td>";
                                Echo "<td>$pmain[task]</td>";
                                Echo "<td>$done</td>";
                                Echo "</tr>";


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

                                    Echo "<tr style='height:10px;'>";
                                    Echo "<td colspan='25'>";
                                    Echo "<table style='width:50%;' id='tabital' class='table table-condensed table-hover table-bordered'>";
                                    Echo "<th style='background-color: #cccccc;width:10%;'>Sr</th>";
                                    Echo "<th style='background-color: #cccccc;width:90%; class='numeric''>Assigned To</th>";
                                    Echo "<tr>";


                                    $ctr2 = 0;
                                    $tqty = 0;
                                    $tamt = 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`,`user` FROM $zroot.`passmain` WHERE `code`='$tran[staff]'")or die(mysqli_error($conn));
                                        $item = mysqli_fetch_array($getitem);

                                        $ctr2++;
                                        Echo "<tr>";
                                        Echo "<td>$ctr2</td>";
                                        Echo "<td>$item[user]</td>";
                                        Echo "</tr>";

                                        $tqty = $tran[qty] + $tqty;
                                        $tamt = $tran[total] + $tamt;

//                                        
                                    }
                                    Echo "</table>";
                                    Echo "</td>";
                                }
                            }
                            $colr = "red";
//                            echo "<tr> ";
//                            echo "<td align='right' style='color:$colr;'></td>";
//                            echo "<td align='right' style='color:$colr;'></td>";
//                            echo "<td align='right' style='color:$colr;'></td>";
//                            echo "<td align='right' style='color:$colr;'></td>";
//                            echo "<td align='right' style='color:$colr;'><h4><b>Grand Total</b></h4></td>";
//                            echo "<td align='right' style='color:$colr;' id='font'><h4><b>$totamt</b></h4></td>";
//                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

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

    $dtypecond = "";
    $mscopecond = "";

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

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

    if (!empty($acname)) {
        $acnamecond = " AND `karigar`='$acname' ";
    }
    if (!empty($tdate)) {
        $fdatecond = " AND `fdate`='$fdate' ";
    }

    if (!empty($tdate)) {
        $tdatecond = " AND `tdate`='$tdate' ";
    }

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

    $contents .= "Sr,Name,Date,Day,Amount\n";

    $ctr = 0;
    $totamt = 0;


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

        $getkari = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`= $pmain[karigar] ")or die(mysqli_error($conn));
        $showkari = mysqli_fetch_array($getkari);

        $ctr++;
        $pkariname = $showkari[name];
        $pdate = $pmain[mydt];
        $pday = $pmain[day];
        $ptot = $pmain[tot];

        $contents .= $ctr . ",";
        $contents .= $pkariname . ",";
        $contents .= $pdate . ",";
        $contents .= $pday . ",";
        $contents .= $ptot . "\n";

        $totamt = $totamt + $pmain[tot];
    }
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= " " . ",";
    $contents .= "Grand Total" . ",";
    $contents .= $totamt . "\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;
    }
}
?>