1<?php
2
3
4include('includes/session.php');
5$Title = _('Supplier Types') . ' / ' . _('Maintenance');
6include('includes/header.php');
7
8if (isset($_POST['SelectedType'])){
9	$SelectedType = mb_strtoupper($_POST['SelectedType']);
10} elseif (isset($_GET['SelectedType'])){
11	$SelectedType = mb_strtoupper($_GET['SelectedType']);
12}
13
14if (isset($Errors)) {
15	unset($Errors);
16}
17
18$Errors = array();
19
20echo '<p class="page_title_text"><img src="'.$RootPath.'/css/'.$Theme.'/images/maintenance.png" title="' . _('Supplier Types')
21	. '" alt="" />' . _('Supplier Type Setup') . '</p>
22	<div class="page_help_text">' . _('Add/edit/delete Supplier Types') . '</div>
23	<br />';
24
25if (isset($_POST['submit'])) {
26
27	//initialise no input errors assumed initially before we test
28	$InputError = 0;
29
30	/* actions to take once the user has clicked the submit button
31	ie the page has called itself with some user input */
32
33	//first off validate inputs sensible
34	$i=1;
35	if (mb_strlen($_POST['TypeName']) >100) {
36		$InputError = 1;
37		echo prnMsg(_('The supplier type name description must be 100 characters or less long'),'error');
38		$Errors[$i] = 'SupplierType';
39		$i++;
40	}
41
42	if (mb_strlen(trim($_POST['TypeName']))==0) {
43		$InputError = 1;
44		echo prnMsg(_('The supplier type name description must contain at least one character'),'error');
45		$Errors[$i] = 'SupplierType';
46		$i++;
47	}
48
49	$CheckSQL = "SELECT count(*)
50		     FROM suppliertype
51		     WHERE typename = '" . $_POST['TypeName'] . "'";
52	$CheckResult=DB_query($CheckSQL);
53	$CheckRow=DB_fetch_row($CheckResult);
54	if ($CheckRow[0]>0) {
55		$InputError = 1;
56		echo prnMsg(_('You already have a supplier type called').' '.$_POST['TypeName'],'error');
57		$Errors[$i] = 'SupplierName';
58		$i++;
59	}
60
61	if (isset($SelectedType) AND $InputError !=1) {
62
63		$sql = "UPDATE suppliertype
64			SET typename = '" . $_POST['TypeName'] . "'
65			WHERE typeid = '" . $SelectedType . "'";
66
67		prnMsg(_('The supplier type') . ' ' . $SelectedType . ' ' .  _('has been updated'),'success');
68	} elseif ($InputError !=1){
69		// Add new record on submit
70
71		$sql = "INSERT INTO suppliertype
72					(typename)
73				VALUES ('" . $_POST['TypeName'] . "')";
74
75
76		$msg = _('Supplier type') . ' ' . $_POST['TypeName'] .  ' ' . _('has been created');
77		$CheckSQL = "SELECT count(typeid) FROM suppliertype";
78		$result = DB_query($CheckSQL);
79		$row = DB_fetch_row($result);
80	}
81
82	if ( $InputError !=1) {
83	//run the SQL from either of the above possibilites
84		$result = DB_query($sql);
85
86
87	// Fetch the default supplier type
88		$sql = "SELECT confvalue
89					FROM config
90					WHERE confname='DefaultSupplierType'";
91		$result = DB_query($sql);
92		$SupplierTypeRow = DB_fetch_row($result);
93		$DefaultSupplierType = $SupplierTypeRow[0];
94
95	// Does it exist
96		$CheckSQL = "SELECT count(*)
97			     FROM suppliertype
98			     WHERE typeid = '" . $DefaultSupplierType . "'";
99		$CheckResult = DB_query($CheckSQL);
100		$CheckRow = DB_fetch_row($CheckResult);
101
102	// If it doesnt then update config with newly created one.
103		if ($CheckRow[0] == 0) {
104			$sql = "UPDATE config
105					SET confvalue='" . $_POST['TypeID'] . "'
106					WHERE confname='DefaultSupplierType'";
107			$result = DB_query($sql);
108			$_SESSION['DefaultSupplierType'] = $_POST['TypeID'];
109		}
110
111		unset($SelectedType);
112		unset($_POST['TypeID']);
113		unset($_POST['TypeName']);
114	}
115
116} elseif ( isset($_GET['delete']) ) {
117
118	$sql = "SELECT COUNT(*) FROM suppliers WHERE supptype='" . $SelectedType . "'";
119
120	$ErrMsg = _('The number of suppliers using this Type record could not be retrieved because');
121	$result = DB_query($sql,$ErrMsg);
122	$myrow = DB_fetch_row($result);
123	if ($myrow[0]>0) {
124		prnMsg (_('Cannot delete this type because suppliers are currently set up to use this type') . '<br />' .
125			_('There are') . ' ' . $myrow[0] . ' ' . _('suppliers with this type code'));
126	} else {
127
128		$sql="DELETE FROM suppliertype WHERE typeid='" . $SelectedType . "'";
129		$ErrMsg = _('The Type record could not be deleted because');
130		$result = DB_query($sql,$ErrMsg);
131		prnMsg(_('Supplier type') . $SelectedType  . ' ' . _('has been deleted') ,'success');
132
133		unset ($SelectedType);
134		unset($_GET['delete']);
135
136	}
137}
138
139if (!isset($SelectedType)){
140
141/* It could still be the second time the page has been run and a record has been selected for modification - SelectedType will
142 *  exist because it was sent with the new call. If its the first time the page has been displayed with no parameters then
143 * none of the above are true and the list of sales types will be displayed with links to delete or edit each. These will call
144 * the same page again and allow update/input or deletion of the records
145 */
146
147	$sql = "SELECT typeid, typename FROM suppliertype";
148	$result = DB_query($sql);
149
150	echo '<table class="selection">
151		<thead>
152			<tr>
153		<th class="ascending" >' . _('Type ID') . '</th>
154		<th class="ascending" >' . _('Type Name') . '</th>
155			</tr>
156		</thead>
157		<tbody>';
158
159while ($myrow = DB_fetch_row($result)) {
160
161	printf('<tr class="striped_row">
162			<td>%s</td>
163			<td>%s</td>
164			<td><a href="%sSelectedType=%s">' . _('Edit') . '</a></td>
165			<td><a href="%sSelectedType=%s&amp;delete=yes" onclick="return confirm(\'' .
166				_('Are you sure you wish to delete this Supplier Type?') . '\');">' . _('Delete') . '</a></td>
167		</tr>',
168		$myrow[0],
169		$myrow[1],
170		htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '?',
171		$myrow[0],
172		htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '?',
173		$myrow[0]);
174	}
175	//END WHILE LIST LOOP
176	echo '</tbody></table>';
177}
178
179//end of ifs and buts!
180if (isset($SelectedType)) {
181
182	echo '<div class="centre">
183			<p><a href="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '">' . _('Show All Types Defined') . '</a></p>
184		</div>';
185}
186if (! isset($_GET['delete'])) {
187
188	echo '<form method="post" action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '">';
189    echo '<div>';
190	echo '<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
191	echo '<br />
192		<table class="selection">'; //Main table
193
194	// The user wish to EDIT an existing type
195	if ( isset($SelectedType) AND $SelectedType!='' ) {
196
197		$sql = "SELECT typeid,
198			       typename
199		        FROM suppliertype
200		        WHERE typeid='" . $SelectedType . "'";
201
202		$result = DB_query($sql);
203		$myrow = DB_fetch_array($result);
204
205		$_POST['TypeID'] = $myrow['typeid'];
206		$_POST['TypeName']  = $myrow['typename'];
207
208		echo '<input type="hidden" name="SelectedType" value="' . $SelectedType . '" />';
209		echo '<input type="hidden" name="TypeID" value="' . $_POST['TypeID'] . '" />';
210
211		// We dont allow the user to change an existing type code
212
213		echo '<tr>
214				<td>' ._('Type ID') . ': </td>
215				<td>' . $_POST['TypeID'] . '</td>
216			</tr>';
217	}
218
219	if (!isset($_POST['TypeName'])) {
220		$_POST['TypeName']='';
221	}
222	echo '<tr>
223			<td>' . _('Type Name') . ':</td>
224			<td><input type="text"  required="true" pattern="(?!^\s+$)[^<>+-]{1,100}" title="'._('The input should not be over 100 characters and contains illegal characters').'" name="TypeName" placeholder="'._('less than 100 characters').'" value="' . $_POST['TypeName'] . '" /></td>
225		</tr>';
226
227	echo '<tr>
228			<td colspan="2">
229				<div class="centre">
230					<input type="submit" name="submit" value="' . _('Accept') . '" />
231				</div>
232			</td>
233		</tr>
234		</table>
235		</div>
236		</form>';
237
238} // end if user wish to delete
239
240include('includes/footer.php');
241?>
242