<?php
ob_start();
include('includes/session.inc');
$Title = _('Re-Print Quote');
include('includes/SQL_CommonFunctions.inc');


	

	
	
if (isset($_GET['dispatchid'])) {
		$_SESSION['dispatchid'] = stripslashes($_GET['dispatchid']);
	}
	$stockrequestno= $_SESSION['dispatchid'];
if (isset($_GET['loccode'])) {
		$_SESSION['loccode'] = stripslashes($_GET['loccode']);
	}
	$loccode= $_SESSION['loccode'];
if (isset($_GET['departmentid'])) {
		$_SESSION['departmentid'] = stripslashes($_GET['departmentid']);
	}
	$departmentid= $_SESSION['departmentid'];
if (isset($_GET['despatchdate'])) {
		$_SESSION['despatchdate'] = stripslashes($_GET['despatchdate']);
	}
	$despatchdate= $_SESSION['despatchdate'];


$date = date('d-m-Y',strtotime($despatchdate));
if (isset($_GET['jobno'])) {
		$_SESSION['jobno'] = stripslashes($_GET['jobno']);
	}
	$jobno= $_SESSION['jobno'];
if (isset($_GET['authorised'])) {
		$_SESSION['authorised'] = stripslashes($_GET['authorised']);
	}
	$authorised= $_SESSION['authorised'];
if (isset($_GET['authorized_by'])) {
		$_SESSION['authorized_by'] = stripslashes($_GET['authorized_by']);
	}
	$authorized_by= $_SESSION['authorized_by'];
if (isset($_GET['narrative'])) {
		$_SESSION['narrative'] = stripslashes($_GET['narrative']);
	}
	$narrative= $_SESSION['narrative'];
if (isset($_GET['initiator'])) {
		$_SESSION['initiator'] = stripslashes($_GET['initiator']);
	}
	$initiator= $_SESSION['initiator'];


				$userid = $_SESSION['UserID'];

include('includes/newheader.php');
echo '<p> </p> <p></p><p class="page_title_text"><img alt="" src="'.$RootPath.'/css/'.$Theme.
		'/images/printer.png" title="' .// Icon image.
		_('Reasons for Cancellation .  ') . '" /> ' .// Icon title.
		_('Please this process needs to be done by your manager') . '</p>';// Page title.
echo '<form method="post" action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '">';
	echo '<div class="col-sm-12">';// div class=?
	echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
echo '<table class="table">';



echo '<tr>
			<td>' ._('Enter Reasons'). ':</td>
			<td><input tabindex="'.$j.'" type="text" maxlength="10" size="15" required="required" name="reason"  /></td>
		</tr>';

echo '</td>';

echo '<input type="submit" name="reversal" value="' ._('Process Reversal') . '" />
		</div>
		';



echo '</div>';

echo '</div>';

$reasons = $_POST['reason'];

if (isset($_POST['reversal']) && ($_POST['reason'] == $reasons )){



//first check whether this costing is approved or not

//Print header PDFStockRequestheader.inc
$SQL77 = "SELECT 	*
							 
			FROM stockrequestitems
			WHERE dispatchid='" . $stockrequestno . "'
			";
	
	$result77 = DB_query($SQL77);
	
	while ($myrow77 = DB_fetch_array($result77) ){
		
		$SQL = "UPDATE locstock set quantity = quantity + '" . $myrow77['qtydelivered'] . "'
		       WHERE stockid='" . $myrow77['stockid'] . "'
			   AND loccode='" . $loccode . "'
			";
	
	    $result = DB_query($SQL);
		
		
		
		
		
	}

//update stockrequest

$timenow =time();

		$modifieddatetime = date("Y-m-d h:i:sa", $timenow);
$fijitime = date("d-m-Y h:i:sa", $timenow);
$SQL="UPDATE stockrequest
						SET closed=1,
						deleted = 1,
						deleted_by= 'Shivakar Chand',
						deleted_datetime='".$modifieddatetime."'
					WHERE dispatchid='".$stockrequestno."'";
					$Result=DB_query($SQL);



			$SQL = "SELECT departmentid,initiator

	               

			FROM stockrequest

			WHERE dispatchid='" . $stockrequestno. "'

			";

	

			$result = DB_query($SQL);

	

			$myrowuser = DB_fetch_array($result) ;

			$department = $myrowuser['departmentid'];

			$initiator = $myrowuser['initiator'];

				$EmailSQL="SELECT email

					FROM www_users

					WHERE realname = '" . $initiator."'

						";

		$EmailResult = DB_query($EmailSQL);

			$myEmail=DB_fetch_array($EmailResult);

			

			$initiatoremail = $myEmail['email'];

			

					$ravinesh ='rdeo@wormaldfj.com';

	$sharukh ='msharukh@wormaldfj.com';

	$sachin = 'technical@wormaldfj.com';

	$shiv='shiv.c@wormaldfj.com';

					
$EmailSQL1="SELECT email,realname

					FROM www_users, department

					WHERE department.authoriser = www_users.userid

						AND department.departmentid = '" . $department ."'";

		$EmailResult1 = DB_query($EmailSQL1);

			$myEmail1=DB_fetch_array($EmailResult1);

			 
$authorisersemail = $myEmail1['email'];
$authorisersname = $myEmail1['realname'];


$userid = $_SESSION['UserID'];

$SQLuser = "SELECT email,realname

	               

			FROM www_users

			WHERE userid='" . $userid. "'

			";

	

			$resultuser = DB_query($SQLuser);

	

			$myrowuser = DB_fetch_array($resultuser) ;

		$emailm= $myrowuser['email'];

$realname= $myrowuser['realname'];


	$ConfirmationText = _('This internal stock request - '.$stockrequestno.' has been cancelled by : '.$realname.' at '.$fijitime.'. Therefore all the lineitems  pertaining to the request has been adjusted back to the location stock QTY. This was authorised by '.$authorisersname.' and created by: '.$initiator);
	        

			
			$EmailSubject = _('Stock Request Cancelled');

			

			      
					 mail($shiv,$EmailSubject,$ConfirmationText);
				  mail($ravinesh,$EmailSubject,$ConfirmationText);

				 mail($sharukh,$EmailSubject,$ConfirmationText);

				 mail($sachin,$EmailSubject,$ConfirmationText);

			 mail($initiatoremail,$EmailSubject,$ConfirmationText);

			 mail($authorisersemail,$EmailSubject,$ConfirmationText);
	
	
	
	
$message = "This stock request has been cancelled!";
	echo "<script type='text/javascript'>alert('$message');</script>";
		echo "<script type='text/javascript'>javascript:history.go(-2)</script>"; 
	exit();

}

//include('includes/footer.inc'); 

?>
