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