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

$formname = 'Rent Item Register';
$maindb = 'rentmain';
$trandb = 'renttran';

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


$party = $_POST["party"];
$einv = $_POST["einv"];
$partycond = "";
$einvcond = "";

if (!empty($_POST["party"])) {
    $partycond = "AND `party` = '$party'";
}

if ($einv == 1){
    $einvcond = "AND `irn` != '' ";
}elseif ($einv == 2){
    $einvcond = "AND `irn` = '' ";
}else{
    $einvcond = "";
}


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


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

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

$getdata = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE
                                `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' $partycond  ORDER BY vouchno ") or die(mysqli_error($conn));

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

    include("includes/linkshead.php");
?>
    <link href="MyStyle/rep_format.css" type="text/css">
    <style type="text/css">
        #tabital {
            font-style: italic;
        }

        #font1 {
            font-style: italic;
        }

        #boldfont {
            font-weight: bold;
        }

        .mtotclr {
            color: #0098DA;
        }

        .ttotclr {
            color: #ed3237;
        }
    </style>
    </head>

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

            <div class="row">
                <div class="col-lg-12 pad">
                    <table style="background: white;width: 100%" class="table table-condensed table-bordered table-hover">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width:2%">Voucher</th>
                                <th style="width:7%">Date</th>
                                <th style="width:23%">Name</th>
                                <th style="width:12%">No. Of Days</th>
                                <th style="width:10%" class="text-right">Total Amount</th>
                                <th style="width:7%" class="text-right">Advance</th>
                                <th style="width:7%" class="text-right">Balance</th>
                                <th style="width:20%">Remarks</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                        

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

                                $ctr++;
                                echo "<tr class='clickable-row2' data-href='billentry.php?mode=edit&vouchno=$pmain[vouchno]'>";
                                echo "<td>$pmain[vouchno]</td>";
                                echo "<td>$pmain[mydt]</td>";
                                echo "<td>$pmain[name]</td>";
                                echo "<td>$pmain[days]</td>";
                                echo "<td class='text-right'>$pmain[billamt]</td>";
                                echo "<td class='text-right'>$pmain[advance]</td>";
                                echo "<td class='text-right'>$pmain[bal]</td>";
                                echo "<td>$pmain[remark1] $pmain[remark2]</td>";
                                echo "</tr>";
                                $totamt = $totamt + $pmain['amount'];
                                $totdiscamt = $totdiscamt + $totdisc;
                                $totvatp = $totvatp + $pmain['vatperc'];
                                $totvatamt = $totvatamt + $pmain['vat'];
                                $totbamt = $totbamt + $pmain['billamt'];



                                if ($_POST["dtype"] == 2) {

                                    echo "<tr style='height:10px;'>";
                                    echo "<td colspan='25'>";
                                    echo "<table style='width:100%;' id='tabital' class='table table-condensed table-hover table-bordered'>";

                                    echo "<thead>";
                                    echo "<tr style='background:grey'>";
                                    echo "<th style='width:5%;'>Sr</th>";
                                    echo "<th style='width:10%;'>Itemname</th>";
                                    echo "<th style='width:5%;'>Qty.</th>";
                                    echo "<th style='width:7%;' class='text-right'>Rate</th>";
                                    echo "<th style='width:10%;' class='text-right'>Amount</th>";
                                    echo "</tr>";
                                    echo "<thead>";
                                    echo "<tr>";


                                    $ctr2 = 0;
                                    $totqty = 0;
                                    $totrate = 0;
                                    $totamount = 0;

                                    $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]' order by srno") or die(mysqli_error($conn));
                                    while ($tran = mysqli_fetch_array($gettran)) {

                                        $gitem = mysqli_query($conn, "SELECT * FROM `rentitems` where code = '$tran[item]'") or die(mysqli_error($conn));
                                        $item = mysqli_fetch_array($gitem);
                                        $itemname = $item['name'];

                                        $ctr2++;
                                        echo "<tr>";
                                        echo "<td>$tran[srno]</td>";
                                        echo "<td>$itemname</td>";
                                        // echo "<td>$tran[description]</td>";
                                        echo "<td class='text-right'>$tran[qty]</td>";
                                        echo "<td class='text-right'>$tran[rate]</td>";
                                        echo "<td class='text-right'>$tran[amount]</td>";
                                        echo "</tr>";

                                        $totqty = $totqty + $tran['qty'];
                                        $totrate = $totrate + $tran['rate'];
                                        $totamount = $totamount + $tran['amount'];
                                        $totcgstamt = $totcgstamt + $tran['cgstamt'];
                                        $totsgstamt = $totsgstamt + $tran['sgstamt'];
                                        $totigstamt = $totigstamt + $tran['igstamt'];
                                        $tottotal = $tottotal + $tran['total'];
                                    }
                                    echo "<tr>";
                                    echo "<td></td>";
                                    echo "<td></td>";
                                    echo "<td class='ttotclr text-right'>$totqty</td>";
                                    echo "<td class='ttotclr text-right'>$totrate</td>";
                                    echo "<td class='ttotclr text-right'>$totamount</td>";

                                    echo "</tr>";
                                    echo "</table>";
                                    $gtotamt += $totamount;
                                }
                            }

                            echo "<tr>";
                            echo "<td ></td>";
                            echo "<td ></td>";
                            echo "<td ></td>";
                            echo "<td ></td>";
                            echo "<td ></td>";
                            echo "<td ></td>";
                            echo "<td class='text-right mtotclr' ><b> Total </b></td>";
                            echo "<td class='text-right mtotclr' ><b> $gtotamt    </b></td>";
                            echo "</tr>";
                            ?>

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

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

    </html>
<?php
}
if (isset($_POST["excel"])) {
    $contents .= "Voucher,Date,Name,No. Of Days,Total Amount,Advance,Balance,Remarks\n";

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

        $ctr++;
        $contents .= "$pmain[vouchno]" . ",";
        $contents .= "$pmain[mydt]" . ",";
        $contents .= "$pmain[name]" . ",";
        $contents .= "$pmain[days]" . ",";
        $contents .= "$pmain[billamt]" . ",";
        $contents .= "$pmain[advance]" . ",";
        $contents .= "$pmain[bal]" . ",";
        $contents .= "$pmain[remark1] $pmain[remark2]" . "\n";
      
        $totamt = $totamt + $pmain['amount'];
        $totdiscamt = $totdiscamt + $totdisc;
        $totvatp = $totvatp + $pmain['vatperc'];
        $totvatamt = $totvatamt + $pmain['vat'];
        $totbamt = $totbamt + $pmain['billamt'];
        
        if ($_POST["dtype"] == 2) {

            $contents .= "Sr,Itemname,Qty,Rate,Amount\n";
        
            $ctr2 = 0;
            $totqty = 0;
            $totrate = 0;
            $totamount = 0;

            $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$pmain[vouchno]' order by srno") or die(mysqli_error($conn));
            while ($tran = mysqli_fetch_array($gettran)) {

                $gitem = mysqli_query($conn, "SELECT * FROM `rentitems` where code = '$tran[item]'") or die(mysqli_error($conn));
                $item = mysqli_fetch_array($gitem);
                $itemname = $item['name'];

                $ctr2++;
                $contents .= "$tran[srno]" . ",";
                $contents .= "$itemname" . ",";
                $contents .= "$tran[qty]" . ",";
                $contents .= "$tran[rate]" . ",";
                $contents .= "$tran[amount]" . "\n";

                $totqty = $totqty + $tran['qty'];
                $totrate = $totrate + $tran['rate'];
                $totamount = $totamount + $tran['amount'];
                // $totcgstamt = $totcgstamt + $tran['cgstamt'];
                // $totsgstamt = $totsgstamt + $tran['sgstamt'];
                // $totigstamt = $totigstamt + $tran['igstamt'];
                // $tottotal = $tottotal + $tran['total'];
            }
            
            $contents .= "" . ",";
            $contents .= "" . ",";
            // $contents .= "Total" . ",";
            $contents .= "$totqty" . ",";
            $contents .= "$totrate" . ",";
            $contents .= "$totamount" . "\n";

            $gtotamt += $totamount;

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