<link rel="stylesheet" href="css/new_mxerp.css">
<?php ob_start();
$PageSecurity = 4;
$PageName = 'MaterialReceiptNote';
include('includes/DefinePOClass.php');
include('includes/SQL_CommonFunctions.inc');
if (isset($_GET['ModifyOrderNumber']) and $_SESSION[UserType]=='SuperAdmin')  {
$title = _('Modify Purchase Order') . ' ' . $_GET['ModifyOrderNumber'];
} else {
}
if(isset($_GET['issueid'])  && isset($_GET['issueid']) !=''){
$title = _('Update Issue Slip');
}
else{
$title = _('Add Material Receipt Note');
}
$Prefix_SQL = "Select prefix_id,prefix_code,prefix_name from prefix_tbl Where prefix_name ='".$PageName."'";
$Prefix_Result = DB_query($Prefix_SQL,$db);
$Prefix_Row = DB_fetch_array($Prefix_Result);
$Prefix_code = $Prefix_Row['prefix_code'];
$Prefix_date = date("y");
$Prefix_date++;
$CurrentFinancialYear = date("y")."-".$Prefix_date;
$Ordernumber = $_GET['orderno'];

$SQL = "select supplierno from purchorders where orderno='".$Ordernumber."'";
$result = DB_query($SQL,$db);
$supp_result = DB_fetch_array($result);
$suppno = $supp_result['supplierno'];

$ORDER_SQL = "SELECT PO_Orderno FROM purchorders WHERE orderno='".$_GET['orderno']."' ";
$ORDER_RES = DB_query($ORDER_SQL, $db);
$ORDER_ROW = DB_fetch_array($ORDER_RES);
$PO_Orderno = $ORDER_ROW['PO_Orderno'];
$time = date('G:ia');
$date = date($_SESSION['DefaultDateFormat']);

if (empty($_GET['identifier'])) {
	$identifier=date('U');
} else {
	$identifier=$_GET['identifier'];
}
?>
<script type="text/javascript">
function FiendPurchasenumber(str)
{
	var a= document.getElementById('hidden_site').value;
	var vendor=document.getElementById("vendor").value;
	if(a !='' )
	{location.replace("index.php?route=AddMRN&type=<?php echo $_GET['type']; ?>"+"&orderno="+str+"&sitecode="+a+"&vendor="+vendor+"&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>");}
	else
	{location.replace("index.php?route=AddMRN&type=<?php echo $_GET['type']; ?>"+"&orderno="+str+"&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>");}
}
function calculate_short(id)
{
	var x = parseFloat(document.getElementById('reciveqty_'+id).value);
	var poqty = parseFloat(document.getElementById('qty_as_per_bill_'+id).value);
	var rate = document.getElementById('itemrate_'+id).value;
 	if(x != '')
	{
		if(poqty < x)
		{	
			alert("Received Quantity can not be greater than PO Quantity");
			return false;
		}
		else if(poqty >= x)
		{
 				document.getElementById('short_accessid_'+id).value = (parseFloat(poqty)- parseFloat(x)).toFixed(2);
				return true;
		}
	  }
	else
	{
		document.getElementById('short_accessid_'+id).value = 0;
			return true;
	}
}
function calculate_discount(str)
{
	var qty=document.getElementById("reciveqty_"+str).value;
	var itemrate=document.getElementById("itemrate_"+str).value;
	var disc_type=document.getElementById("disc_type_"+str).value;
	var disc=document.getElementById("discount_"+str).value;
	//alert(disc);
	if(disc==''){disc=0;}
	if(disc_type=='Amount')
	{
		var total_rate=parseFloat(qty) * parseFloat(itemrate);
		document.getElementById("val_after_disc_"+str).value=(parseFloat(total_rate) - parseFloat(disc)).toFixed(2);
		document.getElementById("discount_amount_"+str).value=(parseFloat(disc)).toFixed(2);
	}
	else if(disc_type=='PercentAmt')
	{
		var total_rate=parseFloat(qty) * parseFloat(itemrate);
		var disc_val=parseFloat(total_rate)*parseFloat(disc)/100;
		document.getElementById("val_after_disc_"+str).value=(parseFloat(total_rate) - parseFloat(disc_val)).toFixed(2);
		document.getElementById("discount_amount_"+str).value=(parseFloat(disc_val)).toFixed(2);
	}
}
function calc_taxval(str,j)
{
	var val_after_disc = document.getElementById("val_after_disc_"+j).value;
	if(val_after_disc != ''){
	var taxvalue=(parseInt(str)*parseInt(val_after_disc))/100;
	}
	document.getElementById("taxamount_"+j).value=parseInt(taxvalue);
	
}
function calc_amount_with_tax(str)
{
	var totalrate = document.getElementById("val_after_disc_"+str).value;
	var taxamount = document.getElementById("taxamount_"+str).value;
if(totalrate != '' && taxamount != '')
{
	var total_amt_with_tax = parseInt(totalrate) + parseInt(taxamount);
	document.getElementById("total_amt_with_tax_"+str).value = parseInt(total_amt_with_tax);
}
}
function openPopupProfile(i)
{
  	window.open("Search_item_MRN.php?rowcount="+i,"profiles_popup_window","height=500,width=700,toolbar=no,menubar=no,dependent=yes,resizable =yes, scrollbars=yes");
}
function ChargeAmount1(CA,ID)
 {
	if (CA=="")
	{
		document.getElementById("PerActual"+ID).innerHTML="";
		return;
	}
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			document.getElementById("PerActual"+ID).innerHTML=xmlhttp.responseText;
		}
	}
 	xmlhttp.open("GET","Charges_Details.php?CA="+CA+"&ID="+ID,true);
	xmlhttp.send();
 }
function calculate_BasicAmount()
{
 	var  itemamount = 0;
 	var  count= document.getElementById('rowcount').value;
 	
	for(var i=0; i<count; i++)
	{
 		var totalrate = document.getElementById('val_after_disc_'+i).value;
			if(!isNaN(totalrate) && totalrate!='' ){
				itemamount  =parseFloat(itemamount)+  parseFloat(totalrate);
  			}
			else if(totalrate=='')
			{ totalrate=0;}
 	}
 	document.getElementById('itemamount').value = itemamount;
 }
 function calculate_TotalTaxAmount()
{
 	var  itemamount = 0;
 	var  count= document.getElementById('rowcount').value;
 	for(var i=1; i<count; i++)
	{
 		var totalrate = document.getElementById('taxamount_'+i).value;
 			if(!isNaN(totalrate) && totalrate!='' ){
				itemamount  =parseFloat(itemamount)+  parseFloat(totalrate);
  			}
 	}
  	document.getElementById('tax_chargeamount').value = itemamount.toFixed(2);
  }
function cal_surcharges(id)
{
	   
	   document.getElementById("hidden_percent_inclusivecharge").value=id;
}
 
function cal_surcharges1()
{
   var surcharge=document.getElementById("hidden_percent_inclusivecharge").value;
    
 	 if(surcharge=='SUR'){  
 	 document.getElementById('tax_suroct_value').value=(parseFloat(document.getElementById('tax_chargeamount').value) *  parseFloat(document.getElementById('percent_inclusive').value))/100;
	 }
	 if(surcharge=='OCT'){
		 
	  $totals=parseInt(document.getElementById('itemamount').value)+parseInt(document.getElementById('tax_chargeamount').value);
	 document.getElementById('tax_suroct_value').value=($totals *  parseInt(document.getElementById('percent_inclusive').value))/100;
		 }
} 
function Completetotal()
{
	var taxableamt = document.getElementById('taxable_amt').value;
	var taxchargeamount = document.getElementById('tax_chargeamount').value;
 	var taxsuroct_value = document.getElementById('tax_suroct_value').value;
	var chargeamount = document.getElementById('chargeamount').value;
	
	var finalgrandtotal=parseFloat(taxableamt)+parseFloat(taxchargeamount)+parseFloat(taxsuroct_value)+parseFloat(chargeamount);
	document.getElementById('grandtotal').value=Math.round(finalgrandtotal.toFixed(2));
}
function CountTotalQty()
{
	var  qty = 0;
	var  count= document.getElementById('rowcount').value;
   	var i;
 	for(i=0; i<count; i++)
	{
 		var totalqty = document.getElementById('reciveqty_'+i).value;
 			if(!isNaN(totalqty) && totalqty!='' ){
				qty  =parseFloat(qty)+  parseFloat(totalqty);
  			}
 	}
  	document.getElementById('totalqty').value = qty;
 }
function check_po()
{
	var rowcount=document.getElementById("rowcount").value;
	
	for(var i=0;i<rowcount;i++)
	{
		poqty=parseFloat(document.getElementById("poqty_"+i).value);
		reciveqty=parseFloat(document.getElementById("reciveqty_"+i).value);
		if(reciveqty!='' && poqty!='')
		{
		if(reciveqty>poqty)
		{
			alert("Received Quantity Cannot Be Greater Than PO Quantity");
			document.getElementById("reciveqty_"+i).focus();
			return false; 
		}
		}
	}
}
function PurchaseOrderNumber_1()
{
	var receiviedagaist=document.getElementById("receiviedagaist").value;
	location.replace("index.php?route=AddMRN&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&type="+receiviedagaist);
}
function check_bill_qty(str,id)
{
	var poqty=parseFloat(document.getElementById("poqty_"+id).value);
	//alert(poqty);
	str1=parseFloat(str);
	//alert(str1);
	if(poqty!='')
	{
	if(str1>poqty)
	{
		alert("Quantity As Per Bill Cannot Be Greater Than PO Quantity");
		document.getElementById("qty_as_per_bill_"+id).focus();
		return false;
	}
	}
}
function calc_total_price(str)
{
	var item_total_rate=0;
	var reciveqty = document.getElementById("reciveqty_"+str).value;
	var itemrate=document.getElementById("itemrate_"+str).value;	
	if(reciveqty!='' && itemrate!='')
	{
		document.getElementById("item_total_rate_"+str).value=(parseFloat(reciveqty)*parseFloat(itemrate)).toFixed(2);
	}
	
}
function cal_Excise_Duty(str)
{
	var excise_rate=document.getElementById("rate_"+str).value;
	var basic_value=document.getElementById("itemamount").value;	
	var excise_value=basic_value*(excise_rate/100);
	document.getElementById("caption_value_"+str).value=excise_value.toFixed(2);
	
}
function cal_Edu_Cess(str)
{
	var excise_rate=document.getElementById("rate_"+str).value;
	var str2=str-1;
	var excise_val=document.getElementById("caption_value_"+str2).value;
	var educess_value=excise_val*(excise_rate/100);
	document.getElementById("caption_value_"+str).value=educess_value.toFixed(2);	
}
function cal_SHE_Cess(str)
{
	var excise_rate=document.getElementById("rate_"+str).value;
	var str2=str-2;
	var excise_val=document.getElementById("caption_value_"+str2).value;
	var educess_value=excise_val*(excise_rate/100);
	document.getElementById("caption_value_"+str).value=educess_value.toFixed(2);	
}
function calc_taxable_amount()
{
	var tot_taxable=0;
	var basic_value=document.getElementById("itemamount").value;	
	var count=document.getElementById("count_tax").value;
	for(var i=1;i<count;i++)
	{
		var value=document.getElementById("caption_value_"+i).value;
		tot_taxable=parseFloat(tot_taxable)+parseFloat(value);
	}
	var final_taxable=parseFloat(tot_taxable)+parseFloat(basic_value);
	document.getElementById("taxable_amt").value=final_taxable.toFixed(2);
}
function tax_rate(str)
{
if (str.length==0)
  {
  document.getElementById("taxval").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("taxval").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","get_tax_val.php?taxid="+str,true);
xmlhttp.send();
}
function calc_tax_amount()
{
	var taxable=document.getElementById("taxable_amt").value;
	var taxval=document.getElementById("taxvalue").value;
	var final=taxable*(taxval/100);
	document.getElementById("tax_chargeamount").value=final.toFixed(2);
}
function cal_suroct(str)
{
	var total_tax=document.getElementById("tax_chargeamount").value;
	var final_sur=total_tax*(str/100);
	document.getElementById("tax_suroct_value").value=final_sur.toFixed(2);
}
function get_creditdays(str)
{
	if (str.length==0)
  {
  document.getElementById("creditid").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("creditid").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","Creditdays.php?q="+str,true);
xmlhttp.send();
}
</script>
<?php 
if($_REQUEST['cancel']!='')
{
	unset($_SESSION['manvi_new']);
	unset($_SESSION['receiviedagaist']);
	unset($_SESSION['sitecode']);
	unset($_SESSION['sitename']);
	unset($_SESSION['hidden_site']);
	unset($_SESSION['vendor']);
	unset($_SESSION['PO']);
	unset($_SESSION['credit_days']);
	unset($_SESSION['MRNdate']);
	unset($_SESSION['matrialreceipt']);
	unset($_SESSION['storecode']);
	unset($_SESSION['storename']);
	unset($_SESSION['rmrs_no']);
	unset($_SESSION['orderdate']);
	unset($_SESSION['billno']);
	unset($_SESSION['MRNVoucherDate']);
	unset($_SESSION['gatepassno']);
	unset($_SESSION['inwardate_save']);
	unset($_SESSION['transport']);
	unset($_SESSION['vehicalno']);
	unset($_SESSION['timein']);
	unset($_SESSION['timeout']);
	unset($_SESSION['lrno']);
	unset($_SESSION['lrdate']);
	unset($_SESSION['total_weight']);
	unset($_SESSION['RemarksIfAny']);
	unset($_SESSION['DocumentType']);
}
if($_REQUEST['item_popup']!='')
{
	$_SESSION['receiviedagaist']=$_REQUEST['receiviedagaist'];
	$_SESSION['sitecode']=$_REQUEST['sitecode'];
	$_SESSION['sitename']=$_REQUEST['sitename'];
	$_SESSION['hidden_site']=$_REQUEST['hidden_site'];
	$_SESSION['vendor']=$_REQUEST['vendor'];
	$_SESSION['PO']=$_REQUEST['PO'];
	$_SESSION['credit_days']=$_REQUEST['credit_days'];
	$_SESSION['MRNdate']=$_REQUEST['MRNdate'];
	$_SESSION['matrialreceipt']=$_REQUEST['matrialreceipt'];
	$_SESSION['storecode']=$_REQUEST['storecode'];
	$_SESSION['storename']=$_REQUEST['storename'];
	$_SESSION['rmrs_no']=$_REQUEST['rmrs_no'];
	$_SESSION['orderdate']=$_REQUEST['orderdate'];
	$_SESSION['billno']=$_REQUEST['billno'];
	$_SESSION['MRNVoucherDate']=$_REQUEST['MRNVoucherDate'];
	$_SESSION['gatepassno']=$_REQUEST['gatepassno'];
	$_SESSION['inwardate_save']=$_REQUEST['inwardate_save'];
	$_SESSION['transport']=$_REQUEST['transport'];
	$_SESSION['vehicalno']=$_REQUEST['vehicalno'];
	$_SESSION['timein']=$_REQUEST['timein'];
	$_SESSION['timeout']=$_REQUEST['timeout'];
	$_SESSION['lrno']=$_REQUEST['lrno'];
	$_SESSION['lrdate']=$_REQUEST['lrdate'];
	$_SESSION['total_weight']=$_REQUEST['total_weight'];
	$_SESSION['RemarksIfAny']=$_REQUEST['RemarksIfAny'];
	$_SESSION['DocumentType']=$_REQUEST['DocumentType'];
}
if($_REQUEST['type']=='Direct')
{
	$sitecode=$_SESSION['sitecode'];
	$sitename=$_SESSION['sitename'];
	$hidden_site=$_SESSION['hidden_site'];
	$vendor=$_SESSION['vendor'];
	$PO=$_SESSION['PO'];
	$credit_days=$_SESSION['credit_days'];
	$MRNdate=$_SESSION['MRNdate'];
	$matrialreceipt=$_SESSION['matrialreceipt'];
	$storecode=$_SESSION['storecode'];
	$storename=$_SESSION['storename'];
	$rmrs_no=$_SESSION['rmrs_no'];
	$orderdate=$_SESSION['orderdate'];
	$billno=$_SESSION['billno'];
	$MRNVoucherDate=$_SESSION['MRNVoucherDate'];
	$gatepassno=$_SESSION['gatepassno'];
	$inwardate_save=$_SESSION['inwardate_save'];
	$transport=$_SESSION['transport'];
	$vehicalno=$_SESSION['vehicalno'];
	$timein=$_SESSION['timein'];
	$timeout=$_SESSION['timeout'];
	$lrno=$_SESSION['lrno'];
	$lrdate=$_SESSION['lrdate'];
	$total_weight=$_SESSION['total_weight'];
	$RemarksIfAny=$_SESSION['RemarksIfAny'];
	$DocumentType=$_SESSION['DocumentType'];
}
else
{
	$sitecode=$_REQUEST['sitecode'];
	$sitename=$_REQUEST['sitename'];
	$hidden_site=$_REQUEST['hidden_site'];
	$vendor=$_REQUEST['vendor'];
	$PO=$_REQUEST['PO'];
	$credit_days=$_REQUEST['credit_days'];
	$MRNdate=date('d/m/Y');
	$matrialreceipt=$_REQUEST['matrialreceipt'];
	$storecode=$_REQUEST['storecode'];
	$storename=$_REQUEST['storename'];
	$rmrs_no=$_REQUEST['rmrs_no'];
	$orderdate=date('d/m/Y');
	$billno=$_REQUEST['billno'];
	$MRNVoucherDate=date('d/m/Y');
	$gatepassno=$_REQUEST['gatepassno'];
	$inwardate_save=date('d/m/Y');
	$transport=$_REQUEST['transport'];
	$vehicalno=$_REQUEST['vehicalno'];
	$timein=$_REQUEST['timein'];
	$timeout=$_REQUEST['timeout'];
	$lrno=$_REQUEST['lrno'];
	$lrdate=date('d/m/Y');
	$total_weight=$_REQUEST['total_weight'];
	$RemarksIfAny=$_REQUEST['RemarksIfAny'];
	$DocumentType=$_REQUEST['DocumentType'];
}
if($_REQUEST['daction']=='del')
{
   $delsql = "update purchorderdetails set item_row_status='0' where podetailitem='".$_REQUEST['id']."'";
  $delete_result = DB_query($delsql,$db);
    if(!$delete_result)
  {
    die("error".DB_error_msg($db));
   }
 }
$date = date($_SESSION['DefaultDateFormat']);

if($_POST['save']!='')
{
	$status=true;
	$msg_error=array();
  	
 	if(empty($_REQUEST['sitename']))
	{
		$msg_error[]="Please Select Site Code ";
		$status=false;
	}
	 
	if(empty($_REQUEST['PO']))
	{
		$msg_error[]="Please Select PO/WO/MTO ";
		$status=false;
	}
	if($_REQUEST['storecode']=='000')
	{
		$msg_error[]="Please Select Storecode ";
		$status=false;
	}
	
	if(empty($_REQUEST['billno'])  )
	{
		$msg_error[]="Please Enter BillNo ";
		$status=false;
	}
  
		  $countcharge = $_POST['countcharges'];
 		 for($P=1;$P<$countcharge;$P++)
		 {
		   if($_POST['Charge_'.$P.'']=='NO' && $_POST[$P]=='')
		   {
				  	$msg_error[]="Please Enter Amount in Charges Details";
					$status=false;
					break;
     		}
 		 }
 	
	$countval=$_POST['countval'];
	
	for($k=1;$k<countval;$k++)
	{
		if($_POST['reciveqty'][$k]>$_POST['poqty'][$k])
		{
				$msg_error[]="Received Quantity Cannot Be Greater Than PO Quantity";
				$status=false;
				break;
		}
	}
	
	if($status)
	{
	
	
	$pending_status=0;
	$_POST['PRDate']=Date($_SESSION['DefaultDateFormat']);
	$_POST['DatePrinted']=Date($_SESSION['DefaultDateFormat']);
	ConvertSQLDate($_SESSION['MR'.$identifier]->Orig_OrderDate);
 	$_POST['sitecode'];
 	$sql='select * from addmrn where OrderFromSite = "'.$_POST['sitecode'].'" ';
 	$result = DB_query($sql,$db);
	$numrows = DB_num_rows($result);
	
	if($numrows>0)
	{
		$i=0;
		$strmax = "";
		$myrow=DB_Fetch_array($result);
		if($_POST['sitecode'] !='')
		{
			$val1= substr($myrow['MRNNo'],0,18);
			$strChar1 = substr($myrow['MRNNo'],0,7);
			$strChar = $strChar1."/".$_POST['sitecode']."/".$CurrentFinancialYear."/";
			
			if($numrows>0)
			{ 
				$maxval = $numrows;
			}
			else
			{
				$maxval=0;
			}
			$strChar = $Prefix_code."/".$_POST['sitecode']."/".$CurrentFinancialYear."/";
		}
	}
	else
	{
	$strChar = $Prefix_code."/".$_POST['sitecode']."/".$CurrentFinancialYear."/";
	}
	$strFinal = $maxval+1;
	$strBothFinal = $strChar.str_pad($strFinal,4,"0",STR_PAD_LEFT);
	$strBothFinal;
	//exit;
	$Billdate = $_POST['Billdate'];
	$expl =explode('/',$Billdate);
	$Billdatesav = $expl['1']."/".$expl['0']."/".$expl['2'];
	$inwarddate = $_POST['inwardate_save'];
	$inwardexplode = explode('/',$inwarddate);
	$inwardate_save = $inwardexplode[1]."/".$inwardexplode[0]."/".$inwardexplode[2];
	$qty=$_REQUEST['itemcode'];
	$qty1=count($qty);

	$sql1 = "select PO_Orderno from purchorders where orderno = '".$_POST['select']."' ";
	$result = DB_query($sql1, $db);
	$myrow = DB_fetch_array($result);
	$PO_Orderno = $myrow['PO_Orderno'];
	
		
	for($str=1;$str<$_REQUEST['count_tax'];$str++)
	{
		$caption_val[$str] = $_REQUEST['caption_value'][$str];
		$percentage[$str] = $_REQUEST['rate'][$str];
	}
		////addmrn details////
		$data['MRNNo'] = $strBothFinal; 
		$data['totalqty'] = $_REQUEST['totalqty']; 
 		$data['MRNDate'] = date('Y-m-d'); 
		$data['Type'] =  $_REQUEST['receiviedagaist'];
		$data['MRNVoucherDate'] = FormatDateForSQL($_REQUEST['MRNVoucherDate']); 
		$data['ReceivedAgainst'] =  $_REQUEST['receiviedagaist'];
		$data['MaterialReceiptOnBasis'] = $_REQUEST['matrialreceipt']; 
		$data['OrderFromSite'] = $_REQUEST['sitecode']; 
		$data['StoreCode'] = $_REQUEST['storecode'];  
		$data['StoreName'] = $_REQUEST['storename'];  
		$data['site_name'] = $_REQUEST['sitename'];  
		$data["PO_WO_MTO"] = $_REQUEST['PO'];  
 		$data['OrderDate'] = date("Y-m-d");//$_REQUEST['orderdate']; 
		$data['ReceivedFrom'] = $_REQUEST['vendor']; 
		$data['DocumentType'] = $_REQUEST['DocumentType'];  
		$data['BillNo'] = $_REQUEST['billno']; 
		$data['BillDate'] = date("Y-m-d"); 
		$data['GetPassNo'] = $_REQUEST['gatepassno']; 
		$data['Inwardsdate'] =FormatDateForSQL($_REQUEST['inwardate_save']);     
		$data['Transp_name'] = $_REQUEST['transport'];  
		$data['VehicleNo'] = $_REQUEST['vehicalno']; 
		$data['TimeIn'] = $_REQUEST['timein']; 
		$data['Timeout'] = $_REQUEST['timeout'];  
		$data['LRno'] = $_REQUEST['lrno'];  
		$data['RemarksIfAny'] = $_REQUEST['RemarksIfAny'];  	 
		$data['LRdate'] = FormatDateForSQL($_REQUEST['lrdate']); 
		$data['TotalWeight'] = $_REQUEST['total_weight'];  
		$data['createdby'] = "admin";  	 
		$data['order_no'] = $_REQUEST['orderno'];  	 
		$data['mrnstatus'] = "Pending";
		$data['Remarks'] = $_REQUEST['remark'];  
		$data['rmrs_no'] = $_REQUEST['rmrs_no'];
		$data['Excise_Duty'] = $caption_val[1];
		$data['Excise_Duty_perc'] = $percentage[1];
		$data['Edu_Cess'] = $caption_val[2];
		$data['Edu_Cess_perc'] = $percentage[2];
		$data['SHE_Cess'] = $caption_val[3];
		$data['SHE_Cess_perc'] = $percentage[3];
		$data['taxable_amt'] = $_REQUEST['taxable_amt'];
		$data['taxname'] = $_REQUEST['taxname'];
		$data['taxrate'] = $_REQUEST['taxvalue'];
		$data['tax_chargeamount']  =$_REQUEST['tax_chargeamount'];  
		$data['CompanyId']  =$_SESSION['ACGCompanyId'];
 // echo "<br>---------------------------Add MRN-------------------------------------------------------------------<br>";
	$insertsql=	$s->insertRecord('addmrn', $data);
	
 	$insertResult = DB_query($insertsql,$db,$ErrMsg);
	$Lastreq_id=DB_Last_Insert_ID($db,'addmrn',$ErrMsg);
 
 	if($insertResult==true)
	{
		$countval= count($_REQUEST['qcreqd']);
		$total_basic_amount=0;
		$total_tax_amount=0;	
 		for($i=0;$i<$countval;$i++)
		{
 				if($_REQUEST['itemcode'][$i]!='' )
				{
				///addmrnqty items///
				$data_arr['issue_id']= $Lastreq_id;
				$data_arr['issue_qty']= $_POST['issuebalanc'][$i];
				$data_arr['itemprice']= $_POST['itemrate'][$i];
				$data_arr['itemcode']= $_REQUEST['itemcode'][$i];
				$data_arr['description']= $_POST['desc'][$i];
				$data_arr['Make']= $_POST['make'][$i];
				$data_arr['Color']= $_REQUEST['color'][$i];
				$data_arr['Gread']= $_REQUEST['gread'][$i];
				$data_arr['size']= $_REQUEST['size'][$i];
				$data_arr['gsm']= $_REQUEST['gsm'][$i];
				$data_arr['UOM']= $_REQUEST['uom'][$i];
				$data_arr['Qcrequd']= $_REQUEST['qcreqd'][$i];
				$data_arr['currency']= $_REQUEST['currency'][$i];
				$data_arr['POQty']= $_REQUEST['poqty'][$i];
				$data_arr['reciveqty']= $_REQUEST['reciveqty'][$i];
				$data_arr['short_accessid']= $_REQUEST['short_accessid'][$i];
				$data_arr['qty_as_per_bill']= $_REQUEST['qty_as_per_bill'][$i];
				$data_arr['disc_type']= $_REQUEST['disc_type'][$i];
				$data_arr['discount']= $_REQUEST['discount'][$i];
				$data_arr['val_after_disc']= $_REQUEST['val_after_disc'][$i];
				$data_arr['tax_rate']= $_REQUEST['tax_rate'][$i];
				$data_arr['total_amt_with_tax']= $_REQUEST['total_amt_with_tax'][$i];
				$data_arr['remarks']= $_REQUEST['remarks'][$i];
				$data_arr['mat_value']= $_REQUEST['mat_value'][$i];
				$data_arr['discount_amount']= $_REQUEST['discount_amount'][$i];
					if($_REQUEST['short_accessid'][$i]!=0)
					{
					$pending_status=1;
					$data_arr['status']= "N";
					}
					else
					{
					 $data_arr['status']= "Y";
					}				
				if($_REQUEST['hidden_addqty']!='')
				$data_arr['parent_id']= $_REQUEST['hidden_addqty'];				
  				$data_arr['rejectqty']= $_REQUEST['rejectqty'][$i];
				$data_arr['order_no']= $_REQUEST['orderno'];
				$data_arr['item_total_rate']= $data_arr['itemprice'] * $data_arr['reciveqty'] ;
				//$data_arr['taxname']= $_REQUEST['taxname'][$i];
				$data_arr['taxamount_rate']=$_REQUEST['taxamount'][$i];		
 				$total_basic_amount+=$data_arr['item_total_rate'];
				$total_tax_amount+=$data_arr['taxamount_rate'];
				$insert_mrm=$s->insertRecord('addmrnqty', $data_arr);
				$stockdata="Select * from stock_maintenance where itemcode='".$_REQUEST['itemcode'][$i]."'";	
				$stock=DB_query($stockdata,$db);
				$stock_row=DB_fetch_array($stock);
			if($stock_row['itemcode']=$_REQUEST['itemcode'][$i] && $stock_row['site']=$_REQUEST['sitecode'] && $stock_row['store']=$_REQUEST['storecode'])
		{
		 $condition1="itemcode='".$_REQUEST['itemcode'][$i]."' && site='".$_REQUEST['sitecode']."' && store='".$_REQUEST['storecode']."'"; 
		$current_bal=$stock_row['stockbalance']+$_REQUEST['reciveqty'][$i];
		//echo $current_bal; exit;
		//print_r($current_bal); exit;
		$balance_arr['site'] = $_REQUEST['sitecode'];
		$balance_arr['store'] = $_REQUEST['storecode'];  
		$balance_arr['itemcode']= $_REQUEST['itemcode'][$i];
		$balance_arr['stockbalance']=$current_bal;
		$balance_arr['created_by']=$_SESSION['UserID'];
		$update1=$s->EditRecord(stock_maintenance, $balance_arr, $condition1);
		$row1=DB_query($update1,$db);
					
					if(!$row1)
					{
	  					 die('Error'.DB_error());
					}
		}
	else{
		//echo $_REQUEST['reciveqty'][$i]; exit;
	$balance_arr['site'] = $_REQUEST['sitecode'];
	$balance_arr['store'] = $_REQUEST['storecode'];  
	$balance_arr['itemcode']= $_REQUEST['itemcode'][$i];
	$balance_arr['stockbalance']= $_REQUEST['reciveqty'][$i];
	
	$balance_arr['created_by']=$_SESSION['UserID'];  
	//print_r($balance_arr);exit;
	$insert_stock_maintenance=$s->insertRecord('stock_maintenance', $balance_arr);
	$row1=DB_query($insert_stock_maintenance,$db);
	
	
	if(!$row1)			
	{
	   die('Error'.DB_error());
	}
	}
//echo "<br>---------------------------Add addmrnqty-------------------------------------------------------------------<br>";
			//	 $sql = "INSERT INTO addmrnqty(`id`, `issue_id`, `issue_qty`, `itemprice`,`itemcode`, `desc`, `Make`, `Color`, `Gread`, `UOM`,`Qcrequd`,`POQty`,`reciveqty`,`short_accessid`,`rejectqty`,	`item_total_rate`,`order_no`,`taxname`,`taxamount_rate`)Values('NULL', '".$Lastreq_id."', '".$_POST['issuebalanc'][$i]."', '".$_POST['itemrate'][$i]."','".$_REQUEST['itemcode'][$i]."','".$_POST['desc'][$i]."', '".$_POST['make'][$i]."', '".$_POST['color'][$i]."', '".$_POST['gread'][$i]."', '".$_POST['uom'][$i]."',   '".$_POST['qcreqd'][$i]."', '".$_POST['poqty'][$i]."','".$_POST['reciveqty'][$i]."','".$_POST['short_accessid'][$i]."','".$_POST['rejectqty'][$i]."','".$_POST['totalrate'][$i]."','".$_REQUEST['orderno']."','".$_POST['taxname'][$i]."',	'".$_REQUEST['taxamount_rate'][$i]."')";					
					$ErrMsg = _('An error occurred Record Note Save') . '. ' . _('The error says');
					$insertResult = DB_query($insert_mrm,$db,$ErrMsg);
				}
		}		
		$Charge_Amount=0;
 		$countcharge = $_REQUEST['countcharges'];
 		 for($P=1;$P<$countcharge;$P++)
		 {
			 if($_POST['desc_'.$P.'']!='')
			 {
			 	$data_arr1['MRN_id']= $Lastreq_id;
				$data_arr1['Inclusive']= $_POST['Charge_'.$P.''];
				$data_arr1['As_Per_Actual']= $_POST['AsPerActual_'.$P.''];
				$data_arr1['charges_name']= $_POST['desc_'.$P.''];
				$data_arr1['charges']= $_POST[$P];
				$data_arr1['charges_amount']= $_POST['actual_'.$P.''];
				$Charge_Amount+=$_POST['actual_'.$P.''];
 //echo  $sql="INSERT INTO MRN_charges(MRN_id, Inclusive, As_Per_Actual, charges_name, charges) VALUES('".$Lastreq_id."','".$_POST['Charge_'.$P.'']."','".$_POST['AsPerActual_'.$P.'']."', '".$_POST['desc_'.$P.'']."','".$_POST[$P]."')"; 
				$sql_MRN_charges=$s->insertRecord('mrn_charges',$data_arr1);
				 $ErrMsg = _('vendor Quotation Order Details is not Created');
				$result =DB_query($sql_MRN_charges,$db,$ErrMsg);	
			 }
		 }
		 		 
//echo "<br>--------------------------UPDATE------------------------------------------------------------------<br>";
	$data['total_Item_Amount'] = $_REQUEST['itemamount']; 
	$percent_inclusivecharge=$_REQUEST['percent_inclusivecharge'];
	$tax_chargeamount=$_REQUEST['tax_chargeamount'];
 	$total_Item_Amount=$_REQUEST['tax_chargeamount'];
	  
	  if($_REQUEST['percent_inclusivecharge']=='SUR')
		 {
	  	  $tax_suroct_value	=($_REQUEST['tax_chargeamount']*$_REQUEST['percent_inclusive'])/100;
		 }
		 else
		 {
		   $tot=$_REQUEST['tax_chargeamount']+$_REQUEST['total_Item_Amount'];
		   $tax_suroct_value=($tot*$_REQUEST['percent_inclusive'])/100;
 		 }
	
	$data['percent_inclusivecharge']= $_REQUEST['percent_inclusivecharge'];  	 
	$data['percent_inclusive'] = $_REQUEST['percent_inclusive'];  
 	$data['tax_suroct_value'] = $tax_suroct_value;  
	$data['Charge_Amount'] = $Charge_Amount;
    $data['grandtotal'] =$_REQUEST['grandtotal'];
	$cond=" id='".$Lastreq_id."'";	
	$update=$s->EditRecord('addmrn', $data, $cond);
	$updateResult = DB_query($update,$db,$ErrMsg);
	if(!$update)
	{
  		die("Not Updated addmrn".DB_error_msg($db));
	}
//*****************************************************************************************************		
	$PO_quantity_SQL = "select Sum(POQty), Sum(reciveqty) FROM  addmrnqty  Where order_no='".$_REQUEST['orderno']."'";
	$PO_quantityResult =DB_query($PO_quantity_SQL,$db,$ErrMsg);
	$PO_quantity_row=DB_fetch_array($PO_quantityResult);
//echo "<br>---------------------------po_quantity------------------------------------------------------------------<br>";
     $SQL_query = "INSERT INTO po_quantity(`id`,
	  `PO_Quantity`, 
	  `Receive_Quantity`, 
	  `orderno`)
	   Value('NULL','".$PO_quantity_row['Sum(POQty)']."','".$PO_quantity_row['Sum(reciveqty)']."','".$_REQUEST['orderno']."')";
		  
$insertresult = DB_query($SQL_query,$db,$ErrMsg);
if($_REQUEST['receiviedagaist']=='PO')
{  $qty1=$_REQUEST['short_accessid'];
   for($i=0;$i<count($qty1);$i++)
   {   
	if($_REQUEST['short_accessid'][$i]==0)
	{
	  $update1="update purchorderdetails set status=1,pending_qty='".$_REQUEST['short_accessid'][$i]."' where orderno='".$_REQUEST['orderno']."'";	
	 $updatequery=DB_query($update1,$db);	
	}
	else {
	 $update1="update purchorderdetails set status=0,pending_qty='".$_REQUEST['short_accessid'][$i]."' where orderno='".$_REQUEST['orderno']."'";	
	   $updatequery=DB_query($update1,$db);	
	}
   }
   $sql="select * from purchorderdetails where status=1 and orderno='".$_REQUEST['orderno']."'";
   $query=DB_query($sql,$db);
   $num=DB_num_rows($query);
   if($num>0)
   {
   	$Update_sql="UPDATE purchorders SET Po_recive_status='Receive' where orderno='".$_REQUEST['orderno']."'";
	$Updateresult = DB_query($Update_sql,$db);	
   }
else
	{
	$Update_sql="UPDATE purchorders SET Po_recive_status='Pending' where orderno='".$_REQUEST['orderno']."'";
	$Updateresult = DB_query($Update_sql,$db);
    }
}
else if($_REQUEST['receiviedagaist']=='IMPO')
{
	$Update_sql="UPDATE import_purchaseorder SET status='received' where id='".$_REQUEST['orderno']."'";
	$Updateresult = DB_query($Update_sql,$db);
}			
			
			//if($PO_quantity_row['Sum(POQty)'] == $PO_quantity_row['Sum(reciveqty)'])
			/*{
				if($data_arr['Po_recive_status']=="Y") // when status is not pending				
				{
					$Update_sql="UPDATE purchorders SET Po_recive_status='Receive' where orderno='".$_REQUEST['orderno']."'";
					$Updateresult = DB_query($Update_sql,$db);
				}
				 elseif($data_arr['status']=="N")
				{
					$Update_sql="UPDATE purchorders SET Po_recive_status='Pending' where orderno='".$_REQUEST['orderno']."'";
					$Updateresult = DB_query($Update_sql,$db);

				} 
			}*/
			  
	header('location:index.php?route=MaterialReceiptNote&modid='.$_REQUEST['modid'].'&frmid='.$_REQUEST['frmid'].'');
	
	}
	}
	unset($_SESSION['manvi_new']);
	unset($_SESSION['receiviedagaist']);
	unset($_SESSION['sitecode']);
	unset($_SESSION['sitename']);
	unset($_SESSION['hidden_site']);
	unset($_SESSION['vendor']);
	unset($_SESSION['PO']);
	unset($_SESSION['credit_days']);
	unset($_SESSION['MRNdate']);
	unset($_SESSION['matrialreceipt']);
	unset($_SESSION['storecode']);
	unset($_SESSION['storename']);
	unset($_SESSION['rmrs_no']);
	unset($_SESSION['orderdate']);
	unset($_SESSION['billno']);
	unset($_SESSION['MRNVoucherDate']);
	unset($_SESSION['gatepassno']);
	unset($_SESSION['inwardate_save']);
	unset($_SESSION['transport']);
	unset($_SESSION['vehicalno']);
	unset($_SESSION['timein']);
	unset($_SESSION['timeout']);
	unset($_SESSION['lrno']);
	unset($_SESSION['lrdate']);
	unset($_SESSION['total_weight']);
	unset($_SESSION['RemarksIfAny']);
	unset($_SESSION['DocumentType']);
}
?>

<div class="body_contain">
<div class="tab_div">



<form action="" method="post">
<table width="100%"border="0" cellpadding="2" cellspacing="1">
<?php 
if(isset($msg_error))
{
?>
  <tr>
  		<td colspan="5">
		<table width="50%" border="0"  style="color:#FF0000;font-weight:bold">
	  <tr>
 		<?php for($i=0;$i<count($msg_error);$i++)
		{	
		  if($i%2==0)	
		  echo "</tr><tr>";
 	    	echo "<td>".$msg_error[$i]."</td>" ;
		}?>
		</table>		</td>
  </tr>
 <?php } ?> 
<tr>
<td>
<div style="width:800px;">
<h1>General Details</h1>
<table width="100%" border="0" cellspacing="2" cellpadding="1" align="center">
  <tr class="row1">
    <td class="tabledata1">Mrn No. </td>
    <td colspan="2"><input type="text" value="Will Be Generated On Saving"  style="width:168px;" name="MRNNo" readonly="true"></td>
    <td class="tabledata1">MRN Date </td>
    <td colspan="2" style="padding-right:5px"><input type="text" name="MRNdate" value="<?php if(isset($_SESSION['MRNdate']))echo $MRNdate; else echo date('d/m/Y');?>" id="MRNdate" class="datepicker" style="width:168px;" ></td>
  </tr>
  <tr class="row1">
    <td class="tabledata1">Received Against</td>
    <td colspan="2">
	<input id="hidden_form_site" type="hidden" value="<?php echo $_REQUEST['sitecode'];?>" />
    <input id="hidden_form_type" type="hidden" value="<?php echo $_GET['type'];?>" />
	<select name="receiviedagaist" id="receiviedagaist"  onchange="PurchaseOrderNumber_1();" style="width:168px">
  <option value="000">---Select---</option>
  <option value="PO" <?php if($_GET['type']=='PO') { echo "Selected";  }?> >Purchase Order</option>
  <option value="Direct" <?php if($_GET['type']=='Direct') { echo "Selected";  }?>>Direct</option>
  <option value="IMPO" <?php if($_GET['type']=='IMPO') { echo "Selected";  }?>>Import Purchase Order</option>
</select>		
	<input type="hidden" name="receive_type" id="receive_type" value="<?php echo $_GET['type'];?>" />	</td>
    <td class="tabledata1">Received On Basis </td>
    <td colspan="2"><select name="matrialreceipt" id="matrialreceipt" style="width:168px">
	<option value="000">---Select---</option>	
	<?php 
	if($_GET['type']=='PO')
	{ ?>
 		<option value="Credit"<?php echo($matrialreceipt=='Credit'?'selected':'')?>>Credit</option>
		<option value="Cash"<?php echo($matrialreceipt=='Cash'?'selected':'')?>>Cash</option>
		<option value="Debitable"<?php echo($matrialreceipt=='Debitable'?'selected':'')?>>Debitable</option>
		<option value="Debitable"<?php echo($matrialreceipt=='rental'?'selected':'')?>>Debitable</option>
		<option value="Hiring"<?php echo($matrialreceipt=='Hiring'?'selected':'')?>>Hiring</option>
	<?php }
	else
	{?>
 	<option value="Credit"<?php echo($matrialreceipt=='Credit'?'selected':'')?>>Credit</option>
	<option value="Cash"<?php echo($matrialreceipt=='Cash'?'selected':'')?>>Cash</option>
<?php } ?>	
	</select></td>
  </tr>
  <tr class="row1">
    <td class="tabledata1">Branch</td>
    <td colspan="2">
	<table width="73%" border="0" cellspacing="0" cellpadding="0">
	<tr>
	<td><select name="sitecode" onchange="showsitename111(this.value)" id="sitecode" style="width:105px">
      <option value="000">---Select---</option>
      <?php 
	 $sql = "SELECT loccode, locationname FROM locations where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode='".$_SESSION['AssignedBranch']."'"; 
	$resultStkLocs = DB_query($sql,$db);
	  while ($myrow=DB_fetch_array($resultStkLocs))
	  {
 		?>
      <option value="<?php echo $myrow['loccode']; ?>" <?php echo ($sitecode==$myrow['loccode']?'selected':'')?>><?php echo $myrow['locationname'];?></option>
      <?php  }	?>
    </select></td>
	<td><div id="txtsite">
	<?php
	$site_sql="Select locationname from locations where loccode ='".$sitecode."'";
	$site_res=DB_query($site_sql,$db);
	$site_row=DB_fetch_array($site_res);
	if(isset($sitecode))
	{
	?>
	<input name="sitename" type="text" id="sitename"  value="<?php echo $site_row['locationname'];?>"  style="width:60px;" readonly="true">
	<?php } else {?>
	<input name="sitename" type="text" id="sitename"  value="<?php echo $sitename;?>"  style="width:60px; float:left;" readonly="true">
	<?php } ?>
	<input type="hidden" style="width:50px;" name="hidden_site" id="hidden_site" value="<?php echo $_REQUEST['hidden_site'];?>"/></div></td>
	</tr>
	<tr></tr>
	</table>
	</td>
    <td class="tabledata1">Godown</td>
    <td>
	<table width="73%" border="0" cellspacing="0" cellpadding="0">
      <tr>
		<td>	
		<?php
		$sql_store = "select distinct StoreCode, StoreName from storetype order by StoreCode ASC";
		$resultStkLocs = DB_query($sql_store,$db);
		?>
		<select name="storecode" onChange="changestorename(this.value)" style="width:100px">
		<option value="000">Select</option> 
		<?php 
		while ($myrow=DB_fetch_array($resultStkLocs))
		{
				$p='';
				if($myrow['StoreCode']==$storecode)
				$p="selected";
		?>
			<option Value="<?php echo $myrow['StoreCode']; ?>" <?php echo $p; ?>><?php echo $myrow['StoreCode']; ?></option>
		<?php }
		?>
		</select>		</td>
        <td><div id="txtid"><input type="text" name="storename" id="storename" style="width:60px;" readonly="true" value="<?php echo $storename; ?>"></div></td>
      </tr>
    </table></td>
  </tr>
  <tr class="row1">
    <td class="tabledata1" scope="row">Recevied From </td>
    <td colspan="2"><?php 
		 if($_GET['type']=='PO' || $_GET['type']=='IMPO')
		 {
		 	$sql="select vendor_code, vendor_name from suppliers";
			$result = DB_query($sql, $db);
			?>
			<select name="vendor" id="vendor" onchange="get_order(this.value)" style="width:168px">
			<option value="000">---Select---</option>
			<?php
			while($myrow = DB_fetch_array($result))
			{ 
			$p='';
			if($myrow['vendor_code']==$vendor)
			$p="selected";
			?>
			 <option  value="<?php echo $myrow['vendor_code']; ?>"<?php echo $p;?>><?php echo $myrow['vendor_name']; ?></option>
		<?php }
			?>
			</select>
			<?php 
		 }
		 else if($_GET['type']=='Direct')
		 {
		 	$sql="select vendor_code, vendor_name from suppliers";
			$result = DB_query($sql, $db);
			?>
			<select name="vendor" id="vendor" onchange="get_creditdays(this.value)" style="width:168px">
			<option value="000">---Select---</option>
			<?php
			while($myrow = DB_fetch_array($result))
			{ 
			$p='';
			if($myrow['vendor_code']==$vendor)
			$p="selected";
			?>
			 <option  value="<?php echo $myrow['vendor_code']; ?>"<?php echo $p;?>><?php echo $myrow['vendor_name']; ?></option>
		<?php }
			?>
			</select>
			<?php 
		 }
		else { ?>
		<select name="vendor" style="width:168px">
		 <option value="0">Select</option>
		</select>
	  <?php } ?></td>
    <td class="tabledata1">R.M.R.S. No. </td>
    <td colspan="2"><input name="rmrs_no" type="text" value="<?php  echo $rmrs_no;?>" style="width:168px" /></td>
  </tr>
  <tr class="row1">
    <td class="tabledata1" scope="row">PO/Challan/WO/ Ref.No.</td>
    <td colspan="2">
	<?php
if($_REQUEST['type']=='PO')
{
 	  $sql="select orderno, PO_Orderno from purchorders Where Po_recive_status='Pending' and sitecode='".$_REQUEST['sitecode']."' and supplierno='".$_REQUEST['vendor']."'";
	$result = DB_query($sql,$db);
?>
<div id="order"><select name="PO" id="PurchaseOrder" onchange="FiendPurchasenumber(this.value)"  style="width:168px">
<option value="000">---Select---</option>
<?php
 while($myrow=DB_Fetch_array($result))
 {
$p='';
if($myrow['orderno']==$_REQUEST['orderno'])
$p="selected";
?>
<option value="<?php echo $myrow['orderno']; ?>"<?php echo $p;?>><?php echo $myrow['PO_Orderno']; ?> </option>
<?php }?> 
</select></div>
<?php }
else if($_GET['type']=='IMPO')
{
	$sql=DB_query("select id,orderNo from import_purchaseorder where site='".$sitecode."' and vendor='".$_GET['vendor']."' and status='pending' ",$db);
?>
<div id="order"><select name="PO" id="PurchaseOrder" onchange="FiendPurchasenumber(this.value)"  style="width:168px">
<option value="000">---Select---</option>
<?php
 while($myrow=DB_Fetch_array($sql))
 {
?>
<option value="<?php echo $myrow['id']; ?>"<?php echo ($_REQUEST['orderno']==$myrow['id']?'selected':'')?>><?php echo $myrow['orderNo']; ?> </option>
<?php }?> 
</select></div>
<?php }
 else if($_REQUEST['type']=='Direct') { ?>
<input type="text"  id="PO" name="PO"  size="15" value="<?php echo $PO; ?>" style="width:100%"/>
<?php } 
else{?>
<select name="" style="width:100px">
<option value="000">---Select---</option>
</select>
<?php } ?>
</td>
    <td class="tabledata1">Order Date </td>
    <td colspan="2"><?php
	   if($_REQUEST['type']=='PO')
	   { 
	   $date_sql="Select orddate from purchorders where orderno='".$_REQUEST['orderno']."' ";
	   $date_res=DB_query($date_sql,$db);
	   $date_row=DB_fetch_array($date_res);
	   $Order_Date=$date_row['orddate'];
	   ?>
	    <input type="text" name="orderdate" id="orderdate" value="<?php echo ConvertSQLDate($Order_Date); ?>" readonly="true" style="width:168px">
		<?php }else{
	    ?>
	   <input type="text" name="orderdate" id="orderdate" value="<?php if(isset($_SESSION['orderdate'])) echo $orderdate; else echo date('d/m/Y'); ?>" readonly="true" class="datepicker" style="width:168px" ></td>
	   <?php } ?>
  </tr>
  <tr class="row1">
  <td class="tabledata1">Credit Days </td>
  <td colspan="2">
  <?php 
  if($_REQUEST['type']=='PO'){
  $Credit_sql="select A.payment_terms , B.* from purchorders A, paymentterms B where A.payment_terms=B.termsindicator and 
orderno='".$_REQUEST['orderno']."'";
  $Credit_res=DB_query($Credit_sql,$db);
  $Credit_row=DB_fetch_array($Credit_res);
  ?>
  <input type="text" name="credit_days" id="credit_days" value="<?php echo $Credit_row['daysbeforedue'];?>" style="width:168px;" readonly="true"/>
  <?php } else if($_REQUEST['type']=='Direct' || $_REQUEST['type']=='IMPO'){?>
  <div id="creditid"><input type="text" style="width:168px" name="credit_days" id="credit_days" value="<?php echo $credit_days;?>" readonly="true"/></div>
  <?php } ?>
  </td> 
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  </tr>
  
  
  
    <tr class="row1">
    <td class="tabledata1" scope="row">Document Type</td>
    <td colspan="2">
		<select style="width:105px" name="DocumentType">
			<option id="Challan">Challan</option>		
			<option id="Bill">Bill</option>		
		</select>
	<input name="billno" type="text" id="billno" style="width:60px" value="<?php  echo $billno; ?>" />
	</td>
    <td class="tabledata1">Voucher Date</td>
    <td colspan="2"><input type="text" name="MRNVoucherDate" style="width:168px" class="datepicker"  onblur="valdate();"  value="<?php if(isset($_SESSION['MRNVoucherDate']))echo $MRNVoucherDate; else echo date('d/m/Y'); ?>"/></td>
	</tr>
  
    <tr class="row1">
    <td class="tabledata1" scope="row">Inwards Gate Pass No.</td>
    <td colspan="2"><input type="text" name="gatepassno" style="width:168px" id="gatepassno"  value="<?php echo $gatepassno; ?>" /></td>
    <td class="tabledata1">Date</td>
    <td colspan="2"><input type="text" name="inwardate_save" style="width:168px" id="inwardate_save"  value="<?php if(isset($_SESSION['inwardate_save']))echo $inwardate_save; else echo date('d/m/Y');?>" class="datepicker"  /></td>
	</tr>
    <tr class="row1">
    <td class="tabledata1" scope="row">Transporter Name</td>
    <td colspan="2"><?php  
		$sql_trans = 'select vendor_code, vendor_name from suppliers Where vendor_type="Transporter"';
		$resulttransp = DB_query($sql_trans,$db);
		  ?>
		<select name="transport" style="width:168px">
		 <?php 
		 while ($myrow=DB_fetch_array($resulttransp))
		 {?>
		 	<option selected Value="<?php echo $myrow['vendor_code'];?>"<?php echo ($transport==$myrow['vendor_code']?'selected':'')?>><?php echo $myrow['vendor_name'];?></option>
		 <?php }
		?>
		</select></td>
    <td class="tabledata1">Vehicle No.</td>
    <td colspan="2"><input name="vehicalno" type="text" id="vehicalno" style="width:168px" value="<?php echo $vehicalno; ?>"/></td>
	</tr>
    <tr class="row1">
    <td class="tabledata1" scope="row">Time</td>
    <td colspan="2"><select name="" onchange="showdivdata(this.value)" style="width:50px">
			<option value="In">In</option>		
			<option value="Out">Out</option>		
		</select>
	
		<div id="show">
      <input name="timein" type="text" id="timein" style="width:50px" value="<?php if(isset($_SESSION['timein']))echo $timein; else echo $time;?>" /> </div>


	  <div id="hide" style="display:none">
        <input name="timeout" type="text" id="timeout" style="width:50px;" value="<?php if(isset($_SESSION['timeout'])) echo $timeout; else echo $time;?>" onblur="Comparetime();" />
</div>

	
	</td>
    <td class="tabledata1">L R No. </td>
    <td colspan="2"><input name="lrno" type="text" id="lrno" style="width:168px"  value="<?php echo $lrno; ?>"/></td>
	</tr>
    <tr class="row1">
    <td class="tabledata1" scope="row">L R Date</td>
    <td colspan="2"><input name="lrdate" type="text" id="lrdate" style="width:168px" value="<?php if(isset($_SESSION['lrdate'])) echo $lrdate; else echo date('d/m/Y'); ?>"  onblur="LRdate();" class="datepicker"/></td>
    <td class="tabledata1">Total Weight</td>
    <td colspan="2"><input type="text" name="total_weight" id="total_weight" style="width:168px"  value="<?php echo $total_weight; ?>"/></td>
	</tr>
    <tr class="row1">
    <td class="tabledata1" scope="row">Remarks If Any</td>
    <td colspan="5"><input name="RemarksIfAny" type="text" id="RemarksIfAny" value="<?php echo $RemarksIfAny;?>" style="width:100%; height:50px;"></td>
	</tr>
  
</table>

</div>
</td>
</tr>
</table>
<?php if($_GET['type']!=''){?>
<table width="100%" border="0" cellspacing="1" cellpadding="2">
<tr>
<td>
<?php if($_GET['type'] =='PO')
{
	if(isset($_GET['orderno'])){
				$CR=1;
			  	$update_sql ="select * from purchorderdetails WHERE orderno='".$_REQUEST['orderno']."' and status=0 ";
				$updateSqlresult=DB_query($update_sql, $db);
 				?>
<fieldset><legend>ITEM DETAILS</legend>
<div style="overflow:auto; width:800px ; height:auto"  >
<table width="100%" border="1" cellspacing="1" cellpadding="2">
  <tr>
    <td class="tabledata1" scope="col">S. NO. </td>
    <td class="tabledata1" scope="col">ITEM CODE </td>
    <td class="tabledata1" scope="col">DESCRIPTION</td>
    <td class="tabledata1" scope="col">MAKE</td>
    <td class="tabledata1" scope="col">GRADE</td>
    <td class="tabledata1" scope="col">COLOR</td>
    <td class="tabledata1" scope="col">UOM</td>
    <td class="tabledata1" scope="col">PO QTY </td>
      <td class="tabledata1" scope="col">Pending Qty </td>
     <td class="tabledata1" scope="col">QTY AS PER BILL </td>
    <td class="tabledata1" scope="col">QTY ACCEPTED </td>
    <td class="tabledata1" scope="col">SHORT QUANTITY </td>
    <td class="tabledata1" scope="col">RATE</td>
	<td class="tabledata1" scope="col">TOTAL ITEM PRICE</td>
    <td class="tabledata1" scope="col">DISCOUNT TYPE </td>
    <td class="tabledata1" scope="col">DISCOUNT</td>
	<td class="tabledata1" scope="col">DISCOUNT AMOUNT</td>
    <td class="tabledata1" scope="col">VALUE AFTER DISCOUNT </td>
    <!--<td class="tabledata1" scope="col">TAX TYPE </td>
    <td class="tabledata1" scope="col">TAX RATE </td>
    <td class="tabledata1" scope="col">TAX VALUE </td>
    <td class="tabledata1" scope="col">VALUE WITH TAX </td>-->
    <td class="tabledata1" scope="col">REMARKS</td>
  </tr>
 <?php				
				 $i=0;
				  while($Itemrow=DB_fetch_array($updateSqlresult))
				  {
				   	$reciveqty = $_REQUEST['reciveqty'][$i];
					$rejectqty=$_REQUEST['rejectqty'][$i];
					$short_accessid=$_REQUEST['short_accessid'][$i];
					$uom=$_REQUEST['uom'][$i];
					$poqty=$_REQUEST['poqty'][$i];
					$totalrate=$_REQUEST['totalrate'][$i];
					$taxname=$_REQUEST['taxname'][$i];
					$taxamount=$_REQUEST['taxamount'][$i];
					$_REQUEST['discount'][$i]=$Itemrow['Discount'];
					$_REQUEST['val_after_disc'][$i]=$Itemrow['Sub_total'];
					$_REQUEST['discount_amount'][$i]=$Itemrow['discount_amount'];
	?>
<tr id="txt_order">
<td><?php echo $i+1;?></td>
<td><input type="hidden"   name="qcreqd[]"size="10" value="<?PHP echo $numrow;?>" />
	<input type="hidden"   name="hidden_addqty[]"size="10" value="<?PHP echo $Itemrow['id'];?>" />
	<input type="hidden" value="" id="user_shipping" name="user_shipping[<?php echo $i;?>]">
	<?php echo $Itemrow['itemcode'] ?><input type="hidden" readonly="" id="washing_profile" name="itemcode[]"size="15" onblur="PO_order(this.value)" value="<?php echo $Itemrow['itemcode'] ?>" />
	<input type="hidden" name="orderno"  id="orderno" onblur="PO_order(this.value)"  value="<?php echo $_REQUEST['orderno'];?>" /></td>
<td><?php echo $Itemrow['itemdescription'] ?><input type="hidden" name="desc[]"   id="desc" size="25" value="<?php echo $Itemrow['itemdescription'] ?>"/></td>
<td><?php 
$makename=$s->getField('product_manufacture','id',$Itemrow['make'],'manufacturer');
echo $makename; ?><input name="make[]" id="make[]" type="hidden" value="<?php echo $Itemrow['make'] ?>" />&nbsp;</td>
<td><?php 
$gradename=$s->getField('product_grade','id',$Itemrow['grade'],'grade');
echo $gradename ?><input name="gread[]" id="gread[]" type="hidden" value="<?php echo $Itemrow['grade'] ?>">&nbsp;</td>
<td><?php 
$colorname=$s->getField('product_color','id',$Itemrow['color'],'color');
echo $colorname ?><input name="color[]" id="color[]" type="hidden" value="<?php echo $Itemrow['color'] ?>" >&nbsp;</td>
<td><?php 
$unitname=$s->getField('unitsofmeasure','unitid',$Itemrow['uom'],'unitname');
echo $unitname;?><input name="uom[]" id="uom[]" type="hidden" value="<?php echo $Itemrow['uom'] ?>">&nbsp;</td>
<td><?php 	 echo $Itemrow['quatity'];?><input name="poqty[]" type="hidden" id="poqty_<?php echo $i; ?>" value="<?php echo $Itemrow['quatity'];?>" size="10"/></td>
<td><?php if(empty($Itemrow['pending_qty'])) { echo  $Itemrow['quatity']; } else {  echo $Itemrow['pending_qty'];  }?><input type="hidden" name="pend_qty[<?php echo $i; ?>]" 
	 value="<?php if(empty($Itemrow['pending_qty'])) { echo  $Itemrow['quatity']; } else {  echo $Itemrow['pending_qty'];  } ?>" /></td>
<td><input name="qty_as_per_bill[]" type="text" id="qty_as_per_bill_<?php echo $i; ?>" value="" size="8" onblur="check_bill_qty(this.value,<?php echo $i;?>)"/></td>
<td><input name="reciveqty[]" type="text" id="reciveqty_<?php echo $i; ?>"    size="8" onblur="calculate_short(<?php echo $i ?>)" value="<?php echo $_REQUEST['reciveqty'][$i];?>"/></td>
<td><input name="short_accessid[]" type="text" id="short_accessid_<?php echo $i; ?>" value="<?php echo $_REQUEST['short_accessid'][$i];?>" size="8" onfocus="calculate_poshort(<?php echo $i ?>);"/></td>
<td><?php echo $Itemrow['Rate'];?><input name="itemrate[]" type="hidden" id="itemrate_<?php echo $i; ?>" value="<?php echo  $Itemrow['Rate'];?>" size="8"/></td>

<td><input name="item_total_rate[]" type="text" id="item_total_rate_<?php echo $i; ?>" value="" size="8" onfocus="calc_total_price(<?php echo $i;?>)"/></td>
<td>
<select name="disc_type[]" id="disc_type_<?php echo $i;?>">
<option value="Amount" <?php echo ($Itemrow['discount_type']=='Amount')?'selected':''?>>Amount</option>
<option value="PercentAmt" <?php echo ($Itemrow['discount_type']=='PercentAmt')?'selected':''?>>Percent Amt</option>
</select></td>
<td><input name="discount[]" type="text" id="discount_<?php echo $i;?>" value="<?php echo $_REQUEST['discount'][$i];?>" size="10" onblur="calculate_discount(<?php echo $i;?>);"/></td>
<td><input name="discount_amount[]" type="text" id="discount_amount_<?php echo $i;?>" value="<?php echo $_REQUEST['discount_amount'][$i]; ?>" size="12" /></td>
<td><input name="val_after_disc[]" type="text" id="val_after_disc_<?php echo $i;?>" value="<?php echo $_REQUEST['val_after_disc'][$i];?>" size="12" readonly="true"/></td>

<td><input type="text" name="remarks[]" id="remarks_<?php echo $i;?>" value="<?php  echo $_REQUEST['remarks'][$i];?>"/></td>
</tr>
  <?php $i++;} ?>
<input type="hidden" name="countval" value="<?php echo $i; ?>"  id="rowcount"/>
</table>
</div></fieldset>

<?php  }} 
else if($_GET['type']=='Direct')
{?>
<p align="left"><input type="submit" name="item_popup" value="Pick Items" id="item_popup"  onclick="open_items();" /></p>
<div align="center"><fieldset style="width:auto"><legend>ITEM DETAILS</legend>
<div style="overflow:auto; width:750px; height:160px">
<table width="100%" border="0" cellspacing="3" cellpadding="2">
  <tr>
	<!--<td class="tabledata1" scope="col">ADD ITEM</td>-->
		<td class="tabledata1">ITEM CODE </td>
		<td class="tabledata1">DESCRIPTION</td>
		<td class="tabledata1">MAKE</td>
		<td class="tabledata1">GRADE</td>
		<td class="tabledata1">COLOR</td>
		<td class="tabledata1">SIZE</td>
		<td class="tabledata1">GSM</td>
		<td class="tabledata1">UOM</td>
		<td class="tabledata1">ITEM NO.</td>
		<td class="tabledata1">FLEXO/OFFSET</td>
		<td class="tabledata1">WIDTH</td>
		<td class="tabledata1">LENGTH</td>
		<td class="tabledata1">OD</td>
		<td class="tabledata1">SERIAL NO.</td>
		<td class="tabledata1">ORIGINAL/LOCAL</td>
		<td class="tabledata1">TYPE</td>
		<td class="tabledata1">SHADE</td>
		<td class="tabledata1">RUNNING METER/ROLL</td>
		<td class="tabledata1">VOLUME</td>
		<td class="tabledata1">TEETH</td>
		<td class="tabledata1">BCM</td>
		<td class="tabledata1">UPS</td>
		<td class="tabledata1">QUALITY</td>
		<td class="tabledata1">QTY AS PER BILL </td>
		<td class="tabledata1">QTY ACCEPTED </td>
		<td class="tabledata1">SHORT QUANTITY </td>
		<td class="tabledata1">RATE</td>
		<td class="tabledata1">TOTAL ITEM PRICE</td>
		<td class="tabledata1">DISCOUNT TYPE </td>
		<td class="tabledata1">DISCOUNT</td>
		<td class="tabledata1">DISCOUNT AMOUNT</td>
		<td class="tabledata1">VALUE AFTER DISCOUNT </td>
		<td class="tabledata1">REMARKS</td>
  </tr>
<?php 
if($_SESSION['manvi_new']!='')
{ 
//print_r($_SESSION['manvi_new']);
$itemArray=array();
$itemArray=explode('#',$_SESSION['manvi_new']);
//print_r($itemArray);
for($j=0;$j<count($itemArray);$j++)
{
if($j%2==0)
		{$bg="#CCCCCC";}
		else
		$bg="#C8BECB";
$item_sql="Select * from item_master where StockID='".$itemArray[$j]."'";
$item_result=DB_query($item_sql,$db);
$item_row=DB_fetch_array($item_result);

$rate_sql="select * from rate_master where ItemId='".$itemArray[$j]."' and PartyId='".$vendor."' and Category='Purchase' and DeActivate='0' ";
$rate_result=DB_query($rate_sql,$db);
$rate_row=DB_fetch_array($rate_result);
$_REQUEST['itemrate'][$j]=$rate_row['Rate'];
 ?>
<tr bgcolor="<?php echo $bg;?>">
<td><?php echo $itemArray[$j];?>
<input type="hidden" name="qcreqd[]"size="10" value="<?php echo $j;?>" />
<input type="hidden" readonly="" id="washing_profile_<?php echo $j;?>" name="itemcode[<?php echo $j;?>]"size="10" onBlur="PO_order(this.value)" value="<?php echo $itemArray[$j]?>"/></td>
<td><?php echo $item_row['item_name'];?>
<input name="desc[<?php echo $j;?>]" type="hidden" id="desc_<?php echo $j;?>" value="<?php echo $item_row['item_name']?>" readonly/></td>
<td>
<?php 
$makespecs_sql="select manufacture from item_specification where proc_id='".$itemArray[$j]."'";
$makespecs_result=DB_query($makespecs_sql,$db);
?>
<select name="make[<?php echo $j;?>]" id="make_<?php echo $j;?>" style="width:110px">
<?php 
while($makespecs_row=DB_fetch_array($makespecs_result))
{
 $make_sql=DB_query("Select manufacturer from product_manufacture where id='".$makespecs_row['manufacture']."'",$db);
 $make_result=DB_fetch_array($make_sql);
 
?>
<option value="<?php echo $makespecs_row['manufacture']?>"><?php echo $make_result['manufacturer']?></option>
<?php } ?>
</select></td>
<td>
<?php 
$gradespecs_sql="Select grade from item_specification where proc_id='".$itemArray[$j]."'";
$gradespecs_result=DB_query($gradespecs_sql,$db);
?>
<select name="gread[<?php echo $j;?>]" id="gread_<?php echo $j;?>" style="width:110px">
  <?php 
while($gradespecs_row=DB_fetch_array($gradespecs_result))
{ 
	$grade_sql=DB_query("Select grade from product_grade where id='".$gradespecs_row['grade']."'",$db);
 	$grade_result=DB_fetch_array($grade_sql);
?>
  <option value="<?php echo $gradespecs_row['grade']?>"><?php echo $grade_result['grade']?></option>
  <?php }?>
</select></td>
<td>
<?php 
$colorspecs_sql="Select color from item_specification where proc_id='".$itemArray[$j]."'";
$colorspecs_result=DB_query($colorspecs_sql,$db);
?>
<select name="color[<?php echo $j;?>]" id="color_<?php echo $j;?>" style="width:110px">
<?php 
while($colorspecs_row=DB_fetch_array($colorspecs_result))
{ 
	$color_sql=DB_query("Select color from product_color where id='".$colorspecs_row['color']."'",$db);
 	$color_result=DB_fetch_array($color_sql);
?>
<option value="<?php echo $colorspecs_row['color']?>"><?php echo $color_result['color']?></option>
<?php }?>
</select></td>
<td>
<?php 
$sizespecs_sql="Select size from item_specification where proc_id='".$itemArray[$j]."'";
$sizespecs_result=DB_query($sizespecs_sql,$db);
?>
<select name="size[<?php echo $j;?>]" id="size_<?php echo $j;?>" style="width:110px">
<?php 
while($sizespecs_row=DB_fetch_array($sizespecs_result))
{ 
	$size_sql=DB_query("Select name from size_master where id='".$sizespecs_row['size']."'",$db);
 	$size_result=DB_fetch_array($size_sql);
?>
<option value="<?php echo $sizespecs_row['size']?>"><?php echo $size_result['name']?></option>
<?php }?>
</select></td>
<td>
<?php 
$gsmspecs_sql="Select gsm from item_specification where proc_id='".$itemArray[$j]."'";
$gsmspecs_result=DB_query($gsmspecs_sql,$db);
?>
<select name="gsm[<?php echo $j;?>]" id="gsm_<?php echo $j;?>" style="width:110px">
<?php 
while($gsmspecs_row=DB_fetch_array($gsmspecs_result))
{ 
	$gsm_sql=DB_query("Select gsm from gsm where id='".$gsmspecs_row['gsm']."'",$db);
 	$gsm_result=DB_fetch_array($gsm_sql);
?>
<option value="<?php echo $gsmspecs_row['gsm']?>"><?php echo $gsm_result['gsm']?></option>
<?php }?>
</select></td>
<td><?php 
$unitname=$s->getField('unitsofmeasure','unitid',$item_row['unit'],'unitname');
echo $unitname;
?>
<input name="uom[<?php echo $j;?>]" type="hidden" id="uom_<?php echo $j;?>" size="5" readonly value="<?php echo $item_row['unit'];?>"></td>
<td><?php echo $item_row['item_no'];?></td>
<td><?php echo $item_row['flexo_offset'];?></td>
<td><?php echo $item_row['width'];?></td>
<td><?php echo $item_row['length'];?></td>
<td><?php echo $item_row['od'];?></td>
<td><?php echo $item_row['serial_no'];?></td>
<td><?php echo $item_row['orig_local'];?></td>
<td><?php echo $item_row['type_name'];?></td>
<td><?php echo $item_row['shade'];?></td>
<td><?php echo $item_row['running_roll'];?></td>
<td><?php echo $item_row['volume'];?></td>
<td><?php echo $item_row['teeth'];?></td>
<td><?php echo $item_row['bcm'];?></td>
<td><?php echo $item_row['ups'];?></td>
<td><?php echo $item_row['quality'];?>
<input name="poqty[]" type="hidden" id="poqty_<?php echo $j; ?>" value="<?php echo  $item_row['quality']; ?>">

</td>
<td><input name="qty_as_per_bill[]" type="text" id="qty_as_per_bill_<?php echo $j;?>" value="<?php echo $_REQUEST['qty_as_per_bill'][$j];?>" size="10"/></td>
<td><input name="reciveqty[]" type="text" id="reciveqty_<?php echo $j; ?>" size="15" onBlur="calculate_short(<?php echo $j ?>)" value="<?php echo $_REQUEST['reciveqty'][$j];?>"/></td>
<td><input name="short_accessid[]" type="text" id="short_accessid_<?php echo $j; ?>" value="<?php echo $_REQUEST['short_accessid'][$j];?>" size="15" onFocus="calculate_poshort1(<?php echo $j; ?>);" readonly="true"/></td>
<td><input name="itemrate[]" type="text" id="itemrate_<?php echo $j; ?>" value="<?php echo  $_REQUEST['itemrate'][$j];?>" size="15"/></td>
<td><input name="item_total_rate[]" type="text" id="item_total_rate_<?php echo $j;?>" value="" size="15" onFocus="calc_total_price(<?php echo $j;?>)"/></td>
<td><select name="disc_type[]" id="disc_type_<?php echo $j;?>">
  <option value="Amount" <?php echo ($_POST['disc_type']=='Amount')?'selected':''?>>Amount</option>
  <option value="PercentAmt" <?php echo ($_POST['disc_type']=='PercentAmt')?'selected':''?>>Percent Amt</option>
</select></td>
<td><input name="discount[]" type="text" id="discount_<?php echo $j;?>" value="<?php echo $_REQUEST['discount'][$j];?>" size="15" onBlur="calculate_discount(<?php echo $j;?>);"/></td>
<td><input name="discount_amount[]" type="text" id="discount_amount_<?php echo $j;?>" value="<?php echo $_REQUEST['discount_amount'][$i]; ?>" size="15" /></td>
<td><input name="val_after_disc[]" type="text" id="val_after_disc_<?php echo $j;?>" value="<?php echo $_REQUEST['val_after_disc'][$j];?>" size="15" readonly="true"/></td>
<td><input type="text" name="remarks[]" id="remarks_<?php echo $j;?>" value="<?php echo $_REQUEST['remarks'][$j];?>"/></td>
</tr>
<?php  } } ?>
<input type="hidden" name="countval" value="<?php echo $j; ?>"  id="rowcount"/>
</table>
</div></fieldset></div>
<?php } 
else if($_GET['type']=='IMPO')
{?>
<div style="overflow:auto; width:800px ; height:150px"  >
  <table width="99%" border="0" cellspacing="1" cellpadding="5">
    <tr>
      <td class="tabledata1" scope="col">Item Code </td>
      <td class="tabledata1" scope="col">Item Name </td>
      <td class="tabledata1" scope="col">Description</td>
      <td class="tabledata1" scope="col">Item No. </td>
      <td class="tabledata1" scope="col">Make</td>
      <td class="tabledata1" scope="col">Color</td>
      <td class="tabledata1" scope="col">Grade</td>
	  <td class="tabledata1" scope="col">Size</td>
	  <td class="tabledata1" scope="col">GSM</td>
	  <td class="tabledata1" scope="col">Unit</td>
	   <td class="tabledata1" scope="col">Quantity</td>
      <td class="tabledata1" scope="col">Rate</td>
	   <td class="tabledata1" scope="col">Currency</td>
      <td class="tabledata1" scope="col">Discount Type</td>
      <td class="tabledata1" scope="col">Discount</td>
      <td class="tabledata1">Discount Amount</td>
      <td class="tabledata1" scope="col">Sub Total </td>
      <td class="tabledata1" scope="col">Flexo Offset</td>
      <td class="tabledata1" scope="col">Od</td>
      <td class="tabledata1" scope="col">Length</td>
      <td class="tabledata1" scope="col">Width</td>
      <td class="tabledata1" scope="col">Unit</td>
      <td class="tabledata1" scope="col">Serial No.</td>
      <td class="tabledata1" scope="col">Orignal/Local</td>
      <td class="tabledata1" scope="col">Type</td>
      <td class="tabledata1" scope="col">Shade</td>
      <td class="tabledata1" scope="col">Running Meter/Roll</td>
      <td class="tabledata1" scope="col">Volume</td>
      <td class="tabledata1" scope="col">Teeth</td>
      <td class="tabledata1" scope="col">Bcm</td>
      <td class="tabledata1" scope="col">Ups</td>
      <td class="tabledata1" scope="col">Quality</td>
      </tr>
	<?php 
	$importDetailsSql=DB_query("select * from import_purchaseorder_details where masterId='".$_REQUEST['orderno']."'",$db);
	$i=0;
	while($importDetailsRow=DB_fetch_array($importDetailsSql))
	{
		$_REQUEST['reciveqty'][$i]=$importDetailsRow['quantity'];
		$_REQUEST['itemrate'][$i]=$importDetailsRow['rate'];
		$_REQUEST['currency'][$i]=$importDetailsRow['currency'];
		$_REQUEST['disc_type'][$i]=$importDetailsRow['discountType'];
		$_REQUEST['discount'][$i]=$importDetailsRow['discount'];
		$_REQUEST['discount_amount'][$i]=$importDetailsRow['discountAmt'];
		$_REQUEST['val_after_disc'][$i]=$importDetailsRow['subTotal'];
	
	//echo "select * from item_master where StockID='".$pp[$i]."'";
	$Query=DB_query("select * from item_master where StockID='".$importDetailsRow['itemCode']."'",$db);
  	$Query_row=DB_fetch_array($Query);
  ?>
    <tr bgcolor="#FFFFFF">
      <td><?php echo $importDetailsRow['itemCode'];?><input type="hidden" name="itemcode[<?php echo $i;?>]" id="itemCode_<?php echo $i;?>" value="<?php echo $importDetailsRow['itemCode'];?>" /><input type="hidden" name="qcreqd[]"size="10" value="<?php echo $i;?>" /></td>
      <td><?php echo $Query_row['item_name'];?></td>
      <td><?php echo $Query_row['item_desc'];?></td>
      <td><?php echo $Query_row['item_no'];?></td>
     <td>
	 <?php 
		$makename=$s->getField('product_manufacture','id',$importDetailsRow['make'],'manufacturer');
		echo $makename;?><input name="make[<?php echo $i;?>]" id="make_<?php echo $i;?>" type="hidden" value="<?php echo $importDetailsRow['make'] ?>" /></td>
      <td>
	 <?php 
	$colorname=$s->getField('product_color','id',$importDetailsRow['color'],'color');
	echo $colorname;
	?><input name="color[<?php echo $i;?>]" id="color_<?php echo $i;?>" type="hidden" value="<?php echo $importDetailsRow['color'] ?>" ></td>
      <td><?php
	$gradername=$s->getField('product_grade','id',$importDetailsRow['grade'],'grade');
	echo $gradername;
?><input name="gread[<?php echo $i;?>]" id="gread_<?php echo $i;?>" type="hidden" value="<?php echo $importDetailsRow['grade'] ?>"></td>
<td><?php 
	$sizename=$s->getField('size_master','id',$importDetailsRow['size'],'name');
	echo $sizename;
?><input name="size[<?php echo $i;?>]" id="size_<?php echo $i;?>" type="hidden" value="<?php echo $importDetailsRow['size'] ?>"></td>
<td><?php
	$gsmname=$s->getField('gsm','id',$importDetailsRow['gsm'],'gsm');
	echo $gsmname;
?><input name="gsm[<?php echo $i;?>]" id="gsm_<?php echo $i;?>" type="hidden" value="<?php echo $importDetailsRow['gsm'] ?>"></td>
<td>
<?php echo $unitname=$s->getField('unitsofmeasure','unitid',$Query_row['unit'],'unitname')?>
<input type="hidden" name="uom[<?php echo $i;?>]" id="uom_<?php echo $i;?>" value="<?php echo $Query_row['unit'];?>" /></td>
	 <td><input type="text" name="reciveqty[<?php echo $i;?>]" id="reciveqty_<?php echo $i;?>" value="<?php echo $_REQUEST['reciveqty'][$i];?>" /></td>
      <td><input type="text" name="itemrate[<?php echo $i;?>]" id="itemrate_<?php echo $i;?>" value="<?php echo $_REQUEST['itemrate'][$i];?>" /></td>
	   <td>
	   <select name="currency[<?php echo $i;?>]" style="width:110px;">
	   <option value="0">Select</option>
	   <?php 
	   $currSql=DB_query("Select * from currency_master",$db);
	   while($currRow=DB_fetch_array($currSql)){
	   ?>            
	   <option value="<?php echo $currRow['id'];?>"<?php echo ($_REQUEST['currency'][$i]==$currRow['id']?'selected':'')?>><?php echo $currRow['currency']." - ".$currRow['abbreviation'];?></option>
	   <?php } ?>
	   </select>
	   </td>
      <td>
	  <select name="disc_type[<?php echo $i;?>]" id="disc_type_<?php echo $i;?>" style="width:70px">
	  <option value="0">Select</option>
	  <option value="Percent"<?php echo ($_REQUEST['disc_type'][$i]=='Percent'?'selected':'')?>>Percent</option>
	  <option value="Amount"<?php echo ($_REQUEST['disc_type'][$i]=='Amount'?'selected':'')?>>Amount</option>
	  </select>
	  </td>
      <td><input type="text" name="discount[<?php echo $i;?>]" id="discount_<?php echo $i;?>" value="<?php echo $_REQUEST['discount'][$i];?>" onblur="DiscountAmount(this.value,<?php echo $i;?>)"/></td>
      <td><input type="text" name="discount_amount[<?php echo $i;?>]" id="discount_amount_<?php echo $i;?>" value="<?php echo $_REQUEST['discount_amount'][$i];?>" /></td>
      <td><input type="text" name="val_after_disc[<?php echo $i;?>]" id="val_after_disc_<?php echo $i;?>" value="<?php echo $_REQUEST['val_after_disc'][$i];?>" /></td>
	  <td><?php echo $Query_row['flexo_offset'];?></td>
      <td><?php echo $Query_row['od'];?></td>
      <td><?php echo $Query_row['length'];?></td>
      <td><?php echo $Query_row['width'];?></td>
      <td><?php $unit=$s->getField('unitsofmeasure','unitid',$Query_row['unit'],'unitname'); echo $unit;?></td>
      <td><?php echo $Query_row['serial_no'];?></td>
      <td><?php echo $Query_row['orig_local'];?></td>
      <td><?php echo $Query_row['type_name'];?></td>
      <td><?php echo $Query_row['shade'];?></td>
      <td><?php echo $Query_row['running_roll'];?></td>
      <td><?php echo $Query_row['volume'];?></td>
      <td><?php echo $Query_row['teeth'];?></td>
      <td><?php echo $Query_row['bcm'];?></td>
      <td><?php echo $Query_row['ups'];?></td>
       <td><?php echo $Query_row['quality'];?></td>
    </tr>
    <?php $i++; }
	?>
	<input type="hidden" name="count" id="rowcount" value="<?php echo $i;?>" />
  </table>
</div>
<?php 
	
}
?>
</td>
</tr>
</table>
<table width="20%" border="0">
<tr>
    <td class="tabledata1" width="47%">TOTAL QUANTITY </td>
    <td width="53%" colspan="4"><input name="totalqty" type="text" id="totalqty" onfocus="CountTotalQty()" size="10" readonly="true"  /></td>
  </tr>
</table>
<br>
<table border="0" cellspacing="1" cellpadding="2" width="100%">
<tr>
<td></td>
</tr>
</table>
<?php }                  //onclick="return check_po()"?>
<p align="center"><input type="submit" name="save" value="Save" onclick="return check_po()"/>
<input type="submit" name="cancel" value="Cancel" /></p>
</form>

</div></div>



<?php
//include('includes/footer.inc');
?>
<script language="javascript" type="text/javascript">
function open_items()
{
	window.open("MRN_items.php?action=add&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>","item_popup_window","height=500,width=800,toolbar=no,menubar=no,dependent=yes,resizable =yes, scrollbars=yes");
}
function calculate_poshort(str)
{
	
var qty1=document.getElementById("reciveqty_"+str).value;
var qty2=document.getElementById("pend_qty_"+str).value;	
var totalqty=parseInt(qty2)-parseInt(qty1);

document.getElementById("short_accessid_"+str).value=totalqty;
	
}
function calculate_poshort(str)
{
	
var qty1=document.getElementById("reciveqty_"+str).value;
var qty2=document.getElementById("qty_as_per_bill_"+str).value;	
var totalqty=parseInt(qty2)-parseInt(qty1);

document.getElementById("short_accessid_"+str).value=totalqty;
}

function checkstatus(str)
{
	var Charge = document.getElementById("Charge_"+str).value;
	var actual = document.getElementById("actual_"+str).value;
	if(Charge == 'NO' && actual == '')
	{
		alert("Please Enter Some Value Since It Is Not Inclusive");
		document.getElementById("str").focus();
		return false;
	}
}
function cal_totalcharge()
{
	var total=0;
	var countcharges = document.getElementById("countcharges").value;
	for(var j=1;j<countcharges;j++)
	{
		var actual=document.getElementById("actual_"+j).value;
		if(actual!=''){
		total=parseFloat(total)+parseFloat(actual); }
	}
	document.getElementById("chargeamount").value=total;
}
function ChargeCalculater(Amt,Amtid)
  {
	 
	 var AsPerActual=document.getElementById(Amtid).value;
	 var ActualType=document.getElementById("AsPerActual_" +Amtid).value;
	 if(AsPerActual !='')
	 {
	 if(ActualType=='PercentAmt')
	   {
	     var Total= document.getElementById("totalqty").value;
		 var charge= parseFloat(Total) * parseFloat(AsPerActual)/100;
		 document.getElementById("actual_"+Amtid).value = charge;
		 TotalPercent= parseFloat(TotalPercent) + parseFloat(charge);
	   }
	 else
	   {
	     document.getElementById("actual_"+Amtid).value=AsPerActual; 
		 TotalAmnt=parseFloat(TotalAmnt) + parseFloat(AsPerActual);
	   }
	}
	return true;
  }
  
  function showdivdata(str)
  {
  if(str=='In')
  {
  document.getElementById("hide").style.display='none';
  document.getElementById("show").style.display='block';
  }
  else
  {
  document.getElementById("hide").style.display='block';
  document.getElementById("show").style.display='none';
  }
  
  
  
  }
</script>