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

$maindb = "sale";
$formname = "Sale Register";
$file = "salereg_fr.php";

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

$fdate = $_POST["fdate"];
$tdate = $_POST["tdate"];
$customer = $_POST["customer"];
$type = $_POST["type"];

$custcond = "";
$typecond = "";


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

if ($type == "A") {
    $typecond = "AND a.`type` = 'A'";
} elseif ($type == "S") {
    $typecond = "AND a.`type` = 'S'";
}

if (($_GET["fdt"]) != '') {
    $mfrmdate = $_GET["fdt"];
    $mtodate = $_GET["tdt"];
} else {

    $mfrmdate = trim($_POST["fdate"]);
    $mtodate = trim($_POST["tdate"]);

    if (empty($_POST["fdate"])) {
        browse("From Date Cannot Be Empty");
        echo "<script>window.close()</script>";
    }
    if (empty($_POST["tdate"])) {
        browse("To Date Cannot Be Empty");
        echo "<script>window.close()</script>";
    }
}



$getpmain = mysqli_query($conn, "SELECT a.*, DATE_FORMAT(a.`vouchdt`, '%d-%m-%Y') as mydt,b.name as custname FROM `$maindb` a 
                                               LEFT JOIN accmast b ON a.party = b.accode    
                                               WHERE a.`vouchdt` >= '$mfrmdate' AND a.`vouchdt` <= '$mtodate' $custcond $typecond
                                               ORDER BY a.vouchno,a.vouchdt ASC") or die(mysqli_error($conn));
$get = mysqli_num_rows($getpmain);

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

    include("includes/linkshead.php");
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
    ?>
    <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_rep.php"); ?>
            <div class="row">
                <div class="col-lg-12 ">
                    <table style="background: white;width: 100%" class="table table-condensed table-bordered">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:5%">Voucher</th>
                                <th style="width:7%">Invoice No.</th>
                                <th style="width:6%">Date</th>
                                <th style="width:15%">Party</th>
                                <th style="width:4%">Type</th>
                                <th style="width:5%" class="text-right">Qty.</th>
                                <th style="width:14%">Description</th>
                                <th style="width:5%">Unit</th>
                                <th style="width:7%" class="text-right">Rate</th>
                                <th style="width:8%" class="text-right">Amount</th>
                                <th style="width:6%" class="text-right">ROE</th>
                                <th style="width:7%" class="text-right">Carr. Chgs.</th>
                                <th style="width:8%" class="text-right">Anti-dumping</th>
                                <th style="width:10%" class="text-right">Total</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;

                            $totqty = 0;
                            $totamt = 0;
                            $totroeamt = 0;
                            $totcarryamt = 0;
                            $totdumpamt = 0;
                            $gtotamt = 0;

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

                                $typeOptions = [
                                    'A' => 'Air',
                                    'S' => 'Sea'
                                ];
                                $typeCode = $pmain['type'];
                                $typeLabel = isset($typeOptions[$typeCode]) ? $typeOptions[$typeCode] : 'Unknown';

                                if ($pmain["type"] == "A") {
                                    $carryamt = $pmain["unittotal"];
                                } else {
                                    $carryamt = $pmain["carryamt"];
                                }


                                $ctr++;
                                echo "<tr id='boldfont' class='clickable-row2' data-href='sale.php?vouchno=$pmain[vouchno]&mode=edit' data-target='_blank'>";
                                echo "<td>$pmain[vouchno]</td>";
                                echo "<td>$pmain[invno]</td>";
                                echo "<td>$pmain[mydt]</td>";
                                echo "<td>$pmain[custname]</td>";
                                echo "<td>$typeLabel</td>";
                                echo "<td class='text-right'>$pmain[qty]</td>";
                                echo "<td>$pmain[descrip]</td>";
                                echo "<td>$pmain[unit]</td>";
                                echo "<td class='text-right'>$pmain[rate]</td>";
                                echo "<td class='text-right'>$pmain[amount]</td>";
                                echo "<td class='text-right'>$pmain[roeamt]</td>";
                                echo "<td class='text-right'>$carryamt</td>";
                                echo "<td class='text-right'>$pmain[dumpamt]</td>";
                                echo "<td class='text-right'>$pmain[netamt]</td>";
                                echo "</tr>";

                                $totqty += $pmain['qty'];
                                $totamt += $pmain['amount'];
                                $totroeamt += $pmain['roeamt'];
                                $totcarryamt += $carryamt;
                                $totdumpamt += $pmain['dumpamt'];
                                $gtotamt += $pmain['netamt'];

                            }

                            echo "<tr>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            // echo "<td></td>";
                            echo "<th class='text-right' style='color: red;' colspan='2'>Grand Total</th>";
                            echo "<th class='text-right' style='color: red;'>" . number_format($totqty, 2) . "</th>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<th class='text-right' style='color: red;'>" . number_format($totamt, 2) . "</th>";
                            echo "<th class='text-right' style='color: red;'>" . number_format($totroeamt, 2) . "</th>";
                            echo "<th class='text-right' style='color: red;'>" . number_format($totcarryamt, 2) . "</th>";
                            echo "<th class='text-right' style='color: red;'>" . number_format($totdumpamt, 2) . "</th>";
                            echo "<th class='text-right' style='color: red;'>" . number_format($gtotamt, 2) . "</th>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

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

    </html>
    <?php
}

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

    $contents .= "Voucher, Invoice No., Date, Party, Type, Qty., Description, Unit, Rate, Amount, ROE, Carr. Chgs., Anti-dumping, Total \n";

    $ctr = 0;
    $totqty = 0;
    $totamt = 0;
    $totroeamt = 0;
    $totcarryamt = 0;
    $totdumpamt = 0;
    $gtotamt = 0;

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

        $typeOptions = [
            'A' => 'Air',
            'S' => 'Sea'
        ];
        $typeCode = $pmain['type'];
        $typeLabel = isset($typeOptions[$typeCode]) ? $typeOptions[$typeCode] : 'Unknown';

        if ($pmain["type"] == "A") {
            $carryamt = $pmain["unittotal"];
        } else {
            $carryamt = $pmain["carryamt"];
        }


        $ctr++;

        $contents .= "$pmain[vouchno]" . ",";
        $contents .= "$pmain[invno]" . ",";
        $contents .= "$pmain[mydt]" . ",";
        $contents .= "$pmain[custname]" . ",";
        $contents .= "$typeLabel" . ",";
        $contents .= "$pmain[qty]" . ",";
        $contents .= "$pmain[descrip]" . ",";
        $contents .= "$pmain[unit]" . ",";
        $contents .= "$pmain[rate]" . ",";
        $contents .= "$pmain[amount]" . ",";
        $contents .= "$pmain[roeamt]" . ",";
        $contents .= "$carryamt" . ",";
        $contents .= "$pmain[dumpamt]" . ",";
        $contents .= "$pmain[netamt]" . "\n";

        $totqty += $pmain['qty'];
        $totamt += $pmain['amount'];
        $totroeamt += $pmain['roeamt'];
        $totcarryamt += $carryamt;
        $totdumpamt += $pmain['dumpamt'];
        $gtotamt += $pmain['netamt'];

    }

    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "Grand Total" . ",";
    $contents .= "$totqty" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "$totamt" . ",";
    $contents .= "$totroeamt" . ",";
    $contents .= "$totcarryamt" . ",";
    $contents .= "$totdumpamt" . ",";
    $contents .= "$gtotamt" . "\n";


    $contents = strip_tags($contents);

    header("Content-Disposition: attachment; filename=$formname " . "" . date('d-m-Y') . ".csv");
    print $contents;

}
?>