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