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