*** empty log message ***
[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;
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                                         //$st .= "<input type='text' name='RP_" . $this->id . "_$index' value='$date' onblur='javascript:checkDate(this)'>";
156                                         $st .= "<input type='text' name='RP_" . $this->id . "_$index' value='$date'>";
157                                         break;
158                                 case 'YES_NO':  
159                                         $sel = array(_('No'), _("Yes"));
160                                         $st .= dup_simple_name_list("RP_" . $this->id . "_$index", $sel);
161                                         break;
162                                 case 'COMPARE':
163                                         $sel = array(_("Accumulated"), _("Period Y-1"), _("Budget"));
164                                         $st .= dup_simple_name_list("RP_" . $this->id . "_$index", $sel);
165                                         break;
166                                 case 'GRAPHIC':
167                                         $sel = array(_("No Graphics"), _("Vertical bars"), _("Horizontal bars"), _("Dots"), _("Lines"), _("Pie"), _("Donut"));
168                                         $st .= dup_simple_name_list("RP_" . $this->id . "_$index", $sel);
169                                         break;
170                                 case 'SYS_TYPES':
171                                         $st .= dup_systypes_list("RP_" . $this->id . "_$index", $dummy, true, _("No Type Filter"), true);
172                                         break;                          
173                                 case 'TEXT':
174                                         $st .= "<input type='text' name='RP_" . $this->id . "_$index'>";
175                                         break;
176                                 case 'TEXTBOX':
177                                         $st .= "<textarea rows=4 cols=30 name='RP_" . $this->id . "_$index'></textarea>";
178                                         break;
179                                 case 'ACCOUNTS':
180                                         $sql = "SELECT id, name FROM ".TB_PREF."chart_types ORDER BY name";
181                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy, true, _("No Account Group Filter"), true);
182                                         break;
183                                 case 'GL_ACCOUNTS':
184                                         $sql = "SELECT account_code, concat(account_code, ' - ', account_name) as account_name FROM ".TB_PREF."chart_master ORDER BY account_code";
185                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
186                                         break;
187                                 case 'BANK_ACCOUNTS':
188                                         $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 
189                                                 WHERE ".TB_PREF."bank_accounts.account_code=".TB_PREF."chart_master.account_code";
190                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
191                                         break;
192                                 case 'DIMENSION':
193                                         $sql = "SELECT reference, concat(reference, ' - ', name) AS DimName FROM ".TB_PREF."dimensions ORDER BY reference";
194                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
195                                         break;
196                                 case 'DIMENSIONS':
197                                         $sql = "SELECT reference, concat(reference, ' - ', name) as DimName FROM ".TB_PREF."dimensions ORDER BY reference";
198                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy, true, _("No Dimension Filter"), true);
199                                         break;
200                                 case 'DIMENSION1':
201                                         $sql = "SELECT reference, concat(reference, ' - ', name) AS DimName FROM ".TB_PREF."dimensions WHERE type_=1 ORDER BY reference";
202                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
203                                         break;
204                                 case 'DIMENSIONS1':
205                                         $sql = "SELECT reference, concat(reference, ' - ', name) as DimName FROM ".TB_PREF."dimensions WHERE type_=1 ORDER BY reference";
206                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy, true, _("No Dimension Filter"), true);
207                                         break;
208                                 case 'DIMENSION2':
209                                         $sql = "SELECT reference, concat(reference, ' - ', name) AS DimName FROM ".TB_PREF."dimensions WHERE type_=2 ORDER BY reference";
210                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
211                                         break;
212                                 case 'DIMENSIONS2':
213                                         $sql = "SELECT reference, concat(reference, ' - ', name) as DimName FROM ".TB_PREF."dimensions WHERE type_=2 ORDER BY reference";
214                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy, true, _("No Dimension Filter"), true);
215                                         break;
216                                 case 'CUSTOMERS_NO_FILTER':
217                                 case 'CUSTOMERS':
218                                         $sql = "SELECT debtor_no, name FROM ".TB_PREF."debtors_master ORDER BY debtor_no";
219                                         if ($param->param_type == 'CUSTOMERS_NO_FILTER')
220                                                 $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy, true, _("No Customer Filter"), true);
221                                         else    
222                                                 $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
223                                         break;
224                                 case 'SUPPLIERS':
225                                         $sql = "SELECT supplier_id, supp_name FROM ".TB_PREF."suppliers ORDER BY supplier_id";
226                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
227                                         break;
228                                 case 'INVOICE':
229                                         $IV = _("IV");
230                                         $CN = _("CN");
231                                         $sql = "SELECT concat(".TB_PREF."debtor_trans.trans_no, '-', 
232                                                 ".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 
233                                                 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";
234                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
235                                         break;
236                                 case 'ORDERS':
237                                         $sql = "SELECT ".TB_PREF."sales_orders.order_no, concat(".TB_PREF."sales_orders.order_no, '-', 
238                                                 ".TB_PREF."debtors_master.name) as IName 
239                                                 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";
240                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
241                                         break;
242                                 case 'PO':
243                                         $sql = "SELECT ".TB_PREF."purch_orders.order_no, concat(".TB_PREF."purch_orders.order_no, '-', 
244                                                 ".TB_PREF."suppliers.supp_name) as IName  
245                                                 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";
246                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
247                                         break;
248                                 case 'ITEMS':
249                                         $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";
250                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
251                                         break;
252                                 case 'LOCATIONS':
253                                         $sql = "SELECT loc_code, location_name FROM ".TB_PREF."locations ORDER BY location_name";
254                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy, true, _("No Location Filter"), false);
255                                         break;
256                                 case 'CATEGORIES':
257                                         $sql = "SELECT category_id, description FROM ".TB_PREF."stock_category ORDER BY description";
258                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy, true, _("No Category Filter"), true);
259                                         break;
260                                 case 'SALESTYPES':
261                                         $sql = "SELECT id, sales_type FROM ".TB_PREF."sales_types ORDER BY sales_type";
262                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
263                                         break;
264                                 case 'AREAS':
265                                         $sql = "SELECT area_code, description FROM ".TB_PREF."areas ORDER BY description";
266                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy, true, _("No Area Filter"), true);
267                                         break;
268                                 case 'SALESMEN':
269                                         $sql = "SELECT salesman_code, salesman_name FROM ".TB_PREF."salesman ORDER BY salesman_name";
270                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy, true, _("No Sales Folk Filter"), true);
271                                         break;
272                                 case 'TRANS_YEARS':
273                                         $sql = "SELECT DISTINCT YEAR(tran_date), YEAR(tran_date) FROM ".TB_PREF."gl_trans";
274                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
275                                         break;
276                                 case 'ACCOUNTS_NO_FILTER':
277                                         $sql = "SELECT id, name FROM ".TB_PREF."chart_types ORDER BY name";
278                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
279                                         break;
280
281                         }
282                         $st .= "<br><br>";
283                 }
284                 return $st;
285         }
286 }
287
288 class ReportParam 
289 {
290         var $param_name;
291         var $param_type;
292
293         function ReportParam($param_name, $param_type) 
294         {
295                 $this->param_name = $param_name;
296                 $this->param_type = $param_type;
297         }
298 }
299
300 function dup_simple_codeandname_list($sql, $name, &$selected_id,
301         $all_option=false, $all_option_name=null, $all_option_numeric=false, 
302         $submit_on_change=false)
303 {
304         if ($submit_on_change == true)
305                 $st = "<select name='$name' onchange='this.form.submit();'>";
306         else
307                 $st = "<select name='$name'>";
308
309         if ($all_option == true)
310         {
311                 if ($all_option_numeric)
312                         $reserved_word = reserved_words::get_all_numeric();
313                 else
314                         $reserved_word = reserved_words::get_all();
315
316         if ($reserved_word == $selected_id)
317         {
318              $st .= "<option selected value='" . $reserved_word . "'>" . $all_option_name . "</option>\n";
319         } 
320         else 
321         {
322              $st .= "<option value='" . $reserved_word . "'>" . $all_option_name . "</option>\n";
323         }
324                 if ($selected_id == "") 
325                 {
326                         $selected_id =  $reserved_word;
327                 }
328         }
329
330         $result = db_query($sql);
331
332         while ($row = db_fetch_row($result)) 
333         {
334                 if ($selected_id == $row[0]) 
335                 {
336                         $st .= "<option selected value='" . $row[0] . "'>";
337                 } 
338                 else 
339                 {
340                         $st .= "<option value='" . $row[0] . "'>";
341                 }
342                 $st .= $row[1] . "</option>\n";
343
344                 if ($selected_id == "") 
345                 {
346                         $selected_id = $row[0];
347                 }
348         }
349
350         $st .= "</select>";
351         db_free_result($result);
352
353         return $st;
354 }
355
356 function dup_simple_name_list($name, $selections)
357 {
358         $st = "<select name='$name'>";
359         for ($i = 0; $i < count($selections); $i++)
360                 $st .= "<option value='" . $i . "'>" . $selections[$i] . "</option>\n";
361         $st .= "</select>";
362         return $st;
363 }
364
365 //------------------------------------------------------------------------------------------------
366
367 function dup_systypes_list($name, &$selected_id, $all_option=false, $all_option_name=null)
368 {
369         global $systypes_array;
370         
371         $st = "<select name='$name'>";
372         if ($all_option == true)
373         {
374                 $reserved_word = reserved_words::get_all_numeric();
375         if ($reserved_word == $selected_id)
376              $st .= "<option selected value='" . $reserved_word . "'>" . $all_option_name . "</option>\n";
377         else 
378              $st .= "<option value='" . $reserved_word . "'>" . $all_option_name . "</option>\n";
379                 if ($selected_id == "") 
380                         $selected_id = $reserved_word;
381         }
382
383         foreach ($systypes_array as $i => $value)
384         {
385                 if ($i == 16 || $i == 18 || $i == 25 || $i >= 28)
386                         continue;
387         $st .= "<option" . ($selected_id == $i ? " selected" : "") . " value='$i'>" . $value['name'] . "</option>\n";
388         }       
389     $st .= "<select>";
390         return $st;
391 }
392
393
394 ?>