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

$maindb = "leadenq";
$trandb = "conftran";
$formname = "Confirm Register";
$file = "confreg_fr.php";

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

if (isset($_POST["view"])) {
    $name = $_POST[name];
    $gtype = $_POST[gtype];

    $dtype = $_POST[dtype];


    $staffcond = "";
    $mtypecond = "";
//    browse($gtype);
    if (!empty($name)) {
        $staffcond = " AND `username`='$name' ";
    }


    $mycheck1 = array();
    $count = 1;

    if (!empty($_POST['gtype'])) {
        foreach ($_POST['gtype'] as $check1) {
            $mycheck1[$count++] .= "OR `status` = '" . $check1 . "' ";
        }
    }

    foreach ($mycheck1 as $g => $values) {
        $mcheck1 .= $values;
    }

    $typecond = '';
    if (!empty($_POST['gtype'])) {
        $typecond = " AND (`status` = '' " . $mcheck1 . " )";
    }
//if (!empty($gtype)) {
//$mtypecond = " AND `status`='$gtype' ";
//}
//browse($mcheck1);


    if (empty($_POST["frmdt"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; leadreg.php");
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; leadreg.php");
    }
    $mfrmdate = trim($_POST["frmdt"]);
    $mtodate = trim($_POST["todt"]);



// print "SELECT * FROM `$maindb` WHERE `vouchno`!='' $partyname $broker $transpor $item $godown $mtype";
    if ($usertype == 1) {
        $getpmain = mysqli_query($conn,"SELECT *,DATE_FORMAT(`confdate`, '%d-%m-%Y') as confdate,DATE_FORMAT(`deptdate`, '%d-%m-%Y') as deptdate
                         FROM $maindb where `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' AND `status` = 'Confirmed'
                         $staffcond $typecond order by vouchno")or die(mysqli_error($conn));
    } else {
        $getpmain = mysqli_query($conn,"SELECT *,DATE_FORMAT(`confdate`, '%d-%m-%Y') as confdate,DATE_FORMAT(`deptdate`, '%d-%m-%Y') as deptdate
                         FROM $maindb where `username`='$user' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' AND `status` = 'Confirmed'
                         $typecond order by vouchno")or die(mysqli_error($conn));
    }
    include("includes/linkshead.php");
    ?>
    <!--<script src="MyScript/item.js" type="text/javascript"></script>-->    
    </head>
    <body>
        <div class="container" style="width:100%">
            <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-3" style="float: right; margin-top: 5px;">
                    <a onclick="window.close()" class="btn btn-primary">GO Back</a>
                </div>
            </div>
            <div class="row">
                <div class="col-lg-12 pad">
                    <table style="width:100%;background: white" class="table-condensed table-hover table-bordered">
                        <thead>
                            <tr style="height:10px;background-color: #64CCFC;">
                                <th style="width:3%">Sr</th>
                                <?php if ($usertype == 1) { ?>
                                    <th>Staff</th>
                                <?php }
                                ?>
                                <th style="width:4%">Book No</th>
                                <th style="width:9%">Conf.Date</th>
                                <th style="width:9%">Name</th>
                                <th style="width:9%">Last Name</th>
                                <th style="width:9%">Destination</th>
                                <th style="width:9%">Source</th>
                                <th style="width:8%">Travel Date</th>
                                <th style="width:7%">Mobile</th>
                                <th style="width:8%">Email-ID</th>
                                <th style="width:7%">MUP</th>
                                <th style="width:5%">Conf. Adult</th>
                                <th style="width:5%">Conf. Child</th>
                                <th style="width:5%">Conf. Total</th>
                                <!--<th class="numeric">New Remark</th>-->                            
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;

                            while ($pmain = mysqli_fetch_array($getpmain)) {
                                $ctr++;
                                if ($pmain[quotvisa] == "Y") {
                                    $quotvisa = "YES";
                                } else {
                                    $quotvisa = "No";
                                }
                                Echo "<tr>";

                                Echo "<td style='width:3%'>$ctr</td>";
                                if ($usertype == 1) {
                                    Echo "<td style='width:10%'>$pmain[username]</td>";
                                }
                                Echo "<td style='width:4%'>$pmain[vouchno]</td>";
                                Echo "<td style='width:9%'>$pmain[confdate]</td>";
                                Echo "<td style='width:9%'>$pmain[name]</td>";
                                Echo "<td style='width:9%'>$pmain[lastname]</td>";
                                Echo "<td style='width:9%'>$pmain[destination]</td>";
                                Echo "<td style='width:9%'>$pmain[source]</td>";
                                Echo "<td style='width:8%'>$pmain[deptdate]</td>";
                                Echo "<td style='width:7%'>$pmain[mobile]</td>";
                                Echo "<td style='width:8%'>$pmain[email]</td>";
                                Echo "<td style='width:7%'>$pmain[mup]</td>";
                                Echo "<td style='width:5%'>$pmain[confadlt]</td>";
                                Echo "<td style='width:5%'>$pmain[confchld]</td>";
                                Echo "<td style='width:5%'>$pmain[conftot]</td>";
//                        Echo "<td style='width:10%'>$pmain[remarks1]</td>";
                                Echo "</tr>";

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

                                    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='background-color: #cccccc;width:5%;'>Sr</th>";
                                    Echo "<th style='background-color: #cccccc';width:10%; >Suppliers</th>";
                                    Echo "<th style='background-color: #cccccc';width:10%; >Particulars</th>";
                                    Echo "<th style='background-color: #cccccc;width:8%;'  class='numeric' >Amount</th>";
                                    Echo "<th style='background-color: #cccccc;width:5%;' >Currency</th>";
                                    Echo "<th style='background-color: #cccccc;width:8%;' class='numeric'>ROE</th>";
                                    Echo "<th style='background-color: #cccccc;width:8%;' class='numeric'>Amt in Rs.</th>";
                                    Echo "<th style='background-color: #cccccc;width:8%;' class='numeric'>Account Amount</th>";
                                    Echo "<th style='background-color: #cccccc;width:8%;' class='numeric'>Bill No.</th>";
                                    Echo "<th style='background-color: #cccccc;width:8%;' >Bill Date.</th>";
                                    Echo "<tr>";


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


                                        $ctr2++;
                                        Echo "<tr>";
                                        Echo "<td>$ctr2</td>";
                                        Echo "<td>$tran[supplier]</td>";
                                        Echo "<td>$tran[particulars]</td>";
                                        Echo "<td class='numeric'>$tran[amt]</td>";
                                        Echo "<td>$tran[curr]</td>";
                                        Echo "<td class='numeric'>$tran[roe]</td>";
                                        Echo "<td class='numeric'>$tran[amtrs]</td>";
                                        Echo "<td class='numeric'>$tran[acnt]</td>";
                                        Echo "<td class='numeric'>$tran[sbillno]</td>";
                                        Echo "<td>$tran[sbilldt]</td>";
                                        Echo "</tr>";
//                                        $topaytot1 = $topaytot1 + $tran[topay];
//                                        $totweight = $totweight + $tran[weight];
                                        $totamt = $totamt + $tran[amt];
//                                        $totqty1 = $totqty1 + $tran[totqty];
//                                if ($_POST["dtype"] == 2) {
                                    }
                                    echo "<tr> ";
                                    $colr1 = "blue";
                                    echo "<td style='color:$colr1;' align='right' id='font1'  ><h5><b>Total :</b></h5></td>";
                                    echo "<td></td>";
                                    echo "<td></td>";
                                    echo "<td style='color:$colr1;' align='right' id='font1'  ><h5><b>$totamt</b></h5></td>";
                                    echo "<td></td>";
                                    echo "<td></td>";
                                    echo "<td></td>";
                                    echo "<td></td>";
                                    echo "<td></td>";
                                    echo "<td></td>";
                                    echo "</tr>";

                                    Echo "</table>";
                                    Echo "</td>";
                                }
//                                }
                            }
//                            
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

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


    $staffcond = "";
    $mtypecond = "";

    if (!empty($name)) {
        $staffcond = " AND `username`='$name' ";
    }

    $mycheck1 = array();
    $count = 1;

    if (!empty($_POST['gtype'])) {
        foreach ($_POST['gtype'] as $check1) {
            $mycheck1[$count++] .= "OR `status` = '" . $check1 . "' ";
        }
    }

    foreach ($mycheck1 as $g => $values) {
        $mcheck1 .= $values;
    }

    $typecond = '';
    if (!empty($_POST['gtype'])) {
        $typecond = " AND (`status` = '' " . $mcheck1 . " )";
    }

    if (empty($_POST["frmdt"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; leadreg.php");
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; leadreg.php");
    }
    $mfrmdate = trim($_POST["frmdt"]);
    $mtodate = trim($_POST["todt"]);

    if ($usertype == 1) {
        $getpmain = mysqli_query($conn,"SELECT *,DATE_FORMAT(`confdate`, '%d-%m-%Y') as confdate,DATE_FORMAT(`deptdate`, '%d-%m-%Y') as deptdate
                         FROM $maindb where `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' AND `status` = 'Confirmed'
                         $staffcond $typecond order by vouchno")or die(mysqli_error($conn));
    } else {
        $getpmain = mysqli_query($conn,"SELECT *,DATE_FORMAT(`confdate`, '%d-%m-%Y') as confdate,DATE_FORMAT(`deptdate`, '%d-%m-%Y') as deptdate
                         FROM $maindb where `username`='$user' and `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' AND `status` = 'Confirmed'
                         $typecond order by vouchno")or die(mysqli_error($conn));
    }

    $contents.="Sr." . ",";
    if ($usertype == 1) {
        $contents.="Staff" . ",";
    }
    $contents.="Book NO., Conf. Date, Name, Last Name, Destination, Source, Travel Date, Mobile, Email-ID, MUP, Conf. Adult,Conf. Child,Conf. Total\n";

    $ctr = 0;
    while ($pmain = mysqli_fetch_array($getpmain)) {
        $ctr++;
        if ($pmain[quotvisa] == "Y") {
            $quotvisa = "YES";
        } else {
            $quotvisa = "No";
        }
        if ($usertype == 1) {
            $pusername = $pmain[username];
        }
        $pvouchno = $pmain[vouchno];
        $pconfdate = $pmain[confdate];
        $pname = $pmain[name];
        $plastname = $pmain[lastname];
        $pdestination = $pmain[destination];
        $psource = $pmain[source];
        $pdeptdate = $pmain[deptdate];
        $pmobile = $pmain[mobile];
        $pemail = $pmain[email];
        $pmup = $pmain[mup];
        $pconfadlt = $pmain[confadlt];
        $pconfchld = $pmain[confchld];
        $pconfpax = $pmain[conftot];

        $contents.=$ctr . ",";
        if ($usertype == 1) {
            $contents.=$pusername . ",";
        }
        $contents.=$pvouchno . ",";
        $contents.=$pconfdate . ",";
        $contents.=$pname . ",";
        $contents.=$plastname . ",";
        $contents.=$pdestination . ",";
        $contents.=$psource . ",";
        $contents.=$pdeptdate . ",";
        $contents.=$pmobile . ",";
        $contents.=$pemail . ",";
        $contents.=$pmup . ",";
        $contents.=$pconfadlt . ",";
        $contents.=$pconfchld . ",";
        $contents.=$pconfpax . "\n";

        if ($_POST["dtype"] == 2) {
            $contents.="Sr, Suppliers, Particulars, Amount, Currency, ROE, Amt in Rs.,Account Amount, Bill No., Bill Date\n";

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


                $ctr2++;

                $supplier = $tran[supplier];
                $particulars = $tran[particulars];
                $amt = $tran[amt];
                $curr = $tran[curr];
                $roe = $tran[roe];
                $amtrs = $tran[amtrs];
                $acnt = $tran[acnt];
                $sbillno = $tran[sbillno];
                $sbilldt = $tran[sbilldt];

                $contents.=$ctr2 . ",";
                $contents.=$supplier . ",";
                $contents.=$particulars . ",";
                $contents.=$amt . ",";
                $contents.=$curr . ",";
                $contents.=$roe . ",";
                $contents.=$amtrs . ",";
                $contents.=$acnt . ",";
                $contents.=$sbillno . ",";
                $contents.=$sbilldt . "\n";


//                                        $topaytot1 = $topaytot1 + $tran[topay];
//                                        $totweight = $totweight + $tran[weight];
                $totamt = $totamt + $tran[amt];
            }
//                                        $totqty1 = $totqty1 + $tran[totqty];
//                                if ($_POST["dtype"] == 2) {
            $contents.= "Total" . ",";
            $contents.= " " . ",";
            $contents.= " " . ",";
            $contents.= "$totamt " . ",";
            $contents.= " " . ",";
            $contents.= " " . ",";
            $contents.= " " . ",";
            $contents.= " " . ",";
            $contents.= " " . ",";
            $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;
    }
}
?>