<?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 Report";
$grid = "karigrep2.php";
$file = "karigrep2.php";
$maindb = "accmast";

mysqli_select_db($conn, $zdb);


$trandb = "actran";
$srcond = "";
$branch = trim($_POST["series"]);
$spltype = $_SESSION["spltype_" . $dbname];
if ($spltype != 1) {
    if (!empty($branch)) {
        $srcond = "and `series`='$branch'";
    }
}

if (isset($_POST["view"])) {
    $gcond = "";
    $accond = "";
    $stcond = "";
    $ctcond = "";

    $dtype = $_POST["dtype"];

    $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 = trim($_POST["frmdt"]);
    $mtodate = trim($_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();
    }

    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>
    <?php
    if ($dtype == 1) {
        ?>
<!--        <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>-->
    <?php } ?>


    </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">
                        <tr class="mySearchTr">
                            <th width="5%;">Sr</th>
                            <th width="30%;">Karigar</th>
                            <th width="20%;" class='text-right'>Payment</th>
                            <th width="20%;" class='text-right'>Good Received</th>
                            </tr>
                        <?php
                        $ctr = 1;
                        $totamt = 0;
                        
                        mysqli_query($conn, "drop temporary table if exists accdbf") or die(mysqli_error($conn));
                        mysqli_query($conn, "create temporary table accdbf 
                                                    SELECT * FROM `$maindb` WHERE accode != '' and active = '1'  $accond $custcond") or die(mysqli_error($conn));
                        
                        mysqli_query($conn, "drop temporary table if exists paydbf") or die(mysqli_error($conn));
                        mysqli_query($conn, "create temporary table paydbf 
                                                    SELECT accode, sum(amount) as pamt FROM `actran` WHERE amount < 0
                                                    and date >= '$mfrmdate' and date <= '$mtodate' and accode in ( select accode from accdbf) 
                                                    group by accode ") or die(mysqli_error($conn));
                        
                        mysqli_query($conn, "drop temporary table if exists matgdbf") or die(mysqli_error($conn));
                        mysqli_query($conn, "create temporary table matgdbf 
                                                    SELECT accode, sum(amount) as pamt FROM `actran` WHERE source = 'SLM'
                                                    and date >= '$mfrmdate' and date <= '$mtodate' and accode in ( select accode from accdbf) 
                                                    group by accode ") or die(mysqli_error($conn));
                        
                        mysqli_query($conn, "drop temporary table if exists goodsdbf") or die(mysqli_error($conn));
                        mysqli_query($conn, "create temporary table goodsdbf 
                                                    SELECT accode, sum(amount) as pamt FROM `actran` WHERE amount > 0 
                                                    and date >= '$mfrmdate' and date <= '$mtodate' and accode in ( select accode from accdbf) 
                                                    group by accode ") or die(mysqli_error($conn));
                        
                        $getacc = mysqli_query($conn, "SELECT * FROM `accdbf` order by name") or die(mysqli_error($conn));
                        
                        
                        $totpay = $totmatg = $totgoods = 0 ;

                        while ($pmain = mysqli_fetch_array($getacc)) {
                            $accode = $pmain['accode'] ;
                            
                            $getp = mysqli_query($conn, "SELECT * FROM paydbf where accode = '$accode' ") or die(mysqli_error($conn));
                            $fetp = mysqli_fetch_array($getp);
                            $pamt = $fetp['pamt'] * -1;
                            
//                            $getm = mysqli_query($conn, "SELECT * FROM matgdbf where accode = '$accode' ") or die(mysqli_error($conn));
//                            $fetm = mysqli_fetch_array($getm);
//                            $mamt = $fetm['pamt'] * -1;
//                            $pmamt = $pamt + $mamt ;

                            $getg = mysqli_query($conn, "SELECT * FROM goodsdbf where accode = '$accode' ") or die(mysqli_error($conn));
                            $fetg = mysqli_fetch_array($getg);
                            $gamt = $fetg['pamt'] ;
                             
                            Echo "<tr>";
                            Echo "<td>$ctr</td>";
                            Echo "<td>$pmain[name]</td>";
                            Echo "<td class='text-right'>$pamt</td>";
                            Echo "<td class='text-right'>$gamt</td>";
                            Echo "</tr>";
                            $ctr++;

                            $totpay = $totpay + $pamt ;
//                            $totmat = $totmat + $mamt ;
                            $totgoods = $totgoods + $gamt ;
                            
                        }
                            Echo "<tr style='background:grey; color:white '>";
                            Echo "<td></td>";
                            Echo "<td class='text-right'>Total</td>";
                            Echo "<td class='text-right'>$totpay</td>";
                            Echo "<td class='text-right'>$totgoods</td>";
                            Echo "</tr>";
                        
                        
                        ?>
                    </table>
                </div>
            </div>
        </div>

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

