<?php
include('./includes/fun-php.php');
include('./includes/fun-HRClass.php');

date_default_timezone_set('Asia/Calcutta');

if(isset($_POST['Submit']))
{                    
// $_REQUEST['issueto']."<br>".$_REQUEST['received_by'];

					       $PageName ='Asset Issue Slip';

			 $prefix_sql=DB_query("Select * from prefix_tbl where prefix_name='".$PageName."' 
								and CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
		$prefix_row=DB_fetch_array($prefix_sql);
		$prefixcode=$prefix_row['prefix_code'];
			
//$prefixcode="CLD9/IS";
	  $sql="select * from asset_issue where site = '".$_REQUEST['sitecode']."' and CompanyId='".$_SESSION['ACGCompanyId']."'";
      
	  $result = DB_query($sql,$db);
	  $numrows = DB_num_rows($result);
	  $sqldata=DB_query("select locationname from locations where loccode='".$_REQUEST['sitecode']."'",$db);
	  $sqltext=DB_fetch_array($sqldata);
	   $sitename=$sqltext['locationname'];
	   $siteCode = $sitename;
	 if($numrows>0)
	 {
			 $i=0;
			 $strmax = "";
			 $myrow=DB_Fetch_array($result);
			 
		 if($siteCode!='')
				$val1= substr($myrow['issue_slipno'],0,18);
				$strChar1 = substr($myrow['issue_slipno'],0,9);
				$strChar = $strChar1."/".ucwords(substr($siteCode,0,1))."/";  
			
			if($numrows>0)
			{ 
				$maxval = $numrows;
			}
			else
			{
				$maxval=0;
			}
		{
			$strChar = $prefixcode."/".ucwords(substr($siteCode,0,1))."/";
		}
	  }
	  else
	  {
	  	$strChar = $prefixcode."/".ucwords(substr($siteCode,0,1))."/";
	  }
	  
	 $strFinal = $maxval+1;
	   $strBothFinal = $strChar.str_pad($strFinal,4,"0",STR_PAD_LEFT); 
 
	 //echo "<pre>";
	 //print_r($_REQUEST);
	 //exit;	
	                    $sql = "INSERT INTO asset_issue set
						site='".$_REQUEST['sitecode']."',  
						issue_slipno='".$strBothFinal."',   					  
						mst_storeid='".$_REQUEST['store']."', 
						issuedate='".FormatDateForSQL($_REQUEST['issuedate'])."',  
						issueto='".$_REQUEST['issueto']."',  
						received_by='".$_REQUEST['received_by']."',
						remarks='".$_REQUEST['remarks']."',
						createdby='".$_SESSION['UserID']."',
						CompanyId='".$_SESSION['ACGCompanyId']."',  
						createddate='".FormatDateForSQL(date('d/m/Y'))."'
						,transaction_time='".date('Y-m-d H:i:s')."' ";
						
				$ErrMsg = _('An error occurred Record Note Save') . '. ' . _('The error says');
				$insertResult = DB_query($sql,$db,$ErrMsg);
			  $Lastreq_id=DB_Last_Insert_ID($db,'asset_issue',$ErrMsg);
			   
				if($Lastreq_id!='')
				{				
					 foreach($_REQUEST['itemid'] as $key=> $val)
					 {		
					 	if($val!=''){
							$sql2 ="INSERT INTO asset_issue_details set issue_id='".$Lastreq_id."',
										 mst_itemid='".$val."',remarks='".$_REQUEST['remarks1'][$key]."',return_date='".FormatDateForSQL($_REQUEST['returndate'][$key])."' " ;	
								$insertResult2 = DB_query($sql2,$db,$ErrMsg);
						DB_query("update machine_center set availability_status='1' where id='".$val."'",$db);
							}
				      }
			       }					
$s->pageLocation("index.php?route=asset_issue_index".$rurl);
 } 
?>
<div id="PickedItems" style="display:none"></div>

<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>Asset Issue Slip</h1>
<form name="form1" action="" method="post" >
<table width="100%" border="0" cellpadding="3" cellspacing="0" style="border-collapse:collapse">
<tr class="row1">
    <td align="right">Branch <span style="color:red">*</span></td>
    <td>
	<?php
		 $sql = "SELECT loccode, locationname FROM locations where CompanyId='".$_SESSION['ACGCompanyId']."'"; 
		$resultStkLocs = DB_query($sql,$db);
		?>
	<select name="sitecode" id="sitecode"  onchange="showsitename158(this.value)">
	<option value="0">Select</option>
      <?php 
			while ($myrow=DB_fetch_array($resultStkLocs))
				{
				  $p='';
				 if($myrow['loccode']==$site)
				 $p='selected';
				
				?>
      <option value="<?php echo $myrow['loccode']; ?>"  <?php  echo $p;?>><?php echo $myrow['locationname'];?></option>
      <?php  }	?>
    </select></td>
    <td colspan="2"></td>
    <td align="right" style="display:none">Godown</td>
    <td style="display:none"><div id='hid-Godown-div'>
	<?php
	      $sql_store ="select   id, storecode, StoreName from storetype where Store_Site='".$site."' ";
		 $resultStkLocs = DB_query($sql_store,$db);
		?>
      <select name="store" id="store">     
        <?php 
		while ($myrow=DB_fetch_array($resultStkLocs))
		{
		 	 $p1='';
				 if($myow['id']==$store)
				 $p1='selected';
		?>
        <option value="<?php echo $myrow['id'];?>"  <?php  echo ($myow['id']==$store?'selected':'');?> ><?php echo $myrow['storecode'];?></option>
        <?php 
		}
		?>
      </select></div></td>
</tr>
  <tr class="row1">
    <td width="13%" align="right">Issue Slip No.</td>
    <td width="35%"><div id="textitem"><input name="issue_slipno" type="text" id="issue_slipno" readonly="readonly" value="Auto Generated"></div></td>
    <td width="13%" align="right">Issue Date</td>
    <td><input name="issuedate" type="text" id="issuedate" value="<?php echo date('d/m/Y'); ?>" class="datepicker" readonly/></td>
  </tr>  
  <tr class="row1">
  	<td style="display:none;" id="ConShow">&nbsp;</td>
   <!-- <td class="Contract">Contractor Name</td>
    <td class="Contract">
   <?php //if($received_by=='0' || $received_by=='') {?>
	<select name="issueto" id="issueto" style="width:150px;" onchange="Get_contractor(this.value)">
	<option value="">Select</option>
	<?php 
	//$floor_sql=DB_query("Select * from contractor_master where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
	$floor_sql=DB_query("SELECT * FROM suppliers WHERE vendorgroup='C'",$db);
	while($floor_row=DB_fetch_array($floor_sql))
	{?>
	<option value="<?php echo $floor_row['id'];?>"<?php echo $issueto==$floor_row['id']?'selected':''; ?>><?php echo $floor_row['vendor_name'];?></option>
	<?php 	
	}
	?>
    </select><?php //} ?></td> -->
    <td style="display:none;" id="EmpShow">&nbsp;</td>
    <td align="right" class="hempname">Employee Name</td>
    <td class="hempname">
    <?php if($issueto==''){ ?>	
	<select name="received_by" id="received_by">
	<option value="0">Select</option>
	<?php 
	$floor_sql=DB_query("Select * from empmaster where CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
	while($floor_row=DB_fetch_array($floor_sql))
	{
			     $p='';
				 if($floor_row['id']==$received_by)
				 $p='selected';	
	
	?>
	<option value="<?php echo $floor_row['id'];?>"<?php echo $received_by==$floor_row['id']?'selected':''; ?>><?php echo $floor_row['emp_name'];?></option>
	<?php 	
	}
	?>
    </select><?php } ?></td> 
    <td colspan="2"></td>   
  </tr>
</table>
<p id="show_data" align="left">
	<input type="button" name="profile_popup" value="Pick Asset" id="profile_popup" class="search" /> <input name="button" type="button" class="search"  onclick="deleteRow('dataTable')" value="Delete" />
	</p> 

<table width="100%" border="0" cellpadding="3" cellspacing="0" id="insert_table_row" class="thborder">
<tr>
  <th width="4%">S. No.</th>
  <th width="1"></th>
  <th width="10%">Asset Code <span style="color:red">*</span></th>
  <th>Asset Name</th>
  <th width="7%">UOM</th>
  <th width="7%">Quantity</th>
  <th width="20%">Remarks</th>
  <th width="8%">Return Date</th>
</tr>
<!-- <tbody id="add_dynamic_row">-->
<tbody id="dataTable">
<tr>
	<td><input name="delitem[]" type="checkbox" value="" id="delitem" /></td>
	<td><input type="hidden" readonly id="itemid" name="itemid[]" /></td>
	<td><input type="text" value="" readonly tabindex="" id="itemcode" name=""size="20"  /></td>
	<td><input type="text" name="" readonly id="descriptions"  value="" /></td>
	<td><input type="text" name="" id="uom" value=""  readonly /></td>
	<td><input type="text" name="" id="quanitity" value=""  readonly /></td>
	<td><input type="text" name="remarks1[]" id="remarks" size="10" value=""  /></td>
	<td><input type="text" name="returndate[]" id="returndate" readonly value="" class="datepicker"  /></td>
</tr>
</tbody>
</table>
<input type="hidden" name="hiditemcount" id="hiditemcount" value="" />
  <input type="hidden" name="" value="0" id="edit_cnt" />
    <input type="hidden" name="" value="0" id="rowcount1" />
<br />

<table width="100%" border="0" cellpadding="3" cellspacing="0">
<tr class="row1">
	<td width="13%" align="right">Remarks</td>
	<td width="35%"><textarea name="remarks"  id="remark"></textarea></td>
	<td width="13%" align="right">Issued By</td>
	<td><input type="text" name="" readonly value="<?php  echo $_SESSION['UsersRealName'] ?>" id="rowcount1" /></td>
</tr>
</table>

<p align="center">
  <input type="submit" name="Submit" value="Save"  onclick="return validation();" class="search"/>
  <!-- <input type="reset" name="reset" value="View" class="search"/> -->
  <input type="button" value="Cancel" name="Back" class="search" onclick="location.reload();" />  
</p>
</form>

</div></div>


<script language="javascript" type="text/javascript">
 
function Get_contractor(str)
{	
	//alert(str);
	if(str!='')
	{
		$(".hempname").hide();
		$("#EmpShow").show();
	}
	else
	{
		$(".hempname").show();
		$("#EmpShow").hide();
	}
	
}
function Get_Employee(str)
{
		
if (str!="0")
  {
 		$(".Contract").hide();
 		$("#issueto").val("");
 		$("#ConShow").show();

  }
  else if (str=="0")
  {
 		$(".Contract").show();
 		$("#ConShow").hide();
  }

var xmlhttp;
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("dept").innerHTML=xmlhttp.responseText;
	
    }
  }
  
xmlhttp.open("GET","get_empdata.php?department="+str,true);
xmlhttp.send();	
	
}

function showsitename158(str)
{

var xmlhttp;
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)
    {
	var data=xmlhttp.responseText.split("__");	
	
	document.getElementById("hid-Godown-div").innerHTML=data[0];
    document.getElementById("empdta").innerHTML=data[1];
	document.getElementById("jobcard").innerHTML=data[2];
	
    }
  }
  xmlhttp.open("GET","GoDownIssueSlip.php?site="+str,true);
	xmlhttp.send();
}

</script>

<script type="text/javascript" language="javascript">
//$.noConflict();
$(document).ready(function(){

		//var cnt=document.getElementById("count").value;
		$('input[name=profile_popup]').click(function(){
		//alert(document.getElementById("sitecode").value);	
		var site=document.getElementById("sitecode").value;
		if(site=='0')
		{
		alert("Must Select Site first ");
		}
		
	else if(document.getElementById("issuedate").value=='')
	{
	alert("Please Select Date First");
	}
		else
		{
		//	$.noConflict();
		//var LeaveType=document.getElementById("LeaveType_"+tt2).value;
		$("#PickedItems").dialog({
				title:'Item List',
				modal: true,
				resizable: false,
				width:900,
				height:400,
				closeText: 'fechar',
				draggable: true,
				show: 'fade',
				hide: 'fade',
				dialogClass: 'main-dialog-class',
				buttons: {
					Exit: function()
					{
						$(this).dialog("close");
					}
				}
				
			});

		GetitemListForDirectPO(site);
		}});
	}) ;
	
function GetitemListForDirectPO(str)
{

  var store1=document.getElementById("store").value;
	
		
		var issusdate=document.getElementById("issuedate").value;
		
	
	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("PickedItems").innerHTML=xmlhttp.responseText;
	}
	}
	
	xmlhttp.open("GET","asset_issue_picked_items.php?site="+str+"&date="+issusdate+"&store="+store1,true);
	xmlhttp.send();
	
}

function getItemsList(id,code,shade,desc,ctr)
{  
	var dup='empty';
	var itemcount=parseInt(document.getElementById('rowcount1').value)+1;
	
	var edit_cnt=document.getElementById('edit_cnt').value;
	//alert(edit_cnt);
	if(edit_cnt=='')
	{
		var counter=1;
	} 
	else if(edit_cnt>itemcount)
	{
		var counter=edit_cnt;
		var itemcount=parseFloat(counter)+parseFloat(itemcount);
	}
	else
	{
		var counter=edit_cnt;
	}
		
	//alert(counter);
//IF WE CHECK THE CHECK BOX FOR SELECT ANY ITEM***********************START HERE******************************************
	if(document.getElementById("row_id_"+ctr).checked==true)
	{	    
		if(document.getElementById('itemid') && document.getElementById('itemid').value=='')
		{
			if(edit_cnt=='')
			{
				var counter=0;
			}
		
			for(var j=0; j<counter; j++)
			{
				if(document.getElementById('itemid_'+j).value==id)
				{
					alert("Duplicate Value"); 
					document.getElementById("row_id_"+ctr).checked=false;
					var dup = "dup";
					break;
				}
			}
			
			if(dup=='empty')
			{	
				var data=addRow('dataTable');
				document.getElementById('itemid').value = id;
				document.getElementById('itemcode').value = code;
				document.getElementById('descriptions').value = shade;	
				document.getElementById('uom').value =desc ;
				document.getElementById('quanitity').value ='1' ;		
				//document.getElementById('Stockbalance').value =current ;					
				//document.getElementById('instrumentname').value = desc;					
			}
		} 
		
		else
		{
			for(var i=counter; i<=itemcount; i++)
			{				
				if(edit_cnt=='')
				{
					var counter=0;
				}
				for(var j=0; j<counter; j++)
				{}
				if(document.getElementById('itemid'))
				{
				if(document.getElementById('itemid').value==id)
				{
					alert("Duplicate Value");
					document.getElementById("row_id_"+ctr).checked=false;
					break;
				}
				}
				if(document.getElementById('itemid_'+i))
				{
				if(document.getElementById('itemid_'+i).value==id)
				{
					alert("Duplicate Value"); 
					document.getElementById("row_id_"+ctr).checked=false;
					break;
				}
				}
				
				if(dup=='empty')
				{
				if(!document.getElementById('itemid_'+i))
				{	
					
				var data= addRow('dataTable');	
				}
					if(document.getElementById('itemid_'+i))
					{	
					
					if(document.getElementById('itemid_'+i).value=='')
					{	
						document.getElementById('itemid_'+i).value = id;
						document.getElementById('itemcode_'+i).value = code;
						document.getElementById('descriptions_'+i).value = shade;
						document.getElementById('uom_'+i).value =desc ;	
						document.getElementById('quanitity_'+i).value ='1' ;	
				        //document.getElementById('Stockbalance_'+i).value =current ;
						//document.getElementById('instrumentname_'+i).value = desc;	
												
						break;
					}
					}
					else
					{
						continue;
					}
				}
			}
		}
	}
	
//IF WE CHECK THE CHECK BOX FOR SELECT ANY ITEM***********************END HERE******************************************


//IF WE UNCHECK THE CHECK BOX FOR DESELECT ANY ITEM***********************START HERE******************************************
	else
	{
		if(document.getElementById('itemid').value==document.getElementById("row_id_"+ctr).value)
		{
			document.getElementById('itemid').value = '';
			document.getElementById('itemcode').value = '';
			document.getElementById('descriptions').value = '';
			document.getElementById('uom').value = '';	
			document.getElementById('quanitity').value ='' ;	
			//document.getElementById('Stockbalance').value = '';
			//document.getElementById('instrumentname').value = '';
		}
		else
		{
			for(var i=counter; i<itemcount; i++)
			{
				if(document.getElementById('itemid_'+i).value==document.getElementById("row_id_"+ctr).value)
				{
					document.getElementById('itemid_'+i).value = '';
					document.getElementById('itemcode_'+i).value = '';
					document.getElementById('descriptions_'+i).value = '';
					document.getElementById('uom_'+i).value = '';	
					document.getElementById('quanitity_'+i).value ='' ;	
					//document.getElementById('Stockbalance_'+i).value = '';
					//document.getElementById('instrumentname_'+i).value = '';
				}
			}
		}
	}
//IF WE UNCHECK THE CHECK BOX FOR DESELECT ANY ITEM***********************END HERE******************************************
}
function date12()
{
$('.datepicker').removeClass('hasDatepicker').datepicker({ dateFormat: 'dd/mm/yy',changeMonth: true, changeYear: true,yearRange: '-80:+20'});
}
function addRow(tableID) 
{		
	var table = document.getElementById(tableID);
	var rowCount = table.rows.length;
	var row = table.insertRow(rowCount);
	document.getElementById("hiditemcount").value=rowCount;
	var row1=document.getElementById("rowcount1").value;
	var rr=parseInt(row1)+1;
	document.getElementById("rowcount1").value=rr;
	document.getElementById("edit_cnt").value=rr;
	var colCount = table.rows[0].cells.length;
	for(var i=0; i<colCount; i++) {	
	//var newCount=parseInt(rowCount)+parseInt(edit_cnt)-1;
	var newCount=parseInt(rowCount);
	var newcell = row.insertCell(i);	
	
	newcell.innerHTML = table.rows[0].cells[i].innerHTML;
	//newcell.style.background="#fff";
	
	switch(newcell.childNodes[0].type) 
	{
	case "text":
	newcell.childNodes[0].value = "";
	//var nod_id = newcell.childNodes[0].id;
	var nod_id = newcell.childNodes[0].id.split("_");
	if(!isNaN(nod_id[1]))
	newcell.childNodes[0].id = nod_id[0]+'_'+rr;
	else
	newcell.childNodes[0].id = nod_id[0]+'_'+rr;
		
	break;
	case "hidden":
	newcell.childNodes[0].value = "";
	var nod_id = newcell.childNodes[0].id.split("_");
	if(!isNaN(nod_id[1]))
	newcell.childNodes[0].id = nod_id[0]+'_'+rr;
	else
	newcell.childNodes[0].id =nod_id[0]+'_'+rr;
	
	break;
	case "button":
	var nod_id = newcell.childNodes[0].id.split("_");
	if(!isNaN(nod_id[1]))
	newcell.childNodes[0].id = nod_id[0]+'_'+rr;
	else
	newcell.childNodes[0].id = nod_id[0]+'_'+rr;
	
	break;
	case "checkbox":
	newcell.childNodes[0].checked = false;
	var nod_id = newcell.childNodes[0].id.split("_");
	if(!isNaN(nod_id[1]))
	newcell.childNodes[0].id = nod_id[0]+'_'+rr;
	else
	newcell.childNodes[0].id = nod_id[0]+'_'+rr;
	
	break;
	case "select-one":
	newcell.childNodes[0].selectedIndex = 0;
	var nod_id = newcell.childNodes[0].id.split("_");
	if(!isNaN(nod_id[1]))
	newcell.childNodes[0].id = nod_id[0]+'_'+rr;
	else
	newcell.childNodes[0].id =nod_id[0]+'_'+rr;
	
	break;	
	}
		//document.getElementById("stock_perunit_"+rowCount).value=1;
	}
	date12();
}

function validation()
{
	 if(document.getElementById('sitecode').value=='0')
	 {
	 	 alert("Please Select Branch");
	 	 return false;
     }
      if(document.getElementById('itemcode').value=='')
	 {
	 	 alert("Please Select at least one Asset");
	 	 return false;
      }
        return true;

}
	function getdataforSearch()
{  
	//var group=document.getElementById('under_group').value;
	var code=document.getElementById('asset_code').value;
	var name=document.getElementById('asset_name').value;
	//var title=document.getElementById('title').value;
	//var titletext=document.getElementById('title_text').value;

	
	var xmlhttp;
	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)
	{
	var data=xmlhttp.responseText;	 
	document.getElementById("ItemListDiv").innerHTML=data;
	}
	}
	xmlhttp.open("GET","asset_search_issue.php?code="+code+"&name="+name,true);
	xmlhttp.send();
}	
</script>
