Extended account id's to 15 chars. Changed account type to varchar(10)
[fa-stable.git] / gl / includes / db / gl_db_account_types.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 function add_account_type($id, $name, $class_id, $parent)
13 {
14         $sql = "INSERT INTO ".TB_PREF."chart_types (id, name, class_id, parent)
15                 VALUES ($id, ".db_escape($name).", ".db_escape($class_id).", ".db_escape($parent).")";
16
17         return db_query($sql);
18 }
19
20 function update_account_type($id, $name, $class_id, $parent, $old_id)
21 {
22         begin_transaction();
23         if ($old_id != $id)
24         {
25                 $sql = "SELECT id FROM ".TB_PREF."chart_types WHERE parent = ".db_escape($old_id);
26
27                 $result = db_query($sql, "could not get account type");
28
29                 while ($myrow = db_fetch($result))
30                 {
31                 $sql = "UPDATE ".TB_PREF."chart_types SET parent=".db_escape($id)
32                                 ." WHERE id = '".$myrow['id']."'";
33                         db_query($sql, "could not update account type");
34                 }
35                 $sql = "SELECT account_code FROM ".TB_PREF."chart_master WHERE account_type = ".db_escape($old_id);
36
37                 $result = db_query($sql, "could not get account");
38
39                 while ($myrow = db_fetch($result))
40                 {
41                 $sql = "UPDATE ".TB_PREF."chart_master SET account_type=".db_escape($id)
42                                 ." WHERE account_code = '".$myrow['account_code']."'";
43                         db_query($sql, "could not update account");
44                 }
45         }
46     $sql = "UPDATE ".TB_PREF."chart_types SET id=".db_escape($id) .", name=".db_escape($name).",
47                 class_id=".db_escape($class_id).", parent=".db_escape($parent)
48                 ." WHERE id = ".db_escape($old_id);
49
50         $ret = db_query($sql, "could not update account type");
51         commit_transaction();
52         return $ret;
53 }
54
55 function get_account_types($all=false)
56 {
57         $sql = "SELECT * FROM ".TB_PREF."chart_types";
58
59         if (!$all) $sql .= " WHERE !inactive";
60         $sql .= " ORDER BY class_id, id";
61
62         return db_query($sql, "could not get account types");
63 }
64
65 function get_account_type($id)
66 {
67         $sql = "SELECT * FROM ".TB_PREF."chart_types WHERE id = ".db_escape($id);
68
69         $result = db_query($sql, "could not get account type");
70
71         return db_fetch($result);
72 }
73
74 function get_account_type_name($id)
75 {
76         $sql = "SELECT name FROM ".TB_PREF."chart_types WHERE id = ".db_escape($id);
77
78         $result = db_query($sql, "could not get account type");
79
80         $row = db_fetch_row($result);
81         return $row[0];
82 }
83
84 function delete_account_type($id)
85 {
86         $sql = "DELETE FROM ".TB_PREF."chart_types WHERE id = ".db_escape($id);
87
88         db_query($sql, "could not delete account type");
89 }
90
91 function add_account_class($id, $name, $ctype)
92 {
93         $sql = "INSERT INTO ".TB_PREF."chart_class (cid, class_name, ctype)
94                 VALUES (".db_escape($id).", ".db_escape($name).", ".db_escape($ctype).")";
95
96         return db_query($sql);
97 }
98
99 function update_account_class($id, $name, $ctype)
100 {
101     $sql = "UPDATE ".TB_PREF."chart_class SET class_name=".db_escape($name).",
102                 ctype=".db_escape($ctype)." WHERE cid = ".db_escape($id);
103
104         return db_query($sql);
105 }
106
107 function get_account_classes($all=false)
108 {
109         $sql = "SELECT * FROM ".TB_PREF."chart_class";
110         if (!$all) $sql .= " WHERE !inactive";
111         $sql .= " ORDER BY cid";
112
113         return db_query($sql, "could not get account types");
114 }
115
116 function get_account_class($id)
117 {
118         $sql = "SELECT * FROM ".TB_PREF."chart_class WHERE cid = ".db_escape($id);
119
120         $result = db_query($sql, "could not get account type");
121
122         return db_fetch($result);
123 }
124
125 function get_account_class_name($id)
126 {
127         $sql = "SELECT class_name FROM ".TB_PREF."chart_class WHERE cid =".db_escape($id);
128
129         $result = db_query($sql, "could not get account type");
130
131         $row = db_fetch_row($result);
132         return $row[0];
133 }
134
135 function delete_account_class($id)
136 {
137         $sql = "DELETE FROM ".TB_PREF."chart_class WHERE cid = ".db_escape($id);
138
139         db_query($sql, "could not delete account type");
140 }
141
142 ?>