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