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

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

$formname = 'Transportation Sale Register';

$file = 'transpsalereg_fr.php';

$maindb = 'pax';
$trandb = 'transent';

$dtype       = $_POST["dtype"];
$fromplace   = $_POST["fromplace"];
$toplace   = $_POST["toplace"];

$fromplacecond = "";
$fromplacecond2 = "";
$toplacecond = "";
$toplacecond2 = "";

if (!empty($fromplace)) {
    $fromplacecond  = "AND b.fromplace = '$fromplace' ";
    $fromplacecond2 = "AND fromplace = '$fromplace'";
}

if (!empty($toplace)) {
    $toplacecond  = "AND b.toplace = '$toplace' ";
    $toplacecond2 = "AND toplace = '$toplace'";
}



if (($_GET["fdt"]) != '') {
    $mfdt = $_GET["fdt"];
    $mtdt = $_GET["tdt"];
    $mfrmdate = date("y-m-d", strtotime($mfdt));
    $mtodate = date("y-m-d", strtotime($mtdt));
    $dtype = $_GET["rpt"];
} else {

    $mfrmdate = trim($_POST["frmdt"]);
    $mtodate = trim($_POST["todt"]);


    if (empty($_POST["frmdt"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; salereg.php");
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; salereg.php");
    }
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
}


$getpmain2 = mysqli_query($conn, "SELECT a.*,DATE_FORMAT(a.`date`,'%d-%m-%Y')as date  FROM `$maindb` a, $trandb b  
                                  WHERE a.vouchno = b.vouchno  AND a.`date` >= '$mfrmdate' AND a.`date` <= '$mtodate' 
                                  AND  a.transinramt > 0  $citycond $hotelcond $checkcond
                                  GROUP BY a.`vouchno` ORDER BY a.`vouchno` ") or die(mysqli_error($conn));
$getno2 = mysqli_num_rows($getpmain2);


if (isset($_POST["view"]) || ($_GET["view"])) {

    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-fluid">
            <div class="row">
                <div class="col-lg-12 ">
                    <?php
                    include("includes/head_rep.php");

                    if ($getno2 > 0) {
                    ?>
                        <table class="table table-condesed table-bordered">
                            <tr>
                                <td style="font-size: 15px;padding:5px" class="HDFont" colspan="2">
                                    Total No. of Record :&nbsp;&nbsp;<b><?php echo $getno2; ?></b>
                                </td>
                            </tr>
                        </table>

                        <table style="background: white;width: 100%" class="table table-condensed table-striped table-hover table-bordered">
                            <thead class="mySearchTr">
                                <tr>
                                    <th width="7%;">Date</th>
                                    <th width="7%;">Voucher</th>
                                    <th width="12%;">For</th>
                                    <th width="30%;">Pax Name</th>
                                    <th style="width:10%; text-align:right">FE Amount</th>
                                    <th width="8%;" class="text-right">INR Amount</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $pctr = 0;
                                $ctr2 = 0;

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

                                    $voucher  = $pmain['vouchno'];
                                    $visiting = $pmain['visitingfor'];
                                    $transinramt = $pmain['transinramt'];
                                    $transamt = $pmain['transamt'];
                                    $fullname = $pmain['surname'] . " " . $pmain['name'] . " " . $pmain['midname'];

                                    $pctr++;
                                    echo "<tr data-href='sale.php?mode=edit&vouchno=$voucher' class='clickable-row2' style='cursor: pointer;' [target]='_blank'>";
                                    echo "<td>$pmain[date]</td>";
                                    echo "<td>$voucher</td>";
                                    echo "<td>$visiting</td>";
                                    echo "<td>$fullname</td>";
                                    echo "<td class='text-right'>$transamt</td>";
                                    echo "<td class='text-right'>$transinramt</td>";
                                    echo "</tr>";
                                    $tottransamt = $tottransamt + $transamt;
                                    // $tottransinramt = $tottransamt + $transamt;
                                    $tottransinramt = $tottransinramt + $transinramt;

                                    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 "<thead>";
                                        echo "<tr class='' style='background-color:#BABFC2'>";
                                        echo "<th style='width:5%;'>Sr</th>";
                                        echo "<th style='width:10%;'>Type</th>";
                                        echo "<th style='width:10%;'>From</th>";
                                        echo "<th style='width:10%;'>To</th>";
                                        echo "<th style='width:10%;'>Pick Up Time</th>";
                                        echo "<th style='width:10%;'>Vehicle</th>";
                                        echo "<th style='width:5%;'>No. Pax</th>";
                                        echo "<th style='width:5%;'>No. Vehicle</th>";
                                        echo "<th style='width:10%;text-align:right'>Rate</th>";
                                        echo "<th style='width:10%;'>Amount</th>";
                                        echo "<th style='width:10%;'>CNF No.</th>";
                                        echo "<th style='width:10%;'>Description</th>";
                                        echo "</tr>";
                                        echo "</thead>";

                                        $totamt = $ctr2 = 0;

                                        $gettransdtl = mysqli_query($conn, "SELECT *,DATE_FORMAT(`pickuptime`, '%d-%m-%Y %r') as pickuptime 
                                                                            FROM `$trandb` WHERE vouchno ='$pmain[vouchno]'
                                                                            $fromplacecond2 $toplacecond2  ORDER BY srno") or die(mysqli_error($conn));

                                        while ($fettrans = mysqli_fetch_array($gettransdtl)) {

                                            if ($fettrans['picktype'] == "D") {
                                                $flttypen = "Departure";
                                            } elseif ($fettrans['picktype'] == "A") {
                                                $flttypen = "Arrival";
                                            } elseif ($fettrans['picktype'] == "I") {
                                                $flttypen = "Internal";
                                            } else {
                                                $flttypen = "";
                                            }

                                            $getveh = mysqli_query($conn, "SELECT `code`,`vehtype` FROM `vehicle` where code='$fettrans[vehicle]'") or die(mysqli_error($conn));
                                            $fetveh = mysqli_fetch_array($getveh);

        
                                            $ctr2++;
                                            echo "<tr>";
                                            echo "<td>$ctr2</td>";
                                            echo "<td>$flttypen</td>";
                                            echo "<td>$fettrans[fromplace]</td>";
                                            echo "<td>$fettrans[toplace]</td>";
                                            echo "<td>$fettrans[pickuptime]</td>";
                                            echo "<td>$fetveh[vehtype]</td>";
                                            echo "<td>$fettrans[nopax]</td>";
                                            echo "<td>$fettrans[noveh]</td>";
                                            echo "<td class='text-right'>$fettrans[rate]</td>";
                                            echo "<td class='text-right'>$fettrans[amount]</td>";
                                            echo "<td>$fettrans[descrip]</td>";
                                            echo "<td>$fettrans[cnfno]</td>";
                                            echo "</tr>";

                                            $totamt = $totamt + $fettrans['amount'];
                                        }
                                        echo "<tr style='background-color:#c7ecee;font-weight:bold;'>";
                                        echo "<td colspan='9' class='text-right'>Total</td>";
                                        echo "<td class='text-right ; font-bolder'>$totamt</td>";
                                        echo "<td></td>";
                                        echo "<td></td>";
                                        echo "</tr>";
                                        echo "</table>";
                                        echo "</td>";
                                        echo "</tr>";
                                    }

                                }

                                echo "<tr style='background-color: #98F9F0;color: red;'>";
                                echo "<td></td>";
                                echo "<td></td>";
                                echo "<td></td>";
                                echo "<td style='font-size:17px' class='text-right'><b>Total</b></td>";
                                echo "<td style='font-size:17px' class='text-right'>$tottransamt</td>";
                                echo "<td style='font-size:17px' class='text-right'>$tottransinramt</td>";
                                echo "</tr>";
                                ?>
                            </tbody>
                        </table>
                </div>
            </div>

        <?php
                    }
        ?>
        </div>
        <?php include("includes/linksfoot.php");
        ?>
    </body>

    </html>
<?php
}

if (isset($_POST["excel"]) || ($_GET["excel"])) {

$contents .="Date, Voucher, For, Pax Name, FE Amount, INR Amount\n";

    $pctr = 0;
    $ctr2 = 0;

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

        $voucher  = $pmain['vouchno'];
        $visiting = $pmain['visitingfor'];
        $transinramt = $pmain['transinramt'];
        $transamt = $pmain['transamt'];
        $fullname = $pmain['surname'] . " " . $pmain['name'] . " " . $pmain['midname'];

        $pctr++;
$contents .="$pmain[date]" . ",";
$contents .="$voucher" . ",";
$contents .="$visiting" . ",";
$contents .="$fullname" . ",";
$contents .="$transamt" . ",";
$contents .="$transinramt" . "\n";

        $tottransamt = $tottransamt + $transamt;
        $tottransinramt = $tottransinramt + $transinramt;

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

$contents .="Sr, Type, From, To, Pick Up Time, Vehicle, No. Pax, No. Vehicle, Rate, Amount, CNF No., Description\n";

            $totamt = $ctr2 = 0;

            $gettransdtl = mysqli_query($conn, "SELECT *,DATE_FORMAT(`pickuptime`, '%d-%m-%Y %r') as pickuptime 
                                                FROM `$trandb` WHERE vouchno ='$pmain[vouchno]'
                                                $fromplacecond2 $toplacecond2  ORDER BY srno") or die(mysqli_error($conn));

            while ($fettrans = mysqli_fetch_array($gettransdtl)) {

                if ($fettrans['picktype'] == "D") {
                    $flttypen = "Departure";
                } elseif ($fettrans['picktype'] == "A") {
                    $flttypen = "Arrival";
                } elseif ($fettrans['picktype'] == "I") {                    $flttypen = "Internal";
                } else {
                    $flttypen = "";
                }

                $getveh = mysqli_query($conn, "SELECT `code`,`vehtype` FROM `vehicle` where code='$fettrans[vehicle]'") or die(mysqli_error($conn));
                $fetveh = mysqli_fetch_array($getveh);


                $ctr2++;
                $contents .="$ctr2" . ",";
                $contents .="$flttypen" . ",";
                $contents .="$fettrans[fromplace]" . ",";
                $contents .="$fettrans[toplace]" . ",";
                $contents .="$fettrans[pickuptime]" . ",";
                $contents .="$fetveh[vehtype]" . ",";
                $contents .="$fettrans[nopax]" . ",";
                $contents .="$fettrans[noveh]" . ",";
                $contents .="$fettrans[rate]" . ",";
                $contents .="$fettrans[amount]" . ",";
                $contents .="$fettrans[descrip]" . ",";
                $contents .="$fettrans[cnfno]" . "\n";

                $totamt = $totamt + $fettrans['amount'];
            }
            $contents .=",,,,,,,,," . ",";
            $contents .="Total" . ",";
            $contents .="$totamt" . ",";
            $contents .="" . ",";
            $contents .="" . "\n";
        }

    }

    $contents .=",,,,,,,,," . ",";
    $contents .="Total" . ",";
    $contents .="$tottransamt" . ",";
    $contents .="$tottransinramt" . "\n";

    $contents = strip_tags($contents);
    if ($_POST["dtype"] == 1 || $_POST["dtype"] == 3) {
        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;
    }
}
?>