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

$maindb = 'purmain';
$trandb = 'purtran';
$accdb = 'actran';
$imgdb = "purchimage";
//$printfile = "purchase_mi_print.php";
$file = 'purchase.php';

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

$gtype = $_GET['type'];
$formname = 'Purchase';
$source = "PU";
$stkdb = 'transacn';


$para = "";
$keycond = "";
//$fseries = $_SESSION["series_" . $dbname];
$myyear = $_SESSION["tit_" . $dbname];
$keyfld = "vouchno";
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$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 = trim($_POST["vouchdt"]);
    $voucher = trim($_POST["voucher"]);
    $mono = $_POST["monno"];
    $invno = $_POST["invno"];
    //    $debitac = $_POST["debitac"];
    $godown = trim($_POST["godown"]);
    $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"]);

    $rem1 = trim($_POST["remark1"]);
    $rem2 = trim($_POST["remark2"]);
    $mtotqty = $_POST["mtotqty"];


    $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 == "C") {
        $purcode = 11;
    } else {
        $purcode = 12;
    }

    if ($a == 1) {

        $gparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `name`='$party'") or die(mysqli_error($conn));
        if (mysqli_num_rows($gparty) > 0) {
            $fparty = mysqli_fetch_array($gparty);
            $accode = $fparty['accode'];
        } else {
            $genac = mysqli_query($conn, "SELECT MAX(accode) AS maxcode FROM `accmast` where accode != '' LIMIT 0,1 ") or die(mysqli_error($conn));
            if (mysqli_num_rows($genac) > 0) {
                $gac = mysqli_fetch_assoc($genac);
                $accode = $gac["maxcode"] + 1;
            } else {
                $accode = '51';
            }
            mysqli_query($conn, "INSERT INTO `accmast`(`accode`, `name`, grpcode)
                       VALUES('$accode', '$party', '009')") or die(mysqli_error($conn));
        }

        $party = $accode;
        mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `$trandb` WRITE, `product` WRITE, `$stkdb` WRITE, `$accdb` WRITE, `cashpay` 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;
        }

        //**************************************************        auto cash Entry Start  ***************************************
        $mcashvouch = $_POST["cashvouch"];

        if ($paidby == 'C') {
            if ($mcashvouch == '0' || $mcashvouch == '') {
                $gen1 = mysqli_query($conn, "SELECT MAX(vouchno) AS maxcode FROM `cashpay` where vouchno!=''   LIMIT 0,1 ") or die(mysqli_error($conn));
                if (mysqli_num_rows($gen1) > 0) {
                    $vvouch = mysqli_fetch_assoc($gen1);
                    $tcashvouch = $vvouch["maxcode"] + 1;
                } else {
                    $tcashvouch = '1';
                }
                $mcashvouch = $tcashvouch;
            }


            $genrecp = mysqli_query($conn, "SELECT * FROM `cashpay` where `vouchno` = '$mcashvouch' ") or die(mysqli_error($conn));
            if (mysqli_num_rows($genrecp) > 0) {
                $fetrecp = mysqli_fetch_array($genrecp);
                $mrecpno = $fetrecp['recpno'];
                $mcashvouch = $fetrecp['vouchno'];
            } else {

                $gen2 = mysqli_query($conn, "SELECT MAX(recpno) AS maxcode FROM `cashpay` where bankname = '3'   LIMIT 0,1 ") or die(mysqli_error($conn));
                if (mysqli_num_rows($gen2) > 0) {
                    $gvouch = mysqli_fetch_assoc($gen2);
                    $mrecpno1 = $gvouch["maxcode"] + 1;
                } else {
                    $mrecpno1 = '1';
                }
                $mrecpno = $mrecpno1;
            }

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


            mysqli_query($conn, "INSERT INTO `cashpay`( vouchno , `vouchdt`, `bankname`, `bankinit`, `recpno`, `party`,`amount`,`remark1`,`remark2`,`trnflag`)
                                       Values ('$mcashvouch', '$vouchdt', '3', 'COH', '$mrecpno','$party','$totbillamt','Cash Paid For','Purchase No.$mvouch','S')") or die(mysqli_error($conn));


            // cash part  
            $minus_amt = ($totbillamt * -1);


            mysqli_query($conn, "DELETE FROM `$accdb` WHERE `$keyfld`='$mcashvouch' and `source` = 'CP' ") or die(mysqli_error($conn));

            $qrygrp = mysqli_query($conn, "select `grpcode` from accmast where `accode` = '$party'") or die(mysqli_error($conn));
            $fetgrp = mysqli_fetch_array($qrygrp);
            $pgrp = $fetgrp['grpcode'];

            mysqli_query($conn, "INSERT INTO `$accdb`(date, godown, vouchno, source, recpno, trnno, bankinit, accode,  grpcode, amount,monno)
                    VALUES ('$vouchdt','$godown', '$mcashvouch', 'CP', '$mrecpno',  '001', 'COH', '3', '002', '$totbillamt', '$mono')");

            // party part
            mysqli_query($conn, "INSERT INTO `$accdb`(date, godown, vouchno, source, recpno, trnno, bankinit, accode, grpcode, amount, monno)
                     VALUES ('$vouchdt','$godown', '$mcashvouch', 'CP', '$mrecpno', '002', 'COH', '$party',  '$pgrp', '$minus_amt', '$mono')");
        } else {
            mysqli_query($conn, "DELETE FROM `cashpay` WHERE `$keyfld`='$mcashvouch' ") or die(mysqli_error($conn));
            mysqli_query($conn, "DELETE FROM `actran` WHERE `$keyfld`='$mcashvouch' AND `source`='CP' ") or die(mysqli_error($conn));
            $mcashvouch = "";
        }


        //**************************************************        auto cash Entry End    ***************************************


        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"]);
            $qty = $_POST["qty_$i"];
            $totqty = $_POST["totqty_$i"];
            $amt = $_POST["amount_$i"];
            $discperc = $_POST["discperc_$i"];
            $discamt = $_POST["discamt_$i"];
            $vatperc = $_POST["vatperc_$i"];
            $vat = $_POST["vat_$i"];
            $total = $_POST["total_$i"];
            $subtot = $_POST["subtot_$i"];
            $cost = $_POST["cost_$i"];
            $unit = $_POST["unit_$i"];
            $cartqty = $_POST["cartqty_$i"];
            $pcscart = $_POST["pcscart_$i"];
            $gstperc = $cgstrate + $sgstrate + $igstrate;

            $mitemcode = mysqli_query($conn, "SELECT code FROM `product` WHERE `name`='$mitem'") or die(mysqli_error($conn));

            $cnt++;
            $group = $source . $mvouch;
            if (!empty($mitem)) {
                if (!empty($mitemcode)) {
                    $mnum = mysqli_num_rows($mitemcode);
                } else {
                    $mnum = 0;
                }

                if ($mnum > 0) {
                    $icode = mysqli_fetch_array($mitemcode);
                    $itcode = $icode['code'];
                } else {
                    $itemgrp = $_POST["itemgrp_$i"];
                    $genit = mysqli_query($conn, "SELECT MAX(code) AS maxcode FROM `product` where code!=''   LIMIT 0,1 ") or die(mysqli_error($conn));
                    if (mysqli_num_rows($gen1) > 0) {
                        $vvouchit = mysqli_fetch_assoc($genit);
                        $itcode = $vvouchit["maxcode"] + 1;
                    } else {
                        $itcode = '1';
                    }
                    mysqli_query($conn, "Insert into `product` (code, name,cost,prodgrp,barcode,unit, pkg)
                                                   values('$itcode','$mitem','$cost','1','$barcode','$unit', '$pcscart')") or die(mysqli_error($conn));
                }

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


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

        ######################### STOCK PART ####################################################
        ############################### Accounts Part ######################################################
        #
        #        

        mysqli_query($conn, "DELETE FROM `$accdb` WHERE `vouchno` = '$mvouch' AND `source` = '$source'") or die(mysqli_error($conn));
        if ($totbillamt > 0) {
            $samtmin = $totbillamt * -1;

            // purchase part
            mysqli_query($conn, "INSERT INTO `$accdb`(godown, date, vouchno, source, trnno, accode, grpcode, amount,monno)
                    VALUES ('$godown','$vouchdt','$mvouch', '$source', '001', '$purcode','004', '$samtmin','$mono')") or die(mysqli_error($conn));
            // party part
            mysqli_query($conn, "INSERT INTO `$accdb`(godown, date, vouchno, source, trnno, accode, grpcode, amount)
                     VALUES ('$godown', '$vouchdt','$mvouch', '$source', '002', '$party','009', '$totbillamt')") 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, godown,
                                               `purcode`,`billamt`, `totqty`)
                                      VALUES('$mvouch', '$vouchdt', '$newvoucher', '$invno', '$mono', '$party', '$paidby', '$debitac',
                                             '$rem1', '$rem2', '$mcashvouch', '$totdisc','$grossamt', '$totvat', '$godown',
                                             '$purcode','$totbillamt', '$mtotqty')") or die(mysqli_error($conn));
        } else {

            mysqli_query($conn, "UPDATE `$maindb` SET  `vouchdt`='$vouchdt',  `invno`='$invno', `mono`='$mono', `party`='$party', `paidby`='$paidby', godown='$godown',
                                               `rem1`='$rem1', `rem2`='$rem2', `totdisc`='$totdisc',`grossamt`='$grossamt',`debitac`='$purcode',`purcode`='$purcode',
                                               `cashvouch`='$mcashvouch',totvat='$totvat', `billamt`='$totbillamt', `totqty`='$mtotqty' 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);

    $getgodwn = mysqli_query($conn, "SELECT `code`,`gcode`,`name` FROM `godown` WHERE `code`= '$get[godown]'") or die(mysqli_error($conn));
    $lgodwn = mysqli_fetch_array($getgodwn);

    $delcsh = $get['cashvouch'];

    $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]' ";
    $delcond1 = " source = 'CP' and vouchno = '$delcsh' ";
    $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, "$accdb", "$delcond1", "$file?mode=last$para");

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

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

<script src="MyScript/purchase.js?a=<?= mt_rand(); ?>" type="text/javascript"></script>
<script src="MyScript/search_item_purchase.js?b=<?= mt_rand(); ?>" type="text/javascript"></script>
<script src="MyScript/purchase_mi_search.js?c=<?= mt_rand(); ?>" type="text/javascript"></script>
<script src="MyScript/checkdate.js" type="text/javascript"></script>
<style>
    .button-29 {
        align-items: center;
        appearance: none;
        background-color: #778da9;
        /* Filled by default */
        border: 2px solid #778da9;
        border-radius: 6px;
        box-sizing: border-box;
        color: #ffffff;
        /* White text on filled background */
        cursor: pointer;
        display: inline-flex;
        font-family: "JetBrains Mono", monospace;
        font-size: 16px;
        height: 40px;
        padding: 0 20px;
        justify-content: center;
        line-height: 1.2;
        text-decoration: none;
        transition: all 0.2s ease;
        user-select: none;
        white-space: nowrap;
        will-change: box-shadow, transform;
        width: auto;
    }

    .button-29:hover {
        background-color: transparent;
        color:#778da9;
        border: 2px solid #778da9;
        box-shadow: none;
        transform: none;
    }

    .button-29:active {
        transform: scale(0.98);
        box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.2);
    }
</style>
</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-1 col-sm-1 col-xs-1 text-right pad">
                            <label class="label-control marg">Voucher</label>
                        </div>
                        <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                            <input type="text"   name="vouchno" class="form-control form vouchno" value="<?php echo ($addflag != 1) ? $get['vouchno'] : $qvouch ?>">
                            <input type="hidden" class="form-control addflag" value="<?php echo $addflag ?>">
                            <input type="hidden" name="rd" class="form-control form rd" value="1">
                            <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-1 col-sm-1 col-xs-1 text-right pad">
                            <label class="label-control marg">Date</label>
                        </div>
                        <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 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-1 col-sm-1 col-xs-1 text-right pad">
                            <label class="label-control marg">Party</label>
                        </div>
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                            <input list='party' name='party' value="<?php echo ($addflag != 1) ? $lparty["name"] : '' ?>" class='party cmbparty form-control form ' autocomplete="off">  
                            <datalist id='party' char="#">
                                <option value=''></option>
                                <?php
                                $pparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `grpcode`='009' order by name") or die(mysqli_error($conn));
                                while ($mparty = mysqli_fetch_array($pparty)) {
                                    $aname = $mparty['name'];
                                    echo "<option value='$aname'>";
                                }
                                ?>
                            </datalist>
                        </div>
                        
                        <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 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'] : ''; ?>" />
                        </div>

                        <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 pad text-right" style="width:5%">
                            <label class="label-control marg">Paid By</label>
                        </div>

                        <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                            <select id="paidby" name="paidby" class="form-control form select2 paidby" required>
                                <option value="R" <?= ($get['paidby'] == 'R') ? "selected" : "" ?>>Credit</option>
                                <option value="C" <?= ($get['paidby'] == 'C') ? "selected" : "" ?>>Cash</option>
                            </select>
                        </div>
                    </div>


                    <div class="row">
                        <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-right pad">
                            <label class="label-control marg">Godown</label>
                        </div>
                        <div class="col-lg-2 col-md-1 col-sm-2 col-xs-2 pad">
                            <select name="godown" class="form-control form godown select2" style='width:100%' required>
                                <option value="<?php echo ($addflag != 1) ? $lgodwn['code'] : ""; ?>"><?php echo ($addflag != 1) ? $lgodwn['name'] : ""; ?></option>
                                <?php
                                $mown3 = mysqli_query($conn, "SELECT `code`, `gcode`,name FROM `godown` WHERE code != '' ORDER BY name") or die(mysqli_error($conn));
                                while ($trow3 = mysqli_fetch_array($mown3)) {
                                    echo "<option value='$trow3[code]'>$trow3[name]</option>";
                                }
                                ?>
                            </select>
                        </div>
                    </div>
                </div>

                <div class="table-responsive">
                    <table class="table-striped table-condensed table-bordered tab" width="100%">
                        <thead class="mySearchTr">
                            <tr>
                                <th style="width: 1%"></th>
                                <th style="width: 2%">Sr.</th>
                                <th style='width: 6%'>Code</th>
                                <th style="width: 22%;">Item Name</th>
                                <th style='width: 10%' class="text-right">Pcs.per Carton</th>
                                <th style='width: 8%' class="text-right">Cartons</th>
                                <th style='width: 5%' class="text-right">Qty.</th>
                                <!-- <th style='width: 4%' class="text-right">Cost</th>
                                <th style='width: 9%' class="text-right">Amount</th>
                                <th style='width: 4%' class="text-right">Disc. %</th>
                                <th style='width: 8%' class="text-right">Discount</th>
                                <th style='width: 10%' class="text-right">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,unit FROM `product` WHERE code = '$row[itemcode]'") or die(mysqli_error($conn));


                                    $geti = mysqli_fetch_array($getitem);
                                    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><input type='text' name='itemname_$ctr' required='true' class='itemname form-control  form' value='$row[itemname]' required  maxlength='40'></td>";
                                    echo "<td><input type='number' step='any' name='pcscart_$ctr' class='pcscart  form-control  text-right form' value='$row[pcscart]'></td>";
                                    echo "<td><input type='number' name='cartqty_$ctr'  class='cartqty    form-control text-right  form' value='$row[cartqty]'></td>";
                                    echo "<td><input type='number' name='qty_$ctr'      class='qty        form-control text-right  form' value='$row[qty]' tabindex='-1' readonly>
                                              <input type='hidden' name='cost_$ctr'     class='cost rate  form-control text-right  form' value='$row[cost]'>
                                              <input type='hidden' name='amount_$ctr'   class='amount     form-control text-right  form' value='$row[amount]' id='amount_$ctr' tabindex='-1' readonly>
                                              <input type='hidden' name='discperc_$ctr' class='discperc   form-control text-right  form' value='$row[discperc]'>
                                              <input type='hidden' name='discamt_$ctr'  class='discamt    form-control text-right  form' value='$row[discamt]'>
                                              <input type='hidden' 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=''></td>
                                    <td><input type='text' name='itemname_0' class='itemname form-control form' value='' maxlength='40'></td>
                                    <td><input type="number" step="any" name='pcscart_0' class='pcscart form-control form text-right zero' value='0'></td>
                                    <td><input type='number' step="any" name='cartqty_0' class='cartqty form-control form text-right' value='0'></td>
                                    <td><input type="number" step="any" name='qty_0'     class='qty     form-control form text-right zero' value='0' tabindex='-1' readonly>
                                        <input type="hidden" name='cost_0'     class='cost rate form-control form text-right zero' value='0'>
                                        <input type="hidden" name='amount_0'   class='amount    form-control form text-right zero'  id="amount_0" value='0' tabindex='-1' readonly>
                                        <input type='hidden' name='discperc_0' class='discperc  form-control form text-right' value='0.00'>
                                        <input type='hidden' name='discamt_0'  class='discamt   form-control form text-right' value='0.00'>
                                        <input type='hidden' name='total_0'    class='total     form-control form text-right' 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-md-12 col-sm-12 col-xs-12 pad">
                    <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 6ad">
                        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 pad"></div>
                        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 pad">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad text-right">
                                <label class="label-control marg">Remarks</label>
                            </div>
                            <div class="col-lg-9 col-md-9 col-sm-9 col-xs-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-md-12 col-sm-12 col-xs-12 pad">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad text-right"></div>
                            <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9 pad">
                                <input type="text" name="remark2" class="form-control form " value="<? echo ($addflag != 1) ? $get["rem2"] : ""; ?>">
                            </div>
                        </div>

                        <!-- <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-4 col-xs-4  text-right pad">
                                <label class="label-control text-danger marg">No. Of Labels</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 ">
                                <input type='text' id="nolabel" name='nolabel' maxlength="4" class='form-control' style='width:70px;' <?php echo ($addflag != 1) ? '' : 'readonly'; ?>>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3">
                                <button <?php if ($addflag != 1) { ?> 
                                                        onclick="window.open('thermal_barcode3.php?barcode=<?= $qvouch ?>&vouchdt=<?= $vouchdt ?>&para=3&number=' + document.getElementById('nolabel').value, 
                                                        'popUpWindow', 'height=600,width=970,left=20,top=20, resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes');" 
                            <?php
                                } else {
                                    echo "disabled";
                                }
                                ?> class="btn" >
                                <i class="fa fa-barcode" style="font-size:20px"></i></button>

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

                    <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 pad">
                        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 pad">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad"></div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad"></div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad text-right">
                                <label class="label-control marg">Total Qty.</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" name="mtotqty" class="form-control form text-right mtotqty"  value="<?php echo ($addflag != 1) ? $get['totqty'] : "0"; ?>" readonly  tabindex="-1">
                            </div>
                        </div>
                        <!-- <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 pad">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad"></div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad"></div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad text-right">
                                <label class="label-control marg">Subtotal</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-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-md-12 col-sm-12 col-xs-12 pad">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad"></div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad"></div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad text-right">
                                <label class="label-control marg">Less Discount</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-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-md-12 col-sm-12 col-xs-12 pad">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad text-right">
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad text-right">
                                <label class="label-control marg">Net Amount</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="number" step="any" name="totbillamt" required="true"  class="form-control form text-right"  value="<?php echo ($addflag != 1) ? $get['billamt'] : "0"; ?>">
                            </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 PurchCreateModal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
        <div class="modal-dialog modal-lg" style="margin-top: 5%;">
            <div class="modal-content" style="width: 550px;border-radius:10px;">
                <div class="modal-header">
                    <label class="label-control marg"><b style="color:red"> Add Product </b></label>
                </div>

                <div class="modal-body">
                    
                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 numeric text-right  pad">
                            <label class="label-control marg">Barcode</label>
                        </div>
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                            <input type="text" class="form-control form mbarcode focusfield" maxlength="10" style="border: 1px solid #778da9;border-radius:4px" name="mbarcode" value="" required>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 numeric text-right  pad">
                            <label class="label-control marg">Name</label>
                        </div>
                        <div class="col-lg-6 col-md-6 col-sm-8 col-xs-8 pad">
                            <input type="text" class="form-control form mprodname" name="mprodname" maxlength="40" value="" style="border: 1px solid #778da9;border-radius:4px" required>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 numeric text-right  pad">
                            <label class="label-control marg">Packing Per Carton</label>
                        </div>
                        <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                            <input type="text" class="form-control form mpkg text-right" name="mpkg" value="" style="border: 1px solid #778da9;border-radius:4px" required>
                        </div>
                    </div>

                    <br>

                    <div class="row">
                        <div class="col-lg-3 text-center"></div>
                        <div class="col-lg-3 text-center">
                            <input type="button" class="btn button-29 saveent" value="Create">
                        </div>
                        <div class="col-lg-3 text-center">
                            <button class="button-29 close1" data-dismiss="modal">Close</button>
                        </div>
                    </div>

                    <div id="loadingIndicator" style="display: none;">
                        <div class="spinner"></div>
                        <span>Saving data, please wait...</span>
                    </div>
                    
                </div>
                <div class="modal-footer"></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%">Rate</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>
                            </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>
                                ">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="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>