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 ***********************************************************************/
13 Update main or gl company setup.
15 function update_company_prefs( $params, $pref = TB_PREF )
17 $sql = "UPDATE {$pref}sys_prefs SET value = ";
18 foreach($params as $name => $value) {
19 if (!db_query($sql. db_escape($value). " WHERE name=".db_escape($name),
20 "The company prefferences could not be updated "))
22 // update cached value
23 $_SESSION['SysPrefs']->prefs[$name] = $value;
28 Get company preferences. Returns cached values from global variable SysPrefs
29 or retrieved from database if SysPrefs values are not set.
30 $prefs can be preference name, array of names, or null for all preferences.
33 function get_company_pref($prefs = null, $tbpref = TB_PREF)
35 global $SysPrefs, $core_version;
37 if (!isset($_SESSION['SysPrefs']->prefs)) { // cached preferences
39 $_SESSION['SysPrefs'] = new sys_prefs();
41 $sql = "SELECT name, value FROM {$tbpref}sys_prefs";
42 $result = @db_query($sql); // supress errors before 2.3 db structure upgrade
47 while($pref = db_fetch_assoc($result)) {
48 $_SESSION['SysPrefs']->prefs[$pref['name']] = $pref['value'];
51 $SysPrefs = &$_SESSION['SysPrefs'];
53 // update current db status for info in log file
54 $SysPrefs->db_ok = $SysPrefs->prefs['version_id'] == $core_version;
57 $all = $_SESSION['SysPrefs']->prefs;
61 elseif (is_string($prefs))
65 foreach($prefs as $name)
66 $ret[$name] = $all[$name];
71 function get_company_prefs($tbpref = TB_PREF)
73 return get_company_pref(null, $tbpref);
76 function get_base_sales_type()
78 return get_company_pref('base_sales');
81 function get_company_extensions($id = -1) {
84 $file = $path_to_root.($id == -1 ? '' : '/company/'.$id).'/installed_extensions.php';
85 $installed_extensions = array();
89 return $installed_extensions;
92 function add_payment_terms($daysOrFoll, $terms, $dayNumber)
96 $sql = "INSERT INTO ".TB_PREF."payment_terms (terms,
97 days_before_due, day_in_following_month)
99 db_escape($terms) . ", " . db_escape($dayNumber) . ", 0)";
103 $sql = "INSERT INTO ".TB_PREF."payment_terms (terms,
104 days_before_due, day_in_following_month)
105 VALUES (" . db_escape($terms) . ",
106 0, " . db_escape($dayNumber) . ")";
108 db_query($sql,"The payment term could not be added");
111 function update_payment_terms($selected_id, $daysOrFoll, $terms, $dayNumber)
115 $sql = "UPDATE ".TB_PREF."payment_terms SET terms=" . db_escape($terms) . ",
116 day_in_following_month=0,
117 days_before_due=" . db_escape($dayNumber) . "
118 WHERE terms_indicator = " .db_escape($selected_id);
122 $sql = "UPDATE ".TB_PREF."payment_terms SET terms=" . db_escape($terms) . ",
123 day_in_following_month=" . db_escape($dayNumber) . ",
125 WHERE terms_indicator = " .db_escape($selected_id);
127 db_query($sql,"The payment term could not be updated");
130 function delete_payment_terms($selected_id)
132 $sql="DELETE FROM ".TB_PREF."payment_terms WHERE terms_indicator=".db_escape($selected_id);
133 db_query($sql,"could not delete a payment terms");
136 function get_payment_terms($selected_id)
138 $sql = "SELECT *, (t.days_before_due=0) AND (t.day_in_following_month=0) as cash_sale
139 FROM ".TB_PREF."payment_terms t WHERE terms_indicator=".db_escape($selected_id);
141 $result = db_query($sql,"could not get payment term");
143 return db_fetch($result);
146 function get_payment_terms_all($show_inactive)
148 $sql = "SELECT * FROM ".TB_PREF."payment_terms";
149 if (!$show_inactive) $sql .= " WHERE !inactive";
150 return db_query($sql,"could not get payment terms");
153 function key_in_foreign_table($id, $table, $key, $escaped=false)
156 $id = db_escape($id);
157 $sql= "SELECT COUNT(*) FROM ".TB_PREF."$table WHERE $key = $id";
158 $result = db_query($sql,"check $table relations failed");
159 $myrow = db_fetch_row($result);
160 return ($myrow[0] > 0);