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

$formname = 'Finished Product';
$maindb = 'finprod';
$trandb = 'finrelprod';
$trandb3 = 'finprodraw';
$trandb4 = 'finprodpack';

$file = 'finprod.php';
$grid = '';
$keyfld = "code";
$printfile = 'articlelst.php';
$trandbexist = 0;
$focusfld = "name";

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

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

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

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

    $a = 1;
    $name = trim($_POST["name"]);
    $rprodcode = trim($_POST["rprodcode"]);
    $gram = trim($_POST["gram"]);
    $approxkg = trim($_POST["approxkg"]);
    $phytest = trim($_POST["phytest"]);
    $totpercqty = trim($_POST["totpercqty"]);


    $count = $_POST["items"];
    $count3 = $_POST["items3"];
    $count4 = $_POST["items4"];
    $mvouch = $qvouch;

    if (empty($name)) {
        browse("Name Cannot Be Empty");
        $a = 0;
    }

    if (isset($_POST["name"])) {
        $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE  `name` = '$name' and `code` != '$qvouch'") or die(mysqli_error($conn));
        if (mysqli_num_rows($result) > 0) {
            $a = 0;
            browse("Name Already Exist");
            header("refresh: 1; $file?code=$qvouch&mode=last");
        }
    }

    if ($a == 1) {
        mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `$trandb3` WRITE, `$trandb4` WRITE, `$trandb` WRITE");

        mysqli_query($conn, "DELETE FROM `$trandb` WHERE `code`='$qvouch'") or die(mysqli_error($conn));
        $qtr2 = 0;
        for ($i = 0; $i <= $count; $i++) {
            $qtr2++;
            $rlsrno = $_POST["srno_$i"];
            $prodcode = $_POST["prodcode_$i"];
            $prodname = $_POST["prodname_$i"];
            $netwt = $_POST["netwt_$i"];
            $tapproxkg = $_POST["approxkg_$i"];
            $approxltr = $_POST["approxltr_$i"];

            if (!empty($prodcode)) {
                mysqli_query($conn, "INSERT INTO `$trandb`(`code`, `srno`, `prodcode`, `prodname`, `netwt`, `approxkg`, `approxltr`)
                                       VALUES('$qvouch',  '$qtr2', '$prodcode', '$prodname', '$netwt', '$tapproxkg', '$approxltr')") or die(mysqli_error($conn));
            }
        }

        mysqli_query($conn, "DELETE FROM `$trandb3` WHERE `code`='$qvouch'") or die(mysqli_error($conn));
        $qtr = 0;
        for ($k = 0; $k < $count3; $k++) {
            $qtr++;
            $rsrno = $_POST["srno3_$k"];
            $rawmat = $_POST["rawmat_$k"];
            $runit = $_POST["runit_$k"];
            $rqty = $_POST["rqty_$k"];
            $rcost = $_POST["rcost_$k"];
            $ramount = $_POST["ramount_$k"];

            if (!empty($rawmat)) {
                mysqli_query($conn, "INSERT INTO `$trandb3`(`code`, `srno`, `rawmat`, `unit`, `qtyperc`, `cost`, `amount`,`prodcode`)
                                       VALUES('$qvouch',  '$qtr', '$rawmat', '$runit', '$rqty', '$rcost', '$ramount', '$prodcode')") or die(mysqli_error($conn));
            }
        }

//        mysqli_query($conn, "DELETE FROM `$trandb4` WHERE `code`='$qvouch'") or die(mysqli_error($conn));
//        $qtr1 = 0;
//        for ($m = 0; $m < $count4; $m++) {
//            $qtr1++;
//            $psrno = $_POST["srno4"][$m];
//            $packing = $_POST["packing"][$m];
//            $punit = $_POST["punit"][$m];
//            $pqty = $_POST["pqty"][$m];
//            $pcost = $_POST["pcost"][$m];
//            $pamount = $_POST["pamount"][$m];
//            $prelprod = $_POST["relprod"][$m];
//
//            $chkexist = mysqli_query($conn, "SELECT qty FROM `$trandb4` WHERE `code`='$qvouch' and `relprod`='$prelprod' and `qty` < $pqty") or die(mysqli_error($conn));
//            $num = mysqli_num_rows($chkexist);
//
//            if ($num > 0) {
//                mysqli_query($conn, "UPDATE `$trandb4` SET  `qty`='$finprodcode', `name`='$name', `gram`='$gram', `rprodcode`='$rprodcode', `approxkg`='$approxkg',
//                       `phytest`='$phytest',`totpercqty`='$totpercqty' WHERE code = '$mvouch' ") or die(mysqli_error($conn));
//            } else {
//                if (!empty($packing) && !empty($prelprod)) {
//                    mysqli_query($conn, "INSERT INTO `$trandb4`(`code`, `srno`, `packing`, `unit`, `qty`, `cost`, `amount`, `relprod`)
//                                       VALUES('$qvouch', '$qtr1', '$packing', '$punit', '$pqty', '$pcost', '$pamount','$prelprod')") or die(mysqli_error($conn));
//                }
//            }
//        }

        if ($addflag == 1) {
            mysqli_query($conn, "INSERT INTO `$maindb`(`code`, `finprodcode`, `name`, `gram`, `rprodcode`, `approxkg`, `phytest`,`totpercqty`, `adduser`, `adddate`, `addtime`)
                           VALUES('$qvouch', '$mvouch', '$name', '$gram', '$rprodcode', '$approxkg', '$phytest','$totpercqty', '$adduser', '$adddate', '$addtime')") or die(mysqli_error($conn));
            insmess();
            $mvouch = $qvouch;
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        } else {
            $mvouch = $qvouch;
            $editdate = date("Y-m-d");
            $edittime = date("h:i:sa");
            mysqli_query($conn, "UPDATE `$maindb` SET  `finprodcode`='$mvouch', `name`='$name', `gram`='$gram', `rprodcode`='$rprodcode', `approxkg`='$approxkg', `edituser`='$adduser', `editdate`='$editdate', `edittime`='$edittime',
                       `phytest`='$phytest',`totpercqty`='$totpercqty' WHERE code = '$mvouch' ") or die(mysqli_error($conn));

            updmess();
            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
        }
        mysqli_query($conn, "UNLOCK TABLES");
    }
}
if ($addflag != 1) {
    $getvouch = '';

    if ($addflag == 0) {
        $getvouch = $_GET[$keyfld];
    } else {
        $getvouch = $qvouch;
    }
    $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE $keyfld='$getvouch'") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);
}

$senable = "";
$cenable = "";
if (isset($_GET["delete"])) {


    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    delete($zdb, "$trandb3", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    delete($zdb, "$trandb4", "$keyfld", $_GET[$keyfld], "$file?mode=last");
}
$versionToken = mt_rand();
include("includes/linkshead.php");
?>

<script src="MyScript/finprod.js<?= '?version=' . $versionToken ?>" type="text/javascript"></script>
<script src="MyScript/finprod_search.js" type="text/javascript"></script>
</head>
<body>
    <form action='' id="fsubmit" method='POST'>

        <!-- Default box -->

        <div class="row">
            <div class="col-lg-5">
                <div class="panel panel-default">
                    <?php include("includes/usermention.php");
 ?>
                    <div class="panel-body">
                        <!--                        <div class="row">
                                                    <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 numeric text-right pad">
                                                        <label class="label-control marg">Code</label>
                                                    </div>
                                                    <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                                        <input type="text" class="form-control form finprodcode" name="finprodcode" value="<?php Echo ($addflag != 1) ? $get["finprodcode"] : ""; ?>" autofocus>
                                                    </div>
                                                </div>-->
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 numeric text-right  pad">
                                <label class="label-control marg">Name</label>
                            </div>
                            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="hidden" class="form-control form code" name="code" value="<?php Echo ($addflag != 1) ? $get["code"] : "$qvouch"; ?>" >
                                <input type="text" class="form-control form prodname" name="name" value="<?php Echo ($addflag != 1) ? $get["name"] : ""; ?>" required>
                            </div>
                        </div>
                        <!--                        <div class="row">
                                                    <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 numeric text-right  pad">
                                                        <label class="label-control marg">Relate To Product</label>
                                                    </div>
                                                    <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 pad">
                                                        <select class="form-control form select2 rprodcode" name="rprodcode" style="width:100%" required>
                                                            <option value="<?php // echo ($addflag != 1) ? $fprodcode['code'] : "";                  ?>"><? echo ($addflag != 1) ? $fprodcode['name'] : ""; ?></option>
                        <?php
//                        $gprodgrp = mysqli_query($conn, "SELECT code,name FROM `product`")or die(mysqli_error($conn));
//                        while ($prodcode = mysqli_fetch_object($gprodgrp)) {
//                            Echo "<option value='$prodcode->code'>$prodcode->name</option>";
//                        }
                        ?>
                                                        </select>
                                                    </div>
                                                </div>-->
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 numeric text-right  pad">
                                <label class="label-control marg">Grammage</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-6 pad">
                                <input type="text" class="form-control form gram" name="gram" value="<?php Echo ($addflag != 1) ? $get["gram"] : ""; ?>" >
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 numeric text-right  pad">
                                <label class="label-control marg">Approx.Pcs.In 1kg.</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-6 pad">
                                <input type="text" class="form-control form " name="approxkg" value="<?php Echo ($addflag != 1) ? $get["approxkg"] : ""; ?>">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 numeric text-right  pad">
                                <label class="label-control marg">Physical Test Of Finished Product</label>
                            </div>
                            <div class="col-lg-5 col-md-5 col-sm-5 col-xs-6 pad">
                                <textarea rows="3" cols="50" class="form-control form" name="phytest">
                                    <?php Echo ($addflag != 1) ? $get["phytest"] : ""; ?>
                                </textarea>
                            </div>
                        </div>
                    </div>
                </div>
                <!--                <div class="panel-footer">
                <?php include("includes/buttons.php"); ?>
                            </div>-->
            </div>
            <div class="col-lg-7">
                <div class="panel panel-default">
                    <div class="panel-body">
                        <div class="nav-tabs-custom">
                            <ul class="nav nav-pills">
                                <li class="active"><a href="#tab_1" data-toggle="pill" aria-expanded="true">Manufacturing Detail</a></li>
                                <!--<li class=""><a href="#tab_4" data-toggle="pill" aria-expanded="false">Packing Detail</a></li>-->
                                <li class=""><a href="#tab_2" data-toggle="pill" aria-expanded="false">Relate To Product</a></li>
                            </ul>
                            <div class="tab-content">
                                <div class="tab-pane active" id="tab_1">
                                    <div class="">
                                        <table class="table table-striped table-condensed table-bordered"  style="width: 100%">
                                            <thead>
                                                <tr style="background-color: #6082BF;">
                                                    <th width="3%"></th>
                                                    <th width="6%">Sr</th>
                                                    <th width="25%">Ingredients</th>
                                                    <th width="15%">Unit</th>
                                                    <th width="8%">Qty. %</th>
<!--                                                    <th width="10%">Cost</th>
                                                    <th width="10%">Amount</th>-->
                                                </tr>
                                            </thead>
                                            <tbody class="tbody3">
                                                <?php
                                                if ($addflag != 1) {
                                                    $ctr3 = 0;
                                                    $gettran3 = mysqli_query($conn, "SELECT * FROM `$trandb3` WHERE `code`='$get[code]' ORDER BY `srno`")or die(mysqli_error($conn));
                                                    while ($tran3 = mysqli_fetch_array($gettran3)) {

                                                        $getitem = mysqli_query($conn, "SELECT code, name FROM `item` where code = '$tran3[rawmat]'")or die(mysqli_error($conn));
                                                        $fitem = mysqli_fetch_array($getitem);

                                                        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='srno3_$ctr3'      value='$tran3[srno]' readonly></td>";
                                                        Echo "<td>";
                                                        Echo "<select class='form-control form rawmat select2' name='rawmat_$ctr3' style='width:100%' >";
                                                        Echo "<option value='$fitem[code]'>$fitem[name]</option>";
                                                        $getitm = mysqli_query($conn, "SELECT code, name FROM `item` order by code")or die(mysqli_error($conn));
                                                        while ($mitem = mysqli_fetch_array($getitm)) {
                                                            Echo "<option value='$mitem[code]'>$mitem[name]</option>";
                                                        }
                                                        Echo "</select>";
                                                        Echo "</td>";
                                                        Echo "<td>";
                                                        Echo "<select class='form-control form unit select2' name='runit_$ctr3' style='width:100%' >";
                                                        Echo "<option value='$tran3[unit]'>$tran3[unit]</option>";
                                                        $gunit = mysqli_query($conn, "SELECT code, name FROM `unit` order by code")or die(mysqli_error($conn));
                                                        while ($munit = mysqli_fetch_array($gunit)) {
                                                            Echo "<option value='$munit[name]'>$munit[name]</option>";
                                                        }
                                                        Echo "</select>";
                                                        Echo "</td>";
                                                        Echo "<td><input type='number' class='form-control form text-right rqty'   name='rqty_$ctr3'  step='any'   value='$tran3[qtyperc]'></td>";
//                                                        Echo "<td><input type='number' class='form-control form text-right rcost'   name='rcost_$ctr3'  step='any'   value='$tran3[cost]'></td>";
//                                                        Echo "<td><input type='number' class='form-control form text-right ramount'   name='ramount_$ctr3'  step='any'   value='$tran3[amount]' readonly></td>";
                                                        Echo "</tr>";
                                                        $ctr3++;
                                                    }
                                                } else {
                                                    ?>
                                                    <tr>
                                                        <td><i class='fa fa-close remove'></i></td>
                                                        <td><input type='text' class='form-control form text-right srno'    name='srno3_0'      value='1' readonly></td>
                                                        <td>
                                                            <select class='form-control form rawmat select2' name='rawmat_0' style='width:100%' >
                                                                <option value=''></option>
                                                                <?
                                                                Echo $getrawmat = mysqli_query($conn, "SELECT code, name FROM `item` order by code")or die(mysqli_error($conn));
                                                                while ($mrawmat = mysqli_fetch_array($getrawmat)) {

                                                                    Echo "<option value='$mrawmat[code]'>$mrawmat[name]</option>";
                                                                }
                                                                ?>
                                                            </select>
                                                        </td>
                                                        <td>
                                                            <select class='form-control form unit select2' name='runit_0' style='width:100%' >
                                                                <option value=''></option>
                                                                <?
                                                                Echo $getunit = mysqli_query($conn, "SELECT code, name FROM `unit` order by code")or die(mysqli_error($conn));
                                                                while ($munit = mysqli_fetch_array($getunit)) {

                                                                    Echo "<option value='$munit[name]'>$munit[name]</option>";
                                                                }
                                                                ?>
                                                            </select>
                                                        </td>
                                                        <td><input type='number' class='form-control form text-right rqty' step='any'   name='rqty_0'  value=''></td>
    <!--                                                        <td><input type='number' class='form-control form text-right rcost' step='any'   name='rcost_0'  value='0'></td>
                                                        <td><input type='number' class='form-control form text-right ramount' step='any'   name='ramount_0'  value='0' readonly></td>-->
                                                    </tr>
                                                    <?
                                                }
                                                ?>
                                            </tbody>
                                        </table>

                                        <button type="button"  class="btn btn-success btn-square form"  id="addnew3">
                                            <i class="fa fa-plus" style="color:#ffffff" data-toggle='tooltip' title='Add Row'></i>    
                                        </button>
                                        <input type="hidden" id="items3" name="items3" value="<?php echo ($addflag != 1) ? $ctr3 : 1; ?>" />

                                        <div class='row'>
                                            <div class='col-lg-10 pad text-right'>
                                                <label class='label-control marg' style="color: #6082BF"><b>Total Qty.%</b></label>
                                            </div>
                                            <div class='col-lg-2 pad'>
                                                <input type='number' step='any' name='totpercqty' class='form-control form text-right totpercqty'
                                                       style="width: 85%;border: 1px solid #6082BF" value='<?php echo ($addflag != 1) ? $get['totpercqty'] : ''; ?>' readonly>
                                            </div>
                                            <!--                                                                                    <div class='col-lg-12 col-xs-12 pad'>
                                                                                                                                    <div class='col-lg-3 pad'></div>-->

                                        </div>
                                        <!--</div>-->
                                    </div>

                                </div><!-- /.tab-pane -->
                                <!--                                <div class="tab-pane" id="tab_4">
                                                                    <div class="table-responsive">
                                                                        <table class="table table-striped table-condensed table-bordered"  style="width: 100%">
                                                                            <thead>
                                                                                <tr style="background-color: #6082BF;">
                                                                                    <th width="3%"></th>
                                                                                    <th width="6%">Sr</th>
                                                                                    <th width="25%">Packing Material</th>
                                                                                    <th width="15%">Unit</th>
                                                                                    <th width="8%">Qty.</th>
                                                                                    <th width="10%">Cost</th>
                                                                                    <th width="10%">Amount</th>
                                                                                </tr>
                                                                            </thead>
                                                                            <tbody class="tbody4">
                                <?php
//                                                if ($addflag != 1) {
//                                                    $ctr4 = 0;
//                                                    $gettran4 = mysqli_query($conn, "SELECT * FROM `$trandb4` WHERE `code`='$get[code]' ORDER BY `srno`")or die(mysqli_error($conn));
//                                                    while ($tran4 = mysqli_fetch_array($gettran4)) {
//
//                                                        $getitem2 = mysqli_query($conn, "SELECT code, name FROM `packing` where code = '$tran4[packing]'")or die(mysqli_error($conn));
//                                                        $fitem2 = mysqli_fetch_array($getitem2);
//
//
//
//                                                        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 srno4'    name='srno4_$ctr4'      value='$tran4[srno]' readonly></td>";
//                                                        Echo "<td>";
//                                                        Echo "<select class='form-control form packing select2' name='packing_$ctr4' style='width:100%' >";
//                                                        Echo "<option value='$fitem2[code]'>$fitem2[name]</option>";
//                                                        $getitm2 = mysqli_query($conn, "SELECT code, name FROM `packing` order by code")or die(mysqli_error($conn));
//                                                        while ($mitem2 = mysqli_fetch_array($getitm2)) {
//                                                            Echo "<option value='$mitem2[code]'>$mitem2[name]</option>";
//                                                        }
//                                                        Echo "</select>";
//                                                        Echo "</td>";
//                                                        Echo "<td>";
//                                                        Echo "<select class='form-control form unit select2' name='punit_$ctr4' style='width:100%' >";
//                                                        Echo "<option value='$tran4[unit]'>$tran4[unit]</option>";
//                                                        $gunit2 = mysqli_query($conn, "SELECT code, name FROM `unit` order by name")or die(mysqli_error($conn));
//                                                        while ($munit2 = mysqli_fetch_array($gunit2)) {
//                                                            Echo "<option value='$munit2[name]'>$munit2[name]</option>";
//                                                        }
//                                                        Echo "</select>";
//                                                        Echo "</td>";
//                                                        Echo "<td><input type='number' class='form-control form text-right pqty'   name='pqty_$ctr4'  step='any'   value='$tran4[qty]'></td>";
////                                                        Echo "<td><input type='number' class='form-control form text-right pcost'   name='pcost_$ctr4'  step='any'   value='$tran4[cost]'></td>";
////                                                        Echo "<td><input type='number' class='form-control form text-right pamount'   name='pamount_$ctr4'  step='any'   value='$tran4[amount]' readonly></td>";
//                                                        Echo "</tr>";
//                                                        $ctr4++;
//                                                    }
//                                                } else {
                                ?>
                                                                                    <tr>
                                                                                        <td><i class='fa fa-close remove'></i></td>
                                                                                        <td><input type='text' class='form-control form text-right srno'    name='srno4_0'      value='1' readonly></td>
                                                                                        <td>
                                                                                            <select class='form-control form packing select2' name='packing_0' style='width:100%' >
                                                                                                <option value=''></option>
                                <?
//                                                                Echo $getpack = mysqli_query($conn, "SELECT code, name FROM `packing` order by code")or die(mysqli_error($conn));
//                                                                while ($mpack = mysqli_fetch_array($getpack)) {
//                                                                    Echo "<option value='$mpack[code]'>$mpack[name]</option>";
//                                                                }
                                ?>
                                                                                            </select>
                                                                                        </td>
                                                                                        <td>
                                                                                            <select class='form-control form unit select2' name='punit_0' style='width:100%' >
                                                                                                <option value=''></option>
                                <?
//                                                                Echo $getunit = mysqli_query($conn, "SELECT code, name FROM `unit` order by name")or die(mysqli_error($conn));
//                                                                while ($munit = mysqli_fetch_array($getunit)) {
//
//                                                                    Echo "<option value='$munit[name]'>$munit[name]</option>";
//                                                                }
                                ?>
                                                                                            </select>
                                                                                        </td>
                                                                                        <td><input type='number' class='form-control form text-right rqty' step='any'   name='rqty_0'  value=''></td>
                                                                                            <td><input type='number' class='form-control form text-right rcost' step='any'   name='rcost_0'  value='0'></td>
                                                                                        <td><input type='number' class='form-control form text-right ramount' step='any'   name='ramount_0'  value='0' readonly></td>
                                                                                    </tr>
                                <?
//                                                }
                                ?>
                                                                            </tbody>
                                                                        </table>
                                                                        <button type="button"  class="btn btn-success btn-square form"  id="addnew4">
                                                                            <i class="fa fa-plus" style="color:#ffffff" data-toggle='tooltip' title='Add Row'></i>    
                                                                        </button>
                                                                        <input type="hidden" id="items4" name="items4" value="<?php // echo ($addflag != 1) ? $ctr4 : 1;      ?>" />
                                                                    </div>  
                                                                </div> /.tab-pane -->
                                <div class="tab-pane" id="tab_2">
                                    <div class="">
                                        <table class="table table-striped table-condensed table-bordered"  style="width: 100%">
                                            <thead>
                                                <tr style="background-color: #6082BF;">
                                                    <th width="3%"></th>
                                                    <th width="6%">Sr</th>
                                                    <th width="10%">Product Code</th>
                                                    <th width="20%">Name</th>
                                                    <th width="10%">Net Wt.</th>
                                                    <th width="8%">Apporx. Pcs./Kg.</th>
                                                    <th width="8%">Apporx. Pcs./ltr.</th>
                                                    <th width="8%">Packing Button</th>
                                                </tr>
                                            </thead>
                                            <tbody class="tbody">
                                                <?php
                                                if ($addflag != 1) {
                                                    $ctr = 0;
                                                    $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `code`='$get[code]' ORDER BY `srno`")or die(mysqli_error($conn));
                                                    while ($tran = mysqli_fetch_array($gettran)) {

                                                        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]' readonly></td>";

                                                        Echo "<td>";
                                                        Echo "<select class='form-control form prodcode ' name='prodcode_$ctr' style='width:100%' >";
                                                        Echo "<option value='$tran[prodcode]'>$tran[prodcode]</option>";
                                                        $getpcode = mysqli_query($conn, "SELECT prodcode FROM `product` order by name")or die(mysqli_error($conn));
                                                        while ($mprod = mysqli_fetch_array($getpcode)) {
                                                            Echo "<option value='$mprod[prodcode]'>$mprod[prodcode]</option>";
                                                        }
                                                        Echo "</select>";
                                                        Echo "</td>";

                                                        Echo "<td><input type='text' class='form-control form  prodname'   name='prodname_$ctr'   value='$tran[prodname]' readonly></td>";
                                                        Echo "<td><input type='text' class='form-control form  netwt'   name='netwt_$ctr'  step='any'   value='$tran[netwt]' readonly></td>";
                                                        Echo "<td><input type='number' class='form-control form text-right approxkg'   name='approxkg_$ctr'  step='any'   value='$tran[approxkg]'></td>";
                                                        Echo "<td><input type='number' class='form-control form text-right approxltr'   name='approxltr_$ctr'  step='any'   value='$tran[approxltr]'></td>";
                                                        Echo "<td><button class='form-control form btn btn-primary pack'>Pack</button></td>";
                                                        Echo "</tr>";
                                                        $ctr++;
                                                    }
                                                } else {
                                                    ?>
                                                    <tr>
                                                        <td><i class='fa fa-close remove'></i></td>
                                                        <td><input type='text' class='form-control form text-right srno'    name='srno_0'      value='1' readonly></td>
                                                        <td>
                                                            <select class='form-control form prodcode ' name='prodcode_0' style='width:100%' >
                                                                <option value=''></option>
                                                                <?
                                                                $getpcode = mysqli_query($conn, "SELECT prodcode FROM `product` order by name")or die(mysqli_error($conn));
                                                                while ($mprod = mysqli_fetch_array($getpcode)) {
                                                                    Echo "<option value='$mprod[prodcode]'>$mprod[prodcode]</option>";
                                                                }
                                                                ?>
                                                            </select>
                                                        </td>
                                                        <td><input type='text' class='form-control form  prodname'   name='prodname_0'   value='' readonly></td>
                                                        <td><input type='text' class='form-control form  netwt'   name='netwt_0'  step='any'   value='' readonly></td>
                                                        <td><input type='number' class='form-control form text-right approxkg'   name='approxkg_0'  step='any'   value='0'></td>
                                                        <td><input type='number' class='form-control form text-right approxltr'   name='approxltr_0'  step='any'   value='0'></td>
                                                        <td><button class='form-control form btn btn-primary pack'>Pack</button></td>
                                                    </tr>
                                                    <?
                                                }
                                                ?>
                                            </tbody>
                                        </table>
                                        <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>
                                        <input type="hidden" id="items" name="items" value="<?php echo ($addflag != 1) ? $ctr : 1; ?>" />
                                        <div class="row divpack" style="display: none">
                                            <div class='col-lg-2 pad sdiv'>
                                                <button class='form-control form btn btn-primary psave'>Save</button>
                                            </div>
                                            <table class="table table-striped table-condensed table-bordered"  style="width: 100%">
                                                <thead>
                                                    <tr style="background-color: #6082BF;">
                                                        <th width="3%"></th>
                                                        <th width="6%">Sr</th>
                                                        <th width="25%">Packing Material</th>
                                                        <th width="15%">Unit</th>
                                                        <th width="8%">Qty.</th>
    <!--                                                    <th width="10%">Cost</th>
                                                        <th width="10%">Amount</th>-->
                                                    </tr>
                                                </thead>
                                                <tbody class="tbody4">
                                                    <?php
//                                                    if ($addflag != 1) {
//                                                        $ctr4 = 0;
//                                                        $gettran4 = mysqli_query($conn, "SELECT * FROM `$trandb4` WHERE `code`='$get[code]' ORDER BY `srno`")or die(mysqli_error($conn));
//                                                        while ($tran4 = mysqli_fetch_array($gettran4)) {
//
//                                                            $getitem2 = mysqli_query($conn, "SELECT code, name FROM `packing` where code = '$tran4[packing]'")or die(mysqli_error($conn));
//                                                            $fitem2 = mysqli_fetch_array($getitem2);
//
//
//
//                                                            Echo "<tr class='curr'>";
//                                                            Echo "<td><i class='fa fa-close remove' style='display:none;'></i></td>";
//                                                            Echo "<td><input type='text' class='form-control form text-right srno4'    name='srno4[]'      value='$tran4[srno]' readonly></td>";
//                                                            Echo "<td>";
//                                                            Echo "<select class='form-control form packing select2' name='packing[]' style='width:100%' >";
//                                                            Echo "<option value='$fitem2[code]'>$fitem2[name]</option>";
//                                                            $getitm2 = mysqli_query($conn, "SELECT code, name FROM `packing` order by code")or die(mysqli_error($conn));
//                                                            while ($mitem2 = mysqli_fetch_array($getitm2)) {
//                                                                Echo "<option value='$mitem2[code]'>$mitem2[name]</option>";
//                                                            }
//                                                            Echo "</select>";
//                                                            Echo "</td>";
//                                                            Echo "<td>";
//                                                            Echo "<select class='form-control form unit select2' name='punit[]' style='width:100%' >";
//                                                            Echo "<option value='$tran4[unit]'>$tran4[unit]</option>";
//                                                            $gunit2 = mysqli_query($conn, "SELECT code, name FROM `unit` order by name")or die(mysqli_error($conn));
//                                                            while ($munit2 = mysqli_fetch_array($gunit2)) {
//                                                                Echo "<option value='$munit2[name]'>$munit2[name]</option>";
//                                                            }
//                                                            Echo "</select>";
//                                                            Echo "</td>";
//                                                            Echo "<td><input type='hidden' class='form-control form prodcode'   name='relprod[]'  value='$tran4[relprod]'>
//                                                                      <input type='number' class='form-control form text-right pqty'   name='pqty[]'  step='any'   value='$tran4[qty]'></td>";
//                                                        Echo "<td><input type='number' class='form-control form text-right pcost'   name='pcost_$ctr4'  step='any'   value='$tran4[cost]'></td>";
//                                                        Echo "<td><input type='number' class='form-control form text-right pamount'   name='pamount_$ctr4'  step='any'   value='$tran4[amount]' readonly></td>";
//                                                            Echo "</tr>";
//                                                            $ctr4++;
//                                                        }
//                                                    }
                                                    ?>
                                                </tbody>
                                            </table>
                                            <button type="button"  class="btn btn-success btn-square form"  id="addnew4">
                                                <i class="fa fa-plus" style="color:#ffffff" data-toggle='tooltip' title='Add Row'></i>    
                                            </button>
                                            <input type="hidden" id="items4" name="items4" value="<?php echo ($addflag != 1) ? $ctr4 : 1; ?>" />
                                        </div>
                                    </div>
                                </div><!-- /.tab-pane -->
                            </div>
                        </div>
                    </div>

                    <!-- /.tab-content -->
                </div>
            </div>
            <div class="row">
                <div class="col-lg-12">
                    <div class="panel panel-default">
                        <div class="panel-footer">
                            <?php include("includes/buttons.php"); ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!--            <div class="panel-footer">
        <?php include("includes/buttons.php"); ?>
                    </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="1">Code</option>
                                <option value="2">Name</option>
                                <option value="3">Grammage</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control soperator">
                                <option value="">Type of Search</option>
                                <option value="%">Like</option>
                                <option value="=">Exactly</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control sortBy">
                                <option value="">Sort By</option>
                                <option value="1">Code</option>
                                <option value="2">Name</option>
                                <option value="3">Grammage</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" style="background-color:#1ABC9C">
                                    <th width="10%">Code</th>
                                    <th width="15%">Name</th>
                                    <th width="10%">Grammage</th>
                                    <th width="10%">Approx pcs. in 1kg.</th>
                                    <th width="10%">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>

