import torrus 1.0.9
[freeside.git] / torrus / perllib / Torrus / ReportOutput.pm
1 #  Copyright (C) 2005  Stanislav Sinyagin
2 #
3 #  This program is free software; you can redistribute it and/or modify
4 #  it under the terms of the GNU General Public License as published by
5 #  the Free Software Foundation; either version 2 of the License, or
6 #  (at your option) any later version.
7 #
8 #  This program is distributed in the hope that it will be useful,
9 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 #  GNU General Public License for more details.
12 #
13 #  You should have received a copy of the GNU General Public License
14 #  along with this program; if not, write to the Free Software
15 #  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
16
17 # $Id: ReportOutput.pm,v 1.1 2010-12-27 00:03:40 ivan Exp $
18 # Stanislav Sinyagin <ssinyagin@yahoo.com>
19
20 # Package for generating report output to HTML, PDF, whatever
21 # Media-specific classes should inherit from this package
22 # and 
23
24 package Torrus::ReportOutput;
25
26 use strict;
27
28 use Torrus::Log;
29 use Torrus::SQL::Reports;
30 use Torrus::ServiceID;
31
32
33 sub new
34 {
35     my $class = shift;
36     my $options = shift;
37     
38     my $self = {};
39     bless ($self, $class);
40     
41     $self->{'options'} = $options;    
42     defined( $self->{'options'}->{'Tree'} ) or die;
43     
44     my $sqlRep = Torrus::SQL::Reports->new( $options->{'ReportsSqlSubtype'} );
45     if( not defined( $sqlRep ) )
46     {
47         Error('Cannot connect to the database');
48         return undef;
49     }
50     $self->{'backend'} = $sqlRep;
51
52     my $outdir = $Torrus::Global::reportsDir . '/' .
53         $self->{'options'}->{'Tree'};
54     $self->{'outdir'} = $outdir;
55
56     if( not -d $outdir )
57     {
58         if( not mkdir( $outdir ) )
59         {
60             Error('Cannot create directory ' . $outdir . ': ' . $!);
61             return undef;
62         }
63     }
64
65     return $self;    
66 }
67
68 # initialize the subclasses' internals
69 sub init
70 {
71     my $self = shift;
72     
73     return 1;
74 }
75
76
77 sub generate
78 {
79     my $self = shift;
80
81     my $ok = 1;
82     
83     my %monthlyReportNames;
84
85     my $srvIdList;
86     if( not $self->{'options'}->{'All_Service_IDs'} )
87     {
88         my $srvId = new Torrus::ServiceID;
89         $srvIdList = $srvId->getAllForTree( $self->{'options'}->{'Tree'} );
90     }
91     
92     my $allReports = $self->{'backend'}->getAllReports( $srvIdList );
93
94     # frontpage, title, list of years, etc.
95     $self->genIntroduction( $allReports );
96
97     while( my( $year, $yearRef ) = each %{$allReports} )
98     {
99         my $monthlyReportFields = {};
100         my $srvidMonthlyFields = {};
101         
102         while( my( $month, $monthRef ) = each %{$yearRef} )
103         {
104             my $dailyReportFields = {};
105             
106             while( my( $day, $dayRef ) = each %{$monthRef} )
107             {
108                 while( my( $reportName, $fieldsRef ) = each %{$dayRef} )
109                 {
110                     # Check if the report is monthly
111                     if( not defined( $monthlyReportNames{$reportName} ) )
112                     {
113                         my $class =
114                             $Torrus::ReportGenerator::modules{$reportName};
115                         eval( 'require ' . $class );
116                         die( $@ ) if $@;
117
118                         $monthlyReportNames{$reportName} =
119                             $class->isMonthly() ? 1:0;
120                     }
121
122                     # This report is monthly -- do not include it in daily
123                     # list.
124                     if( $monthlyReportNames{$reportName} )
125                     {
126                         $monthlyReportFields->{$month}{$reportName} =
127                             $fieldsRef;
128                         while( my( $serviceid, $fref ) = each %{$fieldsRef} )
129                         {
130                             $srvidMonthlyFields->{$serviceid}{$reportName}->{
131                                 $month} = $fref;
132                         }
133                     }
134                     else
135                     {
136                         $dailyReportFields->{$day} = $dayRef;
137                     }
138                 }
139             }
140
141             $ok = $self->genDailyOutput( $year, $month, $dailyReportFields )?
142                 $ok:0;
143         }
144
145         $ok = $self->genSrvIdOutput( $year, $srvidMonthlyFields ) ? $ok:0;
146         $ok = $self->genMonthlyOutput( $year, $monthlyReportFields ) ? $ok:0;;
147     }
148
149     return $ok;
150 }
151
152
153 # Print the head page and years reference
154 sub genIntroduction
155 {
156     my $self = shift;
157     my $allReports = shift;
158
159     return 1;
160 }
161
162
163 # Print monthly report for a given service ID
164 # The fields argument is a hash of hashes:
165 # serviceid => reportname => month => fieldname => {value, units}
166 sub genSrvIdOutput
167 {
168     my $self = shift;
169     my $year = shift;    
170     my $fields = shift;
171
172     return 1;
173 }
174     
175 # Print daily report
176 # Fields structure:
177 # day => reportname => serviceid => fieldname => {value, units}
178 sub genDailyOutput
179 {
180     my $self = shift;
181     my $year = shift;    
182     my $month = shift;    
183     my $fields = shift;
184
185     return 1;
186 }
187
188 # Print monthly report
189 # fields:
190 # month => reportname => serviceid => fieldname => {value, units}
191 sub genMonthlyOutput
192 {
193     my $self = shift;
194     my $year = shift;    
195     my $fields = shift;
196
197     return 1;
198 }    
199     
200         
201     
202
203 1;
204
205
206 # Local Variables:
207 # mode: perl
208 # indent-tabs-mode: nil
209 # perl-indent-level: 4
210 # End: