2 /* $Revision: 1.16 $ */
5 include('includes/session.inc');
6 $title = _('Chart of Accounts Maintenance');
8 include('includes/header.inc');
10 if (isset($_POST['SelectedAccount'])){
11 $SelectedAccount = $_POST['SelectedAccount'];
12 } elseif (isset($_GET['SelectedAccount'])){
13 $SelectedAccount = $_GET['SelectedAccount'];
16 if (isset($_POST['submit'])) {
18 //initialise no input errors assumed initially before we test
21 /* actions to take once the user has clicked the submit button
22 ie the page has called itself with some user input */
24 //first off validate inputs sensible
26 if (!is_long((integer)$_POST['AccountCode'])) {
28 prnMsg(_('The account code must be an integer'),'warn');
29 } elseif (strlen($_POST['AccountName']) >50) {
31 prnMsg( _('The account name must be fifty characters or less long'),'warn');
34 if ($SelectedAccount AND $InputError !=1) {
36 $sql = "UPDATE chartmaster SET accountname='" . DB_escape_string($_POST['AccountName']) . "',
37 group_='" . $_POST['Group'] . "'
38 WHERE accountcode = $SelectedAccount";
40 $ErrMsg = _('Could not update the account because');
41 $result = DB_query($sql,$db,$ErrMsg);
42 prnMsg (_('The general ledger account has been updated'),'success');
43 } elseif ($InputError !=1) {
45 /*SelectedAccount is null cos no item selected on first time round so must be adding a record must be submitting new entries */
47 $ErrMsg = _('Could not add the new account code');
48 $sql = 'INSERT INTO chartmaster (accountcode,
51 VALUES (' . DB_escape_string($_POST['AccountCode']) . ",
52 '" . DB_escape_string($_POST['AccountName']) . "',
53 '" . $_POST['Group'] . "')";
54 $result = DB_query($sql,$db,$ErrMsg);
56 /*Add the new chart details records for existing periods first */
58 $ErrMsg = _('Could not add the chart details for the new account');
60 $sql = 'INSERT INTO chartdetails (accountcode,
62 SELECT ' . $_POST['AccountCode'] . ',
65 $result = DB_query($sql,$db,$ErrMsg);
67 prnMsg(_('The new general ledger account has been added'),'success');
70 unset ($_POST['Group']);
71 unset ($_POST['AccountCode']);
72 unset ($_POST['AccountName']);
73 unset($SelectedAccount);
75 } elseif (isset($_GET['delete'])) {
76 //the link to delete a selected record was clicked instead of the submit button
78 // PREVENT DELETES IF DEPENDENT RECORDS IN 'ChartDetails'
82 $sql= "SELECT COUNT(*) FROM chartdetails WHERE chartdetails.accountcode = $SelectedAccount AND chartdetails.actual <>0";
83 $result = DB_query($sql,$db);
84 $myrow = DB_fetch_row($result);
87 prnMsg(_('Cannot delete this account because chart details have been created using this account and at least one period has postings to it'),'warn');
88 echo '<br>' . _('There are') . ' ' . $myrow[0] . ' ' . _('chart details that require this account code');
91 // PREVENT DELETES IF DEPENDENT RECORDS IN 'GLTrans'
92 $sql= "SELECT COUNT(*) FROM gltrans WHERE gltrans.account = $SelectedAccount";
94 $ErrMsg = _('Could not test for existing transactions because');
96 $result = DB_query($sql,$db,$ErrMsg);
98 $myrow = DB_fetch_row($result);
101 prnMsg( _('Cannot delete this account because transactions have been created using this account'),'warn');
102 echo '<BR>' . _('There are') . ' ' . $myrow[0] . ' ' . _('transactions that require this account code');
105 //PREVENT DELETES IF Company default accounts set up to this account
106 $sql= "SELECT COUNT(*) FROM companies
107 WHERE debtorsact=$SelectedAccount
108 OR pytdiscountact=$SelectedAccount
109 OR creditorsact=$SelectedAccount
110 OR payrollact=$SelectedAccount
111 OR grnact=$SelectedAccount
112 OR exchangediffact=$SelectedAccount
113 OR purchasesexchangediffact=$SelectedAccount
114 OR retainedearnings=$SelectedAccount";
117 $ErrMsg = _('Could not test for default company GL codes because');
119 $result = DB_query($sql,$db,$ErrMsg);
121 $myrow = DB_fetch_row($result);
124 prnMsg( _('Cannot delete this account because it is used as one of the company default accounts'),'warn');
127 //PREVENT DELETES IF Company default accounts set up to this account
128 $sql= "SELECT COUNT(*) FROM taxauthorities
129 WHERE taxglcode=$SelectedAccount
130 OR purchtaxglaccount =$SelectedAccount";
132 $ErrMsg = _('Could not test for tax authority GL codes because');
133 $result = DB_query($sql,$db,$ErrMsg);
135 $myrow = DB_fetch_row($result);
138 prnMsg( _('Cannot delete this account because it is used as one of the tax authority accounts'),'warn');
140 //PREVENT DELETES IF SALES POSTINGS USE THE GL ACCOUNT
141 $sql= "SELECT COUNT(*) FROM salesglpostings
142 WHERE salesglcode=$SelectedAccount
143 OR discountglcode=$SelectedAccount";
145 $ErrMsg = _('Could not test for existing sales interface GL codes because');
147 $result = DB_query($sql,$db,$ErrMsg);
149 $myrow = DB_fetch_row($result);
152 prnMsg( _('Cannot delete this account because it is used by one of the sales GL posting interface records'),'warn');
154 //PREVENT DELETES IF COGS POSTINGS USE THE GL ACCOUNT
155 $sql= "SELECT COUNT(*) FROM cogsglpostings WHERE glcode=$SelectedAccount";
157 $ErrMsg = _('Could not test for existing cost of sales interface codes because');
159 $result = DB_query($sql,$db,$ErrMsg);
161 $myrow = DB_fetch_row($result);
164 prnMsg(_('Cannot delete this account because it is used by one of the cost of sales GL posting interface records'),'warn');
167 //PREVENT DELETES IF STOCK POSTINGS USE THE GL ACCOUNT
168 $sql= "SELECT COUNT(*) FROM stockcategory
169 WHERE stockact=$SelectedAccount
170 OR adjglact=$SelectedAccount
171 OR purchpricevaract=$SelectedAccount
172 OR materialuseagevarac=$SelectedAccount
173 OR wipact=$SelectedAccount";
175 $Errmsg = _('Could not test for existing stock GL codes because');
177 $result = DB_query($sql,$db,$ErrMsg);
179 $myrow = DB_fetch_row($result);
182 prnMsg( _('Cannot delete this account because it is used by one of the stock GL posting interface records'),'warn');
184 //PREVENT DELETES IF STOCK POSTINGS USE THE GL ACCOUNT
185 $sql= "SELECT COUNT(*) FROM bankaccounts
186 WHERE accountcode=$SelectedAccount";
187 $ErrMsg = _('Could not test for existing bank account GL codes because');
189 $result = DB_query($sql,$db,$ErrMsg);
191 $myrow = DB_fetch_row($result);
194 prnMsg( _('Cannot delete this account because it is used by one the defined bank accounts'),'warn');
197 $sql = 'DELETE FROM chartdetails WHERE accountcode=' . $SelectedAccount;
198 $result = DB_query($sql,$db);
199 $sql="DELETE FROM chartmaster WHERE accountcode= $SelectedAccount";
200 $result = DB_query($sql,$db);
201 prnMsg( _('Account') . ' ' . $SelectedAccount . ' ' . _('has been deleted'),'succes');
212 if (!isset($_GET['delete'])) {
214 echo "<FORM METHOD='post' action='" . $_SERVER['PHP_SELF'] . '?' . SID
. "'>";
216 if ($SelectedAccount) {
217 //editing an existing account
219 $sql = "SELECT accountcode, accountname, group_ FROM chartmaster WHERE accountcode=$SelectedAccount";
221 $result = DB_query($sql, $db);
222 $myrow = DB_fetch_array($result);
224 $_POST['AccountCode'] = $myrow['accountcode'];
225 $_POST['AccountName'] = $myrow['accountname'];
226 $_POST['Group'] = $myrow['group_'];
228 echo "<INPUT TYPE=HIDDEN NAME='SelectedAccount' VALUE=$SelectedAccount>";
229 echo "<INPUT TYPE=HIDDEN NAME='AccountCode' VALUE=" . $_POST['AccountCode'] .">";
230 echo "<CENTER><TABLE><TR><TD>" . _('Account Code') . ":</TD><TD>" . $_POST['AccountCode'] . "</TD></TR>";
232 echo "<CENTER><TABLE>";
233 echo "<TR><TD>" . _('Account Code') . ":</TD><TD><INPUT TYPE=TEXT NAME='AccountCode' SIZE=11 MAXLENGTH=10></TD></TR>";
237 echo '<TR><TD>' . _('Account Name') . ":</TD><TD><input type='Text' SIZE=51 MAXLENGTH=50 name='AccountName' value='" . $_POST['AccountName'] . "'></TD></TR>";
239 $sql = 'SELECT groupname FROM accountgroups ORDER BY sequenceintb';
240 $result = DB_query($sql, $db);
242 echo '<TR><TD>' . _('Account Group') . ':</TD><TD><SELECT NAME=Group>';
244 while ($myrow = DB_fetch_array($result)){
245 if ($myrow[0]==$_POST['Group']){
246 echo "<OPTION SELECTED VALUE='";
248 echo "<OPTION VALUE='";
250 echo $myrow[0] . "'>" . $myrow[0];
259 <CENTER
><input type
="Submit" name
="submit" value
="<?php echo _('Enter Information'); ?>"></CENTER
>
263 <?php
} //end if record deleted no point displaying form to add record
266 if (!isset($SelectedAccount)) {
267 /* It could still be the second time the page has been run and a record has been selected for modification - SelectedAccount will exist because it was sent with the new call. If its the first time the page has been displayed with no parameters
268 then none of the above are true and the list of ChartMaster will be displayed with
269 links to delete or edit each. These will call the same page again and allow update/input
270 or deletion of the records*/
272 $sql = "SELECT accountcode,
275 CASE WHEN pandl=0 THEN '" . _('Balance Sheet') . "' ELSE '" . _('Profit/Loss') . "' END AS acttype
278 WHERE chartmaster.group_=accountgroups.groupname
279 ORDER BY chartmaster.accountcode";
281 $ErrMsg = _('The chart accounts could not be retrieved because');
283 $result = DB_query($sql,$db,$ErrMsg);
285 echo '<CENTER><table border=1>';
287 <td class='tableheader'>" . _('Account Code') . "</td>
288 <td class='tableheader'>" . _('Account Name') . "</td>
289 <td class='tableheader'>" . _('Account Group') . "</td>
290 <td class='tableheader'>" . _('P/L or B/S') . "</td>
293 $k=0; //row colour counter
295 while ($myrow = DB_fetch_row($result)) {
297 echo "<tr bgcolor='#CCCCCC'>";
300 echo "<tr bgcolor='#EEEEEE'>";
309 <td><a href=\"%s&SelectedAccount=%s\">" . _('Edit') . "</td>
310 <td><a href=\"%s&SelectedAccount=%s&delete=1\" onclick=\"return confirm('" . _('Are you sure you wish to delete this account? Additional checks will be performed in any event to ensure data integrity is not compromised.') . "');\">" . _('Delete') . "</td>
316 $_SERVER['PHP_SELF'] . '?' . SID
,
318 $_SERVER['PHP_SELF'] . '?' . SID
,
322 //END WHILE LIST LOOP
323 echo '</CENTER></TABLE>';
324 } //END IF SELECTED ACCOUNT
326 //end of ifs and buts!
330 if (isset($SelectedAccount)) {
331 echo "<Center><a href='" . $_SERVER['PHP_SELF'] . '?' . SID
."'>" . _('Show All Accounts') . '</a></CENTER>';
336 include('includes/footer.inc');