6 include('includes/session.inc');
7 $title = _('Sales Types') . ' / ' . _('Price List Maintenance');
8 include('includes/header.inc');
10 if (isset($_POST['SelectedType'])){
11 $SelectedType = strtoupper($_POST['SelectedType']);
12 } elseif (isset($_GET['SelectedType'])){
13 $SelectedType = strtoupper($_GET['SelectedType']);
17 if (isset($_POST['submit'])) {
19 //initialise no input errors assumed initially before we test
22 /* actions to take once the user has clicked the submit button
23 ie the page has called itself with some user input */
25 //first off validate inputs sensible
27 if (strlen($_POST['TypeAbbrev']) > 2) {
29 prnMsg(_('The sales type (price list) code must be two characters or less long'),'error');
30 } elseif ($_POST['TypeAbbrev']=='' OR $_POST['TypeAbbrev']==' ' OR $_POST['TypeAbbrev']==' ') {
32 prnMsg('<BR>' . _('The sales type (price list) code cannot be an empty string or spaces'),'error');
33 } elseif (strlen($_POST['Sales_Type']) >20) {
35 echo prnMsg(_('The sales type (price list) description must be twenty characters or less long'),'error');
36 } elseif ($_POST['TypeAbbrev']=='AN'){
38 prnMsg (_('The sales type code cannot be AN since this is a system defined abbrevation for any sales type in general ledger interface lookups'),'error');
41 if ($SelectedType AND $InputError !=1) {
43 $sql = "UPDATE salestypes
44 SET sales_type = '" . $_POST['Sales_Type'] . "'
45 WHERE typeabbrev = '$SelectedType'";
47 $msg = _('The customer/sales/pricelist type') . ' ' . $SelectedType . ' ' . _('has been updated');
48 } elseif ( $InputError !=1 ) {
50 // First check the type is not being duplicated
52 $checkSql = "SELECT count(*)
54 WHERE typeabbrev = '" . $_POST['TypeAbbrev'] . "'";
56 $checkresult = DB_query($checkSql,$db);
57 $checkrow = DB_fetch_row($checkresult);
59 if ( $checkrow[0] > 0 ) {
61 prnMsg( _('The customer/sales/pricelist type ') . $_POST['TypeAbbrev'] . _(' already exist.'),'error');
64 // Add new record on submit
66 $sql = "INSERT INTO salestypes
69 VALUES ('" . str_replace(' ', '', $_POST['TypeAbbrev']) . "',
70 '" . $_POST['Sales_Type'] . "')";
72 $msg = _('Customer/sales/pricelist type') . ' ' . $_POST["Sales_Type"] . ' ' . _('has been created');
77 if ( $InputError !=1) {
78 //run the SQL from either of the above possibilites
79 $result = DB_query($sql,$db);
81 prnMsg($msg,'success');
84 unset($_POST['TypeAbbrev']);
85 unset($_POST['Sales_Type']);
88 } elseif ( isset($_GET['delete']) ) {
90 // PREVENT DELETES IF DEPENDENT RECORDS IN 'DebtorTrans'
91 // Prevent delete if saletype exist in customer transactions
93 $sql= "SELECT COUNT(*)
95 WHERE debtortrans.tpe='$SelectedType'";
97 $ErrMsg = _('The number of transactions using this customer/sales/pricelist type could not be retrieved');
98 $result = DB_query($sql,$db,$ErrMsg);
100 $myrow = DB_fetch_row($result);
102 prnMsg(_('Cannot delete this sale type because customer transactions have been created using this sales type') . '<br>' . _('There are') . ' ' . $myrow[0] . ' ' . _('transactions using this sales type code'),'error');
106 $sql = "SELECT COUNT(*) FROM debtorsmaster WHERE salestype='$SelectedType'";
108 $ErrMsg = _('The number of transactions using this Sales Type record could not be retrieved because');
109 $result = DB_query($sql,$db,$ErrMsg);
110 $myrow = DB_fetch_row($result);
112 prnMsg (_('Cannot delete this sale type because customers are currently set up to use this sales type') . '<br>' . _('There are') . ' ' . $myrow[0] . ' ' . _('customers with this sales type code'));
115 $sql="DELETE FROM salestypes WHERE typeabbrev='$SelectedType'";
116 $ErrMsg = _('The Sales Type record could not be deleted because');
117 $result = DB_query($sql,$db,$ErrMsg);
118 prnMsg(_('Sales type') . ' / ' . _('price list') . ' ' . $SelectedType . ' ' . _('has been deleted') ,'success');
120 $sql ="DELETE FROM prices WHERE prices.typeabbrev='SelectedType'";
121 $ErrMsg = _('The Sales Type prices could not be deleted because');
122 $result = DB_query($sql,$db,$ErrMsg);
124 prnMsg(' ... ' . _('and any prices for this sales type / price list were also deleted'),'success');
125 unset ($SelectedType);
126 unset($_GET['delete']);
129 } //end if sales type used in debtor transactions or in customers set up
132 if (!isset($SelectedType)){
134 /* 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
135 then none of the above are true and the list of sales types will be displayed with
136 links to delete or edit each. These will call the same page again and allow update/input
137 or deletion of the records*/
139 $sql = 'SELECT * FROM salestypes';
140 $result = DB_query($sql,$db);
142 echo '<CENTER><TABLE BORDER=1>';
144 <TD CLASS='tableheader'>" . _('Type Code') . "</TD>
145 <TD CLASS='tableheader'>" . _('Type Name') . "</TD>
148 $k=0; //row colour counter
150 while ($myrow = DB_fetch_row($result)) {
152 echo "<TR BGCOLOR='#CCCCCC'>";
155 echo "<TR BGCOLOR='#EEEEEE'>";
161 <td><a href='%sSelectedType=%s'>" . _('Edit') . "</td>
162 <td><a href='%sSelectedType=%s&delete=yes' onclick=\"return confirm('" . _('Are you sure you wish to delete this price list and all the prices it may have set up?') . "');\">" . _('Delete') . "</td>
166 $_SERVER['PHP_SELF'] . '?' . SID
, $myrow[0],
167 $_SERVER['PHP_SELF'] . '?' . SID
, $myrow[0]);
169 //END WHILE LIST LOOP
170 echo '</table></CENTER>';
173 //end of ifs and buts!
174 if (isset($SelectedType)) {
176 echo '<CENTER><P><A HREF="' . $_SERVER['PHP_SELF'] . '?' . SID
. '">' . _('Show All Sales Types Defined') . '</A></CENTER><p>';
178 if (! isset($_GET['delete'])) {
180 echo "<FORM METHOD='post' action=" . $_SERVER['PHP_SELF'] . '?' . SID
. '>';
181 echo '<CENTER><FONT SIZE=4 COLOR=blue><B><U>' . _('Sales Type/Price List Setup') . '</B></U></FONT>';
182 echo '<P><TABLE BORDER=1>'; //Main table
183 echo '<TD><TABLE>'; // First column
186 // The user wish to EDIT an existing type
187 if ( isset($SelectedType) AND $SelectedType!='' )
190 $sql = "SELECT typeabbrev,
193 WHERE typeabbrev='$SelectedType'";
195 $result = DB_query($sql, $db);
196 $myrow = DB_fetch_array($result);
198 $_POST['TypeAbbrev'] = $myrow['typeabbrev'];
199 $_POST['Sales_Type'] = $myrow['sales_type'];
201 echo "<INPUT TYPE=HIDDEN NAME='SelectedType' VALUE=" . $SelectedType . ">";
202 echo "<INPUT TYPE=HIDDEN NAME='TypeAbbrev' VALUE=" . $_POST['TypeAbbrev'] . ">";
203 echo "<CENTER><TABLE> <TR><TD>" . _('Type Code') . ":</TD><TD>";
205 // We dont allow the user to change an existing type code
207 echo $_POST['TypeAbbrev'] . '</TD></TR>';
211 // This is a new type so the user may volunteer a type code
213 echo "<CENTER><TABLE><TR><TD>" . _('Type Code') . ":</TD><TD><INPUT TYPE='Text' SIZE=3 MAXLENGTH=2 name='TypeAbbrev'></TD></TR>";
217 echo "<TR><TD>" . _('Sales Type Name') . ":</TD><TD><input type='Text' name='Sales_Type' value='" . $_POST['Sales_Type'] . "'></TD></TR>";
219 echo '</TABLE>'; // close table in first column
220 echo '</TD></TR></TABLE>'; // close main table
222 echo '<P><INPUT TYPE=submit NAME=submit VALUE="' . _('Accept') . '"><INPUT TYPE=submit NAME=Cancel VALUE="' . _('Cancel') . '"></CENTER>';
226 } // end if user wish to delete
229 include('includes/footer.inc');