96134f95e21c05f6949f574566c5fed8938dd1f2
[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 define('RC_FIXEDASSETS', 7);
22
23 class BoxReports
24 {
25         var $ar_classes; // report class names
26         var $ar_reports;
27         var $ctrl_handlers = array();
28
29         function __construct()
30         {
31                 $this->ar_classes = array();
32         }
33
34         function addReportClass($class_name, $class_id=null)
35         {
36                 if (!$class_id)
37                         $class_id = count($this->ar_classes);
38                 $this->ar_classes[$class_id] = $class_name;
39
40                 return $class_id;
41         }
42
43         function addReport($class, $id, $rep_name, $params=null)
44         {
45                 unset($this->ar_reports[$class][$id]); // unset std report if any
46                 $this->ar_reports[$class][$id] = new Report($id, $rep_name, $params);
47         }
48
49         function getDisplay($class=null)
50         {
51                 global $path_to_root, $SysPrefs, $Ajax;
52
53                 if (find_submit('Rep') != -1) {
54                         include($path_to_root.'/reporting/prn_redirect.php');
55                         return;
56                 }
57
58                 $temp = array_values($this->ar_classes);
59                 $display_class = $class==null ? $temp[0] : $this->ar_classes[$class];
60                 $st_reports = "";
61                 $st_params = "";
62                 $st_classes = "<b>" . _("Report Classes:") . "</b><br>";
63
64                 if (isset($_GET['Class']))
65                         set_focus('class'.$_GET['Class']);
66
67                 $cur_class = $_REQUEST['Class'];
68
69                 foreach($this->ar_classes as $class_id => $name)
70                 {
71                         if (!isset($this->ar_reports[$class_id]))
72                                 continue; // e.g. no dimensions
73
74                         $acc = access_string($name);
75                         $st_classes .= "<a href='"
76                                 .$_SERVER['PHP_SELF']."?Class=$class_id'"
77                                 ." style='font-weight:". ($cur_class == $class_id ? 'bold' : 'normal')."'"
78                                 ." class='repclass_link' id='".'class'.$class_id."'"
79                                 ."$acc[1]>$acc[0]</a> <br>";
80
81                         $style = $class_id==$cur_class ? '' : "style='display:none'";
82                         $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";
83                         foreach($this->ar_reports[$class_id] as $rep_id => $report)
84                         {
85                                 $acc = access_string($report->name);
86                                 $st_reports .= "<tr><td><a class='repopts_link'"
87                                         ." href='".$_SERVER['PHP_SELF']."?Class=$class_id&REP_ID=$report->id'"
88                                         ." style='font-weight:". (@$_REQUEST['REP_ID'] == $report->id ? 'bold' : 'normal')."'"
89                                         ." id='".$id = default_focus()."'"
90                                         ."$acc[1]>$acc[0]</a><tr><td>\n";
91
92                                 if (@$_REQUEST['REP_ID'] == $report->id) {
93                                         $Ajax->activate($id);
94                                 }
95
96                                 if (isset($_REQUEST['REP_ID']) && $_REQUEST['REP_ID']==$report->id) {
97                                         // parameters form
98                                         $action = $_SERVER['PHP_SELF'];
99                                         $st_params = "<table><tr><td>\n"
100                                                 . "<form method='POST' action='$action' target='_blank'>\n";
101                                         $st_params .= submit('Rep'.$report->id,  
102                                                 _("Display: ") . access_string($report->name, true),
103                                                 false, '', $SysPrefs->pdf_debug ? false : 'default process') . hidden('REP_ID', $report->id, false).'<br><br>';
104                                         $st_params .= $this->getOptions($report->get_controls(), $report->id);
105                                         $st_params .= "\n<input type=hidden name='Class' value=".$cur_class.">"
106                                                 ."\n</form></td></tr></table>\n";
107                                         set_focus('Rep'.$report->id);
108                                         $Ajax->addUpdate(true, 'rep_form', $st_params);
109                                 }
110                         }
111                         $st_reports .= "</table>";
112                 }
113
114                 $st_params = "<div id='rep_form'>".
115                         "$st_params</div>";
116                 
117                 $st = "<table align='center' width='80%' style='border:1px solid #cccccc;'><tr valign='top'>";
118                 $st .= "<td width='30%'>$st_classes</td>";
119                 $st .= "<td width='35%' style='border-left:1px solid #cccccc;border-right:1px solid #cccccc;padding-left:3px;'>$st_reports</td>";
120                 $st .= "<td width='35%'>$st_params</td>";
121                 $st .= "</tr></table><br>";
122
123                 return $st;
124         }
125
126         function getOptions($controls, $id = 0)
127         {
128                 $st = '';
129                 if ($controls == null)
130                         return "";
131
132                 $cnt = 0;
133                 foreach($controls as $title => $type)
134                 {
135                         $ctrl = '';
136                         foreach($this->ctrl_handlers as $fun) { // first check for non-standard controls
137                                 $ctrl = $fun('PARAM_'.$cnt, $type);
138                                 if ($ctrl) break;
139                         }
140                         if ($ctrl == '') {
141                                 if (($id == 102 || $id == 202) && $cnt == 3) // set default Show Also Allocated to Yes in aged reports 2014-09-19 Joe Hunt
142                                         $_POST['PARAM_'.$cnt] = 1;
143                                 
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, $SysPrefs,
172                         $type_shortcuts;
173
174                 $st = '';
175                         switch ($type)
176                         {
177                                 case 'CURRENCY':
178                                         $sql = "SELECT curr_abrev, concat(curr_abrev,' - ', currency) AS name FROM ".TB_PREF."currencies";
179                                         return combo_input($name, '', $sql, 'curr_abrev', 'name',array('spec_option'=>_("No Currency Filter"),'spec_id'=>ALL_TEXT,'order'=>false));
180
181                                 case 'DATE':
182                                 case 'DATEBEGIN':
183                                 case 'DATEEND':
184                                 case 'DATEBEGINM':
185                                 case 'DATEENDM':
186                                 case 'DATEBEGINTAX':
187                                 case 'DATEENDTAX':
188                                         if (!isset($_POST[$name]))
189                                         {
190                                                 if ($type == 'DATEBEGIN')
191                                                         $date = begin_fiscalyear();
192                                                 elseif ($type == 'DATEEND')
193                                                         $date = end_fiscalyear();
194                                                 else
195                                                         $date = Today();
196                                                 if ($type == 'DATEBEGINM')
197                                                         $date = begin_month($date);
198                                                 elseif ($type == 'DATEENDM')
199                                                         $date = end_month($date);
200                                                 elseif ($type == 'DATEBEGINTAX' || $type == 'DATEENDTAX')
201                                                 {
202                                                         $row = get_company_prefs();
203                                                         $edate = add_months($date, -$row['tax_last']);
204                                                         $edate = end_month($edate);
205                                                         if ($type == 'DATEENDTAX')
206                                                                 $date = $edate;
207                                                         else
208                                                         {
209                                                                 $bdate = begin_month($edate);
210                                                                 $bdate = add_months($bdate, -$row['tax_prd'] + 1);
211                                                                 $date = $bdate;
212                                                         }
213                                                 }
214                                         }
215                                         else // saved parameters
216                                                 $date = $_POST[$name];
217                                         $st = "<input type='text' name='$name' value='$date'>";
218                                         if (user_use_date_picker())
219                                         {
220                                                 $calc_image = (file_exists("$path_to_root/themes/".user_theme()."/images/cal.gif")) ? 
221                                                         "$path_to_root/themes/".user_theme()."/images/cal.gif" : "$path_to_root/themes/default/images/cal.gif";
222                                                 $st .= "<a href=\"javascript:date_picker(document.forms[0].$name);\">"
223                                                 . "     <img src='$calc_image' style='vertical-align:middle;padding-bottom:4px;width:16px;height:16px;border:0;' alt='"._('Click Here to Pick up the date')."'></a>\n";
224                                         }       
225                                         return $st;
226                                         break;
227
228                                 case 'YES_NO':
229                                         return yesno_list($name);
230
231                                 case 'PAYMENT_LINK':
232                                         return payment_services($name, null);
233
234                                 case 'DESTINATION':
235                                         $sel = array(_("PDF/Printer"), "Excel");
236                                         $def = 0;
237                                         if (user_def_print_destination() == 1)
238                                                 $def = 1;
239                                         return array_selector($name, $def, $sel);
240
241                                 case 'ORIENTATION':
242                                         $sel = array(_("Portrait"), _("Landscape"));
243                                         $def = 0;
244                                         if (user_def_print_orientation() == 1)
245                                                 $def = 1;
246                                         return array_selector($name, $def, $sel);
247
248                                 case 'COMPARE':
249                                         $sel = array(_("Accumulated"), _("Period Y-1"), _("Budget"));
250                                         return array_selector($name, null, $sel);
251
252                                 case 'GRAPHIC':
253                                         $sel = array(_("No Graphics"), _("Vertical bars"), _("Horizontal bars"), 
254                                                                 _("Dots"), _("Lines"), _("Pie"), _("Donut"));
255                                         return array_selector($name, null, $sel);
256
257                                 case 'SYS_TYPES':
258                                         return gl_systypes_list($name, null, _("No Type Filter"));
259
260                                 case 'SYS_TYPES_ALL':
261                                         return systypes_list($name, null, _("No Type Filter"));
262
263                                 case 'TEXT':
264                                         return "<input type='text' name='$name'>";
265
266                                 case 'TEXTBOX':
267                                         $value = (isset($_POST[$name]) ? $_POST[$name] : "");
268                                         return "<textarea rows=4 cols=30 maxlength=130 name='$name'>$value</textarea>";
269
270                                 case 'ACCOUNTS': // not used
271                                         return gl_account_types_list($name, null, _("No Account Group Filter"), true);
272
273                                 case 'ACCOUNTS_NO_FILTER': // not used
274                                         return gl_account_types_list($name);
275
276                                 case 'GL_ACCOUNTS':
277                                         return gl_all_accounts_list($name);
278
279                                 case 'BANK_ACCOUNTS':
280                                         return bank_accounts_list($name);
281                                         
282                                 case 'DIMENSION':
283                                         return dimensions_list($name, null, false, ' ', false, true, 0);
284                                         
285                                 case 'DIMENSIONS':
286                                         return dimensions_list($name, null, true, _("No Dimension Filter"), false, true, 0);
287
288                                 case 'DIMENSION1':
289                                         return dimensions_list($name, null, false, ' ', false, true, 1);
290
291                                 case 'DIMENSIONS1':
292                                         return dimensions_list($name, null, true, _("No Dimension Filter"), false, true, 1);
293
294                                 case 'DIMENSION2':
295                                         return dimensions_list($name, null, false, ' ', false, true, 2);
296
297                                 case 'DIMENSIONS2':
298                                         return dimensions_list($name, null, true, _("No Dimension Filter"), false, true, 2);
299
300                                 case 'CUSTOMERS_NO_FILTER':
301                                 case 'CUSTOMERS':
302                                         $sql = "SELECT debtor_no, name FROM ".TB_PREF."debtors_master";
303                                         if ($type == 'CUSTOMERS_NO_FILTER')
304                                                 return customer_list($name, null, _("No Customer Filter"));
305                                         else
306                                                 return customer_list($name);
307
308                                 case 'SUPPLIERS_NO_FILTER':
309                                 case 'SUPPLIERS':
310                                         $sql = "SELECT supplier_id, supp_name FROM ".TB_PREF."suppliers";
311                                         if ($type == 'SUPPLIERS_NO_FILTER')
312                                                 return supplier_list($name, null, _("No Supplier Filter"));
313                                         else
314                                                 return supplier_list($name);
315
316                                 case 'INVOICE':
317                                         $IV = $type_shortcuts[ST_SALESINVOICE];
318                                         $ref = ($SysPrefs->print_invoice_no() == 1 ? "trans_no" : "reference");
319                                         $sql = "SELECT concat(debtor_trans.trans_no, '-', debtor_trans.type) AS TNO,
320                                                                 concat('$IV ', debtor_trans.$ref,' ', debtor.name) as IName
321                                                 FROM ".TB_PREF."debtors_master debtor,"
322                                                         .TB_PREF."debtor_trans debtor_trans
323                                                 WHERE type=".ST_SALESINVOICE." AND debtor.debtor_no=debtor_trans.debtor_no
324                                                 ORDER BY debtor_trans.trans_no DESC";
325                                         return combo_input($name, '', $sql, 'TNO', 'IName',array('order'=>false));
326
327                                 case 'CREDIT':
328                                         $CN = $type_shortcuts[ST_CUSTCREDIT];
329                                         $ref = ($SysPrefs->print_invoice_no() == 1 ? "trans_no" : "reference");
330                                         $sql = "SELECT concat(debtor_trans.trans_no, '-', debtor_trans.type) AS TNO,
331                                                                 concat('$CN ', debtor_trans.$ref,' ', debtor.name) as IName
332                                                 FROM ".TB_PREF."debtors_master debtor,"
333                                                         .TB_PREF."debtor_trans debtor_trans
334                                                 WHERE type=".ST_CUSTCREDIT." AND debtor.debtor_no=debtor_trans.debtor_no
335                                                 ORDER BY debtor_trans.trans_no DESC";
336                                         return combo_input($name, '', $sql, 'TNO', 'IName',array('order'=>false));
337
338                                 case 'DELIVERY':
339                                         $DN = $type_shortcuts[ST_CUSTDELIVERY];
340                                         $sql = "SELECT  concat(debtor_trans.trans_no, '-', debtor_trans.type) AS TNO,
341                                                                 concat(debtor_trans.trans_no, ' $DN ', debtor.name) as IName
342                                                 FROM ".TB_PREF."debtors_master debtor,"
343                                                         .TB_PREF."debtor_trans debtor_trans
344                                                 WHERE type=".ST_CUSTDELIVERY." AND debtor.debtor_no=debtor_trans.debtor_no
345                                                 ORDER BY debtor_trans.trans_no DESC";
346                                         return combo_input($name, '', $sql, 'TNO', 'IName',array('order'=>false));
347
348                                 case 'ORDERS':
349                                         $ref = ($SysPrefs->print_invoice_no() == 1 ? "order_no" : "reference");
350                                         $sql = "SELECT sorder.order_no, concat(sorder.$ref, '-', debtor.name) as IName
351                                                 FROM ".TB_PREF."debtors_master debtor,"
352                                                         .TB_PREF."sales_orders sorder
353                                                 WHERE debtor.debtor_no=sorder.debtor_no AND sorder.trans_type=".ST_SALESORDER
354                                                 ." ORDER BY sorder.order_no DESC";
355                                         return combo_input($name, '', $sql, 'order_no', 'IName',array('order'=>false));
356
357                                 case 'QUOTATIONS':
358                                         $ref = ($SysPrefs->print_invoice_no() == 1 ? "order_no" : "reference");
359                                         $sql = "SELECT sorder.order_no, concat(sorder.$ref, '-', debtor.name) as IName
360                                                 FROM ".TB_PREF."debtors_master debtor,"
361                                                         .TB_PREF."sales_orders sorder
362                                                 WHERE debtor.debtor_no=sorder.debtor_no AND sorder.trans_type=".ST_SALESQUOTE
363                                                         ." ORDER BY sorder.order_no DESC";
364                                         return combo_input($name, '', $sql, 'order_no', 'IName',array('order'=>false));
365
366                                 case 'PO':
367                                         $ref = ($SysPrefs->print_invoice_no() == 1 ? "order_no" : "reference");
368                                         $sql = "SELECT po.order_no, concat(po.$ref, '-', supplier.supp_name) as IName
369                                                 FROM ".TB_PREF."suppliers supplier,"
370                                                         .TB_PREF."purch_orders po
371                                                 WHERE supplier.supplier_id=po.supplier_id
372                                                 ORDER BY po.order_no DESC";
373                                         return combo_input($name, '', $sql, 'order_no', 'IName',array('order'=>false));
374
375                                 case 'REMITTANCE':
376                                         $BP = $type_shortcuts[ST_BANKPAYMENT];
377                                         $SP = $type_shortcuts[ST_SUPPAYMENT];
378                                         $CN = $type_shortcuts[ST_SUPPCREDIT];
379                                         $ref = ($SysPrefs->print_invoice_no() == 1 ? "trans_no" : "reference");
380                                         $sql = "SELECT concat(trans.trans_no, '-',trans.type) AS TNO,
381                                                                 concat(trans.$ref, IF(type=".ST_BANKPAYMENT.", ' $BP ', IF(type=".ST_SUPPAYMENT.", ' $SP ', ' $CN ')), supplier.supp_name) as IName
382                                                         FROM ".TB_PREF."suppliers supplier, "
383                                                                 .TB_PREF."supp_trans trans
384                                                         WHERE type IN(".ST_BANKPAYMENT.",".ST_SUPPAYMENT.",".ST_SUPPCREDIT.")
385                                                                 AND supplier.supplier_id=trans.supplier_id
386                                                         ORDER BY trans.trans_no DESC";
387                                         return combo_input($name, '', $sql, 'TNO', 'IName',array('order'=>false));
388
389                                 case 'RECEIPT':
390                                         $BD = $type_shortcuts[ST_BANKDEPOSIT];
391                                         $CP = $type_shortcuts[ST_CUSTPAYMENT];
392                                         $ref = ($SysPrefs->print_invoice_no() == 1 ? "trans_no" : "reference");
393                                         $sql = "SELECT concat(trans.trans_no, '-', trans.type) AS TNO,
394                                                                 concat(trans.$ref, IF(type=".ST_BANKDEPOSIT.", ' $BD ', ' $CP '), debtor.name) as IName
395                                                 FROM ".TB_PREF."debtors_master debtor,"
396                                                         .TB_PREF."debtor_trans trans
397                                                 WHERE type IN(".ST_BANKDEPOSIT.",".ST_CUSTPAYMENT.",".ST_CUSTCREDIT.") AND debtor.debtor_no=trans.debtor_no
398                                                 ORDER BY trans.trans_no DESC";
399                                         return combo_input($name, '', $sql, 'TNO', 'IName',array('order'=>false));
400
401                                 case 'ITEMS':
402                                         return stock_manufactured_items_list($name);
403
404                                 case 'ITEMS_ALL':
405                                         return stock_manufactured_items_list($name, null, true);
406
407                                 case 'ITEMS_P':
408                                         return stock_purchasable_items_list($name, null, true);
409                                 
410                                 case 'WORKORDER':
411                                         $sql = "SELECT wo.id, concat(wo.id, '-', item.description) as IName
412                                                 FROM ".TB_PREF."stock_master item,"
413                                                         .TB_PREF."workorders wo
414                                                 WHERE item.stock_id=wo.stock_id
415                                                 ORDER BY wo.id DESC";
416                                         return combo_input($name, '', $sql, 'id', 'IName',array('order'=>false));
417
418                                 case 'LOCATIONS':
419                                         return  locations_list($name, null, _("No Location Filter"));
420                                 case 'FLOCATIONS':
421                                         return  locations_list($name, null, _("No Location Filter"), false, true);
422
423                                 case 'CATEGORIES':
424                                         return stock_categories_list($name, null, _("No Category Filter"));
425                                 case 'FCATEGORIES':
426                                         return stock_categories_list($name, null, _("No Category Filter"), false, true);
427                                 case 'FCLASS':
428                                         return fixed_asset_classes_list($name, null, _("No Class Filter"), false);
429                                 case 'SALESTYPES':
430                                         return sales_types_list($name);
431
432                                 case 'AREAS':
433                                         return sales_areas_list($name, null, _("No Areas Filter"));
434
435                                 case 'SALESMEN':
436                                         return sales_persons_list($name, null, _("No Sales Folk Filter"));
437                                         
438                                 case 'TRANS_YEARS':
439                                         return fiscalyears_list($name);
440
441                                 case 'USERS':
442                                         $sql = "SELECT id, user_id FROM ".TB_PREF."users";
443                                         return combo_input($name, '', $sql, 'id', 'user_id',array('spec_option'=>_("No Users Filter"),'spec_id'=>ALL_NUMERIC));
444
445                                 case 'ACCOUNTTAGS':
446                                 case 'DIMENSIONTAGS':
447                                         if ($type == 'ACCOUNTTAGS')
448                                                 $tag_type = TAG_ACCOUNT;
449                                         else
450                                                 $tag_type = TAG_DIMENSION;
451                                         return tag_list($name, 5, $tag_type, true);
452
453                         }
454                 return '';
455         }
456 }
457
458 class Report
459 {
460         var $id;
461         var $name;
462         var $ar_params;
463         var $controls;
464         
465         function __construct($id, $name, $ar_params = null)
466         {
467                 $this->id = $id;
468                 $this->name = $name;
469                 if ($ar_params) $this->set_controls($ar_params);
470         }
471         
472         function set_controls($ar_params) {
473                 $this->controls = $ar_params;
474         }
475         
476         function get_controls() {
477                 return $this->controls;
478         }
479         
480 }
481
482 //------------------------------------------------------------------------------------------------
483 function gl_systypes_list($name, $value=null, $spec_opt=false)
484 {
485         global $systypes_array;
486         
487         $types = $systypes_array;
488         
489         foreach(array(ST_LOCTRANSFER, ST_PURCHORDER, ST_SUPPRECEIVE, ST_MANUISSUE,
490                                 ST_MANURECEIVE, ST_SALESORDER, ST_SALESQUOTE, ST_DIMENSION) as $type)
491                 unset($types[$type]);
492
493         return array_selector($name, $value, $types, 
494                 array( 
495                         'spec_option'=> $spec_opt,
496                         'spec_id' => ALL_NUMERIC,
497                         'async' => false,
498                         )
499         );
500 }
501 /*
502         Add extension/custom reports to report manager.
503 */
504 function add_custom_reports(&$reports)
505 {
506         global $installed_extensions, $path_to_root;
507         
508         // include reports installed inside extension modules
509         if (count($installed_extensions) > 0)
510         {
511                 $extensions = $installed_extensions;
512                 foreach ($extensions as $ext)
513                         if (($ext['active'] && $ext['type'] == 'extension')) {
514                                 $file = $path_to_root.'/'.$ext['path']."/reporting/reports_custom.php";
515                                 if (file_exists($file)) {
516                                         set_ext_domain($ext['path']);
517                                         include_once($file);
518                                         set_ext_domain();
519                                 }
520                         }
521         }
522         $file = company_path()."/reporting/reports_custom.php";
523         if (file_exists($file))
524                 include_once($file);
525 }
526