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

$formname = 'Amount Request';
$maindb = 'amtrqstmn';
$trandb = 'amtrqsttn';
$stkdb = '';


$printfile = "amtreqprn.php";
$file = 'amtrequest.php';
$trandbexist = 1;
$keyfld = "vouchno";
$source = "AMRQ";

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
$compcd = $_SESSION["comp_" . $dbname];

$stkdb1 = 'tran';
$zroot = $_SESSION["zdir"];
$combstkdb = $stkdb1 . $myyear;

mysqli_select_db($conn, $zdb);
include("includes/vouchno_generate.php");
include("includes/prev&next.php");

if (isset($_POST["submit"])) {
    $a = 1;
    $vouchno = $_POST["vouchno"];
    $vouchdt = $_POST["vouchdt"];
    $qty = $_POST["qty"];
    $total = $_POST["total"];
    $remark = $_POST["remark"];
    $remark2 = $_POST["remark2"];


    $count = $_POST["items"];

    $mvouch = $qvouch;

    $salecode = 11;



    if ($a == 1) {

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



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

        $srl = 0;
        for ($i = 0; $i < $count; $i++) {
            $srno = $_POST["srno_$i"];
            $party = $_POST["party_$i"];
            $karigar = $_POST["karigar_$i"];
            $acno = $_POST["acno_$i"];
            $amount = $_POST["amount_$i"];
            $recddate = $_POST["recddate_$i"];

            if (!empty($party)) {
                $srl++;
                mysqli_query($conn, "INSERT INTO `$trandb`(`srno`, `vouchno`, `vouchdt`, `party`, `karigar`, `acno`, `recddate`, `amount`)
                                        VALUES( '$srl', '$qvouch', '$vouchdt', '$party', '$karigar', '$acno', '$recddate','$amount')") or die(mysqli_error($conn));

            }
        }

#################################TRAN PART END #################################
############################## MAIN ######################################################
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `vouchdt`, `total`, `remark`,`remark2`)
                                     VALUES ($qvouch, '$vouchdt',    '$total','$remark', '$remark2')") 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 `vouchdt`='$vouchdt', `total`='$total', `remark`='$remark', `remark2`='$remark2' 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 * FROM `$maindb` WHERE $keyfld='$getvouch'") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);
}

$senable = "";
$cenable = "";

if (isset($_GET["delete"])) {
    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    delete($zdb, "$trandb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
}

include("includes/linkshead.php");
?>
<script src="MyScript/amtrequest.js" type="text/javascript"></script>
</head>
<body>
    <!--    <div id="preloader" class="col-lg-12 text-center">
            <h3 class="text-center">Loading</h3>
            <div id="status">&nbsp;</div>
        </div>-->
    <form action='' method='POST'>
        <!-- Default box -->
        <div>
            <div class="col-lg-10 col-md-8 col-sm-12 col-xs-12  pad">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title" style="color:inherit"><?php Echo $formname; ?></h3>
                        <div class="actions pull-right">
                            <i class="fa fa-chevron-down"></i>
                            <i class="fa fa-times"></i>
                        </div>
                    </div>
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Date</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-8 col-xs-8 pad">
                                <input type="hidden" class="form-control text-size form" name="vouchno" value="<?php Echo ($addflag != 1) ? $get["vouchno"] : " $qvouch"; ?>" required readonly>
                                <input type="date" class="form-control form"  name="vouchdt" value="<?php Echo ($addflag != 1) ? $get["vouchdt"] : date('Y-m-d'); ?>">
                            </div>
                        </div>

                        <div class="table-responsive">
                            <table class="table table-striped table-condensed table-bordered table-padd" width="100%">
                                <thead  style="" class="mySearchTr">
                                    <tr>
                                        <th width="2%"></th>
                                        <th width="3%" class='text-right'>Sr.</th>
                                        <th width="25%">A/c. Name</th>
                                        <th width="10%" class='text-right'>Amount</th>
                                        <th width="25%">Karigar</th>
                                        <th width="14%">A/c. No.</th>
                                        <th width="12%">Recd. Date</th>
                                    </tr>
                                </thead>
                                <tbody class="tbody">
                                    <?php
                                    if ($addflag != 1) {
                                        $ctr = 0;
                                        $tqty = 0;
                                        $tamt = 0;
                                        $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$get[vouchno]' ORDER BY `srno`")or die(mysqli_error($conn));
                                        while ($tran = mysqli_fetch_array($gettran)) {
                                            $getparty = mysqli_query($conn, "SELECT accode,name FROM `accmast` WHERE `accode`='$tran[party]' ")or die(mysqli_error($conn));
                                            $fparty = mysqli_fetch_array($getparty);

                                            $getkari = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$tran[karigar]'") or die(mysqli_error($conn));
                                            $fetkari = mysqli_fetch_array($getkari);


                                            Echo "<tr>";
                                            Echo "<td><i class='fa fa-close remove' style='display:none;'></i></td>";
                                            Echo "<td><input type='text' class='form-control form text-right srno'  name='srno_$ctr'        value='$tran[srno]'></td>";
                                            Echo "<td>";
                                            Echo "<select class='form-control form select2 party' name='party_$ctr' style='width:100%'>";
                                            Echo "<option value='$fparty[accode]'>$fparty[name]</option>";
                                            $gitem = mysqli_query($conn, "SELECT accode,name FROM `accmast` where grpcode='010' order by name")or die(mysqli_error($conn));
                                            while ($item = mysqli_fetch_array($gitem)) {
                                                Echo "<option value='$item[accode]'>$item[name]</option>";
                                            }
                                            Echo "</select>";
                                            Echo "</td>";
                                            Echo "<td><input type='text' class='form-control form text-right amount' name='amount_$ctr'      value='$tran[amount]'> </td>";
                                            Echo "<td>";
                                            Echo "<select class='form-control form select2 karigar' name='karigar_$ctr' style='width:100%'>";
                                            Echo "<option value='$fetkari[accode]'>$fetkari[name]</option>";
                                            $gcolor = mysqli_query($conn, "SELECT accode,name FROM `accmast` where grpcode='009' and active = '1'  order by name")or die(mysqli_error($conn));
                                            while ($color = mysqli_fetch_array($gcolor)) {
                                                Echo "<option value='$color[accode]'>$color[name]</option>";
                                            }
                                            Echo "</select>";
                                            Echo "</td>";
                                            Echo "<td><input type='text' class='form-control form acno' name='acno_$ctr'      value='$tran[acno]'> </td>";
                                            Echo "<td><input type='date' class='form-control form recvdate'   name='recddate_$ctr'        value='$tran[recddate]'>   </td>";
                                            Echo "</tr >";
                                            $ctr++;
                                            $tqty = $tqty + $tran[trqty];
                                            $tamt = $tamt + $tran[amount];
                                        }
                                    } else {
                                        $pctr = 0;
//                                        $yctr = 1;
                                        ?>
                                        <tr>
                                            <td><i class="fa fa-close remove" style="display:none"></i></td> 
                                            <td><input type="text" class="form-control form text-right srno"      name="srno_<?= $pctr ?>"        value="1"></td> 
                                            <td>
                                                <select name="party_<?= $pctr ?>" class="form-control form select2 party" style="width: 100%">
                                                    <option value=""></option>
                                                    <?
                                                    $getitem = mysqli_query($conn, "SELECT accode,name FROM `accmast` where grpcode='010' ORDER BY name")or die(mysqli_error($conn));
                                                    while ($item1 = mysqli_fetch_array($getitem)) {
                                                        Echo "<option value='$item1[accode]'>$item1[name]</option>";
                                                    }
                                                    ?>
                                                </select>
                                            </td>
                                            <td><input type="text" class="form-control form text-right amount"    name="amount_<?= $pctr ?>"      value="" ></td> 
                                            <td>
                                                <select name="karigar_<?= $pctr ?>" class="form-control form select2 karigar" style="width: 100%">
                                                    <option value=""></option>
                                                    <?
                                                    $getcolor = mysqli_query($conn, "SELECT accode,name FROM `accmast` where grpcode='009' and active = '1'   ORDER BY name")or die(mysqli_error($conn));
                                                    while ($color1 = mysqli_fetch_array($getcolor)) {
                                                        Echo "<option value='$color1[accode]'>$color1[name]</option>";
                                                    }
                                                    ?>
                                                </select>
                                            </td>
                                            <td><input type="text" class="form-control form acno"    name="acno_<?= $pctr ?>"      ></td> 
                                            <td><input type="date" class="form-control form recddate"     name="recddate_<?= $pctr ?>" ></td> 
                                        </tr>  
                                        <?php
                                    }
                                    $pctr++;
                                    ?>
                                </tbody>
                            </table>

                        </div>

                        <div class="row">
                            <div class="col-lg-1" >
                                <button type="button" class="btn btn-square form mySearchTr" style=""  id="addnew">
                                    <i class="fa fa-plus" style="color:#ffffff" data-toggle='tooltip' title='Add Row' ></i>    
                                </button>
                                <input type="hidden" id="items" name="items" value="<?php echo ($addflag != 1) ? $ctr : $pctr; ?>" />  
                            </div> 
                        </div> 

                        <div class="row">
                            <div class="col-lg-4 col-md-4 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Total</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form text-right totamt" name="total" value="<?php Echo ($addflag != 1) ? $tamt : ""; ?>" readonly>
                            </div>
                        </div>
                        <hr>
                        <div class="row">
                            <div class="col-lg-1 col-md-1 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control text-size marg">Remark</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form " name="remark" value="<?php Echo ($addflag != 1) ? $get["remark"] : ""; ?>" >
                                <input type="text" class="form-control form " name="remark2" value="<?php Echo ($addflag != 1) ? $get["remark2"] : ""; ?>" >
                            </div>
                        </div>

                    </div>
                </div>
            </div>
        </div>

        <div class="col-lg-10 col-md-10 col-sm-12 col-xs-12 pad">
            <div class="panel panel-default">
                <div class="panel-body">
                    <?php include("includes/buttons.php"); ?>
                </div>
            </div>
        </div>
    </div>
</form>
<div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button class="close" data-dismiss="modal"><span>&times;</span></button>
                <h4 class="modal-title" id="myLargeModalLabel">Search <? Echo $formname; ?></h4>
            </div>
            <div class="modal-body">
                <div class=" table-responsive">
                    <table id="example1" class="table table-bordered">
                        <thead>
                            <tr class="mySearchTr">
                                <th>Voucher</th>
                                <th>Date</th>
                                <!--<th>Qty.</th>-->
                                <th>Total</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php
                            if ($addflag != 1) {
                                $srchresult = mysqli_query($conn, "SELECT *,date_format(vouchdt,'%d-%m-%Y') as mydt FROM `$maindb`")or die(mysqli_error($conn));
                                while ($row = mysqli_fetch_array($srchresult)) {
                                    echo "<tr class='clickable-row' data-href='$file?mode=edit&$keyfld=$row[$keyfld]'>";
                                    echo "<td align='left'>$row[vouchno]</td>";
                                    echo "<td align='left'>$row[mydt]</td>";
//                                    echo "<td align='left'>$row[qty]</td>";
                                    echo "<td align='left'>$row[total]</td>";
                                    echo "<td align='left'><a href=\"$file?mode=edit&$keyfld=$row[$keyfld]\"><i class='fa fa-pencil' style='font-size:1.3em;color:#00C0EF;cursor:pointer;'></i></a></td>";
                                    echo "</tr>";
                                }
                            }
                            ?>
                        </tbody>
                    </table>
                </div>
            </div>
            <div class="modal-footer"></div>
        </div>
    </div>
</div>
<?php include("includes/linksfoot.php"); ?>
</body>
</html>
