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

$formname = 'JOB GIVEN';
$maindb = 'jobgiven';
$trandb = 'jobgivetran';
$stkdb  = 'jobsacn';

$printfile = "jobgiven_fr.php"; //print not done
$file = 'jobgiven.php';
//$trandbexist = 1;
$keyfld = "vouchno";
$source = "JOBG" ;

$fseries = $_SESSION["series_" . $dbname];
$keycond = " and `series`='$fseries'";

//browse($keycond);
$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
$myyear = $_SESSION[$myyear];
$compcd = $_SESSION["comp_" . $dbname];
//$usertype = $_SESSION["usertype_" . $dbname];
$username = $_SESSION["username_" . $dbname];

$zroot = $_SESSION["zdir"];
$zroot1 = substr($zroot, 0, -4);

$currcono = $compcd;
$curryear = $myyear;

//for adding entry and edit timings
$adduser = $_SESSION['username_' . $dbname];
date_default_timezone_set("Asia/Kolkata");
$adddate = date("Y-m-d");
$addtime = date("h:i:sa");
$enttime = date("h:i:sa");

$usertype = $_SESSION["usertype_" . $dbname];
//end

$zyear = right($zdb, 4);
$zStartDate = getzdates($zyear, 'S');
$zEndDate = getzdates($zyear, 'E');

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

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

    $qvouch = $_POST["vouchno"];
    $vouchno = $_POST["vouchno"];

    $vouchdt = $_POST["vouchdt"];
    $saleorder = $_POST["saleorder"];
    $saleorddate = $_POST["saleorddate"];
    $party = $_POST["party"];
    $gvtime = $_POST["gvtime"];
    $prnrate = $_POST["prnrate"];

    $prodcode = $_POST["prodcode"];
    $barcode = $prodcode ;
    
    $colorname = $_POST["colorname"];
    $process = $_POST["process"];
    $karigar = $_POST["karigar"];
    $expectdate = $_POST["expectdate"];
    $compltdate = $_POST["compltdate"];
    $mqty = $_POST["mqty"];
    
    $rate = $_POST["rate"];
    $billamt = $_POST["billamt"];
    $remark1 = $_POST["remark1"];
    $remark2 = $_POST["remark2"];
    $totmtrs = $_POST['totmtrs'];
    $totyard = $_POST['totyard'];
    $totqty = $_POST['totqty'];
    $count = $_POST["items"];

    $mvouch = $qvouch;

    if ($a == 1) {
        
        mysqli_query($conn, "LOCK TABLES `$maindb` WRITE,  $trandb WRITE, $stkdb  WRITE, `rawmat` WRITE, accmast WRITE , rawsacn WRITE ,color WRITE ,item WRITE ,process WRITE") or die(mysqli_error($conn));

        if ($addflag == 1) {
            $gen1 = mysqli_query($conn, "SELECT MAX(vouchno+0) AS maxcode FROM `$maindb` where vouchno != '' $keycond LIMIT 0,1 ") or die(mysqli_error($conn));
            if (mysqli_num_rows($gen1) > 0) {
                $vvouch = mysqli_fetch_assoc($gen1);
                $qvouch = $vvouch["maxcode"] + 1;
            } else {
                $qvouch = '1';
            }
            $mvouch = $qvouch;
        }
        
        mysqli_query($conn, "DELETE FROM `$trandb` WHERE `$keyfld`='$mvouch' $keycond") or die(mysqli_error($conn));
        mysqli_query($conn, "DELETE FROM `rawsacn` WHERE `$keyfld`='$mvouch' and `source` = '$source'  $keycond") or die(mysqli_error($conn));
        for ($i = 0; $i <= $count; $i++) {
            $msrno = $_POST["srno_$i"];
            $mitem = $_POST["item_$i"];
            $color = $_POST["color_$i"];
            $yard = $_POST["yard_$i"];
            $meters = $_POST["meters_$i"];
            $qty = $_POST["qty_$i"];
            $t_rate = 0 ; 
            $t_amount = 0 ; 

            $stkqty = $qty * -1;
            $stkmeters = $meters * -1;
            $stkyard = $yard * -1;

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

                mysqli_query($conn, "INSERT INTO `$trandb`(`series`,`vouchno`, `vouchdt`, `srno`, `item`, `qty`, `color`,`meters`,`yard`) 
                    VALUES ('$fseries','$qvouch', '$vouchdt', '$srl',  '$mitem', '$qty', '$color','$meters','$yard')") or die(mysqli_error($conn));

                ###########Stock Part############
                
                mysqli_query($conn, "INSERT INTO `rawsacn` (`vouchno`, `compcode`, `yearno`, `vouchdt`, `karigar`, `srno`, `barcode`, `itemcode`, `qty`, yard, meters, 
                                            `source`,   `rate`, `amount`, color, series) 
                                     VALUES ('$qvouch', '$compcd', '$myyear', '$vouchdt', '$karigar', '$srl', '$mitem', '$mitem', '$stkqty', '$stkyard', '$stkmeters', 
                                             '$source', '$t_rate', '$t_amount', '$colorname','$fseries')") or die(mysqli_error($conn));
            }
        }

        mysqli_query($conn, "DELETE FROM `$stkdb` WHERE `$keyfld`='$mvouch' and `source` = '$source' $keycond") or die(mysqli_error($conn));
        
        mysqli_query($conn, "INSERT INTO `$stkdb` (`vouchno`, `compcode`, `yearno`, `vouchdt`, `karigar`, `srno`, `barcode`, `itemcode`, `qty`, `source`,
                                                  `process`, `rate`, `amount`, color, series, ordno) 
                             VALUES ('$qvouch', '$compcd', '$myyear', '$vouchdt', '$karigar', 1, '$barcode', '$barcode', '$mqty',  '$source',
                                                 '$process', '$rate', '$billamt', '$colorname','$fseries', '$saleorder') ") or die(mysqli_error($conn));
        
        ############################## MAIN ######################################################
        
        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(`vouchno`, `series`,`vouchdt`, `saleorder`,`saleorddate`, `party`, `prodcode`, `colorname`, `process`, `karigar`, `gvtime`,
                                                     `prnrate` ,`expectdate` , `compltdate`,  `qty` , `rate` , `billamt` , `remark1` , `remark2`, `totmtrs`, `totyard`, `totqty` , `adduser`, `adddate`, `addtime`) 
                        VALUES ($mvouch, '$fseries','$vouchdt', '$saleorder','$saleorddate', '$party' ,'$prodcode', '$colorname', '$process' ,'$karigar', '$gvtime',
                            '$prnrate','$expectdate', '$compltdate', '$mqty' , '$rate', '$billamt' , '$remark1' , '$remark2', '$totmtrs', '$totyard', '$totqty','$adduser','$adddate','$addtime')") or die(mysqli_error($conn));


            $mvouch = $qvouch; //1
        } else {
            $mvouch = $qvouch; //1
            $editdate = date("Y-m-d");
            $edittime = date("h:i:sa");

            mysqli_query($conn, "UPDATE `$maindb` SET `vouchdt` = '$vouchdt',`saleorder` = '$saleorder', `saleorddate`='$saleorddate',`party`='$party', `prnrate`='$prnrate', `gvtime` = '$gvtime', `prodcode`='$prodcode', colorname='$colorname', `process`='$process',
                            `karigar` = '$karigar', `expectdate` = '$expectdate', `compltdate` = '$compltdate',`qty` = '$mqty', `rate` = '$rate',
                            `billamt` = '$billamt', `remark1` = '$remark1', `remark2` = '$remark2', `totmtrs` = '$totmtrs', `totyard` = '$totyard', `totqty` = '$totqty' , 
                            `edituser`='$adduser',`edittime`='$edittime',`editdate`='$editdate' WHERE `vouchno`='$qvouch' $keycond") or die(mysqli_error($conn));

            updmess();
        }
        if ($addflag == 1) {
            header("refresh: 1; $file?mode=edit&$keyfld=$mvouch&form=$form&series=$fseries");
        } else {
            header("refresh: 1; $file?mode=edit&$keyfld=$mvouch&form=$form&series=$fseries");
        }
    }
}

if ($addflag != 1) {

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

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

    $fcolor = mysqli_query($conn, "SELECT `code`,`name` FROM `color` WHERE `code`='$get[colorname]'") or die(mysqli_error($conn));
    $lcolor = mysqli_fetch_array($fcolor);

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

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

    $fitem = mysqli_query($conn, "SELECT `code`,`name` FROM `item` WHERE `code`='$get[prodcode]'") or die(mysqli_error($conn));
    $litem = mysqli_fetch_array($fitem);

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

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

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

    $delcondn = " vouchno = '$_GET[vouchno]' $keycond";
    $delcond = " source = '$source' and vouchno = '$_GET[vouchno]' $keycond";
    $delcond2 = " source = '$source' and vouchno = '$_GET[vouchno]' and `compcode` = '$compcd' $keycond";
    $delcond1 = " source = 'CR' and vouchno = '$delcsh' $keycond";
    $delcond3 = " source = 'BR' and vouchno = '$delbnk' $keycond";

    delnew($zdb, "$maindb", "$delcondn", "$file?mode=last&series=$fseries");
    delnew($zdb, "$trandb", "$delcondn", "$file?mode=last&series=$fseries");
        delnew($zdb, "$stkdb", "$delcondn", "$file?mode=last&series=$fseries");

}


include("includes/linkshead.php");
?>
<style type="text/css">

</style>
<!--<script src="MyScript/shortkeys.js" type="text/javascript"></script>-->
<script src="MyScript/jobgvn.js?A=2" type="text/javascript"></script>
<script src="MyScript/jobgvn_search.js?" type="text/javascript"></script>
<script src="MyScript/checkdate.js" type="text/javascript"></script>

</head>
<body >
    <form action='' method='POST'>

        <div class="row">
            <div class="col-lg-6">
                <div class="panel panel-default">
                <div class="panel-heading">
                        <?php include("includes/usermention.php");
                        ?>
                        <div class="actions pull-right">
                            <i class="fa fa-chevron-down"></i>
                            <i class="fa fa-times"></i>
                        </div>
                    </div>

                    <div class="panel-body">
                        <div class="row">
                            <div class="col-lg-2 col-md-1 col-sm-3 col-xs-2 text-right pad">
                                <label class="label-control marg">Voucher</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-8 col-xs-1 pad">
                                <input type="text" class="form-control form vouchno" name="vouchno" value="<?php Echo ($addflag != 1) ? $get["vouchno"] : "$qvouch"; ?>" readonly required>
                                <!--<input type="hidden" class="form-control form" name="cashvouch" value="<?php Echo ($addflag != 1) ? $get["cashvouch"] : ""; ?>" readonly>-->
                                <!--<input type="hidden" class="form-control form" name="bankvouch" value="<?php Echo ($addflag != 1) ? $get["bankvouch"] : ""; ?>" readonly>-->
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-1 text-right pad">
                                <label class="label-control marg">Date</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-4 pad">
                                <input type="date" class="form-control form vouchdt" name="vouchdt" value="<?php Echo ($addflag != 1) ? $get["vouchdt"] : date("Y-m-d"); ?>">
                                <input type="hidden" value="<?= $zStartDate ?>" class="zstdate">
                                <input type="hidden" value="<?= $zEndDate ?>" class="zenddate">

                            </div>
                            
                        </div>
                        <div class="row">

                        <div class="col-lg-2 col-md-3 col-sm-3 col-xs-1 text-right pad">
                                <label class="label-control marg">Sale Order</label>
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-2 pad">
                                <input type="text" class="form-control form saleorder" name="saleorder" value="<?php Echo ($addflag != 1) ? $get["saleorder"] : ""; ?>">
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-1 text-right pad">
                                <label class="label-control marg">Time</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-2 pad">
                                <input type="time" class="form-control form gvtime" name="gvtime" value="<?php Echo ($addflag != 1) ? $get["gvtime"] : ""; ?>">
                            </div>
                        </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">Qty.</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" name="mqty" class="form-control form text-right qty"  value="<?php echo ($addflag != 1) ? $get['qty'] : ""; ?> " >
                            </div>
                        </div>
                        
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-3 text-right pad">
                                <label class="label-control marg">Customer</label>
                            </div>
                            <div class="col-lg-5 col-md-3 col-sm-3 col-xs-7 pad">
                                <select class="form-control form select2 party" name="party" style="width:100%">
                                    <option value="<?php echo ($addflag != 1) ? $lparty['accode'] : ""; ?>"><? echo ($addflag != 1) ? $lparty['name'] : ""; ?></option>
                                    <?php
                                    $mparty = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast` where grpcode = '010' ")or die(mysqli_error($conn));
                                    while ($ftparty = mysqli_fetch_array($mparty)) {
                                        Echo "<option value='$ftparty[accode]'>$ftparty[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </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" style="color:red">Sale Order Date</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-4 pad">
                                <input type="date" class="form-control form saleorddate" name="saleorddate" value="<?php Echo ($addflag != 1) ? $get["saleorddate"] : ""; ?>">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 numeric pad">
                                <label class="label-control marg">Product</label>
                            </div>
                            <div class="col-lg-5 col-md-6 col-sm-6 col-xs-6 pad">
                                <select class="form-control form select2 prodcode" name="prodcode" style="width:100%">
                                    <option value="<?php echo ($addflag != 1) ? $litem['code'] : ""; ?>"><? echo ($addflag != 1) ? $litem['name'] : ""; ?></option>
                                    <?php
                                    $gitemgrp = mysqli_query($conn, "SELECT code,name FROM `item`")or die(mysqli_error($conn));
                                    while ($itemgrp = mysqli_fetch_array($gitemgrp)) {
                                        Echo "<option value='$itemgrp[code]'>$itemgrp[code] || $itemgrp[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>

                        </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">Color</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <select class="form-control form select2 colorname" name="colorname" style="width:100%">
                                    <option value="<?php echo ($addflag != 1) ? $lcolor['code'] : ""; ?>"><? echo ($addflag != 1) ? $lcolor['name'] : ""; ?></option>
                                    <?php
                                    $color = mysqli_query($conn, "SELECT code,name FROM `color`")or die(mysqli_error($conn));
                                    while ($ftcolor = mysqli_fetch_array($color)) {
                                        Echo "<option value='$ftcolor[code]'>$ftcolor[name]</option>";
                                    }
                                    ?>
                                </select>
                                <!--<input type="text" name="colorname" class="form-control form colorname"  value="<?php echo ($addflag != 1) ? $get['colorname'] : ""; ?> " />-->
                            </div>
                        </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">Process</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <select class="form-control form select2 process" name="process" style="width:100%">
                                    <option value="<?php echo ($addflag != 1) ? $lprocess['code'] : ""; ?>"><? echo ($addflag != 1) ? $lprocess['name'] : ""; ?></option>
                                    <?php
                                    $mprocess = mysqli_query($conn, "SELECT `code`,`name` FROM `process` ")or die(mysqli_error($conn));
                                    while ($ftprocess = mysqli_fetch_array($mprocess)) {
                                        Echo "<option value='$ftprocess[code]'>$ftprocess[name]</option>";
                                    }
                                    ?>
                                </select>
                                <!--<input type="text" name="process" class="form-control form process"  value="<?php echo ($addflag != 1) ? $get['process'] : ""; ?> " >-->
                            </div>
                        </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">Labour</label>
                            </div>
                            <div class="col-lg-5 col-md-3 col-sm-3 col-xs-3 pad">
                                <select class="form-control form select2 karigar" name="karigar" style="width:100%">
                                    <option value="<?php echo ($addflag != 1) ? $lkarigar['accode'] : ""; ?>"><? echo ($addflag != 1) ? $lkarigar['name'] : ""; ?></option>
                                    <?php
                                    $mkarigar = mysqli_query($conn, "SELECT `accode`,`name` FROM `accmast`  WHERE `grpcode`='039' ORDER by name" )or die(mysqli_error($conn));
                                    while ($ftkarigar = mysqli_fetch_array($mkarigar)) {
                                        Echo "<option value='$ftkarigar[accode]'>$ftkarigar[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </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" style="color:red">Expected Date</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-4 pad">
                                <input type="date" class="form-control form expectdate" name="expectdate" value="<?php Echo ($addflag != 1) ? $get["expectdate"] : ""; ?>">
                            </div>
                        </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" >Completed Date</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-4 pad">
                                <input type="date" class="form-control form compltdate" name="compltdate" value="<?php Echo ($addflag != 1) ? $get["compltdate"] : ""; ?>">
                            </div>
                        </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">Rate</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" name="rate" class="form-control form text-right rate"  value="<?php echo ($addflag != 1) ? $get['rate'] : ""; ?> " >
                            </div>
                        </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">Amount</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" name="billamt" class="form-control form text-right billamt"  value="<?php echo ($addflag != 1) ? $get['billamt'] : ""; ?> " readonly>
                            </div>
                        </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">Remarks</label>
                            </div>
                            <div class="col-lg-6 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" name="remark1" class="form-control form remark1"  value="<?php echo ($addflag != 1) ? $get['remark1'] : ""; ?> ">
                            </div>
                        </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"></label>
                            </div>
                            <div class="col-lg-6 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" name="remark2" class="form-control form remark2"  value="<?php echo ($addflag != 1) ? $get['remark2'] : ""; ?> " >
                            </div>
                        </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">Print Rate</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                            <select id="prnrate" name="prnrate" class="form-control form select2 prnrate" required>
                            <option <?php if ($get['prnrate'] == '1') { echo "selected='selected'";} ?> value="1">YES</option>
                            <option <?php if ($get['prnrate'] == '2') { echo "selected='selected'";} ?> value="2">NO</option>
                            </select>

                            </div>
                        </div>
            </div>
                </div>
              <div class="panel-footer">
                        <?php include("includes/buttons.php"); ?>
                    </div>
                </div>
                <div class="col-lg-6 col-md-12 col-sm-12 col-xs-12 ">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title">Raw Material</h3>
                    </div>
                    <div class="panel-body">
                        <div class='table-responsive' style="overflow-y:scroll; height: 300px">
                            <table class="table table-striped table-condensed table-bordered" width="100%">
                                <thead  class="mySearchTr">
                                    <tr>
                                        <th style='width: 1%'></th>
                                        <th style='width: 3%' class='text-right'>Sr.</th>
                                        <th style='width: 13%'>Material</th>
                                        <th style='width: 10%'>Color</th>
                                        <th style='width: 4%' class='text-right'>Meters</th>
                                        <th style='width: 4%' class='text-right'>Yard</th>
                                        <th style='width: 4%' class='text-right'>Qty.</th>
                                    </tr>
                                    <!-- <tr>
                                    <th width="2%"></th>
                                    <th width="2%">Sr No.</th>
                                    <th width="45%">Material.</th>
                                    <th width="10%" class='text-right'>Pcs/Mtr</th>
                                    <th width="12%" class='text-right'>Qty</th>
                                    <th width="10%"class='text-right'>Unit</th>
                                    <th width="15%"class='text-right'>Rate</th>
                                    <th width="15%"class='text-right'>Amount</th>

                                </tr> -->
                                </thead>
                                <tbody id="tabledata">
                                    <?php
                                    if ($addflag != 1) {
                                        $ctr = 0;
                                        $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$get[vouchno]' $keycond ORDER BY `srno`")or die(mysqli_error($conn));
                                        while ($tran = mysqli_fetch_array($gettran)) {
                                            $finditem = mysqli_query($conn, "SELECT `code`,`name` FROM `rawmat` WHERE `code`='$tran[item]'") or die(mysqli_error($conn));
                                            $fitem = mysqli_fetch_array($finditem);

                                            $findcolor = mysqli_query($conn, "SELECT `code`,`name` FROM `color` WHERE `code`='$tran[color]'") or die(mysqli_error($conn));
                                            $fcolor = mysqli_fetch_array($findcolor);

                                            Echo "<tr>";
                                            Echo "<td><i class='fa fa-close remove' style='display:none;'></i></td>";
                                            Echo "<td><input type='text' class='form-control form text-right srno' name='srno_$ctr' value='$tran[srno]'></td>";
                                            Echo "<td>";
                                            Echo "<select class='form-control form item select2' name='item_$ctr' style='width:100%'>";
                                            Echo "<option value='$fitem[code]'>$fitem[name]</option>";
                                            $gitem = mysqli_query($conn, "SELECT `code`,`name` FROM `rawmat` order by name")or die(mysqli_error($conn));
                                            while ($item = mysqli_fetch_array($gitem)) {
                                                Echo "<option value='$item[code]'>$item[name]</option>";
                                            }
                                            Echo "</select>";
                                            Echo "</td>";
                                            Echo "<td>";
                                            Echo "<select class='form-control form color select2' name='color_$ctr' style='width:100%'>";
                                            Echo "<option value='$fcolor[code]'>$fcolor[name]</option>";
                                            $gcolor = mysqli_query($conn, 'SELECT `code`,`name` FROM `color` order by name')or die(mysqli_error($conn));
                                            while ($color = mysqli_fetch_array($gcolor)) {
                                                Echo "<option value='$color[code]'>$color[name]</option>";
                                            }
                                            Echo "</select>";
                                            echo "</td>";
                                            Echo "<td><input type='text' class='form-control form text-right meters' name='meters_$ctr'  value='$tran[meters]'></td>";
                                            Echo "<td><input type='text' class='form-control form text-right yard'   name='yard_$ctr'    value='$tran[yard]'></td>";
                                            Echo "<td><input type='text' class='form-control form text-right tr_qty'    name='qty_$ctr'     value='$tran[qty]'></td>";
                                            Echo "</tr>";
                                            $ctr++;
                                        }
                                    } else {
                                        $pctr = 0;
                                        ?>
                                        <tr>
                                            <td><i class="fa fa-close remove"></i></td>
                                            <td><input type="text" class="form-control form text-right srno" name="srno_0" value="1">
                                            <td>
                                                <select class="form-control form item select2" name="item_0" style='width:100%'>
                                                    <option value=""></option>
                                                    <?php
                                                    $gitem = mysqli_query($conn, "SELECT * FROM `rawmat`")or die(mysqli_error($conn));
                                                    while ($item = mysqli_fetch_array($gitem)) {
                                                        Echo "<option value='$item[code]'>$item[name]</option>";
                                                    }
                                                    ?>
                                                </select>
                                            </td>
                                            <td>
                                                <select class="form-control form color select2" name="color_0" style='width:100%' >
                                                    <option value=""></option>
                                                    <?php
                                                    $gcolor = mysqli_query($conn, "SELECT `code`,`name` FROM `color` order by name")or die(mysqli_error($conn));
                                                    while ($color = mysqli_fetch_array($gcolor)) {
                                                        Echo "<option value='$color[code]'>$color[name]</option>";
                                                    }
                                                    ?>
                                                </select>
                                            </td>
                                            <td><input type="text" class="form-control form text-right meters"      name="meters_0"         value="0"></td>
                                            <td><input type="text" class="form-control form text-right yard"      name="yard_0"         value="0"></td>
                                            <td><input type="text" class="form-control form text-right tr_qty"      name="qty_0"         value="0"></td>
                                        </tr>  
                                        <?
                                        $pctr++;
                                    }
                                    ?>
                            <input type="hidden" id="items" name="items" value="<?php echo ($addflag != 1) ? $ctr : $pctr; ?>" />  

                                </tbody>
                            </table>
                        <!--</div>-->
                        
                        
                        </div>
                        <div class="row" style="padding-left:18px">
                            <button type="button" class="btn btn-success btn-square form"  id="addnew">
                                <i class="fa fa-plus" style="color:#ffffff" data-toggle='tooltip' title='Add Row'></i>    
                            </button>
                        </div>
                        
                        <div class="col-lg-12 col-xs-12 pad" style="padding-left:00px" >
                                    <div class="col-lg-8 text-right pad">
                                    <label class="label-control marg" style='color:#337AB7'>Total</label>
                                    </div>
                                    <div class="col-lg-1 pad">
                                        <input type="text" name="totmtrs" class="form-control form text-right totmtrs"  value="<?php echo ($addflag != 1) ? $get['totmtrs'] : "0.00"; ?>" readonly>
                                    </div>
                                    <div class="col-lg-1 pad">
                                        <input type="text" name="totyard" class="form-control form text-right totyard"  value="<?php echo ($addflag != 1) ? $get['totyard'] : "0.00"; ?>" readonly>
                                    </div>
                                    <div class="col-lg-1 pad">
                                        <input type="text" name="totqty" class="form-control form text-right totqty"  value="<?php echo ($addflag != 1) ? $get['totqty'] : "0.00"; ?>" readonly>
                                    </div>
                        </div>
                    </div>
                </div>
            </div>
       

                  
            
        </div>

    </form>


    <div class="modal fade bs-example-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 sfield">
                                <option value="7">Voucher</option>
                                <option value="1">Date</option>
                                <option value="2">Product</option>
                                <option value="3">Color</option>
                                <option value="4">Process</option>
                                <option value="5">Labour</option>
                                <option value="6">Qty</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="7">Voucher</option>
                                <option value="1">Date</option>
                                <option value="2">Product</option>
                                <option value="3">Color</option>
                                <option value="4">Process</option>
                                <option value="5">Labour</option>
                                <option value="6">Qty</option>
                            </select> 
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control orderAs">
                                <option value="desc">Desc</option>
                                <option value="asc">Asc</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <input type="text" value="" class="form-control ssearch" placeholder="Search Here">
                        </div>
                        <div class="col-lg-2">
                            <input type="button" class="btn btn-primary sload" value="Search" >
                        </div>
                    </div>
                    <div class="col-lg-12 sloader text-center">
                        <!--<img src="images/search.gif" alt="loading">-->       
                    </div>
                    <div class="col-lg-12 snoRecord text-center">
                        <h3>NO RECORDS FOUND</h3>
                    </div>
                    <div class="table-responsive" style="max-height: 400px;overflow: auto;">
                        <table id="" class="table table-striped table-condensed table-bordered" width="100%">
                            <thead>
                                <tr  class="mySearchTr">
                                    <th width="5%">Voucher</th>
                                    <th width="10%">Date</th>
                                    <th width="20%">Product</th>
                                    <th width="20%">Color</th>
                                    <th width="20%">Process</th>
                                    <th width="20%">Labour</th>
                                    <th width="10%">Qty</th>
                                    <th width="1%">Action</th>
                                </tr>
                            </thead>
                            <tbody class="sTbody">
                            </tbody>
                        </table>
                    </div>
                    <div class="row">
                        <div class="col-xs-12 text-right search-action-bar">
                            <input type="button"  class="btn btn-primary sprev" value="Previous">
                            <input type="button"  class="btn btn-primary snext" value="Next">
                        </div>
                    </div>
                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>
    <?php include("includes/linksfoot.php"); ?>
</body>
</html>
