<link rel="stylesheet" href="css/new_mxerp.css">
<link rel="stylesheet" href="/css/jquery.ui.all.css">
<script src="js1/jquery-1.7.2.js"></script>
<script src="js1/jquery.ui.widget.js"></script>
<script src="js1/jquery.ui.tabs.js"></script>
<script>
$(function() {
$( "#tabs" ).tabs();
});
</script>
<?php 



					function openingBalance($function_filter,$item)
							{
								global $db;
								$site=$function_filter[0];
								$store=$function_filter[1];
								$from=$function_filter[2];								
								if($site!='')
								$filter[]="site = '".$site."'";
								if($store!='')
								$filter[]="store = '".$store."'";
								if($from!='--' && $to!='--')
								$filter[]="reveive_date <= '".$from."'";
								if($item!='')
								$filter[]="itemcode = '".$item."'";
								
								$filter=implode(' and ',$filter);
								 $sql1="Select stock, rate from opening_stockitemwise where $filter";
								$result1=DB_query($sql1,$db);
								$myrow1=DB_fetch_array($result1);
								return $myrow1['stock'];
							}
                      
					   
					   

						////1. MRN/////
                     function receivedMRN($function_filter,$item)
						{
							global $db;
							$site=$function_filter[0];
							$store=$function_filter[1];
							$from=$function_filter[2];
							if($site!='')
							$filter[]="addmrn.site = '".$site."'";
							if($store!='')
							$filter[]="addmrn.StoreCode = '".$store."'";
							if($item!='')
							$filter[]="addmrnqty.mst_itemid = '".$item."'";
							
							$filter=implode(' and ',$filter);
							 $sql1="Select SUM(addmrnqty.reciveqty) from addmrn left join addmrnqty 
							       on(addmrn.id=addmrnqty.issue_id) where Cancel_Status='N' and $filter ";
							$result1=DB_query($sql1,$db);
							$myrow1=DB_fetch_array($result1);
							return $myrow1['SUM(addmrnqty.reciveqty)'];
						}
						

							////1. Issue Slip/////
                         function issueSlip($function_filter,$item)
							{
								global $db;
								$site=$function_filter[0];
								$store=$function_filter[1];
								$from=$function_filter[2];
								if($site!='')
								$filter[]="addissue.site = '".$site."'";
								if($store!='')
								$filter[]="addissue.mst_storeid = '".$store."'";
								if($item!='')
								$filter[]="issueqty.mst_itemid = '".$item."'";
								
								
								$filter=implode(' and ',$filter);
							
								 $sql1="Select SUM(issueqty.issueqty) from addissue left join 
								 issueqty on(addissue.id=issueqty.issue_id) where $filter";
								$result1=DB_query($sql1,$db);
								$myrow1=DB_fetch_array($result1);
								
								return $myrow1['SUM(issueqty.issueqty)'];
							}






                                     ////2. Material return/////
						function receivedReturn($function_filter,$item)
						{
							global $db;
							$site=$function_filter[0];
							$store=$function_filter[1];
							$from=$function_filter[2];
							$to=$function_filter[3];
							
							if($site!='')
							$filter[]="material_return.site = '".$site."'";
							if($store!='')
							$filter[]="material_return.store = '".$store."'";
							if($from!='--' && $to!='--')
							$filter[]="material_return.returndate <= '".$from."'";
							if($item!='')
							$filter[]="material_return_details.itemid = '".$item."'";
							
							$filter=implode(' and ',$filter);
							 $sql1="Select SUM(material_return_details.receiveqty) 
							 from material_return left join material_return_details 
							 on(material_return.id=material_return_details.parentId) where $filter";
						 
							
							$result1=DB_query($sql1,$db);
							$myrow1=DB_fetch_array($result1);
							return $myrow1['SUM(material_return_details.receiveqty)'];
						}
						
						
						       ////////////site to site receive//////
						function receivedSiteToSite($function_filter,$item)
						{
							global $db;
							$site=$function_filter[0];
							$store=$function_filter[1];
							$from=$function_filter[2];
							$to=$function_filter[3];
							
							if($site!='')
							$filter[]="transfer_receipt.Received_to = '".$site."'";
							if($store!='')
							$filter[]="transfer_receipt.Received_store = '".$store."'";
							if($from!='--' && $to!='--')
							$filter[]="transfer_receipt.MRNDate <= '".$from."' ";
							if($item!='')
							$filter[]="transfer_receipt_details.itemid = '".$item."'";
							
							$filter=implode(' and ',$filter);
							
							 $sql1="Select SUM(transfer_receipt_details.reciveqty) 
							 from transfer_receipt left join transfer_receipt_details 
							 on(transfer_receipt.id=transfer_receipt_details.transfer_receipt_id) where $filter";
							$result1=DB_query($sql1,$db);
							$myrow1=DB_fetch_array($result1);
							return $myrow1['SUM(transfer_receipt_details.reciveqty)'];
						}
						
						
						////2. Site to site transfer issue/////
						function issueSiteToSite($function_filter,$item)
						{
							global $db;
						 
							$site=$function_filter[0];
							$store=$function_filter[1];
							$from=$function_filter[2];
							$to=$function_filter[3];
							
							if($site!='')
							$filter[]="transfer_issue.sitecode = '".$site."'";
							if($store!='')
							$filter[]="transfer_issue.fromstore = '".$store."'";
							if($from!='--' && $to!='--')
							$filter[]="transfer_issue.issuedate <= '".$from."'";
							if($item!='')
							$filter[]="transfer_issue_details.itemid = '".$item."'";
							
							$filter=implode(' and ',$filter);
								 $sql1="Select SUM(transfer_issue_details.issueqty) 
								  from transfer_issue left join transfer_issue_details 
								  on(transfer_issue.id=transfer_issue_details.transfer_issue_id) where $filter";
							$result1=DB_query($sql1,$db);
							$myrow1=DB_fetch_array($result1);
							$return_array=$myrow1['SUM(transfer_issue_details.issueqty)'];
							//$avg_rate=$myrow1['SUM(transfer_issue_details.rate)'];
							//$return_array[]=$avg_rate;
							return $return_array;
							
						}
						////3. Material return to vendor///
						function issueMatarialReturn($function_filter,$item)
						{
							global $db;
							$site=$function_filter[0];
							$store=$function_filter[1];
							$from=$function_filter[2];
							$to=$function_filter[3];
							
							if($site!='')
							$filter[]="inv_material_return.sitecode = '".$site."'";
							if($store!='')
							$filter[]="inv_material_return.storecode = '".$store."'";
							if($from!='--' && $to!='--')
							$filter[]="inv_material_return.Reqdate <= '".$from."' ";
							if($item!='')
							$filter[]="inv_material_return_details.itemid = '".$item."'";
							
							$filter=implode(' and ',$filter);
							
						  $sql1="Select SUM(inv_material_return_details.ReturnQty) 
						  from inv_material_return left join inv_material_return_details 
						  on(inv_material_return.id=inv_material_return_details.parentId) where $filter";
							$result1=DB_query($sql1,$db);
							$myrow1=DB_fetch_array($result1);
							return $myrow1['SUM(inv_material_return_details.ReturnQty)'];
						}
						
						function Rgpchallan($function_filter,$item)
					  {
					  
					      global $db;
							$site=$function_filter[0];
							$store=$function_filter[1];
							$from=$function_filter[2];
							$to=$function_filter[2];
							
							if($site!='')
							$filter[]="rgp_tab.branch = '".$site."'";
							if($store!='')
							$filter[]="rgp_tab.store = '".$store."'";
							if($from!='--' && $to!='--')
							$filter[]="rgp_tab.date <= '".$from."'";
							if($item!='')
							$filter[]="rgp_tab_details.mst_itemid = '".$item."'";
							
							$filter=implode(' and ',$filter);
							
							  $sql1="Select SUM(rgp_tab_details.qty) 
							  from rgp_tab left join rgp_tab_details 
							  on(rgp_tab.id=rgp_tab_details.parent_id) where $filter";
							$result1=DB_query($sql1,$db);
							$myrow1=DB_fetch_array($result1);
							return $myrow1['SUM(rgp_tab_details.qty)'];
					  
					  
					  		
					  }	
					   function Rgpreturn($function_filter,$item)
					    {
					  
					        global $db;
							$site=$function_filter[0];
							$store=$function_filter[1];
							$from=$function_filter[2];
							$to=$function_filter[2];
							
							if($site!='')
							$filter[]="rgp_return.branch = '".$site."'";
							if($store!='')
							$filter[]="rgp_return.store = '".$store."'";
							if($from!='--' && $to!='--')
							$filter[]="rgp_return.return_date <= '".$from."' ";
							if($item!='')
							$filter[]="rgp_return_details.item_id = '".$item."'";
							
							$filter=implode(' and ',$filter);
							
							  $sql1="Select SUM(rgp_return_details.qty) 
							  from rgp_return left join rgp_return_details 
							  on(rgp_return.id=rgp_return_details.parent_id) where $filter";
							$result1=DB_query($sql1,$db);
							$myrow1=DB_fetch_array($result1);
							return $myrow1['SUM(rgp_return_details.qty)'];
					  
					  
					  		
					  }	
					  
					  function Nrgp($function_filter,$item)
					    {					  
					        global $db;
							$site=$function_filter[0];
							$store=$function_filter[1];
							$from=$function_filter[2];
							$to=$function_filter[2];
							
							if($site!='')
							$filter[]="nrgp_challan.branch = '".$site."'";
							if($store!='')
							$filter[]="nrgp_challan.store = '".$store."'";
							if($from!='--' && $to!='--')
							$filter[]="nrgp_challan.date <= '".$from."' ";
							if($item!='')
							$filter[]="nrgp_challan_details.item_code = '".$item."'";
							
							$filter=implode(' and ',$filter);
							
							  $sql1="Select SUM(nrgp_challan_details.qty) 
							  from nrgp_challan left join nrgp_challan_details 
							  on(nrgp_challan.id=nrgp_challan_details.parant_id) where $filter";
							$result1=DB_query($sql1,$db);
							$myrow1=DB_fetch_array($result1);
							return $myrow1['SUM(nrgp_challan_details.qty)'];
					  
					  
					  		
					  }	
						function FinishedMR($function_filter,$item)
					    {					  
					        global $db;
							$site=$function_filter[0];
							$store=$function_filter[1];
							$from=$function_filter[2];
														
							if($site!='')
							$filter[]="finished_recieved.site = '".$site."'";
							if($store!='')
							$filter[]="finished_recieved.store = '".$store."'";
							if($from!='--' && $to!='--')
							$filter[]="finished_recieved.recieved_date <= '".$from."' ";
							if($item!='')
							$filter[]="finished_recieved_details.mst_itemid = '".$item."'";
							
							$filter=implode(' and ',$filter);
							
							   $sql1="Select SUM(finished_recieved_details.reciveqty) 
							  from finished_recieved left join finished_recieved_details 
							  on(finished_recieved.id=finished_recieved_details.finished_recieved_id) where $filter";
							$result1=DB_query($sql1,$db);
							$myrow1=DB_fetch_array($result1);
							return $myrow1['SUM(finished_recieved_details.reciveqty)'];
					  
					  
					  		
					  }			
					  
					  
					  function disptch($function_filter,$item)
					  {
					        global $db;
							$site=$function_filter[0];
							$store=$function_filter[1];
							$from=$function_filter[2];
														
							if($site!='')
							$filter[]="dispatch.site = '".$site."'";
							if($store!='')
							$filter[]="dispatch.store = '".$store."'";
							if($from!='--' && $to!='--')
							$filter[]="dispatch.dispatchDate <= '".$from."' ";
							if($item!='')
							$filter[]="dispatch_details.item = '".$item."'";
							
							$filter=implode(' and ',$filter);
							
							
							
							
							     $sql1="Select SUM(dispatch_details.dispatchQty)  from 
							  dispatch_details INNER JOIN dispatch 
								ON dispatch.id=dispatch_details.parentId 
								where $filter";
							$result1=DB_query($sql1,$db);
							$myrow1=DB_fetch_array($result1);
							return $myrow1['SUM(dispatch_details.dispatchQty)'];
					  
					  
					  
					  }								


























if($_REQUEST['Save']!='')
{
	$sales_type=DB_query("select id,SalesOrderType from salesorder where id='".$_REQUEST['SID']."'",$db);
	$sales_typeRow=DB_fetch_array($sales_type);
	if($sales_typeRow['SalesOrderType']=='Tel')
	{
	$so_type='Direct';
	}
	else
	{
	$so_type='Indirect';
	}
	
	$data['site']=$_REQUEST['site'];
	$data['store']=$_REQUEST['store'];
	$data['adviceNo']=$_REQUEST['adviceNo'];
	$data['party']=$_REQUEST['party'];
	$data['dispatchDate']=FormatDateForSQL($_REQUEST['dispatchDate']);
	$data['party']=$_REQUEST['party'];
	$data['againstSOP']=$_REQUEST['SID'];  
	$data['totalDispatch']=$_REQUEST['totalDispatch'];
	$data['remarks']=$_REQUEST['remarks']; 
	$data['da_refno']=$_REQUEST['da_refno'];	
	$data['form_status']=$_REQUEST['form_status'];
	$data['form_no']=$_REQUEST['form_no'];
	$data['dispatch_mode']=$_REQUEST['dispatch_mode'];
	$data['desitination']=$_REQUEST['desitination'];
	$data['freight']=$_REQUEST['freight'];
	$data['CompanyId']=$_SESSION['ACGCompanyId'];
	$data['createdBy']=$_SESSION['UserID'];
	$data['createdDate']=date('Y-m-d');
	$data['order_type']=$_REQUEST['mrpwise'];
	$data['dispatchmodetype']=$so_type;
	$data['lr_no']=$_REQUEST['lrNo'];
	$data['lr_date']=FormatDateForSQL($_REQUEST['lrDate']);
	$data['dealerid']=trim($_SESSION['dealerid']);
	$s->RecordInsert('dispatch', $data);//RECORD INSERTED INTO PARANT TABLE
	
	$masterId=DB_Last_Insert_ID($db,'dispatch','id');
	
	if($masterId!='')
	{
	$totalDispatch=0;
	for($y=0;$y<$_REQUEST['count'];$y++)
	{
	if($_REQUEST['dispatchQty'][$y]!=''){
	$data1['parentId']=$masterId;
	$data1['SOPNo']=$_REQUEST['SOPNo'][$y];  
	$data1['item']=$_REQUEST['item'][$y]; 
	$data1['OrderQty']=$_REQUEST['starting_ord_qty'][$y];   
	$data1['ApprorderQty']=$_REQUEST['ApprorderQty'][$y];  
	$data1['dispatchQty']=$_REQUEST['dispatchQty'][$y];  
	
	$s->RecordInsert('dispatch_details', $data1);//RECORD INSERTED INTO CHIELD TABLE
		
	
	$str_ord_qty=$_REQUEST['starting_ord_qty'][$y];// TOTAL ORDER QTY
	
	$FinallyDis=$_REQUEST['totaldispatchaks'][$y]+$_REQUEST['dispatchQty'][$y];//FINAL DISPATCH
	
	$p=$_REQUEST['ApprorderQty'][$y];//PENDING QTY
	
	$dispatchQty=$_REQUEST['dispatchQty'][$y];//DISPATCH QTY
	
	$pendindQty=$p-$dispatchQty;//FINAL PAENDING QTY



//*******IF ORDER QTY & FINAL DISPATCH QTY ARE EQUAL THEN UPDATE SALESORDER DETAILS TABLE***************START HERE******	
	if($str_ord_qty==$FinallyDis)
	{
	$update1="Update salesorder_details set pendingForDispatch='0',pendingQty='0' where id='".$_REQUEST['rowId'][$y]."'";
	}
//*******IF ORDER QTY & FINAL DISPATCH QTY ARE EQUAL THEN UPDATE SALESORDER DETAILS TABLE***************END HERE******



//*******IF ORDER QTY & FINAL DISPATCH QTY ARE NOT EQUAL THEN UPDATE SALESORDER DETAILS TABLE***************START HERE******
	else if($str_ord_qty!=$FinallyDis)
	{
	$update1="Update salesorder_details set pendingQty='".$pendindQty."' where id='".$_REQUEST['rowId'][$y]."'";
	}
	$updateResult1=DB_query($update1,$db);
	}
	}
//*******IF ORDER QTY & FINAL DISPATCH QTY ARE NOT EQUAL THEN UPDATE SALESORDER DETAILS TABLE***************END HERE******
	$updateDispatchQty=DB_query("Update dispatch set totalDispatch='".$FinallyDis."' where id='".$masterId."'",$db);
	}
	
	$s->pageLocation("index.php?route=CRM/dispatch_list".$rurl);//PAGE REDIRECTION
}


    

?>

<script language="javascript" type="text/javascript">
function calqty(k)
{
//alert(document.getElementById("dispatchQty_"+k).value);
	var dispatchQty;
	var ApprorderQty=parseFloat(document.getElementById("ApprorderQty_"+k).value);
	if(document.getElementById("dispatchQty_"+k).value==''){dispatchQty=0;}
	else
	{
	 dispatchQty=parseFloat(document.getElementById("dispatchQty_"+k).value);
	}
	
	var stockqty=parseFloat(document.getElementById("stockqty_"+k).value);
	//var pendig_qty=parseFloat(document.getElementById("pendingQty_"+k).value)
	//alert(pendig_qty);
	//if(dispatchQty>ApprorderQty || dispatchQty>pendig_qty)
//	{
	if(dispatchQty>ApprorderQty)
	{
	alert("Dispatch cannot be greater then the order quantity and Pending QTY");
	document.getElementById("dispatchQty_"+k).value='0';
	}
	else if(dispatchQty==0)
	{
	alert("Dispatch Qty Should be Greater Then Zero");
	document.getElementById("dispatchQty_"+k).value='';
	}
	if(dispatchQty>stockqty)
	{
	alert("Dispatch cannot be greater then the Stock ");
	document.getElementById("dispatchQty_"+k).value='0';
	}
	var Qty=document.getElementById("ApprorderQty_"+k).value;
	var Rate=document.getElementById("Rate_"+k).value;
	var total=parseFloat(dispatchQty)*parseFloat(Qty);
	if(document.getElementById("HidRate_"+k))
	var HRate=document.getElementById("HidRate_"+k).value;
	//document.getElementById("EDamount_"+k).value=dispatchQty*Rate*.01;
	document.getElementById("assable_value_"+k).value=dispatchQty*Rate ;
}
 
function calculateBasic()
{
	var count1=document.getElementById("count").value;
	total=0;
	for(var k=0;k<count1;k++)
	{
	var dispatchQty=document.getElementById("dispatchQty_"+k).value;
	if(dispatchQty!=''){
	var Qty=document.getElementById("assable_value_"+k).value; 
	total+=parseFloat(Qty);}
	}
	if(total!=0)
	document.getElementById("basicAmount").value=total ;
}


function calculateDiscounted()
{
	var totalDiscounted=0;
	var cnt=document.getElementById("count").value;
	for(var i=0;i<cnt;i++)
	{
	var totalDiscounted1=document.getElementById("EDAmt_"+i).value;
	var totalDiscounted=(parseFloat(totalDiscounted)+parseFloat(totalDiscounted1)).toFixed(2);
	}
	if(totalDiscounted1!=='0.00')
	{
	document.getElementById("totalDiscountedPrice").value=totalDiscounted;
	}
}



function calculateTotalExcise()
{
	var EDRate=document.getElementById("EDRate").value;
	if(EDRate==0){
	var totalExcise=0;
	var first_row=document.getElementById("excisevalue").value;
	//if(first_row==''){first_row=0;}
	var cnt=document.getElementById("hiditemcount").value;
	var newcount=parseInt(cnt)+1;
	for(var i=1;i<newcount;i++)
	{
	 	var totalExcise1=document.getElementById("excisevalue_"+i).value;
	 	var totalExcise=(parseFloat(totalExcise)+parseFloat(totalExcise1)).toFixed(2);
	}
	  document.getElementById("totalDiscountedPrice").value=parseFloat(totalExcise)+parseFloat(first_row);
	}
	else{return false;}
}





function calculateECess()
{
	var exciseValue=document.getElementById("totalDiscountedPrice").value;
	var ECessPercentage=document.getElementById("ECessPercentage").value;
	var total=exciseValue*(ECessPercentage/100);
	document.getElementById("ECessValue").value=total.toFixed(2);
}

function calculateSAHCess()
{
	var exciseValue=document.getElementById("totalDiscountedPrice").value;
	var SAHCessPercentage=document.getElementById("SAHCessPercentage").value;
	var total=exciseValue*(SAHCessPercentage/100);
	document.getElementById("SAHCessValue").value=total.toFixed(2);
}


function calculateTaxable()
{
	var Basic=document.getElementById("basicAmount").value;
	var totalDiscountedPrice=document.getElementById("totalDiscountedPrice").value;
	var ECessValue=document.getElementById("ECessValue").value;
	var SAHCessValue=document.getElementById("SAHCessValue").value;
	if(totalDiscountedPrice==''){totalDiscountedPrice=0;}
	if(ECessValue==''){ECessValue=0;}
	if(SAHCessValue==''){SAHCessValue=0;}
	var TOTAL=parseFloat(totalDiscountedPrice)+parseFloat(ECessValue)+parseFloat(SAHCessValue)+parseFloat(Basic);
	document.getElementById("taxableAmount").value=TOTAL.toFixed(2);
}



function calculateVATCST(str)
{
	if (str.length==0)
	{
	document.getElementById("rate").value="";
	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("rate").value=xmlhttp.responseText;
	}
	}
	xmlhttp.open("GET","calculateExcise.php?salesId="+str,true);
	xmlhttp.send();
}


function calculateGrand()
{
	var a=document.getElementById("totalDiscountedPrice").value;
	var b=document.getElementById("taxableAmount").value;
	var c=document.getElementById("VatCstValue").value;
	if(b==''){b=0;}
	if(c==''){c=0;}
	var GRAND=parseFloat(b)+parseFloat(c);
	document.getElementById("grandTotal").value=GRAND.toFixed(2);
}

function calculateEDAmt(j)
{
	var EDRate = document.getElementById("EDRate_"+j).value;
	if(EDRate=='0')
	{
	document.getElementById("EDAmt_"+j).value='0.00';
	}
	else
	{
	var assable_value = document.getElementById("assable_value_"+j).value;
	var EDAmt=((parseFloat(EDRate)*parseFloat(assable_value) )/100);
	document.getElementById("EDAmt_"+j).value=EDAmt.toFixed(2);
	}
}



function CalculateVatCstValue()
{
	var taxableammount12=document.getElementById("taxableAmount").value;
	var str=document.getElementById("rate").value;
	var VAtCst1= parseFloat(str)*parseFloat(taxableammount12)*.01;
	document.getElementById("VatCstValue").value=VAtCst1.toFixed(2);
}



function getTarrifHeading(str)
{
	if (str.length==0)
	{
	document.getElementById("commID").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("commID").innerHTML=xmlhttp.responseText;
	}
	}
	xmlhttp.open("GET","get_category.php?comm="+str,true);
	xmlhttp.send();
}
</script>

<fieldset>
<legend>Dispatch Slip</legend>
<div style="z-index:-1000em;">
<div class="demo">
<div id="tabs">
  <ul>
  <!--	<li><a href="#tabs-2">More Information</a></li>-->
	</ul>
	<div id="tabs-1">
		<div class="tab_div">
<?php
$getSaleorder=DB_query("Select id,orderNo,branch,client,PORefNo,order_type,freight,store from salesorder where   id='".$_REQUEST['SID']."' and status='pending'",$db);
$rdata=DB_fetch_array($getSaleorder);
$GetSite=$rdata['branch'];
$GetStore=$rdata['store'];
$Getclient=$rdata['client'];
$GetOrderno=$rdata['orderNo'];
$GetPORefNo=$rdata['PORefNo'];
$GetOrder_type=$rdata['order_type'];
$freight=$rdata['freight'];

   $function_filter[]=$GetSite;
	$function_filter[]='';
	$function_filter[]=date('Y-m-d'); 
	$function_filter[]=date('Y-m-d'); 
?>
<form name="DispatchSlip" action="" method="post" onsubmit="">
<input type="hidden" name="mrpwise" value="<?php echo $GetOrder_type; ?>" />
<table width="100%" border="1" cellspacing="0" cellpadding="3">
<tr class="row1">
	<td class="tabledata1">Branch<span style="color:#FF0000">*</span></td> 
	<td  class="tabledata1">
	<?php $site_sql=DB_query("Select loccode, locationname from locations  where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode='".$GetSite."'",$db);?>
	<select name="site" id="site" style="width:160px;" >
	<?php while($site_row=DB_fetch_array($site_sql)){?>
		<option value="<?php echo $site_row['loccode']?>"<?php echo($_REQUEST['site']==$site_row['loccode']?'selected':'')?>><?php echo   $site_row['locationname']?></option>
	<?php }?>
	</select>
	<input type="hidden" name="store" id="store" value="<?php echo $GetStore; ?>" />	</td>
	<td  class="tabledata1"><span style="width:100px;">Advice No.<span style="color:#FF0000">*</span></span></td>
	<td  class="tabledata1"><?php 
                $startYr = explode('-', $_SESSION['financialYrStart']);
		$endYr = explode('-', $_SESSION['financialYrEnd']);
		$startYr[0];
		$endYr[0];
		$financial=substr($startYr[0],-2)."-".substr($endYr[0],-2); 
                
	$advno="Select adviceNo  from dispatch where site='".$GetSite."' and INSTR(`adviceNo`, '$financial') > 0 order by adviceNo DESC LIMIT 0,1";
	$GetsiteSql=DB_query("select * from locations where loccode='".$GetSite."'",$db);
	$GetSite1=DB_fetch_array($GetsiteSql);
	$SITECODE=$GetSite1['prefix'];
	$advnoResult=DB_query($advno,$db);
	if(DB_num_rows($advnoResult)>0)
	{
	$disp_row=DB_fetch_array($advnoResult);	
	$DispNo=$disp_row['adviceNo'];
	$OrderNo_brk1=substr($DispNo,-3);
	$next=$OrderNo_brk1+1;
	$finalString=$SITECODE."/"."DA"."/".$financial."/".str_pad($next,4,"0",STR_PAD_LEFT);
	}	
	else
	{
	$finalString=$SITECODE."/"."DA"."/".$financial."/"."0001";
	}	 
	?>
      <input name="adviceNo" id="adviceNo" type="text" value="<?php echo $finalString;?>" style="width:160px;" readonly /></td>
	<td style="width:100px;"><span class="tabledata1">Dispatch Date<span style="color:#FF0000">*</span></span></td>
	<td id="adviceId"><span class="tabledata1">
	  <input name="dispatchDate" type="text" value="<?php if(isset($_REQUEST['dispatchDate'])){echo $_REQUEST['dispatchDate'];}else{echo date('d/m/Y');}?>" class="datepicker"  style="width:160px;"/>
	</span></td>
</tr>
<tr class="row1">
	<td class="tabledata1"><span style="width:100px;">Party<span style="color:#FF0000">*</span></span></td>
	<td class="tabledata1"><input name="Input" type="text" value="<?php echo $s->GetFieldValue('clientmaster',"id='".$Getclient."'",'c_name'); 	?>"  style="width:160px;" readonly="true" />
      <input name="party" id="party" type="hidden" value="<?php  echo $Getclient;?>" /></td>
	<td class="tabledata1">Form </td>
	<td class="tabledata1"><select name="form_status" style="width:160px;">
      <option value="Yes" <?php  echo  $form_status=='Yes'?'selected':'' ?>>Yes</option>
      <option value="No" <?php  echo $form_status=='No'?'selected':'' ?> >No</option>
    </select></td>
	<td class="tabledata1">Form No</td>
	<td  class="tabledata1"><input type="text" name="form_no" value="<?php echo $form_no; ?>"  style="width:160px;" /></td>
	</tr> 
	<tr class="row1">
	<td class="tabledata1">Address<span style="color:#FF0000">*</span></td>
	<td class="tabledata1"><input name="" type="text" value="<?php echo $s->GetFieldValue('clientmaster',"id='".$Getclient."'",'pre_address1'); 	?>"  style="width:160px;" readonly="true" />	</td>
	<td class="tabledata1">City<span style="color:#FF0000">*</span></td>
	<td class="tabledata1"><input name="Input2" type="text" value="<?php echo $city=$s->getField('city_master','id',$s->GetFieldValue('clientmaster',"id='".$Getclient."'",'pre_city'),'city'); 	?>" readonly="true"   style="width:160px;"/>
      <input type="hidden" name="city" value="<?php echo $s->GetFieldValue('clientmaster',"id='".$Getclient."'",'pre_city') ?>"/></td>
	<td class="tabledata1">State<span style="color:#FF0000">*</span></td>
	<td  class="tabledata1"><input name="Input3" type="text" value="<?php echo $city=$s->getField('state_master','id',$s->GetFieldValue('clientmaster',"id='".$Getclient."'",'pre_state'),'state'); 	?>" readonly="true"  style="width:160px;" />
      <input type="hidden" name="state" value="<?php echo $s->GetFieldValue('clientmaster',"id='".$Getclient."'",'pre_state') ?>"/></td>
</tr> 
<tr class="row1">
	<td class="tabledata1">&nbsp;</td>
	<td class="tabledata1"><input name="" type="text" value="<?php echo $s->GetFieldValue('clientmaster',"id='".$Getclient."'",'pre_address2'); 	?>"  style="width:160px;" readonly="true" />	</td>
	<td class="tabledata1">Country<span style="color:#FF0000">*</span></td>
	<td class="tabledata1"><input name="Input4" type="text" value="<?php echo $city=$s->getField('country_master','id',$s->GetFieldValue('clientmaster',"id='".$Getclient."'",'pre_country'),'country'); 	?>"  readonly="true" style="width:160px;"/>
      <input type="hidden" name="county" value="<?php echo $s->GetFieldValue('clientmaster',"id='".$Getclient."'",'pre_country') ?>"/></td>
	<td class="tabledata1">Freight Type</td>
	<td  class="tabledata1"><select style="width:160px;" name="freight">
      <option value="0">Select</option>
      <option value="Paid"<?php echo($freight=='Paid'?'selected':''); ?>>Paid</option>
      <option value="topay"<?php echo($freight=='topay'?'selected':''); ?>>To Pay</option>
      <option value="Nill"<?php echo($freight=='Nill'?'selected':''); ?>>Nill</option>
    </select></td>
</tr> 
<tr class="row1">
	<td class="tabledata1">&nbsp;</td>
	<td class="tabledata1"><input name="" type="text" value="<?php echo $s->GetFieldValue('clientmaster',"id='".$Getclient."'",'pre_address3'); 	?>"  style="width:160px;" readonly="true" />	</td>
	<td class="tabledata1">Destination<span style="color:#FF0000">*</span></td>
	<td class="tabledata1"><input name="desitination" type="text" value="<?php echo $desitination;?>" style="width:160px;" /></td>
	<td class="tabledata1">Dispatch Mode <span style="color:#FF0000">*</span></td>
	<td  class="tabledata1"><select name="dispatch_mode" style="width:160px;">
      <option value="">Select</option>
      <?php $sql1=DB_query("select * from transport_master where CompanyId='".$_SESSION['ACGCompanyId']."' and Site='".$GetSite."'",$db);
	      while($row1=DB_fetch_array($sql1)){?>
      <option value="<?php echo $row1['id']; ?>"><?php echo $row1['TransportName']; ?></option>
      <?php } ?>
    </select></td>
</tr> 
<tr class="row1">
	<td class="tabledata1">LR. No.</td>
	<td class="tabledata1"><input type="text" name="lrNo" id="lrNo" value="<?php echo $grNo;?>" style="width:160px;" /></td>
	<td class="tabledata1">LR Date</td>
	<td class="tabledata1"><input type="text" name="lrDate" id="lrDate" value="<?php echo $grDate;?>"  style="width:160px;" class="datepicker"/></td>
	<td class="tabledata1">&nbsp;</td>
	<td  class="tabledata1">&nbsp;</td>
</tr> 	

<tr class="row1">
	<td colspan="6"  class="tabledata1">
		<table width="100%" cellpadding="1" cellspacing="1">
			<tr class="row1">
				<td nowrap="nowrap" scope="row"   class="tabledata1">Against Single/Multiple S. O. P.</td>
				<td nowrap="nowrap"  class="tabledata1">
				<div style="overflow:auto; height:100px; width:383px; border:1px solid; " >
 				&nbsp;&nbsp;<input name="againstSOP[<?php echo $i;?>]" value="<?php echo $_REQUEST['SID']?>" type="hidden"  >
	 			<?php echo $GetOrderno?><?php echo '--'.'&nbsp;&nbsp;'.$GetPORefNo; ?>				</div></td>
			</tr>
		</table>	</td>
</tr>
<tr class="row1">
	<td colspan="6"><h3>Items Details</h3></td>
</tr>
</table>

<table width="100%" cellpadding="3" cellspacing="0" border="0">
<tr class="row1">
	<td  class="tabledata">Sno.</td>
	<td  class="tabledata">Code</td>
	<td  class="tabledata">Item Name </td>
	
	<td  class="tabledata">Unit</td>
	<td  class="tabledata">T.O No.</td>			
	<td  class="tabledata"> Order Qty</td>
	<td  class="tabledata"> Approved Qty</td>
	<td  class="tabledata">Stock Qty</td>
	<td  class="tabledata">Dispatch Qty </td>
	
</tr>
<?php $details_data=DB_query("Select * from salesorder where id in (".$_REQUEST['SID'].") and CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
$row=DB_fetch_array($details_data);
$test_amit=$row['Sale_Key_Exec']; 
$details_sql=DB_query("Select * from salesorder_details where masterId in (".$_REQUEST['SID'].") and pendingForDispatch!='0' and (ApprovalStatus='3' || ApprovalStatus='2')",$db);
if(DB_num_rows($details_sql)>0)	
{
$j=0; 
while($details_row=DB_fetch_array($details_sql)){	


$sqldispatch=DB_query("select SUM(dispatchQty) as TOTALDIS from dispatch_details where SOPNo='".$_REQUEST['SID']."' and item='".$details_row['mst_itemid']."'",$db);
if(DB_num_rows($sqldispatch)>0)
{
$sqldispatchRow=DB_fetch_array($sqldispatch);
$ApprQty=$details_row['Approved_qty']-$sqldispatchRow['TOTALDIS'];
}
else
{
$ApprQty=$details_row['Approved_qty'];
}
$item_sql=DB_query("Select item_name,mst_unitid as unit,StockID,item_desc from item_master where  id='".$details_row['mst_itemid']."'",$db);
$item_row=DB_fetch_array($item_sql);


/*************************   Stock Qty Calculations  *********************************************/

                            $op=openingBalance($function_filter,$details_row['mst_itemid']);
							//echo "openingBalance"."<br>";
							 $in_1=receivedMRN($function_filter,$details_row['mst_itemid']);
							//echo "receivedMRN"."<br>";
							 $out_1=issueSlip($function_filter,$details_row['mst_itemid']);
							//echo "issueSlip"."<br>";
							 $out_2=issueSiteToSite($function_filter,$details_row['mst_itemid']);		
							//echo "issuesitetosite"."<br>";
							 $in_2=receivedSiteToSite($function_filter,$details_row['mst_itemid']);
							//echo " receivesitetosite"."<br>";
							 $out_3=issueMatarialReturn($function_filter,$details_row['mst_itemid']);
							//echo "issuematerialreturn"."<br>";
							 $in_3=receivedReturn($function_filter,$details_row['mst_itemid']);	
							//echo "receivedReturn"."<br>";
							 $out_4=Rgpchallan($function_filter,$details_row['mst_itemid']);
							//echo "Rgpchallan"."<br>";
							 $out_5=Nrgp($function_filter,$details_row['mst_itemid']);
							//echo "Nrgp"."<br>";
							//echo $out_6=invoice($function_filter,$record_edit['mst_itemid']);
							//echo "invoice"."<br>";
							 $out_7=disptch($function_filter,$details_row['mst_itemid']);
							//echo "disptch"."<br>";
							 $in_4=Rgpreturn($function_filter,$details_row['mst_itemid']);
							//echo "Rgpreturn"."<br>";	 
							 $in_5=FinishedMR($function_filter,$details_row['mst_itemid']);
							//echo "FinishedMR"."<br>";	
                            $invalue=($op+$in_1+$in_2+$in_3+$in_4+$in_5);

                           $outvalue=($out_1+$out_2+$out_3+$out_4+$out_5+$out_7);
                          $tot=$invalue-$outvalue;


/************************************************************************************************/








if($ApprQty!=0)
{
?>
<tr class="row1">
	<td  class="tabledata">
	<input type="hidden" name="totaldispatchaks[<?php echo $j;?>]" value="<?php echo $sqldispatchRow['TOTALDIS']; ?>" />
	<input type="hidden" name="starting_ord_qty[<?php echo $j;?>]" value="<?php echo $details_row['Quantity']; ?>" />
	<?php echo $j+1;?><input type="hidden" name="rowId[<?php echo $j;?>]" value="<?php echo $details_row['id']?>" /><input type="hidden" name="item[<?php echo $j;?>]" value="<?php echo $details_row['mst_itemid']?>" /></td>
	<td  class="tabledata"><?php echo $item_row['StockID']?></td>
	<td  class="tabledata"><?php echo $item_row['item_name']?></td>
	
	<td  class="tabledata"><?php echo	$unitName=$s->GetFieldValue('unitsofmeasure',"unitid='".$item_row['unit']."'",'unitname');	 ?></td>
	<td  class="tabledata"><?php $SOPNo=$s->GetFieldValue('salesorder_details',"id='".$details_row['id']."'",'masterId');
	echo $SOPCode=$s->GetFieldValue('salesorder',"id='".$SOPNo."'",'orderNo');
	?><input type="hidden" name="SOPNo[<?php echo $j;?>]" id="SOPNo_<?php echo $j;?>" value="<?php echo $SOPNo;?>" /></td>
	<td  class="tabledata"><?php echo $details_row['Quantity']?></td>
	<td  class="tabledata"><?php echo $ApprQty?>
	<input type="hidden" name="ApprorderQty[<?php echo $j;?>]" id="ApprorderQty_<?php echo $j;?>" value="<?php echo $ApprQty;?>" />
	</td>
	<td class="tabledata"><?php echo $tot; ?>
	<input type="hidden" name="stockqty[<?php echo $j; ?>]" id="stockqty_<?php echo $j; ?>" value="<?php echo $tot; ?>" />
	</td>
	<td  class="tabledata"><input type="text" name="dispatchQty[<?php echo $j;?>]"  id="dispatchQty_<?php echo $j;?>"    style="width:80px;"  onkeyup="calqty(<?php echo $j;?>)" /></td>
	
	
<?php $j++; } } ?>
	<input type="hidden" name="count"  id='count' value="<?php echo $j;?>"/>
</table>
<table width="100%" cellpadding="3" cellspacing="0" border="0">
	<tr class="row1">
	<td width="10%">Remarks</td>
	<td><textarea name="remarks" cols="60" style="width:100%;" rows="7"></textarea></td>
</tr>
<tr class="row1">
	<td colspan="2"><p align="center"><input type="submit" name="Save" value="Save" class="search" onclick="return validate();" /></p></td>
</tr>

</table>
</form>
<?php }
else { ?>
	<tr bgcolor="#FFFFFF">
<td colspan="12" style="color:#FF0000;">No Records Find</td>
</tr> <?php } 
	
			?>
		</div>
	  </div>
	</div><!-- Tab 1 Close -->
	
	 <!-- Tab 2 Close -->



</div>
</div>
</fieldset>



<script language="javascript" type="text/javascript">
function Dispatch_Slip()
{
	if(document.DispatchSlip.site.value=='0')
	{
	alert("Please Select The Branch Name First");
	return false;
	}
	else if(document.DispatchSlip.party.value=='0')
	{
	alert("Please Select Party First");
	return false;
	}
	else if(document.DispatchSlip.adviceNo.value=='')
	{
	alert("Please Enter The Advoice No First");
	return false;
	}
	else if(document.DispatchSlip.dispatchDate.value=='')
	{
	alert("Please Select The Dispatch date");
	return false;
	}
	else if(document.DispatchSlip.dispatch_mode.value=='')
	{
	alert("Please Enter The Dispatch Mode");
	return false;
	}
	else if(document.DispatchSlip.desitination.value=='')
	{
	alert("Please Enter The Desitination");
	return false;
	}
	return true;
	
}
		
function calculateItemExcise(str, i)
{
	//alert(str);
	if (str.length==0)
	{
	document.getElementById("HidRate_"+i).value="";
	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("HidRate_"+i).value=xmlhttp.responseText;
	}
	}
	xmlhttp.open("GET","calculateExcise.php?exciseId="+str,true);
	xmlhttp.send();
}

function calculateExciseAMt(i)
{
	var excise_rate=document.getElementById("HidRate_"+i).value;
	var assable_value=document.getElementById("assable_value_"+i).value;
	var excise_val=assable_value*(excise_rate/100);
	document.getElementById("EDAmt_"+i).value=excise_val.toFixed(2);
}

function calculateExciseAMt1(i)
{   
	var excise_rate=document.getElementById("HidRate_"+i).value;
	var assable_value=document.getElementById("totalassamt_"+i).value;
	var excise_val=assable_value*(excise_rate/100);
	alert(excise_val);
	document.getElementById("EDAmt_"+i).value=excise_val.toFixed(2);
}


function calculateBillExcise(str)
{
	var value=document.getElementById("basicAmount").value;
	if(str.length==0)
	{
		document.getElementById("totalDiscountedPrice").value="";
		return;
	}
	if(window.XMLHttpRequest)
	{
		xmlhttp=new XMLHttpRequest();
	}
	else
	{
	xmlhttp	=new ActiveXobject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
	if(xmlhttp.readyState==4 && xmlhttp.status==200)
	{
		document.getElementById("totalDiscountedPrice").value=xmlhttp.responseText;
 	}
 }
xmlhttp.open("GET","calculateExcise.php?type2=bill&id="+str+"&value="+value,true);
xmlhttp.send();	
}


function calculateBillExcise1()
{
     var str=document.getElementById("EDRate1").value;
	var value=document.getElementById("basicAmount").value;
	
	if(str.length==0)
	{
		document.getElementById("totalDiscountedPrice").value="";
		return;
	}
	if(window.XMLHttpRequest)
	{
		xmlhttp=new XMLHttpRequest();
	}
	else
	{
	xmlhttp	=new ActiveXobject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
	if(xmlhttp.readyState==4 && xmlhttp.status==200)
	{
		document.getElementById("totalDiscountedPrice").value=xmlhttp.responseText;
 	}
 }


xmlhttp.open("GET","calculateExcise.php?type2=bill&id="+str+"&value="+value,true);
xmlhttp.send();	
}

function validate()
{
//var c=document.getElementById("count").value;
//
//for(var i=0;i<c;i++)
//{
//if(document.getElementById("dispatchQty_"+i).value==0 || document.getElementById("dispatchQty_"+i).value=='')
//{
//alert("Must Enter Dispatch Qty");
//return false;

}



}


}


</script>

