<?php
require_once("../includes/config.php");
if(isset($_REQUEST['vendor'])){ $vdr = $_REQUEST['vendor']; }else{ $vdr = ""; }
/////////check approval is applicable or not
$apply_app = getAnyDetails(base64_decode($_REQUEST['pid']), "apply_approval", "tabid", "tab_master", $link1);
///// get access location details
$access_loc = getAccessLocation($_SESSION['userid'], $link1);
////// if we want to Add Gate Entry
if (isset($_POST['saveButton'])) {

    if ($_POST['saveButton'] == 'Save') {   
        
	    $vendor_code = $_POST['vendor'];
        $loc_code1 = $_POST['location'];
        $vendor_name = getAnyDetails($vendor_code, 'vendor_name', 'vendor_code', 'vendor_master', $link1);
        ///// make a string token
        $messageIdent = md5($_POST['saveButton'] . $vendor_code . $loc_code1);
        //and check it against the stored value:
        $sessionMessageIdent = isset($_SESSION['DirectgateEntrySession']) ? $_SESSION['DirectgateEntrySession'] : '';
        if ($messageIdent != $sessionMessageIdent) {//if its different:
          		       
            //save the session var:
            $_SESSION['DirectgateEntrySession'] = $messageIdent; 
            //// check if approval is applicable then status will set to 3(PFA) otherwise 10 (Received)
            if ($apply_app == "Y") {
                $current_status = 3;
            } else {
                $current_status = 10;
            }
			////// refrence no flag //////
			if(($_POST['refrences']!="") && ($_REQUEST['entry_against']=="VR")){
				$ref_no_flag = $_POST['refrences'];
				$ref_date_flag = getAnyDetails($_POST['refrences'], 'entry_date', 'system_ref_no', 'return_master', $link1);
			}else{
				$ref_no_flag = "";
				$ref_date_flag = "";
			}
            //// initialize transaction parameters
            $flag = true;
            mysqli_autocommit($link1, false);
            $err_msg = "";
            //make ref no
            $rs = mysqli_fetch_array(mysqli_query($link1,"select max(temp_no) as no from po_master where from_location='$loc_code1'"));
            $c_nos = $rs['no'] + 1;
            $pad1 = str_pad($c_nos, 3, "0", STR_PAD_LEFT);
            //// make auto requestno.
            $request_no1 = $loc_code1 ."DGE".$pad1;

            $vendor_addr = mysqli_fetch_array(mysqli_query($link1,"SELECT billing_address FROM vendor_master where vendor_code='$vendor_code'"));
            $loc_addr = mysqli_fetch_array(mysqli_query($link1,"SELECT locationaddress FROM location_master where location_code='$loc_code1'"));
            
			/////insert data into po master table
            $sql_job = "INSERT INTO po_master set from_location='$loc_code1', to_location='$vendor_code',temp_no='$c_nos',system_ref_no='$request_no1',from_address='".$loc_addr['locationaddress']."',to_address='".$vendor_addr['billing_address']."',bill_address='".$loc_addr['locationaddress']."',ship_address='".$vendor_addr['billing_address']."', status='".$current_status."',entry_date='" . $today . "',entry_time='" . $currtime . "',entry_by='" . $_SESSION['userid'] . "',entry_ip='" . $ip . "', ref_no = '".$ref_no_flag."', ref_date = '".$ref_date_flag."' ";
			
            $res1 = mysqli_query($link1,$sql_job)or die("error1" . mysqli_error());
            
			//// check if query is not executed
            if (!$res1) {
                $flag = false;
                $err_msg = "Error Code1: " . mysqli_error($link1);
            }
                   
            ////// make gate entry no.
            $sql1 = "select max(temp_no) as no from gate_entry where location_code = '" . $loc_code1 . "'";
            $res1 = mysqli_query($link1, $sql1)or die("error2" . mysqli_error($link1));
            $row1 = mysqli_fetch_assoc($res1);
            $req_no = $row1['no'] + 1;
            $pad = str_pad($req_no, 3, "0", STR_PAD_LEFT);
            $request_no = "GENT/".$loc_code1."/".$pad;
            // insert in gate entry table \\
            $res2 = mysqli_query($link1, "INSERT INTO gate_entry set system_ref_no='" . $request_no . "',temp_no='" . $req_no . "',entry_date='" . $today . "',entry_time='" . $currtime . "',entry_by='" . $_SESSION['userid'] . "',entry_ip='" . $ip . "',location_code='" . $loc_code1 . "',vendor_code='" . $vendor_code . "',vendor_name='" . $vendor_name . "',vendor_address='" . $_POST['vendor_address'] . "',ref_no='" . $request_no1 . "',ref_date='" . $today . "',lr_no='" . $_POST['lr_no'] . "',no_of_inv='1',vehicle_no='" . $_POST['vehicle_no'] . "',driver_name='" . $_POST['driver_name'] . "',unload_date='" . $_POST['unload_date'] . "',transport_name='" . $_POST['transport_name'] . "',contact_person='" . $_POST['contact_person'] . "',pallet_weight='',no_of_box='" . $_POST['no_of_boxes'] . "',carton_weight='" . $_POST['carton_w'] . "',status='" . $current_status . "',remark='" . $_POST['remark'] . "'");
			
            //// check if query is not executed
            if (!$res2) {
                $flag = false;
                $err_msg = "Error Code2: " . mysqli_error($link1);
            }
//////// generate GRN on the basis of invoices
if($apply_app=="Y"){ $grn_status = 3;}else{ $grn_status = 6;}
    for ($i = 1; $i <= 1; $i++) {
        $sql_grn = "select max(temp_no) as no from grn_inv_master";
        $res_grn = mysqli_query($link1,$sql_grn)or die("error3" . mysqli_error());
        $row_grn = mysqli_fetch_array($res_grn);
        $req_no_grn = $row_grn['no'] + 1;
        $pad = str_pad($req_no_grn, 6, "0", STR_PAD_LEFT);
        $grn_no = "GRN/".$loc_code1."/".$pad; 
		
        $res3 = mysqli_query($link1,"insert into grn_inv_master set vendor_code='$vendor_code',location_code='$loc_code1', system_ref_no='$grn_no',temp_no='$req_no_grn',po_no='$request_no1',po_date='$today',status='".$grn_status."',inv_no='".$_POST['invoice_no']."',inv_date='".$_POST['invoice_date']."',vendor_name='".$vendor_name."',vendor_address='".$_POST['vendor_address']."',entry_date='".$today."',entry_time='".$currtime."',entry_by='".$_SESSION['userid']."',entry_ip='".$ip."',gate_entry_no='".$request_no."'")or die("error4" . mysqli_error());
    
		//// check if query is not executed
		if (!$res3) {
			$flag = false;
			$err_msg = "Error Code3: " . mysqli_error($link1);
		}
        
    }
    if (is_array($_POST['qty'])) {
        $qty = $_POST['qty'];
        $item_id = $_POST['partcode'];
        $price_id = $_POST['price'];
        $remark_id = $_POST['rmk'];
        $len = count($_POST['qty']);
        if ($len > 0) {

            for ($i = 0; $i < $len; $i++) {
                $item = $item_id[$i];
                $quan = $qty[$i];
                $price = $price_id[$i];
                $remark = $remark_id[$i];

                if ($item != "" && $quan != "0") {
                    $getpart = explode("~", $item);
                    $totval = ($price * $quan);
					
                    $res4 = mysqli_query($link1,"insert into po_data set system_ref_no='$request_no1',partcode='$getpart[0]',req_qty='$quan',pending_qty='$quan',price='$price',value='$totval',purchase_unit='".$getpart[1]."',schedule_date='$today',remark='$remark'")or die("error2" . mysqli_error());
					
					//// check if query is not executed
					if (!$res4) {
						$flag = false;
						$err_msg = "Error Code4: " . mysqli_error($link1);
					}
					/////// update in PO RETURN table ////////
					if($_REQUEST['entry_against']=="VR"){ ///// check vender return type start
						$res4_1 = mysqli_query($link1,"update return_data set po_qty =  po_qty + '" . $quan . "' where system_ref_no = '" . $_POST['refrences'] . "'  and partcode = '".$getpart[0]."' ")or die("error 2_1 " . mysqli_error());
						
						//// check if query is not executed
						if (!$res4_1) {
							$flag = false;
							$err_msg = "Error Code 4.1 : " . mysqli_error($link1);
						}
					}///// vender return type end
			
            	}// end if
            }//// end for loop
			
			if(($_POST['refrences']!="") && ($_REQUEST['entry_against']=="VR")){
			
				$som_of = mysqli_fetch_array(mysqli_query($link1, "SELECT SUM(return_qty) AS RQ, SUM(po_qty) AS PQ FROM return_data WHERE  system_ref_no  = '" . $_POST['refrences'] . "' "));
				////// change status of /////
			
				if($som_of['RQ']<=$som_of['PQ']){
					$res4_2 = mysqli_query($link1,"update return_master set status = '16' where system_ref_no = '" . $_POST['refrences'] . "' ")or die("error 2_2 " . mysqli_error());
					//// check if query is not executed
					if (!$res4_2) {
						$flag = false;
						$err_msg = "Error Code 4.2 : " . mysqli_error($link1);
					}
				}
			}//// end check system ref no
			
        }/// end if
    }//// end if
            ////// insert in activity table////
            
			$flag = dailyActivity($_SESSION['userid'], $request_no, "DIRECT GATE ENTRY", "ADD", $ip, $link1, $flag);
            
			///// check both master and data query are successfully executed
            if ($flag) {
                mysqli_commit($link1);
                $msg = "Direct Gate Entry is created with ref. no. " . $request_no;
                $cflag = "success";
                $cmsg = "Success";
            } else {
                mysqli_rollback($link1);
                $msg = "Request could not be processed " . $err_msg . ". Please try again.";
                $cflag = "danger";
                $cmsg = "Failed";
            }
            mysqli_close($link1);
        } else {
            //you've sent this already!
            $msg = "Re-submission is not allowed";
            $cflag = "warning";
            $cmsg = "Warning";
        }
        ///// move to parent page
        header("location:gate_entry_list.php?msg=" . $msg . "&chkflag=" . $cflag . "&chkmsg=" . $cmsg . "" . $pagenav);
        exit;
    }
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title><?= siteTitle ?></title>
        <link rel="shortcut icon" href="../images/titleimg.png" type="image/png">
        <script src="../js/jquery.js"></script>
<?php
include("../includes/fontawesome.html");
include("../includes/main_css.html");
include("../includes/bootstrap.html");
?><link rel="stylesheet" href="../css/datepicker.css">
        <script src="../js/bootstrap-datepicker.js"></script>
        <script>
            $(document).ready(function () {
                var spinner = $('#loader');
                $("#frm1").validate({
                    submitHandler: function (form) {
                        if (!this.wasSent) {
                            this.wasSent = true;
                            $(':submit', form).val('Please wait...')
                                    .attr('disabled', 'disabled')
                                    .addClass('disabled');
                            spinner.show();
                            form.submit();
                        } else {
                            return false;
                        }
                    }
                });
                $('.selectpicker').selectpicker({
                    //actionsBox: true,
                    //container: true,
                    liveSearch: true
                });
                $('.number').on('input', function () {
                    match = (/(\d{0,6})[^.]*((?:\.\d{0,6})?)/g).exec(this.value.replace(/[^\d.]/g, ''));
                    this.value = match[1] + match[2];
                });
                 $('#unload_date').datepicker({
		format: "yyyy-mm-dd",
		//startDate: "<?=$today?>",
                todayHighlight: true,
		autoclose: true
	});
         $('#invoice_date').datepicker({
		format: "yyyy-mm-dd",
		//startDate: "<?=$today?>",
                todayHighlight: true,
		autoclose: true
	});
			//// find the vender addreass/////
			getBillingAdd1();
	
            });
           
            function makedropdown() {
                $('.selectpicker').selectpicker({
                    liveSearch: true
                });               
            }
            /////// add new row 
            $(document).ready(function () {
                $("#add_row").click(function () {
                    var numi = document.getElementById('rowno');
                    var itm = "partcode[" + numi.value + "]";
                    var preno = document.getElementById('rowno').value;
                    var num = (document.getElementById("rowno").value - 1) + 2;
                    numi.value = num;
                    if ((document.getElementById(itm).value != "") || ($("#addr1_" + numi.value + ":visible").length == 0)) {
                        var r = '<tr id="addr1_' + num + '"><td width="40%"><div style="display:inline-block;float:right"><select name="partcode[' + num + ']" id="partcode[' + num + ']" class="form-control required selectpicker show-tick" required data-width="350px" onChange="getunit(this.value,' + num + ')"><option value="">--Select Part--</option><?php
$res_pro = mysqli_query($link1, "select partcode,part_name,bom_unit,purchase_unit from partcode_master where itemtypeid not in ('1','4') and status='1' order by part_name")or die(mysqli_error($link1));
while ($row_pro = mysqli_fetch_assoc($res_pro)) {
    ?><option value="<?= $row_pro['partcode'] . "~" . $row_pro['purchase_unit'] ?>"><?= $row_pro['part_name'] . " (" . $row_pro['partcode'] . ")" ?></option><?php } ?></select></div><div style="display:inline-block;float:left;"><i class="fa fa-close fa-lg" onClick="fun_remove(' + num + ');"></i></div></td><td width="15%"><input type="text" name="qty[' + num + ']" class="form-control number required" id="qty[' + num + ']"/></td><td width="15%"><input type="text" name="price[' + num + ']" id="price[' + num + ']" class="form-control number required" required/></td><td width="15%"><span id="unit' + num + '"></span></td><td width="30%"><textarea name="rmk[' + num + ']" id="rmk[' + num + ']" rows="1" class="form-control addressfield"></textarea></td></tr>';
                        $('#itemsTable1').append(r);
                        makedropdown();
                    }
                });
            });
            function fun_remove(con) {
                var c = document.getElementById('addr1_' + con);
                c.parentNode.removeChild(c);
                con--;
                document.getElementById('rowno').value = con;
            }
            function getunit(val, indx) {
                var split_val = val.split("~");
                document.getElementById("unit" + indx).innerHTML = split_val[1];
                checkDuplicatePart(indx, split_val[0]);
            }

            ///// function for checking duplicate partcode
            function checkDuplicatePart(fldIndx1, val) {
                if (val != '') {
                    var check2 = "partcode[" + fldIndx1 + "]";
                    var flag = 1;
                    for (var i = 0; i <= fldIndx1; i++) {
                        var check1 = "partcode[" + i + "]";
                        if (fldIndx1 != i && document.getElementById(check2).value != '' && document.getElementById(check1).value != '') {
                            if ((document.getElementById(check2).value == document.getElementById(check1).value)) {
                                document.getElementById("error_msg").innerHTML = "Duplicate Part selection";
                                document.getElementById(check2).value = "";
                                flag *= 0;
                            } else {
                                document.getElementById("error_msg").innerHTML = "";
                                //document.getElementById(check2).className="form-control required selectpicker show-tick";
                                flag *= 1;
                                ///do nothing
                            }
                        }
                    }//// close for loop
                    if (flag == 0) {
                        return false;
                    } else {
                        return true;
                    }
                }
            }
            ////////////////// Calculate Tax/////////////////////
            function checkAllRows() {
                var rowno1 = document.getElementById("rowno").value;
                var flag = 1;
                if (rowno1 > 0) {
                    for (var j = 0; j < rowno1; j++) {
                        var check2 = "partcode[" + j + "]";
                        for (var i = 0; i <= rowno1; i++) {
                            if (j != i) {
                                var check1 = "partcode[" + i + "]";
                                var rqty = "qty[" + i + "]";
                                var runit = "unit" + i + "";
                                var rpurpose = "purpose[" + i + "]";
                                if (document.getElementById(check2).value == document.getElementById(check1).value) {
                                    document.getElementById("error_msg").innerHTML = "Duplicate Part selection";
                                    document.getElementById(check1).value = "";
                                    document.getElementById(rqty).value = '';
                                    document.getElementById(runit).innerHTML = '';
                                    document.getElementById(rpurpose).value = "";
                                    flag *= 0;
                                } else {
                                    //document.getElementById(defQty).value='1';
                                    flag *= 1;
                                }
                            }
                        }
                    }
                    //////////
                    if (flag == 0) {
                        return false;
                    } else {
                        return true;
                    }
                }
            }

            //get vendor address location address
            function getBillingAdd(val) {
                  //alert(val);
                $.ajax({
                    type: 'post',
                    data: {vendorcode: val, billing_add: "toadd"},
                    url: '../pagination/gate-pass_grid_data.php',
                    success: function (data) {
                        document.getElementById("vendor_address").value = data;
                    }
                });
            }
			//get vendor address location address
            function getBillingAdd1() {
				var val = document.getElementById("vendor").value;
                $.ajax({
                    type: 'post',
                    data: {vendorcode: val, billing_add: "toadd"},
                    url: '../pagination/gate-pass_grid_data.php',
                    success: function (data) {
                        document.getElementById("vendor_address").value = data;
                    }
                });
            }
            ///// every time transfer is change then form should be reload
            function checkTransferFrom() {
                var locval = $("#location").val();
                var vendval = $("#vendor").val();
                
                if (locval != "" && vendval !="") {
                    $("#frm1").find("input, select, textarea, button").removeAttr("disabled");
                }
            }
            ///// check request to and request from should not be same
            function checkFromTo() {
                var reqfrom = $("#location").val();
                var reqto = $("#vendor").val();
                /////// check wheather request from and request to are both selected or not
                if (reqfrom == "" || reqto == "") {
                    $("#frm1").find("input:enabled, select:enabled, textarea:enabled, button:enabled").attr("disabled", "disabled");
                    $("#location").removeAttr("disabled");
                    $('#vendor').removeAttr("disabled");
                    $("#vendor_address").removeAttr("disabled");
                    $("#back").removeAttr("disabled");
                } else {
                    $("#frm1").find("input, select, textarea, button").removeAttr("disabled");
                }
            }
			////// find refrence drop down //////
			function getRefrenceDD(val){
				if(val == "VR"){
					document.getElementById("refDiv").style.display = "block";
				}else{
					document.getElementById("vendor").value = "";
					document.getElementById("refrences").value = "";
					document.getElementById("refDiv").style.display = "none";
				}
			}
			
			
        </script>        
        <script src="../js/bootstrap-select.js"></script>
        <link href="../css/bootstrap-select.css" rel="stylesheet"/>
        <script src="../js/jquery.validate.js"></script>
        <link href="../css/loader.css" rel="stylesheet"/>
    </head>
    <body onLoad="checkFromTo();">
        <div class="container-fluid">
            <div class="row content">
                <?php
                include("../includes/leftnav_admin.php");
                ?>
                <div class="<?= $screenwidth ?>">
                    <h2 align="center"><i class="fa <?= $fa_icon ?>"></i> Add Direct Gate Entry </h2><br/>
                    <h4 align="center"><span id="error_msg" class="red_small" style="text-align:center"></span></h4>
                    <div class="form-group"  id="page-wrap" style="margin-left:10px;">
                        <form id="frm1" name="frm1" class="form-horizontal" action="" method="post">
                            <div class="form-group">
                                <div class="col-md-6">
                                    <label class="col-md-6 control-label">Vendor Name  <span class="red_small">*</span></label>
                                    <div class="col-md-6">
                                        <select name="vendor" id="vendor" class="form-control required custom-select" onChange="getBillingAdd(this.value);checkFromTo();checkTransferFrom();" required>
                                            <option value ="">--Please Select--</option>
<?php
$sql1 = "SELECT vendor_code,vendor_name FROM vendor_master order by vendor_name";
$res1 = mysqli_query($link1, $sql1);
while ($row1 = mysqli_fetch_array($res1)) {
    ?>
                                                <option value="<?= $row1['vendor_code'] ?>"<?php
    if ($vdr) {
        if ($vdr == $row1['vendor_code']) {
            echo "selected";
        }
    }
    ?>><?php echo $row1['vendor_name'] ?></option>
<?php } ?>
                                        </select>
                                    </div>
                                </div>
                                <div class="col-md-6">
                                    <label class="col-md-6 control-label">Vendor Address <span class="red_small">*</span></label>
                                    <div class="col-md-6">
                                        <textarea name="vendor_address" id="vendor_address" class="form-control addressfield required" style="resize:vertical"></textarea>
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-md-6">
                                    <label class="col-md-6 control-label">Location Name <span class="red_small">*</span></label>
                                    <div class="col-md-6">
                                        <select name="location" id="location" class="form-control required custom-select" required  onChange="checkFromTo();
                                                checkTransferFrom();">
                                            <option value="">--Please Select--</option>
<?php
$sql1 = "SELECT location_code,locationname FROM location_master where statusid= '1' and location_code in (" . $access_loc . ") order by locationname";
$res1 = mysqli_query($link1, $sql1);
while ($row1 = mysqli_fetch_array($res1)) {
    ?>
                                                <option value="<?= $row1['location_code'] ?>"<?php
    if (isset($_REQUEST['location'])) {
        if ($_REQUEST['location'] == $row1['location_code']) {
            echo "selected";
        }
    }
    ?>><?php echo $row1['locationname'] ?></option>
<?php } ?>
                                        </select>
                                    </div>
                                </div>
                                <div class="col-md-6">
									<label class="col-md-6 control-label">Entry Against <span class="red_small">*</span></label>
									<div class="col-md-6">
										<select name="entry_against" id="entry_against" class="form-control required" required onChange="document.frm1.submit();getRefrenceDD(this.value);getBillingAdd1();" >
											
											<option value="OTH" <?php if($_REQUEST['entry_against']=="OTH"){ echo "selected"; } ?> > Other </option>
											<option value="VR" <?php if($_REQUEST['entry_against']=="VR"){ echo "selected"; } ?> > Vendor Return </option>
										</select>
									</div>
                                </div>
                            </div>
							
							<div class="form-group">
                                <div class="col-md-6">
									<div id="refDiv" <?php if($_REQUEST['entry_against']!="VR"){ echo " style='display:none;' "; } ?> >
										<label class="col-md-6 control-label">Refrence <span class="red_small">*</span></label>
										<div class="col-md-6">
											<select name="refrences" id="refrences" class="form-control required" required onChange="document.frm1.submit();getBillingAdd1();" >
												<option value="" <?php if($_REQUEST['refrences']==""){ echo "selected"; } ?> > -- Please Select -- </option>
												<?php
													$dd = mysqli_query($link1, "SELECT system_ref_no FROM return_master WHERE status = '6' ");
													if(mysqli_num_rows($dd)>0){
													while($r = mysqli_fetch_array($dd)){
												?>
												<option value="<?=$r['system_ref_no'];?>" <?php if($_REQUEST['refrences']==$r['system_ref_no']){ echo "selected"; } ?> ><?=$r['system_ref_no'];?></option>
												<?php }} ?>
											</select>
										</div>
									</div>
                                </div>
                                <div class="col-md-6">
									
                                </div>
                            </div>

                            <div class="form-group">
                                <div class="col-md-12">
                                    <table class="table table-bordered" width="100%" id="itemsTable1">
                                        <thead>
                                            <tr class="<?= $tableheadcolor ?>">
                                                <th width="40%">Product</th>
                                                <th width="15%">Qty</th>
                                                <th width="15%">Price</th>
                                                <th width="15%">Purchase Unit</th>
                                                <th width="15%">Remark</th>
                                            </tr>
                                        </thead>
                                        <tbody>
											<?php 
												$k = 0;
												if($_REQUEST['entry_against']=="VR"){												
												$data_qr = mysqli_query($link1, "SELECT * FROM return_data where system_ref_no =  '" . $_REQUEST['refrences'] . "' and (return_qty - po_qty)>0 ");
												if(mysqli_num_rows($data_qr)>0){
												while($dqr = mysqli_fetch_array($data_qr)){
											?>
											
											<tr id="addr1_<?=$k?>">
                                                <td width="40%">
                                                    <select name="partcode[<?=$k?>]" id="partcode[<?=$k?>]" class="form-control required selectpicker show-tick" required onChange="getunit(this.value, 0);">
                                                        
<?php
$res_pro = mysqli_query($link1, "select partcode,part_name,bom_unit,purchase_unit from partcode_master where itemtypeid not in ('1','4') and status='1' and partcode='".$dqr['partcode']."'  order by part_name")or die(mysqli_error($link1));
while ($row_pro = mysqli_fetch_assoc($res_pro)) {
    ?>
                                                            <option value="<?= $row_pro['partcode'] . "~" . $row_pro['purchase_unit'] ?>" <?php if($_REQUEST['partcode']==$dqr['partcode']){ echo "selected"; } ?> ><?= $row_pro['part_name'] . " (" . $row_pro['partcode'] . ")" ?></option>
<?php } ?>
                                                    </select></td>
                                                <td width="15%">
                                                    <input type="text" name="qty[<?=$k?>]" id="qty[<?=$k?>]" class="form-control number required" value="<?=($dqr['return_qty']-$dqr['po_qty'])?>" required/>
                                                </td>
                                                <td width="15%">
                                                    <input type="text" name="price[<?=$k?>]" id="price[<?=$k?>]" class="form-control number required" value="<?=$dqr['price']?>" required/>
                                                </td>
                                                <td width="15%">
                                                    <span><?=$dqr['purchase_unit']?></span>
                                                </td>
                                                <td width="15%">
                                                    <textarea name="rmk[<?=$k?>]" id="rmk[<?=$k?>]" rows="1" class="form-control addressfield"><?=$dqr['inv_remark']?></textarea>
                                                </td>

                                            </tr>
											
											<?php	
												$k++; }}}else{
											?>
                                            <tr id="addr1_0">
                                                <td width="40%">
                                                    <select name="partcode[0]" id="partcode[0]" class="form-control required selectpicker show-tick" required onChange="getunit(this.value, 0);">
                                                        <option value="">--Select Part--</option>
<?php
$res_pro = mysqli_query($link1, "select partcode,part_name,bom_unit,purchase_unit from partcode_master where itemtypeid not in ('1','4') and status='1' order by part_name")or die(mysqli_error($link1));
while ($row_pro = mysqli_fetch_assoc($res_pro)) {
    ?>
                                                            <option value="<?= $row_pro['partcode'] . "~" . $row_pro['purchase_unit'] ?>"><?= $row_pro['part_name'] . " (" . $row_pro['partcode'] . ")" ?></option>
<?php } ?>
                                                    </select></td>
                                                <td width="15%">
                                                    <input type="text" name="qty[0]" id="qty[0]" class="form-control number required" required/>
                                                </td>
                                                <td width="15%">
                                                    <input type="text" name="price[0]" id="price[0]" class="form-control number required" required/>
                                                </td>
                                                <td width="15%">
                                                    <span id="unit0"></span>
                                                </td>
                                                <td width="15%">
                                                    <textarea name="rmk[0]" id="rmk[0]" rows="1" class="form-control addressfield"></textarea>
                                                </td>

                                            </tr>
											<?php } ?>
                                        </tbody>
                                    </table>
                                </div>
                            </div>
							
							<?php if($_REQUEST['entry_against']!="VR"){ ?>
                            <div class="form-group">
                                <div class="col-md-4" style="display:inline-block; float:left">
                                    <button class='btn<?= $btncolor ?>' id="add_row" type="button" name="add" value="Add"><i class="fa fa-plus-circle fa-lg"></i>&nbsp;&nbsp;Add More Row</button>
                                    <input type="hidden" name="rowno" id="rowno" value="0"/>
                                </div>
                                <div class="col-md-6"></div>

                            </div>
							<?php } ?>
							
                            <div class="form-group">
                                <div class="col-md-6"><label class="col-md-6 control-label">Invoice No./Bill No. </label>
                                    <div class="col-md-6">
                                        <input type="text" name="invoice_no" id="invoice_no" class="form-control" disabled="disabled">
                                    </div>
                                </div>
                                <div class="col-md-6"><label class="col-md-6 control-label">Invoice Date <span class="red_small">*</span></label>
                                    <div class="col-md-6">
                                        <input type="text" name="invoice_date" id="invoice_date" class="form-control required" disabled="disabled">
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-md-6"><label class="col-md-6 control-label">L.R. No. <span class="red_small">*</span></label>
                                    <div class="col-md-6">
                                        <input type="text" name="lr_no" id="lr_no" class="form-control required" disabled="disabled">
                                    </div>
                                </div>
                                <div class="col-md-6"><label class="col-md-6 control-label">Vehicle No. <span class="red_small">*</span></label>
                                    <div class="col-md-6">
                                        <input type="text" name="vehicle_no" id="vehicle_no" class="form-control required" disabled="disabled">
                                    </div>
                                </div>

                            </div>
                            <div class="form-group">                              
                                <div class="col-md-6"><label class="col-md-6 control-label">Transport Name <span class="red_small">*</span></label>
                                    <div class="col-md-6">
                                        <input type="text" name="transport_name" id="transport_name" class="form-control required" disabled="disabled">
                                    </div>
                                </div>
                                <div class="col-md-6"><label class="col-md-6 control-label">No. of Boxes <span class="red_small">*</span></label>
                                    <div class="col-md-6">
                                        <input type="text" name="no_of_boxes" id="no_of_boxes" class="form-control required number" disabled="disabled">
                                    </div>
                                </div>

                            </div>
                            <div class="form-group">
                                <div class="col-md-6"><label class="col-md-6 control-label">Unload Date <span class="red_small">*</span></label>
                                    <div class="col-md-6">
                                        <input type="text" name="unload_date" id="unload_date" class="form-control required" disabled="disabled">
                                    </div>
                                </div>
                                <div class="col-md-6"><label class="col-md-6 control-label">Carton Weight <span class="red_small">*</span></label>
                                    <div class="col-md-6">
                                        <input type="text" name="carton_w" id="carton_w" class="form-control required">
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-md-6"><label class="col-md-6 control-label">Driver Name <span class="red_small">*</span></label>
                                    <div class="col-md-6">
                                        <input type="text" name="driver_name" id="driver_name" class="form-control required" disabled="disabled">
                                    </div>
                                </div>
                                <div class="col-md-6"><label class="col-md-6 control-label">Contact Person <span class="red_small">*</span></label>
                                    <div class="col-md-6">
                                        <input type="text" name="contact_person" id="contact_person" class="form-control required" disabled="disabled">
                                    </div>
                                </div>
                            </div>
                            <div class="form-group">                              
                                <div class="col-md-6"><label class="col-md-6 control-label">Remark:</label>
                                    <div class="col-md-6">
                                        <textarea name="remark" id="remark" rows="2" class="form-control addressfield"></textarea>
                                    </div>
                                </div>
                                <div class="col-md-6"> </div>
                            </div>

                            <div class="form-group">
                                <div class="col-md-5"></div>
                                <div class="col-md-7" style="display:inline-block; float:right" align="left">
                                    <button class='btn<?= $btncolor ?>' id="save" type="submit" name="saveButton" value="Save" onClick="checkAllRows();"><i class="fa fa-save fa-lg"></i>&nbsp;&nbsp;Save</button>
                                    <button title="Back" id="back" type="button" class="btn<?= $btncolor ?>" onClick="window.location.href = 'gate_entry_list.php?status=<?php if (isset($_REQUEST['status'])) {
    echo $_REQUEST['status'];
} ?>&request_from=<?php if (isset($_REQUEST['request_from'])) {
    echo $_REQUEST['request_from'];
} ?>&request_to=<?php if (isset($_REQUEST['request_to'])) {
    echo $_REQUEST['request_to'];
} ?>&daterange=<?php if (isset($_REQUEST['daterange'])) {
    echo $_REQUEST['daterange'];
} ?><?= $pagenav ?>'"><i class="fa fa-reply fa-lg"></i>&nbsp;&nbsp;Back</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
        <div id="loader"></div>

<?php
include("../includes/footer.php");
include("../includes/connection_close.php");
?>
    </body>
</html>