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