-
Notifications
You must be signed in to change notification settings - Fork 123
/
Copy pathCustomerTypes.php
274 lines (215 loc) · 8.45 KB
/
CustomerTypes.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<?php
include('includes/session.php');
$Title = _('Customer Types') . ' / ' . _('Maintenance');
$ViewTopic = 'Setup';
$BookMark = 'CustomerTypes';
include('includes/header.php');
if (isset($_POST['SelectedType'])){
$SelectedType = mb_strtoupper($_POST['SelectedType']);
} elseif (isset($_GET['SelectedType'])){
$SelectedType = mb_strtoupper($_GET['SelectedType']);
}
if (isset($Errors)) {
unset($Errors);
}
$Errors = array();
echo '<p class="page_title_text"><img src="'.$RootPath.'/css/'.$Theme.'/images/maintenance.png" title="' . _('Customer Types') .
'" alt="" />' . _('Customer Type Setup') . '</p>';
echo '<div class="page_help_text">' . _('Add/edit/delete Customer Types') . '</div>';
if (isset($_POST['submit'])) {
//initialise no input errors assumed initially before we test
$InputError = 0;
/* actions to take once the user has clicked the submit button
ie the page has called itself with some user input */
//first off validate inputs sensible
$i=1;
if (mb_strlen($_POST['TypeName']) >100) {
$InputError = 1;
prnMsg(_('The customer type name description must be 100 characters or less long'),'error');
$Errors[$i] = 'CustomerType';
$i++;
}
if (mb_strlen($_POST['TypeName'])==0) {
$InputError = 1;
echo '<br />';
prnMsg(_('The customer type name description must contain at least one character'),'error');
$Errors[$i] = 'CustomerType';
$i++;
}
$CheckSQL = "SELECT count(*)
FROM debtortype
WHERE typename = '" . $_POST['TypeName'] . "'";
$Checkresult=DB_query($CheckSQL);
$CheckRow=DB_fetch_row($Checkresult);
if ($CheckRow[0]>0 and !isset($SelectedType)) {
$InputError = 1;
echo '<br />';
prnMsg(_('You already have a customer type called').' '.$_POST['TypeName'],'error');
$Errors[$i] = 'CustomerName';
$i++;
}
if (isset($SelectedType) AND $InputError !=1) {
$SQL = "UPDATE debtortype
SET typename = '" . $_POST['TypeName'] . "'
WHERE typeid = '" .$SelectedType."'";
$Msg = _('The customer type') . ' ' . $SelectedType . ' ' . _('has been updated');
} elseif ( $InputError !=1 ) {
// First check the type is not being duplicated
$CheckSQL = "SELECT count(*)
FROM debtortype
WHERE typename = '" . $_POST['TypeName'] . "'";
$Checkresult = DB_query($CheckSQL);
$CheckRow = DB_fetch_row($Checkresult);
if ( $CheckRow[0] > 0 ) {
$InputError = 1;
prnMsg( _('The customer type') . ' ' . $_POST['typeid'] . _(' already exist.'),'error');
} else {
// Add new record on submit
$SQL = "INSERT INTO debtortype
(typename)
VALUES ('" . $_POST['TypeName'] . "')";
$Msg = _('Customer type') . ' ' . $_POST["typename"] . ' ' . _('has been created');
$CheckSQL = "SELECT count(typeid)
FROM debtortype";
$Result = DB_query($CheckSQL);
$Row = DB_fetch_row($Result);
}
}
if ( $InputError !=1) {
//run the SQL from either of the above possibilites
$Result = DB_query($SQL);
// Fetch the default price list.
$DefaultCustomerType = $_SESSION['DefaultCustomerType'];
// Does it exist
$CheckSQL = "SELECT count(*)
FROM debtortype
WHERE typeid = '" . $DefaultCustomerType . "'";
$Checkresult = DB_query($CheckSQL);
$CheckRow = DB_fetch_row($Checkresult);
// If it doesnt then update config with newly created one.
if ($CheckRow[0] == 0) {
$SQL = "UPDATE config
SET confvalue='" . $_POST['typeid'] . "'
WHERE confname='DefaultCustomerType'";
$Result = DB_query($SQL);
$_SESSION['DefaultCustomerType'] = $_POST['typeid'];
}
echo '<br />';
prnMsg($Msg,'success');
unset($SelectedType);
unset($_POST['typeid']);
unset($_POST['TypeName']);
}
} elseif ( isset($_GET['delete']) ) {
// PREVENT DELETES IF DEPENDENT RECORDS IN 'DebtorTrans'
// Prevent delete if saletype exist in customer transactions
$SQL= "SELECT COUNT(*)
FROM debtortrans
WHERE debtortrans.type='".$SelectedType."'";
$ErrMsg = _('The number of transactions using this customer type could not be retrieved');
$Result = DB_query($SQL,$ErrMsg);
$MyRow = DB_fetch_row($Result);
if ($MyRow[0]>0) {
prnMsg(_('Cannot delete this type because customer transactions have been created using this type') . '<br />' . _('There are') . ' ' . $MyRow[0] . ' ' . _('transactions using this type'),'error');
} else {
$SQL = "SELECT COUNT(*) FROM debtorsmaster WHERE typeid='".$SelectedType."'";
$ErrMsg = _('The number of transactions using this Type record could not be retrieved because');
$Result = DB_query($SQL,$ErrMsg);
$MyRow = DB_fetch_row($Result);
if ($MyRow[0]>0) {
prnMsg (_('Cannot delete this type because customers are currently set up to use this type') . '<br />' . _('There are') . ' ' . $MyRow[0] . ' ' . _('customers with this type code'));
} else {
$Result = DB_query("SELECT typename FROM debtortype WHERE typeid='".$SelectedType."'");
if (DB_Num_Rows($Result)>0){
$TypeRow = DB_fetch_array($Result);
$TypeName = $TypeRow['typename'];
$SQL="DELETE FROM debtortype WHERE typeid='".$SelectedType."'";
$ErrMsg = _('The Type record could not be deleted because');
$Result = DB_query($SQL,$ErrMsg);
echo '<br />';
prnMsg(_('Customer type') . ' ' . $TypeName . ' ' . _('has been deleted') ,'success');
}
unset ($SelectedType);
unset($_GET['delete']);
}
} //end if sales type used in debtor transactions or in customers set up
}
if (!isset($SelectedType)){
/* It could still be the second time the page has been run and a record has been selected for modification - SelectedType will exist because it was sent with the new call. If its the first time the page has been displayed with no parameters
then 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 the same page again and allow update/input
or deletion of the records*/
$SQL = "SELECT typeid, typename FROM debtortype";
$Result = DB_query($SQL);
echo '<table class="selection">';
echo '<thead>
<tr>
<th class="SortedColumn">' . _('Type ID') . '</th>
<th class="SortedColumn">' . _('Type Name') . '</th>
</tr>
</thead>
<tbody>';
while ($MyRow = DB_fetch_row($Result)) {
printf('<tr class="striped_row">
<td>%s</td>
<td>%s</td>
<td><a href="%sSelectedType=%s">' . _('Edit') . '</a></td>
<td><a href="%sSelectedType=%s&delete=yes" onclick=\'return confirm("' . _('Are you sure you wish to delete this Customer Type?') . '");\'>' . _('Delete') . '</a></td>
</tr>',
$MyRow[0],
$MyRow[1],
htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '?',
$MyRow[0],
htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '?',
$MyRow[0]);
}
//END WHILE LIST LOOP
echo '</tbody></table>';
}
//end of ifs and buts!
if (isset($SelectedType)) {
echo '<div class="centre"><br /><a href="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '">' . _('Show All Types Defined') . '</a></div>';
}
if (! isset($_GET['delete'])) {
echo '<form method="post" action="' . htmlspecialchars($_SERVER['PHP_SELF'],ENT_QUOTES,'UTF-8') . '">
<input type="hidden" name="FormID" value="' . $_SESSION['FormID'] . '" />';
// The user wish to EDIT an existing type
if ( isset($SelectedType) AND $SelectedType!='' ) {
$SQL = "SELECT typeid,
typename
FROM debtortype
WHERE typeid='".$SelectedType."'";
$Result = DB_query($SQL);
$MyRow = DB_fetch_array($Result);
$_POST['typeid'] = $MyRow['typeid'];
$_POST['TypeName'] = $MyRow['typename'];
echo '<input type="hidden" name="SelectedType" value="' . $SelectedType . '" />
<input type="hidden" name="typeid" value="' . $_POST['typeid'] . '" />';
echo '<fieldset>
<legend>', _('Edit Customer Type'), '</legend>';
// We dont allow the user to change an existing type code
echo '<field>
<label for="typeid">' . _('Type ID') . ':</label>
<fieldtext>' . $_POST['typeid'] . '</fieldtext>
</field>';
} else {
// This is a new type so the user may volunteer a type code
echo '<fieldset>
<legend>', _('Create New Customer Type'), '</legend>';
}
if (!isset($_POST['TypeName'])) {
$_POST['TypeName']='';
}
echo '<field>
<label for="TypeName">' . _('Type Name') . ':</label>
<input type="text" name="TypeName" required="required" title="" value="' . $_POST['TypeName'] . '" />
<fieldhelp>' . _('The customer type name is required') . '</fieldhelp
</field>
</fieldset>
<div class="centre">
<input type="submit" name="submit" value="' . _('Accept') . '" />
</div>
</form>';
} // end if user wish to delete
include('includes/footer.php');
?>