1<?php
2/* Quality Test Maintenance */
3
4include('includes/session.php');
5$Title = _('Update Related Items');
6$ViewTopic = 'QualityAssurance';
7$BookMark = 'QA_Tests';
8include('includes/header.php');
9
10echo '<p class="page_title_text"><img alt="" src="', $RootPath, '/css/', $Theme,
11	'/images/inventory.png" title="', // Icon image.
12	$Title, '" /> ', // Icon title.
13	$Title, '</p>';// Page title.
14
15include('includes/SQL_CommonFunctions.inc');
16
17//initialise no input errors assumed initially before we test
18$InputError = 0;
19
20if (isset($_GET['Item'])){
21	$Item = trim(mb_strtoupper($_GET['Item']));
22}elseif (isset($_POST['Item'])){
23	$Item = trim(mb_strtoupper($_POST['Item']));
24}
25if (isset($_GET['Related'])){
26	$Related = trim(mb_strtoupper($_GET['Related']));
27}elseif (isset($_POST['Related'])){
28	$Related = trim(mb_strtoupper($_POST['Related']));
29}
30
31echo '<a href="' . $RootPath . '/SelectProduct.php">' . _('Back to Items') . '</a><br />';
32
33
34$result = DB_query("SELECT stockmaster.description
35					FROM stockmaster
36					WHERE stockmaster.stockid='".$Item."'");
37$myrow = DB_fetch_row($result);
38
39if (DB_num_rows($result)==0){
40	prnMsg( _('The part code entered does not exist in the database') . ': ' . $Item . _('Only valid parts can have related items entered against them'),'error');
41	$InputError=1;
42}
43
44
45if (!isset($Item)){
46	echo '<p>';
47	prnMsg (_('An item must first be selected before this page is called') . '. ' . _('The product selection page should call this page with a valid product code'),'error');
48	include('includes/footer.php');
49	exit;
50}
51
52$PartDescription = $myrow[0];
53
54if (isset($_POST['submit'])) {
55
56	/* actions to take once the user has clicked the submit button
57	ie the page has called itself with some user input */
58
59	//first off validate inputs sensible
60
61	$result_related = DB_query("SELECT stockmaster.description,
62							stockmaster.mbflag
63					FROM stockmaster
64					WHERE stockmaster.stockid='".$_POST['Related']."'");
65	$myrow_related = DB_fetch_row($result_related);
66
67	if (DB_num_rows($result_related)==0){
68		prnMsg( _('The part code entered as related item does not exist in the database') . ': ' . $_POST['Related'] .  _('Only valid parts can be related items'),'error');
69		$InputError=1;
70	}
71
72	$sql = "SELECT related
73				FROM relateditems
74			WHERE stockid='".$Item."'
75				AND related = '" . $_POST['Related'] . "'";
76	$result = DB_query($sql);
77	$myrow = DB_fetch_row($result);
78
79	if (DB_num_rows($result)!=0){
80		prnMsg( _('This related item has already been entered.') , 'warn');
81		$InputError =1;
82	}
83
84	if ($_POST['Related'] == $Item){
85		prnMsg( _('An item can not be related to itself') , 'warn');
86		$InputError =1;
87	}
88
89	if ($InputError !=1) {
90		$sql = "INSERT INTO relateditems (stockid,
91									related)
92							VALUES ('" . $Item . "',
93								'" . $_POST['Related'] . "')";
94		$ErrMsg = _('The new related item could not be added');
95		$result = DB_query($sql,$ErrMsg);
96
97		prnMsg($_POST['Related'] . ' ' . _('is now related to') . ' ' . $Item,'success');
98
99		/* It is safe to assume that, if A is related to B, B is related to A */
100		$sql_reverse = "SELECT related
101					FROM relateditems
102				WHERE stockid='".$_POST['Related']."'
103					AND related = '" . $Item . "'";
104		$result_reverse = DB_query($sql_reverse);
105		$myrow_reverse = DB_fetch_row($result_reverse);
106
107		if (DB_num_rows($result_reverse)==0){
108			$sql = "INSERT INTO relateditems (stockid,
109										related)
110								VALUES ('" . $_POST['Related'] . "',
111									'" . $Item . "')";
112			$ErrMsg = _('The new related item could not be added');
113			$result = DB_query($sql,$ErrMsg);
114			prnMsg($Item . ' ' . _('is now related to') . ' ' . $_POST['Related'],'success');
115		}
116	}
117
118	unset($_POST['Related']);
119
120} elseif (isset($_GET['delete'])) {
121//the link to delete a selected record was clicked instead of the submit button
122
123	/* Again it is safe to assume that we have to delete both relations A to B and B to A */
124
125	$sql="DELETE FROM relateditems
126			WHERE (stockid = '". $Item ."' AND related ='". $_GET['Related'] ."')
127			OR (stockid = '". $_GET['Related'] ."' AND related ='". $Item ."')";
128	$ErrMsg = _('Could not delete this relationshop');
129	$result = DB_query($sql,$ErrMsg);
130	prnMsg( _('This relationship has been deleted'),'success');
131
132}
133
134//Always do this stuff
135
136$sql = "SELECT stockmaster.stockid,
137			stockmaster.description
138		FROM stockmaster, relateditems
139		WHERE stockmaster.stockid = relateditems.related
140			AND relateditems.stockid='".$Item."'";
141
142$result = DB_query($sql);
143
144if (DB_num_rows($result) > 0) {
145	echo '<form method="post" action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '">
146		<div>
147		<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />
148		<table class="selection">
149		<thead>
150			<tr>
151				<th colspan="3">' .
152				_('Related Items To') . ':
153				<input type="text" required="required" autofocus="autofocus" name="Item" size="22" value="' . $Item . '" maxlength="20" />
154				<input type="submit" name="NewPart" value="' . _('List Related Items') . '" /></th>
155			</tr>
156			<tr>
157				<th class="ascending">' . _('Code') . '</th>
158				<th class="ascending">' . _('Description') . '</th>
159				<th>' . _('Delete') . '</th>
160			</tr>
161		</thead>
162		<tbody>';
163
164	while ($myrow = DB_fetch_array($result)) {
165
166		echo '<tr class="striped_row">
167				<td>' . $myrow['stockid'] . '</td>
168				<td>' .  $myrow['description'] . '</td>
169				<td><a href="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '?Item=' . $Item . '&amp;Related=' . $myrow['stockid'] . '&amp;delete=yes" onclick="return confirm(\'' . _('Are you sure you wish to delete this relationship?') . '\');">' . _('Delete') . '</a></td>';
170		echo '</tr>';
171
172	}
173	//END WHILE LIST LOOP
174	echo '</tbody></table><br />';
175	echo '</div>
176		  </form>';
177} else {
178	prnMsg(_('There are no items related set up for this part'),'warn');
179}
180
181echo '<form method="post" action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '?Item=' . $Item . '">';
182echo '<div>';
183echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
184if (isset($_GET['Edit'])){
185	/*the price sent with the get is sql format price so no need to filter */
186	$_POST['Related'] = $_GET['Related'];
187}
188
189echo '<br /><table class="selection">';
190
191echo '<tr><th colspan="5"><h3>' . $Item . ' - ' . $PartDescription . '</h3></th></tr>';
192
193echo '<tr><td>' . _('Related Item Code') . ':</td>
194          <td>
195          <input type="text" class="text" required="required" name="Related" size="21" maxlength="20" value="';
196          if (isset($_POST['Related'])) {
197	         echo $_POST['Related'];
198          }
199          echo '" />
200     </td></tr>
201</table>
202<br /><div class="centre">
203<input type="submit" name="submit" value="' . _('Enter') . '/' . _('Amend Relation') . '" />
204</div>';
205
206
207echo '</div>
208      </form>';
209include('includes/footer.php');
210
211?>