380d5b442e1cca62585f39c0566c975ddb365da6
[fa-stable.git] / gl / includes / db / gl_db_accounts.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_gl_account($account_code, $account_name, $account_type, $account_code2)
13 {
14         $account_name = db_escape($account_name);
15         $sql = "INSERT INTO ".TB_PREF."chart_master (account_code, account_code2, account_name, account_type)
16                 VALUES (".db_escape($account_code).", ".db_escape($account_code2).", $account_name, $account_type)";
17
18         db_query($sql, "could not add gl account");
19 }
20
21 function update_gl_account($account_code, $account_name, $account_type, $account_code2)
22 {
23         $account_name = db_escape($account_name);
24     $sql = "UPDATE ".TB_PREF."chart_master SET account_name=$account_name,
25                 account_type=$account_type, account_code2=".db_escape($account_code2)
26                 ." WHERE account_code = '$account_code'";
27
28         db_query($sql, "could not update gl account");
29 }
30
31 function delete_gl_account($code)
32 {
33         $sql = "DELETE FROM ".TB_PREF."chart_master WHERE account_code='$code'";
34
35         db_query($sql, "could not delete gl account");
36 }
37
38 function get_gl_accounts($from=null, $to=null)
39 {
40         $sql = "SELECT ".TB_PREF."chart_master.*,".TB_PREF."chart_types.name AS AccountTypeName
41                 FROM ".TB_PREF."chart_master,".TB_PREF."chart_types
42                 WHERE ".TB_PREF."chart_master.account_type=".TB_PREF."chart_types.id";
43         if ($from != null)
44                 $sql .= " AND ".TB_PREF."chart_master.account_code >= '$from'";
45         if ($to != null)
46                 $sql .= " AND ".TB_PREF."chart_master.account_code <= '$to'";
47         $sql .= " ORDER BY account_code";
48
49         return db_query($sql, "could not get gl accounts");
50 }
51
52 function get_gl_accounts_in_type($type)
53 {
54         $sql = "SELECT * FROM ".TB_PREF."chart_master WHERE account_type=$type ORDER BY account_code";
55
56         return db_query($sql, "could not get gl accounts");
57 }
58
59 function num_accounts_in_type($type, $parent)
60 {
61         $sql = "SELECT COUNT(*) FROM ".TB_PREF."chart_master WHERE account_type=$type OR account_type=$parent";
62
63         $result = db_query($sql, "could not get gl accounts");
64         $row = db_fetch_row($result);
65         return $row[0];
66 }
67
68 function get_gl_account($code)
69 {
70         $sql = "SELECT * FROM ".TB_PREF."chart_master WHERE account_code='$code'";
71
72         $result = db_query($sql, "could not get gl account");
73         return db_fetch($result);
74 }
75
76 function is_account_balancesheet($code)
77 {
78         $sql = "SELECT ".TB_PREF."chart_class.balance_sheet FROM ".TB_PREF."chart_class, ".TB_PREF."chart_types, ".TB_PREF."chart_master
79                 WHERE ".TB_PREF."chart_master.account_type=".TB_PREF."chart_types.id AND
80                 ".TB_PREF."chart_types.class_id=".TB_PREF."chart_class.cid
81                 AND ".TB_PREF."chart_master.account_code='$code'";
82
83         $result = db_query($sql,"could not retreive the account class for $code");
84         $row = db_fetch_row($result);
85         return $row[0];
86 }
87
88 function get_gl_account_name($code)
89 {
90         $sql = "SELECT account_name from ".TB_PREF."chart_master WHERE account_code='$code'";
91
92         $result = db_query($sql,"could not retreive the account name for $code");
93
94         if (db_num_rows($result) == 1)
95         {
96                 $row = db_fetch_row($result);
97                 return $row[0];
98         }
99
100         display_db_error("could not retreive the account name for $code", $sql, true);
101 }
102
103
104 ?>