<?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 Material Given ";
$grid = "karigrep.php";
$file = "karigrep.php";

$maindb = "rawmain";
$trandb = "rawtran";

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="6">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 Name</th>
                                        <th style="width:10%" class='text-right'>Yard</th>
                                        <th style="width:10%" class='text-right'>Meter</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 = $totleft = $mnctr = $mnctr2 = $rnctr = $rnctr2 = 0;
                                    $getopen = mysqli_query($conn, "Select a.item, sum(a.yard) as yard, sum(a.meters) as meters, 
                                                                    sum(a.qty) as qty, sum(a.amount) as tamt,  
                                                                    b.name as itemname from $trandb a, rawmat b where a.item = b.code and 
                                                                    a.vouchno in (select vouchno from $maindb where party = '$getac[accode]' and 
                                                                    vouchdt >= '$mfrmdate' and vouchdt <= '$mtodate')
                                                                    group by item order by b.name") or die(mysqli_error($conn));

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


                                        echo "<tr>";
                                        echo "<td>$mnctr</td>";
                                        echo "<td>$itemname</td>";
                                        echo "<td class='text-right'>" . number_format($yard, 2) . "</td>";
                                        echo "<td class='text-right'>" . number_format($meters, 2) . "</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);
                                        $totyard = $totyard + abs($yard);
                                        $totmeters = $totmeters + abs($meters);
                                        $totqty = $totqty + abs($qty);
                                    }
                                    echo "<tr>";
                                    echo "<td></td>";
                                    echo "<td class='text-right'><b style='font-size:15px;'>Total</b></td>";
                                    echo "<td class='text-right'><b style='font-size:15px;'>" . number_format($totyard, 2) . "</b></td>";
                                    echo "<td class='text-right'><b style='font-size:15px;'>" . number_format($totmeters, 2) . "</b></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 a.item, sum(a.yard) as yard, sum(a.meters) as meters, sum(a.qty) as qty, sum(a.amount) as tamt,  
                                   b.name as itemname from $trandb a, rawmat b where a.item = b.code and 
                                   a.vouchno in (select vouchno from $maindb where party = '$accode' and vouchdt >= '$mfrmdate' and vouchdt <= '$mtodate')
                                   group by item order by b.name") or die(mysqli_error($conn));

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

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

    $ctr = 1;

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


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


        $totleft = $totleft + abs($amt);
        $totyard = $totyard + abs($yard);
        $totmeters = $totmeters + abs($meters);
        $totqty = $totqty + abs($qty);

        $ctr++;
    }

    $contents .= ",Total" . ",";

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

