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