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

$formname = 'Repair Challan Out';
$maindb = 'repoutmain';
$trandb = 'repouttran';
$file = 'repout.php';
$grid = '';
$keyfld = 'vouchno';
$trandbexist = 1;
$printfile = 'repoutprint.php';

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

include("includes/vouchno_generate.php");
include("includes/prev&next.php");

if (isset($_POST["submit"])) {
    $a = 1;
    $chaldate = $_POST["vouchdt"];
    $mpart = trim($_POST["cmbparty"]);
    $madd1 = trim($_POST["add1"]);
    $madd2 = trim($_POST["add2"]);
    $rem1 = trim($_POST["rem1"]);
    $rem2 = trim($_POST["rem2"]);

    $count = $_POST["items"];
    $mvouch = $qvouch;

    $mgroup = $source . $qvouch;

    if (empty($chaldate)) {
        browse("Voucher Date Cannot Be Empty");
        $a = 0;
    }


    if ($a == 1) {

        ###################### TRAN PART #####################################################


        mysqli_query($conn, "DELETE FROM `$trandb` WHERE vouchno='$mvouch'") or die(mysqli_error($conn));
        PRINT "DELETE FROM `$trandb` WHERE vouchno='$mvouch'";

        $cnt = 0;
        $cntamt = 0;
        for ($i = 0; $i < $count; $i++) {
            $msrno = $_POST["srno_$i"];
            $msrno2 = $_POST["srno2_$i"];
            $part = $_POST["particular_$i"];
            $mqty = $_POST["qty_$i"];
            $munit = $_POST["unit_$i"];

            if (!empty($mqty)) {

                mysqli_query($conn, "INSERT INTO `$trandb`(`vouchno`,`srno`,`srno2`, `particular`, qty, unit )
                        VALUES('$mvouch', '$msrno','$msrno2','$part', '$mqty' , '$munit')") or die(mysqli_error($conn));
            }
        }


        ############################## MAIN  ######################################################

        if ($addflag == 1) {

            mysqli_query($conn, "INSERT INTO `$maindb`(vouchno, `vouchdt`, party, `add1`, `add2`, `rem1`, `rem2`)
                                        VALUES($qvouch, '$chaldate', '$mpart', '$madd1', '$madd2', '$rem1', '$rem2')") or die(mysqli_error($conn));
            insmess();
            $mvouch = $qvouch; //1
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            $mvouch = $qvouch; //1

            mysqli_query($conn, "UPDATE `$maindb` SET `vouchno`='$qvouch',`vouchdt`='$chaldate', `party`='$mpart', `add1`='$madd1', `add2`='$madd2', "
                            . "`rem1`='$rem1', `rem2`='$rem2' WHERE vouchno = '$mvouch' ") or die(mysqli_error($conn));
            updmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        }
    }
}

if ($addflag != 1) {

    $getvouch = '';
    if ($addflag == 0) {
        $getvouch = $_GET[$keyfld];
    } else {
        $getvouch = $qvouch;
    }


    $result = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y') as mydt FROM `$maindb`
                           WHERE vouchno='$getvouch'") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);

    $getparty = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `accode`= '$get[party]'") or die(mysqli_error($conn));
    $getpar = mysqli_fetch_object($getparty);
}

/* DELETE */
if (isset($_GET["delete"])) {
    delete($zdb, "$maindb", "vouchno", $_GET["vouchno"], "$file?mode=last");
    delete($zdb, "$trandb", "vouchno", $_GET["vouchno"], "$file?mode=last");
}
include("includes/files.php");
?>
<script type='text/javascript'>
    $(document).ready(function () {

        function addrows() {
            var ctr = $('#items').val();
            var hide_check = "";
            if ($("#del_all").prop("checked")) {
                hide_check = 0;
            } else {
                hide_check = 1;
            }
            $.post('repout_add.php', {ctr: ctr, hide: hide_check}, function (data) {
                $(data).appendTo('#data');
                ctr++;
                $('#items').val(ctr);
            });
        }

        $('#addnew').click(function () {
            addrows();
        });
        
        $("#party").change(function () {
            var party = $("#party").val();
//                    alert(party);

            $.post('get_orders.php', {code: party, para: "padd1"}, function (data) {
                $('#padd1').val(data);
            });
            $.post('get_orders.php', {code: party, para: "padd2"}, function (data) {
                $('#padd2').val(data);
            });
        });

    });
</script>
<script type="text/javascript">

    $(document).ready(function () {
        //open popup
        $("#pop").click(function () {
            $("#overlay_form").fadeIn(1000);
            positionPopup();
        });

        //close popup
        $("#close").click(function () {
            $("#overlay_form").fadeOut(500);
        });



    });

    //position the popup at the center of the page
    function positionPopup() {
        if (!$("#overlay_form").is(':visible')) {
            return;
        }
        $("#overlay_form").css({
            left: '200px',
            top: ($(window).width() - $('#overlay_form').width()) / 7,
            position: 'absolute'
        });
    }

    //maintain the popup at center of the page when browser resized
    $(window).bind('resize', positionPopup);

</script>
<style>
    #data td{
        padding:0px;

    }
    #overlay_form{
        position: absolute;
        border: 1px solid #428BCA;
        padding: 15px;
        background: white;
        width: 550px;
        max-height: 900px;
        overflow: auto;
        font-size:20px;
    }
    #pop{
        display: block;
        border: 1px solid gray;
        width: 70px;
        text-align: center;
        font-size:20px;
        padding: 6px;
        border-radius: 5px;
        text-decoration: none;
        margin: 0 auto;
    }
</style>
</head>
<hr/>
<body onLoad="document.boq.vouchdt.focus()" id="main_container">
    <div id="overlay_form" style="display:none;z-index:999;">
        <a href="#" id="close" style="float: right;font-weight: bold;font-size: 20px;color:red;" >X</a>
        <input type="hidden" id="rowindex">
        <div style='width:100%;' id='browdata'>
        </div>
    </div>
    <div id="div-1" class="accordion-body collapse in body">

        <form action=''  name="repairout" method="POST">
            <div class="row">
                <div class="col-lg-6" style='width:100%;'>
                    <div class="box dark">
                        <header class="navbar navbar-default navbar-fixed-top" style="padding-top: 10px">
                            <div class="icons"><i class="fa fa-edit"></i></div>
                            <h5><?php echo $flag . " " . $formname; ?></h5>
                            <div class="toolbar" >
                                <ul class="nav">
                                    <li>
                                        <?php
                                        include_once("includes/buttons.php");
                                        ?>
                                    </li>
                                    <li>
                                        <a class="accordion-toggle minimize-box" data-toggle="collapse" href="#div-1">
                                            <i class="fa fa-chevron-up"></i>
                                        </a>
                                    </li>
                                </ul>
                            </div>
                        </header>
                        

                        <table border="0">
                            <tr>
                                <th> &nbsp;&nbsp;&nbsp;&nbsp&nbsp;&nbsp;
                                    Voucher No.&nbsp;&nbsp;
                                </th>  
                                <td>
                                    <input type="text"  name="vouchno" value="<?php echo ($addflag == 0) ? $get['vouchno'] : $qvouch; ?>"  class="form-control" disabled /> 
                                </td>
                                <th style="width:150px;"></th>
                                <th> &nbsp;
                                    Voucher Date.&nbsp;&nbsp;
                                </th>  
                                <td>
                                    <input type="date" size="" id="vouchdt" name="vouchdt" value="<?php echo ($addflag == 0) ? $get['vouchdt'] : date("Y-m-d"); ?>" class="form-control form"  required/>  
                                </td>
                                <th style="width:150px;"></th>

                            </tr>
                            <tr style="height:10px;">
                            </tr>
                            <tr>
                                <th> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                    Party
                                    &nbsp;
                                </th>  
                                <td colspan ="2">
                                    <select id="party" name="cmbparty" class="form-control form" style='height:33px;' required>
                                        <option value="<?php echo ($addflag != 1) ? $getpar->accode : " "; ?>"><?php echo ($addflag != 1) ? $getpar->name : ''; ?></option>
                                        <?php
                                        $mown = mysqli_query($conn, "SELECT accode, `name` FROM `accmast` WHERE grpcode = '010' ORDER BY name") or die(mysqli_error($conn));
                                        while ($trow = mysqli_fetch_array($mown)) {
                                            echo "<option value=$trow[accode]>$trow[name]</option>";
                                        }
                                        ?>
                                    </select>
                                </td>

                            </tr>    
                            <tr>
                                <td style ="text-align:right;">
                                    <label class="">Address&nbsp;&nbsp;</label>
                                </td> 
                                <td colspan="2">
                                    <input type="text" style="height:30px;"  id='padd1' name="add1" value="<?php echo ($addflag != 1) ? $get['add1'] : ""; ?>" class="padd1 form-control form" />
                                </td>
                            </tr>
                            <tr>
                                <td width="100px"></td>
                                <td colspan="2">
                                    <input type="text"  style="height:30px;" id='padd2' name="add2" value="<?php echo ($addflag != 1) ? $get['add2'] : ""; ?>" class="padd2 form-control form" />
                                </td>
                            </tr>
                            <tr style="height:10px;">
                            </tr>

                        </table>
                        <br/>
                        <table id="data" border="1px" width="60%">
                            <tr>
                                <td width="8%" align="center"> <label class="control-label">Sr.</label></td>
                                <td width="8%" align="center"> <label class="control-label">Sr.</label></td>
                                <td width="55%" align="center"><label class="control-label">Particular</label></td>
                                <td width="17%" align="center"><label class="control-label">Qty.</label></td>
                                <td width="17%" align="center"><label class="control-label">Unit</label></td>
                            </tr>
                            <?php
                            if ($addflag != 1) {

                                $qry2 = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE vouchno='$getvouch' ORDER BY srno") or die(mysqli_error($conn));
                                $ctr2 = 0;
                                while ($row2 = mysqli_fetch_array($qry2)) {

                                    echo "<tr>";
                                    echo "<td align='left' valign='top'><input type='text' size='6' maxlength='6' name='srno_$ctr2' class='sno form-control form' style='height:33px;' value='$row2[srno]'/></td>";
                                    echo "<td align='left' valign='top'><input type='text' size='6' maxlength='6' name='srno2_$ctr2' class='sno form-control form' style='height:33px;' value='$row2[srno2]'/></td>";
                                    echo "<td align='left' valign='top'><input type='text' size='70'  name='particular_$ctr2' class='particular form-control form'  style='text-align:left;height:33px;' value='$row2[particular]'></td>";
                                    echo "<td align='right' valign='top'><input type='text' size='4'  name='qty_$ctr2' class='qty form-control form'  style='text-align:right;height:33px;' value='$row2[qty]'></td>";
                                    echo "<td align='left' valign='top'><input type='text' size='4'  name='unit_$ctr2' class='unit form-control form'  style='text-align:left;height:33px;' value='$row2[unit]'></td>";

                                    echo "</tr>";
                                    $ctr2++;
                                }
                                /*                                browse($ctr); */
                            } else {
                                ?>
                                <tr>
                                    <td align="left" valign="top"><input type="text" size="6" maxlength="6" maxlength="6" name="srno_0" class="sno form-control form" style="height:33px;" value="1"/></td>
                                    <td align="left" valign="top"><input type="text" size="6" maxlength="6" maxlength="6" name="srno2_0" class="sno2 form-control form" style="height:33px;" value=""/></td>
                                    <td align="left" valign="top"><input type="text" size="70"  name="particular_0" class="particular form-control form"  style="text-align:left;height:33px;" value=""></td>
                                    <td align="right" valign="top"><input type="text" size="4"  name="qty_0" class="qty form-control form"  style="text-align:right;height:33px;" value="0"></td>
                                    <td align="left" valign="top"><input type="text" size="4"  name="unit_0" class="unit form-control form"  style="text-align:left;height:33px;" value=""></td>
                                </tr>
                            <?php } ?>
                        </table>
                    </div>  

                </div>
                <table id="" border='0' width='90%'>
                    <tr>

                        <td width="150px">
                            &nbsp;&nbsp;&nbsp;&nbsp;
                            <input type="button" id="addnew" class="btn btn-success btn-lg form" accode="addnew" value="+" />
                            <input type="hidden" id="items" name="items" value="<?php echo ($addflag != 1) ? $ctr2 : 1; ?>" />  
                        </td><td width="240px"></td><td width="140px"></td>

                    </tr>
                    <tr>
                        <td align="right" valign="top"><b>Remarks</b></td>
                        <td colspan="2">
                            <input type="text" name="rem1" value="<?php echo ($addflag == 0) ? $get['rem1'] : ""; ?>" class="form-control form">
                            <input type="text" name="rem2" value="<?php echo ($addflag == 0) ? $get['rem2'] : ""; ?>" class="form-control form">
                        </td>
                        <td></td>
                        <td></td>
                    </tr>
                    <tr>

                    </tr>
                </table>

                <!--SEARCH PART-->

                <div class="modal fade in slacker-modal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="false">
                    <div class="modal-dialog modal-slacker">
                        <div class="modal-content">
                            <div class="modal-header">
                                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                <h4 class="modal-title" id="myLargeModalLabel"></h4>
                            </div>
                            <div class="modal-body" style="height: 100%;white-space: nowrap;">
                                <div class="row">
                                    <div class="col-md-12">
                                        <div class="col-lg-12">
                                            <div class="box">
                                                <div id="collapse4" class="body">
                                                    <table id="dataTable" class="table table-bordered table-condensed table-hover table-striped">
                                                        <thead>
                                                            <tr>
                                                                <th align='left'>Challan No.</th>
                                                                <th align='left'>Challan Date</th>
                                                                <th align='left'>Party</th>
                                                                <th align='left'>Action</th>
                                                            </tr>
                                                        </thead>
                                                        <tbody>
                                                            <?php
                                                            $result = mysqli_query($conn, "SELECT *, DATE_FORMAT(`vouchdt`, '%d-%m-%Y')as invdt FROM `$maindb`  ORDER BY $keyfld") or die(mysqli_error($conn));
                                                            while ($row = mysqli_fetch_array($result)) {
                                                                $getpary = mysqli_query($conn, "SELECT accode,`name` FROM `accmast` WHERE accode='$row[party]'") or die(mysqli_error($conn));
                                                                $party = mysqli_fetch_object($getpary);

                                                                echo "<tr>";
                                                                echo "<td align='left'>$row[vouchno]</td>";
                                                                echo "<td align='left'>$row[invdt]</td>";
                                                                echo "<td align='left'>$party->name</td>";
                                                                echo "<td align='left'><a href=\"$file?mode=edit&$keyfld=$row[vouchno]\"><img src='images/edit.png'></a>&nbsp;&nbsp;&nbsp;&nbsp;
                                                                </td>";
                                                                echo "</tr>";
                                                            }
                                                            ?>
                                                        </tbody>
                                                    </table>

                                                    <script>
                                                        $(function () {
                                                            metisTable();
                                                            metisSortable();
                                                        });
                                                    </script>

                                                </div>

                                            </div>

                                        </div>

                                    </div><!-- /.row -->

                                </div>
                            </div>  
                            <div class="modal-footer" style="bottom: 0px;position: absolute;padding:0px;">
                                <!--"This is where i want to load my salemain.php"-->
                            </div>
                        </div>
                    </div>
                </div>



            </div>
    </div>        
    <!--</div>        
    
    </div>    
    
    </div>-->
    <div id="myModal" class="modal fade in" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
        <div class="modal-dialog"  style="width: 300px;">
            <div class="modal-content">

                <div class="modal-header">
                    <a class="btn btn-default" data-dismiss="modal"style="float: right;"><span class="glyphicon glyphicon-remove"></span></a>
                    <h4 class="modal-title" id="myModalLabel"></h4>
                </div>
                <div class="modal-body text-center">
                    <h4>Balance : <span id="balanceStk"></span></h4>
                </div>
                <div class="modal-footer">
                </div>

            </div><!-- /.modal-content -->
        </div><!-- /.modal-dalog -->
    </div>
    <!--</div>
    </div>
    </div>            -->

</form>
<?php
include("includes/files_footer.php");
?>
<script>
    $(function () {
        formGeneral();
        metisTable();
        metisSortable();
    });
</script>

</body>
</html>

