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