<?php ob_start();
$PageSecurity = 2;
if (empty($_GET['identifier'])) {
			 $identifier=date('U');
		} else {
			$identifier=$_GET['identifier'];
		}
include('includes/session.inc');
$title = _('Material Issue');
include('includes/header.inc');
include('includes/function_lib.php');
echo '<a href="'. $rootpath . '/material_issue.php?' . SID . "identifier=".$identifier.'">'. _('Back>>'). '</a>';
$time = date('G:ia');
$from_site=$_REQUEST['from_site'];
$prefix_sql=DB_query("select * from prefix_tbl where prefix_name='".$title."' ",$db);
$prefix_row=DB_fetch_array($prefix_sql);
$prefix=$prefix_row['prefix_code'];

    $sql="select * from material_issue where from_site = '".$from_site."' order by voucher_no desc limit 1";
      
	 $result = DB_query($sql,$db);
	 $myrow=DB_Fetch_array($result);
	 $numrows = DB_num_rows($result);
	 if($numrows>0)
	 {
			$voucher_no=$myrow['voucher_no'];
			$voucher_brk1=substr($voucher_no,0,12);
			$voucher_brk2=substr($voucher_no,-4);
			$final_voucher=$voucher_brk2+1;
			$strfinal=$voucher_brk1.str_pad($final_voucher,4,"0",STR_PAD_LEFT);
		
	 }
	else
	{
			$strfinal=$prefix."/".$from_site."/0001";
			
	}		
?>
<script language="javascript" type="text/javascript">
function calculate_total()
{
 	var grand_total = 0;
	var counter = document.getElementById("counter").value;
 	var i;
	for(i=0; i<counter; i++)
	{
		var quantity = document.getElementById("quantity_"+i).value;
		//alert(document.getElementById("quantity_"+i).value);
		
		if(quantity != '')
		{
			grand_total = parseInt(grand_total) + parseInt(quantity);
			 
		}
	}
	document.getElementById("grand_total_quantity").value = parseInt(grand_total);
		
	

}
</script>
<strong><h2 align="center"><?php echo $title; ?></h2><p align="center">&nbsp;</p></strong>


<?php if($_REQUEST['save'])
{
  	$cnt = $_POST['counter'];
	$total_inventory_qty=0;
	for($CR=0;$CR<$cnt;$CR++)
	{
		$total_inventory_qty += $_REQUEST['quantity_in_inventory'][$CR];
	}
	
	 $issue_sql = "INSERT INTO material_issue(
											id,
											from_site,
											to_site,store,
											voucher_no,
											voucher_date,
											time_of_issue,
											grand_total_quantity,
											total_inventory_qty)
				  					 values ('',
									 		 '".$_REQUEST['from_site']."',
											 '".$_REQUEST['to_site']."',
											 '".$_REQUEST['store']."',
											 '".$_REQUEST['voucher_no']."',
											 '".FormatDateForSQL($_REQUEST['voucher_date'])."',
											 '".$_REQUEST['time_of_issue']."',
											 '".$_POST['grand_total_quantity']."',
											 '".$total_inventory_qty."'
											 ) ";
											//echo "<pre>";
											//print_r($issue_sql); exit;
	$issue_result = DB_query($issue_sql,$db);
	$last_id=DB_Last_Insert_ID($db,'material_issue','id');	
	
	if($last_id != '')
	{
		for($k=0; $k<$cnt; $k++)
		{
			$item_sql = "INSERT INTO material_issue_details(
												id,addmrnid,
												material_issue_id,
												item_code,
												item_description,
												quantity_in_inventory,
												quantity,narration,
												remaining_qty)
										values('', '".$_REQUEST['addmrnid'][$k]."',
											   '".$last_id."',
											   '".$_POST['item_code'][$k]."',
											   '".$_POST['item_description'][$k]."',
											   '".$_POST['quantity_in_inventory'][$k]."',
											   '".$_POST['quantity'][$k]."',
											   '".$_POST['narration'][$k]."',
											   '".($_POST['quantity_in_inventory'][$k]-$_POST['quantity'][$k])."'
											   ) ";
				//echo "<pre>";
				//print_r($item_sql);	 exit;						   
											   $item_result = DB_query($item_sql,$db);
		}
	}
prnMsg( _('Record Inserted Successfully!'),'success');
echo "<SCRIPT LANGUAGE='JavaScript'>window.location='material_issue_list.php'</SCRIPT>"; 
}
 ?>
<form method="post" action="">
<table width="100%" height="135" border="0" cellpadding="2" cellspacing="1">
 <tr>
 <td>
 
 <table width="80%" height="55" border="0" cellpadding="2" cellspacing="1">
          <tr>
            <td height="26"><strong>VOUCHER NO. </strong></td>
            <td>
			<input name="from_site" type="hidden" value="<?php echo $_REQUEST['from_site'] ?>" />
			<input name="store" type="hidden" value="<?php echo $_REQUEST['store'] ?>" />
			<input type="text" name="voucher_no" id="voucher_no" value="<?php echo $strfinal;?>" size="25" readonly="true"/></td>
          </tr>
		  <tr>
            <td height="26"><strong>VOUCHER DATE </strong></td>
            <td><input type="text" name="voucher_date" id="voucher_date" class="datepicker" value="<?php echo $_REQUEST['voucher_date']?>" size="15"></td>
          </tr>
		   
		  <tr><td><strong>ISSUE TO</strong></td>
		  <td colspan="3"><select name="to_site" id="to_site">
            <option value="000">Select Site</option>
            <?php 
			$site_sql = "Select loccode, locationname from locations";
			$site_result = DB_query($site_sql,$db);
			while($site_row = DB_fetch_array($site_result))
			{ 
				$p='';
				if($site_row['loccode'] == $_REQUEST['to_site'])
				$p= "selected";
			?>
            <option value="<?php echo $site_row['loccode']; ?>" <?php echo $p;?>><?php echo $site_row['locationname']; ?></option>
            <?php }
			?>
          </select></td>
		  </tr>
        </table>
		
		
 </td>
 </tr>
    <tr>
      <td height="133">
	  <table width="80%" height="52" border="0" cellpadding="2" cellspacing="1">
          <tr>
            <th width="13%" height="23"><strong>ITEM CODE</strong></th>
            <th width="47%"><strong>ITEM NAME </strong></th>
            <th width="20%"><strong>QUANTITY IN INVENTORY</strong></th>
            <th width="20%"><strong>QUANTITY TO TRANSFER</strong></th>
            <th width="20%"><strong>NARRATION</strong></th>
          </tr>
          <?php 
		  
   if($_REQUEST['select'])
  {
		 // print_r($_REQUEST['list_item']);
		  $list_item=$_REQUEST['list_item'];
		  //echo count($_REQUEST['list_item']);
		//  print_r($_REQUEST['list_item']);
		  for($i=0;$i<count($_REQUEST['list_item']);$i++)
		  {
			 $pos= strpos($_REQUEST['list_item'][$i], '#');
				$value= substr($_REQUEST['list_item'][$i], 0, $pos);
			$value1= substr($_REQUEST['list_item'][$i],  $pos);
		 	   $value2= substr($value1,1);
  	   	$sql = "select distinct itemcode, description ,
	sum(reciveqty)
	from addmrnqty inner join addmrn on(addmrnqty.issue_id = addmrn.id) 
	where  addmrnqty.itemcode = '".$value2."'
	group by itemcode
	order by 1";
		$res = DB_query($sql,$db);	
		while($res_row = DB_fetch_array($res))
		{
		?>
          <tr>
            <td height="26"><?php echo $res_row['itemcode']; ?>
			 <input type="hidden" name="addmrnid[]" value="<?php echo $value; ?>"   />
			
                <input type="hidden" name="item_code[<?php echo $i;?>]" value="<?php echo $res_row['itemcode']; ?>" id="item_code_<?php echo $i?>" />
				<?php echo $_REQUEST['hid_addmrnid'][$i]; ?>
			
				</td>
            <td><?php echo $res_row['description']; ?>
                <input type="hidden" name="item_description[<?php echo $i;?>]" value="<?php echo $res_row['description']; ?>" id="item_description_<?php echo $i?>" /></td>
            <td><?php echo $res_row['sum(reciveqty)']; ?>
                <input type="hidden" name="quantity_in_inventory[<?php echo $i;?>]" value="<?php echo $res_row['sum(reciveqty)']; ?>" id="quantity_in_inventory_<?php echo $i;?>" /></td>
            <td><input type="text" name="quantity[<?php echo $i;?>]" id="quantity_<?php echo $i;?>"  />
            </td>
            <td><input type="text" name="narration[<?php echo $i;?>]" id="narration_<?php echo $i;?>"/></td>
          </tr>
          <?php }
		  } 
		  
	}		  ?>
          <tr>
            <td><input type="text" name="counter" id="counter" value="<?php echo $i; ?>"  /></td>
            <td colspan="4"><div align="right"><strong>Total Quantity To Transfer</strong>
                    <input type="text" name="grand_total_quantity" id="grand_total_quantity" value="" size="10" onFocus="calculate_total()"/>
            </div></td>
          </tr>
        </table>
          <p align="center">
            <input type="submit" name="save" value="Save" />
			 <input type="submit" name="cancel" value="Cancel" />
          </p></td>
    </tr>
  </table>
</form>


