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

$maindb = 'credmain';
$trandb = 'credtran';
$accdb = 'actran';
// $imgdb = "purchimage";
$printfile = "crednote_print.php";
$file = 'crednote.php';

$trandbexist = 1;
$comp = $_SESSION["comp_" . $dbname];
$myyear = $_SESSION[$myyear];

$adduser = $_SESSION['username_' . $dbname];
date_default_timezone_set("Asia/Kolkata");
$adddate = date("Y-m-d");
$addtime = date("h:i:sa");

$gtype = $_GET['type'];
$formname = 'Sale Return / Credit Note';
$source = "SR";
$stkdb = 'transacn';

ini_set('display_errors', 1);
error_reporting(E_ERROR);

$para = "";
$keycond = "";
//$fseries = $_SESSION["series_" . $dbname];
$myyear = $_SESSION["tit_" . $dbname];
$keyfld = "vouchno";
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$zroot = $_SESSION["zdbroot"];
$zyear = right($zdb, 4);
$zStartDate = getzdates($zyear, 'S');
$zEndDate = getzdates($zyear, 'E');
mysqli_select_db($conn, $zdb);


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

if (isset($_POST["submit"])) {

    $a = 1;

    $vouchdt = $_POST["vouchdt"];
    $qvouch = $_POST["vouchno"];
    $voucher = trim($_POST["voucher"]);
    $mono = $_POST["monno"];
    $invno = $_POST["invno"];
    $delvnote = $_POST["delvnote"];
    //    $debitac = $_POST["debitac"];
    // $paidby = trim($_POST["paidby"]);
    $party = trim($_POST["party"]);
    $taxableamt = trim($_POST["taxableamt"]);
    $totbillamt = trim($_POST["totbillamt"]);
    $grossamt = trim($_POST["grossamt"]);
    $gstno = trim($_POST["gstno"]);
    $totdisc = trim($_POST["totdisc"]);
    $totvat = trim($_POST["totvat"]);
    $type = trim($_POST["type"]);
    $courchgs = trim($_POST["courchgs"]);
    $rem1 = trim($_POST["remark1"]);
    $rem2 = trim($_POST["remark2"]);
    $company = $_POST["company"];


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

    if ($totbillamt <= 0) {
        browse("Bill Amount Cannot Be Zero");
        $a = 0;
    }

    if (empty($party)) {
        $a = 0;
        browse("Party Cannot Be Empty");
    }

    if ($paidby == "R") {
        $purcode = '30';
    } else {
        $purcode = '29';
    }

    $accode = $party;
    $grpcode = '010';

    if ($vouchdt == '0000-00-00') {
        browse("Voucher Date Cannot Be Empty");
        $a = 0;
    }

    if ($accode == "") {
        browse("Party Cannot Be Empty");
        $a = 0;
    }

    if ($a == 1) {
        mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `$trandb` WRITE, `product` WRITE, `$stkdb` WRITE, `$accdb` WRITE,  `accmast` WRITE");

        if ($addflag == 1) {
            $gen1 = mysqli_query($conn, "SELECT MAX(vouchno) AS maxcode FROM `$maindb` where vouchno != '' LIMIT 0,1 ") or die(mysqli_error($conn));
            if (mysqli_num_rows($gen1) > 0) {
                $vvouch = mysqli_fetch_assoc($gen1);
                $qvouch = $vvouch["maxcode"] + 1;
            } else {
                $qvouch = '1';
            }
            $mvouch = $qvouch;

            $gen2 = mysqli_query($conn, "SELECT MAX(invno) AS maxinv FROM `$maindb` LIMIT 0,1 ") or die(mysqli_error($conn));
            if (mysqli_num_rows($gen2) > 0) {
                $vinvno = mysqli_fetch_assoc($gen2);
                $qinvno = $vinvno["maxinv"] + 1;
            } else {
                $qinvno = '1';
            }
            $minvno = $qinvno;
        }


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

        mysqli_query($conn, "DELETE FROM `$stkdb` WHERE `vouchno`='$mvouch' AND `source`='$source' ") or die(mysqli_error($conn));


        $cnt = 0;
        $cntamt = 0;

        for ($i = 0; $i < $count; $i++) {
            $msrno = $_POST["srno_$i"];
            $barcode = $_POST["barcode_$i"];
            $mitem = mysqli_real_escape_string($conn, $_POST["itemname_$i"]);
            $cartqty = $_POST["cartqty_$i"];
            $qty = $_POST["qty_$i"];
            $totqty = $_POST["totqty_$i"];
            $rate = $_POST["rate_$i"];
            $amt = $_POST["amount_$i"];
            $discperc = $_POST["discperc_$i"];
            $discamt = $_POST["discamt_$i"];
            $vatperc = $_POST["vatperc_$i"];
            $batch = $_POST["batch_$i"];
            $unit = $_POST["unit_$i"];
            $stqty = $_POST["stkqty_$i"];
            $vat = $_POST["vat_$i"];
            $total = $_POST["total_$i"];
            $subtot = $_POST["subtot_$i"];

            $gstperc = $cgstrate + $sgstrate + $igstrate;

            $mitemcode = mysqli_query($conn, "SELECT code FROM `product` WHERE `name`='$mitem'") or die(mysqli_error($conn));
            $icode = mysqli_fetch_array($mitemcode);
            $itcode = $icode['code'];
            $cnt++;
            $group = $source . $mvouch;
            if (!empty($mitem)) {
                if ($unit == 1) {
                    $stkqty = $qty * 1000 * 1;
                } else {
                    $stkqty = $qty * 1;
                }
                $tqty = $qty * 1;

                mysqli_query($conn, "INSERT INTO `$trandb`(`vouchno`, `vouchdt`, `srno`, `itemcode`, `itemname`, `cartqty`, `qty`, `totqty`,
                                                           `rate`, `amount`, `discperc`, `discamt`, `total`, `barcode`, `subtot`, 
                                                           `vatperc`, `vat`, `unit`, `stkqty`, `batch`)
                                                   VALUES('$mvouch', '$vouchdt', '$cnt', '$itcode', '$mitem', '$cartqty', '$qty', '$totqty', 
                                                          '$rate', '$amt', '$discperc', '$discamt', '$total','$barcode','$subtot',
                                                          '$vatperc', '$vat','$unit','$stqty','$batch' )") or die(mysqli_error($conn));


                ######################### STOCK PART ####################################################
                mysqli_query($conn, "INSERT INTO `$stkdb`(`srno`, `yearno`, `vouchno`, `vouchdt`, `itemcode`,  `cartqty`,barcode,
                                                          `qty`, `source`, amount, accode, `group` , `totqty`, `rate`, `itemgrp`, 
                                                          `ordno`, `monno`,`unit`,`stkqty`,`batch`)
                                                   VALUES('$cnt', '$myyear', '$mvouch', '$vouchdt', '$itcode',  '$cartqty','$barcode',
                                                          '$tqty', '$source', '$amt', '$party', '$group', '$totqty', '$rate', '$itemgrp',
                                                          '1', '$mono','$unit','$stkqty','$batch')") or die(mysqli_error($conn));
            }
        }


        ############################### Accounts Part ######################################################
        //        IF ($chkledg == 1) {
        mysqli_query($conn, "DELETE FROM `$accdb` WHERE `vouchno` = '$mvouch' AND `source` = '$source' ") or die(mysqli_error($conn));
        if ($totbillamt > 0) {
            $minus_amt = ($totbillamt * -1);

            // sale return part
            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno,`godown`, source, trnno, accode, grpcode, amount, monno)
                            VALUES ('$vouchdt','$mvouch','$godown', '$source', '001', '$purcode','005', '$minus_amt','$mono')") or die(mysqli_error($conn));

            // party part
            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, `godown`, source, trnno, accode, grpcode, amount, monno)
                                VALUES ('$vouchdt','$mvouch','$godown', '$source', '002', '$accode','010', '$totbillamt','$mono')") or die(mysqli_error($conn));
        }
        //        $nettotal = round($total - $discount + $vat + $packing);

        if ($addflag == 1) {
            $newvoucher = $mono . $mvouch;


            mysqli_query($conn, "INSERT INTO `$maindb`(vouchno, `vouchdt`, `voucher`, `invno`, `mono`, `party`, `paidby`, `debitac`, 
                                               `rem1`, `rem2`,  `cashvouch`, `totdisc`,`grossamt`, totvat, company,
                                               `purcode`,`billamt`,`delvnote`,`adduser`, `adddate`, `addtime`,`courchgs`)
                                      VALUES('$mvouch', '$vouchdt', '$newvoucher', '$minvno', '$mono', '$party', '$paidby', '$debitac',
                                             '$rem1', '$rem2', '$mcashvouch', '$totdisc','$grossamt', '$totvat', '$company',
                                             '$purcode','$totbillamt','$delvnote','$adduser', '$adddate', '$addtime','$courchgs')") or die(mysqli_error($conn));


            //            $_SESSION['print_voucher'] = $mvouch;
        } else {
            $editdate = date("Y-m-d");
            $edittime = date("h:i:sa");
            $mvouch = $qvouch; //1
            mysqli_query($conn, "UPDATE `$maindb` SET  `vouchdt`='$vouchdt',  `invno`='$invno', `mono`='$mono', `party`='$party', `paidby`='$paidby', 
                                               `rem1`='$rem1', `rem2`='$rem2', `totdisc`='$totdisc',`grossamt`='$grossamt',`debitac`='$purcode',`purcode`='$purcode',
                                               `cashvouch`='$mcashvouch',totvat='$totvat', `billamt`='$totbillamt',`delvnote`='$delvnote', company='$company',
                                               `edituser`='$adduser',`edittime`='$edittime',`editdate`='$editdate',`courchgs`='$courchgs' WHERE `vouchno` = '$mvouch' ") or die(mysqli_error($conn));
        }
        mysqli_query($conn, "UNLOCK TABLES");
        if ($addflag == 1) {
            insmess();
            header("refresh: 1; $file?mode=edit&$keyfld=$mvouch");
        } else {
            updmess();
            header("refresh: 1; $file?mode=edit&$keyfld=$mvouch");
        }
    }
}
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);

    $getparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`= '$get[party]'") or die(mysqli_error($conn));
    $lparty = mysqli_fetch_array($getparty);

    $getcompdt = mysqli_query($conn, "SELECT `code`,`name` FROM $zroot.`compmast` WHERE `code`= '$get[company]'") or die(mysqli_error($conn));
    $lcomp = mysqli_fetch_array($getcompdt);

    $delcsh = $get['cashvouch'];

    // $readonly = 'readonly';
    //    $printdisabled = "none";
}


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

if (isset($_GET["delete"])) {
    mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `$trandb` WRITE, `$stkdb` WRITE, `$accdb` WRITE, `cashpay` WRITE") or die(mysqli_error($conn));

    $delcond = " source = '$source' and vouchno = '$_GET[vouchno]' ";
    $delcond3 = " vouchno = '$delcsh'  ";
    $delcond2 = " vouchno = '$_GET[vouchno]' ";

    delnew($zdb, "$maindb", "$delcond2", "$file?mode=last$para");
    delnew($zdb, "$trandb", "$delcond2", "$file?mode=last$para");
    delnew($zdb, "$stkdb", "$delcond", "$file?mode=last$para");

    delnew($zdb, "$accdb", "$delcond", "$file?mode=last$para");

    // delnew($zdb, "cashpay", "$delcond3", "$file?mode=last$para");
    mysqli_query($conn, "UNLOCK TABLES");
}

include("includes/linkshead.php");
?>

<script src="MyScript/crednote.js?a=<?= mt_rand(); ?>" type="text/javascript"></script>
<script src="MyScript/search_item.js?b=<?= mt_rand(); ?>" type="text/javascript"></script>
<script src="MyScript/crednote_search.js?c=<?= mt_rand(); ?>" type="text/javascript"></script>
<script src="MyScript/checkdate.js" type="text/javascript"></script>

</head>

<body>
    <form action='' method='POST'>
        <div class="col-lg-3 pad text-center" id="overlay_form" style="display:none;z-index:999;">
            <button type="button" id="close_form"
                style="float: text-right;font-weight: bold;font-size: 18px;color:red;"><i
                    class="fa fa-close"></i></button>
            <div style='width:100%;' id='browdata'>
            </div>
        </div>
        <!-- Default box -->
        <div>
            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12  pad">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title" style="color:#78909C"><a class="myPrint"><?php echo $formname; ?></a>
                        </h3>
                    </div>
                </div>

                <div class="panel-body">
                    <input type="date" value="<?= $zStartDate ?>" style="display: none;border: none" class="zstdate">
                    <input type="date" value="<?= $zEndDate ?>" style="display: none;border: none" class="zenddate">
                    <div class="row">
                        <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control marg">Voucher</label>
                        </div>
                        <div class="col-lg-1 col-md-8 col-sm-8 col-xs-8 pad">
                            <input type="text" name="vouchno" class="form-control form vouchno"
                                value="<?php echo ($addflag != 1) ? $get['vouchno'] : $qvouch ?>" readonly>
                            <input type="hidden" class="form-control addflag" value="<?php echo $addflag ?>">
                            <input type="hidden" name="voucher" class="form-control form voucher"
                                value="<?php echo ($addflag != 1) ? $get['voucher'] : '' ?>" tabindex="-1" readonly>
                            <input type="hidden" name="source" class="form-control form source" value="<?= $source; ?>"
                                id="source">
                            <input type="hidden" name="monno" class="form-control form monno"
                                value="<?php echo ($addflag != 1) ? $get['monno'] : '' ?>">
                            <input type="hidden" name="type" class="form-control form type gtype"
                                value="<?php echo ($addflag != 1) ? $get['mattype'] : $gtype ?>">
                            <input type="hidden" class="form-control form" name="cashvouch"
                                value="<?php echo ($addflag != 1) ? $get["cashvouch"] : ""; ?>">

                        </div>
                        <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control marg">Date</label>
                        </div>
                        <div class="col-lg-1 col-md-8 col-sm-8 col-xs-8 pad">
                            <input type="date" class="form-control form vouchdt focusfield" name="vouchdt"
                                value="<?php echo ($addflag != 1) ? $get["vouchdt"] : date("Y-m-d") ?>" autofocus>
                        </div>

                        <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control marg">Party</label>
                        </div>
                        <div class="col-lg-3 col-md-8 col-sm-8 col-xs-8 pad">
                            <select name="party" class="form-control form party select2" style='width:90%' required>
                                <option value="<?php echo ($addflag != 1) ? $lparty['accode'] : ""; ?>">
                                    <?php echo ($addflag != 1) ? $lparty['name'] : " "; ?>
                                </option>
                                <?php
                                if ($addflag == 1) {
                                    $mown2 = mysqli_query($conn, "SELECT `accode`, `name` FROM `accmast` WHERE grpcode = '010' ORDER BY name") or die(mysqli_error($conn));
                                    while ($trow2 = mysqli_fetch_array($mown2)) {
                                        echo "<option value='$trow2[accode]'>$trow2[name]</option>";
                                    }
                                }
                                ?>
                            </select>
                        </div>
                        <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                            <label class="label-control marg">Bill No.</label>
                        </div>
                        <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                            <input type="text" id="invno" name="invno" class="form-control form invno" required="true"
                                value="<?php echo ($addflag != 1) ? $get['invno'] : "NEW"; ?>" <?= $readonly ?> />
                        </div>

                        <!-- <div class="col-lg-1 pad text-right">
                            <label class="label-control marg">Delivery Note</label>
                        </div>
                        <div class="col-lg-1 pad">
                            <input type="text" id="delvnote" name="delvnote" class="form-control form delvnote" required="true" value="<?php echo ($addflag != 1) ? $get['delvnote'] : ''; ?>" />
                            <select id="paidby" name="paidby" class="form-control form select2 paidby" required>
                                <option
                        <?php
                        if ($get['paidby'] == 'R') {
                            echo "selected='selected'";
                        }
                        ?> value="R">Credit</option>
                                <option 
                        <?php
                        if ($get['paidby'] == 'C') {
                            echo "selected='selected'";
                        }
                        ?> value="C">Cash</option>
                            </select>
                        </div> -->
                    </div>

                </div>
                <div class="table-responsive" style="overflow-y: scroll;height:500px">

                    <table class="table-striped table-condensed table-bordered tab" width="100%">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width: 1%" class="text-center"></th>
                                <th style="width: 2%" class="text-center">Sr.</th>
                                <th style='width: 6%' class="text-center">Code</th>
                                <th style="width: 22%;" class="text-center">Item Name</th>
                                <th style="width: 10%;" class="text-center">Batch</th>
                                <th style='width: 5%' class="text-center">Stock</th>
                                <th style='width: 5%' class="text-center">Qty.</th>
                                <th style='width: 5%' class="text-center">Unit</th>
                                <th style='width: 4%' class="text-center">Rate</th>
                                <th style='width: 9%' class="text-center">Amount</th>
                                <th style='width: 4%' class="text-center">Disc. %</th>
                                <th style='width: 8%' class="text-center">Discount</th>
                                <th style='width: 4%' class="text-center">VAT. %</th>
                                <th style='width: 8%' class="text-center">VAT</th>
                                <th style='width: 10%' class="text-center">Total</th>
                            </tr>
                        </thead>
                        <tbody class="tbody">
                            <?php
                            if ($addflag != 1) {
                                $qry = mysqli_query($conn, "SELECT * FROM  `$trandb` WHERE vouchno='$getvouch' ORDER BY srno") or die(mysqli_error($conn));
                                $ctr = 0;
                                while ($row = mysqli_fetch_array($qry)) {
                                    $getitem = mysqli_query($conn, "SELECT code, name FROM `product` WHERE code = '$row[itemcode]'") or die(mysqli_error($conn));
                                    $geti = mysqli_fetch_array($getitem);

                                    $getunit = mysqli_query($conn, "SELECT * FROM $zroot.`unit` WHERE code = '$row[unit]'") or die(mysqli_error($conn));
                                    $fetunit = mysqli_fetch_array($getunit);

                                    echo "<tr>";
                                    echo "<td><i class='fa fa-close remove' style='display:none;'></i></td>";
                                    echo "<td><input type='text' name='srno_$ctr' class='srno form-control  form'  value='$row[srno]'></td>";
                                    echo "<td><input type='text' name='barcode_$ctr' class='barcode form-control  form' value='$row[barcode]' ></td>";
                                    echo "<td>";
                                    echo "<input type='text' name='itemname_$ctr' required='true' class='itemname form-control  form' value='$row[itemname]' tabindex='-1' readonly required>";
                                    echo "</td>";
                                    echo "<td><input type='text' name='batch_$ctr' class='batch form-control form' value='$row[batch]'></td>";
                                    //                                    Echo "<td><input type='number' name='cartqty_$ctr' class='cartqty form-control text-right  form' value='$row[cartqty]' ></td>";
                                    echo "<td>";
                                    echo "<input type='text' step='any' name='stkqty_$ctr' id='stkqty_$ctr' class='stkqty form-control form  text-right' value='$row[stkqty]' style='background-color:lightyellow;color:red'  readonly>";
                                    echo "</td>";
                                    echo "<td><input type='number' step='any' name='qty_$ctr' class='qty form-control  text-right form' value='$row[qty]'></td>";
                                    echo "<td>
                                        <select class='form-control select2 unit form' name='unit_$ctr' style='width:100%;' required>";
                                    echo "<option value='$fetunit[code]'>$fetunit[name]</option>";
                                    $ggetunit = mysqli_query($conn, "SELECT * FROM $zroot.unit WHERE `code`!= '' ORDER BY `name`") or die(mysqli_error($conn));
                                    while ($ffetunit = mysqli_fetch_array($ggetunit)) {
                                        echo "<option value='$ffetunit[code]'>$ffetunit[name]</option>";
                                    }
                                    echo "</select>";

                                    echo "</td>";
                                    //                                    Echo "<td><input type='number' step='any' name='totqty_$ctr' class='totqty form-control text-right  form' value='$row[totqty]' tabindex='-1' readonly></td>";
                                    echo "<td><input type='number' step='any' name='rate_$ctr' class='rate rate  form-control text-right  form' value='$row[rate]' ></td>";
                                    echo "<td><input type='number' step='any' name='amount_$ctr' id='amount_$ctr' class='amount form-control text-right  form'  value='$row[amount]' tabindex='-1' readonly></td>";
                                    echo "<td><input type='text' name='discperc_$ctr' class='discperc form-control text-right  form' value='$row[discperc]' ></td>";
                                    echo "<td><input type='text' name='discamt_$ctr' class='discamt form-control text-right  form' value='$row[discamt]' ></td>";
                                    echo "<td><input type='text' name='vatperc_$ctr' class='vatperc form-control text-right  form' value='$row[vatperc]'></td>";
                                    echo "<td><input type='text' name='vat_$ctr' class='vat form-control text-right  form' tabindex='-1' value='$row[vat]'readonly></td>";
                                    echo "<td><input type='text' name='total_$ctr' class='total form-control text-right  form' tabindex='-1' value='$row[total]' readonly></td>";
                                    echo "</tr>";
                                    $ctr++;
                                }
                            } else {
                                ?>
                                <tr>
                                    <td><i class='fa fa-close remove'></i></td>
                                    <td><input type='text' name='srno_0' class='srno form-control form' value='1'></td>
                                    <td><input type='text' name='barcode_0' class='barcode form-control form' value=''
                                            required></td>
                                    <td><input type='text' name='itemname_0' class='itemname form-control form' value=''
                                            required readonly tabindex='-1'></td>
                                    <td><input type='text' name='batch_0' class='batch form-control form' value=''></td>
                                    <!--<td><input type='number' name='cartqty_0' class='cartqty text-right form-control form' value='0' ></td>-->
                                    <td>
                                        <input type="text" step="any" name='stkqty_0'
                                            class='stkqty form-control text-right form zero'
                                            style='background-color:lightyellow;color:red' value='0' readonly>
                                    </td>
                                    <td><input type="number" step="any" name='qty_0'
                                            class='qty form-control text-right form zero' value='0'></td>
                                    <td>
                                        <select class='form-control select2 unit form' name='unit_0' style='width:100%;'
                                            required>
                                            <option value=''></option>
                                            <?php
                                            $ggetunit = mysqli_query($conn, "SELECT * FROM $zroot.unit WHERE `code`!= '' ORDER BY `name`") or die(mysqli_error($conn));
                                            while ($ffetunit = mysqli_fetch_array($ggetunit)) {
                                                echo "<option value='$ffetunit[code]'>$ffetunit[name]</option>";
                                            }
                                            ?>
                                        </select>
                                    </td>
                                    <!--<td><input type="number" step="any" name='totqty_0' class='totqty form-control text-right form zero' tabindex='-1' value='0' readonly></td>-->
                                    <td><input type="number" step="any" name='rate_0'
                                            class='rate rate form-control text-right form zero' value='0'></td>
                                    <td><input type="number" step="any" name='amount_0' id="amount_0"
                                            class='amount text-right form-control form zero' value='0' tabindex='-1'
                                            readonly></td>
                                    <td><input type='text' name='discperc_0' class='discperc text-right form-control form '
                                            value='0.00'></td>
                                    <td><input type='text' name='discamt_0' class='discamt text-right form-control form  '
                                            value='0.00'></td>
                                    <td><input type='text' name='vatperc_0' class='vatperc text-right form-control form  '
                                            value='0.00'></td>
                                    <td><input type='text' name='vat_0' class='vat text-right form-control form  '
                                            tabindex='-1' value='0.00' readonly> </td>
                                    <td><input type='text' name='total_0' class='total text-right form-control form  '
                                            tabindex='-1' value='0.00' readonly></td>
                                </tr>
                                <?php
                            }
                            ?>
                        </tbody>
                    </table>
                </div>
                <div class="col-lg-12 pad">
                    <button type="button" class="btn btn-primary btn-square form" id="addnew">
                        <i class="fa fa-plus" data-toggle='tooltip' title='Add Row'></i>
                    </button>
                    <input type="hidden" id="items" name="items" value="<?php echo ($addflag != 1) ? $ctr : 1; ?>">
                </div>
                <div class="col-lg-12 col-xs-12 pad">
                    <div class="col-lg-6 col-xs-12 pad">
                        <div class="col-lg-12 col-xs-12 pad">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Company</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 pad">
                                <select name="company" class="form-control form company select2" style='width:90%'
                                    required>
                                    <option value="<?php echo ($addflag != 1) ? $lcomp['code'] : ""; ?>">
                                        <?php echo ($addflag != 1) ? $lcomp['name'] : " "; ?>
                                    </option>
                                    <?php
                                    $mcomp2 = mysqli_query($conn, "SELECT `code`, `name` FROM $zroot.`compmast` ORDER BY name") or die(mysqli_error($conn));
                                    while ($tcomp = mysqli_fetch_array($mcomp2)) {
                                        echo "<option value='$tcomp[code]'>$tcomp[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>

                        </div>

                        <div class="col-lg-12 col-xs-12 pad">
                            <div class="col-lg-3 pad text-right">
                                <label class="label-control marg">Remarks</label>
                            </div>
                            <div class="col-lg-9 pad">
                                <input type="text" name="remark1" class="form-control form "
                                    value="<? echo ($addflag != 1) ? $get["rem1"] : ""; ?>">
                            </div>
                        </div>
                        <div class="col-lg-12 col-xs-12 pad">
                            <div class="col-lg-3 pad text-right"></div>
                            <div class="col-lg-9 pad">
                                <input type="text" name="remark2" class="form-control form "
                                    value="<? echo ($addflag != 1) ? $get["rem2"] : ""; ?>">
                            </div>
                        </div>
                    </div>

                    <div class="col-lg-6 col-xs-12 pad">
                        <div class="col-lg-12 col-xs-12 pad">
                            <div class="col-lg-3 pad"></div>
                            <div class="col-lg-3 pad"></div>
                            <div class="col-lg-3 pad text-right">
                                <label class="label-control marg">Subtotal</label>
                            </div>
                            <div class="col-lg-3 pad">
                                <input type="number" step="any" name="grossamt"
                                    class="form-control form text-right totamt grossamt"
                                    value="<?php echo ($addflag != 1) ? $get['grossamt'] : "0"; ?>" readonly
                                    tabindex="-1">
                            </div>
                        </div>
                        <div class="col-lg-12 col-xs-12 pad">
                            <div class="col-lg-3 pad"></div>
                            <div class="col-lg-3 pad"></div>
                            <div class="col-lg-3 pad text-right">
                                <label class="label-control marg">Less Discount</label>
                            </div>
                            <div class="col-lg-3 pad">
                                <input type="number" step="any" name="totdisc"
                                    class="form-control form text-right totdisc"
                                    value="<?php echo ($addflag != 1) ? $get['totdisc'] : "0"; ?>" readonly
                                    tabindex="-1">
                            </div>
                        </div>
                        <div class="col-lg-12 col-xs-12 pad">
                            <div class="col-lg-3 pad"></div>
                            <div class="col-lg-3 pad"></div>
                            <div class="col-lg-3 pad text-right">
                                <label class="label-control marg"> Add VAT</label>
                            </div>
                            <div class="col-lg-3 pad">
                                <input type="number" step="any" name="totvat"
                                    class="form-control form text-right totvat"
                                    value="<?php echo ($addflag != 1) ? $get['totvat'] : "0"; ?>" readonly
                                    tabindex="-1">
                            </div>
                        </div>

                        <!-- <div class="col-lg-12 col-xs-12 pad">
                            <div class="col-lg-3 pad"></div>
                            <div class="col-lg-3 pad"></div>
                            <div class="col-lg-3 pad text-right">
                                <label class="label-control marg">Courier Charges</label>
                            </div>
                            <div class="col-lg-3 pad">
                                <input type="number" step="any" name="courchgs" required="true" class="form-control form text-right courchgs" value="<?php echo ($addflag != 1) ? $get['courchgs'] : "0"; ?>">
                            </div>
                        </div> -->
                        <div class="col-lg-12 col-xs-12 pad">
                            <div class="col-lg-3 pad"></div>
                            <div class="col-lg-3 pad"></div>
                            <div class="col-lg-3 pad text-right">
                                <label class="label-control marg">Net Amount</label>
                            </div>
                            <div class="col-lg-3 pad">
                                <input type="number" step="any" name="totbillamt" required="true"
                                    class="form-control form text-right tottotal totbillamt"
                                    value="<?php echo ($addflag != 1) ? $get['billamt'] : "0"; ?>" readonly
                                    tabindex="-1">
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        </div>
        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 pad">
            <div class="panel panel-default">
                <div class="panel-body">
                    <?php include("includes/buttons.php"); ?>
                </div>
            </div>
        </div>
    </form>
    <div class="modal fade" id="imageUpload" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
        aria-hidden="true">
        <div class="modal-dialog">
            <div class="col-lg-11">
                <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="myModalLabel">Article Image Upload</h4>
                    </div>
                    <div class="modal-body">
                        <div class="row">
                            <div class="col-lg-12 upload">
                                <form id="upload_form" enctype="multipart/form-data" method="post">

                                    <div class="col-lg-3 pad">
                                        <span class="btn btn-default btn-file btn-square btn-warning">
                                            Choose File <input type="file" name="file1" id="file1">
                                        </span>
                                        <input type="hidden" name="code" id="vouchno"
                                            value='<?php echo ($addflag != 1) ? $get['vouchno'] : "$qvouch"; ?>'>
                                        <input type="button" value="Upload File" id="uploadbtn" class="btn btn-square"
                                            style="background-color: #C0CA33; color: white">
                                    </div>
                                    <div class="col-lg-6">
                                        <div class="progress">
                                            <progress id="progressBar" class="progress-bar" role="progressbar" value="0"
                                                max="100" style="width:100%;color: red;"></progress>
                                        </div>
                                        <h3 id="status"></h3>
                                        <p id="loaded_n_total"></p>
                                    </div>
                                </form>
                            </div>
                            <div class="col-lg-10 upload">
                                <img id="preview" src="" alt="your image" />
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="modal fade itemModal" 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">
                    <input type="hidden" value="<?php // Echo $fseries                                                                                
                    ?>" class="form-control series">
                    <input type="hidden" value="<?php echo $get['vouchno']; ?>" class="form-control vouchno">
                    <input type="hidden" value="<?php echo $file ?>" class="form-control file">
                    <input type="hidden" value="<?php echo $para ?>" class="form-control para">
                    <div class="row">
                        <div class="col-lg-3">
                            <select class="form-control ifield">
                                <option value="1">Name</option>
                                <option value="2">Barcode</option>
                                <option value="3">Itemcode</option>
                                <!--                            <option value="4">Gram</option>
                                                    <option value="5">Unit</option>-->
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control ioperator">
                                <option value=""></option>
                                <option value="%">Like</option>
                                <option value="=">Exactly</option>
                            </select>
                        </div>
                        <div class="col-lg-4">
                            <input type="text" value="" class="form-control isearch">
                        </div>
                        <div class="col-lg-3">
                            <input type="button" class="btn btn-primary iload" value="Search">
                        </div>
                    </div>

                    <div class="col-lg-12 iloader text-center">
                        <img src="images/search.gif" alt="loading">
                    </div>
                    <div class="col-lg-12 inoRecord text-center">
                        <h3>NO RECORDS FOUND</h3>
                    </div>
                    <div class="table-responsive" style="max-height: 400px;overflow: auto;">
                        <table id="" class="table table-striped table-condensed table-bordered" width="100%">
                            <thead>
                                <tr class="mySearchTr">
                                    <th width="8%">Itemcode</th>
                                    <th width="49%">Name</th>
                                    <!--                                <th width="17%">Barcode</th>
                            <th width="8%">Gram</th>
                            <th width="8%">Unit</th>-->
                                    <th width="8%">Stock</th>
                                    <th width="8%">Cost</th>
                                    <th width="8%">Rate</th>
                                    <th width="8%">MRP</th>
                                </tr>
                            </thead>
                            <tbody class="iTbody">
                            </tbody>
                        </table>
                    </div>
                    <div class="row">
                        <div class="col-xs-12 text-right search-action-bar">
                            <input type="button" class="btn btn-primary iprevious" value="Previous">
                            <input type="button" class="btn btn-primary inext" value="Next">
                        </div>
                    </div>
                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>
    <div class="modal fade searchModal bs-example-modal-lg" tabindex="-1" role="dialog"
        aria-labelledby="myLargeModalLabel">
        <div class="modal-dialog modal-lg" style="margin-top: 15%">
            <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">
                    <input type="hidden" value="<?php echo $fseries ?>" class="form-control series">
                    <input type="hidden" value="<?php echo $get['vouchno']; ?>" class="form-control vouchno">
                    <input type="hidden" value="<?php echo $file ?>" class="form-control file">
                    <input type="hidden" value="<?php echo $gtype ?>" class="form-control para">
                    <div class="row">
                        <div class="col-lg-2">
                            <select class="form-control sfield">
                                <option value="1">Voucher</option>
                                <option value="2">Date</option>
                                <option value="3">Bill No.</option>
                                <option value="4">Party</option>
                                <!-- <option value="5">Delivery Note</option> -->
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control soperator">
                                <option value="">Type of Search</option>
                                <option value="%">Like</option>
                                <option value="=">Exactly</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control sortBy">
                                <option value="">Sort By</option>
                                <option value="1">Voucher</option>
                                <option value="2">Date</option>
                                <option value="3">Bill No.</option>
                                <option value="4">Party</option>
                                <!-- <option value="5">Delivery Note</option> -->
                                ">Party</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control orderAs">
                                <option value="desc">Desc</option>
                                <option value="asc">Asc</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <input type="text" value="" class="form-control ssearch" placeholder="Search Here">
                        </div>
                        <div class="col-lg-2">
                            <input type="button" class="btn btn-primary sload" value="Search">
                        </div>
                    </div>
                    <div class="col-lg-12 sloader text-center">
                        <img src="images/search.gif" alt="loading">
                    </div>
                    <div class="col-lg-12 snoRecord text-center">
                        <h3>NO RECORDS FOUND</h3>
                    </div>
                    <div class="table-responsive" style="max-height: 400px;overflow: auto;">
                        <table id="" class="table table-striped table-condensed table-bordered" width="100%">
                            <thead>
                                <tr class="mySearchTr">
                                    <th width="15%">Voucher</th>
                                    <th width="15%">Date</th>
                                    <th width="15%">Bill No.</th>
                                    <th width="45%">Party</th>
                                    <!-- <th width="45%">Delivery Note</th> -->
                                    <th width="10%">Action</th>
                                </tr>
                            </thead>
                            <tbody class="sTbody">
                            </tbody>
                        </table>
                    </div>
                    <div class="row">
                        <div class="col-xs-12 text-right search-action-bar">
                            <input type="button" class="btn btn-primary sprevious" value="Previous">
                            <input type="button" class="btn btn-primary snext" value="Next">
                        </div>
                    </div>
                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>

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

</html>