<?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"];

$file = 'challanreg_fr.php';
$maindb = 'chalmain';
$trandb = 'chaltran';
$party = $_POST["party"];
$vessel = $_POST["vessel"];
$ordby = $_POST["ordby"];
$anchor = $_POST["anchor"];
$dtype = $_POST["dtype"];
$ftype = $_POST["ftype"];

$partycond = $vesselcond = $ordcond = $anchcond = "";

if (!empty($party)) {
    $partycond = " AND a.`party` = '$party' ";
}
if (!empty($vessel)) {
    $vesselcond = " AND a.`vessel`= '$vessel'";
}
if (!empty($ordby)) {
    $ordcond = " AND a.`ordby`= '$ordby'";
}
if (!empty($anchor)) {
    $anchcond = " AND a.`anchor`= '$anchor'";
}

$formname = 'Challan Register';

if (!empty($dtype)) {
    $dtypecond = " AND `dtype`= '$dtype' ";
}

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; inwreg.php");
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; inwreg.php");
    }
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
}

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">
            <?php

            include("includes/head_rep2.php");

            $getpmain2 = mysqli_query($conn, "SELECT a.*, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') AS mydt, a.amount as chalamt,
            b.*, GROUP_CONCAT(DISTINCT f.name SEPARATOR '<br> ') AS launchnames,
            GROUP_CONCAT(DISTINCT DATE_FORMAT(b.frmdt, '%d-%m-%Y') SEPARATOR '<br> ') AS fromdates,
            GROUP_CONCAT(DISTINCT DATE_FORMAT(b.todt, '%d-%m-%Y') SEPARATOR '<br> ') AS todates,
            c.name AS partyname, e.name AS portname 
            FROM `$maindb` a 
            LEFT JOIN `$trandb` b ON b.vouchno = a.vouchno
            LEFT JOIN accmast c ON c.accode = a.party
            LEFT JOIN port e ON e.code = a.anchor
            LEFT JOIN launch f ON f.code = b.launch
            WHERE a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` <= '$mtodate' $partycond $vesselcond $ordcond $anchcond
            GROUP BY a.vouchno ORDER BY a.vouchdt") or die(mysqli_error($conn));

            $getno2 = mysqli_num_rows($getpmain2);

            if ($getno2 > 0) {
            ?>
                <div class="row">
                    <div class="col-lg-12 pad">
                        <table class="table table-condesed table-bordered">
                            <tr class="mySearchTr">
                                <!--9221292520-->
                                <th width="100%" class="h4" style="color:white"><b><?= $formname ?></b></th>
                            </tr>
                            <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="3%;">Sr</th>
                                    <th width="5%;">Timesheet</th>
                                    <th width="8%;">Date</th>
                                    <th width="6%;">From Hours</th>
                                    <th width="6%;">To Hours</th>
                                    <th width="6%;">Total Hours</th>
                                    <th width="15%;">Launch</th>
                                    <th width="7%;">Challan No</th>
                                    <th width="20%;">Party</th>
                                    <th width="10%;">Vessel</th>
                                    <th width="10%;">Anchor</th>
                                    <th width="10%;">Purpose</th>
                                    <?php if ($ftype != 1) { ?>
                                        <th width="10%;" class="text-right">Total</th>
                                    <?php } ?>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $pctr = $mtotamt = 0;


                                while ($pmain = mysqli_fetch_array($getpmain2)) {
                                    $totalhrs = $pmain['nohrs'] . " . " . $pmain['nomin'];
                                    $voucher = $pmain['vouchno'];
                                    $mydt = $pmain['mydt'];
                                    $manualno = $pmain['manualno'];
                                    $timesheetno = $pmain['timesheetno'];
                                    $partyname = $pmain['partyname'];
                                    $vesselname = $pmain['vessel'];
                                    $launchnames = $pmain['launchnames'];
                                    $portname = $pmain['portname'];
                                    $frmtime = $pmain['frmtime'];
                                    $totime = $pmain['totime'];
                                    $purpose = $pmain['purpose'] . $pmain['purpose2'];
                                    $purpose2 = $pmain['purpose3'] . $pmain['purpose4'];
                                    $amount = $pmain['chalamt'];

                                    $pctr++;
                                    echo "<tr data-href='challan_new.php?mode=edit&vouchno=$voucher' class='clickable-row2' [target]='_blank'>";
                                    echo "<td>$pctr</td>";
                                    echo "<td>$timesheetno</td>";
                                    echo "<td>$mydt</td>";
                                    echo "<td>$frmtime</td>";
                                    echo "<td>$totime</td>";
                                    echo "<td>$totalhrs</td>";
                                    echo "<td>$launchnames</td>"; // Display concatenated launch names
                                    echo "<td>$manualno</td>";
                                    echo "<td>$partyname</td>";
                                    echo "<td>$vesselname</td>";
                                    echo "<td>$portname</td>";
                                    echo "<td>$purpose $purpose2</td>";
                                    if ($ftype != 1) {
                                        echo "<td class='text-right'>$amount</td>";
                                    }
                                    echo "</tr>";

                                    $mtotamt += $pmain['chalamt'];
                                }

                                echo "<tr style='background-color: #98F9F0'>";
                                if ($ftype != '1') {
                                    echo "<td class='text-right' colspan='12'>Total</td>";
                                    echo "<td class='numeric text-right'><b>" . number_format($mtotamt, 2) . "</b></td>";
                                } else {
                                    echo "<td class='text-right' colspan='12'></td>";
                                }
                                echo "</tr>";
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>

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

    </html>
<?php
}

if (isset($_POST["excel"]) || ($_GET["excel"])) {
    
    $getpmain2 = mysqli_query($conn, "SELECT a.*, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') AS mydt, a.amount as chalamt,
            b.*, GROUP_CONCAT(DISTINCT f.name SEPARATOR '<br> ') AS launchnames,
            GROUP_CONCAT(DISTINCT DATE_FORMAT(b.frmdt, '%d-%m-%Y') SEPARATOR '<br> ') AS fromdates,
            GROUP_CONCAT(DISTINCT DATE_FORMAT(b.todt, '%d-%m-%Y') SEPARATOR '<br> ') AS todates,
            c.name AS partyname, e.name AS portname 
            FROM `$maindb` a 
            LEFT JOIN `$trandb` b ON b.vouchno = a.vouchno
            LEFT JOIN accmast c ON c.accode = a.party
            LEFT JOIN port e ON e.code = a.anchor
            LEFT JOIN launch f ON f.code = b.launch
            WHERE a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` <= '$mtodate' $partycond $vesselcond $ordcond $anchcond
            GROUP BY a.vouchno ORDER BY a.vouchdt desc") or die(mysqli_error($conn));

            $getno2 = mysqli_num_rows($getpmain2);

            if ($getno2 > 0) {
    
    
                $contents .= "Sr., Timesheet, Date, From Hours, To Hours, Total Hours, Launch, Challan No, Party, Vessel, Anchor, Purpose,";
                                    
                                     if ($ftype != 1) { 
                                       $contents .= "Total";
                                    } 
                                    $contents .= "\n";

                                $pctr = $mtotamt = 0;


                                while ($pmain = mysqli_fetch_array($getpmain2)) {
                                    $totalhrs = $pmain['nohrs'] . " . " . $pmain['nomin'];
                                    $voucher = $pmain['vouchno'];
                                    $mydt = $pmain['mydt'];
                                    $manualno = $pmain['manualno'];
                                    $timesheetno = $pmain['timesheetno'];
                                    $partyname = $pmain['partyname'];
                                    $vesselname = $pmain['vessel'];
                                    $launchnames = $pmain['launchnames'];
                                    $portname = $pmain['portname'];
                                    $frmtime = $pmain['frmtime'];
                                    $totime = $pmain['totime'];
                                    $purpose = $pmain['purpose'] . $pmain['purpose2'];
                                    $purpose2 = $pmain['purpose3'] . $pmain['purpose4'];
                                    $amount = $pmain['chalamt'];

                                    $pctr++;
                                    $contents .= "$pctr" . ",";
                                    $contents .= "$timesheetno" . ",";
                                    $contents .= "$mydt" . ",";
                                    $contents .= "$frmtime" . ",";
                                    $contents .= "$totime" . ",";
                                    $contents .= "$totalhrs" . ",";
                                    $contents .= "$launchnames" . ",";
                                    $contents .= "$manualno" . ",";
                                    $contents .= "$partyname" . ",";
                                    $contents .= "$vesselname" . ",";
                                    $contents .= "$portname" . ",";
                                    $contents .= "$purpose" . "$purpose2" . ",";
                                    if ($ftype != 1) {
                                        $contents .= "$amount" . ",";
                                        
                                    }
                                    $contents .= "" . "\n";

                                    $mtotamt += $pmain['chalamt'];
                                }

                                if ($ftype != '1') {
                                    
                                    $contents .= "" . ",,,,,,,,,,,";
                                    $contents .= "Total" . ",";
                                    $contents .= "$mtotamt" . "\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;
                                }
            } else {
                browse("No Records Found");
            }

}
?>