1 package FS::Report::Table::Monthly;
4 use vars qw( @ISA $expenses_kludge );
10 @ISA = qw( FS::Report::Table );
16 FS::Report::Table::Monthly - Tables of report data, indexed monthly
20 use FS::Report::Table::Monthly;
22 my $report = new FS::Report::Table::Monthly (
23 'items' => [ 'invoiced', 'netsales', 'credits', 'receipts', ],
30 'params' => [ [ 'paramsfor', 'item_one' ], [ 'item', 'two' ] ], # ...
31 'remove_empty' => 1, #collapse empty rows, default 0
32 'item_labels' => [ ], #useful with remove_empty
35 my $data = $report->data;
43 Returns a hashref of data (!! describe)
53 my $smonth = $self->{'start_month'};
54 my $syear = $self->{'start_year'};
55 my $emonth = $self->{'end_month'};
56 my $eyear = $self->{'end_year'};
57 my $agentnum = $self->{'agentnum'};
61 while ( $syear < $eyear || ( $syear == $eyear && $smonth < $emonth+1 ) ) {
63 push @{$data{label}}, "$smonth/$syear";
65 my $speriod = timelocal(0,0,0,1,$smonth-1,$syear);
66 push @{$data{speriod}}, $speriod;
67 if ( ++$smonth == 13 ) { $syear++; $smonth=1; }
68 my $eperiod = timelocal(0,0,0,1,$smonth-1,$syear);
69 push @{$data{eperiod}}, $eperiod;
73 foreach my $item ( @{$self->{'items'}} ) {
74 my @param = $self->{'params'} ? @{ $self->{'params'}[$col] }: ();
75 my $value = $self->$item($speriod, $eperiod, $agentnum, @param);
76 #push @{$data{$item}}, $value;
77 push @{$data{data}->[$col++]}, $value;
82 #these need to get generalized, sheesh
83 $data{'items'} = $self->{'items'};
84 $data{'item_labels'} = $self->{'item_labels'} || $self->{'items'};
85 $data{'colors'} = $self->{'colors'};
86 $data{'links'} = $self->{'links'} || [];
91 if ( $self->{'remove_empty'} ) {
93 #warn "removing empty rows\n";
96 #these need to get generalized, sheesh
102 foreach my $item ( @{$self->{'items'}} ) {
104 if ( grep { $_ != 0 } @{$data{'data'}->[$col]} ) {
105 push @newitems, $data{'items'}->[$col];
106 push @newlabels, $data{'item_labels'}->[$col];
107 push @newdata, $data{'data'}->[$col];
108 push @newcolors, $data{'colors'}->[$col];
109 push @newlinks, $data{'links'}->[$col];
115 $data{'items'} = \@newitems;
116 $data{'item_labels'} = \@newlabels;
117 $data{'data'} = \@newdata;
118 $data{'colors'} = \@newcolors;
119 $data{'links'} = \@newlinks;
124 #warn Dumper(\%data);
130 sub invoiced { #invoiced
131 my( $self, $speriod, $eperiod, $agentnum ) = @_;
136 LEFT JOIN cust_main USING ( custnum )
137 WHERE ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum)
142 sub netsales { #net sales
143 my( $self, $speriod, $eperiod, $agentnum ) = @_;
145 my $credited = $self->scalar_sql("
146 SELECT SUM(cust_credit_bill.amount)
147 FROM cust_credit_bill
148 LEFT JOIN cust_bill USING ( invnum )
149 LEFT JOIN cust_main USING ( custnum )
150 WHERE ". $self->in_time_period_and_agent( $speriod,
157 #horrible local kludge
158 my $expenses = !$expenses_kludge ? 0 : $self->scalar_sql("
159 SELECT SUM(cust_bill_pkg.setup)
161 LEFT JOIN cust_bill USING ( invnum )
162 LEFT JOIN cust_main USING ( custnum )
163 LEFT JOIN cust_pkg USING ( pkgnum )
164 LEFT JOIN part_pkg USING ( pkgpart )
165 WHERE ". $self->in_time_period_and_agent( $speriod,
170 AND LOWER(part_pkg.pkg) LIKE 'expense _%'
173 $self->invoiced($speriod,$eperiod,$agentnum) - $credited - $expenses;
178 sub receipts { #cashflow
179 my( $self, $speriod, $eperiod, $agentnum ) = @_;
181 my $refunded = $self->scalar_sql("
184 LEFT JOIN cust_main USING ( custnum )
185 WHERE ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum)
188 #horrible local kludge that doesn't even really work right
189 my $expenses = !$expenses_kludge ? 0 : $self->scalar_sql("
190 SELECT SUM(cust_bill_pay.amount)
192 LEFT JOIN cust_bill USING ( invnum )
193 LEFT JOIN cust_main USING ( custnum )
194 WHERE ". $self->in_time_period_and_agent( $speriod,
197 'cust_bill_pay._date'
199 AND 0 < ( SELECT COUNT(*) from cust_bill_pkg, cust_pkg, part_pkg
200 WHERE cust_bill.invnum = cust_bill_pkg.invnum
201 AND cust_pkg.pkgnum = cust_bill_pkg.pkgnum
202 AND cust_pkg.pkgpart = part_pkg.pkgpart
203 AND LOWER(part_pkg.pkg) LIKE 'expense _%'
206 # my $expenses_sql2 = "SELECT SUM(cust_bill_pay.amount) FROM cust_bill_pay, cust_bill_pkg, cust_bill, cust_pkg, part_pkg WHERE cust_bill_pay.invnum = cust_bill.invnum AND cust_bill.invnum = cust_bill_pkg.invnum AND cust_bill_pay._date >= $speriod AND cust_bill_pay._date < $eperiod AND cust_pkg.pkgnum = cust_bill_pkg.pkgnum AND cust_pkg.pkgpart = part_pkg.pkgpart AND LOWER(part_pkg.pkg) LIKE 'expense _%'";
208 $self->payments($speriod, $eperiod, $agentnum) - $refunded - $expenses;
212 my( $self, $speriod, $eperiod, $agentnum ) = @_;
216 LEFT JOIN cust_main USING ( custnum )
217 WHERE ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum)
222 my( $self, $speriod, $eperiod, $agentnum ) = @_;
226 LEFT JOIN cust_main USING ( custnum )
227 WHERE ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum)
231 #these should be auto-generated or $AUTOLOADed or something
233 my( $self, $speriod, $eperiod, $agentnum ) = @_;
234 $speriod = $self->_subtract_11mo($speriod);
235 $self->invoiced($speriod, $eperiod, $agentnum);
239 my( $self, $speriod, $eperiod, $agentnum ) = @_;
240 $speriod = $self->_subtract_11mo($speriod);
241 $self->netsales($speriod, $eperiod, $agentnum);
245 my( $self, $speriod, $eperiod, $agentnum ) = @_;
246 $speriod = $self->_subtract_11mo($speriod);
247 $self->receipts($speriod, $eperiod, $agentnum);
251 my( $self, $speriod, $eperiod, $agentnum ) = @_;
252 $speriod = $self->_subtract_11mo($speriod);
253 $self->payments($speriod, $eperiod, $agentnum);
257 my( $self, $speriod, $eperiod, $agentnum ) = @_;
258 $speriod = $self->_subtract_11mo($speriod);
259 $self->credits($speriod, $eperiod, $agentnum);
262 #not being too bad with the false laziness
263 use Time::Local qw(timelocal);
265 my($self, $time) = @_;
266 my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($time) )[0,1,2,3,4,5];
268 if ( $mon < 0 ) { $mon+=12; $year--; }
269 timelocal($sec,$min,$hour,$mday,$mon,$year);
273 my( $self, $speriod, $eperiod, $agentnum, %opt ) = @_;
276 if ( $opt{'classnum'} =~ /^(\d+)$/ ) {
278 $where = "classnum IS NULL";
280 $where = "classnum = $1";
284 $agentnum ||= $opt{'agentnum'};
287 SELECT SUM(cust_bill_pkg.setup + cust_bill_pkg.recur)
289 LEFT JOIN cust_bill USING ( invnum )
290 LEFT JOIN cust_main USING ( custnum )
291 LEFT JOIN cust_pkg USING ( pkgnum )
292 LEFT JOIN part_pkg USING ( pkgpart )
295 AND ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum)
300 sub setup_pkg { shift->pkg_field( @_, 'setup' ); }
301 sub susp_pkg { shift->pkg_field( @_, 'susp' ); }
302 sub cancel_pkg { shift->pkg_field( @_, 'cancel'); }
305 my( $self, $speriod, $eperiod, $agentnum, $field ) = @_;
307 SELECT COUNT(*) FROM cust_pkg
308 LEFT JOIN cust_main USING ( custnum )
309 WHERE ". $self->in_time_period_and_agent( $speriod,
318 #this is going to be harder..
320 # my( $self, $speriod, $eperiod, $agentnum ) = @_;
321 # $self->scalar_sql("
322 # SELECT COUNT(*) FROM h_cust_pkg
327 sub in_time_period_and_agent {
328 my( $self, $speriod, $eperiod, $agentnum ) = splice(@_, 0, 4);
329 my $col = @_ ? shift() : '_date';
331 my $sql = "$col >= $speriod AND $col < $eperiod";
334 $sql .= " AND agentnum = $agentnum"
337 #agent virtualization
338 $sql .= ' AND '. $FS::CurrentUser::CurrentUser->agentnums_sql;
344 my( $self, $sql ) = ( shift, shift );
345 my $sth = dbh->prepare($sql) or die dbh->errstr;
347 or die "Unexpected error executing statement $sql: ". $sth->errstr;
348 $sth->fetchrow_arrayref->[0] || 0;