System preferences moved form company to new sys_prefs table
[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         }
23         return true;
24 }
25 /*
26         Get company preferences. Returns cached values from global variable SysPrefs
27         or retrieved from database if SysPrefs values are not set.
28         $prefs can be preference name, array of names, or null for all preferences.
29         
30 */
31 function get_company_pref($prefs = null, $tbpref = TB_PREF)
32 {
33         global $SysPrefs, $core_version;
34         
35         if (!isset($_SESSION['SysPrefs']->prefs)) { // cached preferences
36
37                 $_SESSION['SysPrefs'] = new sys_prefs();
38
39                 $sql = "SELECT name, value FROM ".$tbpref."sys_prefs";
40                 $result = @db_query($sql); // supress errors before 2.3 db structure upgrade
41
42                 if(!$result)
43                         return null;
44
45                 while($pref = db_fetch_assoc($result)) {
46                         $_SESSION['SysPrefs']->prefs[$pref['name']] = $pref['value'];
47                 }
48
49                 $SysPrefs = &$_SESSION['SysPrefs'];
50
51                 // update current db status for info in log file
52                 $SysPrefs->db_ok = $SysPrefs->prefs['version_id'] == $core_version;
53         }
54
55         $all = $_SESSION['SysPrefs']->prefs;
56
57         if (!$prefs)
58                 return $all;
59         elseif (is_string($prefs))
60                 return $all[$prefs];
61
62         $ret = array();
63         foreach($prefs as $name)
64                 $ret[$name] = $all[$name];
65
66                 return $ret;
67 }
68
69 function get_company_prefs($tbpref = TB_PREF)
70 {
71         return get_company_pref(null, $tbpref);
72 }
73
74 function get_base_sales_type()
75 {
76         $sql = "SELECT base_sales FROM ".TB_PREF."company WHERE coy_code=1";
77
78         $result = db_query($sql, "could not get base sales type");
79         $myrow = db_fetch($result);
80         return $myrow[0];
81 }
82
83 function get_company_extensions($id = -1) {
84         global $path_to_root;
85
86         $file = $path_to_root.($id == -1 ? '' : '/company/'.$id).'/installed_extensions.php';
87         $installed_extensions = array();
88         if (is_file($file)) {
89                 include($file);
90         }
91         return $installed_extensions;
92 }
93
94 function add_payment_terms($daysOrFoll, $terms, $dayNumber)
95 {
96         if ($daysOrFoll) 
97         {
98                 $sql = "INSERT INTO ".TB_PREF."payment_terms (terms,
99                         days_before_due, day_in_following_month)
100                         VALUES (" .
101                         db_escape($terms) . ", " . db_escape($dayNumber) . ", 0)";
102         } 
103         else 
104         {
105                 $sql = "INSERT INTO ".TB_PREF."payment_terms (terms,
106                         days_before_due, day_in_following_month)
107                         VALUES (" . db_escape($terms) . ",
108                         0, " . db_escape($dayNumber) . ")";
109         }
110         db_query($sql,"The payment term could not be added");
111 }
112
113 function update_payment_terms($selected_id, $daysOrFoll, $terms, $dayNumber)
114 {
115         if ($daysOrFoll) 
116         {
117                 $sql = "UPDATE ".TB_PREF."payment_terms SET terms=" . db_escape($terms) . ",
118                         day_in_following_month=0,
119                         days_before_due=" . db_escape($dayNumber) . "
120                         WHERE terms_indicator = " .db_escape($selected_id);
121         } 
122         else 
123         {
124                 $sql = "UPDATE ".TB_PREF."payment_terms SET terms=" . db_escape($terms) . ",
125                         day_in_following_month=" . db_escape($dayNumber) . ",
126                         days_before_due=0
127                         WHERE terms_indicator = " .db_escape($selected_id);
128         }
129         db_query($sql,"The payment term could not be updated");
130 }
131
132 function delete_payment_terms($selected_id)
133 {
134         $sql="DELETE FROM ".TB_PREF."payment_terms WHERE terms_indicator=".db_escape($selected_id);
135         db_query($sql,"could not delete a payment terms");
136 }
137
138 function get_payment_terms($selected_id)
139 {
140         $sql = "SELECT * FROM ".TB_PREF."payment_terms WHERE terms_indicator=".db_escape($selected_id);
141
142         $result = db_query($sql,"could not get payment term");
143         return db_fetch($result);
144 }
145
146 function get_payment_terms_all($show_inactive)
147 {
148         $sql = "SELECT * FROM ".TB_PREF."payment_terms";
149         if (!$show_inactive) $sql .= " WHERE !inactive";
150         return db_query($sql,"could not get payment terms");
151 }
152
153 function key_in_foreign_table($id, $table, $key, $escaped=false)
154 {
155         if (!$escaped)
156                 $id = db_escape($id);
157         $sql= "SELECT COUNT(*) FROM ".TB_PREF."$table WHERE $key = $id";
158         $result = db_query($sql,"check $table relations failed");
159         $myrow = db_fetch_row($result);
160         return ($myrow[0] > 0); 
161 }
162
163 ?>