Incorrect Journal Balance (sales invoice) when many decimals in tax and price.
[fa-stable.git] / includes / errors.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 $messages = array(); // container for system messages
13 $before_box = ''; // temporary container for output html data before error box
14
15 function get_backtrace($html = false, $skip=0)
16 {
17         $str = '';
18         if ($html) $str .= '<table border=0>';
19         $trace = debug_backtrace();
20
21         foreach($trace as $trn => $tr) {
22                 if ($trn <= $skip) continue;
23                 if ($html) $str .= '<tr><td>';
24                 if (isset($tr['file']) && isset($tr['line']))
25                         $str .= $tr['file'].':'.$tr['line'].': ';
26                 if ($html) $str .= '</td><td>';
27                 if (isset($tr['type'])) {
28                         if($tr['type'] == '::') {
29                                 $str .= $tr['class'].'::';
30                         } else if($tr['type'] == '->') {
31                                 $str .= '('.$tr['class'].' Object)'.'->';
32                         }
33                 }
34                 $str .= $tr['function'].'(';
35                 
36                 if(isset($tr['args']) && is_array($tr['args'])) {
37                         $args = array();
38                         foreach($tr['args'] as $n=>$a) {
39                                 if (is_object($tr['args'][$n]))
40                                         $args[$n] = "(".get_class($tr['args'][$n])." Object)";
41                                 elseif (is_array($tr['args'][$n]))
42                                         $args[$n] = "(Array[".count($tr['args'][$n])."])";
43                                 else
44                                         $args[$n] = "'".$tr['args'][$n]."'";
45                         }
46                         $str .= implode(',',$args);
47                 }
48                 $str .= ')</td>';
49         }
50
51         if ($html) $str .= '</tr></table>';
52         return $str;
53 }
54
55 //-----------------------------------------------------------------------------
56 //    Error handler - collects all php/user messages for
57 //    display in message box.
58
59 function error_handler($errno, $errstr, $file, $line) {
60     global $messages, $SysPrefs, $cur_error_level;
61
62         // skip well known warnings we don't care about.
63         // Please use restrainedly to not risk loss of important messages
64         $excluded_warnings = array(
65                 'html_entity_decode',                           // nevermind encodings, special chars are processed anyway
66                 'should be compatible with that',       // ignore cpdf/frontreport wrapper warnings
67                 'mysql extension is deprecated'         // ignore strict warning in 5.4
68         );
69         foreach($excluded_warnings as $ref) {
70                 if (strpos($errstr, $ref) !== false) {
71                         return true;
72                 }
73         }
74
75         $bt = isset($SysPrefs) && $SysPrefs->go_debug>1 ? get_backtrace(true, 1) : array();
76
77         // error_reporting!=cur_error_level when messages are set off with @ 
78         if ($cur_error_level == error_reporting()) {
79                 if ($errno & $cur_error_level) {
80                         // suppress duplicated errors
81                         if (!in_array(array($errno, $errstr, $file, $line, @$bt), $messages))
82                                 $messages[] = array($errno, $errstr, $file, $line, @$bt);
83                 }
84                 else if ($errno&~E_NOTICE && $errstr != '') { // log all not displayed messages 
85                         $user = @$_SESSION["wa_current_user"]->loginname;
86                         $context = isset($SysPrefs) && !$SysPrefs->db_ok ? '[before upgrade]' : '';
87                         error_log(user_company() . ":$user:". basename($file) .":$line:$context $errstr");
88                 }
89         }
90         
91     return true;
92 }
93
94 function exception_handler($exception)
95 {
96         error_handler(E_ERROR, sprintf(_("Unhandled exception [%s]: %s."), $exception->getCode(), $exception->getMessage()),
97                  $exception->getFile(), $exception->getLine());
98         end_page();
99 }
100 //------------------------------------------------------------------------------
101 //      Formats system messages before insert them into message <div>
102 // FIX center is unused now
103 function fmt_errors($center=false) {
104     global $messages, $path_to_root, $SysPrefs;
105
106         $msg_class = array(
107                 E_USER_ERROR => 'err_msg',
108                 E_USER_WARNING =>'warn_msg', 
109                 E_USER_NOTICE => 'note_msg');
110
111         $type = E_USER_NOTICE;
112         $content = '';
113
114         if (count($messages)) {
115                 foreach($messages as $cnt=>$msg) {
116                         if ($SysPrefs->go_debug && $msg[0]>E_USER_NOTICE)
117                                 $msg[0] = E_ERROR;
118
119                         if ($msg[0]>$type) continue;
120
121                         if ($msg[0]<$type) { 
122                                 if ($msg[0] == E_USER_WARNING) {
123                                         $type = E_USER_WARNING; // user warnings 
124                                         $content = '';                  // clean notices when we have errors
125                                 } else  {
126                                         $type = E_USER_ERROR;   // php or user errors 
127                                         if($type == E_USER_WARNING)
128                                                 $content = '';                  // clean other messages
129                                 }
130                         }
131
132                 $str = $msg[1];
133                         if (!in_array($msg[0], array(E_USER_NOTICE, E_USER_ERROR, E_USER_WARNING)) && $msg[2] != null)
134                                 $str .= ' '._('in file').': '.$msg[2].' '._('at line ').$msg[3];
135
136                         if ($SysPrefs->go_debug>1 && $type!=E_USER_NOTICE && $type!=E_USER_WARNING)
137                                 $str .= '<br>'.$msg[4];
138                         $content .= ($cnt ? '<hr>' : '').$str;
139                 }
140                 $class = $msg_class[$type];
141         $content = "<div class='$class'>$content</div>";
142         } elseif ($path_to_root=='.')
143                 return '';
144         return $content;
145 }
146 //-----------------------------------------------------------------------------
147 // Error box <div> element.
148 //
149 function error_box() {
150     global $before_box;
151     
152     echo "<div id='msgbox'>";
153
154         // Necessary restart instead of get_contents/clean calls due to a bug in php 4.3.2
155         $before_box = ob_get_clean(); // save html content before error box 
156     ob_start('output_html');
157     echo "</div>";
158 }
159 /*
160         Helper to avoid sparse log notices.
161 */
162 function end_flush() {
163         global $Ajax;
164
165         if (isset($Ajax))
166                 $Ajax->run();
167
168          // on some (but not all) php versions zlib extension adds 1 additional level of buffering, 
169          // so flush the last buffer outside the loop to be on safe side 
170         while(ob_get_level() > 1)
171                 ob_end_flush();
172         @ob_end_flush();
173
174         // if any transaction was aborted unexpectedly rollback changes
175         cancel_transaction();
176 }
177
178 function display_db_error($msg, $sql_statement=null, $exit=true)
179 {
180         global $db, $SysPrefs, $db_connections;
181
182         $warning = $msg==null;
183         $db_error = db_error_no();
184         
185         if($warning)
186                 $str = "<b>" . _("Debug mode database warning:") . "</b><br>";
187         else
188                 $str = "<b>" . _("DATABASE ERROR :") . "</b> $msg<br>";
189         
190         if ($db_error != 0) 
191         {
192                 $str .= "error code : " . $db_error . "<br>";
193                 $str .= "error message : " . db_error_msg($db) . "<br>";
194         }
195         
196         if ($SysPrefs->debug == 1) 
197         {
198                 $cur_prefix = $db_connections[$_SESSION["wa_current_user"]->cur_con]['tbpref'];
199
200                 $str .= "sql that failed was : ".str_replace(TB_PREF, $cur_prefix, $sql_statement)."<br>";
201         }
202         
203         $str .= "<br><br>";
204         if (!$SysPrefs->go_debug)
205                 error_log($str);
206         else {
207                 if($msg)
208                         trigger_error($str, E_USER_ERROR);
209                 else    // $msg can be null here only in debug mode, otherwise the error is ignored
210                         trigger_error($str, E_USER_WARNING);
211         }
212         if ($exit)
213                 exit;
214 }
215
216 function frindly_db_error($db_error)
217 {
218         if ($db_error == DB_DUPLICATE_ERROR) 
219         {
220                 display_error(_("The entered information is a duplicate. Please go back and enter different values."));
221                 return true;
222         }
223         
224         return false;
225 }
226
227 function check_db_error($msg, $sql_statement, $exit_if_error=true, $rollback_if_error=true)
228 {
229         global $db, $SysPrefs;
230         $db_error = db_error_no();
231         
232         if ($db_error != 0) 
233         {
234                 
235                 if ($SysPrefs->go_debug || !frindly_db_error($db_error)) {
236                         display_db_error($msg, $sql_statement, false);
237                 }
238                 
239                 if ($rollback_if_error) 
240                 {
241                         $rollback_result = db_query("rollback");
242                 }
243                 
244                 if ($exit_if_error) 
245                 {
246                         end_page(); exit;
247                 }
248         }
249         return $db_error;
250 }
251