915835d81d509a0bb3c71135b1506347363837fa
[fa-stable.git] / gl / manage / gl_account_types.php
1 <?php
2
3 $page_security = 3;
4 $path_to_root="../..";
5 include($path_to_root . "/includes/session.inc");
6
7 page(_("GL Account Groups"));
8
9 include($path_to_root . "/gl/includes/gl_db.inc");
10
11 include($path_to_root . "/includes/ui.inc");
12
13 if (isset($_GET['selected_id']))
14 {
15         $selected_id = $_GET['selected_id'];
16
17 elseif(isset($_POST['selected_id']))
18 {
19         $selected_id = $_POST['selected_id'];
20 }
21 else
22         $selected_id = "";
23 //-----------------------------------------------------------------------------------
24
25 function can_process() 
26 {
27         global $selected_id;
28
29         if (strlen($_POST['name']) == 0) 
30         {
31                 display_error( _("The account group name cannot be empty."));
32                 return false;
33         }
34
35         if (isset($selected_id) && ($selected_id == $_POST['parent'])) 
36         {
37                 display_error(_("You cannot set an account group to be a subgroup of itself."));
38                 return false;
39         }
40
41         return true;
42 }
43
44 //-----------------------------------------------------------------------------------
45
46 if (isset($_POST['ADD_ITEM']) || isset($_POST['UPDATE_ITEM'])) 
47 {
48
49         if (can_process()) 
50         {
51
52         if ($selected_id != "") 
53         {
54
55                 update_account_type($selected_id, $_POST['name'], $_POST['class_id'], $_POST['parent']);
56
57         } 
58         else 
59         {
60
61                 add_account_type($_POST['name'], $_POST['class_id'], $_POST['parent']);
62         }
63                 meta_forward($_SERVER['PHP_SELF']);
64         }
65 }
66
67 //-----------------------------------------------------------------------------------
68
69 function can_delete($selected_id)
70 {
71         if ($selected_id == "")
72                 return false;
73         $sql= "SELECT COUNT(*) FROM ".TB_PREF."chart_master
74                 WHERE account_type=$selected_id";
75         $result = db_query($sql, "could not query chart master");
76         $myrow = db_fetch_row($result);
77         if ($myrow[0] > 0) 
78         {
79                 display_error(_("Cannot delete this account group because GL accounts have been created referring to it."));
80                 return false;
81         }
82
83         $sql= "SELECT COUNT(*) FROM ".TB_PREF."chart_types
84                 WHERE parent=$selected_id";
85         $result = db_query($sql, "could not query chart types");
86         $myrow = db_fetch_row($result);
87         if ($myrow[0] > 0) 
88         {
89                 display_error(_("Cannot delete this account group because GL account groups have been created referring to it."));
90                 return false;
91         }
92
93         return true;
94 }
95
96
97 //-----------------------------------------------------------------------------------
98
99 if (isset($_GET['delete'])) 
100 {
101
102         if (can_delete($selected_id))
103         {
104                 delete_account_type($selected_id);
105                 meta_forward($_SERVER['PHP_SELF']);
106         }
107 }
108
109 //-----------------------------------------------------------------------------------
110
111 $result = get_account_types();
112
113 start_table($table_style);
114 $th = array(_("Name"), _("Subgroup Of"), _("Class Type"), "", "");
115 table_header($th);
116
117 $k = 0;
118 while ($myrow = db_fetch($result)) 
119 {
120
121         alt_table_row_color($k);
122
123         $bs_text = get_account_class_name($myrow["class_id"]);
124
125         if ($myrow["parent"] == reserved_words::get_any_numeric()) 
126         {
127                 $parent_text = "";
128         } 
129         else 
130         {
131                 $parent_text = get_account_type_name($myrow["parent"]);
132         }
133
134         label_cell($myrow["name"]);
135         label_cell($parent_text);
136         label_cell($bs_text);
137         edit_link_cell("selected_id=" . $myrow["id"]);
138         delete_link_cell("selected_id=" . $myrow["id"]. "&delete=1");
139         end_row();
140 }
141
142 end_table();
143
144 //-----------------------------------------------------------------------------------
145
146 hyperlink_no_params($_SERVER['PHP_SELF'], _("New Account Group"));
147
148 start_form();
149
150 start_table($table_style2);
151
152 if ($selected_id != "") 
153 {
154         //editing an existing status code
155
156         $myrow = get_account_type($selected_id);
157
158         $_POST['name']  = $myrow["name"];
159         $_POST['parent']  = $myrow["parent"];
160         $_POST['class_id']  = $myrow["class_id"];
161
162         hidden('selected_id', $selected_id);
163 }
164
165 text_row_ex(_("Name:"), 'name', 50);
166
167 gl_account_types_list_row(_("Subgroup Of:"), 'parent', null, true, _("None"), true);
168
169 class_list_row(_("Class Type:"), 'class_id', null);
170
171 end_table(1);
172
173 submit_add_or_update_center($selected_id == "");
174
175 end_form();
176
177 //------------------------------------------------------------------------------------
178
179 end_page();
180
181 ?>