Optimised get_company_pref()
[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 /*
13         Update main or gl company setup.
14 */
15 function update_company_prefs($params)
16 {
17         $sql = "UPDATE ".TB_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 "))
21                         return false;
22                 // update cached value
23                 $_SESSION['SysPrefs']->prefs[$name] = $value;
24         }
25         return true;
26 }
27 /*
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.
31         
32 */
33 function get_company_pref($prefs = null)
34 {
35         global $SysPrefs, $db_version;
36
37         if (!isset($SysPrefs->prefs))    // just after first login or reset
38                 $SysPrefs->refresh();
39
40         $all = $SysPrefs->prefs;
41
42         if ($prefs && is_string($prefs))
43                 return @$all[$prefs];
44
45         if (!is_array($all))
46                 $all = array();
47
48         return $all;
49 }
50
51 function get_company_prefs()
52 {
53         return get_company_pref(null);
54 }
55
56 function set_company_pref($pref, $category, $type, $length, $value)
57 {
58         $sql = "REPLACE `".TB_PREF."sys_prefs` SET `name`=".db_escape($pref).", `category`=".db_escape($category)
59                 .", `type`=".db_escape($type).", `length`=".db_escape($length).", `value`=".db_escape($value);
60         return db_query($sql, "cannot set company pref");
61 }
62
63 function get_base_sales_type()
64 {
65         return get_company_pref('base_sales');
66 }
67
68 function get_company_extensions($id = -1) {
69         global $path_to_root;
70
71         $file = $path_to_root.($id == -1 ? '' : '/company/'.(int)$id).'/installed_extensions.php';
72         $installed_extensions = array();
73         if (is_file($file)) {
74                 include($file);
75         }
76         return $installed_extensions;
77 }
78
79 function add_payment_terms($daysOrFoll, $terms, $dayNumber)
80 {
81         if ($daysOrFoll) 
82         {
83                 $sql = "INSERT INTO ".TB_PREF."payment_terms (terms,
84                         days_before_due, day_in_following_month)
85                         VALUES (" .
86                         db_escape($terms) . ", " . db_escape($dayNumber) . ", 0)";
87         } 
88         else 
89         {
90                 $sql = "INSERT INTO ".TB_PREF."payment_terms (terms,
91                         days_before_due, day_in_following_month)
92                         VALUES (" . db_escape($terms) . ",
93                         0, " . db_escape($dayNumber) . ")";
94         }
95         db_query($sql,"The payment term could not be added");
96 }
97
98 function update_payment_terms($selected_id, $daysOrFoll, $terms, $dayNumber)
99 {
100         if ($daysOrFoll) 
101         {
102                 $sql = "UPDATE ".TB_PREF."payment_terms SET terms=" . db_escape($terms) . ",
103                         day_in_following_month=0,
104                         days_before_due=" . db_escape($dayNumber) . "
105                         WHERE terms_indicator = " .db_escape($selected_id);
106         } 
107         else 
108         {
109                 $sql = "UPDATE ".TB_PREF."payment_terms SET terms=" . db_escape($terms) . ",
110                         day_in_following_month=" . db_escape($dayNumber) . ",
111                         days_before_due=0
112                         WHERE terms_indicator = " .db_escape($selected_id);
113         }
114         db_query($sql,"The payment term could not be updated");
115 }
116
117 function delete_payment_terms($selected_id)
118 {
119         $sql="DELETE FROM ".TB_PREF."payment_terms WHERE terms_indicator=".db_escape($selected_id);
120         db_query($sql,"could not delete a payment terms");
121 }
122
123 function get_payment_terms($selected_id)
124 {
125         $sql = "SELECT *, (t.days_before_due=0) AND (t.day_in_following_month=0) as cash_sale
126          FROM ".TB_PREF."payment_terms t WHERE terms_indicator=".db_escape($selected_id);
127
128         $result = db_query($sql,"could not get payment term");
129
130         return db_fetch($result);
131 }
132
133 function get_payment_terms_all($show_inactive)
134 {
135         $sql = "SELECT * FROM ".TB_PREF."payment_terms";
136         if (!$show_inactive) $sql .= " WHERE !inactive";
137         return db_query($sql,"could not get payment terms");
138 }
139 /*
140         Return number of records in tables, where some foreign key $id is used.
141         $id - searched key value
142         $tables - array of table names (without prefix); when table name is used as a key, then
143                 value is name of foreign key field. For numeric keys $stdkey field name is used.
144         $stdkey - standard name of foreign key.
145 */
146 function key_in_foreign_table($id, $tables, $stdkey)
147 {
148
149         if (!is_array($tables))
150                 $tables = array($tables);
151
152         $sqls = array();
153         foreach ($tables as $tbl => $key) {
154                 if (is_numeric($tbl)) {
155                         $tbl = $key;
156                         $key = $stdkey;
157                 }
158                 $sqls[] = "(SELECT COUNT(*) as cnt FROM `".TB_PREF."$tbl` WHERE `$key`=".db_escape($id).")\n";
159         }
160
161         $sql = "SELECT sum(cnt) FROM (". implode(' UNION ', $sqls).") as counts";
162
163         $result = db_query($sql, "check relations for ".implode(',',$tables)." failed");
164         $count =  db_fetch($result);
165
166         return $count[0];
167 }
168
169 //---------------------------------------------------------------------------------------------
170 //
171 // Resets $theme references in users records to 'default'.
172 //
173 function clean_user_themes($theme)
174 {
175         global $db_connections, $db;
176
177         $comp = user_company();
178
179         $connections = $db_connections; // do not use db_connections directly here, or script will hang due to db_connections usage inside the loop
180         foreach ($connections as $n => $conn) {
181                 $db = $_SESSION["wa_current_user"]->set_db_connection($n);
182                 $sql = "UPDATE {$conn['tbpref']}users SET theme='default' WHERE theme='$theme'";
183                 if (!db_query($sql, 'Cannot update user theme settings'))
184                         return false;
185         }
186         $db = $_SESSION["wa_current_user"]->set_db_connection($comp);
187         $_SESSION['wa_current_user']->prefs->theme = 'default';
188         return true;
189 }