array('Hong Kong', 'HKD'), 'sg'=> array('Singapore', 'SGD'), 'my'=> array('Malaysia', 'MYR'), 'au'=> array('Australia', 'AUD'), 'cn'=> array('China', 'RMB'), 'zh' => array('China (Trade)', 'RMB') // ); var $intervalnames = array( 'Y' => 'Since Start of Financial Year to end of ', 'M' => 'Single Month' ); var $groups = array(); var $fullpath = array(); function get() { if (!empty($_REQUEST['_debug'])) { DB_DAtaObject::DebugLevel(1); } //$_REQUEST['flhead_name'] = 'Basic Balance Sheet'; //$_REQUEST['period'] = '2012-08-01'; $interval = empty($_REQUEST['interval']) ? 'Y' : $_REQUEST['interval']; $this->withinterval = ''; if (strlen($interval) == 2) { $this->withinterval = $interval[1]; $interval = $interval[0]; } $nperiod = array(); $period = empty($_REQUEST['period']) ? date('Y-m-01') : date('Y-m-01', strtotime($_REQUEST['period'])); $operiod = $period; if (!empty($this->withinterval )) { if($this->withinterval != 'F' && $this->withinterval != 'L'){ $add = $this->withinterval == 'M' ? ' 1 MONTH' : '1 YEAR'; $nperiod[] = date('Y-m-01', strtotime($period . ' - ' . $add)); $nperiod[] = $period; }else{ $pp = explode('-', $period); $year = $pp[0]; $month = $pp[1]; switch (true){ // case ($this->withinterval == 'L') : $from = date('Y-m-01', strtotime($period . " - 12 MONTH + 1 MONTH")); break; case ($month < 5 ) : $from = ($year -1) . '-05-01'; break; default : $from = $year . '-05-01'; break; } for($m = $from; strtotime($m) <= strtotime($period); $m = date('Y-m-01', strtotime("$m + 1 MONTH"))){ $nperiod[] = $m; } } } if(!count($nperiod)){ $nperiod[] = $period; } $period = implode(',', $nperiod); $x = DB_DataObject::Factory('flhead'); $ourdb = substr($x->database(), -2); //$cbd = substr($x->database(), -2); $cbd = empty($_REQUEST['company']) ? $ourdb : $_REQUEST['company']; $outlets = array(); $outlets[$cbd ] = $this->outlets[$cbd ]; if ($this->hasPerm('Xtuple.AccountsCombined', 'S') && $cbd == 'hk' && empty($_REQUEST['singleCountry'] )) { $outlets = $this->outlets ; } $this->outlets= $outlets; foreach($this->outlets as $o=>$name) { $args = array( 'flhead_name' => $_REQUEST['flhead_name'], 'interval' => $interval, 'period' => $period, // multiple periods...? ); // only show timewarps in hk.. if ( $ourdb != 'hk') { $args['no_timewarp'] =1; } $qq = array(); foreach($args as $k=>$v) { $qq[] = "{$k}=". urlencode($v); } // echo "http://localhost{$this->rootURL}/{$o}.php/Roo/flhead?". implode('&', $q); // continue; //echo "
\nBERFORE:" . date('H:i:s') ;
           
            if ($o == $ourdb) {
                $fl = DB_DataObjecT::factory('flhead');
                $args['_return_result'] = 1;
                $data = $fl->applyFilters($args, $this->authUser, $this);
               
                $this->parseData($data, $o);
            
            } else {
                $url = "http://localhost{$this->rootURL}/{$o}.php/Roo/flhead?". implode('&', $qq);
               //s var_dump($url);
                $xtd = json_decode(file_get_contents($url));
                if (empty($xtd->data)) {
                    continue;
                }
                $this->parseData($xtd->data, $o);
           }
            // echo "
\nAFTER :" . date('H:i:s') ;
            
            // echo '
';print_r($this->groups);exit;
           
            
             //echo '
';print_r($this->groups);exit;
        }
        //exit;
        if (!empty($_REQUEST['_xls'])) {
            $this->toExcel($operiod, $nperiod, $interval);
        }
        $this->toHTMLBody($operiod, $nperiod, $interval);
         
         
       
    }
    function parseData($data, $out)
    {
        //echo '
';print_r($data);exit;
        
        if ($out == 'au') {
           // echo '
';print_r($data);exit;
        }
        $groups = array(); // id to group.
        foreach($data as $g)
        {
            
            if (is_array($g)) {
                $g = (object) $g; // returned directly. - no json..
            }
            switch($g->fltrenditem_type) {
                case 'G': //groups
                    $g->items = array();
                    $g->groups = array();
                    $grp = $g->fltrenditem_name;
                    $g->fullpath = $grp;
                    
                     
                    if ($g->fltrenditem_parent_id > 0) {
                        
                        // not a top levell see if current group has a child.
                        
                        
                        $parname = $groups[$g->fltrenditem_parent_id];
                        
                        $g->fullpath = $parname .'|'.$grp;
                        $par= $this->fullpath[$parname];
                        
                        if (!isset($par->groups[$grp] )) {
                            $par->groups[$grp] = $g;
                        }
                        
                    } else {
                        $g->fullpath = $grp;
                        if (!isset($this->groups[$grp])) {
                            $this->groups[$grp] = $g;
                        }
                    }
                    
                    if (!isset($this->fullpath[$g->fullpath])) {
                        $this->fullpath[$g->fullpath] = $g;
                    }
                    
                    $this->fullpath[$g->fullpath]->{$out} = $g;
                    
                    
                    
                    $groups[$g->fltrenditem_type_id] = $g->fullpath;
                    
                    continue;
                
                
                case 'T': // totals.
                    if (!isset($g->fltrenditem_parent_id)) {
                        continue;
                    }
                    
                    $parname = $groups[$g->fltrenditem_parent_id];
                    $par= $this->fullpath[$parname];
                    $par->total = isset($par->total) ? $par->total : $g;
                    $par->total->{$out} = $g;
                    
                    continue;
                
                
                case 'I': // account.
                    
                    $rowtotal = 0;
                    for ($i =1 ; $i < 13; $i++) {
                        $rowtotal += isset($g->{'fltrenditem_fld'.$i}) ? $g->{'fltrenditem_fld'.$i} : 0;
                    }
                    
                    if ($rowtotal  == 0.0) {
                        continue;
                    }
                    
                    $parname = $groups[$g->fltrenditem_parent_id];
                    $par = $this->fullpath[$parname];
                    if (!isset($par->items[$g->fltrenditem_name])) {
                        $par->items[$g->fltrenditem_name] = clone($g)  ;
                    }
            
                    $par->items[$g->fltrenditem_name]->{$out} = $g;
                    continue;
                    
                    
            }
            
            
        }
        
        
    }
    
    function toHTMLBody($operiod, $nperiod, $interval)
    {
        
        echo '';
        echo '
'; $flhead = DB_DataObject::factory('flhead'); if (!$flhead->get('flhead_name', $_REQUEST['flhead_name'])) { $this->jerr("invalid report"); } echo '

' . htmlspecialchars($flhead->flhead_name) . ' in ' . DB_DataObject::factory('curr_symbol')->base()->curr_name . '

'; echo "For {$this->intervalnames[$interval]} " . date(' F Y', strtotime($operiod)). ''; // echo '
';print_r($this->groups);echo '
'; echo ''; $ret = ''; if(($this->withinterval != 'F' && $this->withinterval != 'L') || count(array_keys($this->outlets)) == 1){ foreach($this->outlets as $k=>$name) { foreach ($nperiod as $n => $p){ if($n == 0){ $ret.=''; continue; } $ret.=''; } } if (count(array_keys($this->outlets)) > 1) { foreach ($nperiod as $n => $p){ if($n == 0){ $ret.= ($this->withinterval) ? '' : ''; continue; } $ret.=''; } } }else{ $ret.=''; } $ret .= "\n"; echo $ret; foreach($this->groups as $kk => $vv ) { echo $this->toHTML($kk, $vv, $nperiod); } echo '
Account' . $name[0] . '
' . date(' F Y', strtotime($p)) .'

' .date(' F Y', strtotime($p)) .'
Combined
' .date(' F Y', strtotime($p)) .'
Combined
' . date(' F Y', strtotime($p)) .'
Consolidated
'; echo '
'; exit; } function toHTML($key , $obj, $nperiod) { // assume head row has been sent. $ret = ''. htmlspecialchars($key) .''; $tmp = ''; foreach($this->outlets as $k=>$name) { foreach ($nperiod as $n => $p){ $str = 'fltrenditem_fld' . ($n+1); $val = !empty($obj->$k->$str) ? $obj->$k->$str : 0; $val = $val ? $this->currtobase($name[1], 'HKD', $val, date('Y-m-d')) : 0; if (($this->withinterval == 'F' || $this->withinterval == 'L') && count(array_keys($this->outlets)) > 1) { if(!isset($consolidated)){ $consolidated = 0; } $consolidated += (isset($obj->$k)) ? $val : 0; continue; } $total = 'total' . ($n+1); if(!isset($$total)){ $$total = 0; } $$total += isset($obj->$k) ? $val : 0; $tmp.='' . (!empty($val) ? number_format($val, 2) : '') . ''; } } if (count(array_keys($this->outlets)) > 1) { foreach ($nperiod as $n => $p){ $total = 'total' . ($n+1); $tmp.= '' . (empty($$total) ? '' : number_format($$total,2)) . ''; } } if(($this->withinterval != 'F' && $this->withinterval != 'L') || count(array_keys($this->outlets)) == 1){ $ret .= $tmp; }else{ $ret.= '' . (empty($consolidated) ? '' : number_format($consolidated,2)) . ''; } $ret .= "\n"; foreach(isset($obj->groups) ? $obj->groups : array() as $kk => $vv ) { $ret .= $this->toHTML($kk, $vv,$nperiod); } $items = isset($obj->items) ? $obj->items: array(); ksort($items); foreach($items as $kk => $vv ) { $ret .= $this->toHTML($kk, $vv, $nperiod); } if (isset($obj->total) ) { $ret.= $this->toHTML($obj->total->fltrenditem_name, $obj->total,$nperiod); } return $ret; } function currtobase($from , $to, $amt , $date) { static $base = false; if (!$base) { $base = DB_DataObject::factory('curr_symbol')->base()->curr_name; } // var_dump(array($from,$base)); if (($from == $base) || ($amt == 0.0)) { return $amt; } $do = DB_DataObject::factory('curr_symbol'); $do->query("select currtobase(getcurrid('{$from}'), {$amt}, '{$date}') as result"); $do->fetch(); return $do->result; } function toExcel($operiod, $nperiod, $interval) { // assume head row has been sent. $curr = DB_DataObject::factory('curr_symbol')->base(); $cfg = array( 'formats' => array( 'alignright' => array( 'Align' => 'right', 'NumFormat' => '"'.$curr->curr_symbol. '"#,##0.00_);[Red]("'.$curr->curr_symbol. '"#,##0.00)' ), 'alignleft' => array( 'Align' => 'left'), ), 'workbook' => 'Consolidated Report', 'head' => array(), 'cols' => array( ) ); $flhead = DB_DataObject::factory('flhead'); if (!$flhead->get('flhead_name', $_REQUEST['flhead_name'])) { $this->jerr("invalid report"); } $cfg['head'][] = array($flhead->flhead_name); $cfg['head'][] = array("For {$this->intervalnames[$interval]} on " . date('d F Y', strtotime($operiod))); $cfg['cols'][] = array( 'header'=> "Account", 'dataIndex'=> 'name', 'width'=> 200, 'format' => 'alignleft' ); if(($this->withinterval != 'F' && $this->withinterval != 'L') || count(array_keys($this->outlets)) == 1){ foreach($this->outlets as $k=>$name) { foreach ($nperiod as $n => $p){ if($n == 0){ $cfg['cols'][] = array( 'header'=> ($this->withinterval) ? $name[0] . "\n" . $p : $name[0] , 'dataIndex'=> 'data_' . $k . ($n+1), 'width'=> 100, 'format' => 'alignright' ); continue; } $cfg['cols'][] = array( 'header'=> "\n" . $p , 'dataIndex'=> 'data_' . $k . ($n+1), 'width'=> 100, 'format' => 'alignright' ); } } }else{ $cfg['cols'][] = array( 'header'=> 'Consolidated', 'dataIndex'=> 'consolidated', 'width'=> 100, 'format' => 'alignright' ); } if ($this->withinterval != 'F' && $this->withinterval != 'L' && count(array_keys($this->outlets)) > 1) { foreach ($nperiod as $n => $p){ if($n == 0){ $cfg['cols'][] = array( 'header'=> ($this->withinterval) ? "Combinded\n" . $p : "Combinded", 'dataIndex'=> 'total' . ($n+1), 'width'=> 100, 'format' => 'alignright' ); continue; } $cfg['cols'][] = array( 'header'=> "\n". $p , 'dataIndex'=> 'total' . ($n+1), 'width'=> 100, 'format' => 'alignright' ); } } $this->edata = array(); foreach($this->groups as $kk => $vv ) { $this->toExcelRow($kk, $vv, $nperiod); } $fn = $flhead->flhead_name.'-'.$this->intervalnames[$interval].'-'.date('d-F-Y', strtotime($operiod)) .'.xls'; require_once 'Pman/Core/SimpleExcel.php'; //echo '
';        print_R($this->edata);        print_R($cfg);        exit;
        $x = new Pman_Core_SimpleExcel($this->edata, $cfg);
        $x->send(  'combined-report-' . $fn );
        exit;
    }
    
    function  toExcelRow($key, $obj, $nperiod)
    { 
        $out = array(
            'name' => $key
        );
        
        foreach($this->outlets as $k=>$name) {
            foreach ((array)$nperiod as $n => $p){
                $str = 'fltrenditem_fld' . ($n+1);
                $val = isset($obj->$k->$str) ? $obj->$k->$str : 0;
                $val = $this->currtobase($name[1], 'HKD', $val, date('Y-m-d'));
                if (($this->withinterval == 'F' || $this->withinterval == 'L') && count(array_keys($this->outlets)) > 1) {
                    if(!isset($out['consolidated'])){
                        $out['consolidated'] = 0;
                    }
                    $out['consolidated'] +=  (isset($obj->$k)) ? $val : 0;
                    continue;
                }
                $out['data_'.$k . ($n+1)] =  (isset($obj->$k) ? $val : '');
                if(!isset($out['total' . ($n+1)])){
                    $out['total' . ($n+1)] = 0;
                }
                $out['total' . ($n+1)]  +=  (isset($obj->$k)) ? $val : 0;
            }
            
        }
        
        $this->edata[] = $out;
        
        foreach(isset($obj->groups) ? $obj->groups : array() as  $kk => $vv ) {
             $this->toExcelRow($kk, $vv, $nperiod);
        }
        $items = isset($obj->items) ? $obj->items: array();
        ksort($items);
        foreach($items as  $kk => $vv ) {
            $this->toExcelRow($kk, $vv, $nperiod);
        }
        if (isset($obj->total) ) {
            
             $this->toExcelRow($obj->total->fltrenditem_name, $obj->total, $nperiod);
        }
        
        
    }
    /*
    function findBeforeMonth($n, $dt)
    {
        //var_dump($dt);
        list($y,$m, $d) = explode('-', $dt);
        
        if ($m > $n) {
            return sprintf('%04d-%02d-%02d', $y,$n,1 );
        }
        
        return sprintf('%04d-%02d-%02d', $y -1,$n, 1);
        
    }
    
    function merge($old, $new, $factor)
    {
        // merge the accounts...
        foreach($old as $o) {
            if (empty($o->fltrenditem_type) || $o->fltrenditem_type !='I') {
                continue;
            }
            $accnt[$o->fltrenditem_accnt_id] = $o;
        }
        foreach($new as $o) {
            if (empty($o->fltrenditem_type)  || $o->fltrenditem_type !='I') {
                continue;
            }
            $accnt[$o->fltrenditem_accnt_id]->fltrenditem_fld1 += $factor * $o->fltrenditem_fld1;
        }
        return $old;
         
    }
    */
    
}