Improved reports menu display.
[fa-stable.git] / reporting / includes / reports_classes.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 // Standard report classess
14 define('RC_CUSTOMER', 0);
15 define('RC_SUPPLIER', 1);
16 define('RC_INVENTORY', 2);
17 define('RC_MANUFACTURE', 3);
18 define('RC_DIMENSIONS', 4);
19 define('RC_BANKING', 5);
20 define('RC_GL', 6);
21
22 class BoxReports
23 {
24         var $ar_classes; // report class names
25         var $ar_reports;
26         var $ctrl_handlers = array();
27
28         function ReportClasses()
29         {
30                 $this->ar_classes = array();
31         }
32
33         function addReportClass($class_name, $class_id=null)
34         {
35                 if (!$class_id)
36                         $class_id = count($this->ar_classes);
37                 $this->ar_classes[$class_id] = $class_name;
38
39                 return $class_id;
40         }
41
42         function addReport($class, $id, $rep_name, $params=null)
43         {
44                 unset($this->ar_reports[$class][$id]); // unset std report if any
45                 $this->ar_reports[$class][$id] = new Report($id, $rep_name, $params);
46         }
47
48         function getDisplay($class=null)
49         {
50                 global $path_to_root, $pdf_debug, $Ajax;
51
52
53                 $temp = array_values($this->ar_classes);
54                 $display_class = $class==null ? $temp[0] : $this->ar_classes[$class];
55                 $st_reports = "";
56                 $st_params = "";
57                 $st_classes = "<b>" . _("Report Classes:") . "</b><br>";
58
59                 if (isset($_GET['Class']))
60                         set_focus('class'.$_GET['Class']);
61
62                 foreach($this->ar_classes as $class_id => $name)
63                 {
64                         if (!isset($this->ar_reports[$class_id]))
65                                 continue; // e.g. no dimensions
66
67                         $acc = access_string($name);
68                         $st_classes .= "<a href='"
69                                 .$_SERVER['PHP_SELF']."?Class=$class_id'"
70                                 ." style='font-weight:". ($_REQUEST['Class'] == $class_id ? 'bold' : 'normal')."'"
71                                 ." class='repclass_link' id='".'class'.$class_id."'"
72                                 ."$acc[1]>$acc[0]</a> <br>";
73
74                         $style = $class_id==$_REQUEST['Class'] ? '' : "style='display:none'";
75                         $st_reports .= "<table class='repclass' id='TAB_" . $class_id ."' $style cellpadding=0 cellspacing=0 border=0 width='100%'><tr><td><b>" . _("Reports For Class: ") . "&nbsp;$name</b></td></tr>\n";
76                         foreach($this->ar_reports[$class_id] as $rep_id => $report)
77                         {
78                                 $acc = access_string($report->name);
79                                 $st_reports .= "<tr><td><a class='repopts_link'"
80                                         ." href='".$_SERVER['PHP_SELF']."?Class=$class_id&rep_id=$report->id'"
81                                         ." style='font-weight:". (@$_GET['rep_id'] == $report->id ? 'bold' : 'normal')."'"
82                                         ." id='".$id = default_focus()."'"
83                                         ."$acc[1]>$acc[0]</a><tr><td>\n";
84
85                                 if (@$_REQUEST['rep_id'] == $report->id) {
86                                         $Ajax->activate($id);
87                                 }
88
89                                 if (isset($_REQUEST['rep_id']) && $_REQUEST['rep_id']==$report->id) {
90
91                                         $action = $path_to_root.'/reporting/prn_redirect.php';
92         
93                                         $st_params = "<table><tr><td>\n"
94                                                 . "<form method='POST' action='$action' target='_blank'>\n";
95                                         $st_params .= submit('Rep'.$report->id,  
96                                                 _("Display: ") . access_string($report->name, true),
97                                                 false, '', $pdf_debug ? false : 'default process') . hidden('REP_ID', $report->id, false).'<br><br>';
98                                         $st_params .= $this->getOptions($report->get_controls(), $report->id);
99                                         $st_params .= "\n</form></td></tr></table>\n";
100                                         set_focus('Rep'.$report->id);
101                                         $Ajax->addUpdate(true, 'rep_form', $st_params);
102                                 }
103                         }
104                         $st_reports .= "</table>";
105                 }
106
107                 $st_params = "<div id='rep_form'>".
108                         "$st_params</div>";
109                 
110                 $st = "<table align='center' width='80%' style='border:1px solid #cccccc;'><tr valign='top'>";
111                 $st .= "<td width='30%'>$st_classes</td>";
112                 $st .= "<td width='35%' style='border-left:1px solid #cccccc;border-right:1px solid #cccccc;padding-left:3px;'>$st_reports</td>";
113                 $st .= "<td width='35%'>$st_params</td>";
114                 $st .= "</tr></table><br>";
115
116                 return $st;
117         }
118
119         function getOptions($controls, $id = 0)
120         {
121                 $st = '';
122                 if ($controls == null)
123                         return "";
124
125                 $cnt = 0;
126                 foreach($controls as $title => $type)
127                 {
128                         $ctrl = '';
129                         foreach($this->ctrl_handlers as $fun) { // first check for non-standard controls
130                                 $ctrl = $fun('PARAM_'.$cnt, $type);
131                                 if ($ctrl) break;
132                         }
133                         if ($ctrl == '') {
134                                 if (isset($_COOKIE['select'][$id][$cnt])) // saved parameters 2010-10-06 Joe Hunt
135                                         $_POST['PARAM_'.$cnt] = $_COOKIE['select'][$id][$cnt];
136                                 $ctrl = $this->get_ctrl('PARAM_'.$cnt, $type);
137                         }
138                         if ($ctrl != '')  {
139                                 $st .= $title . ':<br>';
140                                 $st .= $ctrl;
141                                 $st .= "<br><br>";
142                         } else {
143                                 display_error(_('Unknown report parameter type:').$type);
144                         }
145                         $cnt++;
146                 }
147                 return $st;
148         }
149
150         //
151         //      Register additional control handler
152         // $handle - name of global function f($name, $type) returning html code for control
153         function register_controls($handler) {
154                 $this->ctrl_handlers[] = $handler;
155         }
156         //
157         //      Returns html code for input control $name of type $type 
158         //
159         function get_ctrl($name, $type)
160         {
161                 global $path_to_root, $use_date_picker, $pdf_debug, $print_invoice_no, $def_print_destination;
162
163                 $st = '';
164                         switch ($type)
165                         {
166                                 case 'CURRENCY':
167                                         $sql = "SELECT curr_abrev, concat(curr_abrev,' - ', currency) AS name FROM ".TB_PREF."currencies";
168                                         return combo_input($name, '', $sql, 'curr_abrev', 'name',array('spec_option'=>_("No Currency Filter"),'spec_id'=>ALL_TEXT,'order'=>false));
169
170                                 case 'DATE':
171                                 case 'DATEBEGIN':
172                                 case 'DATEEND':
173                                 case 'DATEBEGINM':
174                                 case 'DATEENDM':
175                                 case 'DATEBEGINTAX':
176                                 case 'DATEENDTAX':
177                                         if (!isset($_POST[$name]))
178                                         {
179                                                 if ($type == 'DATEBEGIN')
180                                                         $date = begin_fiscalyear();
181                                                 elseif ($type == 'DATEEND')
182                                                         $date = end_fiscalyear();
183                                                 else
184                                                         $date = Today();
185                                                 if ($type == 'DATEBEGINM')
186                                                         $date = begin_month($date);
187                                                 elseif ($type == 'DATEENDM')
188                                                         $date = end_month($date);
189                                                 elseif ($type == 'DATEBEGINTAX' || $type == 'DATEENDTAX')
190                                                 {
191                                                         $row = get_company_prefs();
192                                                         $edate = add_months($date, -$row['tax_last']);
193                                                         $edate = end_month($edate);
194                                                         if ($type == 'DATEENDTAX')
195                                                                 $date = $edate;
196                                                         else
197                                                         {
198                                                                 $bdate = begin_month($edate);
199                                                                 $bdate = add_months($bdate, -$row['tax_prd'] + 1);
200                                                                 $date = $bdate;
201                                                         }
202                                                 }
203                                         }
204                                         else // saved parameters 2010-10-16 Joe Hunt
205                                                 $date = $_POST[$name];
206                                         $st = "<input type='text' name='$name' value='$date'>";
207                                         if ($use_date_picker)
208                                                 $st .= "<a href=\"javascript:date_picker(document.forms[0].$name);\">"
209                                                 . "     <img src='$path_to_root/themes/default/images/cal.gif' width='16' height='16' border='0' alt='"._('Click Here to Pick up the date')."'></a>\n";
210                                         return $st;
211                                         break;
212
213                                 case 'YES_NO':
214                                         return yesno_list($name);
215
216                                 case 'PAYMENT_LINK':
217                                         $sel = array(_("No payment Link"), "PayPal");
218                                         return array_selector($name, null, $sel);
219
220                                 case 'DESTINATION':
221                                         $sel = array(_("PDF/Printer"), "Excel");
222                                         $def = 0;
223                                         if (isset($def_print_destination) && $def_print_destination == 1)
224                                                 $def = 1;
225                                         return array_selector($name, $def, $sel);
226
227                                 case 'COMPARE':
228                                         $sel = array(_("Accumulated"), _("Period Y-1"), _("Budget"));
229                                         return array_selector($name, null, $sel);
230
231                                 case 'GRAPHIC':
232                                         $sel = array(_("No Graphics"), _("Vertical bars"), _("Horizontal bars"), 
233                                                                 _("Dots"), _("Lines"), _("Pie"), _("Donut"));
234                                         return array_selector($name, null, $sel);
235
236                                 case 'SYS_TYPES':
237                                         return gl_systypes_list($name, null, _("No Type Filter"));
238
239                                 case 'SYS_TYPES_ALL':
240                                         return systypes_list($name, null, _("No Type Filter"));
241
242                                 case 'TEXT':
243                                         return "<input type='text' name='$name'>";
244
245                                 case 'TEXTBOX':
246                                         $value = (isset($_POST[$name]) ? $_POST[$name] : "");
247                                         return "<textarea rows=4 cols=30 name='$name'>$value</textarea>";
248
249                                 case 'ACCOUNTS': // not used
250                                         return gl_account_types_list($name, null, _("No Account Group Filter"), true);
251
252                                 case 'ACCOUNTS_NO_FILTER': // not used
253                                         return gl_account_types_list($name);
254
255                                 case 'GL_ACCOUNTS':
256                                         return gl_all_accounts_list($name);
257
258                                 case 'BANK_ACCOUNTS':
259                                         return bank_accounts_list($name);
260                                         
261                                 case 'DIMENSION':
262                                         return dimensions_list($name, null, false, ' ', false, true, 0);
263                                         
264                                 case 'DIMENSIONS':
265                                         return dimensions_list($name, null, true, _("No Dimension Filter"), false, true, 0);
266
267                                 case 'DIMENSION1':
268                                         return dimensions_list($name, null, false, ' ', false, true, 1);
269
270                                 case 'DIMENSIONS1':
271                                         return dimensions_list($name, null, true, _("No Dimension Filter"), false, true, 1);
272
273                                 case 'DIMENSION2':
274                                         return dimensions_list($name, null, false, ' ', false, true, 2);
275
276                                 case 'DIMENSIONS2':
277                                         return dimensions_list($name, null, true, _("No Dimension Filter"), false, true, 2);
278
279                                 case 'CUSTOMERS_NO_FILTER':
280                                 case 'CUSTOMERS':
281                                         $sql = "SELECT debtor_no, name FROM ".TB_PREF."debtors_master";
282                                         if ($type == 'CUSTOMERS_NO_FILTER')
283                                                 return combo_input($name, '', $sql, 'debtor_no', 'name',array('spec_option'=>_("No Customer Filter"),'spec_id'=>ALL_NUMERIC));
284 // FIX allitems numeric!
285 //                                              return customer_list($name, null, _("No Customer Filter"));
286                                         else
287                                                 return combo_input($name, '', $sql, 'debtor_no', 'name', null);
288 //                                              return customer_list($name);
289
290                                 case 'SUPPLIERS_NO_FILTER':
291                                 case 'SUPPLIERS':
292                                         $sql = "SELECT supplier_id, supp_name FROM ".TB_PREF."suppliers";
293                                         if ($type == 'SUPPLIERS_NO_FILTER')
294                                                 return combo_input($name, '', $sql, 'supplier_id', 'supp_name',array('spec_option'=>_("No Supplier Filter"),'spec_id'=>ALL_NUMERIC));
295 // FIX allitems numeric!
296 //                                              return supplier_list($name, null, _("No Supplier Filter"));
297                                         else
298                                                 return combo_input($name, '', $sql, 'supplier_id', 'supp_name', null);
299 //                                              return supplier_list($name);
300
301                                 case 'INVOICE':
302                                         $IV = _("IV");
303                                         $CN = _("CN");
304                                         $ref = ($print_invoice_no == 1 ? "trans_no" : "reference");
305                                         $sql = "SELECT concat(".TB_PREF."debtor_trans.trans_no, '-',
306                                                 ".TB_PREF."debtor_trans.type) AS TNO, concat(".TB_PREF."debtor_trans.$ref, if (type=".ST_SALESINVOICE.", ' $IV ', ' $CN '), ".TB_PREF."debtors_master.name) as IName
307                                                 FROM ".TB_PREF."debtors_master, ".TB_PREF."debtor_trans WHERE (type=".ST_SALESINVOICE." OR type=".ST_CUSTCREDIT.") AND ".TB_PREF."debtors_master.debtor_no=".TB_PREF."debtor_trans.debtor_no ORDER BY ".TB_PREF."debtor_trans.trans_no DESC";
308                                         return combo_input($name, '', $sql, 'TNO', 'IName',array('order'=>false));
309
310                                 case 'DELIVERY':
311                                         $DN = _("DN");
312                                         $sql = "SELECT
313                                         concat(".TB_PREF."debtor_trans.trans_no, '-', ".TB_PREF."debtor_trans.type) AS TNO, concat(".TB_PREF."debtor_trans.trans_no, ' $DN ',
314                                          ".TB_PREF."debtors_master.name) as IName
315                                                 FROM ".TB_PREF."debtors_master, ".TB_PREF."debtor_trans
316                                                 WHERE type=".ST_CUSTDELIVERY." AND ".TB_PREF."debtors_master.debtor_no=".
317                                                 TB_PREF."debtor_trans.debtor_no ORDER BY ".TB_PREF."debtor_trans.trans_no DESC";
318                                         return combo_input($name, '', $sql, 'TNO', 'IName',array('order'=>false));
319
320                                 case 'ORDERS':
321                                         $ref = ($print_invoice_no == 1 ? "order_no" : "reference");
322                                         $sql = "SELECT ".TB_PREF."sales_orders.order_no, concat(".TB_PREF."sales_orders.$ref, '-',
323                                                 ".TB_PREF."debtors_master.name) as IName
324                                                 FROM ".TB_PREF."debtors_master, ".TB_PREF."sales_orders WHERE ".TB_PREF."debtors_master.debtor_no=".TB_PREF."sales_orders.debtor_no 
325                                                 AND ".TB_PREF."sales_orders.trans_type=".ST_SALESORDER." ORDER BY ".TB_PREF."sales_orders.order_no DESC";
326                                         return combo_input($name, '', $sql, 'order_no', 'IName',array('order'=>false));
327
328                                 case 'QUOTATIONS':
329                                         $ref = ($print_invoice_no == 1 ? "order_no" : "reference");
330                                         $sql = "SELECT ".TB_PREF."sales_orders.order_no, concat(".TB_PREF."sales_orders.$ref, '-',
331                                                 ".TB_PREF."debtors_master.name) as IName
332                                                 FROM ".TB_PREF."debtors_master, ".TB_PREF."sales_orders WHERE ".TB_PREF."debtors_master.debtor_no=".TB_PREF."sales_orders.debtor_no 
333                                                 AND ".TB_PREF."sales_orders.trans_type=".ST_SALESQUOTE." ORDER BY ".TB_PREF."sales_orders.order_no DESC";
334                                         return combo_input($name, '', $sql, 'order_no', 'IName',array('order'=>false));
335
336                                 case 'PO':
337                                         $ref = ($print_invoice_no == 1 ? "order_no" : "reference");
338                                         $sql = "SELECT ".TB_PREF."purch_orders.order_no, concat(".TB_PREF."purch_orders.$ref, '-',
339                                                 ".TB_PREF."suppliers.supp_name) as IName
340                                                 FROM ".TB_PREF."suppliers, ".TB_PREF."purch_orders WHERE ".TB_PREF."suppliers.supplier_id=".TB_PREF."purch_orders.supplier_id ORDER BY ".TB_PREF."purch_orders.order_no DESC";
341                                         return combo_input($name, '', $sql, 'order_no', 'IName',array('order'=>false));
342
343                                 case 'REMITTANCE':
344                                         $BP = _("BP");
345                                         $SP = _("SP");
346                                         $CN = _("CN");
347                                         $ref = ($print_invoice_no == 1 ? "trans_no" : "reference");
348                                         $sql = "SELECT concat(".TB_PREF."supp_trans.trans_no, '-',
349                                                 ".TB_PREF."supp_trans.type) AS TNO, concat(".TB_PREF."supp_trans.$ref, if (type=".ST_BANKPAYMENT.", ' $BP ', if (type=".ST_SUPPAYMENT.", ' $SP ', ' $CN ')), ".TB_PREF."suppliers.supp_name) as IName
350                                                 FROM ".TB_PREF."suppliers, ".TB_PREF."supp_trans WHERE (type=".ST_BANKPAYMENT." OR type=".ST_SUPPAYMENT." OR type=".ST_SUPPCREDIT.") AND ".TB_PREF."suppliers.supplier_id=".TB_PREF."supp_trans.supplier_id ORDER BY ".TB_PREF."supp_trans.trans_no DESC";
351                                         return combo_input($name, '', $sql, 'TNO', 'IName',array('order'=>false));
352
353                                 case 'RECEIPT':
354                                         $BD = _("BD");
355                                         $CP = _("CP");
356                                         $CN = _("CN");
357                                         $ref = ($print_invoice_no == 1 ? "trans_no" : "reference");
358                                         $sql = "SELECT concat(".TB_PREF."debtor_trans.trans_no, '-',
359                                                 ".TB_PREF."debtor_trans.type) AS TNO, concat(".TB_PREF."debtor_trans.$ref, if (type=".ST_BANKDEPOSIT.", ' $BD ', if (type=".ST_CUSTPAYMENT.", ' $CP ', ' $CN ')), ".TB_PREF."debtors_master.name) as IName
360                                                 FROM ".TB_PREF."debtors_master, ".TB_PREF."debtor_trans WHERE (type=".ST_BANKDEPOSIT." OR type=".ST_CUSTPAYMENT." OR type=".ST_CUSTCREDIT.") AND ".TB_PREF."debtors_master.debtor_no=".TB_PREF."debtor_trans.debtor_no ORDER BY ".TB_PREF."debtor_trans.trans_no DESC";
361                                         return combo_input($name, '', $sql, 'TNO', 'IName',array('order'=>false));
362
363                                 case 'ITEMS':
364                                         return stock_manufactured_items_list($name);
365
366                                 case 'WORKORDER':
367                                         $sql = "SELECT ".TB_PREF."workorders.id, concat(".TB_PREF."workorders.id, '-',
368                                                 ".TB_PREF."stock_master.description) as IName
369                                                 FROM ".TB_PREF."stock_master, ".TB_PREF."workorders WHERE ".TB_PREF."stock_master.stock_id=".TB_PREF."workorders.stock_id ORDER BY ".TB_PREF."workorders.id DESC";
370                                         return combo_input($name, '', $sql, 'id', 'IName',array('order'=>false));
371
372                                 case 'LOCATIONS':
373                                         return  locations_list($name, null, _("No Location Filter"));
374
375                                 case 'CATEGORIES':
376                                         return stock_categories_list($name, null, _("No Category Filter"));
377
378                                 case 'SALESTYPES':
379                                         return sales_types_list($name);
380
381                                 case 'AREAS':
382                                         return sales_areas_list($name);
383
384                                 case 'SALESMEN':
385                                         return sales_persons_list($name, null, _("No Sales Folk Filter"));
386                                         
387                                 case 'TRANS_YEARS':
388                                         return fiscalyears_list($name);
389
390                                 case 'USERS':
391                                         $sql = "SELECT id, user_id FROM ".TB_PREF."users";
392                                         return combo_input($name, '', $sql, 'id', 'user_id',array('spec_option'=>_("No Users Filter"),'spec_id'=>ALL_NUMERIC));
393
394                                 case 'ACCOUNTTAGS':
395                                 case 'DIMENSIONTAGS':
396                                         if ($type == 'ACCOUNTTAGS')
397                                                 $tag_type = TAG_ACCOUNT;
398                                         else
399                                                 $tag_type = TAG_DIMENSION;
400                                         //return tag_list($name, 5, $tag_type, true, false, _("No tags"));
401                                         return tag_list($name, 5, $tag_type, true);
402
403                         }
404                 return '';
405         }
406 }
407
408 class Report
409 {
410         var $id;
411         var $name;
412         var $ar_params;
413         var $controls;
414         
415         function Report($id, $name, $ar_params = null)
416         {
417                 $this->id = $id;
418                 $this->name = $name;
419                 if ($ar_params) $this->set_controls($ar_params);
420         }
421         
422         function set_controls($ar_params) {
423                 $this->controls = $ar_params;
424         }
425         
426         function get_controls() {
427                 return $this->controls;
428         }
429         
430 }
431
432 //------------------------------------------------------------------------------------------------
433 function gl_systypes_list($name, $value=null, $spec_opt=false)
434 {
435         global $systypes_array;
436         
437         $types = $systypes_array;
438         
439         foreach(array(ST_LOCTRANSFER, ST_PURCHORDER, ST_SUPPRECEIVE, ST_MANUISSUE,
440                                 ST_MANURECEIVE, ST_SALESORDER, ST_SALESQUOTE, ST_DIMENSION) as $type)
441                 unset($types[$type]);
442
443         return array_selector($name, $value, $types, 
444                 array( 
445                         'spec_option'=> $spec_opt,
446                         'spec_id' => ALL_NUMERIC,
447                         'async' => false,
448                         )
449         );
450 }
451 /*
452         Add extension/custom reports to report manager.
453 */
454 function add_custom_reports(&$reports)
455 {
456         global $installed_extensions, $path_to_root;
457         
458         // include reports installed inside extension modules
459         if (count($installed_extensions) > 0)
460         {
461                 $extensions = $installed_extensions;
462                 foreach ($extensions as $ext)
463                         if (($ext['active'] && $ext['type'] == 'extension')) {
464                                 $file = $path_to_root.'/'.$ext['path']."/reporting/reports_custom.php";
465                                 if (file_exists($file)) {
466                                         set_ext_domain($ext['path']);
467                                         include_once($file);
468                                         set_ext_domain();
469                                 }
470                         }
471         }
472         $file = company_path()."/reporting/reports_custom.php";
473         if (file_exists($file))
474                 include_once($file);
475 }
476
477 ?>