6 include('includes/session.inc');
8 $title = _('Account Sections');
10 include('includes/header.inc');
12 // SOME TEST TO ENSURE THAT AT LEAST INCOME AND COST OF SALES ARE THERE
13 $sql= "SELECT COUNT(*) FROM accountsection WHERE sectionid=1";
14 $result = DB_query($sql,$db);
15 $myrow = DB_fetch_row($result);
16 if( $myrow[0] == 0 ) {
17 $sql = "INSERT INTO accountsection (
24 $result = DB_query($sql,$db);
27 $sql= "SELECT COUNT(*) FROM accountsection WHERE sectionid=2";
28 $result = DB_query($sql,$db);
29 $myrow = DB_fetch_row($result);
30 if( $myrow[0] == 0 ) {
31 $sql = "INSERT INTO accountsection (
38 $result = DB_query($sql,$db);
40 // DONE WITH MINIMUM TESTS
43 if (isset($_POST['submit'])) {
45 //initialise no input errors assumed initially before we test
49 /* actions to take once the user has clicked the submit button
50 ie the page has called itself with some user input */
52 //first off validate inputs sensible
54 if (strpos($_POST['SectionName'],'&')>0 OR strpos($_POST['SectionName'],"'")>0) {
56 prnMsg( _('The account section name cannot contain the character') . " '&' " . _('or the character') ." '",'error');
57 } elseif (isset($_POST['SectionID']) && (!is_long((int) $_POST['SectionID']))) {
59 prnMsg( _('The section number must be an integer'),'error');
62 if ($_POST['SelectedSectionID']!='' AND $InputError !=1) {
64 /*SelectedSectionID could also exist if submit had not been clicked this code would not run in this case cos submit is false of course see the delete code below*/
66 $sql = "UPDATE accountsection
67 SET sectionname='" . $_POST['SectionName'] . "'
68 WHERE sectionid = " . $_POST['SelectedSectionID'];
70 $msg = _('Record Updated');
71 } elseif ($InputError !=1) {
73 /*SelectedSectionID is null cos no item selected on first time round so must be adding a record must be submitting new entries in the new account section form */
75 $sql = "INSERT INTO accountsection (
79 " . $_POST['SectionID'] . ",
80 '" . $_POST['SectionName'] ."'
82 $msg = _('Record inserted');
86 //run the SQL from either of the above possibilites
87 $result = DB_query($sql,$db);
88 prnMsg($msg,'success');
90 unset ($_POST['SelectedSectionID']);
91 unset ($_POST['SectionID']);
92 unset ($_POST['SectionName']);
94 } elseif (isset($_GET['delete'])) {
95 //the link to delete a selected record was clicked instead of the submit button
97 // PREVENT DELETES IF DEPENDENT RECORDS IN 'accountgroups'
98 $sql= "SELECT COUNT(*) FROM accountgroups WHERE sectioninaccounts='" . $_GET['SelectedSectionID'] . "'";
99 $result = DB_query($sql,$db);
100 $myrow = DB_fetch_row($result);
102 prnMsg( _('Cannot delete this account section because general ledger accounts groups have been created using this section'),'warn');
103 echo '<br>' . _('There are') . ' ' . $myrow[0] . ' ' . _('general ledger accounts groups that refer to this account section') . '</FONT>';
107 $sql="DELETE FROM accountsection WHERE sectionid='" . $_GET['SelectedSectionID'] . "'";
108 $result = DB_query($sql,$db);
109 prnMsg( $_GET['SectionName'] . ' ' . _('section has been deleted') . '!','success');
111 } //end if account group used in GL accounts
112 unset ($_GET['SelectedSectionID']);
113 unset($_GET['delete']);
114 unset ($_POST['SelectedSectionID']);
115 unset ($_POST['SectionID']);
116 unset ($_POST['SectionName']);
119 if (!isset($_GET['SelectedSectionID']) OR !isset($_POST['SelectedSectionID'])) {
121 /* An account section could be posted when one has been edited and is being updated
122 or GOT when selected for modification
123 SelectedSectionID will exist because it was sent with the page in a GET .
124 If its the first time the page has been displayed with no parameters
125 then none of the above are true and the list of account groups will be displayed with
126 links to delete or edit each. These will call the same page again and allow update/input
127 or deletion of the records*/
129 $sql = "SELECT sectionid,
134 $ErrMsg = _('Could not get account group sections because');
135 $result = DB_query($sql,$db,$ErrMsg);
137 echo "<center><table>
139 <td class='tableheader'>" . _('Section Number') . "</td>
140 <td class='tableheader'>" . _('Section Description') . "</td>
143 $k=0; //row colour counter
144 while ($myrow = DB_fetch_row($result)) {
147 echo "<tr bgcolor='#CCCCCC'>";
150 echo "<tr bgcolor='#EEEEEE'>";
154 echo '<TD>' . $myrow[0] . '</TD><TD>' . $myrow[1] . '</TD>';
155 echo '<TD><A HREF="' . $_SERVER['PHP_SELF'] . '?' . SID
. '&SelectedSectionID=' . $myrow[0] . '">' . _('Edit') . '</A></TD>';
156 if ( $myrow[0] == '1' ||
$myrow[0] == '2' ) {
157 echo '<TD><b>'._('Restricted').'</b></TD>';
159 echo '<TD><A HREF="' . $_SERVER['PHP_SELF'] . '?' . SID
. '&SelectedSectionID=' . $myrow[0] . '&delete=1">' . _('Delete') .'</A></TD>';
162 } //END WHILE LIST LOOP
163 echo '</table></CENTER><p>';
164 } //end of ifs and buts!
167 if (isset($_POST['SelectedSectionID']) OR isset($_GET['SelectedSectionID'])) {
168 echo '<CENTER><A HREF=' . $_SERVER['PHP_SELF'] . '?' . SID
.'>' . _('Review Account Sections') . '</a></Center>';
173 if (! isset($_GET['delete'])) {
175 echo "<FORM METHOD='post' action=" . $_SERVER['PHP_SELF'] . '?' . SID
. '>';
177 if (isset($_GET['SelectedSectionID'])) {
178 //editing an existing section
180 $sql = "SELECT sectionid,
183 WHERE sectionid='" . $_GET['SelectedSectionID'] ."'";
185 $result = DB_query($sql, $db);
186 if ( DB_num_rows($result) == 0 ) {
187 prnMsg( _('Could not retrieve the requested section please try again.'),'warn');
188 unset($_GET['SelectedSectionID']);
190 $myrow = DB_fetch_array($result);
192 $_POST['SectionID'] = $myrow['sectionid'];
193 $_POST['SectionName'] = $myrow['sectionname'];
195 echo "<INPUT TYPE=HIDDEN NAME='SelectedSectionID' VALUE='" . $_POST['SectionID'] . "'>";
196 echo "<CENTER><TABLE>
197 <TD>" . _('Section Number') . ':' . "</TD>
198 <TD>" . $_POST['SectionID'] . "</TD>";
203 if (!isset($_POST['SelectedSectionID'])){
204 $_POST['SelectedSectionID']='';
206 if (!isset($_POST['SectionID'])){
207 $_POST['SectionID']='';
209 $_POST['SectionName']='';
210 echo "<CENTER><TABLE>
212 <TD>" . _('Section Number') . ':' . "</TD>
213 <TD><input type='Text' name='SectionID' SIZE=4 MAXLENGTH=4 value='" . $_POST['SectionID'] . "'></TD></TR>";
215 echo "<TR><TD>" . _('Section Description') . ':' . "</TD>
216 <TD><input type='Text' name='SectionName' SIZE=30 MAXLENGTH=30 value='" . $_POST['SectionName'] . "'></TD>
220 echo '<CENTER><input type=Submit name=submit value=' . _('Enter Information') . '>';
224 } //end if record deleted no point displaying form to add record
226 include('includes/footer.inc');