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)
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)";
18 db_query($sql, "could not add gl account");
21 function update_gl_account($account_code, $account_name, $account_type, $account_code2)
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'";
28 db_query($sql, "could not update gl account");
31 function delete_gl_account($code)
33 $sql = "DELETE FROM ".TB_PREF."chart_master WHERE account_code='$code'";
35 db_query($sql, "could not delete gl account");
38 function get_gl_accounts($from=null, $to=null)
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";
44 $sql .= " AND ".TB_PREF."chart_master.account_code >= '$from'";
46 $sql .= " AND ".TB_PREF."chart_master.account_code <= '$to'";
47 $sql .= " ORDER BY account_code";
49 return db_query($sql, "could not get gl accounts");
52 function get_gl_accounts_all($balance=-1)
55 $where ="WHERE ctype>0 AND ctype<".CL_INCOME;
56 elseif ($balance == 0)
57 $where ="WHERE ctype>".CL_EQUITY." OR ctype=0"; // backwards compatibility
58 $sql = "SELECT ".TB_PREF."chart_master.account_code, ".TB_PREF."chart_master.account_name, ".TB_PREF."chart_types.name AS AccountTypeName,".TB_PREF."chart_types.id AS AccountType,
59 ".TB_PREF."chart_types.parent, ".TB_PREF."chart_class.class_name AS AccountClassName, ".TB_PREF."chart_class.cid AS ClassID,
60 ".TB_PREF."chart_class.ctype AS ClassType
61 FROM ".TB_PREF."chart_types INNER JOIN ".TB_PREF."chart_class ON ".TB_PREF."chart_types.class_id=".TB_PREF."chart_class.cid
62 LEFT JOIN ".TB_PREF."chart_master ON ".TB_PREF."chart_master.account_type=".TB_PREF."chart_types.id ";
65 $sql .= " ORDER BY ".TB_PREF."chart_class.cid, IF(parent > 0,parent,".TB_PREF."chart_types.id),
66 IF(parent > 0,".TB_PREF."chart_types.id, parent), ".TB_PREF."chart_master.account_code";
68 return db_query($sql, "could not get gl accounts");
71 function get_gl_account($code)
73 $sql = "SELECT * FROM ".TB_PREF."chart_master WHERE account_code='$code'";
75 $result = db_query($sql, "could not get gl account");
76 return db_fetch($result);
79 function is_account_balancesheet($code)
81 $sql = "SELECT ".TB_PREF."chart_class.ctype FROM ".TB_PREF."chart_class, ".TB_PREF."chart_types, ".TB_PREF."chart_master
82 WHERE ".TB_PREF."chart_master.account_type=".TB_PREF."chart_types.id AND
83 ".TB_PREF."chart_types.class_id=".TB_PREF."chart_class.cid
84 AND ".TB_PREF."chart_master.account_code='$code'";
86 $result = db_query($sql,"could not retreive the account class for $code");
87 $row = db_fetch_row($result);
88 return $row[0] > 0 && $row[0] < CL_INCOME;
91 function get_gl_account_name($code)
93 $sql = "SELECT account_name from ".TB_PREF."chart_master WHERE account_code='$code'";
95 $result = db_query($sql,"could not retreive the account name for $code");
97 if (db_num_rows($result) == 1)
99 $row = db_fetch_row($result);
103 display_db_error("could not retreive the account name for $code", $sql, true);