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

$maindb     = "salemain";
$formname   = "Driver Register";
$file       = "driverreg_fr.php";

$zroot      = 'cepheisy_msp_root';

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

if (isset($_POST["view"])) {
    $driver   = $_POST["driver"];
    $mfdt   = trim($_POST["fdate"]);
    $mtdt   = trim($_POST["tdate"]);

    if ($_POST["order"] == 1) {
        $ordercond = " order by visit";
    } else {
        $ordercond = " order by total";
    }
  
    if (!empty($driver)) {
        $drivecond = " AND `driver`='$driver' ";
    }

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

    $getpmain = mysqli_query($conn, "SELECT driver, COUNT(*)as visit, SUM(netamt) as total,SUM(dcamt) as totdcamt,SUM(dcbperc) as totdcbperc, 
                DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
                            AND `vouchdt` <= '$mtodate'  $drivecond GROUP BY `driver` $ordercond ")or die(mysqli_error($conn));

    include("includes/linkshead.php");
    ?>
    <style type="text/css">
        #tabital{
            font-style: italic;
        }
        #font1{
            font-style: italic;
        }
        #boldfont{
            font-weight: bold; 
        }
    </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-8 col-md-8 col-sm-8 col-xs-8">
        <?php if ($mfdt != '') { ?>
            From Date - 
            <?php Echo $mfdt; ?> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <?php } ?>
        <?php if ($mtdt != '') { ?>
            To Date - 
            <?php Echo $mtdt; ?>
        <?php } ?>

    </div>-->
<!--    </div>-->
<!--            <div class="row">
                <div class="col-lg-6">
                    <label class="label-control marg">Date - </label>
                    <label class="label-control marg"><?php Echo Date("d-m-Y"); ?></label>
                </div>
                <div class="col-lg-5">
                    <label class="label-control marg">Print - </label>
                    <label class="label-control marg"><?php Echo $formname; ?></label>
                </div>
                <div class="col-lg-1">
                    <a onclick="window.close()" class="btn btn-primary">GO Back</a>
                </div>
            </div>-->
            <div class="row">
                <div class="col-lg-12 pad">
                    <table style="background: white;width: 100%" class="table table-condensed table-hover">
                        <thead class="mySearchTr">
                            <tr>


                                <th style="width:10%">Driver</th>
                                <th style="width:10%">Date</th>
                                <th style="width:10%"class="text-right">Visit</th>
                                <th style="width:15%" class="text-right">Total</th>
                                <th style="width:15%" class="text-right">Driver Total</th>
                                <th style="width:15%" class="text-right">Driver Commission</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $totdriver = 0;
                            $total = 0;

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

                                $getdriver = mysqli_query($conn, "Select `code`,`name` from `driver` where `code` = '$pmain[driver]'") or die(mysql_erro());
                                $driver1 = (mysqli_fetch_array($getdriver));

                                $ctr++;
                                Echo "<tr id='boldfont'>";
                                Echo "<td>$driver1[name]</td>";
                                Echo "<td>$pmain[mydt]</td>";
                                Echo "<td class='text-right'>$pmain[visit]</td>";
                                Echo "<td class='text-right'>$pmain[total]</td>";
                                Echo "<td class='text-right'>$pmain[totdcamt]</td>";
                                Echo "<td class='text-right'>$pmain[totdcbperc]</td>";
                                Echo "</tr>";
                                $total = $total + $pmain[total];
                                $totdriver = $totdriver + $pmain[totdcamt];


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

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

    $ordercond = "";

    if ($_POST["order"] == 1) {
        $ordercond = " order by visit";
    } else {
        $ordercond = " order by total";
    }


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

    if (!empty($driver)) {
        $drivecond = " AND `driver`='$driver' ";
    }

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

    $getpmain = mysqli_query($conn, "SELECT driver, COUNT(*)as visit, SUM(netamt) as total,SUM(dcamt) as totdcamt,SUM(dcbperc) as totdcbperc, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE `vouchdt` >= '$mfrmdate' 
                            AND `vouchdt` <= '$mtodate'  $drivercond $ordercond")or die(mysqli_error($conn));


    $contents.="Driver,Date,Visit,Total,Driver Total,Driver Commission\n";

    $ctr = 0;
    $totdriver = 0;
    $total = 0;

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

        $getdriver = mysqli_query($conn, "Select `code`,`name` from driver where `code` = '$pmain[driver]'") or die(mysql_erro());
        $driver = (mysqli_fetch_array($getdriver));

        $ctr++;
        $pdriver = $driver[name];
        $pdate = $pmain[mydt];
        $pvisit = $pmain[visit];
        $ptotal = $pmain[total];
        $ptotdcamt = $pmain[totdcamt];
        $ptotdcbperc = $pmain[totdcbperc];

        $contents.=$pdriver . ",";
        $contents.=$pdate . ",";
        $contents.=$pvisit . ",";
        $contents.=$ptotal . ",";
        $contents.=$ptotdcamt . ",";
        $contents.=$ptotdcbperc . "\n";

        $total = $total + $pmain[total];
        $totdriver = $totdriver + $pmain[totdcamt];

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