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

$maindb = "visamain";
$trandb = "visatran";
$trandb1 = "category";
$formname = "Register";
$file = "register_fr.php";
// $mgrpcode = "010";
$entfile = "entry.php";

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

//    $name = $_POST[name];
//    $manualno = $_POST["manualno"];
$frmdate = $_POST["frmdate"];
$todate = $_POST["todate"];
//$plbirthr = $_POST["plbirth"];

$manualno = $_POST["manualno"];
$visatype = $_POST["visatype"];
$visano = $_POST["visano"];
$passport = $_POST["passport"];
$partyname = $_POST["partyname"];
$sponser = $_POST["sponser"];
$contact = $_POST["contact"];
$mobile = $_POST["mobile"];
$agent = $_POST["agent"];
$paidby = $_POST["paidby"];
$dtype = $_POST["dtype"];

$manualnocond = '';
$visatypecond = '';
$visanocond = '';
$passportcond = '';
$partynamecond = '';
$sponsercond = '';
$contactcond = '';
$mobilecond = '';
$agentcond = '';
$dtypecond = "";
$paidbycond = "";
// $mtypecond = "";

if (!empty($manualno)) {
    $manualnocond = " AND a.`manualno`='$manualno' ";
}
if (!empty($paidby)) {
    $paidbycond = " AND a.`paidby`='$paidby' ";
}
if (!empty($passport)) {
    $passportcond = " AND b.`passport` ='$passport' ";
}
if (!empty($partyname)) {
    $partynamecond = " AND a.`partyname` LIKE '$partyname%' ";
}
if (!empty($sponser)) {
    $sponsercond = " AND a.`sponser` LIKE '$sponser%' ";
}
if (!empty($visano)) {
    $visanocond = " AND a.`visano` = '$visano' ";
}
if (!empty($visatype)) {
    $visatypecond = " AND a.`visatype` = '$visatype' ";
}
if (!empty($contact)) {
    $contactcond = " AND a.`contact` = '$contact' ";
}
if (!empty($mobile)) {
    $mobilecond = " AND a.`mobile` = '$mobile' ";
}
if (!empty($agent)) {
    $agentcond = " AND a.`broker` = '$agent' ";
}
if (empty($_POST["frmdate"])) {
    browse("From Date Cannot Be Empty");
    header("refresh:1; register_sc.php");
}
if (empty($_POST["todate"])) {
    browse("To Date Cannot Be Empty");
    header("refresh:1; register_sc.php");
}



$mfrmdate = trim($_POST["frmdate"]);
$mtodate = trim($_POST["todate"]);

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

//    browse($partynamecond);
$getpmain = mysqli_query($conn, "SELECT a.*, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as vouchdt, DATE_FORMAT(a.`hijridate`, '%d-%m-%Y')
                            as hijridt, b.`vouchno`, b.`passport`,b.`paxname`, b.`memcash` FROM `$maindb` a left join `$trandb` b on 
                            a.vouchno = b.vouchno and a.visatype = b.visatype where a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` <= '$mtodate'
                            $visatypecond $passportcond $manualnocond $visanocond $partynamecond $sponsercond $contactcond $mobilecond $paidbycond $agentcond
                            ") or die(mysqli_error($conn));
//print table($getpmain);
//die();
$get = mysqli_num_rows($getpmain);
if (isset($_POST["view"])) {
    include("includes/linkshead.php");
?>
    </head>
    <style>
        @media print {
            a[href]:after {
                content: none !important;
            }
        }

        .tblhead>th {
            background-color: #F5F5F5;
            color: #C45254;
            width: 10%;
        }

        /*        @media print{
                                .pgbrk{
                                    page-break-before: always;
                                }
                }
                * {
                    box-sizing: border-box;
                    -moz-box-sizing: border-box;
                }
                .page {
                    width: 210mm;
                    min-height: 160mm;
                    padding: 4mm;
                    margin: 10mm auto;
                    border: 1px #D3D3D3 solid;
                    border-radius: 5px;
                    background: white;
        
                }
                .subpage {
                    padding: 0cm;
                    border: 1px solid;
                    height: 288mm;
                    outline: 2cm #ffffff solid;
                }
        
                @page {
                    size: A4;
                    margin: 0;
                }
                @media print {
                    html, body {
                        width: 210mm;
                        height: 297mm;
                    }
                    .page {
                        margin: 0;
                        border: initial;
                        border-radius: initial;
                        width: initial;
                        min-height: initial;
                        box-shadow: initial;
                        background: initial;
                        page-break-after: always;
                    }
                }
                .brkpg{
                    page-break-after: always;
                }*/
    </style>

    <body>
        <div class="container">
            <?php include("includes/head_rep.php"); ?>

            <?php
            //            if ($_POST["dtype"] == 1) {
            $ctr = 1;
            $ctr2 = 0;
            ?>
            <!--<div class="page">-->
            <div class="row">
                <div class="col-lg-12 pad">
                    <table class="table table-condensed table-bordered">
                        <tr class="mySearchTr">
                            <th class="">Sr</th>
                            <!--<th class="">Voucher</th>-->
                            <th class="">Date</th>
                            <th class="">File No</th>
                            <th class="">Agent</th>
                            <th class="">Paidby</th>
                            <th class="">Pax Name</th>
                            <th class="">Visa No</th>
                            <th class="">Passport No</th>
                            <th class="text-right">Mofa</th>
                            <th class="text-right">Stamping</th>
                            <!--<th>Hijri</th>-->
                            <th class="text-right">Immigration</th>
                            <th class="text-right">M/s.</th>
                            <th class="text-right">Misc.</th>
                            <th class="text-right">Total</th>
                            <th class="text-right">Discount</th>
                            <th class="text-right">Final Amt.</th>
                        </tr>
                    <?php
                    $ftot = 0;
                    $mistot = 0;
                    $misctot = 0;
                    $stot = 0;
                    $moftot = 0;
                    $itot = 0;
                    $ttot = 0;
                    $dtot = 0;
                    //                        $ftot = 0;

                    while ($pmain = mysqli_fetch_array($getpmain)) {
                        $imig = $pmain['imig'];
                        $stamp = $pmain['stamp'];
                        $mis = $pmain['mis'];
                        $mofano = $pmain['mofa'];
                        $misc = $pmain['misc'];
                        $total = $pmain['total'];
                        $disc = $pmain['disc'];
                        $tot = $pmain['tot'];
                        $memcash = $pmain['memcash'];
                        $paidby = $pmain['paidby'];
                        $newpaid = [
                            'C' => 'Cash',
                            'R' => 'Credit',
                            'U' => 'UPI / RTGS',
                        ];
                        $getpaid = $newpaid[$paidby];

                        echo "<tr class='clickable-row' data-href='$entfile?mode=edit&b.vouchno='$pmain[vouchno]' [target]='_blank'>";
                        echo "<td>$ctr</td>";
                        //                                Echo "<td>$pmain[visatype] $pmain[vouchno]</td>";
                        echo "<td>$pmain[vouchdt]</td>";
                        echo "<td><a target='_blank' href='entry.php?mode=edit&myvouch=$pmain[myvouch]'>$pmain[manualno]</a></td>";
                        echo "<td>$pmain[broker]</td>";
                        echo "<td>$getpaid</td>";
                        echo "<td>$pmain[paxname]</td>";
                        echo "<td>$pmain[visano]</td>";
                        echo "<td>$pmain[passport]</td>";
                        //                                Echo "<td>$pmain[hijridt]</td>";

                        echo "<td class='text-right'>$pmain[mofa]</td>";
                        echo "<td class='text-right'>$pmain[stamp]</td>";
                        echo "<td class='text-right'>$pmain[imig]</td>";
                        echo "<td class='text-right'>$pmain[mis]</td>";

                        echo "<td class='text-right'>$pmain[misc]</td>";
                        echo "<td class='text-right'>$memcash</td>";
                        echo "<td class='text-right'>$pmain[disc]</td>";
                        echo "<td class='text-right'>$pmain[tot]</td>";
                        echo "</tr>";

                        $ftot = $ftot + $tot;
                        $mistot = $mistot + $mis;
                        $misctot = $misctot + $misc;
                        $moftot = $mofano + $moftot;
                        $stot = $stamp + $stot;
                        $ttot = $ttot + $total;
                        $dtot = $dtot + $disc;
                        $itot = $itot + $imig;
                        $ctr++;
                    }
                    echo "<tr>";
                    //                            Echo "<td></b></td>";
                    echo "<td></b></td>";
                    echo "<td></b></td>";
                    echo "<td></b></td>";
                    echo "<td></b></td>";
                    echo "<td></b></td>";
                    echo "<td class='text-right'></b></td>";
                    echo "<td></b></td>";

                    echo "<td class='text-right text-danger'><b>Total</b></td>";
                    echo "<td class='text-right text-danger'><b>$moftot</b></td>";
                    echo "<td class='text-right text-danger'><b>$stot</b></td>";
                    echo "<td class='text-right text-danger'><b>$itot</b></td>";

                    echo "<td class='text-right text-danger'><b>$mistot</b></td>";

                    echo "<td class='text-right text-danger'><b>$misctot</b></td>";
                    echo "<td class='text-right text-danger'><b>$ttot</b></td>";
                    echo "<td class='text-right text-danger'><b>$dtot</b></td>";
                    echo "<td class='text-right text-danger'><b>$ftot</b></td>";
                    echo "<td><b></b></th>";

                    echo "</tr>";

                    echo "</table>";
                    echo "</td>";
                    echo "</tr>";
                }
                if (isset($_POST["excel"])) {

                    $ctr = 1;
                    $ctr2 = 0;

                    $contents .= "Sr.,Date, File No, Agent, Paidby, Pax Name, Visa No, Passport No, Mofa, Stamping, Immigration, M/s., Misc, Total, Discount, Final Amt. \n ";

                    $ftot = 0;
                    $mistot = 0;
                    $misctot = 0;
                    $stot = 0;
                    $moftot = 0;
                    $itot = 0;
                    $ttot = 0;
                    $dtot = 0;

                    while ($pmain = mysqli_fetch_array($getpmain)) {
                        $imig = $pmain['imig'];
                        $stamp = $pmain['stamp'];
                        $mis = $pmain['mis'];
                        $mofano = $pmain['mofa'];
                        $misc = $pmain['misc'];
                        $total = $pmain['total'];
                        $disc = $pmain['disc'];
                        $tot = $pmain['tot'];
                        $memcash = $pmain['memcash'];
                        $paidby = $pmain['paidby'];
                        $newpaid = [
                            'C' => 'Cash',
                            'R' => 'Credit',
                            'U' => 'UPI / RTGS',
                        ];
                        $getpaid = $newpaid[$paidby];

                        $contents .= "$ctr" . ",";
                        $contents .= "$pmain[vouchdt]" . ",";
                        $contents .= "$pmain[manualno]" . ",";
                        $contents .= "$pmain[broker]" . ",";
                        $contents .= "$getpaid" . ",";
                        $contents .= "$pmain[paxname]" . ",";
                        $contents .= "$pmain[visano]" . ",";
                        $contents .= "$pmain[passport]" . ",";
                        $contents .= "$pmain[mofa]" . ",";
                        $contents .= "$pmain[stamp]" . ",";
                        $contents .= "$pmain[imig]" . ",";
                        $contents .= "$pmain[mis]" . ",";
                        $contents .= "$pmain[misc]" . ",";
                        $contents .= "$memcash" . ",";
                        $contents .= "$pmain[disc]" . ",";
                        $contents .= "$pmain[tot]" . "\n";

                        $ftot = $ftot + $tot;
                        $mistot = $mistot + $mis;
                        $misctot = $misctot + $misc;
                        $moftot = $mofano + $moftot;
                        $stot = $stamp + $stot;
                        $ttot = $ttot + $total;
                        $dtot = $dtot + $disc;
                        $itot = $itot + $imig;
                        $ctr++;
                    }

                    $contents .= "" . ",,,,,,,";
                    $contents .= "Total" . ",";
                    $contents .= "$moftot" . ",";
                    $contents .= "$stot" . ",";
                    $contents .= "$itot" . ",";
                    $contents .= "$mistot" . ",";
                    $contents .= "$misctot" . ",";
                    $contents .= "$ttot" . ",";
                    $contents .= "$dtot" . ",";
                    $contents .= "$ftot" . ",";
                    $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;
                    }
                }
