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

$formname = 'Production / BMR';
$maindb = 'production';
$trandb = 'prodraw';
$trandb2 = 'prodrel';
$trandb3 = 'prodpack';
$stkdbraw = 'rawsacn';
$stkdbprod = 'prodsacn';
$stkdbpack = 'packsacn';
//$accdb = 'actran';
$printfile = "production_print.php";
$file = 'production.php';
$trandbexist = 1;
$keyfld = "vouchno";

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

$myyear = $_SESSION[$myyear];

$zroot = $_SESSION["zdir"];
$zyear = right($zdb, 4);
$zStartDate = getzdates($zyear, 'S');
$zEndDate = getzdates($zyear, 'E');

mysqli_select_db($conn, $zdb);

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

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

    $a = 1;
    $vouchdt = trim($_POST["vouchdt"]);
    $prepdate = trim($_POST["prepdate"]);
    $expdate = trim($_POST["expdate"]);
    $voucher = trim($_POST["voucher"]);
    $mono = $_POST["mono"];
    $finprod = $_POST["product"];
    $godown = $_POST["godown"];
    $ptest = trim($_POST["phytest"], ' ');
    $phytest = $ptest;
    $mfgproc = $_POST["mfgproc"];
    $batchno = $_POST["batchno"];
    $batchsize = $_POST["batchsize"];
    $qty = $_POST["qty"];
    $pcs = $_POST["pcs"];
    $yieldkg = $_POST["yieldkg"];
    $refno = $_POST["refno"];
    $wasteqty = $_POST["wasteqty"];
    $yieldperc = $_POST["yieldperc"];
    $weightby = $_POST["weightby"];
    $checkedby = $_POST["checkedby"];
    $approvedby = $_POST["approvedby"];
    $totactpcs = $_POST["totactpcs"];
    $totpercqty = $_POST["totpercqty"];
    $totbqty = $_POST["totbqty"];

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



    $mvouch = $qvouch;

    $count = $_POST["items"];
    $count2 = $_POST["items3"];
    $count3 = $_POST["items4"];

    $group = $source . $mvouch;


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

    $salecode = 15;
    if ($a == 1) {
        mysqli_query($conn, "LOCK TABLES `$maindb` WRITE, `$trandb` WRITE, `$trandb2` WRITE, `item` WRITE, `$stkdbpack` WRITE, `$trandb3` WRITE,
                   `finprod` WRITE, `product` WRITE, `$stkdbprod` WRITE, `$stkdbraw` WRITE, `godown` WRITE, `accmast` WRITE, `packing` WRITE");

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

        #################################TRAN PART #################################
        mysqli_query($conn, "DELETE FROM `$trandb` WHERE `$keyfld`='$mvouch'") or die(mysqli_error($conn));
        mysqli_query($conn, "DELETE FROM `$trandb2` WHERE `$keyfld`='$mvouch'") or die(mysqli_error($conn));
        mysqli_query($conn, "DELETE FROM `$trandb3` WHERE `$keyfld`='$mvouch'") or die(mysqli_error($conn));
        mysqli_query($conn, "DELETE FROM `$stkdbprod` WHERE `$keyfld`='$mvouch' AND `source`='$source'")or die(mysqli_error($conn));
        mysqli_query($conn, "DELETE FROM `$stkdbraw` WHERE `$keyfld`='$mvouch' AND `source`='$source'")or die(mysqli_error($conn));
        mysqli_query($conn, "DELETE FROM `$stkdbpack` WHERE `$keyfld`='$mvouch' AND `source`='$source'")or die(mysqli_error($conn));


        $cnt = 0;
        for ($i = 0; $i < $count; $i++) {
            $msrno = $_POST["srno_$i"];
            $prodcode = $_POST["prodcode_$i"];
            $prodname = $_POST["prodname_$i"];
            $netwt = $_POST["netwt_$i"];
            $approxpcs = $_POST["approxpcs_$i"];
            $conskg = $_POST["conskg_$i"];
            $actpcs = $_POST["actpcs_$i"];
            $approvby = $_POST["approvby_$i"];

            $mpcode = mysqli_query($conn, "SELECT code,cost,rate FROM `product` WHERE `prodcode`='$procode'") or die(mysqli_error($conn));
            $pcode = mysqli_fetch_array($mpcode);
            $itcode = $pcode['code'];
            $cost = $pcode['cost'];
            $rate = $pcode['rate'];
            $cnt++;


            if (!empty($prodcode)) {
                mysqli_query($conn, "INSERT INTO `$trandb2`(`vouchno`, `mono`, vouchdt, srno, prodcode, prodname, netwt, approxpcs, conskg, actpcs, `approvby`)
                                                  VALUES('$mvouch', '$mono', '$vouchdt', '$cnt', '$prodcode', '$prodname', '$netwt', '$approxpcs','$conskg', '$actpcs', '$approvby')") or die(mysqli_error($conn));

                if (!empty($approvby)) {

                    mysqli_query($conn, "INSERT INTO `$stkdbprod`(`srno`, `yearno`, `godown`, `vouchno`, `vouchdt`, `itemcode`,  `unit`,
                                                  `qty`, `source`, amount, accode, `group` , `rate`, `cost`, `itemgrp`, `ordno`, `monno`)
                            VALUES('$cnt', '$myyear', '$godown', '$mvouch', '$vouchdt', '$prodcode',  '$unit',
                                   '$actpcs', '$source', '', '', '$group', '$rate', '$cost', '', '', '$mono')") or die(mysqli_error($conn));
                }
            }
        }

        $cnt2 = 0;
        for ($k = 0; $k < $count2; $k++) {
            $rlsrno = $_POST["srno_$k"];
            $rawmat = $_POST["rawmat_$k"];
            $unit = $_POST["runit_$k"];
            $qtyperc = $_POST["qtyperc_$k"];
            $qtybatch = $_POST["qtybatch_$k"];
            $stkbqty = $qtybatch * -1;
            $issuedqty = $_POST["issuedqty_$k"];
            $arno = $_POST["arno_$k"];
            $idno = $_POST["idno_$k"];

            $cnt2++;
            $mrcode = mysqli_query($conn, "SELECT code, name, rate, cost,unit FROM `item` WHERE `code`='$rawmat'") or die(mysqli_error($conn));
            $rcode = mysqli_fetch_array($mrcode);
            $rawname = $rcode['name'];
            $rate2 = $rcode['rate'];
            $cost2 = $rcode['cost'];
            $unit2 = $rcode['unit'];
            if (!empty($rawmat)) {
                mysqli_query($conn, "INSERT INTO `$trandb`(`vouchno`, `mono`, vouchdt, srno, itemcode, itemname, unit, qtyperc, qtybatch,
                                                    `issuedqty`, `idno`, `arno`)
                                                  VALUES('$mvouch', '$mono', '$vouchdt', '$cnt2', '$rawmat', '$rawname', '$unit', '$qtyperc', '$qtybatch',
                                                        '$issuedqty','$idno','$arno')") or die(mysqli_error($conn));
            }

            if ($totbqty) {
                mysqli_query($conn, "INSERT INTO `$stkdbraw`(`srno`, `yearno`, `godown`, `vouchno`, `vouchdt`, `itemcode`,  `unit`,
                                                  `qty`, `source`, amount, accode, `group` , `rate`, `cost`, `itemgrp`, `ordno`, `monno`)
                            VALUES('$cnt2', '$myyear', '$godown', '$mvouch', '$vouchdt', '$rawmat',  '$unit2',
                                   '$stkbqty', '$source', '', '', '$group', '$rate2', '$cost2', '', '', '$mono')") or die(mysqli_error($conn));
            }
        }
        
        $cnt3 = 0;
        for ($q = 0; $q < $count3; $q++) {
            $plsrno = $_POST["srno4"][$q];
            $packing = $_POST["packing"][$q];
            $parno = $_POST["arno"][$q];
            $pactqty = $_POST["actqty"][$q];
            $issuedqty = $_POST["issuedqty"][$q];
            $stkpqty = $issuedqty * -1;

            $cnt3++;
            $mpcode = mysqli_query($conn, "SELECT code, name FROM `packing` WHERE `code`='$packing'") or die(mysqli_error($conn));
            $pcode = mysqli_fetch_array($mpcode);
            $packname = $pcode['name'];
            if (!empty($packing)) {
                mysqli_query($conn, "INSERT INTO `$trandb3`(`vouchno`, `mono`, vouchdt, srno, itemcode, itemname, actqty, arno, issuedqty)
                                        VALUES('$mvouch', '$mono', '$vouchdt', '$cnt3', '$packing', '$packname', '$pactqty','$parno','$issuedqty')") or die(mysqli_error($conn));
            }

            if ($issuedqty > 0) {

                mysqli_query($conn, "INSERT INTO `$stkdbpack`(`srno`, `yearno`, `godown`, `vouchno`, `vouchdt`, `itemcode`,  `unit`,
                                                  `qty`, `source`, amount, accode, `group` , `rate`, `cost`, `itemgrp`, `ordno`, `monno`)
                            VALUES('$cnt2', '$myyear', '$godown', '$mvouch', '$vouchdt', '$packing',  '',
                                   '$stkpqty', '$source', '', '', '$group', '', '', '', '', '$mono')") or die(mysqli_error($conn));
            }
        }


        ############################## MAIN ######################################################
        if ($addflag == 1) {
            $newvoucher = $mono . $mvouch;
            mysqli_query($conn, "INSERT INTO `$maindb`(vouchno, `vouchdt`, `prepdate`, `expdate`,`voucher`, `mono`, `product`, `phytest`, `mfgproc`, `batchno`, 
                                               `batchsize`, `qty`,  `pcs`, `yieldkg`, `refno`,`wasteqty`, `yieldperc`, `godown`, `rem1`,
                                               `rem2`,`totactpcs`, `totpercqty`, `totbqty`,`weightby`,`checkedby`)
                                      VALUES('$mvouch', '$vouchdt', '$prepdate', '$expdate','$newvoucher', '$mono', '$finprod', '$phytest', '$mfgproc', '$batchno',
                                             '$batchsize', '$qty', '$pcs', '$yieldkg', '$refno','$wasteqty','$yieldperc', '$godown', '$rem1',
                                             '$rem2', '$totactpcs', '$totpercqty', '$totbqty', '$weightby', '$checkedby')") or die(mysqli_error($conn));
            insmess();
            header("refresh: 1; $file?mode=edit&$keyfld=$mvouch");
//            $_SESSION['print_voucher'] = $mvouch;
        } else {

            mysqli_query($conn, "UPDATE `$maindb` SET  `vouchdt`='$vouchdt',`prepdate`='$prepdate',`expdate`='$expdate', `mono`='$mono', `product`='$finprod', `phytest`='$phytest', `mfgproc`='$mfgproc',
                                               `batchno`='$batchno', `batchsize`='$batchsize', `qty`='$qty',  `pcs`='$pcs', `yieldkg`='$yieldkg', `refno`='$refno',
                                               `wasteqty`='$wasteqty',`yieldperc`='$yieldperc', `godown`='$godown', `rem1`='$rem1',
                                               `rem2`='$rem2', `weightby`='$weightby', `checkedby`='$checkedby', `totactpcs`='$totactpcs', 
                                               `totpercqty`='$totpercqty', `totbqty`='$totbqty' WHERE `vouchno` = '$mvouch' ") or die(mysqli_error($conn));
            updmess();
            header("refresh: 1; $file?mode=edit&$keyfld=$mvouch");
        }
    }
    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' $keycond ") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);

    $getfinprod = mysqli_query($conn, "SELECT `code`,`name` FROM `finprod` WHERE `code`= '$get[product]'") or die(mysqli_error($conn));
    $finprod = mysqli_fetch_array($getfinprod);

    $getgodown = mysqli_query($conn, "SELECT * FROM `godown` WHERE `code`= '$get[godown]'") or die(mysqli_error($conn));
    $getgd = mysqli_fetch_array($getgodown);

    $getchk = mysqli_query($conn, "SELECT accode,name FROM `accmast` where `grpcode`='040' and `accode`='$get[checkedby]' ")or die(mysqli_error($conn));
    $mchk = mysqli_fetch_array($getchk);

    $getwei = mysqli_query($conn, "SELECT accode,name FROM `accmast` where `grpcode`='040' and `accode`='$get[weightby]' ")or die(mysqli_error($conn));
    $mwei = mysqli_fetch_array($getwei);
}

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

if (isset($_GET["delete"])) {
    delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    delete($zdb, "$trandb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    delete($zdb, "$trandb2", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    $delcond = " source = '$source' and vouchno = '$_GET[vouchno]' ";
    delnew($zdb, "$stkdbraw", "$delcond", "$file?mode=last");
    delnew($zdb, "$stkdbprod", "$delcond", "$file?mode=last");
}

include("includes/linkshead.php");
?>
<script src="MyScript/production.js?A=<?= mt_rand(); ?>" type="text/javascript"></script>
<script src="MyScript/production_search.js?A=<?= mt_rand(); ?>" type="text/javascript"></script>
<script src="MyScript/checkdate.js" type="text/javascript"></script>

</head>
<body>
    <form action='' method='POST'>
        <!--        <div class="col-lg-3 pad text-center" id="overlay_form" style="display:none;z-index:999;">
                    <button type="button" id="close_form" style="float: right;font-weight: bold;font-size: 18px;color:red;"><i class="fa fa-close"></i></button>
                    <div style='width:100%;' id='browdata'>
                    </div>
                </div>-->
        <!-- Default box -->
        <div>
            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12  pad">
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h3 class="panel-title" style="color:#1ABC9C"><?php Echo $formname; ?></h3>
                        <div class="actions pull-right">
                        </div>
                    </div>
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Voucher</label>
                            </div>
                            <div class="col-lg-1 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" name="voucher" class="form-control form voucher" value="<?php echo ($addflag != 1) ? $get['voucher'] : '' ?>" tabindex="-1" readonly>
                                <input type="hidden" name="mono" class="form-control form monno" value="<?php echo ($addflag != 1) ? $get['mono'] : '' ?>" >
                                <input type="hidden" name="vouchno" class="form-control form vouchno" value="<?php echo ($addflag != 1) ? $get['vouchno'] : $qvouch ?>" >
                                <input type="hidden" class="form-control form addflag"  value="<?php Echo $addflag ?>" >
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Date</label>
                            </div>
                            <div class="col-lg-2 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="date" class="form-control form vouchdt" name="vouchdt" value="<?php Echo ($addflag != 1) ? $get["vouchdt"] : date("Y-m-d"); ?>" autofocus>
                                   <input type="date" value="<?= $zStartDate ?>" style="width: 1px;border: none" class="zstdate">
                            <input type="date" value="<?= $zEndDate ?>" style="width: 1px;border: none" class="zenddate">
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Product</label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-4 col-xs-4 pad">
                                <select name="product" class="form-control form " id="product" style='width:90%' required>
                                    <option value="<?php echo ($addflag != 1) ? $finprod['code'] : ""; ?>"><?php echo ($addflag != 1) ? $finprod['name'] : ""; ?></option>
                                    <?php
                                    $mown2 = mysqli_query($conn, "SELECT `code`, `name` FROM `finprod` ORDER BY name") or die(mysqli_error($conn));
                                    while ($trow2 = mysqli_fetch_array($mown2)) {
                                        echo "<option value='$trow2[code]'>$trow2[name]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Godown</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                                <select name="godown" class="form-control form godown" required>
                                    <option value="<?php echo ($addflag != 1) ? $getgd['code'] : " "; ?>"><?php echo ($addflag != 1) ? $getgd['gcode'] : " "; ?></option>
                                    <?php
                                    $mowng = mysqli_query($conn, "SELECT * FROM `godown`") or die(mysqli_error($conn));
                                    while ($trowg = mysqli_fetch_array($mowng)) {
                                        echo "<option value=$trowg[code]>$trowg[gcode]</option>";
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Batch No.</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                                <input type="text" class="form-control form numeric batchno " name="batchno" value="<?php Echo ($addflag != 1) ? $get["batchno"] : '' ?>" required>
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Batch Size</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                                <input type="text" class="form-control form numeric batchsize text-right" name="batchsize" value="<?php Echo ($addflag != 1) ? $get["batchsize"] : '0' ?>" >
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg"></label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-right pad">
                                <label class="label-control marg">Mfg. Process</label>
                            </div>
                            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" class="form-control form mfgproc" name="mfgproc" value="<?php Echo ($addflag != 1) ? $get["mfgproc"] : '' ?>">
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                <input type='button' class="btn btn-primary rawmodal form" value='Raw Material'>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-right pad">
                                <label class="label-control marg">Yield(Kg)</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                                <input type="text" class="form-control form text-right yieldkg" name="yieldkg" value="<?php Echo ($addflag != 1) ? $get["yieldkg"] : '0' ?>">
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-right pad">
                                <label class="label-control marg">Waste Qty.(Kg)</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                                <input type="text" class="form-control form text-right wasteqty" name="wasteqty" value="<?php Echo ($addflag != 1) ? $get["wasteqty"] : '0' ?>" readonly>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-right pad">
                                <label class="label-control marg">Yield(%)</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                                <input type="text" class="form-control form text-right yieldperc" name="yieldperc" value="<?php Echo ($addflag != 1) ? $get["yieldperc"] : '0' ?>" readonly>
                            </div>

                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Preparation Date</label>
                            </div>
                            <div class="col-lg-1 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="date" class="form-control form expdate" name="prepdate" value="<?php Echo ($addflag != 1) ? $get["prepdate"] : date("Y-m-d"); ?>" autofocus>
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Expiry Date</label>
                            </div>
                            <div class="col-lg-1 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="date" class="form-control form expdate" name="expdate" value="<?php Echo ($addflag != 1) ? $get["expdate"] : date("Y-m-d"); ?>" autofocus>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 text-center  pad">
                                <label class="label-control marg">Physical Test Of Finished Product</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 pad">
                                <textarea rows="3" cols="40" class="form-control form" name="phytest">
                                    <?php Echo ($addflag != 1) ? $get["phytest"] : ""; ?>
                                </textarea>
                            </div>
                            <div class="col-lg-1 col-md-3 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">REF MFR NO.</label>
                            </div>
                            <div class="col-lg-1 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="text" name="refno" class="form-control form refno" value="<?php echo ($addflag != 1) ? $get['refno'] : '' ?>" >
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 pad">
                <div class="panel panel-default">
                    <div class="panel-body">
                        <div class="table-responsive proddtl">
                            <?php
                            if ($addflag != 1) {
                                echo "<table class='table-striped table-condensed table-bordered tab' width='100%'>";
                                echo "<thead  class='mySearchTr'>";
                                echo "<tr>";
                                echo "<th style='width: 2%'></th>";
                                echo "<th style='width: 3%'>Sr.</th>";
                                echo "<th style='width: 8%;'>Product Code</th> ";
                                echo "<th style='width: 15%;'>Name</th>";
                                echo "<th style='width: 6%'>Net Wt.</th>";
                                echo "<th style='width: 6%' class='text-right'>Approx. Pcs.</th>";
                                echo "<th style='width: 6%' class='text-right'>Consumption in Kg.</th>";
                                echo "<th style='width: 6%' class='text-right'>Actual pcs.</th>";
                                echo "<th style='width: 10%'>Approved By</th>";
                                echo "</tr>";
                                echo "</thead>";
                                echo "<tbody class='tbody'>";
                                $qry = mysqli_query($conn, "SELECT * FROM  `$trandb2` WHERE `vouchno`='$getvouch' ORDER BY srno") or die(mysqli_error($conn));
                                $ctr = 0;
                                while ($tran = mysqli_fetch_array($qry)) {

                                    $getstaf = mysqli_query($conn, "SELECT accode,name FROM `accmast` where `grpcode`='040' and `accode`='$tran[approvby]' order by name")or die(mysqli_error($conn));
                                    $mstaff = mysqli_fetch_array($getstaf);

                                    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 approxpcs'   name='approxpcs_$ctr'  step='any'   value='$tran[approxpcs]'></td>";
                                    Echo "<td><input type='number' class='form-control form text-right conskg'   name='conskg_$ctr'  step='any'   value='$tran[conskg]'></td>";
                                    Echo "<td><input type='number' class='form-control form text-right actpcs'   name='actpcs_$ctr'  step='any'   value='$tran[actpcs]'></td>";
                                    Echo "<td>";
                                    Echo "<select class='form-control form approvby' name='approvby_$ctr' style='width:100%' >";
                                    Echo "<option value='$mstaff[accode]'>$mstaff[name]</option>";
                                    $stff = mysqli_query($conn, "SELECT accode,name FROM `accmast` where `grpcode`='040' order by name")or die(mysqli_error($conn));
                                    while ($mstff = mysqli_fetch_array($stff)) {
                                        Echo "<option value='$mstff[accode]'>$mstff[name]</option>";
                                    }
                                    Echo "</select>";
                                    Echo"</td>";
                                    Echo "</tr>";
                                    $ctr++;
                                }
                                echo "</tbody>";
                                echo "<input type='hidden' id='items' name='items' value= '$ctr' >";
                                echo "</table>";
                            }
                            ?>
                        </div>
                        <div class="col-lg-12 pad">
                            <button type="button" class="btn btn-primary btn-square form"  id="addnew">
                                <i class="fa fa-plus" data-toggle='tooltip' title='Add Row'></i>    
                            </button>
                        </div>
                        <div class="col-lg-12 pad">
                            <div class="row">
                                <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-right pad">
                                    <label class="label-control marg">Checked By</label>
                                </div>
                                <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                    <select name="checkedby" class="form-control form checkedby" style='width:90%' >
                                        <option value="<?php echo ($addflag != 1) ? $mchk['accode'] : ""; ?>"><?php echo ($addflag != 1) ? $mchk['name'] : " "; ?></option>
                                        <?php
                                        $mown3 = mysqli_query($conn, "SELECT `accode`, `name` FROM `accmast` where `grpcode`='040' ORDER BY name") or die(mysqli_error($conn));
                                        while ($trow3 = mysqli_fetch_array($mown3)) {
                                            echo "<option value='$trow3[accode]'>$trow3[name]</option>";
                                        }
                                        ?>
                                    </select>
                                </div>
                                <div class="col-lg-3 pad text-right">
                                    <label class="label-control marg">Total Actual Qty.</label>
                                </div>
                                <div class="col-lg-2 pad">
                                    <input type="number" step="any" name="totactpcs" class="form-control form text-right totactpcs" value="<?php echo ($addflag != 1) ? $get['totactpcs'] : ""; ?>" readonly>
                                </div>
                                <div class="col-lg-1 pad">
                                </div>
                                <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 pad">
                                    <input type='button' class="btn btn-primary packmodal form" value='Packing Material'>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-right pad">
                                    <label class="label-control marg">Weight By</label>
                                </div>
                                <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3 pad">
                                    <select name="weightby" class="form-control form weightby" style='width:90%' >
                                        <option value="<?php echo ($addflag != 1) ? $mwei['accode'] : ""; ?>"><?php echo ($addflag != 1) ? $mwei['name'] : " "; ?></option>
                                        <?php
                                        $mown4 = mysqli_query($conn, "SELECT `accode`, `name` FROM `accmast` where `grpcode`='040' ORDER BY name") or die(mysqli_error($conn));
                                        while ($trow4 = mysqli_fetch_array($mown4)) {
                                            echo "<option value='$trow4[accode]'>$trow4[name]</option>";
                                        }
                                        ?>
                                    </select>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-lg-1 pad text-right">
                                    <label class="label-control marg">Remarks</label>
                                </div>
                                <div class="col-lg-6 pad">
                                    <input type="text" name="remark1" class="form-control form " value="<? Echo ($addflag != 1) ? $get["rem1"] : ""; ?>" maxlength="50">
                                </div>
                                <div class="col-lg-1 pad text-right"></div>
                                <div class="col-lg-6 pad">
                                    <input type="text" name="remark2" class="form-control form " value="<? Echo ($addflag != 1) ? $get["rem2"] : ""; ?>" maxlength="50">
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-lg-1 text-center">
                                    <a id='print' class="btn btn-pad btn-square" href="raw_print.php?vouchno=<?= $getvouch; ?>" target="_blank">
                                        Raw Print
                                    </a>
                                    </td>
                                </div>
                                <div class="col-lg-1 text-center">
                                    <a id='print' class="btn btn-pad btn-square" href="pack_print.php?vouchno=<?= $getvouch; ?>" target="_blank">
                                        Pack Print
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 pad">
                <div class="panel panel-default">
                    <div class="panel-body">
                        <?php include("includes/buttons.php"); ?>
                    </div>
                </div>
            </div>
        </div>
        <div class="modal fade rawModal" 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">RAW MATERIAL</h4>
                        <label style="color:#6082BF" class="prodname modal-title"></label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <label style="color:#6082BF" class="modal-title sbatch"></label>
                    </div>
                    <div class="modal-body" style="overflow-y: scroll">
                        <div class="table-responsive rawdtl">
                            <?php
                            if ($addflag != 1) {
                                echo "<table class='table table-striped table-condensed table-bordered'  style='width: 100%'>";
                                echo "<thead>";
                                echo"<tr style='background-color: #6082BF;'>";
                                echo "<th width='3%'></th>";
                                echo "<th width='6%'>Sr</th>";
                                echo"<th width='25%'>Ingredients</th>";
                                echo"<th width='15%'>Unit</th>";
                                echo"<th width='8%'>Qty. %</th>";
                                echo"<th width='8%'>Qty. Per Batch in kg.</th>";
                                echo"<th width='8%'>Issued Qty.</th>";
                                echo"<th width='8%'>AR.No.</th>";
                                echo"<th width='8%'>ID.No.</th>";
                                echo"</tr>";
                                echo"</thead>";
                                echo"<tbody class='tbody3'>";
                                $ctr3 = 0;
                                $gettran = mysqli_query($conn, "SELECT * FROM `$trandb` WHERE `vouchno`='$getvouch' ORDER BY `srno`")or die(mysqli_error($conn));
                                while ($tran3 = mysqli_fetch_array($gettran)) {

                                    $getitem = mysqli_query($conn, "SELECT code, name FROM `item` where `code` = '$tran3[itemcode]'")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%' readonly>";
                                    Echo "<option value='$fitem[code]'>$fitem[name]</option>";
                                    $getitm = mysqli_query($conn, "SELECT code, name FROM `item` order by name")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%' readonly>";
                                    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 qtyperc'   name='qtyperc_$ctr3'  step='any'   value='$tran3[qtyperc]' readonly></td>";
                                    Echo "<td><input type='number' class='form-control form text-right qtybatch'   name='qtybatch_$ctr3'  step='any'   value='$tran3[qtybatch]' readonly></td>";
                                    Echo "<td><input type='number' class='form-control form text-right issuedqty'   name='issuedqty_$ctr3'  step='any'   value='$tran3[issuedqty]'></td>";
                                    Echo "<td><input type='text' class='form-control form  arno'   name='arno_$ctr3'  step='any'   value='$tran3[arno]'></td>";
                                    Echo "<td><input type='text' class='form-control form  idno'   name='idno_$ctr3'  step='any'   value='$tran3[idno]'></td>";
                                    Echo "</tr>";
                                    $ctr3++;
                                }
                                echo "</tbody>";
                                echo "<input type='hidden' id='items3' name='items3' value='$ctr3'>";
                                echo "</table>";
                            }
                            ?>
                        </div>
                        <div class='row'>
                            <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>
                        </div>
                        <div class='row'>
                            <div class='col-lg-12 col-xs-12 pad'>
                                <div class='col-lg-3 pad'></div>
                                <!--<div class='col-lg-3 pad'></div>-->
                                <div class='col-lg-3 pad text-right'>
                                    <label class='label-control marg'>Total Qty.</label>
                                </div>
                                <div class='col-lg-2 pad'>
                                    <input type='number' step='any' name='totpercqty' class='form-control form text-right totpercqty' value='<?php echo ($addflag != 1) ? $get['totpercqty'] : ''; ?>' readonly>
                                </div>
                                <div class='col-lg-2 pad'>
                                    <input type='number' step='any' name='totbqty' class='form-control form text-right totbqty' value='<?php echo ($addflag != 1) ? $get['totbqty'] : ''; ?>' readonly>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">

            </div>
        </div>
        <div class="modal fade packModal" 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">PACKING MATERIAL</h4>
                        <label style="color:#6082BF" class="prodname modal-title"></label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                        <label style="color:#6082BF" class="modal-title sbatch"></label>
                    </div>
                    <div class="modal-body" style="overflow-y: scroll">
                        <div class="table-responsive packdtl">
                            <?php
                            if ($addflag != 1) {
                                echo "<table class='table table-striped table-condensed table-bordered'  style='width: 100%'>";
                                echo "<thead>";
                                echo"<tr style='background-color: #6082BF;'>";
                                echo "<th width='3%'></th>";
                                echo "<th width='6%'>Sr</th>";
                                echo"<th width='25%'>Packing Material</th>";
                                echo"<th width='8%'>AR.No.</th>";
                                echo"<th width='8%'>Actual Qty.</th>";
                                echo"<th width='8%'>Issued Qty.</th>";
                                echo"</tr>";
                                echo"</thead>";
                                echo"<tbody class='tbody4'>";
                                $ctr4 = 0;
                                $gettran3 = mysqli_query($conn, "SELECT * FROM `$trandb3` WHERE `vouchno`='$getvouch' ORDER BY `srno`")or die(mysqli_error($conn));
                                while ($ptran = mysqli_fetch_array($gettran3)) {

                                    $getitem = mysqli_query($conn, "SELECT code, name FROM `packing` where `code` = '$ptran[itemcode]'")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='srno4[]'      value='$ptran[srno]' readonly></td>";
                                    Echo "<td>";
                                    Echo "<select class='form-control form packing select2' name='packing[]' style='width:100%' readonly>";
                                    Echo "<option value='$fitem[code]'>$fitem[name]</option>";
                                    $getitm = mysqli_query($conn, "SELECT code, name FROM `packing` order by name")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><input type='text' class='form-control form  arno'   name='arno[]'  step='any'   value='$ptran[arno]'></td>";
                                    Echo "<td><input type='number' class='form-control form text-right actqty'   name='actqty[]'  step='any'   value='$ptran[actqty]' readonly></td>";
                                    Echo "<td><input type='number' class='form-control form text-right issuedqty'   name='issuedqty[]'  step='any'   value='$ptran[issuedqty]'></td>";
                                    Echo "</tr>";
                                    $ctr4++;
                                }
                                echo "</tbody>";
                                echo "<input type='hidden' id='items4' name='items4' value='$ctr4'>";
                                echo "</table>";
                            }
                            ?>
                        </div>
                        <div class='row'>
<!--                            <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>-->
                        </div>
                        <div class='row'>
                            <!--                            <div class='col-lg-12 col-xs-12 pad'>
                                                            <div class='col-lg-3 pad'></div>
                                                            <div class='col-lg-3 pad'></div>
                                                            <div class='col-lg-3 pad text-right'>
                                                                <label class='label-control marg'>Total Qty.</label>
                                                            </div>
                                                            <div class='col-lg-2 pad'>
                                                                <input type='number' step='any' name='totpercqty' class='form-control form text-right totpercqty' value='<?php echo ($addflag != 1) ? $get['totpercqty'] : ''; ?>' readonly>
                                                            </div>
                                                            <div class='col-lg-2 pad'>
                                                                <input type='number' step='any' name='totbqty' class='form-control form text-right totbqty' value='<?php echo ($addflag != 1) ? $get['totbqty'] : ''; ?>' readonly>
                                                            </div>
                                                        </div>-->
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">

            </div>
        </div>
    </div>
</form>
<div class="modal fade searchModal bs-example-modal-lg" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
    <div class="modal-dialog modal-lg" style="margin-top: 15%">
        <div class="modal-content">
            <div class="modal-header">
                <button class="close" data-dismiss="modal"><span>&times;</span></button>
                <h4 class="modal-title" id="myLargeModalLabel">Search <? Echo $formname; ?></h4>
            </div>
            <div class="modal-body">
                <input type="hidden" value="<?php Echo $fseries ?>" class="form-control series">
                <input type="hidden" value="<?php Echo $get['vouchno']; ?>" class="form-control vouchno">
                <input type="hidden" value="<?php Echo $file ?>" class="form-control file">
                <input type="hidden" value="<?php Echo $para ?>" class="form-control para">
                <div class="row">
                    <div class="col-lg-2">
                        <select class="form-control sfield">
                            <option value="1">Voucher</option>
                            <option value="2">Date</option>
                            <option value="3">Batch No.</option>
                            <option value="4">Product</option>
                            <option value="5">Godown</option>
                        </select>
                    </div>
                    <div class="col-lg-2">
                        <select class="form-control soperator">
                            <option value="">Type of    Search</option>
                            <option value="%">Like</option>
                            <option value="=">Exactly</option>
                        </select>
                    </div>
                    <div class="col-lg-2">
                        <select class="form-control sortBy">
                            <option value="">Sort By</option>
                            <option value="1">Voucher</option>
                            <option value="2">Date</option>
                            <option value="3">Batch No.</option>
                            <option value="4">Product</option>
                            <option value="5">Godown</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="10%">Voucher</th>
                                <th width="10%">Date</th>
                                <th width="10%">Batch No.</th>
                                <th width="40%">Product</th>
                                <th width="20%">Godown</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 sprevious" 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>
