Merging changes form main trunk 2.2.5-2.2.6
[fa-stable.git] / reporting / includes / reports_classes.inc
index e180ed3b5fde15a8cb85e2bc51f0e0ffdd1d4c27..97bfa0f6e2845a4b5aba147316831815a8d48968 100644 (file)
@@ -1,7 +1,18 @@
 <?php
+/**********************************************************************
+    Copyright (C) FrontAccounting, LLC.
+       Released under the terms of the GNU General Public License, GPL, 
+       as published by the Free Software Foundation, either version 3 
+       of the License, or (at your option) any later version.
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
+    See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
+***********************************************************************/
 class BoxReports
 {
        var $ar_classes;
+       var $ctrl_handlers = array();
 
        function ReportClasses()
        {
@@ -15,12 +26,15 @@ class BoxReports
 
        function addReport($class_name, $id, $rep_name, $params=null)
        {
-               $this->ar_classes[$class_name][] = new Report($id,$rep_name,$params);
+               unset($this->ar_classes[$class_name][$id]); // unset std report if any
+               $this->ar_classes[$class_name][$id] = new Report($id, $rep_name, $params);
        }
 
        function getDisplay($class=null)
        {
-               global $table_style2;
+               global $table_style, $comp_path, $path_to_root, $pdf_debug, $Ajax;
+
+
                $temp = array_values($this->ar_classes);
                $display_class = $class==null ? $temp[0] : $this->ar_classes[$class];
                $class_counter = 0;
@@ -30,39 +44,62 @@ class BoxReports
                $st_classes = "<b>" . _("Report Classes:") . "</b><br>";
                foreach($this->ar_classes as $key=>$value)
                {
-                       $style = $class_counter==0 ? '' : $style = "style='display:none'";
+                       $style = $class_counter==$_REQUEST['Class'] ? '' : "style='display:none'";
                        $acc = access_string($key);
-                       $st_classes .= "<a href='javascript:showClass($class_counter)'$acc[1]>$acc[0]</a> <br>";
-                       $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>";
+                       $st_classes .= "<a href='"
+                               .$_SERVER['PHP_SELF']."?Class=$class_counter'"
+                               ." class='menu_option' id='".default_focus()."'"
+                               ." onclick='return showClass($class_counter);'$acc[1]>$acc[0]</a> <br>";
+                       $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";
                        foreach($value as $report)
-                       {       $acc = access_string($report->name);
-                               $st_reports .= "<tr><td><a href='javascript:showReport($rep_counter)'$acc[1]>$acc[0]</a></td></tr>";
-                               $st_params .= "<table border=0 id='REP_" . $rep_counter++ . "' style='display:none'>";
-                               $st_params .= "<tr><td>" . $report->getDisplay() . "</td></tr></table>";
+                       {
+                               $acc = access_string($report->name);
+                               $st_reports .= "<tr><td><a class='printlink' href='"
+                                       .$_SERVER['PHP_SELF']."?Class=$class_counter&rep_id=$report->id'"
+                                       ." id='".default_focus()."'"
+                                       ."$acc[1]>$acc[0]</a><tr><td>\n";
+                               if (isset($_REQUEST['rep_id']) && $_REQUEST['rep_id']==$report->id) {
+
+                                       $action = $path_to_root.'/reporting/prn_redirect.php';
+       
+                                       $st_params = "<table border=0><tr><td>\n"
+                                               . "<form method='POST' action='$action' target='_blank'>\n";
+                                       $st_params .= submit('Rep'.$report->id,  
+                                               _("Display: ") . access_string($report->name, true),
+                                               false, '', $pdf_debug ? false : 'default') . hidden('REP_ID', $report->id, false).'<br><br>';
+                                       $st_params .= $this->getOptions($report->get_controls());
+                                       $st_params .= "\n</form></td></tr></table>\n";
+                                       set_focus('Rep'.$report->id);
+                                       $Ajax->addUpdate(true, 'rep_form', $st_params);
+                               }
                        }
                        $st_reports .= "</table>";
+                       $class_counter++;
                }
 
+               $st_params = "<div id='rep_form'>".
+                       "$st_params</div>";
+               
+
                $st =   "<script language='javascript'>
                                        function showClass(pClass) {
                                                for(i=0; i<$class_counter; i++) {
-                                                       eval('document.getElementById(\"TAB_\" + i).style.display=\"none\"')
-                                               }
-                                               eval('document.getElementById(\"TAB_\" + pClass).style.display=\"block\"')
-                                               for (i=0; i<$rep_counter; i++) {
-                                                       eval('document.getElementById(\"REP_\" + i).style.display=\"none\"')
+                                                       document.getElementById(\"TAB_\" + i).style.display=
+                                                       i==pClass ? \"block\" : \"none\";
                                                }
+                                               document.getElementById('rep_form').innerHTML = '';
+//                                             document.getElementById('rep_form').style.display = 'none';
+                                               return false;
                                        }
-                                       function showReport(pId) {
-                                               var tab;
-                                               for(i=0; i<$rep_counter; i++) {
-                                                       eval('document.getElementById(\"REP_\" + i).style.display=\"none\"')
+                                       function checkDate(pObj) {
+                                               var re = /^(3[01]|0[1-9]|[12]\d)\/(0[1-9]|1[012])\/\d{4}/;
+                                               if (re.test(pObj.value)==false) {
+                                                       alert('" . _("Invalid date format") . "')
                                                }
-                                               eval('document.getElementById(\"REP_\" + pId).style.display=\"block\"')
                                        }
                                </script>
                                ";
-               $st .= "<table align='center' width='80%' $table_style2><tr valign='top'>";
+               $st .= "<table align='center' width='80%'><tr valign='top'>";
                $st .= "<td width='30%'>$st_classes</td>";
                $st .= "<td width='35%'>$st_reports</td>";
                $st .= "<td width='35%'>$st_params</td>";
@@ -70,65 +107,56 @@ class BoxReports
 
                return $st;
        }
-}
-
-class Report
-{
-       var $id;
-       var $name;
-       var $ar_params;
 
-       function Report($id, $name, $ar_params)
+       function getOptions($controls)
        {
-               $this->id                       = $id;
-               $this->name             = $name;
-               $this->ar_params        = $ar_params;
-       }
-       function getDisplay()
-       {
-               global $comp_path, $path_to_root, $use_date_picker;
-
-               $rep_file = $comp_path.'/'.user_company().
-                    "/reporting/rep".$this->id.".php";
-               if (!file_exists($rep_file))
-                   $rep_file = $path_to_root ."/reporting/rep".$this->id.".php";
-
-               $st = "
-<script language='javascript'>
-       function displayReport_" . $this->id . "() {
-               pParamCount = " . count($this->ar_params) . ";
-               document.forms[0].REP_ID.value = " . $this->id . ";
-               document.forms[0].PARAM_COUNT.value = pParamCount;
-               for (i=0; i<pParamCount; i++) {
-                       eval('document.forms[0].PARAM_' + i + '.value=document.forms[0].RP_" . $this->id . "_' + i + '.value');
-               }
+               $st = '';
+               if ($controls == null)
+                       return "";
 
-               window.open('','REP_WINDOW','toolbar=no,scrollbar=no,resizable=yes,menubar=no');
-               document.forms[0].target='REP_WINDOW';
-               document.forms[0].action= '$rep_file';
-               document.forms[0].submit();
-       }
-       function checkDate(pObj) {
-               var re = /^(3[01]|0[1-9]|[12]\d)\/(0[1-9]|1[012])\/\d{4}/;
-               if (re.test(pObj.value)==false) {
-                       alert('" . _("Invalid date format") . "')
+               $cnt = 0;
+               foreach($controls as $title => $type)
+               {
+                       $ctrl = '';
+                       foreach($this->ctrl_handlers as $fun) { // first check for non-standard controls
+                               $ctrl = $fun('PARAM_'.$cnt, $type);
+                               if ($ctrl) break;
+                       }
+                       if ($ctrl == '') {
+                               $ctrl = $this->get_ctrl('PARAM_'.$cnt, $type);
+                       }
+                       if ($ctrl != '')  {
+                               $st .= $title . ':<br>';
+                               $st .= $ctrl;
+                               $st .= "<br><br>";
+                       } else {
+                               display_error(_('Unknown report parameter type:').$type);
+                       }
+                       $cnt++;
                }
+               return $st;
        }
-</script>
-                       ";
-               $st .= "<input type='button' onclick='javascript:displayReport_" . $this->id ."()' value='" . _("Display: ") . $this->name . "'><br><br>";
-               $dummy = "";
-               if ($this->ar_params==null)
-                       return "";
-               foreach($this->ar_params as $index=>$param)
-               {
-                       $st .= $param->param_name . ':<br>';
-                       switch ($param->param_type)
+
+       //
+       //      Register additional control handler
+       // $handle - name of global function f($name, $type) returning html code for control
+       function register_controls($handler) {
+               $this->ctrl_handlers[] = $handler;
+       }
+       //
+       //      Returns html code for input control $name of type $type 
+       //
+       function get_ctrl($name, $type)
+       {
+               global $path_to_root, $use_date_picker, $pdf_debug, $print_invoice_no;
+
+               $st = '';
+                       switch ($type)
                        {
                                case 'CURRENCY':
-                                       $sql = "SELECT curr_abrev, concat(curr_abrev,' - ', currency) FROM ".TB_PREF."currencies";
-                                       $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy, true, _("No Currency Filter"));
-                                       break;
+                                       $sql = "SELECT curr_abrev, concat(curr_abrev,' - ', currency) AS name FROM ".TB_PREF."currencies";
+                                       return combo_input($name, '', $sql, 'curr_abrev', 'name',array('spec_option'=>_("No Currency Filter"),'spec_id'=>ALL_TEXT,'order'=>false));
+
                                case 'DATE':
                                case 'DATEBEGIN':
                                case 'DATEEND':
@@ -136,289 +164,290 @@ class Report
                                case 'DATEENDM':
                                case 'DATEBEGINTAX':
                                case 'DATEENDTAX':
-                                       if ($param->param_type == 'DATEBEGIN')
+                                       if ($type == 'DATEBEGIN')
                                                $date = begin_fiscalyear();
-                                       elseif ($param->param_type == 'DATEEND')
+                                       elseif ($type == 'DATEEND')
                                                $date = end_fiscalyear();
                                        else
                                                $date = Today();
-                                       if ($param->param_type == 'DATEBEGINM')
+                                       if ($type == 'DATEBEGINM')
                                                $date = begin_month($date);
-                                       elseif ($param->param_type == 'DATEENDM')
+                                       elseif ($type == 'DATEENDM')
                                                $date = end_month($date);
-                                       elseif ($param->param_type == 'DATEBEGINTAX' || $param->param_type == 'DATEENDTAX')
+                                       elseif ($type == 'DATEBEGINTAX' || $type == 'DATEENDTAX')
                                        {
                                                $row = get_company_prefs();
                                                $edate = add_months($date, -$row['tax_last']);
                                                $edate = end_month($edate);
-                                               if ($param->param_type == 'DATEENDTAX')
+                                               if ($type == 'DATEENDTAX')
                                                        $date = $edate;
                                                else
                                                {
-                                                       $bdate = add_months($edate, -$row['tax_prd'] + 1);
-                                                       $date = begin_month($bdate);
+                                                       $bdate = begin_month($edate);
+                                                       $bdate = add_months($bdate, -$row['tax_prd'] + 1);
+                                                       $date = $bdate;
                                                }
                                        }
-                                       $name = "RP_" . $this->id . "_$index";
-                                       //$st .= "<input type='text' name='$name' value='$date' onblur='javascript:checkDate(this)'>";
-                                       $st .= "<input type='text' name='$name' value='$date'>";
+
+                                       $st = "<input type='text' name='$name' value='$date'>";
                                        if ($use_date_picker)
                                                $st .= "<a href=\"javascript:date_picker(document.forms[0].$name);\">"
                                                . "     <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";
-
+                                       return $st;
                                        break;
+
                                case 'YES_NO':
-                                       $sel = array(_('No'), _("Yes"));
-                                       $st .= dup_simple_name_list("RP_" . $this->id . "_$index", $sel);
-                                       break;
+                                       return yesno_list($name);
+
                                case 'PAYMENT_LINK':
-                                       $sel = array(_("No Payment Link"), "PayPal");
-                                       $st .= dup_simple_name_list("RP_" . $this->id . "_$index", $sel);
-                                       break;
+                                       $sel = array(_("No payment Link"), "PayPal");
+                                       return array_selector($name, null, $sel);
+
+                               case 'DESTINATION':
+                                       $sel = array(_("PDF/Printer"), "Excel");
+                                       return array_selector($name, null, $sel);
+
                                case 'COMPARE':
                                        $sel = array(_("Accumulated"), _("Period Y-1"), _("Budget"));
-                                       $st .= dup_simple_name_list("RP_" . $this->id . "_$index", $sel);
-                                       break;
+                                       return array_selector($name, null, $sel);
+
                                case 'GRAPHIC':
-                                       $sel = array(_("No Graphics"), _("Vertical bars"), _("Horizontal bars"), _("Dots"), _("Lines"), _("Pie"), _("Donut"));
-                                       $st .= dup_simple_name_list("RP_" . $this->id . "_$index", $sel);
-                                       break;
+                                       $sel = array(_("No Graphics"), _("Vertical bars"), _("Horizontal bars"), 
+                                                               _("Dots"), _("Lines"), _("Pie"), _("Donut"));
+                                       return array_selector($name, null, $sel);
+
                                case 'SYS_TYPES':
-                                       $st .= dup_systypes_list("RP_" . $this->id . "_$index", $dummy, true, _("No Type Filter"), true);
-                                       break;
+                                       return gl_systypes_list($name, null, _("No Type Filter"));
+
+                               case 'SYS_TYPES_ALL':
+                                       return systypes_list($name, null, _("No Type Filter"));
+
                                case 'TEXT':
-                                       $st .= "<input type='text' name='RP_" . $this->id . "_$index'>";
-                                       break;
+                                       return "<input type='text' name='$name'>";
+
                                case 'TEXTBOX':
-                                       $st .= "<textarea rows=4 cols=30 name='RP_" . $this->id . "_$index'></textarea>";
-                                       break;
-                               case 'ACCOUNTS':
-                                       $sql = "SELECT id, name FROM ".TB_PREF."chart_types ORDER BY name";
-                                       $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy, true, _("No Account Group Filter"), true);
-                                       break;
+                                       return "<textarea rows=4 cols=30 name='$name'></textarea>";
+
+                               case 'ACCOUNTS': // not used
+                                       return gl_account_types_list($name, null, _("No Account Group Filter"), true);
+
+                               case 'ACCOUNTS_NO_FILTER': // not used
+                                       return gl_account_types_list($name);
+
                                case 'GL_ACCOUNTS':
-                                       $sql = "SELECT account_code, concat(account_code, ' - ', account_name) as account_name FROM ".TB_PREF."chart_master ORDER BY account_code";
-                                       $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
-                                       break;
+                                       return gl_all_accounts_list($name);
+
                                case 'BANK_ACCOUNTS':
-                                       $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
-                                               WHERE ".TB_PREF."bank_accounts.account_code=".TB_PREF."chart_master.account_code";
-                                       $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
-                                       break;
+                                       return bank_accounts_list($name);
+                                       
                                case 'DIMENSION':
-                                       $sql = "SELECT reference, concat(reference, ' - ', name) AS DimName FROM ".TB_PREF."dimensions ORDER BY reference";
-                                       $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
-                                       break;
+                                       return dimensions_list($name, null, false, ' ', false, true, 0);
+                                       
                                case 'DIMENSIONS':
-                                       $sql = "SELECT reference, concat(reference, ' - ', name) as DimName FROM ".TB_PREF."dimensions ORDER BY reference";
-                                       $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy, true, _("No Dimension Filter"), true);
-                                       break;
+                                       return dimensions_list($name, null, true, _("No Dimension Filter"), false, true, 0);
+
                                case 'DIMENSION1':
-                                       $sql = "SELECT reference, concat(reference, ' - ', name) AS DimName FROM ".TB_PREF."dimensions WHERE type_=1 ORDER BY reference";
-                                       $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
-                                       break;
+                                       return dimensions_list($name, null, false, ' ', false, true, 1);
+
                                case 'DIMENSIONS1':
-                                       $sql = "SELECT reference, concat(reference, ' - ', name) as DimName FROM ".TB_PREF."dimensions WHERE type_=1 ORDER BY reference";
-                                       $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy, true, _("No Dimension Filter"), true);
-                                       break;
+                                       return dimensions_list($name, null, true, _("No Dimension Filter"), false, true, 1);
+
                                case 'DIMENSION2':
-                                       $sql = "SELECT reference, concat(reference, ' - ', name) AS DimName FROM ".TB_PREF."dimensions WHERE type_=2 ORDER BY reference";
-                                       $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
-                                       break;
+                                       return dimensions_list($name, null, false, ' ', false, true, 2);
+
                                case 'DIMENSIONS2':
-                                       $sql = "SELECT reference, concat(reference, ' - ', name) as DimName FROM ".TB_PREF."dimensions WHERE type_=2 ORDER BY reference";
-                                       $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy, true, _("No Dimension Filter"), true);
-                                       break;
+                                       return dimensions_list($name, null, true, _("No Dimension Filter"), false, true, 2);
+
                                case 'CUSTOMERS_NO_FILTER':
                                case 'CUSTOMERS':
-                                       $sql = "SELECT debtor_no, name FROM ".TB_PREF."debtors_master ORDER BY name";
-                                       if ($param->param_type == 'CUSTOMERS_NO_FILTER')
-                                               $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy, true, _("No Customer Filter"), true);
+                                       $sql = "SELECT debtor_no, name FROM ".TB_PREF."debtors_master";
+                                       if ($type == 'CUSTOMERS_NO_FILTER')
+                                               return combo_input($name, '', $sql, 'debtor_no', 'name',array('spec_option'=>_("No Customer Filter"),'spec_id'=>ALL_NUMERIC));
+// FIX allitems numeric!
+//                                             return customer_list($name, null, _("No Customer Filter"));
                                        else
-                                               $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
-                                       break;
+                                               return combo_input($name, '', $sql, 'debtor_no', 'name', null);
+//                                             return customer_list($name);
+
                                case 'SUPPLIERS_NO_FILTER':
                                case 'SUPPLIERS':
-                                       $sql = "SELECT supplier_id, supp_name FROM ".TB_PREF."suppliers ORDER BY supp_name";
-                                       if ($param->param_type == 'SUPPLIERS_NO_FILTER')
-                                               $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy, true, _("No Supplier Filter"), true);
+                                       $sql = "SELECT supplier_id, supp_name FROM ".TB_PREF."suppliers";
+                                       if ($type == 'SUPPLIERS_NO_FILTER')
+                                               return combo_input($name, '', $sql, 'supplier_id', 'supp_name',array('spec_option'=>_("No Supplier Filter"),'spec_id'=>ALL_NUMERIC));
+// FIX allitems numeric!
+//                                             return supplier_list($name, null, _("No Supplier Filter"));
                                        else
-                                               $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
-                                       break;
+                                               return combo_input($name, '', $sql, 'supplier_id', 'supp_name', null);
+//                                             return supplier_list($name);
+
                                case 'INVOICE':
                                        $IV = _("IV");
                                        $CN = _("CN");
+                                       $ref = ($print_invoice_no == 1 ? "trans_no" : "reference");
                                        $sql = "SELECT concat(".TB_PREF."debtor_trans.trans_no, '-',
-                                               ".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
-                                               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";
-                                       $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
-                                       break;
+                                               ".TB_PREF."debtor_trans.type) AS TNO, concat(".TB_PREF."debtor_trans.$ref, if (type=".ST_SALESINVOICE.", ' $IV ', ' $CN '), ".TB_PREF."debtors_master.name) as IName
+                                               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";
+                                       return combo_input($name, '', $sql, 'TNO', 'IName',array('order'=>false));
+
                                case 'DELIVERY':
                                        $DN = _("DN");
                                        $sql = "SELECT
                                        concat(".TB_PREF."debtor_trans.trans_no, '-', ".TB_PREF."debtor_trans.type) AS TNO, concat(".TB_PREF."debtor_trans.trans_no, ' $DN ',
                                         ".TB_PREF."debtors_master.name) as IName
                                                FROM ".TB_PREF."debtors_master, ".TB_PREF."debtor_trans
-                                               WHERE type=13 AND ".TB_PREF."debtors_master.debtor_no=".
+                                               WHERE type=".ST_CUSTDELIVERY." AND ".TB_PREF."debtors_master.debtor_no=".
                                                TB_PREF."debtor_trans.debtor_no ORDER BY ".TB_PREF."debtor_trans.trans_no DESC";
-                                       $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
-                                       break;
+                                       return combo_input($name, '', $sql, 'TNO', 'IName',array('order'=>false));
+
                                case 'ORDERS':
-                                       $sql = "SELECT ".TB_PREF."sales_orders.order_no, concat(".TB_PREF."sales_orders.order_no, '-',
+                                       $ref = ($print_invoice_no == 1 ? "order_no" : "reference");
+                                       $sql = "SELECT ".TB_PREF."sales_orders.order_no, concat(".TB_PREF."sales_orders.$ref, '-',
                                                ".TB_PREF."debtors_master.name) as IName
-                                               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";
-                                       $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
-                                       break;
+                                               FROM ".TB_PREF."debtors_master, ".TB_PREF."sales_orders WHERE ".TB_PREF."debtors_master.debtor_no=".TB_PREF."sales_orders.debtor_no 
+                                               AND ".TB_PREF."sales_orders.trans_type=".ST_SALESORDER." ORDER BY ".TB_PREF."sales_orders.order_no DESC";
+                                       return combo_input($name, '', $sql, 'order_no', 'IName',array('order'=>false));
+
+                               case 'QUOTATIONS':
+                                       $ref = ($print_invoice_no == 1 ? "order_no" : "reference");
+                                       $sql = "SELECT ".TB_PREF."sales_orders.order_no, concat(".TB_PREF."sales_orders.$ref, '-',
+                                               ".TB_PREF."debtors_master.name) as IName
+                                               FROM ".TB_PREF."debtors_master, ".TB_PREF."sales_orders WHERE ".TB_PREF."debtors_master.debtor_no=".TB_PREF."sales_orders.debtor_no 
+                                               AND ".TB_PREF."sales_orders.trans_type=".ST_SALESQUOTE." ORDER BY ".TB_PREF."sales_orders.order_no DESC";
+                                       return combo_input($name, '', $sql, 'order_no', 'IName',array('order'=>false));
+
                                case 'PO':
-                                       $sql = "SELECT ".TB_PREF."purch_orders.order_no, concat(".TB_PREF."purch_orders.order_no, '-',
+                                       $ref = ($print_invoice_no == 1 ? "order_no" : "reference");
+                                       $sql = "SELECT ".TB_PREF."purch_orders.order_no, concat(".TB_PREF."purch_orders.$ref, '-',
                                                ".TB_PREF."suppliers.supp_name) as IName
                                                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";
-                                       $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
-                                       break;
+                                       return combo_input($name, '', $sql, 'order_no', 'IName',array('order'=>false));
+
+                               case 'REMITTANCE':
+                                       $BP = _("BP");
+                                       $SP = _("SP");
+                                       $CN = _("CN");
+                                       $ref = ($print_invoice_no == 1 ? "trans_no" : "reference");
+                                       $sql = "SELECT concat(".TB_PREF."supp_trans.trans_no, '-',
+                                               ".TB_PREF."supp_trans.type) AS TNO, concat(".TB_PREF."supp_trans.$ref, if (type=".ST_BANKPAYMENT.", ' $BP ', if (type=".ST_SUPPAYMENT.", ' $SP ', ' $CN ')), ".TB_PREF."suppliers.supp_name) as IName
+                                               FROM ".TB_PREF."suppliers, ".TB_PREF."supp_trans WHERE (type=".ST_BANKPAYMENT." OR type=".ST_SUPPAYMENT." OR type=".ST_SUPPCREDIT.") AND ".TB_PREF."suppliers.supplier_id=".TB_PREF."supp_trans.supplier_id ORDER BY ".TB_PREF."supp_trans.trans_no DESC";
+                                       return combo_input($name, '', $sql, 'TNO', 'IName',array('order'=>false));
+
+                               case 'RECEIPT':
+                                       $BD = _("BD");
+                                       $CP = _("CP");
+                                       $CN = _("CN");
+                                       $ref = ($print_invoice_no == 1 ? "trans_no" : "reference");
+                                       $sql = "SELECT concat(".TB_PREF."debtor_trans.trans_no, '-',
+                                               ".TB_PREF."debtor_trans.type) AS TNO, concat(".TB_PREF."debtor_trans.$ref, if (type=".ST_BANKDEPOSIT.", ' $BD ', if (type=".ST_CUSTPAYMENT.", ' $CP ', ' $CN ')), ".TB_PREF."debtors_master.name) as IName
+                                               FROM ".TB_PREF."debtors_master, ".TB_PREF."debtor_trans WHERE (type=".ST_BANKDEPOSIT." OR type=".ST_CUSTPAYMENT." OR type=".ST_CUSTCREDIT.") AND ".TB_PREF."debtors_master.debtor_no=".TB_PREF."debtor_trans.debtor_no ORDER BY ".TB_PREF."debtor_trans.trans_no DESC";
+                                       return combo_input($name, '', $sql, 'TNO', 'IName',array('order'=>false));
+
                                case 'ITEMS':
-                                       $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";
-                                       $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
-                                       break;
+                                       return stock_manufactured_items_list($name);
+
+                               case 'WORKORDER':
+                                       $sql = "SELECT ".TB_PREF."workorders.id, concat(".TB_PREF."workorders.id, '-',
+                                               ".TB_PREF."stock_master.description) as IName
+                                               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";
+                                       return combo_input($name, '', $sql, 'id', 'IName',array('order'=>false));
+
                                case 'LOCATIONS':
-                                       $sql = "SELECT loc_code, location_name FROM ".TB_PREF."locations ORDER BY location_name";
-                                       $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy, true, _("No Location Filter"), false);
-                                       break;
+                                       return  locations_list($name, null, _("No Location Filter"));
+
                                case 'CATEGORIES':
-                                       $sql = "SELECT category_id, description FROM ".TB_PREF."stock_category ORDER BY description";
-                                       $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy, true, _("No Category Filter"), true);
-                                       break;
+                                       return stock_categories_list($name, null, _("No Category Filter"));
+
                                case 'SALESTYPES':
-                                       $sql = "SELECT id, sales_type FROM ".TB_PREF."sales_types ORDER BY sales_type";
-                                       $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
-                                       break;
+                                       return sales_types_list($name);
+
                                case 'AREAS':
-                                       $sql = "SELECT area_code, description FROM ".TB_PREF."areas ORDER BY description";
-                                       $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy, true, _("No Area Filter"), true);
-                                       break;
+                                       return sales_areas_list($name);
+
                                case 'SALESMEN':
-                                       $sql = "SELECT salesman_code, salesman_name FROM ".TB_PREF."salesman ORDER BY salesman_name";
-                                       $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy, true, _("No Sales Folk Filter"), true);
-                                       break;
+                                       return sales_persons_list($name, null, _("No Sales Folk Filter"));
+                                       
                                case 'TRANS_YEARS':
-                                       $sql = "SELECT DISTINCT YEAR(tran_date), YEAR(tran_date) FROM ".TB_PREF."gl_trans";
-                                       $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
-                                       break;
-                               case 'ACCOUNTS_NO_FILTER':
-                                       $sql = "SELECT id, name FROM ".TB_PREF."chart_types ORDER BY name";
-                                       $st .= dup_simple_codeandname_list($sql, "RP_" . $this->id . "_$index", $dummy);
-                                       break;
+                                       return fiscalyears_list($name);
 
-                       }
-                       $st .= "<br><br>";
-               }
-               return $st;
-       }
-}
+                               case 'USERS':
+                                       $sql = "SELECT id, user_id FROM ".TB_PREF."users";
+                                       return combo_input($name, '', $sql, 'id', 'user_id',array('spec_option'=>_("No Users Filter"),'spec_id'=>ALL_NUMERIC));
 
-class ReportParam
-{
-       var $param_name;
-       var $param_type;
+                               case 'ACCOUNTTAGS':
+                               case 'DIMENSIONTAGS':
+                                       if ($type == 'ACCOUNTTAGS')
+                                               $tag_type = TAG_ACCOUNT;
+                                       else
+                                               $tag_type = TAG_DIMENSION;
+                                       return tag_list($name, 5, $tag_type, true, _("No tags"));
 
-       function ReportParam($param_name, $param_type)
-       {
-               $this->param_name = $param_name;
-               $this->param_type = $param_type;
+                       }
+               return '';
        }
 }
 
-function dup_simple_codeandname_list($sql, $name, &$selected_id,
-       $all_option=false, $all_option_name=null, $all_option_numeric=false,
-       $submit_on_change=false)
+class Report
 {
-       if ($submit_on_change == true)
-               $st = "<select name='$name' onchange='this.form.submit();'>";
-       else
-               $st = "<select name='$name'>";
-
-       if ($all_option == true)
+       var $id;
+       var $name;
+       var $ar_params;
+       var $controls;
+       
+       function Report($id, $name, $ar_params = null)
        {
-               if ($all_option_numeric)
-                       $reserved_word = reserved_words::get_all_numeric();
-               else
-                       $reserved_word = reserved_words::get_all();
-
-       if ($reserved_word == $selected_id)
-       {
-            $st .= "<option selected value='" . $reserved_word . "'>" . $all_option_name . "</option>\n";
-       }
-       else
-       {
-            $st .= "<option value='" . $reserved_word . "'>" . $all_option_name . "</option>\n";
-       }
-               if ($selected_id == "")
-               {
-                       $selected_id =  $reserved_word;
-               }
+               $this->id = $id;
+               $this->name = $name;
+               if ($ar_params) $this->set_controls($ar_params);
        }
-
-       $result = db_query($sql);
-
-       while ($row = db_fetch_row($result))
-       {
-               if ($selected_id == $row[0])
-               {
-                       $st .= "<option selected value='" . $row[0] . "'>";
-               }
-               else
-               {
-                       $st .= "<option value='" . $row[0] . "'>";
-               }
-               $st .= $row[1] . "</option>\n";
-
-               if ($selected_id == "")
-               {
-                       $selected_id = $row[0];
-               }
+       
+       function set_controls($ar_params) {
+               $this->controls = $ar_params;
        }
-
-       $st .= "</select>";
-       db_free_result($result);
-
-       return $st;
-}
-
-function dup_simple_name_list($name, $selections)
-{
-       $st = "<select name='$name'>";
-       for ($i = 0; $i < count($selections); $i++)
-               $st .= "<option value='" . $i . "'>" . $selections[$i] . "</option>\n";
-       $st .= "</select>";
-       return $st;
+       
+       function get_controls() {
+               return $this->controls;
+       }
+       
 }
 
 //------------------------------------------------------------------------------------------------
-
-function dup_systypes_list($name, &$selected_id, $all_option=false, $all_option_name=null)
+function gl_systypes_list($name, $value=null, $spec_opt=false)
 {
        global $systypes_array;
+       
+       $types = $systypes_array;
+       
+       foreach(array(ST_LOCTRANSFER, ST_PURCHORDER, ST_SUPPRECEIVE, ST_MANUISSUE,
+                               ST_MANURECEIVE, ST_SALESORDER, ST_SALESQUOTE, ST_DIMENSION) as $type)
+               unset($types[$type]);
 
-       $st = "<select name='$name'>";
-       if ($all_option == true)
-       {
-               $reserved_word = reserved_words::get_all_numeric();
-       if ($reserved_word == $selected_id)
-            $st .= "<option selected value='" . $reserved_word . "'>" . $all_option_name . "</option>\n";
-       else
-            $st .= "<option value='" . $reserved_word . "'>" . $all_option_name . "</option>\n";
-               if ($selected_id == "")
-                       $selected_id = $reserved_word;
-       }
+       return array_selector($name, $value, $types, 
+               array( 
+                       'spec_option'=> $spec_opt,
+                       'spec_id' => ALL_NUMERIC,
+                       'async' => false,
+                       )
+       );
+}
 
-       foreach ($systypes_array as $i => $value)
+function add_custom_reports(&$reports)
+{
+       global $installed_extensions, $path_to_root, $comp_path;
+       
+       // include reports installed inside extension modules
+       if (count($installed_extensions) > 0)
        {
-               if ($i == 16 || $i == 18 || $i == 25 || ($i >= 28 && $i <= 30) || $i >= 40)
-                       continue;
-       $st .= "<option" . ($selected_id == $i ? " selected" : "") . " value='$i'>" . $value['name'] . "</option>\n";
+               $extensions = $installed_extensions;
+               foreach ($extensions as $ext)
+                       if (($ext['active'] && $ext['type'] == 'module')) {
+                               $file = $path_to_root.'/'.$ext['path']."/reporting/reports_custom.php";
+                               if (file_exists($file))
+                                       include_once($file);
+                       }
        }
-    $st .= "</select>";
-       return $st;
+       $file = $comp_path.'/'.user_company()."/reporting/reports_custom.php";
+       if (file_exists($file))
+               include_once($file);
 }
 
-
 ?>
\ No newline at end of file