Changed so -1 as parameters to dimensions in get_transactions db routines only fetch...
[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
16         function ReportClasses()
17         {
18                 $this->ar_classes = array();
19         }
20
21         function addReportClass($class_name)
22         {
23                 $this->ar_classes[$class_name] = array();
24         }
25
26         function addReport($class_name, $id, $rep_name, $params=null)
27         {
28                 $this->ar_classes[$class_name][] = new Report($id,$rep_name,$params);
29         }
30
31         function getDisplay($class=null)
32         {
33                 global $table_style2, $comp_path, $path_to_root, $pdf_debug, $Ajax;
34
35                 $temp = array_values($this->ar_classes);
36                 $display_class = $class==null ? $temp[0] : $this->ar_classes[$class];
37                 $class_counter = 0;
38                 $rep_counter = 0;
39                 $st_reports = "";
40                 $st_params = "";
41                 $st_classes = "<b>" . _("Report Classes:") . "</b><br>";
42                 foreach($this->ar_classes as $key=>$value)
43                 {
44                         $style = $class_counter==$_REQUEST['Class'] ? '' : "style='display:none'";
45                         $acc = access_string($key);
46                         $st_classes .= "<a href='"
47                                 .$_SERVER['PHP_SELF']."?Class=$class_counter'"
48                                 ." class='menu_option' id='".default_focus()."'"
49                                 ." onclick='return showClass($class_counter);'$acc[1]>$acc[0]</a> <br>";
50                         $st_reports .= "<table id='TAB_" . $class_counter ."' $style cellpadding=0 cellspacing=0 border=0 width='100%'><tr><td><b>" . _("Reports For Class: ") . "&nbsp;$key</b></td></tr>\n";
51                         foreach($value as $report)
52                         {
53                                 $acc = access_string($report->name);
54                                 $st_reports .= "<tr><td><a class='printlink' href='"
55                                         .$_SERVER['PHP_SELF']."?Class=$class_counter&rep_id=$report->id'"
56                                         ." id='".default_focus()."'"
57                                         ."$acc[1]>$acc[0]</a><tr><td>\n";
58                                 if (isset($_REQUEST['rep_id']) && $_REQUEST['rep_id']==$report->id) {
59
60                                         $action = $path_to_root.'/reporting/prn_redirect.php';
61         
62                                         $st_params = "<table border=0><tr><td>\n"
63                                                 . "<form method='POST' action='$action' target='_blank'>\n";
64                                         $st_params .= hidden('REP_ID', $report->id, false);
65                                         $st_params .= submit('Rep'.$report->id,  
66                                                 _("Display: ") . access_string($report->name, true),
67                                                 false, '', $pdf_debug ? false : 'default') . '<br><br>';
68                                         $st_params .= $report->getDisplay() 
69                                                 . "\n</form></td></tr></table>\n";
70                                         set_focus('Rep'.$report->id);
71                                         $Ajax->addUpdate(true, 'rep_form', $st_params);
72                                 }
73                         }
74                         $st_reports .= "</table>";
75                         $class_counter++;
76                 }
77
78                 $st_params = "<div id='rep_form'>".
79                         "$st_params</div>";
80                 
81
82                 $st =   "<script language='javascript'>
83                                         function showClass(pClass) {
84                                                 for(i=0; i<$class_counter; i++) {
85                                                         document.getElementById(\"TAB_\" + i).style.display=
86                                                         i==pClass ? \"block\" : \"none\";
87                                                 }
88                                                 document.getElementById('rep_form').innerHTML = '';
89 //                                              document.getElementById('rep_form').style.display = 'none';
90                                                 return false;
91                                         }
92                                         function checkDate(pObj) {
93                                                 var re = /^(3[01]|0[1-9]|[12]\d)\/(0[1-9]|1[012])\/\d{4}/;
94                                                 if (re.test(pObj.value)==false) {
95                                                         alert('" . _("Invalid date format") . "')
96                                                 }
97                                         }
98                                 </script>
99                                 ";
100                 $st .= "<table align='center' width='80%' $table_style2><tr valign='top'>";
101                 $st .= "<td width='30%'>$st_classes</td>";
102                 $st .= "<td width='35%'>$st_reports</td>";
103                 $st .= "<td width='35%'>$st_params</td>";
104                 $st .= "</tr></table><br>";
105
106                 return $st;
107         }
108 }
109
110 class Report
111 {
112         var $id;
113         var $name;
114         var $ar_params;
115
116         function Report($id, $name, $ar_params)
117         {
118                 $this->id                       = $id;
119                 $this->name             = $name;
120                 $this->ar_params        = $ar_params;
121         }
122         
123         function getDisplay()
124         {
125                 global $path_to_root, $use_date_picker, $pdf_debug, $print_invoice_no;
126
127                 $st = '';
128                 $dummy = "";
129                 if ($this->ar_params==null)
130                         return "";
131                 foreach($this->ar_params as $index=>$param)
132                 {
133                         $st .= $param->param_name . ':<br>';
134                         switch ($param->param_type)
135                         {
136                                 case 'CURRENCY':
137                                         $sql = "SELECT curr_abrev, concat(curr_abrev,' - ', currency) AS name FROM ".TB_PREF."currencies";
138                                         $st .= combo_input("PARAM_$index", '', $sql, 'curr_abrev', 'name',array('spec_option'=>_("No Currency Filter"),'spec_id'=>ALL_TEXT,'order'=>false));
139                                         break;
140                                 case 'DATE':
141                                 case 'DATEBEGIN':
142                                 case 'DATEEND':
143                                 case 'DATEBEGINM':
144                                 case 'DATEENDM':
145                                 case 'DATEBEGINTAX':
146                                 case 'DATEENDTAX':
147                                         if ($param->param_type == 'DATEBEGIN')
148                                                 $date = begin_fiscalyear();
149                                         elseif ($param->param_type == 'DATEEND')
150                                                 $date = end_fiscalyear();
151                                         else
152                                                 $date = Today();
153                                         if ($param->param_type == 'DATEBEGINM')
154                                                 $date = begin_month($date);
155                                         elseif ($param->param_type == 'DATEENDM')
156                                                 $date = end_month($date);
157                                         elseif ($param->param_type == 'DATEBEGINTAX' || $param->param_type == 'DATEENDTAX')
158                                         {
159                                                 $row = get_company_prefs();
160                                                 $edate = add_months($date, -$row['tax_last']);
161                                                 $edate = end_month($edate);
162                                                 if ($param->param_type == 'DATEENDTAX')
163                                                         $date = $edate;
164                                                 else
165                                                 {
166                                                         $bdate = begin_month($edate);
167                                                         $bdate = add_months($bdate, -$row['tax_prd'] + 1);
168                                                         $date = $bdate;
169                                                 }
170                                         }
171                                         $name = "PARAM_$index";
172
173                                         $st .= "<input type='text' name='$name' value='$date'>";
174                                         if ($use_date_picker)
175                                                 $st .= "<a href=\"javascript:date_picker(document.forms[0].$name);\">"
176                                                 . "     <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";
177
178                                         break;
179                                 case 'YES_NO':
180                                         $sel = array(_('No'), _("Yes"));
181                                         $st .= dup_simple_name_list("PARAM_$index", $sel);
182                                         break;
183                                 case 'PAYMENT_LINK':
184                                         $sel = array(_("No Payment Link"), "PayPal");
185                                         $st .= dup_simple_name_list("PARAM_$index", $sel);
186                                         break;
187                                 case 'DESTINATION':
188                                         $sel = array(_("PDF/Printer"), "Excel");
189                                         $st .= dup_simple_name_list("PARAM_$index", $sel);
190                                         break;
191                                 case 'COMPARE':
192                                         $sel = array(_("Accumulated"), _("Period Y-1"), _("Budget"));
193                                         $st .= dup_simple_name_list("PARAM_$index", $sel);
194                                         break;
195                                 case 'GRAPHIC':
196                                         $sel = array(_("No Graphics"), _("Vertical bars"), _("Horizontal bars"), _("Dots"), _("Lines"), _("Pie"), _("Donut"));
197                                         $st .= dup_simple_name_list("PARAM_$index", $sel);
198                                         break;
199                                 case 'SYS_TYPES':
200                                 case 'SYS_TYPES_ALL':
201                                         $st .= dup_systypes_list("PARAM_$index", $dummy, true, _("No Type Filter"), ($param->param_type=='SYS_TYPES_ALL'));
202                                         break;
203                                 case 'TEXT':
204                                         $st .= "<input type='text' name='PARAM_$index'>";
205                                         break;
206                                 case 'TEXTBOX':
207                                         $st .= "<textarea rows=4 cols=30 name='PARAM_$index'></textarea>";
208                                         break;
209                                 case 'ACCOUNTS':
210                                         $sql = "SELECT id, name FROM ".TB_PREF."chart_types";
211                                         $st .= combo_input("PARAM_$index", '', $sql, 'id', 'name',array('spec_option'=>_("No Account Group Filter"),'spec_id'=>ALL_NUMERIC));
212                                         break;
213                                 case 'ACCOUNTS_NO_FILTER':
214                                         $sql = "SELECT id, name FROM ".TB_PREF."chart_types";
215                                         $st .= combo_input("PARAM_$index", '', $sql, 'id', 'name', null);
216                                         break;
217                                 case 'GL_ACCOUNTS':
218                                         $sql = "SELECT chart.account_code, chart.account_name, type.name, chart.inactive, type.id
219                                                 FROM ".TB_PREF."chart_master chart,".TB_PREF."chart_types type WHERE chart.account_type=type.id";
220                                         $st .= combo_input("PARAM_$index", '', $sql, 'chart.account_code', 'chart.account_name',
221                                                 array('format' => '_format_account','order' => array('type.id','account_code'),'async' => false,'category' => 2));
222                                         break;
223                                 case 'BANK_ACCOUNTS':
224                                         $sql = "SELECT ".TB_PREF."bank_accounts.id, concat(bank_account_name, if (bank_curr_code=curr_default,'', concat(' - ', bank_curr_code))) AS name FROM ".TB_PREF."bank_accounts, ".TB_PREF."company";
225                                         $st .= combo_input("PARAM_$index", '', $sql, 'id', 'name',array('order'=>array('id')));
226                                         break;
227                                 case 'DIMENSION':
228                                         $sql = "SELECT id, concat(reference, ' - ', name) AS DimName FROM ".TB_PREF."dimensions";
229                                         $st .= combo_input("PARAM_$index", '', $sql, 'id', 'DimName',array('order'=>array('reference')));
230                                         break;
231                                 case 'DIMENSIONS':
232                                         $sql = "SELECT id, concat(reference, ' - ', name) as DimName FROM ".TB_PREF."dimensions";
233                                         $st .= combo_input("PARAM_$index", '', $sql, 'id', 'DimName',array('spec_option'=>_("No Dimension Filter"),'spec_id'=>0,'order'=>array('reference')));
234                                         break;
235                                 case 'DIMENSION1':
236                                         $sql = "SELECT id, concat(reference, ' - ', name) AS DimName FROM ".TB_PREF."dimensions WHERE type_=1";
237                                         $st .= combo_input("PARAM_$index", '', $sql, 'id', 'DimName',array('order'=>array('reference')));
238                                         break;
239                                 case 'DIMENSIONS1':
240                                         $sql = "SELECT id, concat(reference, ' - ', name) as DimName FROM ".TB_PREF."dimensions WHERE type_=1";
241                                         $st .= combo_input("PARAM_$index", '', $sql, 'id', 'DimName',array('spec_option'=>_("No Dimension Filter"),'spec_id'=>0,'order'=>array('reference')));
242                                         break;
243                                 case 'DIMENSION2':
244                                         $sql = "SELECT id, concat(reference, ' - ', name) AS DimName FROM ".TB_PREF."dimensions WHERE type_=2";
245                                         $st .= combo_input("PARAM_$index", '', $sql, 'id', 'DimName',array('order'=>array('reference')));
246                                         break;
247                                 case 'DIMENSIONS2':
248                                         $sql = "SELECT id, concat(reference, ' - ', name) as DimName FROM ".TB_PREF."dimensions WHERE type_=2";
249                                         $st .= combo_input("PARAM_$index", '', $sql, 'id', 'DimName',array('spec_option'=>_("No Dimension Filter"),'spec_id'=>0,'order'=>array('reference')));
250                                         break;
251                                 case 'CUSTOMERS_NO_FILTER':
252                                 case 'CUSTOMERS':
253                                         $sql = "SELECT debtor_no, name FROM ".TB_PREF."debtors_master";
254                                         if ($param->param_type == 'CUSTOMERS_NO_FILTER')
255                                                 $st .= combo_input("PARAM_$index", '', $sql, 'debtor_no', 'name',array('spec_option'=>_("No Customer Filter"),'spec_id'=>ALL_NUMERIC));
256                                         else
257                                                 $st .= combo_input("PARAM_$index", '', $sql, 'debtor_no', 'name', null);
258                                         break;
259                                 case 'SUPPLIERS_NO_FILTER':
260                                 case 'SUPPLIERS':
261                                         $sql = "SELECT supplier_id, supp_name FROM ".TB_PREF."suppliers";
262                                         if ($param->param_type == 'SUPPLIERS_NO_FILTER')
263                                                 $st .= combo_input("PARAM_$index", '', $sql, 'supplier_id', 'supp_name',array('spec_option'=>_("No Supplier Filter"),'spec_id'=>ALL_NUMERIC));
264                                         else
265                                                 $st .= combo_input("PARAM_$index", '', $sql, 'supplier_id', 'supp_name', null);
266                                         break;
267                                 case 'INVOICE':
268                                         $IV = _("IV");
269                                         $CN = _("CN");
270                                         if ($print_invoice_no == 1)
271                                                 $sql = "SELECT concat(".TB_PREF."debtor_trans.trans_no, '-',
272                                                         ".TB_PREF."debtor_trans.type) AS TNO, concat(".TB_PREF."debtor_trans.trans_no, if (type=".ST_SALESINVOICE.", ' $IV ', ' $CN '), ".TB_PREF."debtors_master.name) as IName
273                                                         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";
274                                         else            
275                                                 $sql = "SELECT concat(".TB_PREF."debtor_trans.trans_no, '-',
276                                                         ".TB_PREF."debtor_trans.type) AS TNO, concat(".TB_PREF."debtor_trans.reference, if (type=".ST_SALESINVOICE.", ' $IV ', ' $CN '), ".TB_PREF."debtors_master.name) as IName
277                                                         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";
278                                         $st .= combo_input("PARAM_$index", '', $sql, 'TNO', 'IName',array('order'=>false));
279                                         break;
280                                 case 'DELIVERY':
281                                         $DN = _("DN");
282                                         $sql = "SELECT
283                                         concat(".TB_PREF."debtor_trans.trans_no, '-', ".TB_PREF."debtor_trans.type) AS TNO, concat(".TB_PREF."debtor_trans.trans_no, ' $DN ',
284                                          ".TB_PREF."debtors_master.name) as IName
285                                                 FROM ".TB_PREF."debtors_master, ".TB_PREF."debtor_trans
286                                                 WHERE type=".ST_CUSTDELIVERY." AND ".TB_PREF."debtors_master.debtor_no=".
287                                                 TB_PREF."debtor_trans.debtor_no ORDER BY ".TB_PREF."debtor_trans.trans_no DESC";
288                                         $st .= combo_input("PARAM_$index", '', $sql, 'TNO', 'IName',array('order'=>false));
289                                         break;
290                                 case 'ORDERS':
291                                         $sql = "SELECT ".TB_PREF."sales_orders.order_no, concat(".TB_PREF."sales_orders.order_no, '-',
292                                                 ".TB_PREF."debtors_master.name) as IName
293                                                 FROM ".TB_PREF."debtors_master, ".TB_PREF."sales_orders WHERE ".TB_PREF."debtors_master.debtor_no=".TB_PREF."sales_orders.debtor_no 
294                                                 AND ".TB_PREF."sales_orders.trans_type=".ST_SALESORDER." ORDER BY ".TB_PREF."sales_orders.order_no DESC";
295                                         $st .= combo_input("PARAM_$index", '', $sql, 'order_no', 'IName',array('order'=>false));
296                                         break;
297                                 case 'QUOTATIONS':
298                                         $sql = "SELECT ".TB_PREF."sales_orders.order_no, concat(".TB_PREF."sales_orders.order_no, '-',
299                                                 ".TB_PREF."debtors_master.name) as IName
300                                                 FROM ".TB_PREF."debtors_master, ".TB_PREF."sales_orders WHERE ".TB_PREF."debtors_master.debtor_no=".TB_PREF."sales_orders.debtor_no 
301                                                 AND ".TB_PREF."sales_orders.trans_type=".ST_SALESQUOTE." ORDER BY ".TB_PREF."sales_orders.order_no DESC";
302                                         $st .= combo_input("PARAM_$index", '', $sql, 'order_no', 'IName',array('order'=>false));
303                                         break;
304                                 case 'PO':
305                                         $sql = "SELECT ".TB_PREF."purch_orders.order_no, concat(".TB_PREF."purch_orders.order_no, '-',
306                                                 ".TB_PREF."suppliers.supp_name) as IName
307                                                 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";
308                                         $st .= combo_input("PARAM_$index", '', $sql, 'order_no', 'IName',array('order'=>false));
309                                         break;
310                                 case 'ITEMS':
311                                         $sql = "SELECT stock_id, concat(stock_id, '-', description) as name FROM ".TB_PREF."stock_master WHERE (mb_flag='A' OR mb_flag='M')";
312                                         $st .= combo_input("PARAM_$index", '', $sql, 'stock_id', 'name',array('order'=>array('stock_id')));
313                                         break;
314                                 case 'WORKORDER':
315                                         $sql = "SELECT ".TB_PREF."workorders.id, concat(".TB_PREF."workorders.id, '-',
316                                                 ".TB_PREF."stock_master.description) as IName
317                                                 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";
318                                         $st .= combo_input("PARAM_$index", '', $sql, 'id', 'IName',array('order'=>false));
319                                         break;
320                                 case 'LOCATIONS':
321                                         $sql = "SELECT loc_code, location_name FROM ".TB_PREF."locations";
322                                         $st .= combo_input("PARAM_$index", '', $sql, 'loc_code', 'location_name',array('spec_option'=>_("No Location Filter"),'spec_id'=>ALL_TEXT));
323                                         break;
324                                 case 'CATEGORIES':
325                                         $sql = "SELECT category_id, description FROM ".TB_PREF."stock_category";
326                                         $st .= combo_input("PARAM_$index", '', $sql, 'category_id', 'description',array('spec_option'=>_("No Category Filter"),'spec_id'=>ALL_NUMERIC));
327                                         break;
328                                 case 'SALESTYPES':
329                                         $sql = "SELECT id, sales_type FROM ".TB_PREF."sales_types";
330                                         $st .= combo_input("PARAM_$index", '', $sql, 'id', 'sales_type', null);
331                                         break;
332                                 case 'AREAS':
333                                         $sql = "SELECT area_code, description FROM ".TB_PREF."areas";
334                                         $st .= combo_input("PARAM_$index", '', $sql, 'area_code', 'description',array('spec_option'=>_("No Area Filter"),'spec_id'=>ALL_NUMERIC));
335                                         break;
336                                 case 'SALESMEN':
337                                         $sql = "SELECT salesman_code, salesman_name FROM ".TB_PREF."salesman";
338                                         $st .= combo_input("PARAM_$index", '', $sql, 'salesman_code', 'salesman_name',array('spec_option'=>_("No Sales Folk Filter"),'spec_id'=>ALL_NUMERIC));
339                                         break;
340                                 case 'TRANS_YEARS':
341                                         $sql = "SELECT DISTINCT YEAR(tran_date) AS tran_date, YEAR(tran_date) AS tran_date2 FROM ".TB_PREF."gl_trans";
342                                         $st .= combo_input("PARAM_$index", '', $sql, 'tran_date', 'tran_date2',array('order'=>array('tran_date')));
343                                         break;
344                                 case 'USERS':
345                                         $sql = "SELECT id, user_id FROM ".TB_PREF."users";
346                                         $st .= combo_input("PARAM_$index", '', $sql, 'id', 'user_id',array('spec_option'=>_("No Users Filter"),'spec_id'=>ALL_NUMERIC));
347                                         break;
348                                 case 'ACCOUNTTAGS':
349                                 case 'DIMENSIONTAGS':
350                                         if ($param->param_type == 'ACCOUNTTAGS')
351                                                 $type = TAG_ACCOUNT;
352                                         else
353                                                 $type = TAG_DIMENSION;
354                                         $sql = "SELECT id, name FROM ".TB_PREF."tags WHERE type=$type"; 
355                                         $st .= combo_input("PARAM_$index", '', $sql, 'id', 'name',array('spec_option'=>_("No tags"),'spec_id' => ALL_NUMERIC,'multi'=>true,'height'=>5));
356                                         break;
357
358                         }
359                         $st .= "<br><br>";
360                 }
361                 return $st;
362         }
363 }
364
365 class ReportParam
366 {
367         var $param_name;
368         var $param_type;
369
370         function ReportParam($param_name, $param_type)
371         {
372                 $this->param_name = $param_name;
373                 $this->param_type = $param_type;
374         }
375 }
376
377 function dup_simple_name_list($name, $selections)
378 {
379         $st = "<select name='$name'>";
380         for ($i = 0; $i < count($selections); $i++)
381                 $st .= "<option value='" . $i . "'>" . $selections[$i] . "</option>\n";
382         $st .= "</select>";
383         return $st;
384 }
385
386 //------------------------------------------------------------------------------------------------
387
388 function dup_systypes_list($name, &$selected_id, $all_option=false, $all_option_name=null, $all_types=false)
389 {
390         global $systypes_array;
391
392         $st = "<select name='$name'>";
393         if ($all_option == true)
394         {
395                 $reserved_word = ALL_NUMERIC;
396         if ($reserved_word == $selected_id)
397              $st .= "<option selected value='" . $reserved_word . "'>" . $all_option_name . "</option>\n";
398         else
399              $st .= "<option value='" . $reserved_word . "'>" . $all_option_name . "</option>\n";
400                 if ($selected_id == "")
401                         $selected_id = $reserved_word;
402         }
403
404         foreach ($systypes_array as $i => $value)
405         {
406                 if (!$all_types && ($i == ST_LOCTRANSFER || $i == ST_PURCHORDER || $i == ST_SUPPRECEIVE || ($i >= ST_MANUISSUE && $i <= ST_SALESQUOTE) || 
407                         $i >= ST_DIMENSION))
408                         continue;
409         $st .= "<option" . ($selected_id == $i ? " selected" : "") . " value='$i'>" . $value . "</option>\n";
410         }
411     $st .= "</select>";
412         return $st;
413 }
414
415
416 ?>