Bug when selecting report, List of Journal Entries, by type. Nothing selected.
[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 $comp_path, $path_to_root, $use_date_picker;
89
90                 $rep_file = $comp_path.'/'.user_company().
91                      "/reporting/rep".$this->id.".php";
92                 if (!file_exists($rep_file))
93                     $rep_file = $path_to_root ."/reporting/rep".$this->id.".php";
94
95                 $st = "
96 <script language='javascript'>
97         function displayReport_" . $this->id . "() {
98                 pParamCount = " . count($this->ar_params) . ";
99                 document.forms[0].REP_ID.value = " . $this->id . ";
100                 document.forms[0].PARAM_COUNT.value = pParamCount;
101                 for (i=0; i<pParamCount; i++) {
102                         eval('document.forms[0].PARAM_' + i + '.value=document.forms[0].RP_" . $this->id . "_' + i + '.value');
103                 }
104
105                 window.open('','REP_WINDOW','toolbar=no,scrollbar=no,resizable=yes,menubar=no');
106                 document.forms[0].target='REP_WINDOW';
107                 document.forms[0].action= '$rep_file';
108                 document.forms[0].submit();
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 .= "<input type='button' onclick='javascript:displayReport_" . $this->id ."()' value='" . _("Display: ") . $this->name . "'><br><br>";
119                 $dummy = "";
120                 if ($this->ar_params==null)
121                         return "";
122                 foreach($this->ar_params as $index=>$param)
123                 {
124                         $st .= $param->param_name . ':<br>';
125                         switch ($param->param_type)
126                         {
127                                 case 'CURRENCY':
128                                         $sql = "SELECT curr_abrev, concat(curr_abrev,' - ', currency) FROM ".TB_PREF."currencies";
129                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy, true, _("No Currency Filter"));
130                                         break;
131                                 case 'DATE':
132                                 case 'DATEBEGIN':
133                                 case 'DATEEND':
134                                 case 'DATEBEGINM':
135                                 case 'DATEENDM':
136                                 case 'DATEBEGINTAX':
137                                 case 'DATEENDTAX':
138                                         if ($param->param_type == 'DATEBEGIN')
139                                                 $date = begin_fiscalyear();
140                                         elseif ($param->param_type == 'DATEEND')
141                                                 $date = end_fiscalyear();
142                                         else
143                                                 $date = Today();
144                                         if ($param->param_type == 'DATEBEGINM')
145                                                 $date = begin_month($date);
146                                         elseif ($param->param_type == 'DATEENDM')
147                                                 $date = end_month($date);
148                                         elseif ($param->param_type == 'DATEBEGINTAX' || $param->param_type == 'DATEENDTAX')
149                                         {
150                                                 $row = get_company_prefs();
151                                                 $edate = add_months($date, -$row['tax_last']);
152                                                 $edate = end_month($edate);
153                                                 if ($param->param_type == 'DATEENDTAX')
154                                                         $date = $edate;
155                                                 else
156                                                 {
157                                                         $bdate = add_months($edate, -$row['tax_prd'] + 1);
158                                                         $date = begin_month($bdate);
159                                                 }
160                                         }
161                                         $name = "RP_" . $this->id . "_$index";
162                                         //$st .= "<input type='text' name='$name' value='$date' onblur='javascript:checkDate(this)'>";
163                                         $st .= "<input type='text' name='$name' value='$date'>";
164                                         if ($use_date_picker)
165                                                 $st .= "<a href=\"javascript:date_picker(document.forms[0].$name);\">"
166                                                 . "     <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";
167
168                                         break;
169                                 case 'YES_NO':
170                                         $sel = array(_('No'), _("Yes"));
171                                         $st .= dup_simple_name_list("RP_" . $this->id . "_$index", $sel);
172                                         break;
173                                 case 'PAYMENT_LINK':
174                                         $sel = array(_("No Payment Link"), "PayPal");
175                                         $st .= dup_simple_name_list("RP_" . $this->id . "_$index", $sel);
176                                         break;
177                                 case 'COMPARE':
178                                         $sel = array(_("Accumulated"), _("Period Y-1"), _("Budget"));
179                                         $st .= dup_simple_name_list("RP_" . $this->id . "_$index", $sel);
180                                         break;
181                                 case 'GRAPHIC':
182                                         $sel = array(_("No Graphics"), _("Vertical bars"), _("Horizontal bars"), _("Dots"), _("Lines"), _("Pie"), _("Donut"));
183                                         $st .= dup_simple_name_list("RP_" . $this->id . "_$index", $sel);
184                                         break;
185                                 case 'SYS_TYPES':
186                                         $st .= dup_systypes_list("RP_" . $this->id . "_$index", $dummy, true, _("No Type Filter"), true);
187                                         break;
188                                 case 'TEXT':
189                                         $st .= "<input type='text' name='RP_" . $this->id . "_$index'>";
190                                         break;
191                                 case 'TEXTBOX':
192                                         $st .= "<textarea rows=4 cols=30 name='RP_" . $this->id . "_$index'></textarea>";
193                                         break;
194                                 case 'ACCOUNTS':
195                                         $sql = "SELECT id, name FROM ".TB_PREF."chart_types ORDER BY name";
196                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy, true, _("No Account Group Filter"), true);
197                                         break;
198                                 case 'GL_ACCOUNTS':
199                                         $sql = "SELECT account_code, concat(account_code, ' - ', account_name) as account_name FROM ".TB_PREF."chart_master ORDER BY account_code";
200                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
201                                         break;
202                                 case 'BANK_ACCOUNTS':
203                                         $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
204                                                 WHERE ".TB_PREF."bank_accounts.account_code=".TB_PREF."chart_master.account_code";
205                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
206                                         break;
207                                 case 'DIMENSION':
208                                         $sql = "SELECT reference, concat(reference, ' - ', name) AS DimName FROM ".TB_PREF."dimensions ORDER BY reference";
209                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
210                                         break;
211                                 case 'DIMENSIONS':
212                                         $sql = "SELECT reference, concat(reference, ' - ', name) as DimName FROM ".TB_PREF."dimensions ORDER BY reference";
213                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy, true, _("No Dimension Filter"), true);
214                                         break;
215                                 case 'DIMENSION1':
216                                         $sql = "SELECT reference, concat(reference, ' - ', name) AS DimName FROM ".TB_PREF."dimensions WHERE type_=1 ORDER BY reference";
217                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
218                                         break;
219                                 case 'DIMENSIONS1':
220                                         $sql = "SELECT reference, concat(reference, ' - ', name) as DimName FROM ".TB_PREF."dimensions WHERE type_=1 ORDER BY reference";
221                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy, true, _("No Dimension Filter"), true);
222                                         break;
223                                 case 'DIMENSION2':
224                                         $sql = "SELECT reference, concat(reference, ' - ', name) AS DimName FROM ".TB_PREF."dimensions WHERE type_=2 ORDER BY reference";
225                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
226                                         break;
227                                 case 'DIMENSIONS2':
228                                         $sql = "SELECT reference, concat(reference, ' - ', name) as DimName FROM ".TB_PREF."dimensions WHERE type_=2 ORDER BY reference";
229                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy, true, _("No Dimension Filter"), true);
230                                         break;
231                                 case 'CUSTOMERS_NO_FILTER':
232                                 case 'CUSTOMERS':
233                                         $sql = "SELECT debtor_no, name FROM ".TB_PREF."debtors_master ORDER BY name";
234                                         if ($param->param_type == 'CUSTOMERS_NO_FILTER')
235                                                 $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy, true, _("No Customer Filter"), true);
236                                         else
237                                                 $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
238                                         break;
239                                 case 'SUPPLIERS_NO_FILTER':
240                                 case 'SUPPLIERS':
241                                         $sql = "SELECT supplier_id, supp_name FROM ".TB_PREF."suppliers ORDER BY supp_name";
242                                         if ($param->param_type == 'SUPPLIERS_NO_FILTER')
243                                                 $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy, true, _("No Supplier Filter"), true);
244                                         else
245                                                 $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
246                                         break;
247                                 case 'INVOICE':
248                                         $IV = _("IV");
249                                         $CN = _("CN");
250                                         $sql = "SELECT concat(".TB_PREF."debtor_trans.trans_no, '-',
251                                                 ".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
252                                                 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";
253                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
254                                         break;
255                                 case 'DELIVERY':
256                                         $DN = _("DN");
257                                         $sql = "SELECT
258                                         concat(".TB_PREF."debtor_trans.trans_no, '-', ".TB_PREF."debtor_trans.type) AS TNO, concat(".TB_PREF."debtor_trans.trans_no, ' $DN ',
259                                          ".TB_PREF."debtors_master.name) as IName
260                                                 FROM ".TB_PREF."debtors_master, ".TB_PREF."debtor_trans
261                                                 WHERE type=13 AND ".TB_PREF."debtors_master.debtor_no=".
262                                                 TB_PREF."debtor_trans.debtor_no ORDER BY ".TB_PREF."debtor_trans.trans_no DESC";
263                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
264                                         break;
265                                 case 'ORDERS':
266                                         $sql = "SELECT ".TB_PREF."sales_orders.order_no, concat(".TB_PREF."sales_orders.order_no, '-',
267                                                 ".TB_PREF."debtors_master.name) as IName
268                                                 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";
269                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
270                                         break;
271                                 case 'PO':
272                                         $sql = "SELECT ".TB_PREF."purch_orders.order_no, concat(".TB_PREF."purch_orders.order_no, '-',
273                                                 ".TB_PREF."suppliers.supp_name) as IName
274                                                 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";
275                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
276                                         break;
277                                 case 'ITEMS':
278                                         $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";
279                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
280                                         break;
281                                 case 'LOCATIONS':
282                                         $sql = "SELECT loc_code, location_name FROM ".TB_PREF."locations ORDER BY location_name";
283                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy, true, _("No Location Filter"), false);
284                                         break;
285                                 case 'CATEGORIES':
286                                         $sql = "SELECT category_id, description FROM ".TB_PREF."stock_category ORDER BY description";
287                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy, true, _("No Category Filter"), true);
288                                         break;
289                                 case 'SALESTYPES':
290                                         $sql = "SELECT id, sales_type FROM ".TB_PREF."sales_types ORDER BY sales_type";
291                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
292                                         break;
293                                 case 'AREAS':
294                                         $sql = "SELECT area_code, description FROM ".TB_PREF."areas ORDER BY description";
295                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy, true, _("No Area Filter"), true);
296                                         break;
297                                 case 'SALESMEN':
298                                         $sql = "SELECT salesman_code, salesman_name FROM ".TB_PREF."salesman ORDER BY salesman_name";
299                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy, true, _("No Sales Folk Filter"), true);
300                                         break;
301                                 case 'TRANS_YEARS':
302                                         $sql = "SELECT DISTINCT YEAR(tran_date), YEAR(tran_date) FROM ".TB_PREF."gl_trans";
303                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
304                                         break;
305                                 case 'ACCOUNTS_NO_FILTER':
306                                         $sql = "SELECT id, name FROM ".TB_PREF."chart_types ORDER BY name";
307                                         $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
308                                         break;
309
310                         }
311                         $st .= "<br><br>";
312                 }
313                 return $st;
314         }
315 }
316
317 class ReportParam
318 {
319         var $param_name;
320         var $param_type;
321
322         function ReportParam($param_name, $param_type)
323         {
324                 $this->param_name = $param_name;
325                 $this->param_type = $param_type;
326         }
327 }
328
329 function dup_simple_codeandname_list($sql, $name, &$selected_id,
330         $all_option=false, $all_option_name=null, $all_option_numeric=false,
331         $submit_on_change=false)
332 {
333         if ($submit_on_change == true)
334                 $st = "<select name='$name' onchange='this.form.submit();'>";
335         else
336                 $st = "<select name='$name'>";
337
338         if ($all_option == true)
339         {
340                 if ($all_option_numeric)
341                         $reserved_word = reserved_words::get_all_numeric();
342                 else
343                         $reserved_word = reserved_words::get_all();
344
345         if ($reserved_word == $selected_id)
346         {
347              $st .= "<option selected value='" . $reserved_word . "'>" . $all_option_name . "</option>\n";
348         }
349         else
350         {
351              $st .= "<option value='" . $reserved_word . "'>" . $all_option_name . "</option>\n";
352         }
353                 if ($selected_id == "")
354                 {
355                         $selected_id =  $reserved_word;
356                 }
357         }
358
359         $result = db_query($sql);
360
361         while ($row = db_fetch_row($result))
362         {
363                 if ($selected_id == $row[0])
364                 {
365                         $st .= "<option selected value='" . $row[0] . "'>";
366                 }
367                 else
368                 {
369                         $st .= "<option value='" . $row[0] . "'>";
370                 }
371                 $st .= $row[1] . "</option>\n";
372
373                 if ($selected_id == "")
374                 {
375                         $selected_id = $row[0];
376                 }
377         }
378
379         $st .= "</select>";
380         db_free_result($result);
381
382         return $st;
383 }
384
385 function dup_simple_name_list($name, $selections)
386 {
387         $st = "<select name='$name'>";
388         for ($i = 0; $i < count($selections); $i++)
389                 $st .= "<option value='" . $i . "'>" . $selections[$i] . "</option>\n";
390         $st .= "</select>";
391         return $st;
392 }
393
394 //------------------------------------------------------------------------------------------------
395
396 function dup_systypes_list($name, &$selected_id, $all_option=false, $all_option_name=null)
397 {
398         global $systypes_array;
399
400         $st = "<select name='$name'>";
401         if ($all_option == true)
402         {
403                 $reserved_word = reserved_words::get_all_numeric();
404         if ($reserved_word == $selected_id)
405              $st .= "<option selected value='" . $reserved_word . "'>" . $all_option_name . "</option>\n";
406         else
407              $st .= "<option value='" . $reserved_word . "'>" . $all_option_name . "</option>\n";
408                 if ($selected_id == "")
409                         $selected_id = $reserved_word;
410         }
411
412         foreach ($systypes_array as $i => $value)
413         {
414                 if ($i == 16 || $i == 18 || $i == 25 || ($i >= 28 && $i <= 30) || $i >= 40)
415                         continue;
416         $st .= "<option" . ($selected_id == $i ? " selected" : "") . " value='$i'>" . $value['name'] . "</option>\n";
417         }
418     $st .= "</select>";
419         return $st;
420 }
421
422
423 ?>