Added column Currency in Bank Accounts. Changed to all years in fixed assets.
[fa-stable.git] / includes / dashboard.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 function dashboard($sel_app)
14 {
15         $selected_app = $sel_app;
16
17
18         if (!$_SESSION["wa_current_user"]->check_application_access($selected_app))
19                 return;
20         // first have a look through the directory, 
21         // and remove old temporary pdfs and pngs
22         $dir = company_path(). '/pdf_files';
23
24         if ($d = @opendir($dir)) {
25                 while (($file = readdir($d)) !== false) {
26                         if (!is_file($dir.'/'.$file) || $file == 'index.php') continue;
27                 // then check to see if this one is too old
28                         $ftime = filemtime($dir.'/'.$file);
29                  // seems 3 min is enough for any report download, isn't it?
30                         if (time()-$ftime > 180){
31                                 unlink($dir.'/'.$file);
32                         }
33                 }
34                 closedir($d);
35         }
36
37         //check_for_overdue_recurrent_invoices();
38         if ($selected_app == "orders")
39                 display_customer_topten();
40         elseif ($selected_app == "AP")
41                 display_supplier_topten();
42         elseif ($selected_app == "stock")
43                 display_stock_topten();
44         elseif ($selected_app == "manuf")
45                 display_stock_topten(1);
46         elseif ($selected_app == "assets")
47                 display_stock_topten(2);
48         elseif ($selected_app == "proj")
49                 display_dimension_topten();
50         elseif ($selected_app == "GL")
51                 display_gl_info();
52         else    
53                 display_all();
54 }
55
56 function display_title($title)
57 {
58         br();
59         display_heading($title);
60         br();
61         //echo "<tr><td colspan=$colspan class='headingtext' style='background-color:#fff;text-align:center;border:0;height:40px;'>$title</td></tr>\n";
62 }       
63
64 function table_one()
65 {
66         echo "<table width='100%'>";
67         echo "<tr valign=top><td style='width:50%'>\n"; // outer table
68 }
69
70 function table_two()
71 {
72         echo "</td><td style='width:50%'>\n";
73 }
74
75 function table_end()
76 {
77         echo "</td></tr></table>\n";
78 }
79
80 function display_customer_topten()
81 {
82         $pg = new graph();
83
84         table_one();
85         $today = Today();
86         $title = customer_top($today, 10, 66, $pg);
87         customer_trans($today);
88         table_two();
89         source_graphic($today, $title, _("Customer"), $pg, _("Sales"));
90         customer_recurrent_invoices($today);
91         table_end();
92 }
93
94 function display_supplier_topten()
95 {
96         $pg = new graph();
97
98         $today = Today();
99         table_one();
100         $title = supplier_top($today, 10, 66, $pg);
101         supplier_trans($today);
102         table_two();
103         source_graphic($today, $title, _("Supplier"), $pg, _("Purchase"));
104         table_end();
105 }
106
107 function display_stock_topten($type=0)
108 {
109         $pg = new graph();
110
111         table_one();
112         $today = Today();
113         $title = stock_top($today, 10, 66, $type, $pg);
114         table_two();
115         if ($type == 2)
116                 $source = _("Fixed Assets");
117         elseif ($type == 1)
118                 $source = _("Manufacturing");
119         else    
120                 $source = _("Items");
121         source_graphic($today, $title, $source, $pg, ($type == 0 ? _("Sales") : _("Amount")), 
122                 ($type == 0 ? _("Costs") : null));
123         table_end();
124 }
125
126 function display_dimension_topten()
127 {
128         $pg = new graph();
129
130         table_one();
131         $today = Today();
132         $title = dimension_top($today, 10, 66, $pg);
133         table_two();
134         source_graphic($today, $title, _("Dimension"), $pg, _("Performance"), null, 5);
135         table_end();
136 }       
137
138 function display_gl_info()
139 {
140         $pg = new graph();
141
142         table_one();
143         $today = Today();
144         $title = gl_top($today, 66, $pg);
145         bank_balance($today, 66);
146         table_two();
147         source_graphic($today, $title, _("Class"), $pg, _("Amount"), null, 5);
148         gl_performance($today, 66, 5);
149         table_end();
150 }       
151
152 function display_all()
153 {
154         $today = Today();
155         $pg = new graph();
156
157         table_one();
158         $title = customer_top($today, 3, 66, $pg);
159         source_graphic($today, $title, _("Customer"), $pg, _("Sales"));
160         $title = supplier_top($today, 3, 66, $pg);
161         source_graphic($today, $title, _("Supplier"), $pg, _("Purchases"));
162         $title = stock_top($today, 3, 66, 0, $pg);
163         source_graphic($today, $title, _("Items"), $pg, _("Sales"), _("Costs"));
164         table_two();
165         dimension_top($today, 3, 66);
166         $title = gl_top($today, 66, $pg);
167         source_graphic($today, $title, _("Class"), $pg, _("Amount"), null, 5);
168         stock_top($today, 3, 66, 2);
169         stock_top($today, 3, 66, 1);
170         table_end();
171 }
172
173 function customer_top($today, $limit=10, $width="33", &$pg=null)
174 {
175         $begin = begin_fiscalyear();
176         $begin1 = date2sql($begin);
177         $today1 = date2sql($today);
178         $sql = "SELECT SUM((ov_amount + ov_discount) * rate * IF(trans.type = ".ST_CUSTCREDIT.", -1, 1)) AS total,d.debtor_no, d.name FROM
179                 ".TB_PREF."debtor_trans AS trans, ".TB_PREF."debtors_master AS d WHERE trans.debtor_no=d.debtor_no
180                 AND (trans.type = ".ST_SALESINVOICE." OR trans.type = ".ST_CUSTCREDIT.")
181                 AND tran_date >= '$begin1' AND tran_date <= '$today1' GROUP by d.debtor_no ORDER BY total DESC, d.debtor_no 
182                 LIMIT $limit";
183         $result = db_query($sql);
184         $title = _("Top $limit customers in fiscal year");
185         display_title($title);
186         $th = array(_("Customer"), _("Amount"));
187         start_table(TABLESTYLE, "width='$width%'");
188         table_header($th);
189         check_page_security('SA_SALESTRANSVIEW');
190         $k = 0; //row colour counter
191         $i = 0;
192         while ($myrow = db_fetch($result))
193         {
194                 alt_table_row_color($k);
195                 $name = $myrow["debtor_no"]." ".$myrow["name"];
196                 label_cell($name);
197                 amount_cell($myrow['total']);
198                 if ($pg != null)
199                 {
200                         $pg->x[$i] = $name; 
201                         $pg->y[$i] = $myrow['total'];
202                 }       
203                 $i++;
204                 end_row();
205         }
206         end_table(2);
207         return $title;
208 }
209
210 function supplier_top($today, $limit=10, $width="33", &$pg=null)
211 {
212         $begin = begin_fiscalyear();
213         $begin1 = date2sql($begin);
214         $today1 = date2sql($today);
215         $sql = "SELECT SUM((trans.ov_amount + trans.ov_discount) * rate) AS total, s.supplier_id, s.supp_name FROM
216                 ".TB_PREF."supp_trans AS trans, ".TB_PREF."suppliers AS s WHERE trans.supplier_id=s.supplier_id
217                 AND (trans.type = ".ST_SUPPINVOICE." OR trans.type = ".ST_SUPPCREDIT.")
218                 AND tran_date >= '$begin1' AND tran_date <= '$today1' GROUP by s.supplier_id ORDER BY total DESC, s.supplier_id 
219                 LIMIT $limit";
220         $result = db_query($sql);
221         $title = _("Top $limit suppliers in fiscal year");
222         display_title($title);
223         $th = array(_("Supplier"), _("Amount"));
224         start_table(TABLESTYLE, "width='$width%'");
225         table_header($th);
226         check_page_security('SA_SUPPTRANSVIEW');
227         $k = 0; //row colour counter
228         $i = 0;
229         while ($myrow = db_fetch($result))
230         {
231                 alt_table_row_color($k);
232                 $name = $myrow["supplier_id"]." ".$myrow["supp_name"];
233                 label_cell($name);
234                 amount_cell($myrow['total']);
235                 if ($pg != null)
236                 {
237                         $pg->x[$i] = $name; 
238                         $pg->y[$i] = $myrow['total'];
239                 }       
240                 $i++;
241                 end_row();
242         }
243         end_table(2);
244         return $title;
245 }
246
247 function stock_top($today, $limit=10, $width="33", $type=0, &$pg=null)
248 {
249         if ($type == 2)
250                 $sec = 'SA_ASSETSANALYTIC';
251         elseif ($type == 1)
252                 $sec = 'SA_WORKORDERANALYTIC';
253         else
254                 $sec = 'SA_ITEMSTRANSVIEW';
255         $begin = begin_fiscalyear();
256         $begin1 = date2sql($begin);
257         $today1 = date2sql($today);
258         if ($type == 0)
259         {
260                 $sql = "SELECT SUM((trans.unit_price * trans.quantity) * d.rate) AS total, s.stock_id, s.description, 
261                         SUM(trans.quantity) AS qty, SUM((s.material_cost + s.overhead_cost + s.labour_cost) * trans.quantity) AS costs FROM
262                         ".TB_PREF."debtor_trans_details AS trans, ".TB_PREF."stock_master AS s, ".TB_PREF."debtor_trans AS d 
263                         WHERE trans.stock_id=s.stock_id AND trans.debtor_trans_type=d.type AND trans.debtor_trans_no=d.trans_no
264                         AND (d.type = ".ST_SALESINVOICE." OR d.type = ".ST_CUSTCREDIT.") ";
265         }
266         else
267         {
268                 $sql = "SELECT SUM(m.qty * (s.material_cost + s.labour_cost + s.overhead_cost)) AS total, s.stock_id, s.description, 
269                         SUM(qty) AS qty FROM ".TB_PREF."stock_master AS s, ".TB_PREF."stock_moves AS m 
270                         WHERE s.stock_id=m.stock_id ";
271                 if ($type == 1)
272                         $sql .= "AND s.mb_flag='M' ";
273                 elseif ($type == 2)     
274                         $sql .= "AND s.mb_flag='F' ";
275         }
276         if ($type != 2)
277                 $sql .= "AND tran_date >= '$begin1' ";
278         $sql .= "AND tran_date <= '$today1' GROUP by s.stock_id ORDER BY total DESC, s.stock_id 
279                 LIMIT $limit";
280         $result = db_query($sql);
281         if ($type == 1)
282                 $title = _("Top $limit Manufactured Items in fiscal year");
283         elseif ($type == 2)
284                 $title = _("Top $limit Fixed Assets");
285         else    
286                 $title = _("Top $limit Sold Items in fiscal year");
287         display_title($title);  
288         if ($type == 0) 
289                 $th = array(_("Item"), _("Sales"), _("Costs"), _("Quantity"));
290         else    
291                 $th = array(_("Item"), _("Amount"), _("Quantity"));
292         start_table(TABLESTYLE, "width='$width%'");
293         table_header($th);
294         check_page_security($sec);
295         $k = 0; //row colour counter
296         $i = 0;
297         while ($myrow = db_fetch($result))
298         {
299                 alt_table_row_color($k);
300                 $name = $myrow["description"];
301                 label_cell($name);
302                 amount_cell($myrow['total']);
303                 if ($type == 0)
304                         amount_cell($myrow['costs']);
305                 qty_cell($myrow['qty']);
306                 if ($pg != NULL)
307                 {
308                         $pg->x[$i] = $name; 
309                         $pg->y[$i] = $myrow['total'];
310                         if ($type == 0)
311                                 $pg->z[$i] = $myrow['costs'];
312                 }       
313                 $i++;
314                 end_row();
315         }
316         end_table(2);
317         return $title;
318 }
319
320 function dimension_top($today, $limit=10, $width="33", &$pg=null)
321 {
322
323         $begin = begin_fiscalyear();
324         $begin1 = date2sql($begin);
325         $today1 = date2sql($today);
326         $sql = "SELECT SUM(-t.amount) AS total, d.reference, d.name FROM
327                 ".TB_PREF."gl_trans AS t,".TB_PREF."dimensions AS d WHERE
328                 (t.dimension_id = d.id OR t.dimension2_id = d.id) AND
329                 t.tran_date >= '$begin1' AND t.tran_date <= '$today1' GROUP BY d.id ORDER BY total DESC LIMIT $limit";
330         $result = db_query($sql, "Transactions could not be calculated");
331         $title = _("Top $limit Dimensions in fiscal year");
332         display_title($title);
333         $th = array(_("Dimension"), _("Amount"));
334         start_table(TABLESTYLE, "width='$width%'");
335         table_header($th);
336         check_page_security('SA_DIMTRANSVIEW');
337         $k = 0; //row colour counter
338         $i = 0;
339         while ($myrow = db_fetch($result))
340         {
341                 alt_table_row_color($k);
342                 $name = $myrow['reference']." ".$myrow["name"];
343                 label_cell($name);
344                 amount_cell($myrow['total']);
345                 if ($pg != null)
346                 {
347                         $pg->x[$i] = $name; 
348                         $pg->y[$i] = abs($myrow['total']);
349                 }       
350                 $i++;
351                 end_row();
352         }
353         end_table(2);
354         return $title;
355 }
356
357 function gl_top($today, $width="33", &$pg=null)
358 {
359         $begin = begin_fiscalyear();
360         $begin1 = date2sql($begin);
361         $today1 = date2sql($today);
362         $sql = "SELECT SUM(amount) AS total, c.class_name, c.ctype FROM
363                 ".TB_PREF."gl_trans,".TB_PREF."chart_master AS a, ".TB_PREF."chart_types AS t, 
364                 ".TB_PREF."chart_class AS c WHERE
365                 account = a.account_code AND a.account_type = t.id AND t.class_id = c.cid
366                 AND IF(c.ctype > 3, tran_date >= '$begin1', tran_date >= '0000-00-00') 
367                 AND tran_date <= '$today1' GROUP BY c.cid ORDER BY c.cid"; 
368         $result = db_query($sql, "Transactions could not be calculated");
369         $title = _("Class Balances");
370         display_title($title);
371         start_table(TABLESTYLE2, "width='$width%'");
372         check_page_security('SA_GLANALYTIC');
373         $i = 0;
374         $total = 0;
375         while ($myrow = db_fetch($result))
376         {
377                 if ($myrow['ctype'] > 3)
378                 {
379                         $total += $myrow['total'];
380                         $myrow['total'] = -$myrow['total'];
381                         if ($pg != null)
382                         {
383                                 $pg->x[$i] = $myrow['class_name']; 
384                                 $pg->y[$i] = abs($myrow['total']);
385                         }       
386                         $i++;
387                 }       
388                 label_row($myrow['class_name'], number_format2($myrow['total'], user_price_dec()), 
389                         "class='label' style='font-weight:bold;'", "style='font-weight:bold;' align=right");
390         }
391         $calculated = _("Calculated Return");
392         label_row("&nbsp;", "");
393         label_row($calculated, number_format2(-$total, user_price_dec()), 
394                 "class='label' style='font-weight:bold;'", "style='font-weight:bold;' align=right");
395         if ($pg != null)
396         {
397                 $pg->x[$i] = $calculated; 
398                 $pg->y[$i] = -$total;
399         }
400         end_table(2);
401         return $title;
402 }
403
404 function gl_performance($today, $width="33", $weeks=5)
405 {
406         $pg = new graph();
407
408         $begin = begin_fiscalyear();
409         $begin1 = date2sql($begin);
410         $today1 = date2sql($today);
411         $sql = "SELECT CONCAT(YEAR(tran_date), '/', WEEK(tran_date)) AS week_name, 
412                         SUM(IF (c.ctype = 4, amount * -1, 0)) AS sales, 
413                         SUM(IF (c.ctype = 6, amount, 0)) AS costs 
414                 FROM
415                         ".TB_PREF."gl_trans, ".TB_PREF."chart_master AS a, ".TB_PREF."chart_types AS t, 
416                         ".TB_PREF."chart_class AS c WHERE (c.ctype = 4 OR c.ctype = 6)
417                         AND account = a.account_code AND a.account_type = t.id AND t.class_id = c.cid
418                         AND IF(c.ctype > 3, tran_date >= '$begin1', tran_date >= '0000-00-00') 
419                         AND tran_date <= '$today1' 
420                 GROUP BY week_name 
421                 ORDER BY YEAR(tran_date) DESC, WEEK(tran_date) DESC limit 0, $weeks";
422         $result = db_query($sql, "Transactions could not be calculated");
423         $title = _("Last $weeks weeks Performance");
424         check_page_security('SA_GLANALYTIC');
425         $i = 0;
426         while ($myrow = db_fetch($result))
427         {
428                 $pg->x[$i] = $myrow['week_name']; 
429                 $pg->y[$i] = $myrow['sales'];
430                 $pg->z[$i] = $myrow['costs'];
431                 $i++;
432         }       
433         $pg->x = array_reverse($pg->x);
434         $pg->y = array_reverse($pg->y);
435         $pg->z = array_reverse($pg->z);
436         
437         source_graphic($today, $title, _("Week"), $pg, _("Sales"), _("Costs"), 1);
438 }
439
440 function source_graphic($today, $title, $x_axis, $pg, $graphic1, $graphic2=null, $type=2)
441 {
442         if (count($pg->y) ==0 || (count($pg->y) == 1 && $pg->y[0] == 0))
443                 return;
444         display_title("$title ($today)");       
445         //$pg->title     = $title . " - " . $today;
446         $pg->axis_x    = $x_axis;
447         $pg->axis_y    = _("Amount");
448         $pg->graphic_1 = $graphic1;
449         if ($graphic2 != null)
450                 $pg->graphic_2 = $graphic2;
451         $pg->type      = $type;
452         $pg->skin      = 1;
453         $pg->built_in  = false;
454         $filename = company_path(). "/pdf_files/". uniqid("").".png";
455         $pg->display($filename, true);
456         start_table(TABLESTYLE);
457         start_row();
458         echo "<td>";
459         echo "<img src='$filename' border='0' alt='$title'>";
460         echo "</td>";
461         end_row();
462         end_table(1);
463 }
464
465 function customer_trans($today)
466 {
467         $today = date2sql($today);
468
469         $sql = "SELECT trans.trans_no, trans.reference, trans.tran_date, trans.due_date, debtor.debtor_no, 
470                 debtor.name, branch.br_name, debtor.curr_code,
471                 (trans.ov_amount + trans.ov_gst + trans.ov_freight 
472                         + trans.ov_freight_tax + trans.ov_discount)     AS total,  
473                 (trans.ov_amount + trans.ov_gst + trans.ov_freight 
474                         + trans.ov_freight_tax + trans.ov_discount - trans.alloc) AS remainder,
475                 DATEDIFF('$today', trans.due_date) AS days      
476                 FROM ".TB_PREF."debtor_trans as trans, ".TB_PREF."debtors_master as debtor, 
477                         ".TB_PREF."cust_branch as branch
478                 WHERE debtor.debtor_no = trans.debtor_no AND trans.branch_code = branch.branch_code
479                         AND trans.type = ".ST_SALESINVOICE." AND (trans.ov_amount + trans.ov_gst + trans.ov_freight 
480                         + trans.ov_freight_tax + trans.ov_discount - trans.alloc) > ".FLOAT_COMP_DELTA." 
481                         AND DATEDIFF('$today', trans.due_date) > 0 ORDER BY days DESC";
482         $result = db_query($sql);
483         $title = db_num_rows($result) . _(" overdue Sales Invoices");
484         display_title($title);
485         $th = array("#", _("Ref."), _("Date"), _("Due Date"), _("Customer"), _("Branch"), _("Currency"), 
486                 _("Total"), _("Remainder"),     _("Days"));
487         start_table(TABLESTYLE);
488         table_header($th);
489         $k = 0; //row colour counter
490         while ($myrow = db_fetch($result))
491         {
492                 alt_table_row_color($k);
493                 label_cell(get_trans_view_str(ST_SALESINVOICE, $myrow["trans_no"]));
494                 label_cell($myrow['reference']);
495                 label_cell(sql2date($myrow['tran_date']));
496                 label_cell(sql2date($myrow['due_date']));
497                 $name = $myrow["debtor_no"]." ".$myrow["name"];
498                 label_cell($name);
499                 label_cell($myrow['br_name']);
500                 label_cell($myrow['curr_code']);
501                 amount_cell($myrow['total']);
502                 amount_cell($myrow['remainder']);
503                 label_cell($myrow['days'], "align='right'");
504                 end_row();
505         }
506         end_table(2);
507 }
508
509 function calculate_next_invoice($myrow)
510 {
511         if ($myrow["last_sent"] == '0000-00-00')
512                 $next = sql2date($myrow["begin"]);
513         else
514                 $next = sql2date($myrow["last_sent"]);
515         $next = add_months($next, $myrow['monthly']);
516         $next = add_days($next, $myrow['days']);
517         return add_days($next,-1);
518 }
519
520 function customer_recurrent_invoices($today)
521 {
522         $result = get_recurrent_invoices($today);
523         $title = _("Overdue Recurrent Invoices");
524         display_title($title);
525         $th = array(_("Description"), _("Template No"),_("Customer"),_("Branch")."/"._("Group"),_("Next invoice"));
526         start_table(TABLESTYLE, "width=70%");
527         table_header($th);
528         $k = 0;
529         while ($myrow = db_fetch($result)) 
530         {
531                 if (!$myrow['overdue'])
532                         continue;
533                 alt_table_row_color($k);
534
535                 label_cell($myrow["description"]);
536                 label_cell(get_customer_trans_view_str(ST_SALESORDER, $myrow["order_no"]));
537                 if ($myrow["debtor_no"] == 0)
538                 {
539                         label_cell("");
540
541                         label_cell(get_sales_group_name($myrow["group_no"]));
542                 }
543                 else
544                 {
545                         label_cell(get_customer_name($myrow["debtor_no"]));
546                         label_cell(get_branch_name($myrow['group_no']));
547                 }
548                 label_cell(calculate_next_invoice($myrow),  "align='center'");
549                 end_row();
550         }
551         end_table(2);
552 }
553
554 function supplier_trans($today)
555 {
556         $today = date2sql($today);
557         $sql = "SELECT trans.trans_no, trans.reference, trans.tran_date, trans.due_date, s.supplier_id, 
558                 s.supp_name, s.curr_code,
559                 (trans.ov_amount + trans.ov_gst + trans.ov_discount) AS total,  
560                 (trans.ov_amount + trans.ov_gst + trans.ov_discount - trans.alloc) AS remainder,
561                 DATEDIFF('$today', trans.due_date) AS days      
562                 FROM ".TB_PREF."supp_trans as trans, ".TB_PREF."suppliers as s 
563                 WHERE s.supplier_id = trans.supplier_id
564                         AND trans.type = ".ST_SUPPINVOICE." AND (ABS(trans.ov_amount + trans.ov_gst + 
565                                 trans.ov_discount) - trans.alloc) > ".FLOAT_COMP_DELTA."
566                         AND DATEDIFF('$today', trans.due_date) > 0 ORDER BY days DESC";
567         $result = db_query($sql);
568         $title = db_num_rows($result) . _(" overdue Purchase Invoices");
569         display_title($title);
570         $th = array("#", _("Ref."), _("Date"), _("Due Date"), _("Supplier"), _("Currency"), _("Total"), 
571                 _("Remainder"), _("Days"));
572         start_table(TABLESTYLE);
573         table_header($th);
574         $k = 0; //row colour counter
575         while ($myrow = db_fetch($result))
576         {
577                 alt_table_row_color($k);
578                 label_cell(get_trans_view_str(ST_SUPPINVOICE, $myrow["trans_no"]));
579                 label_cell($myrow['reference']);
580                 label_cell(sql2date($myrow['tran_date']));
581                 label_cell(sql2date($myrow['due_date']));
582                 $name = $myrow["supplier_id"]." ".$myrow["supp_name"];
583                 label_cell($name);
584                 label_cell($myrow['curr_code']);
585                 amount_cell($myrow['total']);
586                 amount_cell($myrow['remainder']);
587                 label_cell($myrow['days'], "align='right'");
588                 end_row();
589         }
590         end_table(2);
591 }
592
593 function bank_balance($today, $width)
594 {
595         $today = date2sql($today);
596         $sql = "SELECT bank_act, bank_account_name, bank_curr_code, SUM(amount) balance FROM ".TB_PREF."bank_trans bt 
597                     INNER JOIN ".TB_PREF."bank_accounts ba ON bt.bank_act = ba.id
598                     WHERE trans_date <= '$today'
599                     AND inactive <> 1
600                     GROUP BY bank_act, bank_account_name
601                                 ORDER BY bank_account_name";
602         $result = db_query($sql);
603         $title = _("Bank Account Balances");
604         display_title($title);
605         $th = array(_("Account"), _("Currency"), _("Balance"));
606         start_table(TABLESTYLE, "width='$width%'");
607         table_header($th);
608         $k = 0; //row colour counter
609         while ($myrow = db_fetch($result))
610         {
611                 alt_table_row_color($k);
612                 label_cell(viewer_link($myrow["bank_account_name"], 'gl/inquiry/bank_inquiry.php?bank_account='.$myrow["bank_act"]));
613                 label_cell($myrow["bank_curr_code"]);
614                 amount_cell($myrow['balance']);
615                 end_row();
616         }
617         end_table(1);
618 }