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

$maindb = "purmain";
$trandb = "purtran";

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"])) {


    $acc = mysqli_query($conn, "SELECT * FROM `accmast` WHERE grpcode = '009' and active = '1'  $accond $custcond ") 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($acc)) {
                                    ?>
                                    <tr>
                                        <th align="left" colspan="5">Karigar Name :- <b><?php echo $getac['name']; ?></b></th>
                                    </tr>
                                </thead>
                                <tbody>



                                    <tr>
                                        <th style="width:5%">Sr No.</th>
                                        <th style="width:40%">Item</th>
                                        <th style="width:10%" class='text-right'>Color</th>
                                        <th style="width:10%" class='text-right'>Qty</th>
                                        <th  style="width:15%" class='text-right'>Amount</th>
                                    </tr>
                                    </thead>
                                <tbody class="tbody">

                                    <?php
                                    $totright = $totqty = $totleft = $mnctr = $mnctr2 = $rnctr = $rnctr2 = 0;
                                    $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 = '$getac[accode]'
                                                                    and vouchdt >= '$mfrmdate' and vouchdt <= '$mtodate')
                                                                    group by a.itemcode, a.color order by b.name") or die(mysqli_error($conn));

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

                                        $getcol = mysqli_query($conn, "SELECT name FROM `color` WHERE code = '$colorcode' ") or die(mysqli_error($conn));
                                        $fetcol = mysqli_fetch_array($getcol);
                                        $colorname = $fetcol['name'];

                                        echo "<tr>";
                                        echo "<td>$mnctr</td>";
                                        echo "<td>$itemname</td>";
                                        echo "<td>$colorname</td>";
                                        echo "<td class='text-right'>" . number_format($qty, 2) . "</td>";
                                        echo "<td class='text-right'>" . number_format($amt, 2) . "</td>";
                                        echo "</tr>";
                                        $totleft = $totleft + abs($amt);
                                        $totqty = $totqty + abs($qty);
                                    }
                                    echo "<tr>";
                                    echo "<td></td>";
                                    echo "<td class='text-right'><b style='font-size:15px;'>Total</b></td>";
                                    echo "<td></td>";
                                    echo "<td class='text-right'><b style='font-size:15px;'>" . number_format($totqty, 2) . "</b></td>";
                                    echo "<td class='text-right'><b style='font-size:15px;'>" . number_format($totleft, 2) . "</b></td>";
                                    echo "</tr>";
                                    ?>

                                </tbody>
                            <?php } ?>
                        </table>    


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

if (isset($_POST["excel"])) {
    $accode = $_POST[acname];

    $mfrmdate = $_POST["frmdt"];
    $mtodate = $_POST["todt"];
    $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'];

        $getcol = mysqli_query($conn, "SELECT name FROM `color` WHERE code = '$colorcode' ") or die(mysqli_error($conn));
        $fetcol = mysqli_fetch_array($getcol);
        $colorname = $fetcol['name'];

        $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;
    }
}
?>

