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

$formname = 'Challan';
$maindb = 'challan';
$cashdb = 'cashrecp';
$accdb = 'actran';

$printfile = "chal_print.php";
$file = 'chal.php';
$fileadd = 'leadenq.php';
$trandbexist = 0;
$grid = 'chalmain.php';
$keyfld = "vouchno";
$keycond = " ";
$focusfield = "name";
$source = "CH";

//$mgrpcode = '009';

$zroot = $_SESSION["zdir"];

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$usertype = $_SESSION["usertype"];
$user = $_SESSION['username_' . $dbname];

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

if (isset($_POST["submit"])) {
    mysqli_select_db($conn,$zdb);
    $a = 1;
    $vouchno = $_POST["vouchno"];
    $vouchdt = $_POST["vouchdt"];
    $mark = $_POST["mark"];
    $delv = $_POST["delv"];
    $delvdate = $_POST["delvdate"];
    $party = $_POST["party"];
    $nag = $_POST["nag"];
    $size = $_POST["size"];
    $stype = $_POST["stype"];
    $sizemm = $_POST["sizemm"];
    $guage = $_POST["guage"];
    $gtype = $_POST["gtype"];
    $guagemm = $_POST["guagemm"];
    $makesize = $_POST["makesize"];
    $mktype = $_POST["mktype"];
    $makemm = $_POST["makemm"];
    $dia = $_POST["dia"];
    $ap = $_POST["ap"];
    $weld = $_POST["weld"];
    $straight = $_POST["straight"];
    $foot = $_POST["foot"];
    $totfeet = $_POST["totfeet"];
    $weight = $_POST["weight"];
    $rate = $_POST["rate"];
    $amount = $_POST["amount"];
    $gstflag = $_POST["gstflag"];
    $gstperc = $_POST["gstperc"];

    $cashcred = $_POST["cashcred"];
    $type = $cashcred;

    $descrip = $_POST["descrip"];
    $cashvouch = $_POST["cashvouch"];
    $netamt = $amount;

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


    if ($delv == "D") {
        if ($delvdate == '0000-00-00' || $delvdate == '') {
            $delvdate = date("Y-m-d");
        }
    }

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

    if ($a == 1) {

        if ($addflag == 2) {

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

        $mvouch = $qvouch;

#############AUto Cash Receipt Start####################
        $mcashamt = $amount;


        $mcashvouch = $_POST["cashvouch"];

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

//            browse($mcashvouch);


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

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

            if ($ladvance > 0) {
                $tamt = $ladvance;
                $adv = "Advance Received For";
            } else {
                $tamt = $mcashamt;
                $adv = "Cash Received For";
            }
            mysqli_query($conn,"DELETE FROM `$cashdb` WHERE `$keyfld`='$mcashvouch'") or die(mysqli_error($conn));


            $type = '';
            $weight = 0;
            if ($weight == 0) {
                if ($straight == 'Y' || $straight == 'y') {
                    $qty = $totfeet;
                } else {
                    $qty = $nag;
                }
                $type = "F";
            } else {
                $qty = $weight;
                $type = "W";
            }

//            browse($totfeet);
//            browse($nag);

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

// cash part     

            $minus_amt = ($tamt * -1);


            $actamt = $tamt;
            $actminus = $tamt * -1;



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

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


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


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






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



        if ($amount > 0 && $delvdate != '0000-00-00' && $delvdate != '' && $gstflag == 'N') {

            $minus_amt = $amount * -1;

// sale part
            mysqli_query($conn,"INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, grpcode, amount)
                    VALUES ('$vouchdt','$mvouch', '$source', '001', '$salecode','005', '$amount')") OR die(mysqli_error($conn));
// party part
            mysqli_query($conn,"INSERT INTO `$accdb`(date, vouchno, source, trnno, accode, grpcode, amount)
                     VALUES ('$vouchdt','$mvouch', '$source', '002', '$party', '010', '$minus_amt')") or die(mysqli_error($conn));
        }
############################## MAIN ######################################################
        if ($addflag == 2) {
            mysqli_query($conn,"INSERT INTO `$maindb`(`vouchno`, `vouchdt`, `mark`, `delv`, `delvdate`, `party`, `nag`, `sizemm`,
                        `guagemm`,`makemm`, `dia`,`ap`, `weld`, `straight`, `foot`, `totfeet`, `weight`, `rate`,
                        `amount`, `cashcred`, `descrip`, `cashvouch` , `size`, `stype`, `makesize`, `mktype`, `guage`, `gtype`, `gstflag`, `gstperc`)
                 VALUES ('$qvouch', '$vouchdt', '$mark', '$delv', '$delvdate','$party', '$nag', '$sizemm',
                        '$guagemm', '$makemm', '$dia', '$ap', '$weld', '$straight', '$foot', '$totfeet', '$weight', '$rate',
                        '$amount', '$cashcred', '$descrip', '$mcashvouch', '$size', '$stype', '$makesize','$mktype','$guage','$gtype','$gstflag','$gstperc')") or die(mysqli_error($conn));


            insmess();
            $mvouch = $qvouch; //1
            header("refresh: 1; $grid?mode=true");
        } else {
//            browse($qvouch);
            mysqli_query($conn,"UPDATE `$maindb` SET `vouchno`='$qvouch',`vouchdt`='$vouchdt',`mark`='$mark',`delv`='$delv',
                    `party`='$party',`delvdate`='$delvdate',`nag`='$nag', `size` = '$size', `stype`='$stype', 
                    `sizemm`='$sizemm',`guagemm`='$guagemm',`makemm`='$makemm', `makesize`='$makesize', `mktype`='$mktype',
                    `guage`='$guage', `gtype`='$gtype',   
                    `dia`='$dia',`ap`='$ap',`weld`='$weld',`straight`='$straight',`foot`='$foot',`totfeet`='$totfeet',
                    `weight`='$weight',`rate`='$rate',`amount`='$amount',`cashcred`='$cashcred',`descrip`='$descrip',
                    `cashvouch`='$mcashvouch', `gstflag`='$gstflag', `gstperc`='$gstperc' WHERE `vouchno`='$qvouch'") or die(mysqli_error($conn));
            updmess();
//            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
            header("refresh: 1; $grid?mode=last");
        }
    }
}

mysqli_select_db($conn,$zdb);
if ($addflag != 1) {
    $getvouch = '';
    if ($addflag == 0) {
        $getvouch = $_GET[$keyfld];
    } else {
        $getvouch = $qvouch;
    }
    $result = mysqli_query($conn,"SELECT * FROM `$maindb` WHERE $keyfld='$getvouch' ") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);
    $delcsh = $get['cashvouch'];
    $getparty = mysqli_query($conn,"SELECT `accode`,`name` FROM `accmast` where `grpcode`='010' AND `accode`='$get[party]' order by name")or die(mysqli_error($conn));
    $fsetparty = mysqli_fetch_array($getparty);

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

//include("includes/vouchno_generate.php");
//include("includes/prev&next.php");
if (isset($_GET["delete"])) {
    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$grid?mode=last");

    $delcond2 = " source = 'CR' and vouchno = '$delcsh' ";
    $delcond1 = " source = 'CH' and vouchno = '$_GET[vouchno]' ";
    delnew($zdb, "$accdb", "$delcond1", "$grid?mode=last");
    delnew($zdb, "$accdb", "$delcond2", "$grid?mode=last");
    delete($zdb, "$cashdb", "$keyfld", $delcsh, "$grid?mode=last");
}


include("includes/linkshead.php");
?>
<!--<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.css">
<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.js"></script>-->

<script src="MyScript/chalmain2.js" type="text/javascript"></script>
<script src="MyScript/chalmain_search.js" type="text/javascript"></script>
<script src="MyScript/challanfunctn.js" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {

        $("body").on("change blur", '.querysizeval', sizecal);
        $("body").on("change blur", '.queryggval', guagecal);
        $("body").on("blur", '.querymakeval', makeCal);

        function sizecal() {
            var qsval = $('.qsval').val(),
                    querysizeval = $('.querysizeval').val();
            $.post('get_chalDtl.php', {sval: qsval, soval: querysizeval}, function (data) {
                $(".qsoval").val(data);
                // $('#sizemmod').modal('hide');
                $(".qsoval").focus();
            });
        }
        function guagecal() {
        var qgval = $('.qgval').val(),
                qsval = $('.qsval').val(),
                queryggval = $('.queryggval').val();
        $.post('get_chalDtlG.php', {gval: qgval, goval: queryggval, qsval: qsval}, function (data) {
            $(".qgoval").val(data);
            // $('#guagemmod').modal('hide');
            $(".qgoval").focus();
        });
    }
    function makeCal() {
        var qmval = $('.qmval').val(),
                querymakeval = $('.querymakeval').val();
        $.post('get_chalDtl.php', {sval: qmval, soval: querymakeval}, function (data) {
            $(".qmoval").val(data);
//            $('#makemmod').modal('hide');
            $(".qmoval").focus();
        });
    }
    });
    $(document).keydown(function (event) {
        if (!(String.fromCharCode(event.which).toLowerCase() == 'a' && event.altKey) && !(event.which == 19))
            return true;
        $("#addnew").click();
        event.preventDefault();
        return false;
    });
    $(document).keydown(function (event) {
        //19 for Mac Command+S
        if (!(String.fromCharCode(event.which).toLowerCase() == 's' && event.altKey) && !(event.which == 19))
            return true;
        //alert("Ctrl-s pressed");
        $("#update").click();
        event.preventDefault();
        return false;
    });

    $(document).keydown(function (event) {
        //19 for Mac Command+S
        if (!(String.fromCharCode(event.which).toLowerCase() == 'f' && event.altKey) && !(event.which == 19))
            return true;
        //alert("Ctrl-s pressed");
        $("#btsearch").click();
        event.preventDefault();
        return false;
    });

</script>
<style>
    input:focus tr{ background-color:#skyblue;}

    .button {
        padding: 15px 25px;
        font-size: 15px;
        text-align: center;
        cursor: pointer;
        outline: none;
        color: #fff;
        background-color: #4CAF50;
        border: none;
        border-radius: 15px;
        box-shadow: 0 9px #999;
    }

    .button:hover {background-color: #3e8e41}

    .button:active {
        background-color: #3e8e41;
        box-shadow: 0 5px #666;
        transform: translateY(4px);
    }
    /* Sortable tables */
    table.sortable thead {
        /*background-color:#eee;*/
        /*color:#666666;*/
        font-weight: bold;
        cursor: default;
    }
    .table>tbody>tr>td {
        padding: 1px;
    }
    .form-control{
        padding: 0px 0px; 
    }
    body{
        font-size: 12px;
    }
    table td{
        font-size:13px;
    }


    .f12{
        font-size:12px ;
    }

    #PTbody{overflow-y: scroll;}

    .modal-content {
        margin-right: 20%;
        margin-left: 20%;
    }
    .bcolor {

        background-color: white;
    }

</style>

</head>
<body>
    <form action='' method='POST'>
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title" style="color:inherit"><?php Echo $formname; ?></h3>
                <div class="actions pull-right">
                    <i class="fa fa-chevron-down"></i>
                    <i class="fa fa-times"></i>
                </div>
            </div>
            <div class="row tohide">

                <div class="col-lg-2 " style="padding-left: 2%">

                    <input id='addnew' value="Add New Challan" type="button"  class="btn btn-success mySearchTr btn btn-labeled btn-success" style="background-color: #009552;">

                </div>
                <div class="col-lg-2 " >
                    <!--<input id='addnew' value="Add New Challan" type="button"  class="btn btn-success mySearchTr" style="" onclick="window.location.href = '<?= $file . "?mode=true" ?>'" class="btn btn-labeled btn-success">-->
                    <!--<a href="menu.php?action=chal_print" class="btn btn-info" role="button">Print</a>-->
                    <a id='print' type="button" class="btn btn-info"  href="<?= $printfile ?>" target="_blank">Print</a>

                </div>
                <div class="col-lg-2 " >
                    <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModalCenter">
                        LIST
                    </button>
                </div>
                
            <div class="row ndata" style="display:none;" >
                <div class="row">
                    <div class="col-lg-12 table responsive " style="height: 80px;padding-left: 3px;padding-right: 3px;">
                        <table class="table table-striped  table-bordered" style="font-size: 11px;padding:0px;width:100%">
                            <thead>

                                <tr class="mySearchTr" style="background-color: #009552;">

                                    <th style="width: 2%;">Sr</th>
                                    <th style="width: 2%;">?</th>
                                    <th style="width: 3%;">Date</th>
                                    <th style="width: 3%;">P/R/D</th>
                                    <th style="width: 4%;">Delv.Date</th>
                                    <th style="width:20%;">Party Name</th>
                                    <th style="width: 4%;"class='text-right'>Nags</th>
                                    <th style="width: 5%;" class='text-right'>Size(MM)</th>
                                    <th style="width: 5%;" class='text-right'>T(MM)</th>
                                    <th style="width: 5%;" class='text-right'>Make(MM)</th>
                                    <th style="width: 3%;">Dia</th>
                                    <th style="width: 3%">A/P</th>
                                    <th style="width: 3%">Wld</th>
                                    <th style="width: 3%">STRT</th>
                                    <th style="width: 4%" class='text-right'>FTS</th>
                                    <th style="width: 4%" class='text-right'>T.FTS</th>
                                    <th style="width: 4%" class='text-right'>Weight</th>
                                    <th style="width: 5%" class='text-right'>Rate</th>
                                    <th style="width: 6%" class='text-right'>Amount</th>
                                    <th style="width: 10%">Description</th>
                                    <th style="width: 4%">C/R</th>
                                    <th style="width: 4%">GST</th>
                                    <th style="display: none;width: 4%;" class="tblgstperc">GST%</th>
                                </tr>
                            </thead>

                            <tbody class="nTbody">                                
                                <tr class='' style='font-size: 11px;padding:0px !important;' > 
    <!--                                <td align='left'>
                                        <input type="text" class="form-control text-size vouchno" name="vouchno" value="" required readonly>
                                    </td> -->
                                    <td align='left'>
                                        <input type="text" class="form-control text-size srno f12" name="srno" value="1">
                                    </td>
                                    <td align='left'>
                                        <input type="text" class="form-control text-size mark f12" name="mark" value="">
                                    </td>
                                    <td align='left'>
                                        <input type="date" style="" class="form-control vouchdt f12"  name="vouchdt" value="<?php Echo ($addflag == 0) ? $get["vouchdt"] : date('Y-m-d'); ?>" required>
                                    </td> 
                                    <td align='left'>
                                        <input type="text" class="form-control text-size f12 delv" name="delv" value="<?php Echo ($addflag == 0) ? $get["delv"] : ""; ?>" >                                
                                    </td> 
                                    <td align='left'>
                                        <input type="date" class="form-control delvdate f12"  name="delvdate" value="<?php Echo ($addflag == 0) ? $get["delvdate"] : '0000-00-00' ?>" >
                                    </td>
                                    <td align='left'>
                                        <input type="text" class="form-control text-size f12 partyname" name="partyname" value="<?php Echo ($addflag == 0) ? $fsetparty["name"] : ""; ?>">
                                        <input type="hidden" class="form-control text-size f12 party" name="party" value="<?php Echo ($addflag == 0) ? $fsetparty["accode"] : ""; ?>">
                                        <!--<select class="form-control party f12 select2" name="party" style="width:100%;" required>-->
                                            <!--<option value="<?php Echo ($addflag != 1) ? $fsetparty['accode'] : ""; ?>"><?php Echo ($addflag != 1) ? $fsetparty['name'] : ""; ?></option>-->
                                        <?php
//                                            if ($addflag == 1) {
//                                            $gparty = mysqli_query($conn,"SELECT `accode`,`name` FROM `accmast` where grpcode='010' order by name asc")or die(mysqli_error($conn));
//                                            while ($fparty = mysqli_fetch_array($gparty)) {
//                                                Echo "<option value='$fparty[accode]'>$fparty[name]</option>";
//                                            }
//                                            }
                                        ?>
                                        <!--</select>-->
                                    </td> 
                                    <td align='left' class="text-right">
                                        <input type="text" class="form-control text-right text-size  number f12" name="nag" value="<?php Echo ($addflag == 0) ? $get["nag"] : ""; ?>" required>
                                    </td> 
                                    <td align='left' class="text-right">
                                        <input type="text" class="form-control text-size sizemm text-right f12" name="sizemm" value="<?php Echo ($addflag == 0) ? $get["sizemm"] : ""; ?>" >
                                    </td> 
                                    <td align='left' class="text-right">
                                        <input type="text" class="form-control guagemm text-right f12"  name="guagemm" value="<?php Echo ($addflag == 0) ? $get["guagemm"] : ""; ?>" >
                                    </td> 
                                    <td align='left' class="text-right">
                                        <input type="text" class="form-control text-size makemm text-right f12" name="makemm" value="<?php Echo ($addflag == 0) ? $get["makemm"] : ""; ?>" >
                                    </td> 
                                    <td align='left'>
                                        <input type="text" class="form-control text-size dia number f12" name="dia" value="<?php Echo ($addflag == 0) ? $get["dia"] : ""; ?>" >                                

                                    </td> 
                                    <td align='left'>
                                        <input type="text" class="form-control text-size ap f12" name="ap" value="<?php Echo ($addflag == 0) ? $get["ap"] : "N"; ?>" >                               

                                    </td> 
                                    <td align='left'>
                                        <input type="text" class="form-control text-size weld f12" name="weld" value="<?php Echo ($addflag == 0) ? $get["weld"] : "N"; ?>" >                                

                                    </td> 
                                    <td align='left'>
                                        <input type="text" class="form-control straight f12"  name="straight" value="<?php Echo ($addflag == 0) ? $get["straight"] : "N"; ?>" >

                                    </td> 
                                    <td align='left' class="text-right">
                                        <input type="text" class="form-control text-size foot text-right f12" name="foot" value="<?php Echo ($addflag == 0) ? $get["foot"] : ""; ?>" >                                

                                    </td> 
                                    <td align='left' class="text-right">
                                        <input type="text" class="form-control text-size totfeet text-right f12" name="totfeet" value="<?php Echo ($addflag == 0) ? $get["totfeet"] : ""; ?>" >                                

                                    </td> 
                                    <td align='left' class="text-right">
                                        <input type="text" class="form-control text-size weight text-right f12" id="weight" name="weight" value="<?php Echo ($addflag == 0) ? $get["weight"] : ""; ?>" >                               

                                    </td> 
                                    <td align='left' class="text-right">
                                        <input type="text" class="form-control text-size rate text-right f12" name="rate" value="<?php Echo ($addflag == 0) ? $get["rate"] : ""; ?>" required>                                

                                    </td> 
                                    <td align='left' class="text-right">
                                        <input type="text" class="form-control amount text-right f12"  name="amount" value="<?php Echo ($addflag == 0) ? $get["amount"] : ""; ?>"  required readonly>
                                    </td> 
                                    <td align='left'>
                                        <input type="text" class="form-control text-size descrip f12" name="descrip" value="<?php Echo ($addflag == 0) ? $get["descrip"] : ""; ?>" >                                

                                    </td> 
                                    <td align='left'>
                                        <input type="text" class="form-control text-size cashcred f12" name="cashcred" value="<?php Echo ($addflag == 0) ? $get["cashcred"] : ""; ?>" >                                
                                    </td>
                                    <td><input type="text" class="form-control text-size gstflag" name="gstflag" value="<?php Echo ($addflag == 0) ? $get["gstflag"] : "N"; ?>" required></td>
                                    <td style="display: none;" class="tblgstperc">
                                        <select name="gstperc" class="form-control select2">
                                            <option value="<?php Echo ($addflag != 1) ? $fetgst["code"] : ""; ?>"><?php Echo ($addflag != 1) ? $fetgst["name"] : ""; ?></option>
                                            <?php
                                            $getgst = mysqli_query($conn,"SELECT * FROM `saletype` where `code`!='' ORDER BY per")or die(mysqli_error($conn));
                                            while ($fetgst = mysqli_fetch_array($getgst)) {
                                                Echo "<option value='$fetgst[code]'>$fetgst[name]</option>";
                                            }
                                            ?>
                                        </select>
                                    </td>
                            <input type="hidden" class="form-control text-size cashvouch f12" name="cashvouch" value="<?php Echo ($addflag == 0) ? $get["cashvouch"] : "0"; ?>" >                               
                            </tr>
                            </tbody>  
                        </table>
                    </div>
                </div>
                <div class="row">

                    <div class="panel-footer">
                        <div style="overflow: auto; ">
                            <table class="table-condensed" style="margin: 0 auto;">
                                <tr>

                                    <td class=" buttonform tooltip-bottom" id="ssubmit" style="" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="Save Record">
                                        <button id='submit' type='submit' name='submit' value='<?php echo ($addflag == 0) ? "Update" : "Save"; ?>' class="btn btn-pad btn-square " style="background-color:#008D4C;display: inline; ">
                                            <i class="fa fa-check" style="color:#ffffff"></i>
                                        </button>
                                    </td>
                                    <td class=" buttonform tooltip-bottom" id="scancel" style="" data-toggle="tooltip" data-placement="bottom" title="" data-original-title="Cancel Record">
                                        <button id='cancel' type="button" class="btn btn-pad btn-square " style="background-color:red; "  onClick="window.location.href = 'chalmain.php?mode=last'">
                                            <i class="fa fa-times" style="color:#ffffff"></i>
                                        </button>
                                    </td>
                                </tr>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
            <div class="panel-body tohide" >
                <div class="row">
                    <div class="col-lg-12">
                        <input type="hidden" class="vouchno" value="<?php echo $getvouch; ?>">
                        <input type="hidden" class="series" value="<?php echo $bseries; ?>">
                        <input type="hidden" class="type" value="<?php echo $invoicingtype; ?>">
                        <div class="col-lg-2" style="padding-left:0px;">
                            <select class="form-control sfield">                         
                                <option value="1">Date</option>
                                <option value="2">P/R/D</option>
                                <option value="3">Delv.Date</option>
                                <option value="4">Party</option>                             
                                <option value="5">Nag</option>                             
                                <option value="6">Size(MM)</option>                             
                                <option value="7">T(MM)</option>                             
                                <option value="8">Make(MM)</option>                             
                                <option value="9">Dia</option>                             
                                <option value="10">A/P</option>                             
                                <option value="11">Wld</option>                             
                                <option value="12">STRT</option>                             
                                <option value="13">FTS</option>                             
                                <option value="14">T.FTS</option>                             
                                <option value="15">Weight</option>                             
                                <option value="16">Rate</option>                             
                                <option value="17">Amount</option>                             
                            </select>
                        </div>
                        <div class="col-lg-1">
                            <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">Date</option>
                                <option value="2">P/R/D</option>
                                <option value="3">Delv.Date</option>
                                <option value="4">Party</option>                             
                                <option value="5">Nag</option>                             
                                <option value="6">Size(MM)</option>                             
                                <option value="7">T(MM)</option>                             
                                <option value="8">Make(MM)</option>                             
                                <option value="9">Dia</option>                             
                                <option value="10">A/P</option>                             
                                <option value="11">Wld</option>                             
                                <option value="12">STRT</option>                             
                                <option value="13">FTS</option>                             
                                <option value="14">T.FTS</option>                             
                                <option value="15">Weight</option>                             
                                <option value="16">Rate</option>                             
                                <option value="17">Amount</option>    
                            </select> 
                        </div>
                        <div class="col-lg-1">
                            <select class="form-control orderAs">
                                <option value="desc">Desc</option>
                                <option value="asc">Asc</option>
                            </select>
                        </div>
                        <div class="col-lg-2" style="padding-left:0px;">
                            <select class="form-control sdelv">                         
                                <option value="1">All</option>
                                <option value="2">Not Deliver</option>
                                <option value="3">Deliver</option>                                
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <input type="text" value="" class="form-control ssearch" placeholder="Search Here" autofocus>
                        </div>
                        <div class="col-lg-1">
                            <input type="button" class="btn btn-primary sload" value="Search" >
                        </div>
                        <div class="col-lg-12 sloader text-center">
                        </div>
                        <div class="col-lg-12 snoRecord text-center">
                            <h3>NO RECORDS FOUND</h3>
                        </div>

                    </div>
                </div>

                <div class="col-lg-11 table responsive ssload" style="overflow-y: scroll;height: 480px;">
                    <table class="table table-striped  table-bordered" width="100%" style="font-size: 11px;padding:0px;">
                        <thead>

                            <tr class="mySearchTr" style="background-color: #77BCFF;">
                                <!--<th style="width:2%;">Voucher</th>-->
                                <th style="width:2%;">?</th>
                                <th style="width:7%;">Date</th>
                                <th style="width:3%;">P/R/D</th>
                                <th style="width:7%;">Delv.Date</th>
                                <th style="width:13%;">Party Name</th>
                                <th style="width:2%;">Nags</th>
                                <th style="width:4%;">Size(MM)</th>
                                <th style="width:4%;">T(MM)</th>
                                <th style="width:3%;">Make(MM)</th>
                                <th style="width:2%;">Dia</th>
                                <th style="width: 2%">A/P</th>
                                <th style="width: 3%">Wld</th>
                                <th style="width: 3%">STRT</th>
                                <th style="width: 3%">FTS</th>
                                <th style="width: 4%">T.FTS</th>
                                <th style="width: 4%">Weight</th>
                                <th style="text-align:right;width: 6%">Rate</th>
                                <th style="text-align:right;width: 6%">Amount</th>
                                <th style="width: 10%">Description</th>
                                <th style="width: 2%">C/R</th>
                                <th style="width: 6%">Cashvouch</th>
                                <th align="center" style="width:2%;">Action</th>
                                <th align="center" style="width:2%;">Label</th>
                            </tr>
                        </thead>
                        <tbody class="sTbody">

                        </tbody>  
                    </table>
                </div>
                <br>
                <div class="row">
                    <div class="col-xs-11 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>
        <div class="modal fade" id="sizemmod" role="dialog">
            <div class="modal-dialog modal-sm">
                <div class="modal-content">
                    <div class="modal-header">
                        <h4 class="modal-title">Size</h4>
                    </div>
                    <div class="modal-body">
                        <table class="table" width="100%">
                            <thead>
                                <tr>
                                    <td style="width: 50%;"><input type="text" class="form-control text-size sval" name="size" value=""></td>
                                    <td style="width: 50%;"><select class="form-control soval" name="stype" value="">                         
                                            <option value=""></option>
                                            <option value="1">OD</option>
                                            <option value="2">NB</option>
                                            <option value="3">MM</option>
                                        </select>
                                    </td>
                                </tr>
                            </thead>
                        </table>
                    </div>
                </div>
            </div>
        </div>
        <div class="modal fade" id="guagemmod" role="dialog">
            <div class="modal-dialog modal-sm">
                <div class="modal-content">
                    <div class="modal-header">
                        <h4 class="modal-title">T(MM)</h4>
                    </div>                
                    <div class="modal-body">
                        <table class="table" width="100%">
                            <thead>
                                <tr>
                                    <td style="width: 50%;"><input type="text" class="form-control text-size gval" name="guage"></td>
                                    <td style="width: 50%;">
                                        <select class="form-control goval" name="gtype">
                                            <option value=""></option>
                                            <option value="1">NR</option>
                                            <option value="2">SH</option>
                                            <option value="3">MM</option>
                                        </select></td></tr>
                            </thead>
                        </table>
                    </div>
                </div>      
            </div>
        </div>
        <div class="modal fade" id="makemmod" role="dialog">
            <div class="modal-dialog modal-sm">
                <div class="modal-content">
                    <div class="modal-header">
                        <h4 class="modal-title">Make</h4>
                    </div>                
                    <div class="modal-body">
                        <table class="table" width="100%">
                            <thead>
                                <tr>
                                    <td style="width: 50%;"><input type="text" class="form-control text-size mval" name="makesize" value=""></td>
                                    <td style="width: 50%;">
                                        <select class="form-control moval" name="mktype">                         
                                            <option value=""></option>
                                            <option value="1">OD</option>
                                            <option value="2">NB</option>
                                            <option value="3">MM</option>
                                        </select>
                                    </td>
                                </tr>
                            </thead>
                        </table>
                    </div>
                </div>                
            </div>
        </div>

        <div class="modal fade" id="getpartydata" role="dialog" width='120%'>
            <div class="modal-dialog modal-sm">
                <div class="modal-content">
                    <div class="modal-header">
                        <h4 class="modal-title">Party</h4>
                    </div>

                    <div class="modal-body">
                        <input type="text" name="srchparty" class="srchparty form-control"><br><br>
                        <div class="table-responsive PartySrchTbl" style="max-height: 500px;overflow: auto;">
                            <table width="100%" >
                                <tbody class="PTbody">
                                    <?php
                                    $getparty = mysqli_query($conn,"SELECT `accode`, `name` FROM `accmast` WHERE `accode`!='' $partycond order by name asc")or die(mysqli_error($conn));
                                    while ($fparty = mysqli_fetch_array($getparty)) {

                                        Echo "<tr>";
                                        Echo "<td><input type='text' name='disblsrchparty' class='form-control disblsrchparty' value='$fparty[name]' readonly></td>";
                                        Echo "</tr>";
                                        $ctr++;
                                    }
                                    ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>                
            </div>
        </div>
    </form>

    <div class="modal fade" id="prate" role="dialog">
        <div class="modal-dialog modal-lg" style="padding-top:20%;" >
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" style="font-size: 45px;color:red; opacity: 0">&times;</button>
                    <h4 class="modal-title">Previous Rates / Work Done</h4>

                </div>                
                <div class="modal-body">
                    <table class="table table-striped  table-bordered" width="100%" style="font-size: 11px;padding:0px;">
                        <thead>
                            <tr class="mySearchTr" style="background-color: #009552;">
                                <!--<th style="width:2%;">Voucher</th>-->
                                <th style="width:2%;">?</th>
                                <th style="width:7%;">Date</th>
                                <th style="width:3%;">P/R/D</th>
                                <th style="width:7%;">Delv.Date</th>
                                <th style="width:13%;">Party Name</th>
                                <th style="width:2%;"class='text-right'>Nags</th>
                                <th style="width:4%;" class='text-right'>Size(MM)</th>
                                <th style="width:4%;" class='text-right'>T(MM)</th>
                                <th style="width:3%;" class='text-right'>Make(MM)</th>
                                <th style="width:2%;">Dia</th>
                                <th style="width: 2%">A/P</th>
                                <th style="width: 3%">Wld</th>
                                <th style="width: 3%">STRT</th>
                                <th style="width: 3%" class='text-right'>FTS</th>
                                <th style="width: 4%" class='text-right'>T.FTS</th>
                                <th style="width: 4%" class='text-right'>Weight</th>
                                <th style="text-align:right;width: 6%" class='text-right'>Rate</th>
                                <th style="text-align:right;width: 6%" class='text-right'>Amount</th>
                                <th style="width: 10%">Description</th>
                                <th style="width: 2%">C/R</th>
    <!--                                <th style="width: 6%">Cashvouch</th>-->
                                <!--<th align="center"  style="width:2%;">Action</th>-->
                            </tr>
                        </thead>
                        <tbody class="prTbody">
                        </tbody>
                    </table>
                </div>                
            </div>
        </div>
    </div>
    <form action="chal_print_fr2.php" method="POST">
    
                <div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true">
                    <div class="modal-dialog modal-dialog-centered" role="document">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h5 class="modal-title" id="exampleModalLongTitle">LIST</h5>
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                    <span aria-hidden="true">&times;</span>
                                </button>
                            </div>
                            <div class="modal-body">
                                <div class="row">
                                    <div class="col-lg-1 col-md-3">

                                    </div>

                                    <div class="row">        
                                        <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                            <label class="label-control marg">Party Name</label>
                                        </div>
                                        <div class="col-lg-6" >
                                            <select class="form-control select2 partymod" name="partymod" style="width:100%">
                                                <option value=""></option>
                                                <?php
                                                $getexp = mysqli_query($conn,"SELECT `accode`, `name` FROM `accmast` where `grpcode`='010' ORDER BY `name` asc") or die(mysqli_error($conn));
                                                while ($fetexp = mysqli_fetch_assoc($getexp)) {
                                                    Echo "<option value='$fetexp[accode]'>$fetexp[name]</option>";
                                                }
                                                ?>
                                            </select>
                                        </div>
                                    </div> 
                                </div>
                                <div class="row">
                                    <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                        <label class="label-control marg ">Size</label>
                                    </div>
                                    <div class="col-lg-2" >
                                        <input type="text" class="qsval bcolor form-control " name="mainsize" >
                                    </div>
                                    <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 " style="margin-left: -29px;">
                                        <select name="sizetype" class="querysizeval form-control " style="width: 48px; ">
                                            <option value=""></option>
                                            <option value="1">OD</option>
                                            <option value="2">NB</option>
                                            <option value="3">NM</option>
                                        </select>
                                    </div>
                                    <div class="col-lg-2" >
                                        <input type="text" class="qsoval bcolor form-control " style="margin-left: -12px;" name="mm" >
                                    </div>
                                    <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-left pad mmsize" name="mmsize" style="margin-left: -28px;">
                                        <label class="label-control marg">MM</label>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                        <label class="label-control marg">Guage</label>
                                    </div>
                                    <div class="col-lg-2" >
                                        <input type="text" class="qgval bcolor form-control size" name="mainguage" >
                                    </div>
                                    <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 " style="margin-left: -29px;">
                                        <select name="guagetype" class="queryggval form-control" change=""  style="width: 48px; ">
                                            <option value=""></option>
                                            <option value="1">NR</option>
                                            <option value="2">SH</option>
                                            <option value="3">NM</option>
                                        </select>
                                    </div>
                                    <div class="col-lg-2" >
                                        <input type="text" class="qgoval bcolor form-control guagemm" name="mmguage" style="margin-left: -12px;">
                                    </div>
                                    <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-left pad"style="margin-left: -25px;">
                                        <label class="label-control marg">MM</label>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad" >
                                        <label class="label-control marg">Make Size</label>
                                    </div>
                                    <div class="col-lg-2" >
                                        <input type="text" class="qmval bcolor form-control size" name="mainmksize" >
                                    </div>
                                    <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 " style="margin-left: -29px;">
                                        <select name="maketype" class="querymakeval form-control " name="makedrop" style="width: 48px; ">
                                            <option value=""></option>
                                            <option value="1">OD</option>
                                            <option value="2">NB</option>
                                            <option value="3">NM</option>
                                        </select>
                                    </div>
                                    <div class="col-lg-2" >
                                        <input type="text" class="qmoval bcolor form-control size" style="margin-left: -12px;" name="makesizemm" >
                                    </div>
                                    <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-left pad" name="makesizemm" style="margin-left: -28px;">
                                        <label class="label-control marg">MM</label>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                        <label class="label-control marg">Fts</label>
                                    </div>   
                                    <div class="col-lg-2" >
                                        <input type="text" class="bcolor form-control " name="mainfts" value="0.00" style="text-align: right;" >
                                    </div>             
                                </div>
                            </div>
                            <div class="modal-footer">
                                <div class="row">
                                    <input type="submit" name="chalmodalprint" value="VIEW" style="margin-left: 13%; width: 12%; background-color:#34a853;"  class="btn" >
                                        <!--<span class="" style="color:#ffffff; font-weight: bold;" data-toggle='tooltip' title='Print'>VIEW</span>-->   
                                    <a type="button" style=" width: 12%; margin-right: 45%;  background-color:#DC143C;" data-dismiss="modal" onclick="window.close()" class="btn"  id="print" target="_blank">
                                        <span class="" style="color:#ffffff; font-weight: bold;" data-toggle='tooltip' title='Print'>Exit</span>   
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
    
    </form>
    <script type="text/javascript">
<?php if ($addflag == 0) { ?>
            //            $(".ndata").show();
            //            $(".tohide").hide();
            //            //            $("#submit").show();
            //            document.getElementById("submit").style.display = "block";
            //            document.getElementById("submit").style.visibility = "visible";
            $(".ndata").show();
            $("#submit").show();
            $(".mark").focus();
            $(".tohide").hide();
<?php } ?>
    </script>
    <?php include("includes/linksfoot.php"); ?>

</body>
</html>
