Fixed warning during database restore.
[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, $pref = TB_PREF )
16 {
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 "))
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, $tbpref = null)
34 {
35         global $SysPrefs, $db_version;
36         
37         static $cached; // retrieve values from db once a request. Some values can't be cached between requests
38                                         // to ensure prefs integrity for all usrs (e.g. gl_close_date).
39         
40         if (!$cached || !isset($_SESSION['SysPrefs'])) { // cached preferences
41
42                 $_SESSION['SysPrefs'] = new sys_prefs();
43
44                 if (!isset($tbpref))
45                         $tbpref = TB_PREF;
46
47                 $sql = "SELECT name, value FROM {$tbpref}sys_prefs";
48                 $result = @db_query($sql); // supress errors before 2.3 db structure upgrade
49
50                 if(!$result)
51                         return null;
52
53                 while($pref = db_fetch_assoc($result)) {
54                         $_SESSION['SysPrefs']->prefs[$pref['name']] = $pref['value'];
55                 }
56
57                 $SysPrefs = &$_SESSION['SysPrefs'];
58
59                 // update current db status for info in log file
60                 $SysPrefs->db_ok = $SysPrefs->prefs['version_id'] == $db_version;
61                 $cached = true;
62         }
63
64         $all = $_SESSION['SysPrefs']->prefs;
65
66         if (!$prefs)
67                 return $all;
68         elseif (is_string($prefs))
69                 return @$all[$prefs];
70
71         $ret = array();
72         foreach($prefs as $name)
73                 $ret[$name] = $all[$name];
74
75                 return $ret;
76 }
77
78 function get_company_prefs($tbpref = TB_PREF)
79 {
80         return get_company_pref(null, $tbpref);
81 }
82
83 function set_company_pref($pref, $category, $type, $length, $value)
84 {
85         $sql = "REPLACE ".TB_PREF."sys_prefs SET name=".db_escape($pref).", category=".db_escape($category)
86                 .", type=".db_escape($type).", length=".db_escape($length).", value=".db_escape($value);
87         return db_query($sql, "cannot set company pref");
88 }
89
90 function refresh_sys_prefs()
91 {
92         flush_dir(company_path().'/js_cache'); // clear cache
93         unset($_SESSION['SysPrefs']);
94         get_company_prefs();
95 }
96
97 function get_base_sales_type()
98 {
99         return get_company_pref('base_sales');
100 }
101
102 function get_company_extensions($id = -1) {
103         global $path_to_root;
104
105         $file = $path_to_root.($id == -1 ? '' : '/company/'.(int)$id).'/installed_extensions.php';
106         $installed_extensions = array();
107         if (is_file($file)) {
108                 include($file);
109         }
110         return $installed_extensions;
111 }
112
113 function add_payment_terms($daysOrFoll, $terms, $dayNumber)
114 {
115         if ($daysOrFoll) 
116         {
117                 $sql = "INSERT INTO ".TB_PREF."payment_terms (terms,
118                         days_before_due, day_in_following_month)
119                         VALUES (" .
120                         db_escape($terms) . ", " . db_escape($dayNumber) . ", 0)";
121         } 
122         else 
123         {
124                 $sql = "INSERT INTO ".TB_PREF."payment_terms (terms,
125                         days_before_due, day_in_following_month)
126                         VALUES (" . db_escape($terms) . ",
127                         0, " . db_escape($dayNumber) . ")";
128         }
129         db_query($sql,"The payment term could not be added");
130 }
131
132 function update_payment_terms($selected_id, $daysOrFoll, $terms, $dayNumber)
133 {
134         if ($daysOrFoll) 
135         {
136                 $sql = "UPDATE ".TB_PREF."payment_terms SET terms=" . db_escape($terms) . ",
137                         day_in_following_month=0,
138                         days_before_due=" . db_escape($dayNumber) . "
139                         WHERE terms_indicator = " .db_escape($selected_id);
140         } 
141         else 
142         {
143                 $sql = "UPDATE ".TB_PREF."payment_terms SET terms=" . db_escape($terms) . ",
144                         day_in_following_month=" . db_escape($dayNumber) . ",
145                         days_before_due=0
146                         WHERE terms_indicator = " .db_escape($selected_id);
147         }
148         db_query($sql,"The payment term could not be updated");
149 }
150
151 function delete_payment_terms($selected_id)
152 {
153         $sql="DELETE FROM ".TB_PREF."payment_terms WHERE terms_indicator=".db_escape($selected_id);
154         db_query($sql,"could not delete a payment terms");
155 }
156
157 function get_payment_terms($selected_id)
158 {
159         $sql = "SELECT *, (t.days_before_due=0) AND (t.day_in_following_month=0) as cash_sale
160          FROM ".TB_PREF."payment_terms t WHERE terms_indicator=".db_escape($selected_id);
161
162         $result = db_query($sql,"could not get payment term");
163
164         return db_fetch($result);
165 }
166
167 function get_payment_terms_all($show_inactive)
168 {
169         $sql = "SELECT * FROM ".TB_PREF."payment_terms";
170         if (!$show_inactive) $sql .= " WHERE !inactive";
171         return db_query($sql,"could not get payment terms");
172 }
173 /*
174         Return number of records in tables, where some foreign key $id is used.
175         $id - searched key value
176         $tables - array of table names (without prefix); when table name is used as a key, then
177                 value is name of foreign key field. For numeric keys $stdkey field name is used.
178         $stdkey - standard name of foreign key.
179 */
180 function key_in_foreign_table($id, $tables, $stdkey)
181 {
182
183         if (!is_array($tables))
184                 $tables = array($tables);
185
186         $sqls = array();
187         foreach ($tables as $tbl => $key) {
188                 if (is_numeric($tbl)) {
189                         $tbl = $key;
190                         $key = $stdkey;
191                 }
192                 $sqls[] = "(SELECT COUNT(*) as cnt FROM `".TB_PREF."$tbl` WHERE `$key`=".db_escape($id).")\n";
193         }
194
195         $sql = "SELECT sum(cnt) FROM (". implode(' UNION ', $sqls).") as counts";
196
197         $result = db_query($sql, "check relations for ".implode(',',$tables)." failed");
198         $count =  db_fetch($result);
199
200         return $count[0];
201 }
202
203 ?>