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)
17 begin_transaction(__FUNCTION__, func_get_args());
19 $sql = "UPDATE ".TB_PREF."sys_prefs SET value = ";
20 foreach($params as $name => $value) {
21 if (!db_query($sql. db_escape($value). " WHERE name=".db_escape($name),
22 "The company prefferences could not be updated "))
24 // update cached value
25 $_SESSION['SysPrefs']->prefs[$name] = $value;
32 Get company preferences. Returns cached values from global variable SysPrefs
33 or retrieved from database if SysPrefs values are not set.
34 $prefs can be preference name, array of names, or null for all preferences.
37 function get_company_pref($prefs = null)
39 global $SysPrefs, $db_version;
41 if (!isset($SysPrefs->prefs)) // just after first login or reset
44 $all = $SysPrefs->prefs;
46 if ($prefs && is_string($prefs))
55 function get_company_prefs()
57 return get_company_pref(null);
60 function set_company_pref($pref, $category, $type, $length, $value)
62 begin_transaction(__FUNCTION__, func_get_args());
64 $sql = "REPLACE `".TB_PREF."sys_prefs` SET `name`=".db_escape($pref).", `category`=".db_escape($category)
65 .", `type`=".db_escape($type).", `length`=".db_escape($length).", `value`=".db_escape($value);
66 $result = db_query($sql, "cannot set company pref");
73 function get_base_sales_type()
75 return get_company_pref('base_sales');
78 function get_company_extensions($id = -1) {
81 $file = $path_to_root.($id == -1 ? '' : '/company/'.(int)$id).'/installed_extensions.php';
82 $installed_extensions = array();
86 return $installed_extensions;
89 function add_payment_terms($terms, $type, $days=0, $early_discount=0, $early_days=0)
91 begin_transaction(__FUNCTION__, func_get_args());
93 $sql = "INSERT INTO ".TB_PREF."payment_terms (terms, type, days, early_discount, early_days)
94 VALUES (" .db_escape($terms) . "," . db_escape($type) . ", ".db_escape($days). ", ".db_escape($early_discount). ", ".db_escape($early_days).")";
96 db_query($sql,"The payment term could not be added");
101 function update_payment_terms($selected_id, $terms, $type, $days=0, $early_discount=0, $early_days=0)
103 begin_transaction(__FUNCTION__, func_get_args());
105 $sql = "UPDATE ".TB_PREF."payment_terms SET terms=" . db_escape($terms) . ",
106 type=".db_escape($type).",
107 days=" . db_escape($days).",
108 early_discount=".db_escape($early_discount).",
109 early_days=".db_escape($early_days).
110 " WHERE id = " .db_escape($selected_id);
112 db_query($sql,"The payment term could not be updated");
114 commit_transaction();
117 function delete_payment_terms($selected_id)
119 begin_transaction(__FUNCTION__, func_get_args());
121 $sql="DELETE FROM ".TB_PREF."payment_terms WHERE id=".db_escape($selected_id);
122 db_query($sql,"could not delete a payment terms");
124 commit_transaction();
127 function get_payment_terms($selected_id)
129 $sql = "SELECT * FROM ".TB_PREF."payment_terms t WHERE id=".db_escape($selected_id);
131 $result = db_query($sql,"could not get payment term");
133 return db_fetch($result);
136 function get_payment_terms_all($show_inactive)
138 $sql = "SELECT * FROM ".TB_PREF."payment_terms";
139 if (!$show_inactive) $sql .= " WHERE !inactive";
140 return db_query($sql,"could not get payment terms");
143 Return number of records in tables, where some foreign key $id is used.
144 $id - searched key value
145 $tables - array of table names (without prefix); when table name is used as a key, then
146 value is name of foreign key field. For numeric keys $stdkey field name is used.
147 $stdkey - standard name of foreign key.
149 function key_in_foreign_table($id, $tables, $stdkey)
152 if (!is_array($tables))
153 $tables = array($tables);
156 foreach ($tables as $tbl => $key) {
157 if (is_numeric($tbl)) {
161 $sqls[] = "(SELECT COUNT(*) as cnt FROM `".TB_PREF."$tbl` WHERE `$key`=".db_escape($id).")\n";
164 $sql = "SELECT sum(cnt) FROM (". implode(' UNION ', $sqls).") as counts";
166 $result = db_query($sql, "check relations for ".implode(',',$tables)." failed");
167 $count = db_fetch($result);
172 //---------------------------------------------------------------------------------------------
174 // Resets $theme references in users records to 'default'.
176 function clean_user_themes($theme)
178 global $db_connections, $db;
180 begin_transaction(__FUNCTION__, func_get_args());
182 $comp = user_company();
184 $connections = $db_connections; // do not use db_connections directly here, or script will hang due to db_connections usage inside the loop
185 foreach ($connections as $n => $conn) {
186 $db = $_SESSION["wa_current_user"]->set_db_connection($n);
187 $sql = "UPDATE {$conn['tbpref']}users SET theme='default' WHERE theme='$theme'";
188 if (!db_query($sql, 'Cannot update user theme settings'))
191 $db = $_SESSION["wa_current_user"]->set_db_connection($comp);
192 $_SESSION['wa_current_user']->prefs->theme = 'default';
194 commit_transaction();