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

$maindb = "saleamt";
$trandb = "";
$formname = "Sale Amt Register";
$file = "saleamtreg.php";


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

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



$dtypecond = "";
$partycond = "";

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

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


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

$getpmain = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as `mydt`  FROM `$maindb` WHERE
                           `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate'   $partycond ") or die(mysqli_error($conn));
$mfdt = date("d-m-Y", strtotime($mfrmdate));
$mtdt = date("d-m-Y", strtotime($mtodate));
if (isset($_POST["view"])) {

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

        #font1 {
            font-style: italic;
        }

        #boldfont {
            font-weight: bold;
        }

        .dtype {
            background-color: #cccccc;
        }

        @media print {

            .table-bordered>thead>tr>th {
                border: 1px solid black !important;
            }

            .table-bordered>tbody>tr>td {
                border: 1px solid black !important;
            }

            .table-bordered>tbody>tr>th {
                border: 1px solid black !important;
            }
        }
    </style>
    </head>

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

            <div class="row">
                <div class="col-lg-12 pad">
                    <table style="background: white;width: 100%"
                        class="table table-striped table-condensed table-bordered table-hover">
                        <thead class="mySearchTr">
                            <tr>
                                <th width='5%'>Sr</th>
                                <th width='12%'>Voucher No.</th>
                                <th width='12%'>Date</th>
                                <th width='5%'>Invoice No</th>
                                <th width='30%'>Party Name</th>
                                <th width='5%' class="text-right">Amount</th>

                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            $ttotqty = 0;
                            $tsubtotal = 0;
                            $tlitemdisc = 0;
                            $tpackchgs = 0;
                            $ttotgst5 = 0;
                            $ttotgst12 = 0;
                            $ttotgst18 = 0;
                            $ttotgst28 = 0;
                            $ttotamt = 0;
                            $ttotalamount = 0;
                            $ttcsamt = 0;
                            $ttottaxamt = 0;

                            while ($pmain = mysqli_fetch_array($getpmain)) {
                                $getsupp = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$pmain[party]'") or die(mysqli_error($conn));
                                $supp = mysqli_fetch_array($getsupp);
                                $partyname = $supp['name'];
                                $anetamt = $pmain['amount'];
                                $ctr++;
                                echo "<tr id='boldfont'>";
                                echo "<td>$ctr</td>";
                                echo "<td>$pmain[vouchno]</td>";
                                echo "<td>$pmain[mydt]</td>";
                                echo "<td>$pmain[recpno]</td>";
                                echo "<td>$partyname</td>";
                                echo "<td class='text-right'>$anetamt</td>";
                                echo "</tr>";


                                $ttotamt = $ttotamt + $anetamt;
                            }

                            $colr = "green";
                            echo "<tr> ";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td></td>";
                            echo "<td colspan='2' align='right' style='color:$colr;'><h4><b>Grand Total</b></h4></td>";
                            echo "<td style='color:$colr' class='numeric'><h5><b></b></h5></td>";
                            echo "</tr>";
                            echo "</tr>";

                            echo "<tr> ";
                            echo "<td></td>";
                            echo "<td colspan='2' align='right' style='color:$colr;'><h4><b></b></h4></td>";
                            echo "<td colspan='3' style='color:$colr' class='numeric'><h4><b>$ttotamt</b></h4></td>";
                            echo "</tr>";
                            echo "</tr>";
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>

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

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

    include("includes/head_excel_rep.php");

    $contents .= "Sr, Voucher No., Date, Invoice No, Party Name, Amount\n";

    $ttotqty = 0;
    $tsubtotal = 0;
    $tlitemdisc = 0;
    $tpackchgs = 0;
    $ttotgst5 = 0;
    $ttotgst12 = 0;
    $ttotgst18 = 0;
    $ttotgst28 = 0;
    $ttotamt = 0;
    $ttotalamount = 0;
    $ttcsamt = 0;
    $ttottaxamt = 0;

    while ($pmain = mysqli_fetch_array($getpmain)) {
        $getsupp = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$pmain[party]'") or die(mysqli_error($conn));
        $supp = mysqli_fetch_array($getsupp);
        $partyname = $supp['name'];
        $anetamt = $pmain['amount'];

        $ctr++;
        $contents .= "$ctr" . ",";
        $contents .= "$pmain[vouchno]" . ",";
        $contents .= "$pmain[mydt]" . ",";
        $contents .= "$pmain[recpno]" . ",";
        $contents .= "$partyname" . ",";
        $contents .= "$anetamt" . "\n";

        $ttotamt = $ttotamt + $anetamt;
    }

    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "" . ",";
    $contents .= "Grand Total" . ",";
    $contents .= "" . "\n";

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