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


$zdb = $_SESSION["zdbyear_" . $dbname];
$admin = $_SESSION["admin_" . $dbname];
mysqli_select_db($conn, $zdb);

$formname = 'Karigar Register';
$maindb = 'outrmain';
$trandb = 'outrtran';

$grid = 'outrawreg.php';

if (isset($_POST["view"])) {
    include ('includes/function.php');
    $cmbparty = $_POST[cmbparty];
    $pcond = "";
    $scond = "";

    if (!empty($_POST["cmbparty"])) {
        $pname = trim($_POST["cmbparty"]);
        $pcond = "AND `accode` = '$pname'";
    }

    if (empty($_POST["frmdt"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; outrawreg.php");
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; outrawreg.php");
    }


    $mfrmdate = trim($_POST["frmdt"]);
    $mtodate = trim($_POST["todt"]);

    $get = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE
                       `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' $pcond $scond") or die(mysqli_error($conn));
    //  print "SELECT *, DATE_FORMAT(`billdate`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE
    //                     `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' $pcond $scond";

    $getno = mysqli_num_rows($get);
    include("includes/files.php");
    ?>

    </head>
    <body>

        <?php
        require("includes/dbconnect.php");
        $comp = mysqli_query($conn, "SELECT * FROM `company`") or die(mysql_errno());
        $zcomp = mysqli_fetch_assoc($comp);
        echo "<font size='4px'><b>$zcomp[compname]</font></b></font>";
        echo "<font size='2px'><b> $zcomp[add1]. $zcomp[add2].  $zcomp[add3] </b></font><br />";
        echo "Report Date : " . date("d-m-Y");
        echo "&nbsp;&nbsp;&nbsp;&nbsp;(From Date : " . date("d-m-Y", strtotime($mfrmdate)) . " &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  To Date : " . date("d-m-Y", strtotime($mtodate)) . ")" . "<br>";
        echo "Total Records : $getno";
        echo "<br />";
        echo "</font>";
        ?>

        <div class="row">
            <div class="col-lg-6" style="width:100%;">
                <div class="box">
                    <header>
                        <h5><?php echo $formname; ?></h5>
                        <div class="toolbar">
                            <div class="btn-group">		  
                                <div class="btn-group">

                                    <a href="#sortableTable" data-toggle="collapse" class="btn btn-default btn-sm minimize-box">
                                        <i class="fa fa-angle-up"></i>
                                    </a>

                                    <input type='button' name='submit' class='btn btn-sm btn-danger' onClick="window.location.href = '<?php echo $grid; ?>'" value="Exit"  value='Save'/> 
                                </div>
                            </div>
                        </div>
                    </header>
                    <div id="sortableTable" class="body collapse in">
                        <!--<table class="table table-bordered sortableTable responsive-table">-->
                        <table style="width:100%" class="table-bordered sortableTable responsive-table">    
                            <thead>
                                <tr style="height:10px">
                                    <th align="left" width='10%'>Invoice No.<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                    <th align="left" width='10%'>Date<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                    <th align="left" width='40%'>karigar<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                    <th align="left" width='10%'>Godown<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                    <th align="right" width='10%' style="text-align: right;">Amount<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                    <!--<th align="right" width='10%' style="text-align: right;">Discount<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>-->
                                    <th align="right" width='10%' style="text-align: right;">Bill Amount<i class="icon-sort"></i><i class="icon-sort-down"></i> <i class="icon-sort-up"></i></th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                $totamt = 0;
                                $billamt = 0;
                                $tdisc = 0;
                                $tvat = 0;
                                $tpack = 0;

                                $ctr = 0;
                                while ($row = mysqli_fetch_object($get)) {
                                    mysqli_select_db($conn, $zdb);
                                    $getparty = mysqli_query($conn, "SELECT * FROM `accmast` WHERE  `accode`='$row->accode' ORDER BY  `name`") or die(mysqli_error($conn));
                                    $party = mysqli_fetch_object($getparty);

                                    $getgodown = mysqli_query($conn, "SELECT * FROM `godown` WHERE  `code`='$row->godown' ORDER BY  `name`") or die(mysqli_error($conn));
                                    $gdwn = mysqli_fetch_object($getgodown);
                                    $ctr++;
                                    echo "<tr style='height:10px'> ";
                                    #echo "<td align='left'>$ctr</td>";
                                    echo "<td align='left'>$row->vouchno</td>";
                                    echo "<td align='left'>$row->mydt</td>";
                                    echo "<td align='left'>$party->name</td>";
                                    echo "<td align='left'>$gdwn->gdcode</td>";
                                    echo "<td align='right'>$row->subtotal</td>";
//                                        echo "<td align='right'>$row->discount</td>";
                                    echo "<td align='right'>$row->billamt</td>";

                                    $totamt = $totamt + $row->subtotal;
                                    $tdisc = $tdisc + $row->discount;
                                    $billamt = $billamt + $row->billamt;

                                    echo "</tr>";
                                    if ($_POST["dtype"] == 2) {
//                                                $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE vouchno='$row->vouchno' ORDER BY vouchno") or die(mysqli_error($conn));
                                        $gettran = mysqli_query($conn, "SELECT a.*, b.name as item FROM `$trandb` a, rawmat b 
                                                           WHERE a.vouchno='$row->vouchno' AND a.itemcode = b.code ORDER BY vouchno, srno") or die(mysqli_error($conn));

                                        $style = "calibri";
                                        $size = '2px';
                                        echo "<tr style='height:0px'>";
                                        echo "<td colspan='25'>";
                                        echo "<table style='width:100%;' class='table-bordered sortableTable responsive-table'>";
                                        echo "<th align='left' width='4%'><font face=$style size='$size'>Sr.</font></th>";

                                        echo "<th align='left' width='25%'><font face=$style size='$size'>Raw Material</font></th>";
                                        echo "<th align='right' width='8%' text-align: right;'><font face=$style size='$size'>Qty.</font></th>";
                                        echo "<th align='right' width='8%' text-align: right;'><font face=$style size='$size'>Unit</font></th>";
                                        echo "<th align='right' width='8%' text-align: right;'><font face=$style size='$size'>Rate</font></th>";
                                        echo "<th align='right' width='8%' text-align: right;'><font face=$style size='$size'>Amount</font></th>";




                                        echo "<tr style='height:0px'>";
                                        $ctrdtl = 0;
                                        while ($row2 = mysqli_fetch_object($gettran)) {
                                            $ctrdtl++;

                                            echo "<tr style='height:10px'>";
                                            echo "<td style=\"text-align:right;\" valign='top' width=\"6\"> \n <span><font face=$style size='$size'> $ctrdtl</font> </span> \n </td> \n";
                                            echo "<td style=\"text-align:right;\" valign='top'> \n <span><font face=$style size='$size'> $row2->item</font> </span> \n </td> \n";
                                            echo "<td style=\"text-align:right;\" valign='top' width=\"6\"> \n <span><font face=$style size='$size'> $row2->qty</font> </span> \n </td> \n";
                                            echo "<td style=\"text-align:left;\" valign='top' width=\"6\"> \n <span><font face=$style size='$size'> $row2->unit</font> </span> \n </td> \n";
                                            echo "<td style=\"text-align:right;\" valign='top' width=\"6\"> \n <span><font face=$style size='$size'> $row2->rate</font> </span> \n </td> \n";
                                            echo "<td style=\"text-align:right;\" valign='top' width=\"6\"> \n <span><font face=$style size='$size'> $row2->amt</font> </span> \n </td> \n";

                                            echo "</tr>";
                                        }
                                        echo "</table>";
                                        echo "</td>";
                                    }
                                }
                                echo "<tr style='height:10px'> ";
                                echo "<td align='left'></td>";
                                echo "<td align='right'><font><b></b></font></td>";
                                echo "<td align='right'><font><b></b></font></td>";
                                echo "<td align='right'><font><b>Total&nbsp;&nbsp;</font></b></td>";
                                echo "<td align='right'><font><b>" . number_format($totamt, 2, '.', '') . "</b></font></td>";
//                                    echo "<td align='right'><font><b>" . number_format($tdisc , 2, '.', '') . "</b></font></td>";
                                echo "<td align='right'><font color='green'><b>" . number_format($billamt, 2, '.', '') . "</b></font></td>";
                                echo "</tr>";
                                ?>


                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            <!-- /.col-lg-6 -->
        </div>  

        <script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
        <script>window.jQuery || document.write('<script src="assets/lib/jquery.min.js"><\/script>')</script> 
        <script src="assets/lib/bootstrap/js/bootstrap.js"></script>
        <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
        <script src="assets/lib/datatables/jquery.dataTables.js"></script>
        <script src="assets/lib/datatables/DT_bootstrap.js"></script>
        <script src="assets/lib/tablesorter/js/jquery.tablesorter.min.js"></script>
        <script src="assets/lib/touch-punch/jquery.ui.touch-punch.min.js"></script>
        <script src="assets/js/main.js"></script>
        <script>
                                        $(function () {
                                            metisTable();
                                            metisSortable();
                                        });
        </script>
    </body>
    </html>
    <?php
}
// Excel part
if (isset($_POST["export"])) {

    $cmbparty = $_POST[cmbparty];
    $pcond = "";
    $scond = "";

    if (!empty($_POST["cmbparty"])) {
        $pname = trim($_POST["cmbparty"]);
        $pcond = "AND `accode` = '$pname'";
    }

    if (empty($_POST["frmdt"])) {
        browse("From Date Cannot Be Empty");
        header("refresh:1; outrawreg.php");
    }
    if (empty($_POST["todt"])) {
        browse("To Date Cannot Be Empty");
        header("refresh:1; outrawreg.php");
    }


    $mfrmdate = trim($_POST["frmdt"]);
    $mtodate = trim($_POST["todt"]);

    $get = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE
                       `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' $pcond $scond") or die(mysqli_error($conn));
    //  print "SELECT *, DATE_FORMAT(`billdate`, '%d-%m-%Y') as mydt FROM `$maindb` WHERE
    //                     `vouchdt` >= '$mfrmdate' AND `vouchdt` <= '$mtodate' $pcond $scond";

    $getno = mysqli_num_rows($get);

    $contents = "Invoice No.,Date,karigar,Godown,Amount,Bill Amount\n";


    $totamt = 0;
    $billamt = 0;
    $tdisc = 0;
    $tvat = 0;
    $tpack = 0;

    $ctr = 0;
    while ($row = mysqli_fetch_object($get)) {
        mysqli_select_db($conn, $zdb);
        $getparty = mysqli_query($conn, "SELECT * FROM `accmast` WHERE  `accode`='$row->accode' ORDER BY  `name`") or die(mysqli_error($conn));
        $party = mysqli_fetch_object($getparty);

        $getgodown = mysqli_query($conn, "SELECT * FROM `godown` WHERE  `code`='$row->godown' ORDER BY  `name`") or die(mysqli_error($conn));
        $gdwn = mysqli_fetch_object($getgodown);
        $ctr++;

        $exctr = $ctr++;
        $exvouchno = $row->vouchno;
        $exmydt = $row->mydt;
        $exname = $party->name;
        $gdcode = $gdwn->gdcode;
        $exrow = $row->subtotal;
        $exbillamt = $row->billamt;

        $contents.=$exmydt . ",";
        $contents.=$exname . ",";
        $contents.=$gdcode . ",";
        $contents.=$exrow . ",";
        $contents.=$exbillamt . "\n";

        $totamt = $totamt + $row->subtotal;
        $tdisc = $tdisc + $row->discount;
        $billamt = $billamt + $row->billamt;


        if ($_POST["dtype"] == 2) {
//                                                $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE vouchno='$row->vouchno' ORDER BY vouchno") or die(mysqli_error($conn));
            $gettran = mysqli_query($conn, "SELECT a.*, b.name as item FROM `$trandb` a, rawmat b 
                                                           WHERE a.vouchno='$row->vouchno' AND a.itemcode = b.code ORDER BY vouchno, srno") or die(mysqli_error($conn));
            $contents .= "Sr.,Raw Material,Qty.,Unit,Rate,Amount\n";
            $ctrdtl = 0;


            while ($row2 = mysqli_fetch_object($gettran)) {

                $ctrdtl++;


                $exctrdtl = $ctrdtl++;
                $exitem = $row2->item;
                $exqty = $row2->qty;
                $exunit = $row2->unit;
                $exrate = $row2->rate;
                $examt = $row2->amt;

                $contents.=$exctrdtl . ",";
                $contents.=$exitem . ",";
                $contents.=$exqty . ",";
                $contents.=$exunit . ",";
                $contents.=$exrate . ",";
                $contents.=$examt . "\n";
            }
        }
    }
//     $contents.="" . ",";
//    $contents.="" . ",";
//    $contents.="" . ",";
    $contents.=",,,Total" . ",";
    $contents.=number_format($totamt, 2, '.', '') . ",";
    $contents.=number_format($billamt, 2, '.', '') . "\n";



    $contents = strip_tags($contents);

//header to make force download the file
    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;
    }
}
?>