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

$maindb = "pax";
$formname = 'Laundry Purchase Register';
$file = "laundrypurchreg_fr.php";

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


$mfrmdate = $_POST["fromdt"];
$mtodate = $_POST["todt"];


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

$getpmain = mysqli_query($conn, "SELECT *,DATE_FORMAT(`date`, '%d-%m-%Y') AS date FROM $maindb
                                 WHERE  `date` >= '$mfrmdate' AND `date` <= '$mtodate' 
                                 AND laundpuramt > 0 ") or die(mysqli_error($conn));

if (isset($_POST["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">
            <?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-hover">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:6%">Voucher</th>
                                <th style="width:6%">Date</th>
                                <th style="width:7%;">For</th>
                                <th style="width:20%;">Pax</th>
                                <th style="width:18%;">Supplier</th>
                                <th style="width:7%;">Invoice No.</th>
                                <th style="width:12%;">Invoice Date</th>
                                <th style="width:5%;text-align:right">Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ctr = 0;
                            $totlaundrypuramt = 0;
                            
                            while ($pmain = mysqli_fetch_array($getpmain)) {

                                $getsupp = mysqli_query($conn, "SELECT accode,name FROM accmast WHERE accode = '$pmain[laundsupp]' ") or die(mysqli_error($conn));
                                $fetsupp = mysqli_fetch_array($getsupp);

                                $laundinvdt = date('d-m-Y', strtotime($pmain['laundinvdt']));
                                $fullname = $pmain['surname'] ." ". $pmain['name'] ." ". $pmain['midname'] ;


                                $ctr++;
                                echo "<tr id='boldfont' class='clickable-row2' data-href='fit_ent.php?vouchno=$pmain[vouchno]&mode=edit' data-target='_blank'>";
                                echo "<td>$pmain[vouchno]</td>";
                                echo "<td>$pmain[date]</td>";
                                echo "<td>$pmain[visitingfor]</td>";
                                echo "<td>$fullname</td>";
                                echo "<td>$fetsupp[name]</td>";
                                echo "<td>$pmain[laundinvno]</td>";
                                echo "<td>$laundinvdt</td>";
                                echo "<td style='text-align:right;'>$pmain[laundpuramt]</td>";
                                echo "</tr>";
                                
                                $totlaundpuramt = $pmain['laundpuramt'] + $totlaundpuramt;
                                $tlaundpuramt = number_format($totlaundpuramt, 2);
                            }

                            echo "<tr id='boldfont'>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td style='text-align:right;font-size:14px;font-weight:bold'>Total</td>";
                            echo "<td style='text-align:right;font-size:14px;font-weight:bold'>$tlaundpuramt</td>";
                            echo "</tr>";

                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

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

    </html>
<?php
}
if (isset($_POST["excel"])) {

    $contents .= "Voucher,Date,For,Pax,Supplier,Invoice No.,Invoice Date,Amount\n";

    $ctr = 0;
    $totlaundpuramt = 0;
    
    while ($pmain = mysqli_fetch_array($getpmain)) {

        $getsupp = mysqli_query($conn, "SELECT accode,name FROM accmast WHERE accode = '$pmain[laundsupp]' ") or die(mysqli_error($conn));
        $fetsupp = mysqli_fetch_array($getsupp);

        $laundinvdt = date('d-m-Y', strtotime($pmain['laundinvdt']));
        $fullname = $pmain['surname'] ." ". $pmain['name'] ." ". $pmain['midname'] ;


        $ctr++;

        $contents .= "$pmain[vouchno]" . ",";
        $contents .= "$pmain[date]" . ",";
        $contents .= "$pmain[visitingfor]" . ",";
        $contents .= "$fullname" . ",";
        $contents .= "$fetsupp[name]" . ",";
        $contents .= "$pmain[laundinvno]" . ",";
        $contents .= "$laundinvdt" . ",";
        $contents .= "$pmain[laundpuramt]" . "\n";
     
        $totlaundpuramt = $pmain['laundpuramt'] + $totlaundpuramt;
    }

    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "Total" . ",";
    $contents .= "$totlaundpuramt" . "\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;
    }
}
?>