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

$zdb = $_SESSION["zdbyear_" . $dbname];
$admin = $_SESSION["admin_" . $dbname];

$formname = "Karigar Advance  / Balance Report";
$file = "karadvbal.php";

$maindb = "accmast";
$trandb = "actran";

mysqli_select_db($conn, $zdb);

$gcond = "";
$accond = "";
$stcond = "";
$ctcond = "";

if (!empty($_POST["acname"])) {
    $accode = trim($_POST["acname"]);
    $accond = " and accode = '$accode' ";
}

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

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

//    if ($mfrmdate > $mtodate) {
//        browse("To Date Cannot Be Less Than From Date");
//        header("refresh: 1; karigrep.php");
//        exit();
//    }

$scond = '';
$mscope = $_POST["scope"];
//if ($mscope == '0') {
//    $scond = '' ;   
//} else {
//  if ($mscope == '1') {
//      $scond = 'A' ;   
//  }  else {
//      $scond = 'B' ;   
//  }
//}

$getacc = mysqli_query($conn, "SELECT * FROM `accmast` WHERE grpcode = '009' and active = '1'  $accond order by name") or die(mysqli_error($conn));

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

    include("includes/linkshead.php");
    $mfdt = date("d-m-Y", strtotime($mfrmdate));
    $mtdt = date("d-m-Y", strtotime($mtodate));
    ?>
    <style>
        table th{
            background-color: #337AB7;
        }
    </style>
    <script type="text/javascript">
        $(document).ready(function () {
            //        left side
            $(".dtlsale").hide();
            $(".dtlsale2").hide();
            $("body").on('dblclick', '.getdtl', getDetails);
            $("body").on('dblclick', '.getdtl2', getDetails2);
            $("body").on('dblclick', '.dtlhide1', hideDetails);
            $("body").on('dblclick', '.dtlhide2', hideDetails2);
            function getDetails() {
                var trid = $(this).closest('tr').attr('id');
                var idval = trid.split('_')[1];
                $("#dtlsale_" + idval).show();
            }
            function getDetails2() {
                var trid = $(this).closest('tr').attr('id');
                var idval = trid.split('_')[1];
                $("#dtlsale2_" + idval).show();
            }
            function hideDetails() {
                var trid = $(this).closest('tr').attr('id');
                var idval = trid.split('_')[1];
                $("#dtlsale_" + idval).hide();
            }
            function hideDetails2() {
                var trid = $(this).closest('tr').attr('id');
                var idval = trid.split('_')[1];
                $("#dtlsale2_" + idval).hide();
            }

            //        right side
            $(".rdtlsale").hide();
            $(".rdtlsale2").hide();
            $("body").on('dblclick', '.rgetdtl', getDetailsr);
            $("body").on('dblclick', '.rgetdtl2', getDetailsr2);
            $("body").on('dblclick', '.rdtlhide1', hideDetailsr);
            $("body").on('dblclick', '.rdtlhide2', hideDetailsr2);
            function getDetailsr() {
                var trid = $(this).closest('tr').attr('id');
                var idval = trid.split('_')[1];
                $("#rdtlsale_" + idval).show();
            }
            function getDetailsr2() {
                var trid = $(this).closest('tr').attr('id');
                var idval = trid.split('_')[1];
                $("#rdtlsale2_" + idval).show();
            }
            function hideDetailsr() {
                var trid = $(this).closest('tr').attr('id');
                var idval = trid.split('_')[1];
                $("#rdtlsale_" + idval).hide();
            }
            function hideDetailsr2() {
                var trid = $(this).closest('tr').attr('id');
                var idval = trid.split('_')[1];
                $("#rdtlsale2_" + idval).hide();
            }

            $('#create_excel').click(function () {
                var excel_data = $('.tablerep').html();
                var page = "excellib.php?data=" + excel_data.trim();
                console.log(page);
                var data = {excel_data: excel_data};
                window.open('data:application/vnd.ms-excel,' + encodeURIComponent(excel_data));
            });
        });

    </script>


    </head>

    <body id="ledger">   
        <div class="tablerep">
            <div class="col-lg-12 pad text-right">
                <button id="create_excel" name="create_excel" style="color: #26A69A;background: white; border: 1px black solid" type="button" class="btn btn-danger"><span style="text-align: right;">&nbsp;&nbsp;Excel&nbsp;&nbsp;</span></button>
            </div>

            <div class="row" style="padding : 1%">
                <div class="col-lg-1"></div>
                <div class="col-lg-8">
                    <?php include ("includes/head_rep.php"); ?>
                    <div class="box">
                        <div id="sortableTable" class="body collapse in">
                            <table class="table table-bordered sortableTable responsive-table">
                                <thead>
                                    <tr>
                                        <th style="width:5%">Sr.</th>
                                        <th style="width:40%">Karigar Name</th>
                                        <?php if ($mscope == 0) { ?>
                                            <th  style="width:15%" class='text-right'>Balance</th>
                                            <th  style="width:15%" class='text-right'>Advance</th>
                                            <?php
                                        }
                                        if ($mscope == 1) {
                                            ?>
                                            <th  style="width:15%" class='text-right'>Balance</th>
                                        <?php
                                        }
                                        if ($mscope == 2) {
                                            ?>
                                            <th  style="width:15%" class='text-right'>Advance</th>
                                        <?php }
                                        ?>
                                    </tr>
                                </thead>
                                <tbody class="tbody">
                                    <?php
                                    $totadv = $totbal = $mnctr = 0;

                                    while ($fetacc = mysqli_fetch_array($getacc)) {
                                        $accode = $fetacc['accode'];
                                        $acname = $fetacc['name'];
                                        $openbal = $fetacc['openbal'];
                                        $opendc = $fetacc['dc'];

                                        if ($opendc == 'D') {
                                            $mopbal = $openbal * -1;
                                        } else {
                                            $mopbal = $openbal;
                                        }

                                        $getcol = mysqli_query($conn, "SELECT sum(amount) as amt FROM `actran` WHERE accode = '$accode' and date <= '$mtodate' ") or die(mysqli_error($conn));
                                        $fetcol = mysqli_fetch_array($getcol);
                                        $amt = $fetcol['amt'];

                                        $amt2 = $amt + $mopbal;

                                        $amt1 = abs($amt2);

                                        if ($mscope == 0) {
                                            $mnctr++;
                                            echo "<tr>";
                                            echo "<td>$mnctr</td>";
                                            echo "<td>$acname</td>";

                                            if ($amt2 > 0) {
                                                echo "<td class='text-right'>" . number_format($amt1, 2) . "</td>";
                                                echo "<td> </td>";
                                                $totadv = $totadv + abs($amt1);
                                            } else {
                                                echo "<td> </td>";
                                                echo "<td class='text-right'>" . number_format($amt1, 2) . "</td>";
                                                $totbal = $totbal + abs($amt1);
                                            }
                                            echo "</tr>";
                                        }

                                        if ($mscope == 1) {
                                            if ($amt2 > 0) {
                                                $mnctr++;
                                                echo "<tr>";
                                                echo "<td>$mnctr</td>";
                                                echo "<td>$acname</td>";

                                                echo "<td class='text-right'>" . number_format($amt1, 2) . "</td>";
                                                $totadv = $totadv + abs($amt1);
                                                echo "</tr>";
                                            }
                                        }

                                        if ($mscope == 2) {
                                            if ($amt2 < 0) {
                                                $mnctr++;
                                                echo "<tr>";
                                                echo "<td>$mnctr</td>";
                                                echo "<td>$acname</td>";

                                                echo "<td class='text-right'>" . number_format($amt1, 2) . "</td>";
                                                $totbal = $totbal + abs($amt1);
                                                echo "</tr>";
                                            }
                                        }
                                    }

                                    echo "<tr>";
                                    echo "<td></td>";
                                    echo "<td></td>";
                                    if ($mscope == 0) {
                                        echo "<td class='text-right'><b style='font-size:15px;'>" . number_format($totadv, 2) . "</b></td>";
                                        echo "<td class='text-right'><b style='font-size:15px;'>" . number_format($totbal, 2) . "</b></td>";
                                    }
                                    if ($mscope == 1) {
                                        echo "<td class='text-right'><b style='font-size:15px;'>" . number_format($totadv, 2) . "</b></td>";
                                    }
                                    if ($mscope == 2) {
                                        echo "<td class='text-right'><b style='font-size:15px;'>" . number_format($totbal, 2) . "</b></td>";
                                    }
                                    echo "</tr>";
                                    ?>

                                </tbody>
                            </table>    


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

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

    $contents .= "Sr,Karigar,Advance,Balance\n";

    $ctr = 1;

    while ($fetacc = mysqli_fetch_array($getacc)) {
        $mnctr++;
        $accode = $fetacc['accode'];
        $acname = $fetacc['name'];
        $openbal = $fetacc['openbal'];
        $opendc = $fetacc['dc'];

        if ($opendc == 'D') {
            $mopbal = $openbal * -1;
        } else {
            $mopbal = $openbal;
        }

        $getcol = mysqli_query($conn, "SELECT sum(amount) as amt FROM `actran` WHERE accode = '$accode' and date <= '$mtodate' ") or die(mysqli_error($conn));
        $fetcol = mysqli_fetch_array($getcol);
        $amt = $fetcol['amt'];

        $contents .= "$mctr" . ",";
        $contents .= "$acname" . ",";



        $amt2 = $amt + $mopbal;


        $amt1 = abs($amt2);
        if ($amt2 > 0) {
            $contents .= "$amt1" . ",,";


            $totadv = $totadv + abs($amt1);
        } else {
            $contents .= ",$amt1" . ",";

            $totbal = $totbal + abs($amt1);
        }
        $contents .= "\n";
    }


    $contents .= ",Total" . ",";
    $contents .= "$totadv" . ",";
    $contents .= "$totbal" . "\n";


    $contents = strip_tags($contents);

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

