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

$maindb = "accmain";
$trandb = "acctran";
//$trandb1 = "category";
$formname = "Accounts Register";
$file = "acc_reg_fr.php";
// $mgrpcode = "010";
$entfile = "acc_ent.php";
$keycond = "";
$keyfld = "myvouch";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);
mysqli_query($conn, "set names 'utf8'");

$frmdate = $_POST["frmdate"];
$todate = $_POST["todate"];

$comp = $_POST["comp"];
$dtype = $_POST["dtype"];

$compcond = '';

if (!empty($comp)) {
    $compcond = " AND a.`customer` = '$comp' ";
}

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

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

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

$getmain = mysqli_query($conn, "SELECT a.*, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt, b.name as custname FROM $maindb a, `accmast` b WHERE a.`vouchno` != '' AND a.customer=b.accode $compcond") or die(mysqli_error($conn));

// $gdate = mysqli_query($conn, "SELECT a.*,DATE_FORMAT(a.`appdt`, '%d-%m-%Y') as shdt FROM `$trandb` a LEFT JOIN $maindb b ON a.vouchno=b.vouchno  WHERE a.appdt != '0000-00-00' AND a.`appdt` >= '$mfrmdate' AND a.`appdt` <= '$mtodate' $agcond ORDER BY appdt") or die(mysqli_error($conn));

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

    include("includes/linkshead.php");
?>
    </head>
    <style>
        .tblhead>th {
            background-color: #F5F5F5;
            color: #C45254;
        }
    </style>

    <body>
        <form action='' method='POST'>
            <div class="container" style="width:80%">
                <?php
                include("includes/head_rep.php");
                ?>

                <div class="row">
                    <div class="col-lg-12 pad">
                        <table class="table table-condensed table-bordered">
                            <tr class="mySearchTr">
                                <th class="" style="width: 3%">Sr</th>
                                <th class="" style="width: 9%">Invoice No</th>
                                <th class="" style="width: 12%">Date</th>
                                <th class="" colspan="3" style="width: 9%">Company / Customer</th>
                                <th class="" style="width: 7%">Amount</th>
                            </tr>
                            <?php
                            $ttotamt = 0;
                            while ($pmain = mysqli_fetch_array($getmain)) {
                                echo "<tr class='clickable-row2' data-href='$entfile?myvouch=$myvouch&mode=edit' target='_blank' style='$bgcolr'>";
                                echo "<td>$ctr</td>";
                                echo "<td>$pmain[vouchno]</td>";
                                echo "<td>$pmain[mydt]</td>";
                                echo "<td colspan='3'>$pmain[custname]</td>";
                                echo "<td class='text-right'>$pmain[totamt]</td>";
                                echo "</tr>";

                                if ($dtype == 2) {
                                    echo "<tr class='mySearchTr' style='background-color:#BABFC2;'>";
                                    echo "<th style='width: 3%'>Sr</th>";
                                    echo "<th style='width: 9%'>Passport</th>";
                                    echo "<th style='width: 12%'>Paxname</th>";
                                    echo "<th style='width: 9%' class='text-right'>Charges</th>";
                                    echo "<th style='width: 9%'>Remark</th>";
                                    echo "<th style='width: 9%'>Flight No</th>";
                                    echo "<th style='width: 9%'>Flight Date</th>";
                                    echo "</tr>";

                                    $gettran = mysqli_query($conn, "SELECT *,DATE_FORMAT(`fltdt`, '%d-%m-%Y') as fltdt FROM `$trandb` where vouchno=$pmain[vouchno] ORDER BY srl") or die(mysqli_error($conn));
                                    $ctr = 1;
                                    $ctr2 = 0;
                                    $totamt = 0;
                                    while ($tran = mysqli_fetch_array($gettran)) {

                                        echo "<tr class='clickable-row2' data-href='$entfile?myvouch=$myvouch&mode=edit' target='_blank' style='$bgcolr'>";
                                        echo "<td>$ctr</td>";
                                        echo "<td>$tran[passport]</td>";
                                        echo "<td>$tran[paxname]</td>";
                                        echo "<td class='text-right'>$tran[charges]</td>";
                                        echo "<td>$tran[remark]</td>";
                                        echo "<td>$tran[fltno]</td>";
                                        echo "<td>$tran[fltdt]</td>";
                                        echo "</tr>";
                                        $ctr++;
                                        $ctr2++;
                                        $totamt += $tran['charges'];
                                    }
                                    echo "<tr class='clickable-row2' style='background-color:#c7ecee;font-weight:bold;' data-href='$entfile?myvouch=$myvouch&mode=edit' target='_blank'>";
                                    echo "<td colspan='4' class='text-right'>$totamt</td>";
                                    echo "<td colspan='3'></td>";
                                    echo "</tr>";
                                }
                                $ttotamt += $pmain['totamt'];
                            }
                            echo "<tr style='background-color: #98F9F0;color: red;'>";
                            echo "<td colspan='9' class='text-right' style='height:20px'>$ttotamt</td>";
                            echo "<tr>";
                            ?>
                        </table>
                    </div>
                </div>
            </div>
        </form>

    </body>
<?php
}

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

    $contents .= "Sr., Invoice No., Date, Company / Customer, Amount \n";

    $ttotamt = 0;
    while ($pmain = mysqli_fetch_array($getmain)) {

        $contents .= "$ctr" . ",";
        $contents .= "$pmain[vouchno]" . ",";
        $contents .= "$pmain[mydt]" . ",";
        $contents .= "$pmain[custname]" . ",";
        $contents .= "$pmain[totamt]" . "\n";

        if ($dtype == 2) {

            $contents .= "Sr., Passport, Paxname, Charges, Remark, Flight No, Flight Date \n";

            $gettran = mysqli_query($conn, "SELECT *,DATE_FORMAT(`fltdt`, '%d-%m-%Y') as fltdt FROM `$trandb` where vouchno=$pmain[vouchno] ORDER BY srl") or die(mysqli_error($conn));
            $ctr = 1;
            $ctr2 = 0;
            $totamt = 0;
            while ($tran = mysqli_fetch_array($gettran)) {

                $contents .= "$ctr" . ",";
                $contents .= "$tran[passport]" . ",";
                $contents .= "$tran[paxname]" . ",";
                $contents .= "$tran[charges]" . ",";
                $contents .= "$tran[remark]" . ",";
                $contents .= "$tran[fltno]" . ",";
                $contents .= "$tran[fltdt]" . "\n";
                $ctr++;
                $ctr2++;
                $totamt += $tran['charges'];
            }
            $contents .= "" . ",,,";
            $contents .= "$totamt" . "\n";
        }
        $ttotamt += $pmain['totamt'];
    }
    $contents .= "" . ",,,,";
    $contents .= "$ttotamt" . "\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;
    }
}
