<?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 Payment - Datewise";
$file = "karigpay.php";

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

mysqli_select_db($conn, $zdb);

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

$custcond = '';
if (!empty($_POST['acnamemul'])) {
    foreach ($_POST['acnamemul'] as $check2) {
        $mycheck2[$count2++] .= "OR `accode` = '" . $check2 . "' ";
    }

    foreach ($mycheck2 as $g => $values2) {
        $mcheck2 .= $values2;
    }

    $custcond = " AND (`accode` = '' " . $mcheck2 . " )";
}
$accode = trim($_POST["acname"]);

if ($accode == '' && $custcond == '') {
    browse("Atleast select one A/c.");
    echo "<script>"
    . "window.close();"
    . "</script>";
}
if (!empty($_POST["acname"])) {

    $accond = "and accode = '$accode'";
}

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

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

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();
}

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

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


    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();
            }
        });

    </script>


    </head>

    <body id="ledger">   
        <div class="row" style="padding : 1%">
            <div class="col-lg-1"></div>
            <div class="col-lg-10">
                <?php include ("includes/head_rep.php"); ?>
                <!--            </div>
                            <div class="col-lg-8">-->

                <div class="box">
                    <div id="sortableTable" class="body collapse in">
                        <table class="table table-bordered sortableTable responsive-table">
                            <thead>
                                <?php
                                while ($getac = mysqli_fetch_array($getacc)) {
                                    ?>
                                    <tr>
                                        <th colspan="2">Karigar Name</th>
                                        <?php
                                        echo "<th colspan='5'>$getac[name]</th>";
                                        ?>
                                    </tr>
                                </thead>
                                <thead>
                                    <tr>
                                        <th style="width:5%">Sr.</th>
                                        <th style="width:10%">Date</th>
                                        <th style="width:5%">Source</th>
                                        <th style="width:8%">Voucher</th>
                                        <th  style="width:15%" class='text-right'>Cash</th>
                                        <th  style="width:15%" class='text-right'>Bank</th>
                                        <th  style="width:15%" class='text-right'>Transfer</th>
                                    </tr>
                                </thead>
                                <tbody class="tbody">

                                    <?php
                                    $totcash = $totbank = $tottrf = $mnctr = 0;
                                    $gettran = mysqli_query($conn, "SELECT * FROM `actran` WHERE (source = 'CR' or source = 'BR' or source = 'BRBP') and accode = '$getac[accode]'
                                                                    order by date, source, vouchno") or die(mysqli_error($conn));

                                    while ($fetacc = mysqli_fetch_array($gettran)) {
                                        $mnctr++;
                                        $date = $fetacc['date'];
                                        $vouchno = $fetacc['vouchno'];
                                        $source = $fetacc['source'];
                                        $amt = $fetacc['amount'];

                                        echo "<tr>";
                                        echo "<td>$mnctr</td>";
                                        echo "<td>$date</td>";
                                        echo "<td>$source</td>";
                                        echo "<td>$vouchno</td>";

                                        $amt1 = abs($amt);

                                        if ($source == 'CR') {
                                            echo "<td class='text-right'>" . number_format($amt1, 2) . "</td>";
                                            echo "<td> </td>";
                                            echo "<td> </td>";
                                            $totcash = $totcash + abs($amt1);
                                        } else {
                                            if ($source == 'BR') {
                                                echo "<td> </td>";
                                                echo "<td class='text-right'>" . number_format($amt1, 2) . "</td>";
                                                echo "<td> </td>";
                                                $totbank = $totbank + abs($amt1);
                                            } else {
                                                echo "<td> </td>";
                                                echo "<td> </td>";
                                                echo "<td class='text-right'>" . number_format($amt1, 2) . "</td>";
                                                $tottrf = $tottrf + abs($amt1);
                                            }
                                            echo "</tr>";
                                        }
                                    }
                                    echo "<tr>";
                                    echo "<td></td>";
                                    echo "<td></td>";
                                    echo "<td></td>";
                                    echo "<td>Total</td>";
                                    echo "<td class='text-right'><b style='font-size:15px;'>" . number_format($totcash, 2) . "</b></td>";
                                    echo "<td class='text-right'><b style='font-size:15px;'>" . number_format($totbank, 2) . "</b></td>";
                                    echo "<td class='text-right'><b style='font-size:15px;'>" . number_format($tottrf, 2) . "</b></td>";
                                    echo "</tr>";
                                    ?>
                                </tbody>
                            <?php } ?>
                        </table>    


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

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

    $acc = mysqli_query($conn, "SELECT * FROM `accmast` WHERE accode = '$accode' ") or die(mysqli_error($conn));
    $getac = mysqli_fetch_array($acc);

    $getopen = mysqli_query($conn, "Select itemcode, a.color, sum(a.qty) as qty, sum(a.amount) as tamt,  
                                   b.name as itemname from $trandb a, item b where itemcode = b.code and 
                                   a.vouchno in (select vouchno from $maindb where party = '$accode' and vouchdt >= '$mfrmdate' and vouchdt <= '$mtodate')
                                   group by a.itemcode, a.color order by b.name") or die(mysqli_error($conn));

    $contents .= "Karigar," . $getac['name'] . "\n";

    $contents .= "Sr,Item Name,color,Qty, Amount\n";

    $ctr = 1;

    while ($fetopen = mysqli_fetch_array($getopen)) {
        $mnctr++;
        $itemcode = $fetopen['item'];
        $colorcode = $fetopen['color'];
        $qty = $fetopen['qty'];
        $amt = $fetopen['tamt'];
        $itemname = $fetopen['itemname'];



        $contents .= "$ctr" . ",";
        $contents .= "$itemname" . ",";
        $contents .= "$colorname" . ",";
        $contents .= "$qty" . ",";
        $contents .= "$amt" . "," . "\n";


        $totleft = $totleft + abs($amt);
        $totqty = $totqty + abs($qty);

        $ctr++;
    }

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

