<?php

/* $Revision: 1.20 $ */

$PageSecurity = 2;

include('includes/session.inc');

$title = _('Search Outstanding Purchase Orders');

include('includes/header.inc');

if(!empty($_GET['indent_id']) && is_numeric($_GET['indent_id'])){
	$indent_id = $_GET['indent_id'];
	$SQL = "select * from issue where issue_id = $indent_id";	
	//echo $SQL;
	$result=DB_query($SQL,$db,$ErrMsg);
	$inrow=DB_fetch_array($result);
}



if(isset($_POST['issue_date']) && !empty($_POST['issue_date'])){	
	extract($_POST);	
	
	$issue_date = date('Y-m-d',strtotime($issue_date));	
	
	if(isset($_POST['issue_id']) && empty($_POST['issue_id'])){
		$SQL = "insert into issue set indent_id = $indent_id, issue_date = '$issue_date', date_added = NOW(), 
				last_modified = NOW(), raised_by = '$raised_by', modified_by = '$_SESSION[UserID]'";
		
		$result=DB_query($SQL,$db,$ErrMsg);		
		$issue_id = DB_Last_Insert_ID($db,'indents','indent_id');
	}else{	
		$SQL = "update issue set indent_id = $indent_id, issue_date = '$issue_date',
				last_modified = NOW(), modified_by = '$_SESSION[UserID]'
				where issue_id = $issue_id";
		$result=DB_query($SQL,$db,$ErrMsg);				
	}
	//echo $SQL;
	for($i=1;$i<=$maxbrand;$i++){		
		$item = $_POST['item_code'.$i];
		$issue_qty = $_POST['issue_qty'.$i];
		$req_qty = $_POST['req_qty'.$i];
		if($issue_qty != '' && $issue_qty>0){
			if($issue_qty > $req_qty){
				$itmarr[] = $item;
			}else{
				$sql1 = "update indent_items set issue_qty = '$issue_qty', issue_status='issued' where indent_id = $indent_id and item_code = '$item'";				
				$insResult=DB_query($sql1,$db,$ErrMsg);				
			}
		}		
//echo $sql1;die;		
	}	
	if (count($itmarr)>0){ /*There is ony one order header returned */

		$title = _('Issue Error');
		include('includes/header.inc');
		echo '<div class="centre"><br><br><br>';
		prnMsg( _('The issue quantity is greater than the required quantity of item code') . ' : ' . implode(', ',$itmarr) . ' ', 'error');
		echo '</div><br>
		<div align="center"><a href="javascript:history.back();">&laquo;&laquo;Back</a></div>
		<br><br>';
		include('includes/footer.inc');
		exit();

	}	
	header("Location:issue.php?indent_id=$indent_id&issue_id=$issue_id");
}
?><table width="100%" border="0">
  <tr>
    <td><form method="get" action="" name="BatchQtyForm">
			<table width="60%" border="0" align="center">
			  <tr>
				<td width="26%" class="reg_item">Indent No.</td>

						<td><select name="indent_id" onchange="this.form.submit();">
				<option value="">---- Select ----</option>
				<?php
				$indSql = "select * from indents";	
				//echo $SQL;
				$res=DB_query($indSql,$db,$ErrMsg);
				while($resrow=DB_fetch_array($res)){
					$chkSql = "select count(*) as `totCount` from indent_items where indent_id = $resrow[indent_id] and issue_status = 'pending'";
					$chkresult=DB_query($chkSql, $db);
					$chkrow=DB_fetch_array($chkresult);
					if($chkrow['totCount']>0){
						if($indent_id == $resrow['indent_id']){
							$selected = " selected";
						}else{
							$selected = "";
						}				
						echo '<option value="'.$resrow['indent_id'].'"'.$selected.'>'.$resrow['indent_id'].'</option>';
					}
				}
				?>
				</select></td>
			  </tr>
			</table>
			</form></td>
  </tr>
  <tr>
    <td><form method="post" action="" name="IssueItemForm" ENCTYPE="multipart/form-data" onSubmit="return chkform(this)">
	
	

		<table width="60%" border="1" align="center">
		  <tr>
		    <td class="reg_item">Issue No.</td>
		    <td><input name="issue_id" value="<? if(isset($inrow['issue_id'])){ echo $inrow['issue_id'];}?>" maxlength="25" id="issue_id" tabindex="1" class="text" type="text" readonly="readonly"></td>
	      </tr>
		  <tr>
		   <td class="reg_item">Issue Date</td>
		    <td><input name="issue_date" value="<? if(isset($inrow['issue_date'])){ echo date('d-m-Y',strtotime($inrow['issue_date']));}?>" maxlength="25" id="issue_date" tabindex="1" class="text" type="text" readonly="readonly">
				<img src="<?=$rootpath;?>/images/cal.gif" id="issue_date_trigger"  />
	<script type="text/javascript">
				
                  new Calendar({
                          inputField: "issue_date",
                          dateFormat: "%d-%m-%Y",
                          trigger: "issue_date_trigger",
                          bottomBar: false,
                          onSelect: function() {                                  
                                  this.hide();
                          }
                  });  				 
                </script>			</td>
		  </tr>
		  		 
		  <tr>
		    <td class="reg_item">Raised By</td>
			<td><input name="raised_by" type="text" value="<? if(isset($inrow['raised_by'])){ echo $inrow['raised_by'];}else{ echo $_SESSION['UserID'].'t';}?>"  size="35" readonly></td>
		  </tr> 
		</table>		
		
		<br />
		
		
		
 <table width="60%" border="1" class="reg_input">
  <tr>
    <th scope="col">S.No.</th>
	<th scope="col">Code</th>
    <th scope="col">Description</th>
	<th scope="col" width="10%">Units</th>
    <th scope="col">Required Qty.</th>
	<th scope="col">Issue Qty.</th>	
    </tr>
	<?php 
	if(!empty($_GET['indent_id']) && is_numeric($_GET['indent_id'])){
		$SQL = "select *,s.stockid from indents i
				left join indent_items itm on i.indent_id = itm.indent_id
				left join stockmaster s on s.stockid = itm.item_code
				where i.indent_id = $indent_id and itm.issue_status='pending'";	
		//echo $SQL;
		$result=DB_query($SQL,$db,$ErrMsg);
		if(!empty($_POST['maxbrand'])){
			$maxbrand = $_POST['maxbrand'];
		}else{
			$maxbrand = DB_num_rows($result);
		}
		$k=1;
		while($row=DB_fetch_array($result)){
			$myrow['indent_item_id'.$k] = $row['indent_item_id'];
			$myrow['item_code'.$k] = $row['item_code'];
			$myrow['description'.$k] = $row['description'];
			$myrow['req_qty'.$k] = $row['req_qty'];
			$myrow['issue_qty'.$k] = $row['issue_qty'];
			$myrow['units'.$k] = $row['units'];
			$k++;
		}
		//echo $maxbrand;
		for($i=1;$i<=$maxbrand;$i++){
			if(($i%2)==0){
				$class = 'class="EvenTableRows"';
			}else{
				$class = 'class="OddTableRows"';
			}
	?>
		<tr <?=$class;?>>
  	<td><div align="center"><?=$i;?></div>
	<input type="hidden" name="issue_item_id" value="<?=$myrow['indent_item_id'.$i];?>" />	</td>

	<td><div id="item_code<?=$i;?>">
	  <div>
	  <span id="itmcode<?=$i;?>"><?=$myrow['item_code'.$i];?></span>
	    <input type="hidden" size="10" name="item_code<?=$i;?>" id="item_code<?=$i;?>" value="<?=$myrow['item_code'.$i];?>" readonly style="background-color:#eeeeee" />
	  </div>
	</div></td>
    <td><div>
	<span id="itmdesc<?=$i;?>"><?=$myrow['description'.$i];?></span>      
	  
    </div>	</td>
   
    <td>
	<div id="base_unit<?=$i;?>">
	<div>
	<span id="baseunit<?=$i;?>"><?=$myrow['units'.$i];?></span>    </div>
	</div></td>
	 <td><div align="center">
	  <?=$myrow['req_qty'.$i];?>
	  </div></td>
	  <td><div align="center">
	    <input type="text" name="issue_qty<?=$i;?>" size="8" value="<?=$myrow['issue_qty'.$i];?>"  />
	  </div></td>
    </tr>
	
	<? } 
	}?>
</table>
<input type="hidden" name="maxbrand" value="<?=$maxbrand;?>" />
<input type="hidden" name="indent_id" value="<?=$_GET['indent_id'];?>" />

		<p>		
		<table width="250" border="0">
		  <tr>
			<td align="right"><input type="submit" name="submit" value="Submit" border=0 width="99" height="24"  alt="" align="absmiddle"></td>
			<td><input type="button" onclick="window.location.href='issue_search.php?'" name="cancel" value="Cancel" border=0 width="99" height="24"  alt="" align="absmiddle"></td>

			<td><a href="javascript:history.back();"></a></td>
		  </tr>
		</table>
		</p>
		
		
		</form></td>
  </tr>

</table>


			

