1 package FS::Report::Table::Monthly;
7 use Time::Local qw( timelocal );
9 @ISA = qw( FS::Report::Table );
13 FS::Report::Table::Monthly - Tables of report data, indexed monthly
17 use FS::Report::Table::Monthly;
19 my $report = new FS::Report::Table::Monthly (
20 'items' => [ 'invoiced', 'netsales', 'credits', 'receipts', ],
28 'params' => [ [ 'paramsfor', 'item_one' ], [ 'item', 'two' ] ], # ...
29 'remove_empty' => 1, #collapse empty rows, default 0
30 'item_labels' => [ ], #useful with remove_empty
33 my $data = $report->data;
41 Returns a hashref of data (!! describe)
46 local $FS::UID::AutoCommit = 0;
49 my $smonth = $self->{'start_month'};
50 my $syear = $self->{'start_year'};
51 my $emonth = $self->{'end_month'};
52 my $eyear = $self->{'end_year'};
53 # whether to extrapolate into the future
54 my $projecting = $self->{'projection'};
57 if ( $eyear < $syear or
58 ($eyear == $syear and $emonth < $smonth) ) {
59 return { error => 'Start month must be before end month' };
62 my $agentnum = $self->{'agentnum'};
63 my $refnum = $self->{'refnum'};
67 $self->init_projection;
69 my $thismonth = $smonth;
70 my $thisyear = $syear;
71 while ( $thisyear < $eyear ||
72 ( $thisyear == $eyear and $thismonth <= $emonth )
74 my $speriod = timelocal(0,0,0,1,$thismonth-1,$thisyear);
76 if ( $thismonth == 13 ) { $thisyear++; $thismonth = 1; }
77 my $eperiod = timelocal(0,0,0,1,$thismonth-1,$thisyear);
79 $self->extend_projection($speriod, $eperiod);
85 my $max_year = $eyear;
86 my $max_month = $emonth;
88 while ( $syear < $max_year
89 || ( $syear == $max_year && $smonth < $max_month+1 ) ) {
91 if ( $self->{'doublemonths'} ) {
92 my($firstLabel,$secondLabel) = @{$self->{'doublemonths'}};
93 push @{$data{label}}, "$smonth/$syear $firstLabel";
94 push @{$data{label}}, "$smonth/$syear $secondLabel";
97 push @{$data{label}}, "$smonth/$syear";
100 my $speriod = timelocal(0,0,0,1,$smonth-1,$syear);
101 push @{$data{speriod}}, $speriod;
102 if ( ++$smonth == 13 ) { $syear++; $smonth=1; }
103 my $eperiod = timelocal(0,0,0,1,$smonth-1,$syear);
104 push @{$data{eperiod}}, $eperiod;
107 my @items = @{$self->{'items'}};
110 for ( $i = 0; $i < scalar(@items); $i++ ) {
111 if ( $self->{'doublemonths'} ) {
112 my $item = $items[$i];
113 my @param = $self->{'params'} ? @{ $self->{'params'}[$i] }: ();
114 push @param, 'project', $projecting;
115 push @param, 'refnum' => $refnum if $refnum;
116 my $value = $self->$item($speriod, $eperiod, $agentnum, @param);
117 push @{$data{data}->[$col]}, $value;
118 $item = $items[$i+1];
119 @param = $self->{'params'} ? @{ $self->{'params'}[++$i] }: ();
120 push @param, 'project', $projecting;
121 push @param, 'refnum' => $refnum if $refnum;
122 $value = $self->$item($speriod, $eperiod, $agentnum, @param);
123 push @{$data{data}->[$col++]}, $value;
126 my $item = $items[$i];
127 my @param = $self->{'params'} ? @{ $self->{'params'}[$col] }: ();
128 push @param, 'project', $projecting;
129 push @param, 'refnum' => $refnum if $refnum;
130 my $value = $self->$item($speriod, $eperiod, $agentnum, @param);
131 push @{$data{data}->[$col++]}, $value;
137 #these need to get generalized, sheesh
138 $data{'items'} = $self->{'items'};
139 $data{'item_labels'} = $self->{'item_labels'} || $self->{'items'};
140 $data{'colors'} = $self->{'colors'};
141 $data{'links'} = $self->{'links'} || [];
143 if ( $self->{'remove_empty'} ) {
146 #these need to get generalized, sheesh
147 #(though we now return a list of item indices that are present in the
148 #output, so the front-end code could do this)
155 foreach my $item ( @{$self->{'items'}} ) {
157 if ( grep { $_ != 0 } @{$data{'data'}->[$col]} ) {
158 push @newitems, $data{'items'}->[$col];
159 push @newlabels, $data{'item_labels'}->[$col];
160 push @newdata, $data{'data'}->[$col];
161 push @newcolors, $data{'colors'}->[$col];
162 push @newlinks, $data{'links'}->[$col];
169 $data{'items'} = \@newitems;
170 $data{'item_labels'} = \@newlabels;
171 $data{'data'} = \@newdata;
172 $data{'colors'} = \@newcolors;
173 $data{'links'} = \@newlinks;
174 $data{'indices'} = \@indices;
177 # clean up after ourselves
179 # leave in until development is finished, for diagnostics