Added Account tags selection in Annual Expense Breakdown, Balanse Sheet
[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') . hidden('REP_ID', $report->id, false).'<br><br>';
85                                         $st_params .= $this->getOptions($report->get_controls());
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)
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                                 $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
169                 $st = '';
170                         switch ($type)
171                         {
172                                 case 'CURRENCY':
173                                         $sql = "SELECT curr_abrev, concat(curr_abrev,' - ', currency) AS name FROM ".TB_PREF."currencies";
174                                         return combo_input($name, '', $sql, 'curr_abrev', 'name',array('spec_option'=>_("No Currency Filter"),'spec_id'=>ALL_TEXT,'order'=>false));
175
176                                 case 'DATE':
177                                 case 'DATEBEGIN':
178                                 case 'DATEEND':
179                                 case 'DATEBEGINM':
180                                 case 'DATEENDM':
181                                 case 'DATEBEGINTAX':
182                                 case 'DATEENDTAX':
183                                         if ($type == 'DATEBEGIN')
184                                                 $date = begin_fiscalyear();
185                                         elseif ($type == 'DATEEND')
186                                                 $date = end_fiscalyear();
187                                         else
188                                                 $date = Today();
189                                         if ($type == 'DATEBEGINM')
190                                                 $date = begin_month($date);
191                                         elseif ($type == 'DATEENDM')
192                                                 $date = end_month($date);
193                                         elseif ($type == 'DATEBEGINTAX' || $type == 'DATEENDTAX')
194                                         {
195                                                 $row = get_company_prefs();
196                                                 $edate = add_months($date, -$row['tax_last']);
197                                                 $edate = end_month($edate);
198                                                 if ($type == 'DATEENDTAX')
199                                                         $date = $edate;
200                                                 else
201                                                 {
202                                                         $bdate = begin_month($edate);
203                                                         $bdate = add_months($bdate, -$row['tax_prd'] + 1);
204                                                         $date = $bdate;
205                                                 }
206                                         }
207
208                                         $st = "<input type='text' name='$name' value='$date'>";
209                                         if ($use_date_picker)
210                                                 $st .= "<a href=\"javascript:date_picker(document.forms[0].$name);\">"
211                                                 . "     <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";
212                                         return $st;
213                                         break;
214
215                                 case 'YES_NO':
216                                         return yesno_list($name);
217
218                                 case 'PAYMENT_LINK':
219                                         $sel = array(_("No payment Link"), "PayPal");
220                                         return array_selector($name, null, $sel);
221
222                                 case 'DESTINATION':
223                                         $sel = array(_("PDF/Printer"), "Excel");
224                                         return array_selector($name, null, $sel);
225
226                                 case 'COMPARE':
227                                         $sel = array(_("Accumulated"), _("Period Y-1"), _("Budget"));
228                                         return array_selector($name, null, $sel);
229
230                                 case 'GRAPHIC':
231                                         $sel = array(_("No Graphics"), _("Vertical bars"), _("Horizontal bars"), 
232                                                                 _("Dots"), _("Lines"), _("Pie"), _("Donut"));
233                                         return array_selector($name, null, $sel);
234
235                                 case 'SYS_TYPES':
236                                         return gl_systypes_list($name, null, _("No Type Filter"));
237
238                                 case 'SYS_TYPES_ALL':
239                                         return systypes_list($name, null, _("No Type Filter"));
240
241                                 case 'TEXT':
242                                         return "<input type='text' name='$name'>";
243
244                                 case 'TEXTBOX':
245                                         return "<textarea rows=4 cols=30 name='$name'></textarea>";
246
247                                 case 'ACCOUNTS': // not used
248                                         return gl_account_types_list($name, null, _("No Account Group Filter"), true);
249
250                                 case 'ACCOUNTS_NO_FILTER': // not used
251                                         return gl_account_types_list($name);
252
253                                 case 'GL_ACCOUNTS':
254                                         return gl_all_accounts_list($name);
255
256                                 case 'BANK_ACCOUNTS':
257                                         return bank_accounts_list($name);
258                                         
259                                 case 'DIMENSION':
260                                         return dimensions_list($name, null, false, ' ', false, true, 0);
261                                         
262                                 case 'DIMENSIONS':
263                                         return dimensions_list($name, null, true, _("No Dimension Filter"), false, true, 0);
264
265                                 case 'DIMENSION1':
266                                         return dimensions_list($name, null, false, ' ', false, true, 1);
267
268                                 case 'DIMENSIONS1':
269                                         return dimensions_list($name, null, true, _("No Dimension Filter"), false, true, 1);
270
271                                 case 'DIMENSION2':
272                                         return dimensions_list($name, null, false, ' ', false, true, 2);
273
274                                 case 'DIMENSIONS2':
275                                         return dimensions_list($name, null, true, _("No Dimension Filter"), false, true, 2);
276
277                                 case 'CUSTOMERS_NO_FILTER':
278                                 case 'CUSTOMERS':
279                                         $sql = "SELECT debtor_no, name FROM ".TB_PREF."debtors_master";
280                                         if ($type == 'CUSTOMERS_NO_FILTER')
281                                                 return combo_input($name, '', $sql, 'debtor_no', 'name',array('spec_option'=>_("No Customer Filter"),'spec_id'=>ALL_NUMERIC));
282 // FIX allitems numeric!
283 //                                              return customer_list($name, null, _("No Customer Filter"));
284                                         else
285                                                 return combo_input($name, '', $sql, 'debtor_no', 'name', null);
286 //                                              return customer_list($name);
287
288                                 case 'SUPPLIERS_NO_FILTER':
289                                 case 'SUPPLIERS':
290                                         $sql = "SELECT supplier_id, supp_name FROM ".TB_PREF."suppliers";
291                                         if ($type == 'SUPPLIERS_NO_FILTER')
292                                                 return combo_input($name, '', $sql, 'supplier_id', 'supp_name',array('spec_option'=>_("No Supplier Filter"),'spec_id'=>ALL_NUMERIC));
293 // FIX allitems numeric!
294 //                                              return supplier_list($name, null, _("No Supplier Filter"));
295                                         else
296                                                 return combo_input($name, '', $sql, 'supplier_id', 'supp_name', null);
297 //                                              return supplier_list($name);
298
299                                 case 'INVOICE':
300                                         $IV = _("IV");
301                                         $CN = _("CN");
302                                         $ref = ($print_invoice_no == 1 ? "trans_no" : "reference");
303                                         $sql = "SELECT concat(".TB_PREF."debtor_trans.trans_no, '-',
304                                                 ".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
305                                                 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";
306                                         return combo_input($name, '', $sql, 'TNO', 'IName',array('order'=>false));
307
308                                 case 'DELIVERY':
309                                         $DN = _("DN");
310                                         $sql = "SELECT
311                                         concat(".TB_PREF."debtor_trans.trans_no, '-', ".TB_PREF."debtor_trans.type) AS TNO, concat(".TB_PREF."debtor_trans.trans_no, ' $DN ',
312                                          ".TB_PREF."debtors_master.name) as IName
313                                                 FROM ".TB_PREF."debtors_master, ".TB_PREF."debtor_trans
314                                                 WHERE type=".ST_CUSTDELIVERY." AND ".TB_PREF."debtors_master.debtor_no=".
315                                                 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 'ORDERS':
319                                         $ref = ($print_invoice_no == 1 ? "order_no" : "reference");
320                                         $sql = "SELECT ".TB_PREF."sales_orders.order_no, concat(".TB_PREF."sales_orders.$ref, '-',
321                                                 ".TB_PREF."debtors_master.name) as IName
322                                                 FROM ".TB_PREF."debtors_master, ".TB_PREF."sales_orders WHERE ".TB_PREF."debtors_master.debtor_no=".TB_PREF."sales_orders.debtor_no 
323                                                 AND ".TB_PREF."sales_orders.trans_type=".ST_SALESORDER." ORDER BY ".TB_PREF."sales_orders.order_no DESC";
324                                         return combo_input($name, '', $sql, 'order_no', 'IName',array('order'=>false));
325
326                                 case 'QUOTATIONS':
327                                         $ref = ($print_invoice_no == 1 ? "order_no" : "reference");
328                                         $sql = "SELECT ".TB_PREF."sales_orders.order_no, concat(".TB_PREF."sales_orders.$ref, '-',
329                                                 ".TB_PREF."debtors_master.name) as IName
330                                                 FROM ".TB_PREF."debtors_master, ".TB_PREF."sales_orders WHERE ".TB_PREF."debtors_master.debtor_no=".TB_PREF."sales_orders.debtor_no 
331                                                 AND ".TB_PREF."sales_orders.trans_type=".ST_SALESQUOTE." ORDER BY ".TB_PREF."sales_orders.order_no DESC";
332                                         return combo_input($name, '', $sql, 'order_no', 'IName',array('order'=>false));
333
334                                 case 'PO':
335                                         $ref = ($print_invoice_no == 1 ? "order_no" : "reference");
336                                         $sql = "SELECT ".TB_PREF."purch_orders.order_no, concat(".TB_PREF."purch_orders.$ref, '-',
337                                                 ".TB_PREF."suppliers.supp_name) as IName
338                                                 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";
339                                         return combo_input($name, '', $sql, 'order_no', 'IName',array('order'=>false));
340
341                                 case 'REMITTANCE':
342                                         $BP = _("BP");
343                                         $SP = _("SP");
344                                         $CN = _("CN");
345                                         $ref = ($print_invoice_no == 1 ? "trans_no" : "reference");
346                                         $sql = "SELECT concat(".TB_PREF."supp_trans.trans_no, '-',
347                                                 ".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
348                                                 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";
349                                         return combo_input($name, '', $sql, 'TNO', 'IName',array('order'=>false));
350
351                                 case 'RECEIPT':
352                                         $BD = _("BD");
353                                         $CP = _("CP");
354                                         $CN = _("CN");
355                                         $ref = ($print_invoice_no == 1 ? "trans_no" : "reference");
356                                         $sql = "SELECT concat(".TB_PREF."debtor_trans.trans_no, '-',
357                                                 ".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
358                                                 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";
359                                         return combo_input($name, '', $sql, 'TNO', 'IName',array('order'=>false));
360
361                                 case 'ITEMS':
362                                         return stock_manufactured_items_list($name);
363
364                                 case 'WORKORDER':
365                                         $sql = "SELECT ".TB_PREF."workorders.id, concat(".TB_PREF."workorders.id, '-',
366                                                 ".TB_PREF."stock_master.description) as IName
367                                                 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";
368                                         return combo_input($name, '', $sql, 'id', 'IName',array('order'=>false));
369
370                                 case 'LOCATIONS':
371                                         return  locations_list($name, null, _("No Location Filter"));
372
373                                 case 'CATEGORIES':
374                                         return stock_categories_list($name, null, _("No Category Filter"));
375
376                                 case 'SALESTYPES':
377                                         return sales_types_list($name);
378
379                                 case 'AREAS':
380                                         return sales_areas_list($name);
381
382                                 case 'SALESMEN':
383                                         return sales_persons_list($name, null, _("No Sales Folk Filter"));
384                                         
385                                 case 'TRANS_YEARS':
386                                         return fiscalyears_list($name);
387
388                                 case 'USERS':
389                                         $sql = "SELECT id, user_id FROM ".TB_PREF."users";
390                                         return combo_input($name, '', $sql, 'id', 'user_id',array('spec_option'=>_("No Users Filter"),'spec_id'=>ALL_NUMERIC));
391
392                                 case 'ACCOUNTTAGS':
393                                 case 'DIMENSIONTAGS':
394                                         if ($type == 'ACCOUNTTAGS')
395                                                 $tag_type = TAG_ACCOUNT;
396                                         else
397                                                 $tag_type = TAG_DIMENSION;
398                                         //return tag_list($name, 5, $tag_type, true, false, _("No tags"));
399                                         return tag_list($name, 5, $tag_type, true);
400
401                         }
402                 return '';
403         }
404 }
405
406 class Report
407 {
408         var $id;
409         var $name;
410         var $ar_params;
411         var $controls;
412         
413         function Report($id, $name, $ar_params = null)
414         {
415                 $this->id = $id;
416                 $this->name = $name;
417                 if ($ar_params) $this->set_controls($ar_params);
418         }
419         
420         function set_controls($ar_params) {
421                 $this->controls = $ar_params;
422         }
423         
424         function get_controls() {
425                 return $this->controls;
426         }
427         
428 }
429
430 //------------------------------------------------------------------------------------------------
431 function gl_systypes_list($name, $value=null, $spec_opt=false)
432 {
433         global $systypes_array;
434         
435         $types = $systypes_array;
436         
437         foreach(array(ST_LOCTRANSFER, ST_PURCHORDER, ST_SUPPRECEIVE, ST_MANUISSUE,
438                                 ST_MANURECEIVE, ST_SALESORDER, ST_SALESQUOTE, ST_DIMENSION) as $type)
439                 unset($types[$type]);
440
441         return array_selector($name, $value, $types, 
442                 array( 
443                         'spec_option'=> $spec_opt,
444                         'spec_id' => ALL_NUMERIC,
445                         'async' => false,
446                         )
447         );
448 }
449 /*
450         Add extension/custom reports to report manager.
451 */
452 function add_custom_reports(&$reports)
453 {
454         global $installed_extensions, $path_to_root;
455         
456         // include reports installed inside extension modules
457         if (count($installed_extensions) > 0)
458         {
459                 $extensions = $installed_extensions;
460                 foreach ($extensions as $ext)
461                         if (($ext['active'] && $ext['type'] == 'extension')) {
462                                 $file = $path_to_root.'/'.$ext['path']."/reporting/reports_custom.php";
463                                 if (file_exists($file))
464                                         include_once($file);
465                         }
466         }
467         $file = company_path()."/reporting/reports_custom.php";
468         if (file_exists($file))
469                 include_once($file);
470 }
471
472 ?>