<style>
  .ui-dialog.ui-corner-all.ui-widget.ui-widget-content.ui-front.ui-draggable.ui-resizable {
    position: absolute!important;
    height: auto!important;
    width: 946px!important;
    top: 190px!important;
    left: 274.5px!important;
    z-index: 101!important;
}
.table-bordered, .table-bordered td, .table-bordered th {
    border: 1px solid #626e82;
}
th {
    white-space: break-spaces;
    padding: 5px;
}
td{
  padding: 5px;
}
</style>
<div class="container">
    
    <?php
    if(!empty($prd_req)){
    
    foreach($prd_req as $preq) ?>
		<form class="form" autocomplete="off" action="<?php echo site_url('ProductRequest/addSplOrdIssue');?>" method="post" enctype="multipart/form-data">
			<div class="row justify-content-md-right ">
				<div class="col-md-3">
				<div class="form-group">
					<label>RO.NOs</label>
					<input type="text" name="order_no" id="order_no" class="form-control" value="<?php echo $preq->pr_req_no; ?>" readonly />
				</div>
				</div>
			<div class="col-md-3">
				<div class="form-group">
					<label>RO DATE</label>
					<input type="text" name="order_date"  id="order_date" class="form-control" readonly="readonly" value="<?php echo $preq->request_date; ?>"/>
				</div>
			</div>
			<div class="col-md-3">
				<div class="form-group">
					<label>Issue DATE</label>
					<?php $cur_date = date("d-m-Y"); ?>
						<input class="form-control" type="text" name="issue_date5"  id="issue_date5" value="<?php echo $cur_date; ?>" readonly >
				</div>
			</div>
			<div class="col-md-3">
				<div class="form-group">
					<label>Department</label>
					<?php 
					$uname5 = $_SESSION['username'];
					$br5 = $preq->branchname;
					
					$dept_uname = $preq->dept;
					$q2 = $this->db->query("select * from user where branch_name='$br5'");
					$r = $q2->row_array();
					
					$q3 = $this->db->query("select * from user where username='$dept_uname'");
					$r1 = $q3->row_array();?>
					<input class="form-control" type="text" name="dept5" id="dept5" value="<?php echo $r['username']; ?>" readonly >
					<input class="form-control" type="hidden" name="outlet_branch5" id="outlet_branch5" value="<?php echo $preq->branchname; ?>" >
					<input name="req_id5" id="req_id5" type="hidden" class="textbox" value="<?php echo $preq->request_id; ?>">
					<input type="hidden" name="branchname5" id="branchname5" class="form-control" value="<?php echo $r1['branch_name']; ?>"/>
					<input type="hidden" name="branch_id5" id="branch_id5" class="form-control" value="<?php echo $_SESSION['branch_id']?>"/>
					<input type="hidden" name="username5" id="username5" class="form-control" value="<?php echo $_SESSION['username']?>"/>
					<input type="hidden" name="uname5" id="uname5" class="form-control" value="<?php echo $_SESSION['full_name']?>"/>
				</div>
			</div>
		</div>
			
		
				
		<?php
	$req_id=$preq->request_id;
	$dept_branch=$r1['branch_name'];
	$count = 1;
	$q1=$this->db->query("SELECT *, (PI.required_qty-PI.recieved_qty) as item_balance_qty FROM product_request P inner join product_request_item PI on P.request_id=PI.request_id inner join recipe R on PI.item_name=R.rname where PI.request_id='$req_id' and R.branchname='$dept_branch'");
	$req_res = $q1->result();
    foreach($req_res as $req_row) { 
	
	?>
		<div class="row justify-content-md-right">
		
			<div class="col-md-5">
				<div class="row">
					<div class="col-md-8">
						<div class="form-group">
							<input class="form-control" type="text" name="iname5[]" id="iname5" value="<?php echo $req_row->item_name; ?>" readonly />
						</div>
					</div>
					<div class="col-md-4">
						<div class="form-group">
							<input class="form-control" type="text" name="rqty5[]"  id="rqty5" value="<?php echo $req_row->required_qty; ?>" readonly />
						</div>
					</div>
				</div>
			</div>
						
			<?php
				if($req_row->required_qty == $req_row->recieved_qty) {
				if(($uname5 == 'Savories Kitchen' && $r['username'] == 'counter') || ($uname5 == 'CK_CW3' && $r['username'] == 'cw3-counter')) {
			?>
				<div class="col-md-7">
					<div class="row">
						<input class="item_id5" type="hidden" name="req_item_id5[]" id="req_item_id5" value="<?php echo $req_row->request_item_id;?>" >
						<input class="item_id5" type="hidden" name="item_name5[]" id="item_name5" value="<?php echo $req_row->item_name;?>" >
						<input class="item_id5" type="hidden" name="req_qty5[]" id="req_qty5" value="<?php echo $req_row->required_qty;?>" >
						<input class="item_id5" type="hidden" name="item_id5[]" id="item_id5" value="<?php echo $req_row->recipe_id;?>" >
						<input class="item_id5" type="hidden" name="recipe_no5[]" id="recipe_no5" value="<?php echo $req_row->recipe_no;?>" >
						<input class="yeild5" type="hidden" name="yeild5[]" id="yeild5" value="<?php echo $req_row->yeild;?>">
						<input class="item_id5" type="hidden" name="uom5[]" id="uom5" value="<?php echo $req_row->uom;?>">
						<input class="item_id5" type="hidden" name="cat_id5[]" id="cat_id5" value="<?php echo $req_row->pr_cat_id;?>">
						<input class="qty5 form-control col-md-2 comm" type="text" name="inward_quantity5[]" id="inward_quantity5"  placeholder="Qty"   data-uk-tooltip="{pos:'bottom'}" title=" Remaining Qty :  <?php echo $req_row->item_balance_qty; ?>"  />
						<?php if($r['username'] == 'CW4-Counter' || $r['username'] == 'Santhe4') { ?>
						<input class="form-control price5" type="hidden" name="price5[]"  id="price5" value="<?php echo $req_row->cw4_price; ?>" />
						<input class="form-control tax5" type="hidden" name="tax5[]"  id="tax5" value="<?php echo $req_row->cw4_tax; ?>" />
						<input class="form-control basic_tax5" type="hidden" name="basic_tax5[]" id="basic_tax5" value="<?php echo $req_row->cw4_tax_amount; ?>" />
						<?php } else if($r['username'] == 'CW5-Counter' || $r['username'] == 'Santhe5') { ?>
						<input class="form-control price5" type="hidden" name="price5[]"  id="price5" value="<?php echo $req_row->cw5_price; ?>" />
						<input class="form-control tax5" type="hidden" name="tax5[]"  id="tax5" value="<?php echo $req_row->cw5_tax; ?>" />
						<input class="form-control basic_tax5" type="hidden" name="basic_tax5[]" id="basic_tax5" value="<?php echo $req_row->cw5_tax_amount; ?>" />
						<?php } else { ?>
						<input class="form-control price5" type="hidden" name="price5[]"  id="price5" value="<?php echo $req_row->cw_price; ?>" />
						<input class="form-control tax5" type="hidden" name="tax5[]"  id="tax5" value="<?php echo $req_row->cw_tax; ?>" />
						<input class="form-control basic_tax5" type="hidden" name="basic_tax5[]" id="basic_tax5" value="<?php echo $req_row->cw_tax_amount; ?>" />
						<?php } ?>
						<input class="form-control tot_basic5" type="hidden" name="tot_basic5[]"  id="tot_basic5" />
						<input class="form-control tax_amt5" type="hidden" name="tax_amt5[]" id="tax_amt5" />
						<input class="form-control tot_price5" type="hidden" name="total_price5[]"  id="total_price5" />
						<input class="form-control stk5" type="hidden" name="stock5[]" id="stock5" value="<?php echo $req_row->production_stock; ?>" data-uk-tooltip="{pos:'bottom'}" title="Present Stock" readonly/>
						<input class="grn_balance5 form-control col-md-2 comm" type="text" name="received_qty5[]" id="received_qty5" value="<?php echo $req_row->recieved_qty; ?>" data-uk-tooltip="{pos:'bottom'}" title="Issued Qty" readonly />
						<input class="grn_balance5 qty_pending5" type="hidden" name="bal_item_qty5[]"  id="bal_item_qty5" value="<?php echo $req_row->item_balance_qty; ?>" />
						<input class="remarks form-control col-md-2 comm" type="text" name="remarks5[]" id="remarks5[]" placeholder="Remarks"/>
					</div>		
				</div>
				<?php } else { ?>
				<div class="col-md-7">
					<div class="row">
						<div class="col-md-3">
							<div class="form-group">
								<input class="form-control" type="text" name="iqty"  id="iqty[]" value="<?php echo $req_row->required_qty; ?>" placeholder ="Qty" disabled />
							</div>
						</div>
						<div class="col-md-1">
							<div class="form-group">
								<img src="<?php echo base_url(); ?>app-assets/images/right.png" alt="" class="img_main"/>
							</div>
						</div>
						<div class="col-md-2">
							<div class="form-group">
								<span>Issued</span>
							</div>
						</div>
					</div>
				</div>
				<?php
					}
				} else {
				?>
				<div class="col-md-7">
					<div class="row">
						<input class="item_id5" type="hidden" name="req_item_id5[]" id="req_item_id5" value="<?php echo $req_row->request_item_id;?>" >
						<input class="item_id5" type="hidden" name="item_name5[]" id="item_name5" value="<?php echo $req_row->item_name;?>" >
						<input class="item_id5" type="hidden" name="req_qty5[]" id="req_qty5" value="<?php echo $req_row->required_qty;?>" >
						<input class="item_id5" type="hidden" name="item_id5[]" id="item_id5" value="<?php echo $req_row->recipe_id;?>" >
						<input class="item_id5" type="hidden" name="recipe_no5[]" id="recipe_no5" value="<?php echo $req_row->recipe_no;?>" >
						<input class="yeild5" type="hidden" name="yeild5[]" id="yeild5" value="<?php echo $req_row->yeild;?>">
						<input class="item_id5" type="hidden" name="uom5[]" id="uom5" value="<?php echo $req_row->uom;?>">
						<input class="item_id5" type="hidden" name="cat_id5[]" id="cat_id5" value="<?php echo $req_row->pr_cat_id;?>">
						<input class="qty5 form-control col-md-2 comm" type="text" name="inward_quantity5[]" id="inward_quantity5"  placeholder="Qty"   data-uk-tooltip="{pos:'bottom'}" title=" Remaining Qty :  <?php echo $req_row->item_balance_qty; ?>"  />
						<?php if($r['username'] == 'CW4-Counter' || $r['username'] == 'Santhe4') { ?>
						<input class="form-control price5" type="hidden" name="price5[]"  id="price5" value="<?php echo $req_row->cw4_price; ?>" />
						<input class="form-control tax5" type="hidden" name="tax5[]"  id="tax5" value="<?php echo $req_row->cw4_tax; ?>" />
						<input class="form-control basic_tax5" type="hidden" name="basic_tax5[]" id="basic_tax5" value="<?php echo $req_row->cw4_tax_amount; ?>" />
						<?php } else if($r['username'] == 'CW5-Counter' || $r['username'] == 'Santhe5') { ?>
						<input class="form-control price5" type="hidden" name="price5[]"  id="price5" value="<?php echo $req_row->cw5_price; ?>" />
						<input class="form-control tax5" type="hidden" name="tax5[]"  id="tax5" value="<?php echo $req_row->cw5_tax; ?>" />
						<input class="form-control basic_tax5" type="hidden" name="basic_tax5[]" id="basic_tax5" value="<?php echo $req_row->cw5_tax_amount; ?>" />
						<?php } else { ?>
						<input class="form-control price5" type="hidden" name="price5[]"  id="price5" value="<?php echo $req_row->cw_price; ?>" />
						<input class="form-control tax5" type="hidden" name="tax5[]"  id="tax5" value="<?php echo $req_row->cw_tax; ?>" />
						<input class="form-control basic_tax5" type="hidden" name="basic_tax5[]" id="basic_tax5" value="<?php echo $req_row->cw_tax_amount; ?>" />
						<?php } ?>
						<input class="form-control tot_basic5" type="hidden" name="tot_basic5[]"  id="tot_basic5" />
						<input class="form-control tax_amt5" type="hidden" name="tax_amt5[]" id="tax_amt5" />
						<input class="form-control tot_price5" type="hidden" name="total_price5[]"  id="total_price5" />
						<input class="form-control stk5" type="hidden" name="stock5[]" id="stock5" value="<?php echo $req_row->production_stock; ?>" data-uk-tooltip="{pos:'bottom'}" title="Present Stock" readonly/>
						<input class="grn_balance5 form-control col-md-2 comm" type="text" name="received_qty5[]" id="received_qty5" value="<?php echo $req_row->recieved_qty; ?>" data-uk-tooltip="{pos:'bottom'}" title="Issued Qty" readonly />
						<input class="grn_balance5 qty_pending5" type="hidden" name="bal_item_qty5[]"  id="bal_item_qty5" value="<?php echo $req_row->item_balance_qty; ?>" />
						<input class="remarks form-control col-md-2 comm" type="text" name="remarks5[]" id="remarks5[]" placeholder="Remarks"/>
					</div>		
				</div>
						
						<?php
						}
					?>	
					
			
	</div>	
<?php $count++; } ?>

	<div class="form-actions text-center">
		<input class="form-control gt5" type="hidden" name="gtotal5" id="gtotal5" />	
		<button type="submit" class="btn btn-primary" onclick="return confirm('Are you sure you want to submit this form?')" >
			<i class="la la-check-square-o"></i> Save
		</button>
	</div>
</form>
<?php } ?>
<br/>

<script>
	$(function () {
		$('.qty5').keypress(function(e) {
			var a = [];
			var k = e.which;
			for (i = 46; i < 58; i++)
			a.push(i);
			if (!($.inArray(k,a)>=0))
			e.preventDefault();
		});
	});
	
	$('.qty5').each( function(){ $(this).on( {'change': function() {
		var uname = $('#username5').val();
		var outlet = $('#dept5').val();
		//alert(uname);
		//alert(outlet);
		if((uname == 'Savories Kitchen' && outlet == 'counter') || (uname == 'CK_CW3' && outlet == 'cw3-counter'))
		{
			//alert('success');
		}
		else
		{
			var q1 = $(this).parent().find('.qty5').val();
			var q2 = $(this).parent().find('.qty_pending5').val();
			var tot_price = $(this).parent().find('.tot_price5').val();
			var tot = $("#gtotal5").val();
			var tot_amt = 0;
			//alert($(this).parent().find('.qty').val()); 
			if(parseFloat(q1) <= parseFloat(q2)){
				//alert("sucess");
			}
			else{
				alert("You cant enter more than RO balance quantity");
				tot_amt = parseFloat(tot) - parseFloat(tot_price);
				$(this).parent().find('.qty5').val('0');
				$(this).parent().find('.tot_basic5').val('0');
				$(this).parent().find('.tax_amt5').val('0');
				$(this).parent().find('.tot_price5').val('0');
				$("#gtotal5").val(tot_amt.toFixed(2));
			}
		}
	}
	})
	})
	
	$('.qty5').each( function(){ $(this).on( {'change': function() {
		var uname = $('#username5').val();
		var outlet = $('#dept5').val();
		//alert(uname);
		//alert(outlet);
	
			var q1 = $(this).parent().find('.qty5').val();
			var q2 = $(this).parent().find('.stk5').val();
			var tot_price = $(this).parent().find('.tot_price5').val();
			var tot = $("#gtotal5").val();
			var tot_amt = 0;
			//alert($(this).parent().find('.qty').val()); 
			if(parseFloat(q1) <= parseFloat(q2)){
				//alert("sucess");
			}
			else{
				alert("You cant enter more than the current stock");
				tot_amt = parseFloat(tot) - parseFloat(tot_price);
				$(this).parent().find('.qty5').val('0');
				$(this).parent().find('.tot_basic5').val('0');
				$(this).parent().find('.tax_amt5').val('0');
				$(this).parent().find('.tot_price5').val('0');
				$("#gtotal5").val(tot_amt.toFixed(2));
			}
		}
	})
	})
	
	//calculation
	$('.qty5').each(function() { $(this).on( {'keyup': function() {
		var qty = $(this).parent().find('.qty5').val();
		if(qty >= 0)
		{
			var price = $(this).parent().find('.price5').val();
			var tax = $(this).parent().find('.tax5').val();
			var basic_tax = $(this).parent().find('.basic_tax5').val();
			//alert(basic_tax);
			var tot_basic = (parseFloat(qty) * parseFloat(price)).toFixed(2);
			//var tot_tax = ((parseFloat(tot_basic) * parseFloat(tax))/100).toFixed(2);
			var tot_tax = (parseFloat(basic_tax) * parseFloat(qty)).toFixed(2);
			var total_price = (parseFloat(tot_basic) + parseFloat(tot_tax)).toFixed(2);
			//alert(total_price);
			$(this).parent().find('.tot_basic5').val(tot_basic);
			$(this).parent().find('.tax_amt5').val(tot_tax);
			$(this).parent().find('.tot_price5').val(total_price);
		}
	}
	})
	})
	
		
	$('.qty5').keyup(function() {
		var sum = 0;
		$('.tot_price5').each(function() {
			sum += Number($(this).val());
		});
		//alert(sum);
		$('#gtotal5').val(sum.toFixed());
	});
</script>
<script>
	$(".qty5").each( function(){ $(this).on( {'change': function() { 
		var q1 = $(this).parent().find('.qty5').val();
		var tot_price = $(this).parent().find('.tot_price5').val();
		var tot = $("#gtotal5").val();
		var tot_amt = 0;
		//alert(tot);
		if(parseFloat(tot) < 49000)
		{
			//alert("success");
		}
		else
		{
			alert("Bill amount is greater than 49000");
			tot_amt = parseFloat(tot) - parseFloat(tot_price);
			//$('#inward_quantity').focus();
			$(this).parent().find('.qty5').val('0');
			$(this).parent().find('.tot_basic5').val('0');
			$(this).parent().find('.tax_amt5').val('0');
			$(this).parent().find('.tot_price5').val('0');
			$("#gtotal5").val(tot_amt.toFixed(2));
		}
	}
	})
	})
</script>
</div>