Payment terms related functions moved to separate file, common function for calculati...
[fa-stable.git] / admin / db / company_db.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 include_once($path_to_root. "/admin/db/payment_terms_db.inc");
13
14 /*
15         Update main or gl company setup.
16 */
17 function update_company_prefs($params)
18 {
19         begin_transaction(__FUNCTION__, func_get_args());
20
21         $sql = "UPDATE ".TB_PREF."sys_prefs SET value = ";
22         foreach($params as $name => $value) {
23                 if (!db_query($sql. db_escape($value). " WHERE name=".db_escape($name),
24                          "The company prefferences could not be updated "))
25                         return false;
26                 // update cached value
27                 $_SESSION['SysPrefs']->prefs[$name] = $value;
28         }
29
30         commit_transaction();
31         return true;
32 }
33 /*
34         Get company preferences. Returns cached values from global variable SysPrefs
35         or retrieved from database if SysPrefs values are not set.
36         $prefs can be preference name, array of names, or null for all preferences.
37         
38 */
39 function get_company_pref($prefs = null)
40 {
41         global $SysPrefs, $db_version;
42
43         if (!isset($SysPrefs->prefs))    // just after first login or reset
44                 $SysPrefs->refresh();
45
46         $all = $SysPrefs->prefs;
47
48         if ($prefs && is_string($prefs))
49                 return @$all[$prefs];
50
51         if (!is_array($all))
52                 $all = array();
53
54         return $all;
55 }
56
57 function get_company_prefs()
58 {
59         return get_company_pref(null);
60 }
61
62 function set_company_pref($pref, $category, $type, $length, $value)
63 {
64         begin_transaction(__FUNCTION__, func_get_args());
65
66         $sql = "REPLACE `".TB_PREF."sys_prefs` SET `name`=".db_escape($pref).", `category`=".db_escape($category)
67                 .", `type`=".db_escape($type).", `length`=".db_escape($length).", `value`=".db_escape($value);
68         $result = db_query($sql, "cannot set company pref");
69
70         commit_transaction();
71         return $result;
72
73 }
74
75 function get_base_sales_type()
76 {
77         return get_company_pref('base_sales');
78 }
79
80 function get_company_extensions($id = -1) {
81         global $path_to_root;
82
83         $file = $path_to_root.($id == -1 ? '' : '/company/'.(int)$id).'/installed_extensions.php';
84         $installed_extensions = array();
85         if (is_file($file)) {
86                 include($file);
87         }
88         return $installed_extensions;
89 }
90
91 //---------------------------------------------------------------------------------------------
92 //
93 // Resets $theme references in users records to 'default'.
94 //
95 function clean_user_themes($theme)
96 {
97         global $db_connections, $db;
98
99         begin_transaction(__FUNCTION__, func_get_args());
100
101         $comp = user_company();
102
103         $connections = $db_connections; // do not use db_connections directly here, or script will hang due to db_connections usage inside the loop
104         foreach ($connections as $n => $conn) {
105                 $db = $_SESSION["wa_current_user"]->set_db_connection($n);
106                 $sql = "UPDATE {$conn['tbpref']}users SET theme='default' WHERE theme='$theme'";
107                 if (!db_query($sql, 'Cannot update user theme settings'))
108                         return false;
109         }
110         $db = $_SESSION["wa_current_user"]->set_db_connection($comp);
111         $_SESSION['wa_current_user']->prefs->theme = 'default';
112
113         commit_transaction();
114         return true;
115 }