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