<?php
ob_start();

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

//browse(111);

$maindb = 'cacamain';
$trandb = 'cacatran';

$accdb = 'actran';

$printfile = "caca_print.php";
$file = 'cashandcarry.php';
$trandbexist = 1;
$source = "SLCC";

$keyfld = "vouchno";
$myyear = $_SESSION["tit_" . $dbname];

date_default_timezone_set('Asia/Kolkata');
$time = date('H:i:s');

$usertype = $_SESSION["usertype"];
$username = $_SESSION["username_" . $dbname];

//browse($username);
$formname = 'Cash & Carry';

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$zyear = right($zdb, 4);
$zStartDate = getzdates($zyear, 'S');
$zEndDate = getzdates($zyear, 'E');
mysqli_select_db($conn, $zdb);

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

if (isset($_POST["submit"])) {
    $a = 1;

    $vouchdt = trim($_POST["vouchdt"]);
    $voucher = trim($_POST["voucher"]);
    $qvouch = trim($_POST["vouchno"]);

    $shift = trim($_POST["shift"]);
    $rtgs = trim($_POST["rtgs"]);
    $party = trim($_POST["owner"]);

    $bank = trim($_POST["bank"]);
    $subtotal = trim($_POST["subtotal"]);
    $totqty = trim($_POST["totqty"]);

    date_default_timezone_set("Asia/Dubai");
    $usertime = date("Y-m-d H:i:sa");

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

    $accode = $party;
    $grpcode = '039';

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

    if ($qvouch == '') {
        browse("Voucher Cannot Be Empty");
        $a = 0;
    }


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

    $amount = $subtotal;

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

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

            $time = strtotime($vouchdt);
            $month = date("m", $time);
            $intmonth = intval($month);
            if ($intmonth < 10) {
                $intmonth = '0' . $intmonth;
            }
            $voucher = $intmonth . $mvouch;
        }

        //**************************************************        auto bank receipt Entry Start  ***************************************

        $mbankvouch = $_POST["bankvouch"];

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

            $genrecp = mysqli_query($conn, "SELECT * FROM `bankrecp` where `vouchno` = '$mbankvouch' ") or die(mysqli_error($conn));
            if (mysqli_num_rows($genrecp) > 0) {
                $fetrecp = mysqli_fetch_array($genrecp);
                $mrecpno = $fetrecp['recpno'];
                $mbankvouch = $fetrecp['vouchno'];
                $mbankinit = $fetrecp['bankinit'];
            } else {
                $gen2 = mysqli_query($conn, "SELECT MAX(recpno+0) AS maxcode FROM `bankrecp` where bankname = '$bank' LIMIT 0,1 ") or die(mysqli_error($conn));
                if (mysqli_num_rows($gen2) > 0) {
                    $gvouch = mysqli_fetch_assoc($gen2);
                    $mrecpno1 = $gvouch["maxcode"] + 1;
                } else {
                    $mrecpno1 = '1';
                }
                $mrecpno = $mrecpno1;
            }
            $recpamt = $amount;

            $genbank = mysqli_query($conn, "SELECT bankinit FROM `accmast` where `accode` = '$bank' ") or die(mysqli_error($conn));
            $fetbank = mysqli_fetch_array($genbank);
            $binit = $fetbank['bankinit'];

            $genacc = mysqli_query($conn, "SELECT grpcode FROM `accmast` where `accode` = '$party' ") or die(mysqli_error($conn));
            $fetacc = mysqli_fetch_array($genacc);
            $pgrp = $fetbank['grpcode'];

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

            mysqli_query($conn, "INSERT INTO `bankrecp`( vouchno ,`vouchdt`, `bankname`, `bankinit`, `recpno`, `rtgsno`, `chqdate`, 
                                                        `party`,`amount`,`remark1`,`remark2`,`trnflag`)
                                            Values ('$mbankvouch', '$vouchdt', '$bank', '$binit', '$mrecpno','$rtgs', '$vouchdt', 
                                                        '$accode', '$recpamt', 'NEFT/RTGS. Recd.','', 'C') ") or die(mysqli_error($conn));

            // cash part  

            $minus_amt = ($amount * -1);
            $drawbank = 'Cash & Carry ' . $rtgs;

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


            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, bankinit, grpcode, 
                                                      amount, drawbank, shift, `trnflag`)
                                              VALUES ('$vouchdt','$mbankvouch', 'BR', '001', '$bank', '$binit', '001',
                                                     '$minus_amt','$drawbank', '$shift', 'C')");

            // party part
            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, bankinit, grpcode, 
                                                      amount, drawbank, shift, `trnflag`)
                                              VALUES ('$vouchdt','$mbankvouch', 'BR', '002', '$party', '$binit', '$pgrp', 
                                                     '$amount','$drawbank', '$shift', 'C')");
        } else {
            mysqli_query($conn, "DELETE FROM `bankrecp` WHERE `$keyfld`='$mbankvouch' $keycond") or die(mysqli_error($conn));
            mysqli_query($conn, "DELETE FROM `actran` WHERE `$keyfld`='$mbankvouch' AND `source`='BR' $keycond")or die(mysqli_error($conn));
            $mbankvouch = "";
        }
        
//      **************************************************        bank Entry End    *************************************** 
//        
//      **************************************************        auto cash Entry Start  ***************************************

        $mcashvouch = $_POST["cashvouch"];

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

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

            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', '$accode', 
                                                       '$recpamt', 'NEFT/RTGS. Recd.','', 'C') ") or die(mysqli_error($conn));

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

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

            mysqli_query($conn, "INSERT INTO `$accdb`(date,  vouchno, recpno, source, trnno, bankinit, accode, grpcode, amount, trnflag)
                            VALUES ('$vouchdt', '$mcashvouch', '$mrecpno', 'CP', '001', 'COH', '3', '002', '$recpamt', 'C' )") 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', 'CP', '002', 'COH', '$accode', '010', '$minus_amt', 'C' ) ") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "DELETE FROM `cashpay` WHERE `$keyfld` = '$mcashvouch' $keycond") or die(mysqli_error($conn));
            mysqli_query($conn, "DELETE FROM `actran` WHERE `$keyfld` = '$mcashvouch' AND `source` = 'CP' $keycond")or die(mysqli_error($conn));
            $mcashvouch = "";
        }

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

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

        $cnt = 0;
        $cntamt = 0;

        for ($i = 0; $i <= $count; $i++) {
            $msrno = $_POST["srno_$i"];
            $mitem = mysqli_real_escape_string($conn, $_POST["vehicleno_$i"]);
            $prodcode = $_POST["prodcode_$i"];
            $prodname = $_POST["prodname_$i"];
            $qty = $_POST["qty_$i"];
            $unit = $_POST["unit_$i"];
            $rate = $_POST["rate_$i"];
            $amt = $_POST["amount_$i"];

            if (!empty($mitem)) {

                $cnt++;
                $group = $source . $mvouch;
                $tcartqty = $cartqty * -1;
                $tqty = $qty * -1;
                $ttotqty = $totqty * -1;

                mysqli_query($conn, "INSERT INTO `$trandb`(vouchno, vouchdt, srno, vehicleno, prodcode, prodname, qty, amount,
                            rate, unit)
                            VALUES('$mvouch', '$vouchdt', '$cnt', '$mitem', '$prodcode', '$prodname', '$qty', '$amt',
                            '$rate', '$unit')") or die(mysqli_error($conn));

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

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

            mysqli_query($conn, "INSERT INTO `$maindb`(vouchno, `vouchdt`, `shift`, `party`, `rtgs`, `bank`, `totqty`,`subtotal`, `bankvouch`,`cashvouch`)
                                      VALUES('$mvouch', '$vouchdt',  '$shift', '$party', '$rtgs', '$bank','$totqty','$subtotal', '$mbankvouch', '$mcashvouch')") or die(mysqli_error($conn));
        } else {
            mysqli_query($conn, "UPDATE `$maindb` SET  `vouchdt`='$vouchdt', `shift`='$shift', `party`='$party', `rtgs`='$rtgs',
                                    `bank`='$bank',`totqty`='$totqty', `subtotal`='$subtotal', `bankvouch`='$mbankvouch', `cashvouch`='$mcashvouch'    
                                     WHERE `vouchno` = '$mvouch' ") or die(mysqli_error($conn));
        }
        mysqli_query($conn, "UNLOCK TABLES");
        if ($addflag == 1) {
            insmess();
            header("refresh: 1; $file?mode=edit&$keyfld=$mvouch&form=$form");
        } else {
            updmess();
        }
    }
}

if ($addflag != 1) {

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

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

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

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

//    $getarchitect = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`= '$get[architect]'") or die(mysqli_error($conn));
//    $larchitect = mysqli_fetch_array($getarchitect);
//
//    $getsale = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` WHERE `accode`= '$get[salesman]'") or die(mysqli_error($conn));
//    $lsale = mysqli_fetch_array($getsale);
//
//    $gettransp = mysqli_query($conn, "SELECT `code`,`name` FROM `transport` WHERE `code`= '$get[transporter]'") or die(mysqli_error($conn));
//    $ltrans = mysqli_fetch_array($gettransp);
//
//    $getgodown = mysqli_query($conn, "SELECT * FROM `godown` WHERE `code`= '$get[godown]'") or die(mysqli_error($conn));
//    $getgd = mysqli_fetch_array($getgodown);
//    browse($getgd[code]);

    $delbnk  = $get['bankvouch'];
    $delcsh  = $get['cashvouch'];
}

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

    $delcond1 = " source = 'BR' and vouchno = '$delbnk'";
    $delcond11 = " vouchno = '$delbnk'  ";

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

    delnew($zdb, "$maindb", "$delcond", "$file?mode=last");
    delnew($zdb, "$trandb", "$delcond", "$file?mode=last");
    
    delnew($zdb, "$accdb",  "$delcond1",  "$file?mode=last");
    delnew($zdb, "$accdb",  "$delcond2", "$file?mode=last");
    
    delnew($zdb, "bankrecp", "$delcond11", "$file?mode=last");
    
    delnew($zdb, "cashpay",  "$delcond22", "$file?mode=last");

    mysqli_query($conn, "UNLOCK TABLES");
}

include("includes/linkshead.php");
?>
<script src="MyScript/caca.js?A=<?= mt_rand(); ?>" type="text/javascript"></script>
<!--<script src="MyScript/search_item_sale.js?A=<?= mt_rand(); ?>" type="text/javascript"></script>-->
<script src="MyScript/caca_search.js" type="text/javascript"></script>
<script src="MyScript/checkdate.js" type="text/javascript"></script>
<!--<script>
    $(document).ready(function () {
      
    })
</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" style="height:40px">
                        <div class="col-lg-10 col-md-10 col-sm-10 col-xs-12">
                            <h3 class="panel-title" style="color:black"><a class="myPrint"><?php echo $formname; ?></a></h3>
                        </div>
                        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-12">
                            <input type="date" value="<?= $zStartDate ?>" style="width: 1px;border: none" class="zstdate">
                            <input type="date" value="<?= $zEndDate ?>" style="width: 1px;border: none" class="zenddate">
                            <h4 class="panel-title" style="float:right"><b><?php echo $get['username'] ?></b></h4>
                        </div>

                    </div>
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-lg-1 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg" >Voucher</label>
                            </div>
                            <div class="col-lg-1 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type="text" name="vouchno" class="form-control form vouchno" id="vouchno" readonly value="<?php echo ($addflag != 1) ? $get['vouchno'] : $qvouch ?>" >
                                <input type="hidden" name="voucher" class="form-control form voucher" value="<?php echo ($addflag != 1) ? $get['voucher'] : '' ?>" tabindex="-1" readonly required>
                                <input type="hidden" name="monno" class="form-control form monno" value="<?php echo ($addflag != 1) ? $get['monno'] : '' ?>" >
                                <input type="hidden" class="form-control addflag" value="<?php echo $addflag ?>" >
                                <input type="hidden" name="source" class="form-control form source" value="<?= $source; ?>" id="source">
                                <input type="hidden" class="form-control form" name="cashvouch" value="<?php Echo ($addflag != 1) ? $get["cashvouch"] : ""; ?>" >
                                <input type="hidden" class="form-control form" name="bankvouch" value="<?php Echo ($addflag != 1) ? $get["bankvouch"] : ""; ?>" >
                                <input type="hidden" class="form-control form canc_flag" name="" value="<?php Echo ($addflag != 1) ? $get["canc_flag"] : ""; ?>" >
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-2 col-xs-3 text-right pad">
                                <label class="label-control marg" >Date</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-3 col-xs-4 pad">
                                <input type="date" class="form-control form vouchdt focusfield" name="vouchdt"  value="<?php Echo ($addflag != 1) ? $get["vouchdt"] : date("Y-m-d"); ?>" autofocus required>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg " >Shift</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-3 col-xs-4 pad">
                                <select name="shift" class="form-control form select2 shift" required >
                                    <option value="<?php echo ($addflag != 1) ? $get['shift'] : ""; ?>"><?php echo ($addflag != 1) ? $get['shift'] : ""; ?></option>
                                    <?php
                                    $getshift = mysqli_query($conn, "SELECT shift FROM shift WHERE code != '' ") or die(mysqli_error($conn));
                                    while ($trow = mysqli_fetch_array($getshift)) {
                                        echo "<option value='$trow[shift]'>$trow[shift]</option>";
                                    }
                                    ?>
                                </select>                                
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-1 col-md-1 col-sm-2 col-xs-2  pad text-right">
                                <label class="label-control marg">Party</label>
                            </div>

                            <div class="col-lg-3 col-md-3 col-sm-7 col-xs-9  pad">
                                <select name="owner" class="form-control form  select2 owner" required>
                                    <option value="<?php echo ($addflag != 1) ? $lparty['accode'] : ""; ?>"><?php echo ($addflag != 1) ? $lparty['name'] : ""; ?></option>
                                    <?php
                                    $getcust = mysqli_query($conn, "SELECT accode,name FROM accmast WHERE grpcode = '039' ") or die(mysqli_error($conn));
                                    while ($trow = mysqli_fetch_array($getcust)) {

                                        echo "<option value='$trow[accode]'>$trow[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <!--
                             <div class="row">
                                 <div class="col-lg-1 col-md-1 col-sm-2 col-xs-2  pad text-right">
                                     <label class="label-control marg">Party</label>
                                 </div>
                                 <div class="col-lg-3 col-md-3 col-sm-7 col-xs-9  pad">
                                     <input type="text" name="owner" class="form-control form owner" value="<?php // echo ($addflag != 1) ? $get['owner'] : "";    ?>">
                                 </div>
                             </div>-->
                        <div class="row">
                            <div class="col-lg-1 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">RTGS</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-7 col-xs-9  pad">
                                <input type="text" name="rtgs" class="form-control form rtgs" value="<?php echo ($addflag != 1) ? $get['rtgs'] : ""; ?>">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-1 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Our Bank</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-7 col-xs-9  pad">
                                <select name="bank" class="form-control form  select2 bank" required>
                                    <option value="<?php echo ($addflag != 1) ? $lbank['accode'] : ""; ?>"><?php echo ($addflag != 1) ? $lbank['name'] : ""; ?></option>
                                    <?php
                                    $getbank = mysqli_query($conn, "SELECT accode,name FROM accmast WHERE grpcode = '001' ") or die(mysqli_error($conn));
                                    while ($trow = mysqli_fetch_array($getbank)) {
                                        echo "<option value='$trow[accode]'>$trow[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                    </div>

                    <!--                        <div class="row">
                                                <div class="col-lg-1 col-xs-2 pad text-right">
                                                    <label class="label-control marg">Paid By</label>
                                                </div>-->
                    <!--                            <div class="col-lg-1 col-xs-4 pad">
                                                    <select id="paidby" name="paidby" class="form-control form select2 paidby" required>
                                                        <option 
                    <?php
                    if ($get['paidby'] == 'C') {
                        echo "selected='selected'";
                    }
                    ?> value="C">Cash</option>
                                                        <option
                    <?php
                    if ($get['paidby'] == 'R') {
                        echo "selected='selected'";
                    }
                    ?> value="R">Credit</option>
                    
                    
                                                    </select>
                                                </div>-->
                    <!--                        </div>-->



                    <div class="table-responsive">
                        <table class="table-striped table-condensed table-bordered tab" width="100%">
                            <thead  class="mySearchTr">
                                <tr>
                                    <th style="width: 1%"></th>
                                    <th style="width: 1%" class="text-left">Sr.</th>
                                    <th style='width: 2%' class="text-left">Vehicle No.</th>                                        
                                    <th style='width: 2%' class="text-left">Prod.Code</th>                                        
                                    <th style='width: 4%' class="text-left">Product Name</th>                                        
                                    <th style='width: 1%' class="text-center">Qty.</th>
                                    <th style='width: 1%' class="text-center">Unit</th>
                                    <th style='width: 1%' class="text-center">Rate</th>
                                    <th style='width: 2%' class="text-left">Amount</th>                                        

                                </tr>
                            </thead>
                            <tbody class="tbody">
                                <?php
                                if ($addflag != 1) {
                                    $qry = mysqli_query($conn, "SELECT * FROM  `$trandb` WHERE vouchno='$getvouch' ORDER BY srno") or die(mysqli_error($conn));
                                    $ctr = 0;
                                    while ($row = mysqli_fetch_array($qry)) {
                                        Echo "<tr>";
                                        Echo "<td><i class='fa fa-close remove' style='display:none;'></i></td>";
                                        Echo "<td><input type='text' name='srno_$ctr' class='srno text-right form-control  form'  value='$row[srno]'></td>";
                                        Echo "<td>";
                                        Echo"<select name='vehicleno_$ctr' class='form-control form  select2 vehicleno' style='width:100%' required>";
                                        Echo "<option value='$row[vehicleno]'>$row[vehicleno]</option>";
                                        $getp = mysqli_query($conn, "SELECT vehicleno FROM custvehicle ") or die(mysqli_error($conn));
                                        while ($trow = mysqli_fetch_array($getp)) {
                                            echo "<option value='$trow[vehicleno]'>$trow[vehicleno]</option>";
                                        }
                                        echo"</select>";
                                        Echo "</td>";

//                                            Echo "<td><input type='text' class='form-control form vehicleno'          name='vehicleno_$ctr' value='$row[vehicleno]'></td>";
                                        Echo "<td><input type='text' class='form-control form prodcode'           name='prodcode_$ctr'  value='$row[prodcode]'></td>";
                                        Echo "<td><input type='text' class='form-control form prodname'           name='prodname_$ctr'  value='$row[prodname]'></td>";
                                        Echo "<td><input type='text' class='form-control form text-right qty'     name='qty_$ctr' value='$row[qty]'></td>";
                                        Echo "<td><input type='text' class='form-control form unit'               name='unit_$ctr'  value='$row[unit]'></td>";
                                        Echo "<td><input type='text' class='form-control form text-right rate'    name='rate_$ctr'  value='$row[rate]'></td>";
                                        Echo "<td><input type='text' class='form-control form text-right amount'  name='amount_$ctr'  value='$row[amount]' ></td>";
                                        Echo "</tr>";
                                        $ctr++;
                                    }
                                } else {
                                    ?>
                                    <tr>                                    
                                        <td><i class='fa fa-close remove'></i></td>
                                        <td><input type='text' name='srno_0' class='srno form-control form' value='1'></td>
                                        <td>
                                            <select name="vehicleno_0" class="form-control form  select2 vehicleno" style="width:100%" required>
                                                <?php
                                                $getv = mysqli_query($conn, "SELECT * FROM custvehicle") or die(mysqli_error($conn));
                                                while ($trow = mysqli_fetch_array($getv)) {
                                                    echo "<option value='$trow[srno]'>$trow[vehicleno]</option>";
                                                }
                                                ?>
                                            </select>
                                        </td>

                                        <td><input type='text' name='prodcode_0' class='prodcode form-control form' value='' ></td>
                                        <td><input type='text' name='prodname_0' class='prodname form-control form' value='' ></td>
                                        <td><input type='text' name='qty_0' class='qty text-right form-control form' value='' ></td>
                                        <td><input type='text' name='unit_0' class='unit form-control form' value='' ></td>
                                        <td><input type='text' name='rate_0' class='rate text-right form-control form' value='' ></td>
                                        <td><input type='text' name='amount_0' class='amount text-right form-control form' value='' ></td>
                                    </tr>
                                    <?php
                                }
                                ?>
                            <input type="hidden" id="items" name="items" value="<?php Echo ($addflag != 1) ? $ctr : 1; ?>">  

                            </tbody>
                        </table>
                    </div>

                    <div class="col-lg-12 pad">
                        <button type="button" class="btn btn-primary btn-square form"  id="addnew">
                            <i class="fa fa-plus" data-toggle='tooltip' title='Add Row'></i>    
                        </button>
<!--                            <input type="hidden" class="mandatoryId" name="manId" value="">  -->
                    </div>
                    <div class="col-lg-12 col-xs-12 pad">
                        <div class="col-lg-12 col-xs-12 pad">
                            <div class="col-lg-5 pad"></div>
                            <div class="col-lg-3 col-xs-5 pad text-right">
                                <label class="label-control marg text-right">Total Qty.</label>
                            </div>
                            <div class="col-lg-1 col-xs-3 pad">
                                <input type="number" step="any" name="totqty" class="form-control form text-right totqty" value="<?php Echo ($addflag != 1) ? $get['totqty'] : "0"; ?>">

                            </div>
                            <div class="col-lg-1 col-xs-1 pad text-right">
                                <label class="label-control marg">RTGS.Amount(Rs.)</label>
                            </div>
                            <div class="col-lg-2 col-xs-3 pad">
                                <input type="number" step="any" name="subtotal" class="form-control form text-right subtotal" value="<?php Echo ($addflag != 1) ? $get['subtotal'] : "0"; ?>">    
                            </div>
                        </div>    


                    </div>
                    <div class="modal fade" id="modelWindow" role="dialog">
                        <div class="modal-dialog modal-lg vertical-align-center">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <button type="button" class="close" data-dismiss="modal">&times;</button>
                                    <h4 class="modal-title">Purchase</h4>
                                </div>
                                <div class="modal-body">
                                    <div class="table-responsive">
                                        <table class="table-striped table-condensed table-bordered tab" width="100%">
                                            <thead  class="mySearchTr">
                                                <tr>
                                                    <th style="width: 1%"></th>
                                                    <th style="width: 2%">Sr.</th>
                                                    <th style='width: 20%' class="text-left">Supplier</th>
                                                    <th style='width: 5%' >Bill No</th>
                                                    <th style="width: 5%;">Bill Date</th> 
                                                    <th style='width: 5%' class="text-right">Qty</th>
                                                    <th style='width: 9%' class="text-right">Cost</th>
                                                    <th style='width: 10%' class="text-right">Amount</th>
                                                </tr>
                                            </thead>
                                            <tbody class="tbody2">
                                                <?php
//                                                        if ($addflag != 1) {
//                                                            $qry = mysqli_query($conn, "SELECT * FROM  `salepurch` WHERE vouchno='$getvouch' ORDER BY srno") or die(mysqli_error($conn));
//                                                            $ctr = 0;
//                                                            while ($row = mysqli_fetch_array($qry)) {
//                                                                
//                                                            $sup = mysqli_query($conn, "SELECT accode,name FROM `accmast` WHERE `code`='$row[itemcode]'") or die(mysqli_error($conn));
//                                                            $fsup = mysqli_fetch_array($sup);
//                                                                
//                                                                Echo "<tr>";
//                                                                Echo "<td><i class='fa fa-close remove' style='display:none;'></i></td>";
//                                                                Echo "<td><input type='text' name='srno_$ctr' class='srno form-control  form'  value='$row[srno]'></td>";
//                                                                Echo "<td>";
//                                                                Echo "<select class='form-control form supplier select2' name='supplier_$ctr' style='width:100%'>";
//                                                                Echo "<option value='$fsup[code]'>$fsup[name]</option>";
//                                                                $gsupplier = mysqli_query($conn, "SELECT accode,name FROM `accmast` WHERE grpcode='009' order by name")or die(mysqli_error($conn));
//                                                                while ($supplier = mysqli_fetch_array($gsupplier)) {
//                                                                Echo "<option value='$supplier[code]'>$supplier[name]</option>";
//                                                                }
//                                                                Echo "</select>";
//                                                                Echo "</td>";
//                                                                
////                                                                Echo "<td><input type='text' class='form-control form text-right supplier'     name='supplier_$ctr' value='$row[supplier]'></td>";
//                                                                Echo "<td><input type='text' class='form-control form billno'               name='billno_$ctr'  value='$row[billno]'></td>";
//                                                                Echo "<td><input type='date' class='form-control form billdate'            name='billdate_$ctr'  value='$row[billdate]'></td>";
//                                                                Echo "<td><input type='text' class='form-control form text-right purqty'    name='purqty_$ctr'  value='$row[purqty]'></td>";
//                                                                Echo "<td><input type='text' class='form-control form text-right purcost'    name='purcost_$ctr'  value='$row[purcost]'></td>";
//                                                                Echo "<td><input type='text' class='form-control form text-right puramt'  name='puramt_$ctr'  value='$row[puramt]' ></td>";
//                                                                Echo "</tr>";
//                                                                $ctr++;
//                                                            }
//                                                        } else {
//                                                            
                                                ?>
<!--                                                            <tr>                                    
                                                        <td><i class='fa fa-close remove'></i></td>
                                                        <td><input type='text' name='srno_0' class='srno form-control form' value='1'></td>
                                                        <td><input type='text' name='supplier_0' class='supplier form-control form' value='' ></td>
                                                        <td><input type='text' name='billno_0' class='billno form-control form' value='' ></td>
                                                        <td><input type='date' name='billdate_0' class='billdate form-control form' value='' ></td>
                                                        <td><input type='text' name='purqty_0' class='purqty form-control form' value='' ></td>
                                                        <td><input type='text' name='purcost_0' class='purqty form-control form' value='' ></td>
                                                        <td><input type='text' name='puramt_0' class='puramt form-control form' value='' ></td>
                                                    </tr>-->
                                                <?php
//                                                        }
                                                ?>
                                                                                                    <!--<input type="hidden" id="itemspur" name="itemspur" value="<?php Echo ($addflag != 1) ? $ctr : 1; ?>">-->  

                                            </tbody>
                                        </table>
                                    </div>

                                </div>
                                <div class="modal-footer">
                                    <div class="col-lg-1 pad">
                                        <button type="button" class="btn btn-primary btn-square form"  id="puradd">
                                            <i class="fa fa-plus" data-toggle='tooltip' title='Add Row'></i>    
                                        </button>
                                    </div>

                                    <button type="button" data-dismiss="modal" class="form btn btn-default pursave">Save</button>
                                </div>
                            </div>
                        </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-2 col-xs-3 pad text-left">
                                                    <label class="label-control marg">Packed By</label>
                                                </div>
                                                <div class="col-lg-4 col-xs-7 pad">
                                                    <input type="text" name="packby" class="form-control form packby" value="<? Echo ($addflag != 1) ? $get["packby"] : ""; ?>" tabindex="-1">
                                                </div>
                                            </div>-->
                <!--                            <div class="col-lg-12 col-xs-12 pad">
                                                <div class="col-lg-2 col-xs-3 pad text-left">
                                                    <label class="label-control marg">Remarks</label>
                                                </div>
                                                <div class="col-lg-6 col-xs-9 pad">
                                                    <input type="text" name="remark1" class="form-control form " value="<? Echo ($addflag != 1) ? $get["rem1"] : ""; ?>">
                                                </div>
                                            </div>-->
                <!--                            <div class="col-lg-12 col-xs-12 pad">
                                                <div class="col-lg-2 col-xs-3 pad text-right"></div>
                                                <div class="col-lg-6 col-xs-9 pad">
                                                    <input type="text" name="remark2" class="form-control form " value="<? Echo ($addflag != 1) ? $get["rem2"] : ""; ?>">
                                                </div>
                                            </div>-->
                <div class="col-lg-12 col-xs-12 pad">
                    <div class="col-lg-9 pad">

                        <?php
//                                        browse($get['canc_flag']);
                        if ($get['canc_flag'] != '') {
                            ?>
                            <img src="images/cancel.jpg" alt="Cancelled Bill" style="width:100%;height:100%">
                        <?php } ?>

                    </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"); ?>
                <script>
                    $(document).ready(function () {

                        var usertype = <?= $usertype ?>;
                        if (usertype == 3) {
                            $("#hedit").hide();
                        }
                    });
                </script>
            </div>
        </div>
    </div>
</div>
</div>
</form>


<div class="modal fade" id="cancmodel" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="col-lg-11">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title" id="myModalLabel">Cancel Bill</h4>
                </div>
                <div class="modal-body">
                    <div class="row">
                        <div class="col-lg-12 upload">
                            <form id="upload_form" method="post">

                                <div class="row pad">
                                    <input type="hidden" name="cancvouch" id="cancvouch" value='<?php Echo ($addflag != 1) ? $get['vouchno'] : "$qvouch"; ?>'>
                                    <input type="hidden" name="cashvouch2" id="cashvouch2" value='<?php Echo ($addflag != 1) ? $get['cashvouch'] : ""; ?>'>
                                    <div class="col-lg-4 col-md-4 col-sm-4 col-xs-5 text-right">
                                        <label class="label-control marg">Cancel Date</label>
                                    </div>
                                    <div class="col-lg-4 col-md-4 col-sm-6 col-xs-7">

                                        <input type="date" name="cancdate" value="<?php Echo ($addflag != 1) ? $get['cancdate'] : ""; ?>" class="form-control cancdate">
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-lg-6 col-md-6 col-sm-7 col-xs-8 text-right">
                                        <input type="submit" value="Cancel"  name="cancbtn" class="btn btn-square" style="background-color: #C0CA33; color: white">
                                    </div>
                                </div>

                            </form>
                        </div>

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

<!--<div class="modal fade itemModal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button class="close" data-dismiss="modal"><span>&times;</span></button>
                <h4 class="modal-title" id="myLargeModalLabel">Search <? Echo $formname; ?></h4>
            </div>
            <div class="modal-body">
                <input type="hidden" value="<?php // Echo $fseries                                                                                                    ?>" class="form-control series">
                <input type="hidden" value="<?php Echo $get['vouchno']; ?>" class="form-control vouchno">
                <input type="hidden" value="<?php Echo $file ?>" class="form-control file">
                <input type="hidden" value="<?php Echo $para ?>" class="form-control para">
                <div class="row">
                    <div class="col-lg-3">
                        <select class="form-control ifield">
                            <option value="1">Name</option>
                            <option value="2">Barcode</option>
                            <option value="3">Itemcode</option>
                                                        <option value="4">Gram</option>
                                                        <option value="5">Unit</option>
                        </select>
                    </div>
                    <div class="col-lg-2">
                        <select class="form-control ioperator">
                            <option value=""></option>
                            <option value="%">Like</option>
                            <option value="=">Exactly</option>
                        </select>
                    </div>
                    <div class="col-lg-4">
                        <input type="text" value="" class="form-control isearch">
                    </div>
                    <div class="col-lg-3">
                        <input type="button" class="btn btn-primary iload" value="Search">
                    </div>
                </div>

                <div class="col-lg-12 iloader text-center">
                    <img src="images/search.gif" alt="loading">       
                </div>
                <div class="col-lg-12 inoRecord text-center">
                    <h3>NO RECORDS FOUND</h3>
                </div>
                <div class="table-responsive" style="max-height: 400px;overflow: auto;">
                    <table id="" class="table table-striped table-condensed table-bordered" width="100%">
                        <thead>
                            <tr class="mySearchTr">
                                <th width="15%">Itemcode</th>
                                <th width="49%">Name</th>
                                <th width="8%">Stock</th>
                                <th width="17%">Barcode</th>
                                <th width="8%">Gram</th>
                                <th width="8%">Unit</th>
                                <th width="8%">Rate</th>
                            </tr>
                        </thead>
                        <tbody class="iTbody">
                        </tbody>
                    </table>
                </div>
                <div class="row">
                    <div class="col-xs-12 text-right search-action-bar">
                        <input type="button"  class="btn btn-primary iprevious" value="Previous">
                        <input type="button"  class="btn btn-primary inext" value="Next">
                    </div>
                </div>
            </div>
            <div class="modal-footer"></div>
        </div>
    </div>
</div>-->
<div class="modal fade searchModal bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
    <div class="modal-dialog modal-lg" style="margin-top: 15%">
        <div class="modal-content">
            <div class="modal-header">
                <button class="close" data-dismiss="modal"><span>&times;</span></button>
                <h4 class="modal-title" id="myLargeModalLabel">Search <? Echo $formname; ?></h4>
            </div>
            <div class="modal-body">
                <input type="hidden" value="<?php Echo $fseries ?>" class="form-control series">
                <input type="hidden" value="<?php Echo $get['vouchno']; ?>" class="form-control vouchno">
                <input type="hidden" value="<?php Echo $file ?>" class="form-control file">
                <input type="hidden" value="S" class="form-control parasrch">
                <div class="row">
                    <div class="col-lg-2">
                        <select class="form-control sfield">
                            <option value="1">Voucher</option>
                            <option value="2">Date</option>
                            <option value="3"> Party Name</option>
                            <option value="4">Total Qty</option>
                            <option value="5">Amount</option>
                        </select>
                    </div>
                    <div class="col-lg-2">
                        <select class="form-control soperator">
                            <option value="">Type of Search</option>
                            <option value="%">Like</option>
                            <option value="=">Exactly</option>
                        </select>
                    </div>
                    <div class="col-lg-2">
                        <select class="form-control sortBy">
                            <option value="">Sort By</option>
                            <option value="1">Voucher</option>
                            <option value="2">Date</option>
                            <option value="3"> Party Name</option>
                            <option value="4">Total Qty</option>
                            <option value="5">Amount</option>
                        </select>
                    </div>
                    <div class="col-lg-2">
                        <select class="form-control orderAs">
                            <option value="desc">Desc</option>
                            <option value="asc">Asc</option>
                        </select>
                    </div>
                    <div class="col-lg-2">
                        <input type="text" value="" class="form-control ssearch" placeholder="Search Here">
                    </div>
                    <div class="col-lg-2">
                        <input type="button" class="btn btn-primary sload" value="Search" >
                    </div>
                </div>

                <div class="col-lg-12 sloader text-center">
                    <img src="images/search.gif" alt="loading">       
                </div>
                <div class="col-lg-12 snoRecord text-center">
                    <h3>NO RECORDS FOUND</h3>
                </div>
                <div class="table-responsive" style="max-height: 400px;overflow: auto;">
                    <table id="" class="table table-striped table-condensed table-bordered" width="100%">
                        <thead>
                            <tr class="mySearchTr">
                                <th width="5%">Voucher</th>
                                <th width="10%">Date</th>
                                <th width="30%">Party Name</th>
                                <th style="text-align: right" width="10%">Total Qty</th>
                                <th style="text-align: right" width="10%">Amount</th>
                                <th width="5%">Action</th>
                            </tr>
                        </thead>
                        <tbody class="sTbody">
                        </tbody>
                    </table>
                </div>
                <div class="row">
                    <div class="col-xs-12 text-right search-action-bar">
                        <input type="button"  class="btn btn-primary sprev" value="Previous">
                        <input type="button"  class="btn btn-primary snext" value="Next">
                    </div>
                </div>
            </div>
            <div class="modal-footer"></div>
        </div>
    </div>
</div>
<?php include("includes/linksfoot.php"); ?>
</body>
</html>
