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