<?php
if($_REQUEST['Save']!='')
{
	$insert1="Insert into dispatch set
					 site='".$_REQUEST['site1']."',    
					 adviceNo='".$_REQUEST['adviceNo1']."', 
					 party='".$_REQUEST['party1']."',   
					 dispatchDate='".FormatDateForSQL($_REQUEST['dispatchDate1'])."',  
					 againstSOP='".$_REQUEST['againstSOP1']."',  
					 totalDispatch='".$_REQUEST['totalDispatch']."',
					 remarks='".$_REQUEST['remarks']."', 
					 createdBy='".$_SESSION['UserID']."',  
					 createdDate='".date('Y-m-d')."'  ";
	$result1=DB_query($insert1,$db);
	
	$masterId=DB_Last_Insert_ID($db,'dispatch','id');
	
	if($masterId!='')
	{
			$totalDispatch=0;
		for($y=0;$y<$_REQUEST['count'];$y++)
		{
			$insert2="Insert into dispatch_details set 
							 parentId='".$masterId."',
							 SOPNo='".$_REQUEST['SOPNo'][$y]."',  
							 item='".$_REQUEST['item'][$y]."',    
							 orderQty='".$_REQUEST['orderQty'][$y]."',  
							 dispatchQty='".$_REQUEST['dispatchQty'][$y]."',  
							 boxDetails='".$_REQUEST['boxDetails'][$y]."',  
							 weight='".$_REQUEST['weight'][$y]."',  
							 piecePerBox='".$_REQUEST['piecePerBox'][$y]."'  ";
			$result2=DB_query($insert2,$db);
			
			
			$totalDispatch=$totalDispatch+$_REQUEST['dispatchQty'][$y];
			
			$p=$_REQUEST['pendingQty'][$y];
			$dispatchQty=$_REQUEST['dispatchQty'][$y];
			$pendindQty=$p-$dispatchQty;

			if($dispatchQty==$p)
			{
				$update1="Update salesorder_details set pendingForDispatch='0',pendingQty='0' where id='".$_REQUEST['rowId'][$y]."'";
			}
			else if($dispatchQty!=$p)
			{
				$update1="Update salesorder_details set pendingQty='".$pendindQty."' where id='".$_REQUEST['rowId'][$y]."'";
			}
			$updateResult1=DB_query($update1,$db);
		}
		$updateDispatchQty=DB_query("Update dispatch set totalDispatch='".$totalDispatch."' where id='".$masterId."'",$db);
	}

	$salesDetailsArray=explode(',',$_REQUEST['againstSOP1']);
	for($d=0;$d<count($salesDetailsArray);$d++)
	{
		$check_pending=DB_query("Select pendingForDispatch from salesorder_details where masterId='".$salesDetailsArray[$d]."'",$db);
		$pending='0';
		while($pendingResult=DB_fetch_array($check_pending))
		{
			$pending=$pending+$pendingResult['pendingForDispatch'];
		}	
		if($pending=='0')
		{
			$updatesql2=DB_query("Update salesorder set status='dispatched' where id='".$salesDetailsArray[$d]."'",$db);
		}	
	
}	
prnMsg( _('Record Inserted Successfully'),'success');
echo "<script>location.href='index.php?route=dispatch_list&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";	
}
?>
<script language="JavaScript" type="text/javascript">
	function getOrders(str)
	{
		var branch=document.getElementById("site").value;
		if (str.length==0)
  		{
  			document.getElementById("SOPId").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("SOPId").innerHTML=xmlhttp.responseText;
    		}
  		}
		xmlhttp.open("GET","showSalesOrders.php?client="+str+"&branch="+branch,true);
		xmlhttp.send();
	}

function getDispatch(str)
	{
		if (str.length==0)
  {
  document.getElementById("adviceId").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("adviceId").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","codeGenerator.php?dispatchSite="+str,true);
xmlhttp.send();
	}
</script>
<p><h2 align="center">Dispatch Slip</h2></p>
<form action="" method="post">

<table width="65%"><tr><td bgcolor="#CCCCCC">
<table width="100%" border="0"><tr><td bgcolor="#FFFFFF">
<table width="100%" border="0" cellspacing="1" cellpadding="0" align="center">
  <tr>
  	<th width="17%">Branch</th>
  	<td ><select name="site" id="site" style="width: 100%" onchange="getDispatch(this.value)">
  		<option value="0">Select</option>
  		<?php 
  		$site_sql=DB_query("Select loccode, locationname from locations",$db);
		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></td>
    <th width="21%" nowrap="nowrap" scope="row">Advice No.</th>
    <td width="24%" nowrap="nowrap" id="adviceId"><input name="adviceNo" id="adviceNo" type="text" value="<?php echo $_REQUEST['adviceNo'];?>" size="25" style="width:100%;" /></td>
    
  </tr>
  <tr>
    <th>Party </th>
    <td><select name="party"  style="width:100%;" onchange="getOrders(this.value)">
      <option value="0">---Select---</option>
      <?php 
	$vendor_sql=DB_query("select client_code,c_name from clientmaster",$db);
	while($vendor=DB_fetch_array($vendor_sql))
	{
	?>
      <option value="<?php echo $vendor['client_code'];?>"<?php echo ($_REQUEST['party']==$vendor['client_code']?'selected':'')?>><?php echo $vendor['c_name'];?></option>
      <?php } ?>
    </select></td>
    <th width="26%"><strong>Dispatch Date</strong></th>
    <td width="29%"><input name="dispatchDate" type="text" value="<?php if(isset($_REQUEST['dispatchDate'])){echo $_REQUEST['dispatchDate'];}else{echo date('d/m/Y');}?>" size="12" class="datepicker"  style="width:100%;"/></td>
  </tr>
  <tr>
    <td colspan="4"><div id="SOPId">
    <table width="100%" cellpadding="1" cellspacing="1">
    	<tr>
    <th nowrap="nowrap" scope="row" >Against Single/Multiple S. O. P.</th>
    <?php 
    if($_REQUEST['Proceed']!='' && $_REQUEST['againstSOP']!='')
	{
		$finalArray=explode(',',implode(',',$_REQUEST['againstSOP']));
		//print_r($finalArray);
	}
    ?>
    <td width="29%" nowrap="nowrap">
    	<div style="overflow:auto; height:100px; width:380px; border:1px solid; " >
    	<?php 
    	$sql2=DB_query("Select id,orderNo from salesorder where branch='".$_REQUEST['site']."' and client='".$_REQUEST['party']."' and status='pending'",$db);
    	$i=0;	
    		while($myrow2=DB_fetch_array($sql2))
			{
				$c='';
				for($x=0;$x<count($finalArray);$x++)
				{
					if($_REQUEST['againstSOP'][$i]==$finalArray[$x])
					$c="checked";
				}
				?>
				<input type="checkbox" name="againstSOP[<?php echo $i;?>]" value="<?php echo $myrow2['id']?>" <?php echo $c;?>/><?php echo $myrow2['orderNo']?><br/>
			<?php 	$i++;
			}
    	?>			
    	</div>	
    </td>
    </tr>
    </table></div>
    </td>
    </tr>
</table>
</td></tr></table></td></tr></table>


<p align="center"><input type="submit" name="Proceed" value="Proceed" /><input type="submit" name="Cancel" value="Cancel" /></p>
</form>

<form action="" method="post">
<?php
if($_REQUEST['Proceed']!='' && $_REQUEST['againstSOP']!='')
{
	$SOParray=array();
	$SOParray=$_REQUEST['againstSOP'];
	$SOstring=implode(',',$SOParray);
}
?>
<input type="hidden" name="site1" value="<?php echo $_REQUEST['site'];?>" />
<input type="hidden" name="adviceNo1" value="<?php echo $_REQUEST['adviceNo'];?>" />
<input type="hidden" name="party1" value="<?php echo $_REQUEST['party'];?>" />
<input type="hidden" name="dispatchDate1" value="<?php echo $_REQUEST['dispatchDate'];?>" />
<input type="hidden" name="againstSOP1" value="<?php echo $SOstring;?>" />
	<table width="100%" cellpadding="1" cellspacing="1">
		<tr>
			<th>Item Code</th>
			<th>Item Name</th>
			<th>Unit</th>
			<th>S.O.P No.</th>
			<th>Order Qty</th>
			<th>Pending Qty</th>
			<th>Dispatch Qty</th>
			<th>Box Details</th>
			<th>Weight</th>
			<th>Piece Per Box</th>
		</tr>
		<?php 
		if($_REQUEST['Proceed']!='')
		{
			if($_REQUEST['againstSOP']!='')
			{
				//echo "Select * from salesorder_details where masterId in (".$SOstring.")";				
				$details_sql=DB_query("Select * from salesorder_details where masterId in (".$SOstring.") and pendingForDispatch!='0'",$db);
			}	
		$j=0; 
		while($details_row=DB_fetch_array($details_sql))
		{	
			$item_sql=DB_query("Select item_name,unit from item_master where StockID='".$details_row['ItemCode']."'",$db);
			$item_row=DB_fetch_array($item_sql);
		?>
		<tr bgcolor="#FFFFF">
		<td><?php echo $details_row['ItemCode']?><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['ItemCode']?>" style="width: 98%" /></td>
		<td><?php echo $item_row['item_name']?></td>
		<td><?php 
		$unitName=$s->getField('unitsofmeasure','unitid',$item_row['unit'],'unitname');
		echo $unitName;?></td>
		<td>
		<?php 
			$SOPNo=$s->getField('salesorder_details','id',$details_row['id'],'masterId');
			echo $SOPCode=$s->getField('salesorder','id',$SOPNo,'orderNo');
		?><input type="text" name="SOPNo[<?php echo $j;?>]" id="SOPNo_<?php echo $j;?>" value="<?php echo $SOPNo;?>" /></td>
		<td><?php echo $details_row['Quantity']?><input type="hidden" name="orderQty[<?php echo $j;?>]" value="<?php echo $details_row['Quantity']?>" style="width: 98%" /></td>
		<td><?php if($details_row['pendingQty']!=''){echo $details_row['pendingQty'];}else{echo $details_row['Quantity'];}?><input type="hidden" name="pendingQty[<?php echo $j;?>]" value="<?php if($details_row['pendingQty']!=''){echo $details_row['pendingQty'];}else{echo $details_row['Quantity'];}?>" /></td>
		<td><input type="text" name="dispatchQty[<?php echo $j;?>]" value="" style="width: 98%" /></td>
		<td><input type="text" name="boxDetails[<?php echo $j;?>]" value="" style="width: 98%" /></td>
		<td><input type="text" name="weight[<?php echo $j;?>]" value="" style="width: 98%" /></td>
		<td><input type="text" name="piecePerBox[<?php echo $j;?>]" value="" style="width: 98%" /></td>	
		</tr>
		<?php $j++;}} ?>
		<input type="hidden" name="count" value="<?php echo $j;?>"/>
	</table>
	<p></p>
	<table>
		<tr>
			<th>Remarks</th>
			<td><textarea name="remarks" cols="50" rows="5"></textarea></td>
		</tr>
	</table>
	<p align="center"><input type="submit" name="Save" value="Save" /></p>
</form>
