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

$formname = 'Sale';
$maindb = 'salemain';
$trandb = 'saletran';
$stkdb = 'transacn';
$accdb = 'actran';
$cashdb = 'cashrecp';
$printfile = "sale_print.php";
$file = 'sale.php';
$trandbexist = 1;
$keyfld = "vouchno";
$source = "SL";

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

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

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

if (isset($_POST["submit"])) {
    $a = 1;
    $invno = $_POST["invno"];
    $vouchdt = $_POST["vouchdt"];
    $invdt = $_POST["invdt"];
    $party = $_POST["party"];
    $type = $_POST["type"];
    $prodtype = $_POST["prodtype"];
    $salesman = $_POST["salesman"];
    $driver = $_POST["driver"];
    $dcperc = $_POST["dcperc"];
    $dcbperc = $_POST["dcbperc"];
    $dcamt = $_POST["dcamt"];
    $subtotal = $_POST["subtotal"];
    $litemdisc = $_POST["litemdisc"];
    $cdiscperc = $_POST["cdiscperc"];
    $cdiscamt = $_POST["cdiscamt"];
    $coukg = $_POST["coukg"];
    $courate = $_POST["courate"];
    $couamt = $_POST["couamt"];
    $netamt = $_POST["netamt"];
    $ladvance = $_POST["ladvance"];
    $lesother = $_POST["lesother"];
    $balance = $_POST["balance"];
    $remark1 = $_POST["remark1"];
    $remark2 = $_POST["remark2"];

    $count = $_POST["items"];

    $mvouch = $qvouch;
    if ($type == 'C') {
        $salecode = 14;
    } else {
        $salecode = 15;
    }

 
    if ($a == 1) {

        $mcashvouch = $_POST["cashvouch"];

        if ($type == 'C' || $ladvance > 0) {
            if ($mcashvouch == '0') {
                $gen1 = mysqli_query($conn, "SELECT MAX(vouchno) AS maxcode FROM `$cashdb`  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 `$cashdb` 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 maxrecp FROM `$cashdb` where bankname = '3'  LIMIT 0,1 ") or die(mysqli_error($conn));
//                $gvouch = mysqli_fetch_assoc($gen2);
                if (mysqli_num_rows($gen2) > 0) {
                    $gvouch = mysqli_fetch_assoc($gen2);
                    $mrecpno = $gvouch["maxrecp"] + 1;
                } else {
                    $mrecpno = '1';
                }
            }
            if ($ladvance > 0) {
                $tamt = $ladvance;
                $adv = "Advance Received For";
            } else {
                $tamt = $netamt;
                $adv = "Cash Received For";
            }

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


            mysqli_query($conn, "INSERT INTO `$cashdb`( vouchno ,`vouchdt`, `bankname`, `bankinit`, `recpno`, `party`,`amount`,`remark1`,`remark2`,`trnflag`)
                                       Values ('$mcashvouch', '$vouchdt', '3', 'COH', '$mrecpno','$party','$tamt','$adv','Sale No.$mvouch','S')") or die(mysqli_error($conn));


            // cash part  

            $minus_amt = ($tamt * -1);

            mysqli_query($conn, "DELETE FROM `$accdb` WHERE `$keyfld`='$mcashvouch' and `source` = 'CR'") 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, recpno, source, trnno, bankinit, accode, grpcode, amount,trnflag)
                    VALUES ('$vouchdt','$mcashvouch', '$mrecpno', 'CR', '001', 'COH', '3','002', '$minus_amt','S')") or die(mysqli_error($conn));

            // party part
            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, recpno, source, trnno, bankinit, accode, grpcode, amount,trnflag)
                    VALUES ('$vouchdt','$mcashvouch', '$mrecpno', 'CR', '002', 'COH', '$party', '$pgrp', '$tamt','S')") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "DELETE FROM `$cashdb` WHERE `$keyfld`='$mcashvouch'") or die(mysqli_error($conn));
            mysqli_query($conn, "DELETE FROM `$accdb` WHERE `$keyfld`='$mcashvouch' AND `source`='CR'")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 $zroot.$combstkdb WHERE `$keyfld`='$mvouch' AND `source`='$source' and `compcode`='$compcd'")or die(mysqli_error($conn));
        $group = $source . $mvouch;

        $srl = 0;
        for ($i = 0; $i < $count; $i++) {
            $msrno = $_POST["srno_$i"];
            $barcode = $_POST["barcode_$i"];
            $mitem = $_POST["item_$i"];
            $unit = $_POST["unit_$i"];
            $qty = $_POST["qty_$i"];
            $mrp = $_POST["mrp_$i"];
            $msp = $_POST["msp_$i"];
            $rate = $_POST["rate_$i"];
            $amount = $_POST["amount_$i"];
            $discperc = $_POST["discperc_$i"];
            $discount = $_POST["discount_$i"];
            $total = $_POST["total_$i"];

            $stkqty = $qty * -1;

            if (!empty($mitem)) {
                $srl = $srl + 1;

                mysqli_query($conn, "INSERT INTO `$trandb`(`vouchno`, `vouchdt`, `srno`, `barcode`, `item`, `qty`, `unit`, 
                                `mrp`, `msp`, `rate`, `amount`, `discperc`, `discount`, `total`) 
                    VALUES ('$qvouch', '$vouchdt', '$srl', '$barcode', '$mitem', '$qty', '$unit', 
                                '$mrp', '$msp', '$rate', '$amount', '$discperc', '$discount', '$total')") or die(mysqli_error($conn));

                ###########Stock Part############

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

                ###########Combine Stock Part############

                mysqli_query($conn, "INSERT INTO $zroot.$combstkdb(`vouchno`, `compcode`, `yearno`, `vouchdt`, `srno`, `barcode`, `itemcode`, `qty`, `unit`,  `source`,
                                `mrp`, `rate`, `amount` ) 
                    VALUES ('$qvouch', '$compcd', '$myyear', '$vouchdt', '$srl', '$barcode', '$barcode', '$stkqty', '$unit', '$source',
                                '$mrp', '$rate', '$amount')") 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);

// sale part
            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, grpcode, amount)
                    VALUES ('$vouchdt','$mvouch', '$source', '001', '$salecode','005', '$netamt')") OR die(mysqli_error($conn));
// party part
            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, grpcode, amount)
                     VALUES ('$vouchdt','$mvouch', '$source', '002', '$party', '010', '$minus_amt')") or die(mysqli_error($conn));
        }
        ############################## MAIN ######################################################
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `salemain`(`vouchno`, `invno`, `invdt`, `vouchdt`, `party`, `transporter`, `lrno`, 
                        `lrdate`, `cashcred`, `prodtype`, `salesman`, `driver`, `dcperc`, `dcbperc`, `dcamt`, `totqty`, `subtotal`, `litemdisc`, 
                        `cdiscperc`, `cdiscamt`, `coukg`, `courate`, `couamt`, `discperc`, `discount`, `netamt`, `ladvance`, `lesother`, `balance`, 
                        `remark1`, `remark2`,`cashvouch`) 
                        VALUES ('$qvouch', '$invno', '$invdt', '$vouchdt', '$party', '$transporter', '$lrno', 
                        '$lrdate', '$type', '$prodtype', '$salesman', '$driver', '$dcperc', '$dcbperc', '$dcamt', '$totqty', '$subtotal', '$litemdisc', 
                        '$cdiscperc', '$cdiscamt', '$coukg', '$courate', '$couamt', '$discperc', '$discount', '$netamt', '$ladvance', '$lesother', '$balance', 
                        '$remark1', '$remark2','$mcashvouch')") 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 `salemain` SET `vouchno`='$qvouch',`invno`='$invno',`invdt`='$invdt',`vouchdt`='$vouchdt',
                    `party`='$party',`transporter`='$transporter',`lrno`='$lrno',`lrdate`='$lrdate',`cashcred`='$type',`prodtype`='$prodtype',`salesman`='$salesman',
                    `driver`='$driver',`dcperc`='$dcperc',`dcbperc`='$dcbperc',`dcamt`='$dcamt',`totqty`='$totqty',`subtotal`='$subtotal',
                    `litemdisc`='$litemdisc',`cdiscperc`='$cdiscperc',`cdiscamt`='$cdiscamt',`coukg`='$coukg',`courate`='$courate',`couamt`='$couamt',
                    `discperc`='$discperc',`discount`='$discount',`netamt`='$netamt',`ladvance`='$ladvance', `lesother`='$lesother', `balance`='$balance',`remark1`='$remark1',
                    `remark2`='$remark2', `cashvouch`='$mcashvouch' WHERE `vouchno`='$qvouch'") 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];


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

    $findsalem = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`='$get[salesman]' AND `grpcode`='040'") or die(mysqli_error($conn));
    $lsalem = mysqli_fetch_array($findsalem);

    $finddriver = mysqli_query($conn, "SELECT `code`,`name` FROM `driver` WHERE `code`='$get[driver]'") or die(mysqli_error($conn));
    $ldriver = mysqli_fetch_array($finddriver);
}

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

if (isset($_GET["delete"])) {
    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 `compcd` = '$compcd' ";
    $delcond1 = " source = 'CR' and vouchno = '$delcsh' ";

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

include("includes/linkshead.php");
?>
<script src="MyScript/sale.js?A=2" type="text/javascript"></script>
</head>
<body>
    <form action='' method='POST'>
        <div class="col-lg-3 pad text-center" id="overlay_form" style="display:none;z-index:999;">
            <button type="button" id="close_form" style="float: 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 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-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Voucher</label>
                            </div>
                            <div class="col-lg-1 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form vouchno" name="vouchno" value="<?php Echo ($addflag != 1) ? $get["vouchno"] : " $qvouch"; ?>" required readonly>
                                <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-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Date</label>
                            </div>
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="date" class="form-control form  focusfield" name="vouchdt" value="<?php Echo ($addflag != 1) ? $get["vouchdt"] : date("Y-m-d"); ?>" autofocus="autofocus">
                            </div>
                            <div class="col-lg-1 col-md-3 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-8 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form" name="invno" value="<?php Echo ($addflag != 1) ? $get["invno"] : "$qvouch"; ?>"  >
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Inv. Date</label>
                            </div>
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="date" class="form-control form" name="invdt" value="<?php Echo ($addflag != 1) ? $get["invdt"] : date("Y-m-d"); ?>">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Party</label>
                            </div>
                            <div class="col-lg-4 col-md-8 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`='010'")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-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Type</label>
                            </div>
                            <div class="col-lg-1 col-md-8 col-sm-8 col-xs-8 pad">
                                <select class="form-control form  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>
                                    <option value='D'<?php
                                    if ($addflag != 1) {
                                        if ($get["cashcred"] == "D") {
                                            echo "selected='selected'";
                                        }
                                    }
                                    ?>>Credit Card</option>
                                </select>
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Product Type</label>
                            </div>
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <select class="form-control form prodtype select2" name="prodtype" style="width: 100%" required>
                                    <option value="<?php Echo ($addflag != 1) ? $get["prodtype"] : ""; ?>">
                                        <?php
                                        if ($get["prodtype"] == "P") {
                                            Echo "PERFUMES";
                                        } elseif ($get["prodtype"] == "O") {
                                            Echo "OTHERS";
                                        }
                                        ?>
                                    </option>
                                    <? if ($addflag == 1) { ?>
                                        <option value="P">PERFUMES</option>
                                        <option value="O">OTHERS</option>
                                    <? } ?>
                                </select>
                            </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">
                        <!--                        <h3 class="panel-title">Item Details</h3>-->
                        <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" width="100%">
                                <thead  class="mySearchTr">
                                    <tr>
                                        <th width="1%"></th>
                                        <th width="3%" class='text-right'>Sr.</th>
                                        <th width="6%" class='text-left'>Barcode</th>
                                        <th width="10%">Item</th>
                                        <th width="5%" class='text-right'>Qty.</th>
                                        <th width="4%" >Unit</th>
                                        <th width="5%" class='text-right'>Mrp</th>
                                        <th width="5%" class='text-right'>Rate</th>
                                        <th width="5%" class='text-right'>Amount</th>
                                        <th width="3%" class='text-right'>Discperc</th>
                                        <th width="4%" class='text-right'>Discount</th>
                                        <th width="7%" class='text-right'>Total</th>
                                    </tr>
                                </thead>
                                <tbody class="tbody">
                                    <?php
                                    if ($addflag != 1) {
                                        $ctr = 0;
                                        $gettran = mysqli_query($conn, "SELECT `vouchno`,`srno`,`barcode`,`item`,`qty`,`unit`,`mrp`,`msp`,`rate`,`amount`,`discperc`,`discount`,`total` 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 `code`,`name` FROM `item` WHERE `code`='$tran[item]'") or die(mysqli_error($conn));
                                            $fitem = mysqli_fetch_array($finditem);

                                            $findunit = mysqli_query($conn, "SELECT `code`,`name` 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 srno_$ctr' name='srno_$ctr' value='$tran[srno]'></td>";
                                            Echo "<td>";
                                            Echo "<input type='text' class='form-control form barcode' name='barcode_$ctr' value='$tran[barcode]'>";
                                            Echo "</td>";
                                            Echo "<td>";
                                            Echo "<select class='form-control form item select2' name='item_$ctr' style='width:100%'>";
                                            Echo "<option value='$fitem[code]'>$fitem[name]</option>";
                                            $gitem = mysqli_query($conn, "SELECT `code`,`name` FROM `item` WHERE `prodtype`='$get[prodtype]'")or die(mysqli_error($conn));
                                            while ($item = mysqli_fetch_array($gitem)) {
                                                Echo "<option value='$item[code]'>$item[name]</option>";
                                            }
                                            Echo "</select>";
                                            Echo "<input type='hidden' class='msp' name='msp_$ctr' value='$tran[msp]' >";
                                            Echo "</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 unit select2' name='unit_$ctr' style='width:100%'>";
                                            Echo "<option value='$funit[code]'>$funit[name]</option>";
                                            $gunit = mysqli_query($conn, 'SELECT `code`,`name` FROM `unit`')or die(mysqli_error($conn));
                                            while ($unit = mysqli_fetch_array($gunit)) {
                                                Echo "<option value='$unit[code]'>$unit[name]</option>";
                                            }
                                            Echo "</select>";
                                            Echo "<input type='hidden' class='msp' name='msp_$ctr' >";
                                            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]'></td>";
                                            Echo "</tr >";
                                            $ctr++;
                                        }
                                    } else {
                                        ?>
                                        <tr>
                                            <td><i class="fa fa-close remove"></i></td>
                                            <td><input type="text" class="form-control form text-right srno srno_0" name="srno_0" value="1"></td>
                                            <td><input type="text" class="form-control form barcode" name="barcode_0" name="barcode"></td>
                                            <td>
                                                <select class="form-control form item select2" name="item_0" style='width:100%' required>
                                                    <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>
                                                <input type='hidden' class='msp' name='msp_0' >
                                            </td>
                                            <td><input type="text" class="form-control form text-right qty"      name="qty_0"         value="0"></td>
                                            <td>
                                                <select class="form-control form unit select2" name="unit_0" style='width:100%' required>
                                                    <option value=""></option>
                                                    <?php
                                                    $gunit = mysqli_query($conn, "SELECT `code`,`name` FROM `unit`")or die(mysqli_error($conn));
                                                    while ($unit = mysqli_fetch_array($gunit)) {
                                                        Echo "<option value='$unit[code]'>$unit[name]</option>";
                                                    }
                                                    ?>
                                                </select>
                                            </td>
                                            <td><input type="text" class="form-control form text-right mrp"      name="mrp_0"         value="0" readonly></td>
                                            <td><input type="text" class="form-control form text-right rate"     name="rate_0"        value="0"></td>
                                            <td><input type="text" class="form-control form text-right amount"   name="amount_0"      value="0" readonly></td>
                                            <td><input type="text" class="form-control form text-right discperc" name="discperc_0"    value="0"></td>
                                            <td><input type="text" class="form-control form text-right discount" name="discount_0"    value="0" readonly></td>
                                            <td><input type="text" class="form-control form text-right total"    name="total_0"       value="0"></td>
                                        </tr>  
                                        <?
                                    }
                                    ?>
                                </tbody>
                            </table>
                        </div>
                        <div class="col-lg-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-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>
                </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="col-lg-12 col-xs-12 pad">
                            <div class="col-lg-6 col-xs-12 pad">
                                <div class="col-lg-12 col-xs-12 pad">
                                    <div class="col-lg-3 pad text-right">
                                        <label class="label-control marg">Salesman</label>
                                    </div>
                                    <div class="col-lg-6 pad">
                                        <select class="form-control form  select2" name="salesman" style="width: 100%">
                                            <option value="<?php Echo ($addflag != 1) ? $get["salesman"] : ""; ?>"><?php Echo ($addflag != 1) ? $lsalem["name"] : ""; ?></option>
                                            <?php
                                            $gsales = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `grpcode`='040'")or die(mysqli_error($conn));
                                            while ($sales = mysqli_fetch_array($gsales)) {
                                                Echo "<option value='$sales[accode]'>$sales[name]</option>";
                                            }
                                            ?>
                                        </select>
                                    </div>
                                </div>
                                <div class="col-lg-12 col-xs-12 pad">
                                    <div class="col-lg-3 pad text-right">
                                        <label class="label-control marg">Driver</label>
                                    </div>
                                    <div class="col-lg-6 pad">
                                        <select class="form-control form driver select2" name="driver" style="width: 100%">
                                            <option value="<?php Echo ($addflag != 1) ? $get["driver"] : ""; ?>"><?php Echo ($addflag != 1) ? $ldriver["name"] : ""; ?></option>
                                            <?php
                                            $gdriver = mysqli_query($conn, "SELECT `code`,`name` FROM `driver` ORDER BY `name`")or die(mysqli_error($conn));
                                            while ($driver = mysqli_fetch_array($gdriver)) {
                                                Echo "<option value='$driver[code]'>$driver[name]</option>";
                                            }
                                            ?>
                                        </select>
                                    </div>
                                </div>
                                <div class="col-lg-12 col-xs-12 pad">
                                    <div class="col-lg-3 pad text-right">
                                        <label class="label-control marg">Remarks</label>
                                    </div>
                                    <div class="col-lg-9 pad">
                                        <input type="text" name="remark1" class="form-control form remark1" value="<? Echo ($addflag != 1) ? $get["remark1"] : ""; ?>">
                                    </div>
                                </div>
                                <div class="col-lg-12 col-xs-12 pad">
                                    <div class="col-lg-3 pad text-right"></div>
                                    <div class="col-lg-9 pad">
                                        <input type="text" name="remark2" class="form-control form remark2" value="<? Echo ($addflag != 1) ? $get["remark2"] : ""; ?>">
                                    </div>
                                </div>

                            </div>

                            <div class="col-lg-6 col-xs-12 pad">
                                <div class="col-lg-12 col-xs-12 pad">
                                    <div class="col-lg-3 pad"></div>
                                    <div class="col-lg-3 pad"></div>
                                    <div class="col-lg-3 pad text-right">
                                        <label class="label-control marg">SubTotal</label>
                                    </div>
                                    <div class="col-lg-3 pad">
                                        <input type="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-xs-12 pad">
                                    <div class="col-lg-3 pad"></div>
                                    <div class="col-lg-3 pad"></div>
                                    <div class="col-lg-3 pad text-right">
                                        <label class="label-control marg">Less Item Discount</label>
                                    </div>
                                    <div class="col-lg-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-xs-12 pad">
                                    <div class="col-lg-3 pad"></div>
                                    <div class="col-lg-3 pad text-right">
                                        <label class="label-control marg">Less Cash Discount</label>
                                    </div>
                                    <div class="col-lg-2 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 pad">
                                        <label class="label-control marg">%</label>
                                    </div>
                                    <div class="col-lg-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-xs-12 pad">
                                    <div class="col-lg-2 pad text-right">
                                        <label class="label-control marg">Add Courier</label>
                                    </div>
                                    <div class="col-lg-2 pad text-right">
                                        <input type="text" class="form-control form coukg text-right" value="<? Echo ($addflag != 1) ? $get[coukg] : "0"; ?>" name="coukg" >
                                    </div>
                                    <div class="col-lg-1 pad text-left">
                                        <label class="label-control marg">/Kg</label>
                                    </div>
                                    <div class="col-lg-3 pad">
                                        <input type="text" class="form-control form courate text-right" value="<? Echo ($addflag != 1) ? $get[courate] : "0"; ?>" name="courate">
                                    </div>
                                    <div class="col-lg-1 pad">
                                        <label class="label-control marg">Rate</label>
                                    </div>
                                    <div class="col-lg-3 pad">
                                        <input type="text" class="form-control form couamt text-right" value="<? Echo ($addflag != 1) ? $get[couamt] : "0"; ?>" name="couamt" readonly>
                                    </div>
                                </div>
                                <div class="col-lg-12 col-xs-12 pad">
                                    <div class="col-lg-3 pad"></div>
                                    <div class="col-lg-3 pad"></div>
                                    <div class="col-lg-3 pad text-right">
                                        <label class="label-control marg">Netamt (Rs.)</label>
                                    </div>
                                    <div class="col-lg-3 pad">
                                        <input type="text" class="form-control form netamt text-right" value="<? Echo ($addflag != 1) ? $get[netamt] : "0"; ?>" name="netamt" readonly>
                                    </div>
                                </div>
                                <div class="col-lg-12 col-xs-12 pad">
                                    <div class="col-lg-3 pad"></div>
                                    <div class="col-lg-3 pad"></div>
                                    <div class="col-lg-3 pad text-right">
                                        <label class="label-control marg">Less Advance</label>
                                    </div>
                                    <div class="col-lg-3 pad">
                                        <input type="text" class="form-control form ladvance text-right" value="<? Echo ($addflag != 1) ? $get[ladvance] : "0"; ?>" name="ladvance" >
                                    </div>
                                </div>
                                <div class="col-lg-12 col-xs-12 pad">
                                    <div class="col-lg-3 pad"></div>
                                    <div class="col-lg-3 pad"></div>
                                    <div class="col-lg-3 pad text-right">
                                        <label class="label-control marg">Balance</label>
                                    </div>
                                    <div class="col-lg-3 pad">
                                        <input type="text" class="form-control form balance text-right" value="<? Echo ($addflag != 1) ? $get[balance] : "0"; ?>" name="balance" readonly>
                                    </div>
                                </div>
                                <div class="col-lg-12 col-xs-12 pad">
                                    <div class="col-lg-3 pad text-right">
                                        <label class="label-control marg">DC On</label>
                                    </div>
                                    <div class="col-lg-2 pad text-right">
                                        <input type="text" class="form-control form dcperc text-right" value="<? Echo ($addflag != 1) ? $get[dcperc] : "0"; ?>" name="dcperc" >
                                    </div>
                                    <div class="col-lg-1 pad text-left">
                                        <label class="label-control marg">%</label>
                                    </div>
                                    <div class="col-lg-2 pad">
                                        <input type="text" class="form-control form dcbperc text-right" value="<? Echo ($addflag != 1) ? $get[dcbperc] : "0"; ?>" name="dcbperc">
                                    </div>
                                    <div class="col-lg-1 pad">
                                        <label class="label-control marg">On</label>
                                    </div>
                                    <div class="col-lg-3 pad">
                                        <input type="text" class="form-control form dcamt text-right" value="<? Echo ($addflag != 1) ? $get[dcamt] : "0"; ?>" name="dcamt" readonly>
                                    </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>
        </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">Prod. 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`,Date_Format(`vouchdt`, '%d/%m/%Y') as mydt, 
                                                             `party`,`cashcred`,`prodtype`,`discount`,`netamt` 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[mydt]</td>";
                                        echo "<td align='center'>$sparty[name]</td>";
                                        echo "<td align='center'>$row[cashcred]</td>";
                                        echo "<td align='center'>$row[prodtype]</td>";
                                        echo "<td align='center'>$row[discount]</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>
