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

$maindb = 'salemain';
$trandb = 'saletran';
$accdb = 'actran';
$stkdb = 'transacn';

$zroot = $_SESSION["zdbroot"];

$printfile = "sale_print.php";
$saleinvprint = "sale_invoice_print.php";
$file = 'sale.php';
$trandbexist = 1;
$source = "SL";
$keyfld = "vouchno";
$keycond = "";

$keycond1 = " AND saletype != 'C'";
$par = $_GET["par"];

$myyear = $_SESSION["tit_" . $dbname];
$zyear = $myyear;

$zsdate = getzdates($zyear, 'S');
$zedate = getzdates($zyear, 'E');

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

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

//browse($username);
$formname = 'Sale Credit';

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


mysqli_select_db($conn, $zdb);

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

if (isset($_POST["cancbtn"])) {
    $mcancvouch = $_POST['cancvouch'];
    $mcashvouch2 = $_POST['cashvouch2'];
    $cancdate = $_POST['cancdate'];

    //    browse($cancdate);
    if ($cancdate != '' || $cancdate != '0000-00-00') {
        //    browse($mcancvouch);
        //    browse($mcashvouch2);
        mysqli_query($conn, "DELETE FROM `$accdb` WHERE `vouchno` = '$mcancvouch' AND `source` = '$source' ") or die(mysqli_error($conn));
        mysqli_query($conn, "DELETE FROM `cashrecp` WHERE `$keyfld`='$mcashvouch2' ") or die(mysqli_error($conn));
        mysqli_query($conn, "DELETE FROM `$accdb` WHERE `$keyfld`='$mcashvouch2' and `source` = 'CR' ") or die(mysqli_error($conn));
        mysqli_query($conn, "DELETE FROM `$stkdb` WHERE vouchno='$mcancvouch'  AND `source`='$source'") or die(mysqli_error($conn));

        mysqli_query($conn, "Update `$maindb` set `cancdate`='$cancdate', cancuser='$username', canc_flag='C' where vouchno = '$mcancvouch'") or die(mysqli_error($conn));
        mysqli_query($conn, "Update `$trandb` set cancdate='C' where vouchno = '$mcancvouch'") or die(mysqli_error($conn));
        browse('Bill Cancelled');
    }
    header("refresh: 1; $file?mode=edit&$keyfld=$mcancvouch");
}

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

    $a = 1;

    $vouchdt = $_POST["vouchdt"];
    $vouchno = $_POST["vouchno"];
    $mono = $_POST["monno"];
    $invno = $_POST["invno"];
    $name = $_POST["name"];
    $custcode = $_POST["custcode"];

    $paidby = 'A';

    $party = $_POST["party"];
    $gstno = trim($_POST["gstno"]);
    $totalqty = $_POST["totalqty"];
    $rem1 = trim($_POST["remark1"]);
    $rem2 = trim($_POST["remark2"]);

    $direct = $_POST["direct"];
    $dept = $_POST["dept"];
    $vehicleno = $_POST["vehicleno"];
    $tcsamt = $_POST["tcsamt"];
    $tcsperc = $_POST["tcsperc"];

    $subtotal = $_POST["subtotal"];
    $discdies = $_POST["discdies"];
    $totqty = $_POST["totqty"];
    $discondies = $_POST["discondies"];
    $totbillamt = $_POST["totbillamt"];
    $slipno = $_POST["slipno"];
    $billno = $_POST["billno"];
    $totadv = $_POST["totadv"];
    $totbal = $_POST["totbal"];
    $shift = $_POST["shift"];

    $mbilldate = $vouchdt;
    date_default_timezone_set('Asia/Kolkata');
    $usertime = date("Y-m-d H:i:sa");

    //    $taxableamt = $grossamt - $totdiscamt + $postamt;
    //    $totbillamt1 = $taxableamt + $totgst5 + $totgst12 + $totgst18 + $totgst28 + $tcsamt;
    //    $gstamount = $pretcs - $taxableamt;
    //
    //    $totbillamt = round($totbillamt1);
    //    if ($totgst1 > 0) {
    //        $oms = 1;
    //    } else {
    $oms = $_POST['oms'];
    //    }

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

    //browse($count);

    $purcode = '15';

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

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

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

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

    if ($custcode == "") {
        browse("Customer Code Cannot Be Empty");
        $a = 0;
    }

    if ($slipno == "") {
        browse("Slip No. Cannot Be Empty");
        $a = 0;
    }

    if ($a == 1) {

        $time = strtotime($vouchdt);
        $mono = date("n", $time);


        mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `$trandb` WRITE, `product` WRITE, `$stkdb` WRITE, $zroot.`compyear` WRITE,
                                          $zroot.`company` WRITE, `$accdb` WRITE, `cashrecp` WRITE, `accmast` WRITE, `invoice` WRITE");

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

            $intmonth = intval($mono);
            if ($intmonth < 10) {
                $intmonth = '0' . $intmonth;
            }
            $voucher = $intmonth . $mvouch;
        }


        $qryparty = mysqli_query($conn, "SELECT `accode`, `name`, `discdies`, `tcsperc`, `tdsapply`, `dailyprint` FROM `accmast` WHERE `accode`= '$party' ") or die(mysqli_error($conn));
        $fetparty = mysqli_fetch_array($qryparty);
        $mdiscdies = $fetparty['discdies'];
        $mdailyprint = $fetparty['dailyprint'];
        $mtdsapply = $fetparty['tdsapply'];

        if ($mdailyprint == 1) {
            if ($invno == '') {
                $mmonth1 = strtotime($vouchdt);
                $mmonth = date('m', $mmonth1);

                if ($mmonth > 3) {
                    $myear = date('Y', strtotime($zsdate));
                } else {
                    $myear = date('Y', strtotime($zedate));
                }

                $qrysale = mysqli_query($conn, "SELECT * FROM salemain WHERE party = '$accode' AND vouchdt = '$vouchdt' ") or die(mysqli_error($conn));
                $fetsale = mysqli_fetch_array($qrysale);
                if (mysqli_num_rows($qrysale) <= 0) {
                    $cno = 1;
                } else {
                    $qrysal2 = mysqli_query($conn, "SELECT * FROM salemain WHERE party = '$accode' AND vouchdt = '$vouchdt' AND invno != '' ") or die(mysqli_error($conn));
                    if (mysqli_num_rows($qrysal2) == 0) {
                        $cno = 1;
                    } else {
                        $cno1 = mysqli_num_rows($qrysal2);
                        $cno = $cno1 + 1;
                    }
                }

//                browse($vouchdt);

                $mdat = $vouchdt;
                $mday = date("d", strtotime($mdat));

                $minvoiceno = trim($custcode) . '-' . $mmonth . '/' . $myear . '/' . $mday . '-' . $cno;
            } else {
                $minvoiceno = $invno;

                $noc1 = strpos($minvoiceno, '-') + 1;
                $noc = strpos($minvoiceno, '-', $noc1);
                $nocc = $noc + 1;

                $ccno = SUBSTR($minvoiceno, $nocc);
                $cno = (int) $ccno;
            }
        }

        //**************************************************    auto cash Entry Start  ***************************************

        $mcashvouch = $_POST["cashvouch"];

        if ($paidby == 'C' || $totadv > 0) {
            if ($mcashvouch == '0' || $mcashvouch == '') {
                $gen1 = mysqli_query($conn, "SELECT MAX(vouchno+0) AS maxcode FROM `cashrecp` 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 `cashrecp` 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 `cashrecp` 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 = $totbillamt;

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

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

            // cash part  

            $minus_amt = ($recpamt * -1);

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

            mysqli_query($conn, "INSERT INTO `$accdb`(date,godown, vouchno, recpno, source, trnno, bankinit, accode, grpcode, amount, trnflag, monno)
                    VALUES ('$vouchdt','$godown','$mcashvouch', '$mrecpno', 'CR', '001', 'COH', '3','002', '$minus_amt','S','$mono')") or die(mysqli_error($conn));

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

        //**************************************************        auto cash Entry End    *************************************** 
        //        browse($mvouch);
        mysqli_query($conn, "DELETE FROM `$trandb` WHERE vouchno='$mvouch' ") or die(mysqli_error($conn));

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

        $cnt = 0;
        $cntamt = 0;
        //        browse($count);
        for ($i = 0; $i <= $count; $i++) {
            $msrno = $_POST["srno_$i"];
            $prodcode = $_POST["prodcode_$i"];
            $product = $_POST["product_$i"];
            $qty = $_POST["qty_$i"];
            $unit = $_POST["unit_$i"];
            $rate = $_POST["rate_$i"];
            $amt = $_POST["amount_$i"];
            $diesdisc = $_POST["diesdisc_$i"];
            $delivery_date = $_POST["delivery_date_$i"];
            $gstperc = $cgstrate + $sgstrate + $igstrate;

            if (!empty($product)) {
                $cnt++;
                $group = $source . $mvouch;
                $tcartqty = $cartqty * -1;
                $tqty = $qty * -1;
                $ttotqty = $totqty * -1;

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

                if ($direct == 'N') {
                    mysqli_query($conn, "INSERT INTO `$stkdb`(`srno`, `yearno`, `vouchno`, `vouchdt`, `prodcode`, `qty`, rate, godown,
                                                  `source`, amount, accode, `group` , `cost`, `itemgrp`, `ordno`, `monno`)
                                     VALUES('$cnt', '$myyear', '$mvouch', '$vouchdt', '$product',  '$tqty', '$rate','$godown',
                                                   '$source', '$amt', '$party', '$group', '$cost', '$itemgrp', '1', '$mono')") or die(mysqli_error($conn));
                }
            }
        }


        ############################### Accounts Part ######################################################

        $sstot = $totbillamt - $tcsamt + $discondies;
        $minus_tot = $sstot * -1;
        $minus_tcs = $tcsamt * -1;
        $minus_discdies = $discondies * -1;

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

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

        // party part
        mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, `godown`, source, trnno, accode, grpcode, amount, staffcode)
                                VALUES ('$vouchdt','$mvouch','$godown', '$source', '002', '$accode','010', '$minus_tot','$purcode')") or die(mysqli_error($conn));

        // discount on diesel - party
        if ($discondies > 0) {
            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, `godown`, source, trnno, accode, grpcode, amount, staffcode)
                                VALUES ('$vouchdt','$mvouch','$godown', '$source', '003', '$accode', '010', '$discondies','31')") or die(mysqli_error($conn));

            // discount on diesel
            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, `godown`, source, trnno, accode, grpcode, amount, staffcode)
                                VALUES ('$vouchdt','$mvouch','$godown', '$source', '004', '31', '012', '$minus_discdies','$accode')") or die(mysqli_error($conn));
        }

        // TCS party
        if ($tcsamt > 0) {
            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, `godown`, source, trnno, accode, grpcode, amount, staffcode)
                                VALUES ('$vouchdt','$mvouch','$godown', '$source', '005', '$accode', '010', '$minus_tcs','32')") or die(mysqli_error($conn));

            // TCS
            mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, `godown`, source, trnno, accode, grpcode, amount, staffcode)
                                VALUES ('$vouchdt','$mvouch','$godown', '$source', '006', '32', '020', '$tcsamt','$accode')") or die(mysqli_error($conn));
        }
        ///////////////////        

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

            mysqli_query($conn, "INSERT INTO `$maindb`(vouchno, `vouchdt`, `voucher`, `monno`,`custcode`, `party`, `direct`,`paidby`, `dept`,`subtotal`, 
                                             `discondies`,`totbillamt`,`slipno`,`billno`, `daily`, `rem1`, `rem2`, `cashvouch`, `grossamt`, `mobile`, vehicleno, 
                                              `lrno`, `lrdate`, `transporter`, `area`,  `godown`,`totqty`,`tcsamt`,`tcsperc`,
                                               `purcode`,`billamt`,username,`shift`,discdies, invno)
                                      VALUES('$mvouch', '$vouchdt', '$newvoucher', '$mono','$custcode', '$party', '$direct','$paidby','$dept','$subtotal', 
                                             '$discondies','$totbillamt','$slipno','$billno', '$mdailyprint',
                                             '$rem1', '$rem2', '$mcashvouch', '$grossamt', '$mobile','$vehicleno',
                                             '$lrno', '$lrdate', '$transporter', '$area','$godown','$totqty','$tcsamt','$tcsperc',
                                             '$purcode','$totbillamt','$username','$shift','$discdies','$minvoiceno')") or die(mysqli_error($conn));
        } else {
            //            browse($mvouch);
            mysqli_query($conn, "UPDATE `$maindb` SET  `vouchdt`='$vouchdt', `monno`='$mono',`custcode`='$custcode', `party`='$party', `direct`='$direct',
                                         `dept`='$dept',`paidby`='$paidby', `daily`='$mdailyprint',
                                         `subtotal`='$subtotal',`discondies`='$discondies',`totbillamt`='$totbillamt',`slipno`='$slipno',`billno`='$billno',
                                         `rem1`='$rem1', `rem2`='$rem2',`grossamt`='$grossamt', mobile='$mobile',`vehicleno`='$vehicleno',`tcsamt`='$tcsamt',  discdies='$discdies',
                                         `purcode`='$purcode',`cashvouch`='$mcashvouch',`godown`='$godown', `totqty`='$totqty',
                                         `billamt`='$totbillamt',  lrno = '$lrno', lrdate = '$lrdate', transporter = '$transporter',`tcsperc` = '$tcsperc', 
                                         `area` = '$area',  `username`='$username' , `shift` = '$shift', `invno` = '$minvoiceno'
                                          WHERE `vouchno` = '$mvouch' ") or die(mysqli_error($conn));
        }

        $tdsperc = 0;
        $tdsamt = 0;

        if ($mdailyprint == 1) {
            if ($mtdsapply == "Y") {

                $mcono = RIGHT($zcomp, 2);
                $getcomp = mysqli_query($conn, "SELECT * from $zroot.company WHERE cono = '$mcono' ") or die(mysqli_error($conn));
                $lcomp = mysqli_fetch_array($getcomp);
                $tdsperc = $lcomp['tdsperc'];

                $tdsamt = $totbillamt * $tdsperc / 100;
                $minus_tds = $tdsamt * -1;

                // tds - party
                mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, grpcode, amount, staffcode)
                            VALUES ('$vouchdt','$mvouch', '$source', '004', '$accode', '010', '$tdsamt', '34')") or die(mysqli_error($conn));

                // tds
                mysqli_query($conn, "INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, grpcode, amount, staffcode)
                                VALUES ('$vouchdt','$mvouch', '$source', '004', '000034', '020', '$minus_tds','$accode')") or die(mysqli_error($conn));
            }
        }

        ////    for setup and invoice dbf plus actran part


        if ($mdailyprint == 1) {
            $stdate = '2012-07-01';     //      && date - service tax applicable

            $mmonth1 = strtotime($vouchdt);
            $mmonth = date('m', $mmonth1);

            if ($mmonth > 3) {
                $myear = date('Y', strtotime($zsdate));
            } else {
                $myear = date('Y', strtotime($zedate));
            }

            $mperiod = 8;
            $dinvno = $minvoiceno;
            $mdaydate = $vouchdt;
            $acode = $accode;
            $paccode = $accode;
            $minvdt = $vouchdt;
            $chkdate = $vouchdt;
            $mvehno = $vehicleno;

            $opchg = 0;
            $oper = 0;
            $stopperc = 0;

            $zcomp = $_SESSION["comp_" . $dbname];
            $mcono = $mcono;

            $qryyear = mysqli_query($conn, "SELECT * from $zroot.compyear WHERE cono = '$mcono' AND finyear = '$zyear' ") or die(mysqli_error($conn));
            $fetyear = mysqli_fetch_array($qryyear);
            $stopperc = $fetyear['stopperc'];
            $zacopchgs = $fetyear['minopchg'];

            $getp = mysqli_query($conn, "SELECT * FROM `accmast` WHERE `custcode`= '$custcode' ") or die(mysqli_error($conn));
            $fetp = mysqli_fetch_array($getp);
            $opflag = $fetp['apply'];
            $charges = $fetp['charges'];

            $oparr = 0;

            if ($opflag == 'Y') {
                if ($charges > 0) {
                    $opchg = $charges;
                } else {
                    $opchg = $zacopchgs;
                }
            } else {
                $opchg = 0;
            }

            $sub = $subtotal;
            $discdies = $ttdiscdies;
            $mtcsamt = $tcsamt;

            if ($opflag == "Y") {
                if (($sub * ($opchg / 100)) < $zminopchg) {
                    $oper = $zminopchg;
                } else {
                    $oper = $sub * ($opchg / 100);
                }
            } else {
                $oper = 0;
            }

            if ($chkdate >= $stdate and $oper > 0) {
                $stopchgs = $oper * ($stopperc / 100);
            } else {
                $stopchgs = 0;
                $stopperc = 0;
            }

            $arrears = 0;

            $sot = $sub + $oper + $stopchgs - $discdies + $tcsamt;
            $rsot = ROUND($sot, 0);

            $rof = $sot - $rsot;
            $grd = $rsot;

            //// *********** invoice replace  part     **** 

            include('delinvoice.php');

            //     do delinv with mmonth,acode,7, " and invdate = minvdt and tran = cno "


            delinv($mmonth, $acode, 7, " and invdate = '$mdaydate' and tran = '$cno' ");

            //////////// 999999999999

            $mhalf2 = $mperiod - 1;
            mysqli_query($conn, "INSERT INTO `invoice` (`month`, `half`, `year`,  `period`,  `salevouch`,  `invno`, `tran`, `invdate`, `custcode`, `accode`, `vehno`,
                                                `subtotal`, `opchg`, `operchgs`, `discdies`, `tcsamt`, `tdsperc`, `tdsamt`, 
                                                `stonopperc`, `stonopchgs`, `sototal`, `roff`, `billamt`, `arrears`, `oparr`, `grdtot`)
                                        VALUES ('$mmonth', '$mhalf2', '$myear', '$mperiod', '$vouchno', '$dinvno', '$cno', '$mbilldate', '$custcode', '$accode', '$vehno',
                                                '$sub', '$opchg', '$oper', '$discdies', '$tcsamt', '$tdsperc', '$tdsamt', 
                                                '$stopperc', '$stopchgs', '$sot', '$rof', '$rsot', '$arrears', '$oparr', '$grd') ") or die(mysqli_error($conn));



            ////////////    actran   /// accounts part   -- -  For Operating Charges

            $ddate = $mtdt;
            $paccode = $accode;

            $trnno = 12 + $mperiod;
            $minoper = $oper * -1;
            if ($oper > 0) {
                mysqli_query($conn, "INSERT INTO `actran` (`source`, `vouchno`, `date`, `trnno`, `accode`, `grpcode`, `amount`, `staffcode`)
                                        VALUES ('OC', '$vouchno', '$ddate', '$trnno', '$paccode', '010', '$minoper', '000027') ") or die(mysqli_error($conn));

                mysqli_query($conn, "INSERT INTO `actran` (`source`, `vouchno`, `date`, `trnno`, `accode`, `grpcode`, `amount`, `staffcode`)
                                        VALUES ('OC', '$vouchno', '$ddate', '$trnno', '000027', '011', '$oper', '$paccode') ") or die(mysqli_error($conn));
            }

            //// *************************   	   && For service tax on Operating Charges

            if ($stopchgs > 0) {
                $minstopchgs = $stopchgs * -1;
                mysqli_query($conn, "INSERT INTO `actran` (`source`, `vouchno`, `date`, `trnno`, `accode`, `grpcode`, `amount`, `staffcode`)
                                        VALUES ('STOC', '$vouchno', '$ddate', '$trnno', '$paccode', '010', '$minstopchgs', '000026') ") or die(mysqli_error($conn));

                mysqli_query($conn, "INSERT INTO `actran` (`source`, `vouchno`, `date`, `trnno`, `accode`, `grpcode`, `amount`, `staffcode`)
                                        VALUES ('STOC', '$vouchno', '$ddate', '$trnno', '000006', '020', '$stopchgs', '$paccode' ") or die(mysqli_error($conn));
            }


            /// *************** 	      && For operating charges on arrears 

            if ($oparr > 0) {
                $minoparr = $oparr * -1;
                mysqli_query($conn, "INSERT INTO `actran` (`source`, `vouchno`, `date`, `trnno`, `accode`, `grpcode`, `amount`, `staffcode`)
                                        VALUES ('AC', '$vouchno', '$ddate', '$trnno', '$paccode', '010', '$minoparr', '000039') ") or die(mysqli_error($conn));

                mysqli_query($conn, "INSERT INTO `actran` (`source`, `vouchno`, `date`, `trnno`, `accode`, `grpcode`, `amount`, `staffcode`)
                                        VALUES ('AC', '$vouchno', '$ddate', '$trnno', '000039', '011', '$oparr', '$paccode') ") or die(mysqli_error($conn));
            }

            if ($rof != 0) {
                $minrof = $rof * -1;
                mysqli_query($conn, "INSERT INTO `actran` (`source`, `vouchno`, `date`, `trnno`, `accode`, `grpcode`, `amount`, `staffcode`)
                                        VALUES ('RO', '$vouchno', '$ddate', '$trnno', '$paccode', '010', '$rof', '000028') ") or die(mysqli_error($conn));

                mysqli_query($conn, "INSERT INTO `actran` (`source`, `vouchno`, `date`, `trnno`, `accode`, `grpcode`, `amount`, `staffcode`)
                                        VALUES ('RO', '$vouchno', '$ddate', '$trnno', '000028', '012', '$minrof', '$paccode') ") or die(mysqli_error($conn));
            }

            if ($tdsamt > 0) {
                $mintds = $tdsamt * -1;
                mysqli_query($conn, "INSERT INTO `actran` (`source`, `vouchno`, `date`, `trnno`, `accode`, `grpcode`, `amount`, `staffcode`)
                                        VALUES ('TDS', '$paccode', '$ddate', '$trnno', '$paccode', '010', '$tdsamt', '000034') ") or die(mysqli_error($conn));

                mysqli_query($conn, "INSERT INTO `actran` (`source`, `vouchno`, `date`, `trnno`, `accode`, `grpcode`, `amount`, `staffcode`)
                                        VALUES ('TDS', '$paccode', '$ddate', '$trnno', '000034', '020', '$mintds', '$paccode') ") or die(mysqli_error($conn));
            }
        }
        ///// ************ for setup and invoice dbf plus actran part

        if ($addflag == 1) {
            insmess();
            header("refresh: 1; $file?mode=true&par=A");
//            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            updmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        }
        mysqli_query($conn, "UNLOCK TABLES");
    }
}

if ($addflag != 1) {

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

    $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE $keyfld='$getvouch' AND saletype !='C' ") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);
    $daily = $get['daily'];

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

    $depa = mysqli_query($conn, "SELECT `code`,`name`,`dptcode` FROM `dept` WHERE `dptcode`= '$get[dept]'") or die(mysqli_error($conn));
    $getdept = mysqli_fetch_array($depa);

    $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);

    $getsh = mysqli_query($conn, "SELECT * FROM `shift` WHERE `shift`= '$get[shift]'") or die(mysqli_error($conn));
    $getshift = mysqli_fetch_array($getsh);

    $getdaily = mysqli_query($conn, "SELECT dailyprint FROM `accmast` WHERE `accode`= '$get[party]' AND billcycle = 'D' ") or die(mysqli_error($conn));
    $dailypn = mysqli_fetch_array($getdaily);

    $dn = $dailypn['dailyprint'];

    $delcsh = $get['cashvouch'];
} else {
    $result1 = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE $keyfld < $qvouch order by vouchno desc limit 0,1") or die(mysqli_error($conn));
    if (mysqli_num_rows($result1) > 0) {
        $get1 = mysqli_fetch_array($result1);
        if ($par == 'A') {
            $vchdt = $get1['vouchdt'];
            $shft = $get1['shift'];
            $fcs = "autofocus";
            $afs = "";
            $fca = "focusfield";
            $afa = "";
        } else {
            $vchdt = date("Y-m-d");
            $vehcode2 = '';
            $vehicle2 = '';
            $fcs = "";
            $afs = "autofocus";
            $fca = "";
            $afa = "focusfield";
        }
    }
}

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

    $delcond = " source = '$source' and vouchno = '$_GET[vouchno]'";
    $delcond6 = " source = 'RO' and vouchno = '$_GET[vouchno]'";
    $delcond1 = " source = 'CR' and vouchno = '$delcsh'";
    $delcond3 = " vouchno = '$delcsh'  ";
    $delcond2 = " vouchno = '$_GET[vouchno]'";
    $delcond4 = " salevouch = '$_GET[vouchno]'";

    if ($daily == 1) {
        delnew($zdb, "invoice", "$delcond4", "$file?mode=last");
    }

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

    delnew($zdb, "cashrecp", "$delcond3", "$file?mode=last");

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

include("includes/linkshead.php");
?>
<script src="MyScript/sale.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/sale_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" value="<?php echo ($addflag != 1) ? $get['vouchno'] : $qvouch ?>" readonly required>
                                <input type="hidden" name="voucher" class="form-control form voucher" value="<?php echo ($addflag != 1) ? $get['voucher'] : '' ?>" tabindex="-1">
                                <input type="hidden" name="invno" class="form-control form invno" value="<?php echo ($addflag != 1) ? $get['invno'] : '' ?>" tabindex="-1">
                                <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 canc_flag" name="" value="<?php echo ($addflag != 1) ? $get["canc_flag"] : ""; ?>">
                                <input type="hidden" class="form-control form discdies" name="discdies" value="<?php echo ($addflag != 1) ? $get["discdies"] : ""; ?>">
                            </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 <?= $afa ?>" name="vouchdt" value="<?php echo ($addflag != 1) ? $get["vouchdt"] : "$vchdt"; ?>" <? $afs ?> required>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-2 col-xs-3 text-right pad">
                                <label class="label-control marg">Direct</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-3 col-xs-4 pad">
                                <select id="direct" name="direct" class="form-control form select2 direct" required>
                                    <option <?php
if ($get['direct'] == 'N') {
    echo "selected='selected'";
}
?> value="N">No</option>
                                    <option <?php
                                    if ($get['direct'] == 'Y') {
                                        echo "selected='selected'";
                                    }
?> value="Y">Yes</option>

                                </select>
                            </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" id="shift" class="form-control form " required>
                                    <option value="<?php echo ($addflag != 1) ? $get['shift'] : "$shft"; ?>"><?php echo ($addflag != 1) ? $get['shift'] : "$shft"; ?></option>
                                    <?php
                                    $mshift = mysqli_query($conn, "SELECT shift FROM shift order by shift") or die(mysqli_error($conn));
                                    while ($trow = mysqli_fetch_array($mshift)) {
                                        echo "<option value='$trow[shift]'>$trow[shift]</option>";
                                    }
                                    ?>
                                </select>

                            </div>

                            <input type="hidden" name="dept" class="form-control form  dept" value="DW" required>
                        </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">Department</label>
                                                    </div>
                                                    <div class="col-lg-3 col-md-2 col-sm-3 col-xs-3  pad">
                                                        <select name="dept" class="form-control form  select2 dept" required>
                                                            <option value="<?php echo ($addflag != 1) ? $getdept['dptcode'] : "dw"; ?>"><?php echo ($addflag != 1) ? $getdept['name'] : ""; ?></option>
                        <?php
                        $mown = mysqli_query($conn, "SELECT * FROM dept") or die(mysqli_error($conn));
                        while ($trow = mysqli_fetch_array($mown)) {
                            echo "<option value='$trow[dptcode]'>$trow[name]</option>";
                        }
                        ?>
                                                        </select>
                                                    </div>
                        
                                                    
                        
                                                     comment 
                                                </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">A/c.Code</label>
                            </div>
                            <div class="col-lg-1 col-md-2 col-sm-3 col-xs-3  pad">
                                <input type="text" class="form-control form custcode  <?= $fca ?>" name="custcode" value="<?php echo ($addflag != 1) ? $get["custcode"] : ""; ?>" <?= $fcs ?>>
                            </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">Name</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-7 col-xs-9  pad">
                                <select name="party" class="form-control form  select2 party" 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 = '010' ") 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-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Vehicle No.</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-7 col-xs-9  pad" required>
                                <select name="vehicleno" class="form-control form select2 vehicleno" required>
                                    <option value="<?php echo ($addflag != 1) ? $get['vehicleno'] : ""; ?>"><?php echo ($addflag != 1) ? $get['vehicleno'] : ""; ?></option>
                                    <?php
                                    $gvehicleno = mysqli_query($conn, "SELECT vehicleno FROM `custvehicle` where `accode`='$lparty[accode]' group by vehicleno") or die(mysqli_error($conn));
                                    while ($tvehicleno = mysqli_fetch_array($gvehicleno)) {
                                        $vehicleno = $tvehicleno['vehicleno'];
                                        echo "<option value='$vehicleno'>$vehicleno</option>";
                                    }
                                    ?>

                                </select>
                            </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">Slip No.</label>
                            </div>
                            <div class="col-lg-1 col-md-2 col-sm-3 col-xs-3  pad">
                                <input type="text" name="slipno" class="form-control form slipno" value="<?php echo ($addflag != 1) ? $get['slipno'] : ""; ?>">
                            </div>

                            <div class="col-lg-1 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Bill.No.</label>
                            </div>
                            <div class="col-lg-1 col-md-2 col-sm-3 col-xs-3  pad">
                                <input type="text" name="billno" class="form-control form billno" value="<?php echo ($addflag != 1) ? $get['billno'] : ""; ?>">
                            </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: 2%">Sr.</th>
                                        <th style='width: 5%' class="text-left">Code</th>
                                        <th style='width: 10%' class="text-left">Item</th>
                                        <th style='width: 5%' class="text-right">Qty.</th>
                                        <th style='width: 5%'>Unit</th>
                                        <th style='width: 5%;text-align:right'>Rate</th>
                                        <th style='width: 9%' class="text-right">Amount</th>
                                        <!--<th style='width: 9%' class="text-right">Disc. Diesel</th>-->
                                    </tr>
                                </thead>
                                <tbody class="tbody">
                                    <?php
                                    if ($addflag != 1) {
                                        $qry = mysqli_query($conn, "SELECT * FROM  `$trandb` WHERE vouchno='$getvouch' and saletype !='C' ORDER BY srno") or die(mysqli_error($conn));
                                        $ctr = 0;
                                        while ($row = mysqli_fetch_array($qry)) {
                                            $getitem = mysqli_query($conn, "SELECT prodcode, name FROM `product` WHERE prodcode = '$row[product]'") or die(mysqli_error($conn));
                                            $geti = mysqli_fetch_array($getitem);

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

                                            echo "<td>";
                                            echo "<select name='product_$ctr' class='form-control form  select2 product' style='width:100%' >";
                                            echo "<option value='$geti[prodcode]'>$geti[name]</option>";
                                            $getp = mysqli_query($conn, "SELECT `prodcode`,`name` FROM `product`") or die(mysqli_error($conn));
                                            while ($trow = mysqli_fetch_array($getp)) {
                                                echo "<option value='$trow[prodcode]'>$trow[name]</option>";
                                            }
                                            echo "</select>";
                                            echo "</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]' readonly></td>";
                                            echo "<td><input type='text' class='form-control form text-right amount'  name='amount_$ctr'  value='$row[amount]' readonly>";
                                            echo "<input type='hidden' class='form-control form text-right diesdisc'  name='diesdisc_$ctr'  value='$row[diesdisc]' readonly></td>";
                                            echo "</tr>";
                                            $ctr++;
                                        }
                                    } else {
                                        ?>
                                        <tr>
                                            <td><i class='fa fa-close remove'></i></td>
                                            <td><input type='text' name='srno_0' class='srno form-control form' value='1'></td>
                                            <td><input type='text' name='prodcode_0' class='prodcode form-control form' value=''></td>
                                            <td>
                                                <select name="product_0" class="form-control form  select2 product" style="width:100%">
                                                    <option></option>
                                                    <?php
                                                    $getp = mysqli_query($conn, "SELECT `prodcode`,`name` FROM product ORDER BY name") or die(mysqli_error($conn));
                                                    while ($trow = mysqli_fetch_array($getp)) {
                                                        echo "<option value='$trow[prodcode]'>$trow[name]</option>";
                                                    }
                                                    ?>
                                                </select>
                                            </td>
                                            <td><input type='text' name='qty_0' class='qty text-right form-control form' value='0'></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='0' readonly></td>
                                            <td><input type='text' name='amount_0' class='amount text-right form-control form' value='0' readonly>
                                                <input type='hidden' name='diesdisc_0' class='diesdisc text-right form-control form' value='0' readonly>
                                            </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>
                        </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">Total (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 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-4 col-xs-7 pad text-right">
                                        <label class="label-control marg">Discount On Diesel</label>
                                    </div>
                                    <div class="col-lg-2 col-xs-5 pad">
                                        <input type="number" step="any" name="discondies" class="form-control form text-right  discondies" value="<?php echo ($addflag != 1) ? $get['discondies'] : "0"; ?>" readonly tabindex="-1">
                                    </div>
                                </div>
                                <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">TCS</label>
                                    </div>
                                    <div class="col-lg-1 col-xs-3 pad">
                                        <input type="number" step="any" name="tcsperc" class="form-control form text-right tcsperc" value="<?php echo ($addflag != 1) ? $get['tcsperc'] : "0"; ?>">

                                    </div>
                                    <div class="col-lg-1 col-xs-1 pad text-right">
                                        <label class="label-control marg">%</label>
                                    </div>
                                    <div class="col-lg-2 col-xs-3 pad">
                                        <input type="number" step="any" name="tcsamt" class="form-control form text-right tcsamt" value="<?php echo ($addflag != 1) ? $get['tcsamt'] : "0"; ?>">
                                    </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-4 col-xs-7 pad text-right">
                                            <label class="label-control marg">Bill Amount</label>
                                        </div>
                                        <div class="col-lg-2 col-xs-5 pad">
                                            <input type="number" step="any" name="totbillamt" class="form-control form text-right tottotal totbillamt" value="<?php echo ($addflag != 1) ? $get['billamt'] : "0"; ?>" readonly tabindex="-1">
                                        </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 if ($dn == 1) { ?>

                                <div class="col-lg-10 col-md-12 col-sm-12 col-xs-12 pad">
                                    <?php
                                    include("includes/buttons.php");
                                    $hpara = "N";
                                    ?>
                                </div>

                                <div class="col-lg-2 col-md-12 col-sm-12 col-xs-12 pad">

                                    <td class=" buttonform tooltip-bottom" id="hprint" style="display: <? Echo $printdisabled ?>" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="Print">
                                        <a id='print' class="btn btn-pad btn-square " style="background-color:#5BC0DE;" href="<?= $saleinvprint; ?>?vouchno=<?= $qvouch; ?>" target="_blank">
                                            <b class="" style="color:#ffffff">Invoice Print</b></a>
                                    </td>
                                </div>
                            <?php } else { ?>

                                <?php include("includes/buttons.php"); ?>
                            <?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 modal-lg  searchModal" 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="row">
                        <div class="col-lg-2">
                            <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">Cust Code</option>
                                <option value="4">Vehicle No.</option>
                                <option value="5">Name</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">Cust Code</option>
                                <option value="4">Vehicle No.</option>
                                <option value="5">Name</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 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 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="15%">Cust Code</th>
                                    <th width="30%">Vehicle No.</th>
                                    <th width="40%">Name</th>
                                    <th width="5%">Action</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 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>