<?php ob_start();
$PageSecurity = 4;
date_default_timezone_set('Asia/Calcutta');
$PageName = 'Return Slip';
include('includes/DefinePOClass.php');
unset($_SESSION['ACGRequisition-ItemList_1']);	
unset($_SESSION['ACGRequisition-ItemList']);
$Prefix_SQL = "Select prefix_id,prefix_code,prefix_name from prefix_tbl Where prefix_name ='".$PageName."'";
$Prefix_Result = DB_query($Prefix_SQL,$db);
$Prefix_Row = DB_fetch_array($Prefix_Result);
$Prefix_code = $Prefix_Row['prefix_code'];
$Prefix_date = date("Y");
$Prefix_date++;
$CurrentFinancialYear = date("Y")."-".$Prefix_date; 
if(isset($_GET['issueid'])  && isset($_GET['issueid']) !='')
{$title = _('Update Return Slip');}
else
{$title = _('Add Return Slip');}
include('includes/SQL_CommonFunctions.inc'); 
$_SESSION['modid'];
if (empty($_GET['identifier'])) {
 $identifier=date('U');
} else {
 $identifier=$_GET['identifier'];

}
$date = date($_SESSION['DefaultDateFormat']);

if(isset($_POST['reset']))
{


//unset($_POST);
unset($_REQUEST['site']);
unset($_REQUEST['store']);
unset($_REQUEST['returndate']);

}

if(isset($_POST['Save']))
{    
	$PageName =$_REQUEST['frmid']; //'Asset Return Slip';

		//$prefix_sql=DB_query("Select * from prefix_tbl where prefix_name='".$PageName."' and CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
	$prefix_sql=DB_query("Select * from prefix_tbl where prefix_name='".$PageName."' and module_id='".$_REQUEST['modid']."'",$db);
		$prefix_row=DB_fetch_array($prefix_sql);
		$prefixcode=$prefix_row['prefix_code'];
			
//$prefixcode="CLD9/RS";
	 $sql="select * from asset_return where site = '".$_POST['site']."' and CompanyId='".$_SESSION['ACGCompanyId']."'";
      
	  $result = DB_query($sql,$db);
	  $numrows = DB_num_rows($result);
	  $sqldata=DB_query("select locationname from locations where loccode='".$_POST['site']."'",$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['return_slipno'],0,18);
				$strChar1 = substr($myrow['return_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); 
	
	
	if(empty($_REQUEST['received_by']))
	{
	$receive1=$_REQUEST['issueto'];	
	}
	else
	 {
	$receive2=$_REQUEST['received_by'];		
}	
    $sql = "INSERT INTO asset_return set
						site='".$_REQUEST['site']."',
						store='".$_REQUEST['store']."',  
						receivefrom='".$receive2."',
						issueto='".$receive1."',  
						asset_issue_no='".$_REQUEST['issue_slipno']."',  
						return_slipno='".$strBothFinal."',  
						returndate='".FormatDateForSQL($_REQUEST['returndate'])."',
						remarks='".$_REQUEST['remarks']."',
						createdby='".$_SESSION['UserID']."', 
						CompanyId='".$_SESSION['ACGCompanyId']."',   
						createddate='".FormatDateForSQL(date('d/m/Y'))."',transaction_time='".date('Y-m-d H:i:s')."'  ";

	$insertResult = DB_query($sql,$db,$ErrMsg);
	$Lastreq_id=DB_Last_Insert_ID($db,'asset_return',$ErrMsg);
			 
	if($Lastreq_id!='')
	{
		foreach($_REQUEST['issue_detail_id'] as $key=>$val)
		{
			           $sql2 = "INSERT INTO asset_return_details set
								parentId='".$Lastreq_id."', 
								asset_issue_detail_id='".$val."',
								remarks='".$_REQUEST['return_rem'][$val]."',
								working_status='".$_REQUEST['working_status'][$val]."'";
			      $insertResult2 = DB_query($sql2,$db,$ErrMsg);
		DB_query("update  machine_center set availability_status='0', working='".$_REQUEST['working_status'][$val]."' where id='".$_REQUEST['itemid'][$val]."'",$db);
		DB_query("update asset_issue_details set returning_status='1' where id='".$val."'",$db);
		}
	}
	
$unreturn_count=DB_fetch_assoc(DB_query("select count(*) as tot_child from asset_issue a, asset_issue_details b where a.id=b.issue_id and a.id='".$_REQUEST['issue_slipno']."'",$db));

$return_count= DB_fetch_assoc(DB_query("select count(*) as tot_ret_child from asset_issue a, asset_issue_details b where a.id=b.issue_id and a.id='".$_REQUEST['issue_slipno']."' and b.returning_status='1'",$db));
if($unreturn_count['tot_child']==$return_count['tot_ret_child'])
{
	Db_query("update asset_issue set full_returning_status='1' where id='".$_REQUEST['issue_slipno']."'",$db);
}
	$s->pageLocation("index.php?route=asset_return_index".$rurl);
}
 ?>
 <script>
 $(document).ready(function(){
 	$('#return_submit').submit(function(){
 		if($('#issue_slipno').val()=='0')
 		{
 			alert("Please Select Issue slip Number");
 			return false;
 		}
		var $b = $('.check_validate');
		if($('.check_validate').filter(':checked').length=='0')
		{
			alert("Please check atleast one asset")
			return false;
		}
 	});
 });
 </script>
 <link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h3>Asset Return</h3>
<form name="form1" action="" id="return_submit" method="post" >
<div align="center">
<table width="100%" border="0" cellpadding="3" cellspacing="0" style="border-collapse:collapse">
<tr class="row1">
    <td width="13%" align="right">Branch <span style="color:#FF0000">*</span></td>
    <td width="35%"><?php
 		 $sql = "SELECT loccode, locationname FROM locations where CompanyId='".$_SESSION['ACGCompanyId']."'";
		$resultStkLocs = DB_query($sql,$db);
		?>
	<select name="site" id="site"   onchange="showsitename159(this.value)">
	<option value="0">Select</option>
      <?php 
			while ($myrow=DB_fetch_array($resultStkLocs))
				{
				?>
      <option value="<?php echo $myrow['loccode']; ?>"<?php echo($_REQUEST['site']==$myrow['loccode']?'selected':'')?>><?php echo $myrow['locationname'];?></option>
      <?  }	?>
    </select></td>
	<td width="13%" align="right">Return Date</td>
    <td><input name="returndate" class="datepicker" readonly type="text" id="returndate" value="<?php echo date('d/m/Y') ?>"  class=""/>
    </td>
    <td style="display:none">Store</strong></td>
    <td style="display:none">
	<div  id='hid-Godown-div'><?php
	     $sql_store ='select   id, StoreCode, StoreName from storetype';
		 $resultStkLocs = DB_query($sql_store,$db);
		?>
      <select  name="store" id="store" >
         <?php 
		while ($myrow=DB_fetch_array($resultStkLocs))
		{
			$k='';
		if($myrow['id']==$_REQUEST['store'])
		{
		$k="selected";
		}	
		?>
        <!--<option value="<?php echo $myrow['id'];?>"<?php echo $k; ?>><?php echo $myrow['StoreCode'];?></option>-->
        <?php 
		}
		?>
      </select>
 	  </div>
	 </td>
   </tr>
    
  <tr  class="row1">
  	<td align="right">Issue Slip No. <span style="color:#FF0000">*</span></td>
    <td id="IssueSlip-div">
	<select name="issue_slipno" id="issue_slipno" onchange=ShowDiv(this.value,'GetIssueSlip_no-div','ajax_asset_return_details.php?issueno=1') >
	<option value="0">Select</option>	
	<?php  
	$ISss=DB_query("select * from asset_issue where CompanyId='".$_SESSION['ACGCompanyId']."' AND full_returning_status='0' order by id desc ",$db);
	while($issued=DB_fetch_assoc($ISss)){ ?>
	<option value="<?php echo $issued['id']; ?>"> <?php echo $issued['issue_slipno']; ?></option>	
	<?php }

	?>
	</select></td> 	
    <td align="right">Return Slip No.</td>
    <td><div id=""><input name="return_slipno" type="text" id="	" value="Auto Generate"  readonly/></div></td>
  </tr>  
  <tr class="row1">
  	<!-- <td style="display:none;" colspan="2" id="ConShow">&nbsp;</td>
    <td class="Contract">Contractor Name </strong></td>
    <td class="Contract">
   <?php //if($received_by=='0' || $received_by=='') {?>
	<select name="issueto" id="issueto"  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;" colspan="2" 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"  onchange="Get_Employee(this.value)" >
	<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">&nbsp;</td>    
  </tr>
</table>
</div>


<div align="center">
<div style="overflow:auto; width:100%; height:auto">
<table width="100%" border="0" cellpadding="3" cellspacing="0">
<tr>
<td id="GetIssueSlip_no-div"></td>
</tr>
</table>
</div>
<br>
<table width="100%" cellpadding="3" border="0" cellspacing="0">
<tr class="row1">
	<td width="13%" align="right">Remarks</td>
	<td><textarea name="remarks" rows="5" cols="85" id="remark"><?php echo $mysqlrow['remarks']; ?></textarea></td>
</tr>
</table>
 
</div>
<p align="center"><input type="submit" name="Save" value="Save" onclick="return validate()" class="search" />
  <input type="reset" name="reset" value="Reset" class="search" />
<!--  <a href="index.php?route=Return_Slip_show&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&NewReturn=yes">
  <input type="button" value="Cancel" name="Back" /></a>-->
</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();
  }
	
  val=url.indexOf("?");
   if (str=="")
  {
  document.getElementById(hiddiv).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(hiddiv).innerHTML=xmlhttp.responseText;
    }
  }
  if( val==-1)
xmlhttp.open("GET",url+"?q="+str,true);
else
xmlhttp.open("GET",url+"&q="+str,true);
xmlhttp.send();
}

function showsitename159(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("textitem").innerHTML=data[1];
	
    }
  }
  xmlhttp.open("GET","GoDownIssueSlip1.php?site="+str,true);
	xmlhttp.send();

//xmlhttp.open("GET","getSort.php?s="+str+"&cat="+cat,true);

	
}

</script>
