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

$formname = 'Purchase GST';
$maindb = 'purmain';
$trandb = 'purtran';
$stkdb = 'transacn';
$accdb = 'actran';
$printfile = "purchgst_print.php";
$file = 'purchgst.php';
$trandbexist = 1;
$keyfld = "vouchno";
$source = "PU";

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

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

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

if (isset($_POST["submit"])) {
    $a = 1;
    $vouchno = $_POST["vouchno"];
    $invno = $_POST["invno"];
    $vouchdt = $_POST["vouchdt"];
    $party = $_POST["party"];
    $type = $_POST["type"];
    $subtotal = $_POST["subtotal"];
    $litemdisc = $_POST["litemdisc"];
    $cdiscperc = $_POST["cdiscperc"];
    $cdiscamt = $_POST["cdiscamt"];
    $lesother = $_POST["lesother"];
    $vatperc = $_POST["vatperc"];
    $vatamt = $_POST["vatamt"];
    $packing = $_POST["packing"];
    $netamt = $_POST["netamt"];
    $remark1 = $_POST["remark1"];
    $remark2 = $_POST["remark2"];

    $oms = $_POST["oms"];
    $taxamt = $_POST["taxamt"];
    $totcgst = $_POST["totcgst"];
    $totsgst = $_POST["totsgst"];
    $totigst = $_POST["totigst"];
    $sgstperc = $_POST["sgstperc"];
    $cgstperc = $_POST["cgstperc"];
    $igstperc = $_POST["igstperc"];
    $gstperc = $_POST["gstperc"];

    if ($igstperc > 0) {
        $gstp = $igstperc;
    } else {
        $gstp = $cgstperc + $sgstperc;
    }


    $count = $_POST["items"];


    $mvouch = $qvouch;

    $salecode = 11;

    if (isset($_POST["name"])) {
        $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `name`= '$name' AND vouchno != '$qvouch'") or die(mysqli_error($conn));
        if (mysqli_num_rows($result) > 0) {
            $a = 0;
            browse("Name Already Exist");
            header("refresh: 1; $file?$keyfld=$qvouch&mode=last");
        }
    }


    if ($a == 1) {


        $mcashvouch = $_POST["cashvouch"];

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


            $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);
                    $mrecpno = $gvouch["maxcode"] + 1;
                } else {
                    $mrecpno = '1';
                }
            }

            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','$netamt','Cash Paid For','Purchase No.$mvouch','S')") or die(mysqli_error($conn));


            // cash part  

            $minus_amt = ($netamt * -1);

//            if ($prodtype == 'P') {
//                $actamt = $netamt * 10;
//                $actminus = $netamt * -10;
//            } else {
//                $actamt = $netamt;
//                $actminus = $netamt * -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, vouchno, source, recpno, trnno, bankinit, accode,  grpcode, amount,prodtype,actamt)
                    VALUES ('$vouchdt','$mcashvouch', 'CP', '$mrecpno',  '001', 'COH', '3', '002', '$netamt','$prodtype','$netamt')");

            // party part
            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, recpno, trnno, bankinit, accode, grpcode, amount,prodtype,actamt)
                     VALUES ('$vouchdt','$mcashvouch', 'CP', '$mrecpno', '002', 'COH', '$party',  '$pgrp', '$minus_amt','$prodtype','$minus_amt')");
        } 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 = "";
        }




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

        mysqli_query($conn, "DELETE FROM `$trandb` WHERE `$keyfld`='$mvouch'") or die(mysqli_error($conn));
        mysqli_query($conn, "DELETE FROM `$stkdb` WHERE `$keyfld`='$mvouch' AND `source`='$source'")or die(mysqli_error($conn));
//        mysqli_query($conn, "DELETE FROM `pursacn` WHERE `$keyfld`='$mvouch' AND `source`='$source' ")or die(mysqli_error($conn));
//        mysqli_query($conn, "DELETE FROM $zroot.$combstkdb WHERE `$keyfld`='$mvouch' AND `source`='$source' and `compcode`='$compcd'")or die(mysqli_error($conn));
        $group = $source . $mvouch;

        $srl = 0;
        $totdisc = $cdiscamt + $lesother;
//        $avgdisc = $totdisc / $count;

        for ($i = 0; $i < $count; $i++) {
            $msrno = $_POST["srno_$i"];
            $mbarcode = $_POST["barcode_$i"];
            $mitem = $_POST["item_$i"];
            $mqty = $_POST["qty_$i"];
            $munit = $_POST["unit_$i"];
            $mmrp = $_POST["mrp_$i"];
            $mrate = $_POST["rate_$i"];
            $mamount = $_POST["amount_$i"];
            $mdiscperc = $_POST["discperc_$i"];
            $mdiscount = $_POST["discount_$i"];
            $mtotal = $_POST["total_$i"];
            $msp = $_POST["msp_$i"];
            $hsncode = $_POST["hsncode_$i"];
            $mactbatch = $source . $mvouch . $mbatch;

            $avgdisc = ($totdisc * $mamount) / $subtotal;

            $purgstamt = $mtotal * ($gstp / 100);
            $actcost = $mtotal + $purgstamt;



            if (!empty($mitem)) {
                $chkmsp = mysqli_query($conn, "SELECT * FROM `item` WHERE `code`= '$mbarcode'") or die(mysqli_error($conn));
                $gmsp = mysqli_fetch_array($chkmsp);
                $valmsp = $gmsp['msp'];
                $sellrate = $gmsp['rate'];
                $pdtype = $gmsp['prodtype'];
                if ($pdtype == "O") {
                    $profperc = 8;
                } else {
                    $profperc = 12;
                }

                $sellamt = $sellrate * $mqty;
                $sellgstamt = $sellamt + ($sellrate * $gstp / 100);
                $discbill = $avgdisc;
                $purgstrate = $actcost + ($actcost * $gstp / 100);
                $costeach = $actcost / $mqty;
                $profamt = $costeach * ($profperc / 100);
                $approxrate = round($costeach + $profamt, 0);
                $approxamt = round($approxrate * $mqty, 0);


                $srl = $srl + 1;

                mysqli_query($conn, "INSERT INTO `$trandb`(`vouchno`, `vouchdt`, `srno`, `itemcode`, `qty`, `unit`, `mrp`,
                                                            `rate`, `amount`, `discperc`, `discount`, `total`, `msp`, `hsncode`)
                                            VALUES('$mvouch', '$vouchdt', '$srl', '$mbarcode', '$mqty', '$munit', '$mmrp',
                                           '$mrate', '$mamount', '$mdiscperc', '$mdiscount', '$mtotal', '$msp', '$hsncode')") or die(mysqli_error($conn));


                #################################STOCK PART #################################

                mysqli_query($conn, "INSERT INTO `$stkdb`(`srno`, `compcode`, `yearno`, `vouchno`, `vouchdt`, `source`, `barcode`, `itemcode`,  `qty`, `unit`, `mrp`, `rate`, `amount`)
                                        VALUES( '$srl', '$compcd', '$myyear', '$qvouch', '$vouchdt', '$source', '$mbarcode', '$mbarcode', '$mqty', '$munit', '$mmrp', '$mrate', '$mamount')") or die(mysqli_error($conn));

                #################################PURSACN PART #################################
//                mysqli_query($conn, "INSERT INTO `pursacn`(`pursrno`, `compcode`, `yearno`, `vouchno`, `purvouch`, `vouchdt`, `source`, `barcode`, `itemcode`,  `prodtype`,  `qty`, `unit`, `mrp`, `rate`, `amount`, 
//                                                  `purgstamt`, `hsncode`, `sellrate`, `sellamt`, `gst`, `sellgstamt`, `discbill`, `actcost`,`discperc`,`discount`,`total`,
//                                                  `costeach`, `profperc`, `profamt`, `approxrate`, `approxamt`)
//                                        VALUES( '$msrno', '$compcd', '$myyear', '$qvouch', '$qvouch', '$vouchdt', '$source', '$mbarcode', '$mbarcode', '$pdtype', '$mqty', '$munit', '$mmrp', '$mrate', '$mamount',
//                                                '$purgstamt', '$hsncode', '$sellrate', '$sellamt', '$gstp', '$sellgstamt', '$discbill', '$actcost','$mdiscperc','$mdiscount','$mtotal',
//                                                '$costeach', '$profperc', '$profamt', '$approxrate',  '$approxamt')") or die(mysqli_error($conn));
                #################################COMBINED STOCK PART(For Combine) #################################
//                mysqli_query($conn, "INSERT INTO $zroot.$combstkdb(`srno`, `compcode`, `yearno`, `vouchno`, `vouchdt`, `source`, `barcode`, `itemcode`,  `qty`, `unit`, `mrp`, `rate`, `amount`)
//                                        VALUES( '$srl',  '$compcd', '$myyear', '$qvouch', '$vouchdt', '$source', '$mbarcode', '$mbarcode', '$mqty', '$munit', '$mmrp', '$mrate', '$mamount')") or die(mysqli_error($conn));
                ################Update MSP in Item#############


                if ($mmsp != $valmsp) {
                    mysqli_query($conn, "UPDATE `item` SET `msp`='$mmsp'  WHERE `code`= '$mbarcode' ") or die(mysqli_error($conn));
                }
            }
        }

        ############################### Accounts Part ######################################################
        mysqli_query($conn, "DELETE FROM `$accdb` WHERE `vouchno` = '$mvouch' AND `source`='$source'") OR die(mysqli_error($conn));

        if ($netamt > 0) {
            $minus_amt = ($netamt * -1);
//            if ($prodtype == 'P') {
//                $actamt = $netamt * 10;
//                $actminus = $netamt * -10;
//            } else {
//                $actamt = $netamt;
//                $actminus = $netamt * -1;
//            }
// purchase part
            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, grpcode, amount,prodtype,actamt)
                    VALUES ('$vouchdt','$mvouch', '$source', '001', '$salecode','004', '$minus_amt','$prodtype','$minus_amt')") OR die(mysqli_error($conn));
// party part
            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, grpcode, amount,prodtype,actamt)
                     VALUES ('$vouchdt','$mvouch', '$source', '002', '$party','009', '$netamt','$prodtype','$netamt')") or die(mysqli_error($conn));
        }



        ############################## MAIN ######################################################
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `invno`, `vouchdt`, `party`, `cashcred`, `subtotal`, `litemdisc`, `cdiscperc`, 
                                            `cdiscamt`, `discount`, `lesother`, `vatperc`, `vatamt`, `packing`, `netamt`, `remark1`, 
                                            `remark2`,`cashvouch`, `oms`, `cgstperc`, `sgstperc`, `igstperc`, `totcgst`, `totsgst`, 
                                            `totigst`, `tottaxamt`, `gstperc`) 
                                     VALUES($qvouch, '$invno', '$vouchdt', '$party', '$type', '$subtotal', '$litemdisc', '$cdiscperc',
                                            '$cdiscamt', '$discount', '$lesother', '$vatperc', '$vatamt', '$packing', '$netamt', '$remark1', 
                                            '$remark2','$mcashvouch','$oms', '$cgstperc', '$sgstperc', '$igstperc', '$totcgst', '$totsgst',
                                             '$totigst', '$taxamt', '$gstperc')")or die(mysqli_error($conn));

            insmess();
            $mvouch = $qvouch; //1
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            $mvouch = $qvouch; //1

            mysqli_query($conn, "UPDATE `$maindb` SET `vouchdt`='$vouchdt', `invno`='$invno', `party`='$party', `cashcred`='$type', `subtotal`='$subtotal',
                                               `litemdisc`='$litemdisc', `cdiscperc`='$cdiscperc', `cdiscamt`='$cdiscamt', `discount`='$discount',
                                               `lesother`='$lesother', `vatperc`='$vatperc', `vatamt`='$vatamt', `packing`='$packing', `cashvouch`='$mcashvouch',
                                               `netamt`='$netamt', `remark1`='$remark1', `remark2`='$remark2' , `oms`='$oms',`cgstperc`='$cgstperc',
                                                `sgstperc`='$sgstperc', `igstperc`='$igstperc', `totcgst`='$totcgst', `totsgst`='$totsgst', 
                                                `totigst`='$totigst', `tottaxamt`='$taxamt',`gstperc`='$gstperc'   WHERE `vouchno`= '$mvouch' ") or die(mysqli_error($conn));
            updmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        }
    }
}

if ($addflag != 1) {

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

    $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE $keyfld='$getvouch'") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);

    $delcsh = $get[cashvouch];

//    $findtype = mysqli_query($conn, "SELECT * FROM `cepheisy_gujtran_root`.`branch` WHERE `code`='$get[name]'") or die(mysqli_error($conn));
//    $ltype = mysqli_fetch_array($findtype);

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

    $getgst = mysqli_query($conn, "SELECT * FROM `saletype` WHERE code='$get[gstperc]'") or die(mysqli_error($conn));
    $fetgst = mysqli_fetch_array($getgst);
}

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

if (isset($_GET["delete"])) {

    $chekpur = mysqli_query($conn, "Select vouchno,purvouch,pursrno from pursacn where purvouch = '$keyfld' and source = 'SL'") or die(mysqli_error($conn));
    $numpur = mysqli_num_rows($chekpur);

    if ($numpur > 0) {
        browse('Cannot Delete. Used in Autosale Part');
        header("refresh: 1; $file?$keyfld=$_GET[vouchno]&mode=edit");
    } else {
//        browse('udd');
        delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
        delete($zdb, "$trandb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
        $delcond = " source = '$source' and vouchno = '$_GET[vouchno]' ";
        $delcond2 = " source = '$source' and vouchno = '$_GET[vouchno]' and `compcode` = '$compcd' ";
        $delcond1 = " source = 'CP' and vouchno = '$delcsh' ";
        $delcond3 = " source = 'PU' and vouchno =  '$_GET[vouchno]' and `compcode` = '$compcd'";

        delnew($zdb, "$stkdb", "$delcond", "$file?mode=last");
        delnew($zdb, "$accdb", "$delcond", "$file?mode=last");
        delnew($zdb, "$accdb", "$delcond1", "$file?mode=last");
        delete($zdb, "cashpay", "$keyfld", $delcsh, "$file?mode=last");
//        delnew($zdb, "pursacn", "$delcond", "$file?mode=last");
//        delnew($zroot, "$combstkdb", "$delcond2", "$file?mode=last");
    }
}

include("includes/linkshead.php");
?>
<script src="MyScript/purchgst.js" type="text/javascript"></script>
</head>
<body>

    <form action='' method='POST'>
        <!-- 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"><?php Echo $formname; ?></h3>
                        <div class="actions pull-right">
                            <i class="fa fa-chevron-down"></i>
                            <i class="fa fa-times"></i>
                        </div>
                    </div>
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-lg-1 col-md-1 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Voucher</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form" name="vouchno" value="<?php Echo ($addflag != 1) ? $get["vouchno"] : " $qvouch"; ?>" readonly required>
                                <input type="hidden" class="form-control form" name="cashvouch" value="<?php Echo ($addflag != 1) ? $get["cashvouch"] : ""; ?>" readonly>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Date</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-8 col-xs-8 pad">
                                <input type="date" class="form-control form" name="vouchdt" value="<?php Echo ($addflag != 1) ? $get["vouchdt"] : date("Y-m-d"); ?>" required>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Invoice No.</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form focusfield" name="invno" value="<?php Echo ($addflag != 1) ? $get["invno"] : ""; ?>"  autofocus required>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Type</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-8 col-xs-8 pad">
                                <select class="form-control form type select2" name="type" style="width: 100%" required>

                                    <option value=""></option>
                                    <option value='C'<?php
                                    if ($addflag != 1) {
                                        if ($get["cashcred"] == "C") {
                                            echo "selected='selected'";
                                        }
                                    }
                                    ?>>Cash</option>
                                    <option value='R'<?php
                                    if ($addflag != 1) {
                                        if ($get["cashcred"] == "R") {
                                            echo "selected='selected'";
                                        }
                                    }
                                    ?>>Credit</option>
                                </select>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-1 col-md-1 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Party</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-8 col-xs-8 pad">
                                <select class="form-control form  select2" name="party" style="width: 100%" required>
                                    <option value="<?php Echo ($addflag != 1) ? $get["party"] : ""; ?>"><?php Echo ($addflag != 1) ? $lparty["name"] : ""; ?></option>
                                    <?php
                                    $gparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `grpcode`='009'")or die(mysqli_error($conn));
                                    while ($party = mysqli_fetch_array($gparty)) {
                                        Echo "<option value='$party[accode]'>$party[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>

                            <div class="col-lg-1 col-md-1 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Out OF State</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                                <input type="checkbox"  class="form-control form oms" <?php
                                if ($get[oms] == "1") {
                                    Echo "checked";
                                }
                                ?> value="1" name="oms">                            
                            </div>

                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-2 text-right pad">
                                <label class="label-control marg" style="color: #78909C">GST %</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-2 pad">
                                <select name="gstperc" class="form-control form gstperc ">
                                    <option value="<?php Echo ($addflag != 1) ? $fetgst["code"] : ""; ?>"><?php Echo ($addflag != 1) ? $fetgst["name"] : ""; ?></option>
                                    <?
                                    $getgst = mysqli_query($conn, "SELECT * FROM `saletype` where `code`!='' ORDER BY per")or die(mysqli_error($conn));
                                    while ($fetgst = mysqli_fetch_array($getgst)) {
                                        Echo "<option value='$fetgst[code]'>$fetgst[name]</option>";
                                    }
                                    ?>
                                </select>
        <!--<input type="text" step="any" class="form-control form gstperc text-right" name="gstperc" value="<?php // Echo ($addflag != 1) ? $get["gstperc"] : "";            ?>">--> 
                            </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-heading">
                        <div class="actions pull-right">
                            <i class="fa fa-chevron-down"></i>
                        </div>
                    </div>
                    <div class="panel-body">
                        <div class="table-responsive">
                            <table class="table table-striped table-condensed table-bordered table-padd" width="100%">
                                <thead  class="mySearchTr">
                                    <tr>
                                        <th width="2%"></th>
                                        <th width="3%" class='text-right'>Sr.</th>
                                        <th width="7%">Barcode</th>
                                        <th width="15%">Itemname</th>
                                        <th width="8%">Hsn Code</th>
                                        <th width="6%" class='text-right'>Qty.</th>
                                        <th width="9%">Unit</th>
                                        <th width="7%" class='text-right'>MRP.</th>
                                        <th width="7%" class='text-right'>Rate</th>
                                        <th width="7%" class='text-right'>Amount</th>
                                        <th width="7%" class='text-right'>Discperc</th>
                                        <th width="7%" class='text-right'>Discount</th>
                                        <th width="7%" class='text-right'>Total</th>
                                        <th width="7%" class='text-right'>MSP</th>
                                    </tr>
                                </thead>
                                <tbody class="tbody">
                                    <?php
                                    if ($addflag != 1) {
                                        $ctr = 0;
                                        $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$get[vouchno]' ORDER BY `srno`")or die(mysqli_error($conn));
                                        while ($tran = mysqli_fetch_array($gettran)) {
                                            $finditem = mysqli_query($conn, "SELECT * FROM `item` WHERE `code`='$tran[itemcode]'") or die(mysqli_error($conn));
                                            $fitem = mysqli_fetch_array($finditem);
//                                            $findunit = mysqli_query($conn, "SELECT * FROM `unit` WHERE `code`='$tran[unit]'") or die(mysqli_error($conn));
//                                            $funit = mysqli_fetch_array($findunit);

                                            Echo "<tr>";
                                            Echo "<td><i class='fa fa-close remove' style='display:none;'></i></td>";
                                            Echo "<td><input type='text' class='form-control form text-right srno'     name='srno_$ctr'             value='$tran[srno]'></td>";

                                            Echo "<td><input type='text' class='form-control form itemcode ' name='barcode_$ctr' value='$tran[itemcode]'></td>";
                                            Echo "<td>";
                                            Echo "<select class='form-control form fetchpart_$ctr item' name='item_$ctr' style='width:100%'>";
                                            Echo "<option value='$fitem[code]'>$fitem[name]</option>";
                                            $gitem = mysqli_query($conn, 'SELECT * FROM `item` order by name')or die(mysqli_error($conn));
                                            while ($item = mysqli_fetch_array($gitem)) {
                                                Echo "<option value='$item[code]'>$item[name]</option>";
                                            }
                                            Echo "</select>";
                                            Echo "</td>";
                                            Echo "<td><input type='text' class='form-control form  hsncode'   name='hsncode_$ctr'         value='$tran[hsncode]'></td>";
                                            Echo "<td><input type='text' class='form-control form text-right qty'       name='qty_$ctr'         value='$tran[qty]'></td>";
                                            Echo "<td>";
                                            Echo "<select class='form-control form fetchpart_$ctr unit select2' name='unit_$ctr' style='width:100%'>";
                                            Echo "<option value='$tran[unit]'>$tran[unit]</option>";
                                            $gunit = mysqli_query($conn, 'SELECT * FROM `unit` order by name')or die(mysqli_error($conn));
                                            while ($unit = mysqli_fetch_array($gunit)) {
                                                Echo "<option value='$unit[name]'>$unit[name]</option>";
                                            }
                                            Echo "</select>";
                                            Echo "</td>";
                                            Echo "<td><input type='text' class='form-control form text-right mrp'       name='mrp_$ctr'        value='$tran[mrp]' readonly></td>";
                                            Echo "<td><input type='text' class='form-control form text-right rate'      name='rate_$ctr'        value='$tran[rate]'></td>";
                                            Echo "<td><input type='text' class='form-control form text-right amount'    name='amount_$ctr'      value='$tran[amount]' readonly></td>";
                                            Echo "<td><input type='text' class='form-control form text-right discperc'  name='discperc_$ctr'      value='$tran[discperc]'></td>";
                                            Echo "<td><input type='text' class='form-control form text-right discount'  name='discount_$ctr'      value='$tran[discount]' readonly></td>";
                                            Echo "<td><input type='text' class='form-control form text-right total'     name='total_$ctr'      value='$tran[total]' readonly></td>";
                                            Echo "<td><input type='text' class='form-control form text-right msp'       name='msp_$ctr'      value='$tran[msp]'></td>";
                                            Echo "</tr >";
                                            $ctr++;
                                        }
                                    } else {
                                        $pctr = 0;
                                        ?>
                                        <tr>
                                            <td><i class="fa fa-close remove"></i></td>
                                            <td><input type="text" class="form-control form text-right srno" name="srno_<?= $pctr ?>" value="1"></td>
                                            <td><input type="text" class="form-control form itemcode barcode" name="barcode_<?= $pctr ?>" value="" ></td>
                                            <td>
                                                <select class="form-control form select2 item" name="item_<?= $pctr ?>" style='width:100%'>
                                                    <option value=""></option>
                                                    <?php
                                                    $gitem = mysqli_query($conn, "SELECT * FROM `item`")or die(mysqli_error($conn));
                                                    while ($item = mysqli_fetch_array($gitem)) {
                                                        Echo "<option value='$item[code]'>$item[name]</option>";
                                                    }
                                                    ?>
                                                </select>
                                            </td>
                                            <td><input type="text" class="form-control form hsncode"  name="hsncode_<?= $pctr ?>"        value=""></td>
                                            <td><input type="text" class="form-control form text-right qty"     name="qty_<?= $pctr ?>"        value="0"></td>
                                            <td>
                                                <select class='form-control form select2 unit' name="unit_<?= $pctr ?>" style='width:100%'>
                                                    <option value=""></option>
                                                    <?php
                                                    $gunit = mysqli_query($conn, 'SELECT * FROM `unit` order by name')or die(mysqli_error($conn));
                                                    while ($unit = mysqli_fetch_array($gunit)) {
                                                        Echo "<option value='$unit[name]'>$unit[name]</option>";
                                                    }
                                                    ?>
                                                </select>
                                            </td>
                                            <td><input type="text" class="form-control form text-right mrp"         name="mrp_<?= $pctr ?>"       value="0" readonly></td>
                                            <td><input type="text" class="form-control form text-right rate"        name="rate_<?= $pctr ?>"       value="0"></td>
                                            <td><input type="text" class="form-control form text-right amount"      name="amount_<?= $pctr ?>"     value="0" readonly></td>
                                            <td><input type="text" class="form-control form text-right discperc"    name="discperc_<?= $pctr ?>"     value="0"></td>
                                            <td><input type="text" class="form-control form text-right discount"    name="discount_<?= $pctr ?>"     value="0" readonly></td>
                                            <td><input type="text" class="form-control form text-right total"       name="total_<?= $pctr ?>"     value="0" readonly></td>
                                            <td><input type="text" class="form-control form text-right msp"       name="msp_<?= $pctr ?>"     value="0"></td>
                                        </tr>  
                                        <?
                                        $pctr++;
                                    }
                                    ?>
                                </tbody>
                            </table>
                        </div>


                        <div class="col-lg-12 col-md-12 pad">
<!--                            <input type="button" style="height: 30px; width: 30px; padding: 0px" id="addnew" class="btn btn-success btn-lg form" accode="addnew2" value="+" />-->
                            <button type="button" class="btn btn-success btn-square form"  id="addnew">
                                <i class="fa fa-plus" style="color:#ffffff" data-toggle='tooltip' title='Add Row'></i>    
                            </button>
                            <input type="hidden" id="items" name="items" value="<?php echo ($addflag != 1) ? $ctr : $pctr; ?>" />  
                        </div>
                        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12  pad">
                            <div class="panel panel-default">

                                <div class="panel-body">
                                    <div class="col-lg-12 col-md-12 col-xs-12 pad">
                                        <div class="col-lg-6 col-md-6 col-xs-12 pad">
                                        </div>

                                        <div class="col-lg-6 col-md-6 col-xs-12 pad">
                                            <div class="col-lg-12 col-md-12 col-xs-12 pad">
                                                <div class="col-lg-3 col-md-3 pad"></div>
                                                <div class="col-lg-2 col-md-2 pad">
                                                    <label class="label-control marg" style="color: red">SubTotal</label>
                                                </div>
                                                <div class="col-lg-3 col-md-3 pad text-center">
                                                </div>
                                                <div class="col-lg-4 col-md-4 pad">
                                                    <input type="text" class="form-control form subtotal text-right" value="<? Echo ($addflag != 1) ? $get[subtotal] : "0"; ?>" name="subtotal" readonly>
                                                </div>
                                            </div>
                                            <div class="col-lg-12 col-md-12 col-xs-12 pad">
                                                <div class="col-lg-3 col-md-3 pad"></div>
                                                <div class="col-lg-4 col-md-4 pad">
                                                    <label class="label-control marg">Less Item Discount</label>
                                                </div>
                                                <div class="col-lg-2 col-md-2 pad text-center">
                                                </div>
                                                <div class="col-lg-3 col-md-3 pad">
                                                    <input type="text" class="form-control form litemdisc text-right" value="<? Echo ($addflag != 1) ? $get[litemdisc] : "0"; ?>" name="litemdisc" readonly>
                                                </div>
                                            </div>
                                            <div class="col-lg-12 col-md-12 col-xs-12 pad">
                                                <div class="col-lg-3 col-md-3 pad"></div>
                                                <div class="col-lg-3 col-md-4 pad">
                                                    <label class="label-control marg">Less Cash Discount</label>
                                                </div>
                                                <div class="col-lg-2 col-md-1 pad">
                                                    <input type="text" class="form-control form cdiscperc text-right" value="<? Echo ($addflag != 1) ? $get[cdiscperc] : "0"; ?> "  name="cdiscperc">
                                                </div>
                                                <div class="col-lg-1 col-md-1 pad text-center">
                                                    <label class="label-control marg ">%</label>
                                                </div>
                                                <div class="col-lg-3 col-md-3 pad">
                                                    <input type="text" class="form-control form cdiscamt text-right" value="<? Echo ($addflag != 1) ? $get[cdiscamt] : "0"; ?>" name="cdiscamt" readonly>
                                                </div>
                                            </div>
                                            <div class="col-lg-12 col-md-12 col-xs-12 pad">
                                                <div class="col-lg-3 col-md-3 pad"></div>
                                                <div class="col-lg-3 col-md-3 pad">
                                                    <label class="label-control marg">Less Discount</label>
                                                    <!--<label class="label-control marg">Less Other</label>-->
                                                </div>
                                                <div class="col-lg-3 col-md-3 pad"></div>
                                                <div class="col-lg-3 col-md-3 pad">
                                                    <input type="text" class="form-control form lesother text-right" value="<? Echo ($addflag != 1) ? $get[lesother] : "0"; ?>" name="lesother" >
                                                </div>
                                            </div>

                                            <div class="col-lg-12 col-md-12 col-xs-12 pad">
                                                <div class="col-lg-3 col-md-3 pad"></div>
                                                <div class="col-lg-4 col-md-4 pad">
                                                    <label class="label-control marg">Add Transport/Packing</label>
                                                </div>
                                                <div class="col-lg-2 col-md-2 pad"></div>
                                                <div class="col-lg-3 col-md-3 pad">
                                                    <input type="text" class="form-control form packing text-right" value="<? Echo ($addflag != 1) ? $get[packing] : "0"; ?>" name="packing" >
                                                </div>
                                            </div>
                                            <div class="col-lg-12 col-md-12 col-xs-12 pad">
                                                <div class="col-lg-3 col-md-3 pad"></div>
                                                <div class="col-lg-3 col-md-3  pad">
                                                    <label class="label-control marg">Taxable Value</label>
                                                    <!--<label class="label-control marg">Less Other</label>-->
                                                </div>
                                                <div class="col-lg-3 col-md-3 pad"></div>
                                                <div class="col-lg-3 col-md-3 pad">
                                                    <input type="text" class="form-control form taxamt numeric" value="<? Echo ($addflag != 1) ? $get[tottaxamt] : "0"; ?>" name="taxamt" readonly>
                                                </div>
                                            </div>
                                            <div class="col-lg-12 col-xs-12 pad">
                                                <div class="col-lg-3 pad">   
                                                    <!--<input type="button" name="gendsotp" class="form-control gendsotp form" style="color:red;border: none;" value="Generate Discount OTP">-->
                                                </div>
                                                <div class="col-lg-3 pad ">
                                                    <label class="label-control marg">Add CGST</label>
                                                </div>
                                                <div class="col-lg-2 pad">
                                                    <input type="text" class="form-control form cgstperc numeric" value="<? Echo ($addflag != 1) ? $get[cgstperc] : "$gstperc"; ?> "  name="cgstperc" readonly>
                                                </div>
                                                <div class="col-lg-1 pad">
                                                    <label class="label-control marg">%</label>
                                                </div>
                                                <div class="col-lg-3 pad">
                                                    <input type="text" class="form-control form cgstamt numeric" value="<? Echo ($addflag != 1) ? $get[totcgst] : "0"; ?>" name="totcgst" readonly>
                                                </div>
                                            </div>
                                            <div class="col-lg-12 col-xs-12 pad">
                                                <div class="col-lg-3 pad">   
                                                    <!--<input type="button" name="gendsotp" class="form-control gendsotp form" style="color:red;border: none;" value="Generate Discount OTP">-->
                                                </div>
                                                <div class="col-lg-3 pad ">
                                                    <label class="label-control marg">Add SGST</label>
                                                </div>
                                                <div class="col-lg-2 pad">
                                                    <input type="text" class="form-control form sgstperc numeric" value="<? Echo ($addflag != 1) ? $get[sgstperc] : "$gstperc"; ?> "  name="sgstperc" readonly>
                                                </div>
                                                <div class="col-lg-1 pad">
                                                    <label class="label-control marg">%</label>
                                                </div>
                                                <div class="col-lg-3 pad">
                                                    <input type="text" class="form-control form sgstamt numeric" value="<? Echo ($addflag != 1) ? $get[totsgst] : "0"; ?>" name="totsgst" readonly>
                                                </div>
                                            </div>
                                            <div class="col-lg-12 col-xs-12 pad">
                                                <div class="col-lg-3 pad">   
                                                    <!--<input type="button" name="gendsotp" class="form-control gendsotp form" style="color:red;border: none;" value="Generate Discount OTP">-->
                                                </div>
                                                <div class="col-lg-3 pad">
                                                    <label class="label-control marg">Add IGST</label>
                                                </div>
                                                <div class="col-lg-2 pad">
                                                    <input type="text" class="form-control form igstperc numeric" value="<? Echo ($addflag != 1) ? $get[igstperc] : "$gstperc"; ?> "  name="igstperc" readonly>
                                                </div>
                                                <div class="col-lg-1 pad">
                                                    <label class="label-control marg">%</label>
                                                </div>
                                                <div class="col-lg-3 pad">
                                                    <input type="text" class="form-control form igstamt numeric" value="<? Echo ($addflag != 1) ? $get[totigst] : "0"; ?>" name="totigst" readonly>
                                                </div>
                                            </div>
                                            <div class="col-lg-12 col-md-12 col-xs-12 pad">
                                                <div class="col-lg-3 col-md-3 pad"></div>
                                                <div class="col-lg-3 col-md-3 pad">
                                                    <label class="label-control marg" style="color: blue">Netamt (Rs.)</label>
                                                </div>
                                                <div class="col-lg-2 col-md-2 pad"></div>
                                                <div class="col-lg-4 col-md-4 pad">
                                                    <input type="text" class="form-control form netamt text-right" readonly value="<? Echo ($addflag != 1) ? $get[netamt] : "0"; ?>" name="netamt">
                                                </div>
                                            </div>

                                        </div>
                                    </div>
                                    <div class="col-lg-12 col-md-12 col-xs-12 pad">
                                        <div class="col-lg-1 col-md-1 pad text-right">
                                            <label class="label-control marg">Remarks</label>
                                        </div>
                                        <div class="col-lg-6 col-md-6 pad">
                                            <input type="text" name="remark1" class="form-control form remark1" value="<? Echo ($addflag != 1) ? $get["remark1"] : ""; ?>">
                                        </div>
                                    </div>
                                    <div class="col-lg-12 col-md-12 col-xs-12 pad">
                                        <div class="col-lg-1 col-md-1 pad text-right"></div>
                                        <div class="col-lg-6 col-md-6 pad">
                                            <input type="text" name="remark2" class="form-control form remark2" value="<? Echo ($addflag != 1) ? $get["remark2"] : ""; ?>">
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="panel panel-default">
                <div class="panel-body">
                    <div class="col-lg-11 col-md-10 col-sm-11 col-xs-11 pad">
                        <?php include("includes/buttons.php"); ?>
                    </div>
                    <div class="col-lg-1 col-md-2 col-sm-2 col-xs-2 pad">
                        <?php if ($addflag != 1) { ?> 
                                <a href="#" onclick="window.open('thermal_barcode3.php?vouchno=<?= $qvouch ?>&vouchdt=<?= $get[vouchdt] ?>&para=1', 'popUpWindow', 'height=600,width=970,left=20,top=20, resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no, status=yes');"><i class="fa fa-barcode btn" style="color:black;font-size:2.5em;"></i></a>
                                    <?php
                                } else {
                                    echo "disabled";
                                }
                                ?>
                    </div>
                    <div class="col-lg-2">
                    </div>
                </div>
            </div>
        </div>
    </form>
    <div class="modal fade bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" data-dismiss="modal"><span>&times;</span></button>
                    <h4 class="modal-title" id="myLargeModalLabel">Search <? Echo $formname; ?></h4>
                </div>
                <div class="modal-body">
                    <div class="">
                        <table id="example1" class="table table-striped">
                            <thead>
                                <tr class="mySearchTr">
                                    <th style="text-align:center">Voucher</th>
                                    <th style="text-align:center">Date</th>
                                    <th style="text-align:center">Party</th>
                                    <th style="text-align:center">Type</th>
                                    <th style="text-align:center">Discount</th>
                                    <th style="text-align:center">Net Amount</th>
                                    <th style="text-align:center">Action</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php
                                if ($addflag != 1) {
                                    $srchresult = mysqli_query($conn, "SELECT `vouchno`,`vouchdt`,`party`,`cashcred`,`netamt`,`litemdisc` FROM `$maindb` ORDER BY `vouchdt` DESC")or die(mysqli_error($conn));
                                    while ($row = mysqli_fetch_array($srchresult)) {
                                        $getsparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$row[party]'")or die(mysqli_error($conn));
                                        $sparty = mysqli_fetch_array($getsparty);
                                        echo "<tr class='clickable-row' data-href='$file?mode=edit&$keyfld=$row[$keyfld]'>";
                                        echo "<td align='center'>$row[vouchno]</td>";
                                        echo "<td align='center'>$row[vouchdt]</td>";
                                        echo "<td align='center'>$sparty[name]</td>";
                                        echo "<td align='center'>$row[cashcred]</td>";
                                        echo "<td align='center'>$row[litemdisc]</td>";
                                        echo "<td align='center'>$row[netamt]</td>";
                                        echo "<td align='center'><a href=\"$file?mode=edit&$keyfld=$row[$keyfld]\"><i class='fa fa-pencil' style='font-size:1.3em;color:#00C0EF;cursor:pointer;'></i></a></td>";
                                        echo "</tr>";
                                    }
                                }
                                ?>
                            </tbody>
                        </table>
                    </div>
                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>
