<?php
defined('BASEPATH') OR exit('No direct script access allowed');
$producttotalrows = 1;

if($info4edit["info"]) $producttotalrows = count($info4edit["info"]);


?>


      <div class="main my-0">
        <div class="row mt-3 mx-0">
          <div class="col-md-6">
            <h3><?php echo $pgheading;?></h3>
          </div>
          <div class="col-md-5 text-right">
            &nbsp;
          </div>
        </div>
        
        <div class="container_fluid mt-2 px-3">
		<form name="frmgeneral" method="post" action="outlet/<?php echo $mgmtname;?>/managedata" enctype="multipart/form-data">
		<input type="hidden" name="rsrcid" value="<?php echo $rsrcid;?>">                
        <table class="webforms mt-0 table-responsive-sm">
		<tr>
          <td width="20%"><label>Customer Mobile:</label></td>
          <td width="80%"><input type="text" name="mobile" id="mobile" value="<?php if($info4edit["main"]) { echo $info4edit["main"]->fld_mobile; }?>" required onblur="getcustomerinfo('<?php echo base_url();?>')">
		  
		  </td>
      
         </tr>

		<tr>
          <td width="20%"><label> Customer Name:</label></td>
          <td width="80%"><input type="text" name="name" id="name" value="<?php if($info4edit["main"]) { echo $info4edit["main"]->fld_name; }?>" required></td>
         </tr>	
	
         <tr>
          <td width="20%"><label>Special Discount:</label></td>
          <td width="80%">
			<label class="container-radio horzbox">
				<input type="radio" name="specialdiscount" id="specialdiscount" value="yes" required <?php if($info4edit["main"]->fld_specialdiscount=='yes') { echo 'checked'; }?>/> Yes
				<span class="checkmark leftchkbx"></span>
			</label>
			<label class="container-radio horzbox">
				<input type="radio" name="specialdiscount" id="specialdiscount2" value="no" required <?php if($info4edit["main"]->fld_specialdiscount=='no') { echo 'checked'; }?>/> No
				<span class="checkmark leftchkbx"></span>
			</label>		  
			</td>
         </tr>

		<tr id="tr1" <?php if($info4edit["main"]->fld_specialdiscount=='yes') {  echo 'style="display:bolck;"'; } else{  echo 'style="display:none;"'; }?>>
          <td width="20%"><label> Given Name:</label></td>
          <td width="80%"><input type="text" name="givenname" id="givenname" value="<?php if($info4edit["main"]) { echo $info4edit["main"]->fld_givenname; }?>" ></td>
         </tr>
         <tr id="tr2"  <?php if($info4edit["main"]->fld_specialdiscount=='yes') {  echo 'style="display:bolck;"'; } else{  echo 'style="display:none;"'; }?>>
          <td width="20%"><label> Given Email:</label></td>
          <td width="80%"><input type="text" name="givenemail" id="givenemail" value="<?php if($info4edit["main"]) { echo $info4edit["main"]->fld_givenemail; }?>" ></td>
         </tr>
        <tr id="tr3"  <?php if($info4edit["main"]->fld_specialdiscount=='yes') {  echo 'style="display:bolck;"'; } else{  echo 'style="display:none;"'; }?>>
          <td width="20%"><label>  Given Mobile:</label></td>
          <td width="80%"><input type="text" name="givenmobile" id="givenmobile" value="<?php if($info4edit["main"]) { echo $info4edit["main"]->fld_givenmobile; }?>" ></td>
         </tr>

<tr>
             <td colspan="2" id="divwarehousestockinfo">
			 
	<table class="webforms mt-0 table-responsive-sm">
			 <tbody id="productrowbox">
			 <tr>
				<th width="25%">Product</th>
				<th width="15%">Instock</th>
				<th width="15%">Quantity</th>
				<th width="15%">Price</th>
				<th width="20%">Sub Total</th>
				<th width="10%">&nbsp;</th>
			</tr>
			<?php
			//predata($productlist);
					if($info4edit["info"])
					{
						for($iix=0;$iix<count($info4edit["info"]);$iix++)
						{
							$rowidx = $iix+1;
							$rowinfo = $info4edit["info"][$iix];
							
				?>
					  
<tr id="productrow_<?php echo $rowidx;?>">
				<td>
<input type="hidden" name="transactioninfoids[]" id="transactioninfoids<?php echo $rowidx;?>" value="<?php echo $rowinfo->fld_id;?>"> 				
				<select  id="productid<?php echo $rowidx;?>" name="productid[]" required onchange="showproductinstock(this.value,'<?php echo $rowidx;?>');" >
                                                                <option value="">--Select Product Name--</option>
<?php

if(is_array($productlist) && count(array_filter($productlist))>0)
{
	$instocktoshow = 0;
	foreach($productlist as $itemrow)
	{
		$sel_item ="";
		$instock = ($itemrow->incoming-$itemrow->outgoing+$itemrow->outgoing2);
		if($rowinfo->fld_productid==$itemrow->fld_productid)
		{
			$sel_item = 'selected="selected"';
			$instock = ($itemrow->incoming-($itemrow->outgoing+$rowinfo->fld_qty));
			$instocktoshow = $instock; 
		}	
			$instock = 0;
			$this->db->select("SUM( Case When (T.fld_type= 'stockout' or T.fld_type= 'o2o') and T.fld_outletid='".$this->outletid."' and T.fld_clientid='".$this->clientid."' then TI.fld_qty end) as incoming, SUM( Case When T.fld_type= 'o2o' and T.fld_fromoutletid='".$this->outletid."' and T.fld_clientid='".$this->clientid."' then TI.fld_qty end) as outgoing");
			$this->db->from("tbl_transactioninfo TI");
			$this->db->join("tbl_transaction T", "T.fld_id=TI.fld_transactionid");			
			$this->db->where("T.fld_clientid", $this->clientid);
			$this->db->where("T.fld_outletid", $this->outletid);
			$this->db->where("TI.fld_productid", $itemrow->fld_productid);
			$this->db->where("(T.fld_type='stockout' or T.fld_type='o2o')");
			//$this->db->or_where("T.fld_type", 'o2o');
			//$this->db->group_by('P.fld_name,TI.fld_productid');
			$results = $this->db->get(); 
			//echo $this->db->last_query(); exit;
			if($results->num_rows()>0)
			{
				$row = $results->row();
				$this->db->select("SUM( BI.fld_qty) as outgoing2");
				$this->db->from("tbl_billinginfo BI");
				$this->db->join("tbl_billing B", "B.fld_id=BI.fld_billingid");			
				$this->db->where("B.fld_clientid", $this->clientid);
				$this->db->where("B.fld_outletid", $this->outletid);
				$this->db->where("BI.fld_productid", $itemrow->fld_productid);
				
				//$this->db->or_where("T.fld_type", 'o2o');
				//$this->db->group_by('P.fld_name,TI.fld_productid');
				$results2 = $this->db->get(); 
				//echo $this->db->last_query(); exit;
				$row2 = $results2->row();
				$instock = (($row->incoming+$rowinfo->fld_qty)-($row->outgoing+$row2->outgoing2));
				$instocktoshow = $instock;
			}
		
			$price1 = $itemrow->fld_price;
			$price2 = $itemrow->fld_price3bottles;
			$price3 = $itemrow->fld_price6bottles;
			$price4 = $itemrow->fld_price9bottles;
			$price5 = $itemrow->fld_price12bottles;
			?>
			
					<option value="<?php echo $itemrow->fld_productid."_".$instock."_".$price1."_".$price2."_".$price3."_".$price4."_".$price5;?>"  <?php echo $sel_item;?>><?php echo $itemrow->fld_name;?></option>
			<?php
		
	}
}
?> 
				</select>
			</td>
			<td id="instock<?php echo $rowidx;?>">

			<?php echo $instocktoshow;?>
			<span id="rcvdmsg1" class=""></span></td>
			<td><input type="text" name="qty[]" id="qty<?php echo $rowidx;?>"  placeholder="Quanity" value="<?php echo $rowinfo->fld_qty;?>" required="required" data-validation-required-message="This field is required" aria-invalid="false" onchange="checkQtyRCVD('<?php echo $rowidx;?>');" onkeypress='return event.charCode >= 48 && event.charCode <= 57 || event.charCode==46'>

			<span id="qtymsg<?php echo $rowidx;?>" class=""></span></td>
			<td><input type="text" step="any" name="price[]" id="price<?php echo $rowidx;?>"  placeholder="Price" value="<?php echo $rowinfo->fld_price;?>" required="required" data-validation-required-message="This field is required" aria-invalid="false" onchange="calcPurchaseAmt();" onkeypress='return event.charCode >= 48 && event.charCode <= 57 || event.charCode==46' ></td>
			
			<td><input type="text" step="any" name="subtotal[]" id="subtotal<?php echo $rowidx;?>"  placeholder="Sub Total" value="<?php echo ($rowinfo->fld_price*$rowinfo->fld_qty);?>" required="required" data-validation-required-message="This field is required" aria-invalid="false" onchange="calcPurchaseAmt();" onkeypress='return event.charCode >= 48 && event.charCode <= 57 || event.charCode==46' ></td>
			<td>
			<?php
			if($iix==0)
			{
			?>
			<a class="btn btn-success text-white" style="cursor:pointer" onclick="return addrow('<?php echo base_url();?>');"><i class="fa fa-plus"></i> Row</a>
			<?php
			}
			else
			{
			?>
			<a class="btn btn-danger text-white" style="cursor:pointer" onclick="return removerow('<?php echo $rowidx;?>','product', 'edit', '<?php echo base_url();?>');">x</a>
			<?php
			}
			?>
			</td>
			</tr>					  
				<?php
						}
				?>
				<?php
					}
					else
					{
				?>
			 <tr id="productrow_1">
				<td><select  id="productid1" name="productid[]" required onchange="showproductinstock(this.value,'1');" >
                                                                <option value="">--Select Product Name--</option>
<?php
if(is_array($productlist) && count(array_filter($productlist))>0)
{
	
	foreach($productlist as $itemrow)
	{
		$sel_item ="";
		if($results->fld_productid==$itemrow->fld_productid)
		{
			$sel_item = 'selected="selected"';
		}
			$instock = 0;
			$this->db->select("SUM( Case When (T.fld_type= 'stockout' or T.fld_type= 'o2o') and T.fld_outletid='".$this->outletid."' and T.fld_clientid='".$this->clientid."' then TI.fld_qty end) as incoming, SUM( Case When T.fld_type= 'o2o' and T.fld_fromoutletid='".$this->outletid."' and T.fld_clientid='".$this->clientid."' then TI.fld_qty end) as outgoing");
			$this->db->from("tbl_transactioninfo TI");
			$this->db->join("tbl_transaction T", "T.fld_id=TI.fld_transactionid");			
			$this->db->where("T.fld_clientid", $this->clientid);
			$this->db->where("T.fld_outletid", $this->outletid);
			$this->db->where("TI.fld_productid", $itemrow->fld_productid);
			$this->db->where("(T.fld_type='stockout' or T.fld_type='o2o')");
			//$this->db->or_where("T.fld_type", 'o2o');
			//$this->db->group_by('P.fld_name,TI.fld_productid');
			$results = $this->db->get(); 
			//echo $this->db->last_query(); exit;
			if($results->num_rows()>0)
			{
				$row = $results->row();
				$this->db->select("SUM( BI.fld_qty) as outgoing2");
				$this->db->from("tbl_billinginfo BI");
				$this->db->join("tbl_billing B", "B.fld_id=BI.fld_billingid");			
				$this->db->where("B.fld_clientid", $this->clientid);
				$this->db->where("B.fld_outletid", $this->outletid);
				$this->db->where("BI.fld_productid", $itemrow->fld_productid);
				
				//$this->db->or_where("T.fld_type", 'o2o');
				//$this->db->group_by('P.fld_name,TI.fld_productid');
				$results2 = $this->db->get(); 
				//echo $this->db->last_query(); exit;
				$row2 = $results2->row();
				$instock = ($row->incoming-($row->outgoing+$row2->outgoing2));
			}
		
			$price1 = $itemrow->fld_price;
			$price2 = $itemrow->fld_price3bottles;
			$price3 = $itemrow->fld_price6bottles;
			$price4 = $itemrow->fld_price9bottles;
			$price5 = $itemrow->fld_price12bottles;
			?>
					<option value="<?php echo $itemrow->fld_productid."_".$instock."_".$price1."_".$price2."_".$price3."_".$price4."_".$price5;?>"  <?php echo $sel_item;?>><?php echo $itemrow->fld_name;?></option>
			<?php
		
	}
}
?>                                                            </select></td>
<td id="instock1">
<span id="rcvdmsg1" class=""></span></td>
<td><input type="text" name="qty[]" id="qty1"  placeholder="Quanity" value="" required="required" data-validation-required-message="This field is required" aria-invalid="false" onchange="checkQtyRCVD('1');" onkeypress='return event.charCode >= 48 && event.charCode <= 57 || event.charCode==46'>
<span id="qtymsg1" class=""></span></td>
<td>
<input type="text" step="any" name="price[]" id="price1"  placeholder="Price" value="0" required="required" data-validation-required-message="This field is required" aria-invalid="false" onchange="calcPurchaseAmt();" onkeypress='return event.charCode >= 48 && event.charCode <= 57 || event.charCode==46' ></td>
<td><input type="text" step="any" name="subtotal[]" id="subtotal1"  placeholder="Sub Total" value="0" required="required" data-validation-required-message="This field is required" aria-invalid="false" onchange="calcPurchaseAmt();" onkeypress='return event.charCode >= 48 && event.charCode <= 57 || event.charCode==46' ></td>
<td><a class="btn btn-success text-white" style="cursor:pointer" onclick="return addrow('<?php echo base_url();?>');"><i class="fa fa-plus"></i> Row</a></td>
			</tr>
	
<?php
					}
		 ?>
		</tbody> 
		</table> 
			 
			 </td>
             
         </tr>         
			

		 <tr>
             
				
            <td class="text-right" width="20%"></td>
             <td  width="80%" style="padding-left:500px;"> <strong>Total Amount:<strong> <input type="hidden" name="producttotalrows" id="producttotalrows" value="<?php echo $producttotalrows;?>"  /><input type="text" readonly step="any" name="producttotalamount" id="producttotalamount"  placeholder="Total Amount" value="<?php if($info4edit["main"]) { echo $info4edit["main"]->fld_amount; } else { echo "0"; }?>" required="required" data-validation-required-message="This field is required" aria-invalid="false" style="width:200px"></td> 
			
         
			            
         </tr>
         <tr>
          <td width="20%"></td>
          <td width="80%" style="padding-left:500px;"><strong>Discount Type:</strong>
			<?php 
		  $selectedRT = $info4edit["main"]->fld_discounttype;
		  ?>
             <select name="discounttype" id="discounttype" required aria-controls="sample_3" class="form-control input-sm input-xsmall input-inline" onchange="calcPurchaseAmt();">
			 <option value="percent" <?php if($selectedRT=="percent"){?> selected="selected"<?php }?>>Percent</option>
			 <option value="flat" <?php if($selectedRT=="flat"){?> selected="selected"<?php }?>>Flat</option>
			  </select>
		  </td>
         </tr>
         <tr>
          <td width="20%"></td>
          <td width="80%" style="padding-left:500px;"><strong>Special Discount :</strong>
		  <input type="text" class="form-control ui-autocomplete-input calcInvAmnt" name="discount"  id="discount" value="<?php if($info4edit["main"]) { echo $info4edit["main"]->fld_discount; }?>" placeholder="Discount" onblur="calcPurchaseAmt();" />
		  </td>
         </tr>
         <tr>
          <td width="20%"></td>
          <td width="80%" style="padding-left:500px;"><strong>Net to Pay:</strong>
		  <input type="text" class="form-control ui-autocomplete-input calcInvAmnt" name="nettopay"  id="nettopay" value="<?php if($info4edit["main"]) { echo $info4edit["main"]->fld_netamount; }?>" placeholder="0" onblur="calcPurchaseAmt();" />
		  </td>
         </tr>
		 <tr>
             <td></td>
             <td><button class="btn btn-danger">Submit</button></td>
         </tr>
         </table>
         </form> 
	         <div class="clearfix"></div>          
        </div>
       </div>
<script>
		function showproductinstock(val,index)
		{	
			
			var proddata = val.split("_");
			var stock = proddata[1];
			
			$("#instock"+index).html(stock);
			
		}

		function getcustomerinfo(basePath)
		{
			var mobile = $("#mobile").val();
			var rsrcid = $("#rsrcid").val();
			
			  if(mobile!="")
			  { 
				$.ajax({
					url: basePath+"outlet/getcustomerinfo",
					data: {'mobile': mobile,'rsrcid': rsrcid}, 
					type: "post",
					beforeSend: function() {
						//$("#divindentinfo").html('<img src="'+basePath+'assets/images/loader.gif" style="width: 40px;" />');	
					},
					success: function(data){
						
						//$("#divwarehousestockinfo").css('display','block');				
						datavalue = data.trim();
						var cinfo = datavalue.split("~");
						$("#name").val(cinfo[0]);
						

						if($("input[name='specialdiscount']:checked").val()=='yes')
						{
							producttotalamount= $("#producttotalamount").val();//totalproductamount.toFixed(2)
							producttotalamount = parseInt(producttotalamount);

							if(cinfo[1]=='percent')
							{
								$("#divdiscounttype").html("(%"+cinfo[2]+")");
								discountamount =  producttotalamount - (producttotalamount*cinfo[2]/100);
							}
							else if(cinfo[1]=='flat')
							{
								$("#divdiscounttype").html("(Flat)");
								discountamount =  Math.abs(producttotalamount - cinfo[2]);
							}
							producttotalamount= $("#producttotalamount").val();
							$("#discounttype").val(cinfo[1]);
							$("#discount").val(cinfo[2]);
							
							
							$("#discountamount").val(discountamount);
							$("#givenname").val(cinfo[3]);
							$("#givenemail").val(cinfo[4]);
							$("#givenmobile").val(cinfo[5]);
						}
	
					}
				});

			  }	
		}
		setTimeout(function(){
			
			getcustomerinfo('<?php echo base_url();?>');
		},3000);


function validateFrm()
{
	var totalrows = $("#producttotalrows").val();
	totalrows = parseInt(totalrows);
		errorstatus = 1;
		for(ix=1;ix<=totalrows;ix++)
		{
			
			checkQtyRCVD(ix);
			var qty = $("#qty"+ix).val();	
			qty = parseInt(qty);
			if(qty>0)
			{
				errorstatus = 0;

			}
		}	
		
		if(errorstatus==1)
		{		
			$("#frmerrormsg").html('Please update atleast one row');
			$("#frmerrormsg").addClass('alert alert-danger');
			setTimeout(function(){
			$("#frmerrormsg").removeClass('alert alert-danger');			
			$("#frmerrormsg").html("");
			},4000);
			return false;
		}
		else
		{
			return true;	  
		}
		
}
</script>
<script type="text/javascript">
function addrow(base_url)
{
	var id = $("#producttotalrows").val();
	
	var productlist = $("#productid1").html();
	
	id = parseInt(id) + 1;
	$("#producttotalrows").val(id);	
		
	$("#productrowbox").append('<tr id="productrow_'+id+'"><td><select id="productid'+id+'" name="productid[]" required="" onchange="showproductinstock(this.value,\''+id+'\');">'+productlist+'<option value="" selected>--Select Product Name--</option></select></td><td id="instock'+id+'"></td><td><input type="text" name="qty[]" id="qty'+id+'" placeholder="Quanity" value="" required="required" data-validation-required-message="This field is required" aria-invalid="false" onchange="checkQtyRCVD(\''+id+'\');" onkeypress="return event.charCode >= 48 &amp;&amp; event.charCode <= 57 || event.charCode==46"><span id="qtymsg'+id+'" class=""></span></td><td><input type="text" step="any" name="price[]" id="price'+id+'" placeholder="Price" value="0" required="required" data-validation-required-message="This field is required" aria-invalid="false" onchange="calcPurchaseAmt();" onkeypress="return event.charCode >= 48 &amp;&amp; event.charCode <= 57 || event.charCode==46"><span id="subtotalmsg'+id+'" class=""></span></td><td><input type="text" step="any" name="subtotal[]" id="subtotal'+id+'" placeholder="Sub Total" value="0" required="required" data-validation-required-message="This field is required" aria-invalid="false" onchange="calcPurchaseAmt();" onkeypress="return event.charCode >= 48 &amp;&amp; event.charCode <= 57 || event.charCode==46"><span id="ratemsg'+id+'" class=""></span></td><td><a class="btn btn-danger text-white" style="cursor:pointer" onclick="return removerow('+id+',\'product\',\'add\',\''+base_url+'\')">x</a></td></tr>');

}
function removerow(rowId,actionFor,actionType,base_url)
{
	totalRowIdname = '#'+actionFor+'totalrows';
	removerowId= '#'+actionFor+'row_'+rowId;
	if(actionType == "add")
	{
			totalRow = $(totalRowIdname).val();
			//alert(totalRow);
			$(removerowId).remove();
			
			for(i=eval(rowId+1); i<=totalRow; i++)
			{
				if(actionFor=="product")
				{
					
					$("#productid"+i).attr('id','productid'+eval(i-1));
					$("#qty"+i).attr('id','qty'+eval(i-1));
					$("#price"+i).attr('id','price'+eval(i-1));				
				}
						
			}
			
			var id = $(totalRowIdname).val();
			id = parseInt(id) - 1;
			$(totalRowIdname).val(id);
		
	}
	if(actionType == "edit")
	{
		if(!confirm('Action will delete the row!\nAre you sure?')) return false;			
				if(actionFor=="product")
				{
					delrowid = $("#transactioninfoids"+rowId).val();
				}				
		//alert(delrowid);
		$.ajax({
			url: base_url+"outlet/removecommonrow",
			data: {'task': 'billproduct', 'rowid': delrowid},
			type: "post",

			beforeSend: function() {
				//$("#row"+rowId).html('<img src="'+base_url+'assets/images/loader.gif" width="40" style="margin-left: 141px; margin-top: 11px;" />');
			},
			success: function(data){
				var msg = data.trim();
				//alert(msg);
				if(msg=="success")
				{					
					totalRow = $(totalRowIdname).val();
					
					for(i=eval(rowId+1); i<=totalRow; i++)
					{
						if(actionFor=="product")
						{							
							$("#productid"+i).attr('id','productid'+eval(i-1));
							$("#qty"+i).attr('id','qty'+eval(i-1));
							$("#price"+i).attr('id','price'+eval(i-1));			
							$("#subtotal"+i).attr('id','subtotal'+eval(i-1));			
						}
					}					
					setTimeout(function(){ 
						$(removerowId).remove();
						var id = $(totalRowIdname).val();
						id = parseInt(id) - 1;
						$(totalRowIdname).val(id);

						calcPurchaseAmt();
			
					}, 500);
					
				}
			}
		});
	}
	
	
}

function calcPurchaseAmt()
{	
		totalproductamount = 0;
		rowamount=0;
		var producttotalrows = $("#producttotalrows").val();
		for(c=1; c<=producttotalrows; c++)
		{
			price 	= $("#price"+c).val();
			price = parseInt(price);
			qty 	= $("#qty"+c).val();
			qty = parseInt(qty);
			
			rowamount = rowamount+(price*qty);
			
		}	
	
	totalproductamount = parseInt(rowamount);
	totalproductamount = parseInt(totalproductamount);
	//alert(totalproductamount)
	$("#producttotalamount").val(totalproductamount.toFixed(2));
	var discounttype = $("#discounttype  option:selected").val();
	var discount = $("#discount").val();
			if(discounttype=="percent")
			{
				var nettopay = totalproductamount-((totalproductamount) * discount/100);
				
			}
			else
			{
				if(totalproductamount>discount)
				{
					var nettopay = totalproductamount-discount;
					
				}
				else
				{
					var nettopay = 0;
					alert(" Please enter discount less then net price!");

				}
			}
	//alert(nettopay);
	nettopay = parseInt(nettopay);
	$("#nettopay").val(nettopay.toFixed(2));
}

function checkQtyRCVD(indexno)
{

		var totalrows = $("#producttotalrows").val();
		totalrows = parseInt(totalrows);

		var instock = $("#instock"+indexno).html();	
		instock = parseFloat(instock);

		var qty = $("#qty"+indexno).val();	
		qty = parseFloat(qty);

		var productid = $("#productid"+indexno+"  option:selected").val();
		//alert(productid);
		var proddata = productid.split("_");
		if(qty>0 && qty<3)
		{
			var price = proddata[2];
			price = parseFloat(price);
			$("#price"+indexno).val(price);
		}
		else if(qty>2 && qty<6)
		{
			var price = proddata[3];
			price = parseFloat(price);
			$("#price"+indexno).val(price);
		}
		else if(qty>5 && qty<9)
		{
			var price = proddata[4];
			price = parseFloat(price);
			$("#price"+indexno).val(price);
		}
		else if(qty>8 && qty<12)
		{
			var price = proddata[5];
			price = parseFloat(price);
			$("#price"+indexno).val(price);
		}
		else if(qty>11)
		{
			var price = proddata[6];
			price = parseFloat(price);
			$("#price"+indexno).val(price);
		}
		else
		{
			$("#price"+indexno).val(0);
		}

		subtotal=  (price*qty);
		if(subtotal>0)
		{
			$("#subtotal"+indexno).val(subtotal);
		}
		else
		{
			$("#subtotal"+indexno).val(0); 
		}
		temprice=0;
		totalamount=0;
		for(ix=1;ix<=totalrows;ix++)
		{
			temprice = $("#price"+ix).val();
			temprice = parseFloat(temprice);
			var qty2 = $("#qty"+ix).val();	
			qty2 = parseFloat(qty2);
			if(totalamount>0)
			{
				totalamount = totalamount+(qty2*temprice);
			}
			else
			{
				totalamount = (temprice*qty2);
			}
		}
		totalamount = parseFloat(totalamount);		
		$("#producttotalamount").val(totalamount);
		calcPurchaseAmt();
		if(instock<qty)
		{
			//alert(checkrqty);
			$("#qtymsg"+indexno).html('Quantity should not exceed Instock');
			//$("#qtymsg"+indexno).addClass('alert');
			$('#qtymsg'+indexno).css('color', 'red');
			$('#qty'+indexno).css('border-color', 'red');
			setTimeout(function(){
			//$("#qtymsg"+indexno).removeClass('alert');			
			$("#qtymsg"+indexno).html("");
			$('#qtymsg'+indexno).css('color', '');
			$('#qty'+indexno).css('border-color', '');
			},4000);
			temprice =  (qty*price);
			temprice = parseFloat(temprice);
			totalamount =  Math.abs(totalamount-temprice);
			$("#producttotalamount").val(totalamount);			
			$("#qty"+indexno).val('');			
			$("#price"+indexno).val('');

			return false;
		}
		else
		{
			return true;
		}



}

</script>

<script>
$(document).ready(
    function() {
		//var selectedValue = document.querySelector('input[name="specialdiscount"]:checked');
		//$("input[name='specialdiscount']:checked").val();
		$("input[name='specialdiscount']").click(function() {
            //$("#specialdiscount").toggle(this.checked);
			if($("input[name='specialdiscount']:checked").val()=='yes')
			{				
				$("#tr0").show("slow");
				$("#tr1").show("slow");
				$("#tr2").show("slow");
				$("#tr3").show("slow");
				getcustomerinfo('<?php echo base_url();?>');

			}
			else
			{
				$("#tr0").hide("slow");
				$("#tr1").hide("slow");
				$("#tr2").hide("slow");
				$("#tr3").hide("slow");
				$("#discount").val(0);
			}            
        });        
    });
    
</script>