<?php
session_name('CAKEPHP'); session_start();
$PageSecurity = 1;
include('config.php');
include('includes/ConnectDB.inc');
include('includes/function_lib.php');
include('includes/LanguageSetup.php');
include('includes/DateFunctions.inc');
?>
<link rel="stylesheet" href="POModule/css/purchase_requision_print.css">
<script>
function getOther(int)
{
	if(int=="other")
	{
	document.getElementById("otherReason").style.display="block";
	}
	else
	{
	document.getElementById("otherReason").style.display="none";
	}
}

function validation(){
	if(document.getElementById("reason").value=="")
	{
	alert('Select Reason');
	document.getElementById("reason").focus();
	return false;
	}
	if(document.getElementById("reason").value=="other")
	{
		if(document.getElementById("otherReason").value=="")
		{
			alert('Enter Reason');
			document.getElementById("otherReason").focus();
			return false;
		}
	}
	}
</script>
<div class="body_contain">
<div class="tab_div">

<form action="" method="post" onSubmit="return validation();">
<table width="100%" border="0">
  <tr>
    <td align="center"><strong>Reason</strong></td>
  </tr>
  <tr>
   <?php $mosql=DB_query("SELECT id,reason FROM reason_master where moduleid='".$_REQUEST['modid']."' and frm_id='".$_REQUEST['frmid']."' and reason_type='3' and created_by='".$_SESSION['UserID']."' and CompanyId='".$_SESSION['ACGCompanyId']."' order by created_date DESC",$db);?>
    <td><select name="reason" id="reason" style="width:200px;" onChange="getOther(this.value)">
      <option value="">--Select--</option>
	 <?php while($mo=DB_fetch_array($mosql)){ ?>
	  <option value="<?php echo $mo['id']; ?>"><?php echo $mo['reason']; ?></option>
	  <?php }?>
	  <option value="other">Other</option>
    </select>
	<textarea name="otherReason" id="otherReason" style="width:100%;display:none;"></textarea></td>
  </tr>
  <tr>
    <td align="center"><input type="submit" name="Cancel" value="Proceed" /></td>
  </tr>
</table>
</form>  
</div>
<?php
if(isset($_POST['Cancel']))
{
	
		$mosql1=DB_query("SELECT createdBy,createdDate,PO_WO_MTO FROM addmrn where id='".$_REQUEST['id']."'",$db);
 		$mo1=DB_fetch_array($mosql1);
		
		$dataUpadate['approval_status'] = 0;	
		$dataUpadate['mrn_status'] = 0;	
		$conditionUpa="id='". $mo1['PO_WO_MTO']."'"; 
		$s->RecordEdit('direct_po', $dataUpadate, $conditionUpa);
	
		if($_REQUEST['reason']=='other')
	{
		$datain['moduleid']=$_REQUEST['modid']; 
		$datain['frm_id']=$_REQUEST['frmid']; 
		$datain['reason_type']='3'; 
		$datain['reason']=$_REQUEST['otherReason']; 
		$datain['created_by']=$mo1['createdBy']; 
		$datain['created_date']=$mo1['createdDate']; 
		$datain['companyID']=$_SESSION['ACGCompanyId'];  
		$datain['modify_by']=$_SESSION['UserID'];
		$datain['modify_date']=date('Y-m-d');  
		$s->RecordInsert('reason_master', $datain);
		$last_id=DB_Last_Insert_ID($db,'reason_master','id');
	    $datareason['doc_id']=$_REQUEST['id']; 
		$datareason['reason_id']=$last_id; 
		$datareason['frm_id']=$_REQUEST['frmid']; 
		$datareason['doc_type']='3'; 
		$datareason['doc_date']=$mo1['createdDate']; 
		$datareason['doc_created_by']=$mo1['createdBy']; 
		$datareason['company_id']=$_SESSION['ACGCompanyId'];  
		$datareason['modified_by']=$_SESSION['UserID'];
		$datareason['modified_date']=date('Y-m-d');  
		$s->RecordInsert('log_reason', $datareason);
	}
	else
	{
	   $datareason['doc_id']=$_REQUEST['id']; 
		$datareason['reason_id']=$_REQUEST['reason']; 
		$datareason['frm_id']=$_REQUEST['frmid']; 
		$datareason['doc_type']='3'; 
		$datareason['doc_date']=$mo1['createdDate'];
		$datareason['doc_created_by']=$mo1['createdBy'];  
		$datareason['company_id']=$_SESSION['ACGCompanyId'];  
		$datareason['modified_by']=$_SESSION['UserID'];
		$datareason['modified_date']=date('Y-m-d');  
		$s->RecordInsert('log_reason', $datareason);

	}
		
	$cond="issue_id='".$_REQUEST['id']."'";
	$row= $s->DeleteRecord('addmrnqty', $cond);
	
	$cond="id='".$_REQUEST['id']."'";
	$row= $s->DeleteRecord('addmrn', $cond);
	
echo "<script>parent.window.close();
parent.window.opener.location.href='index.php?route=Inventory/transation/material_receipt_note_exo_index&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script//>";			
				
			}
?>
