<?php
$PageSecurity = 15;
$title = _('process  Master');
if ( isset($_GET['pid']) )
	$pid = $_GET['pid'];
elseif (isset($_POST['pid']))
	$pid = $_POST['pid'];
if (isset($_POST['submit'])) 
{
	$InputError = 0;
	if (strpos($_POST['processdescription'],'&')>0 OR strpos($_POST['processdescription'],"'")>0) {
		$InputError = 1;
		prnMsg( _('The process  Master cannot contain the character') . " '&' " . _('or the character') ." '",'error');
	}
	if (trim($_POST['processdescription']) == '') {
		$InputError = 1;
		prnMsg( _('The process  Master may not be empty'), 'error');
	}

	if ($_REQUEST['pid']!=''  AND $InputError !=1) {

			$sql = "SELECT * FROM processmaster
				WHERE pid = " . $pid;
			$result = DB_query($sql,$db);
			if ( DB_num_rows($result) != 0 ) {
				// This is probably the safest way there is
				$myrow = DB_fetch_row($result);
				$Oldprocessdescription = $myrow[0];
				$sql = array();
				$sql[] = "UPDATE processmaster
					SET processdescription='" . $_POST['processdescription'] . "'
					WHERE pid ".LIKE." '".$_GET['pid']."'";
				
			} else {
				$InputError = 1;
				prnMsg( _('The process Master no longer exist.'),'error');
			}
		
		$msg = _('process Master changed');
	} 
	elseif ($InputError !=1) {
		/*pid is null cos no item selected on first time round so must be adding a record*/
		$sql = "SELECT count(*) FROM processmaster
				WHERE processdescription " .LIKE. " '".$_POST['processdescription'] ."'";
		$result = DB_query($sql,$db);
		$myrow = DB_fetch_row($result);
		if ( $myrow[0] > 0 ) {
			$InputError = 1;
			prnMsg( _('The process  Master can not be created because another with the same name already exists.'),'error');
		} else {
			$sql = "INSERT INTO processmaster (
						processdescription  )
				VALUES (
					'" . $_POST['processdescription'] ."'
					)";
		}
		$msg = _('New process  Master added');
	}

	if ($InputError!=1){
		//run the SQL from either of the above possibilites
		if (is_array($sql)) {
			$result = DB_Txn_Begin($db);
			$tmpErr = _('Could not update process  Master');
			$tmpDbg = _('The sql that failed was') . ':';
			foreach ($sql as $stmt ) {
				$result = DB_query($stmt,$db, $tmpErr,$tmpDbg,true);
				if(!$result) {
					$InputError = 1;
					break;
				}
			}
			if ($InputError!=1){
				$result = DB_Txn_Commit($db);
			} else {
				$result = DB_Txn_Rollback($db);
			}
		} else {
			$result = DB_query($sql,$db);
		}
		prnMsg($msg,'success');
	}
	unset ($pid);
	unset ($_POST['pid']);
	unset ($_POST['processdescription']);

} 
if (isset($_GET['delete'])) 
{
	$sql = "SELECT *  FROM processmaster	WHERE pid = " . $pid;
	$result = DB_query($sql,$db);
	if ( DB_num_rows($result) == 0 ) {
		prnMsg( _('Cannot delete this process  Master because it no longer exist'),'warn');
	} else {
		$myrow = DB_fetch_row($result);
		$Oldprocessdescription = $myrow[0];
		$sql= "SELECT COUNT(*) FROM stockmaster WHERE units ".LIKE." '" . $Oldprocessdescription . "'";
		$result = DB_query($sql,$db);
		$myrow = DB_fetch_row($result);
		if ($myrow[0]>0) {
			prnMsg( _('Cannot delete this process  Master because inventory items have been created using this process  Master'),'warn');
			echo '<br>' . _('There are') . ' ' . $myrow[0] . ' ' . _('inventory items that refer to this process  Master') . '</font>';
		} else {
			$sql= "SELECT COUNT(*) FROM contracts WHERE units ".LIKE." '" . $Oldprocessdescription . "'";
			$result = DB_query($sql,$db);
			$myrow = DB_fetch_row($result);
			if ($myrow[0]>0) {
				prnMsg( _('Cannot delete this process  Master because contracts have been created using this process  Master'),'warn');
				echo '<br>' . _('There are') . ' ' . $myrow[0] . ' ' . _('contracts that refer to this process  Master') . '</font>';
			} else {
				$sql="DELETE FROM processmaster WHERE pid ".LIKE."'" . $Oldprocessdescription . "'";
				$result = DB_query($sql,$db);
				prnMsg( $Oldprocessdescription . ' ' . _('process  Master has been deleted') . '!','success');
			}
		}

	} 
	echo "<script>location.href='index.php?route=Process_master&modpid=".$_REQUEST['modpid']."&frmpid=".$_REQUEST['frmpid']."'</script>";
	unset ($pid);
	unset ($_GET['pid']);
	unset($_GET['delete']);
	unset ($_POST['pid']);
	unset ($_POST['pid']);
	unset ($_POST['processdescription']);
}
if (! isset($_GET['delete'])) {
?>
<form method="post" action="">
<?php 
	if (isset($pid)) {
		//editing an existing section
		$sql = "SELECT * FROM processmaster	WHERE pid=" . $pid;
		$result = DB_query($sql, $db);
		if ( DB_num_rows($result) == 0 ) {
			prnMsg( _('Could not retrieve the requested process Master, please try again.'),'warn');
			unset($pid);
		} else {
			$myrow = DB_fetch_array($result);

			$_POST['pid'] = $myrow['pid'];
			$_POST['processdescription']  = $myrow['process '];
?>
			<input type="hpidden" name="pid" VALUE="<?php echo $_POST['pid'];?>" />
			<?php 
		}

	}  else {
		$_POST['processdescription']='';
	}?>
	<table>
	<tr>
		<td><strong>Process</strong></td>
		<td><input type="Text" name="processdescription" size="30" maxlength="30" value="<?php echo $_POST['processdescription'];?>"></td>
		</tr>
		</table>
		<br />
		<div class="centre"><input type="Submit" name="submit" value="Save"></div>
		</form>
	<?php 	
if (!isset($pid)) 
 {
	$sql = "SELECT * FROM processmaster";
	$ErrMsg = _('Could not get process  Masters because');
	$result = DB_query($sql,$db,$ErrMsg);
?>
	<table wpidth="50%" cellpadding="2" cellspacing="2">
	<tr>
	<th>process </th>
	<th>Edit </th>
	<th>Delete</th>
	</tr><?php 
	$k=0; //row colour counter
	while ($myrow = DB_fetch_row($result)) 
	{?>
		<tr bgcolor="#FFFFFF">
		<td><?php echo $myrow[2];?></td>
		<td><a href="index.php?route=Process_master&modpid=<?php echo $_REQUEST['modpid'];?>&frmpid=<?php echo $_REQUEST['frmpid'];?>&pid=<?php echo $myrow[0];?>">Edit</a></td>
		<td><a href="index.php?route=Process_master&modpid=<?php echo $_REQUEST['modpid'];?>&frmpid=<?php echo $_REQUEST['frmpid'];?>&pid=<?php echo $myrow[0];?>&delete=1" onclick="return confirm('This process  will be deleted. Are you sure?')">Delete</a></td>
	</tr>
<?php 
	} ?>
	</table>
	<?php 
} 
} include('includes/footer.inc');
?>