1 package FS::Report::Table::Monthly;
4 use vars qw( @ISA $DEBUG );
10 @ISA = qw( FS::Report::Table );
11 $DEBUG = 0; # turning this on will trace all SQL statements, VERY noisy
15 FS::Report::Table::Monthly - Tables of report data, indexed monthly
19 use FS::Report::Table::Monthly;
21 my $report = new FS::Report::Table::Monthly (
22 'items' => [ 'invoiced', 'netsales', 'credits', 'receipts', ],
29 'params' => [ [ 'paramsfor', 'item_one' ], [ 'item', 'two' ] ], # ...
30 'remove_empty' => 1, #collapse empty rows, default 0
31 'item_labels' => [ ], #useful with remove_empty
34 my $data = $report->data;
42 Returns a hashref of data (!! describe)
52 my $smonth = $self->{'start_month'};
53 my $syear = $self->{'start_year'};
54 my $emonth = $self->{'end_month'};
55 my $eyear = $self->{'end_year'};
56 my $agentnum = $self->{'agentnum'};
60 while ( $syear < $eyear || ( $syear == $eyear && $smonth < $emonth+1 ) ) {
62 push @{$data{label}}, "$smonth/$syear";
64 my $speriod = timelocal(0,0,0,1,$smonth-1,$syear);
65 push @{$data{speriod}}, $speriod;
66 if ( ++$smonth == 13 ) { $syear++; $smonth=1; }
67 my $eperiod = timelocal(0,0,0,1,$smonth-1,$syear);
68 push @{$data{eperiod}}, $eperiod;
72 foreach my $item ( @{$self->{'items'}} ) {
73 my @param = $self->{'params'} ? @{ $self->{'params'}[$col] }: ();
74 my $value = $self->$item($speriod, $eperiod, $agentnum, @param);
75 #push @{$data{$item}}, $value;
76 push @{$data{data}->[$col++]}, $value;
81 #these need to get generalized, sheesh
82 $data{'items'} = $self->{'items'};
83 $data{'item_labels'} = $self->{'item_labels'} || $self->{'items'};
84 $data{'colors'} = $self->{'colors'};
85 $data{'links'} = $self->{'links'} || [];
90 if ( $self->{'remove_empty'} ) {
92 #warn "removing empty rows\n";
95 #these need to get generalized, sheesh
101 foreach my $item ( @{$self->{'items'}} ) {
103 if ( grep { $_ != 0 } @{$data{'data'}->[$col]} ) {
104 push @newitems, $data{'items'}->[$col];
105 push @newlabels, $data{'item_labels'}->[$col];
106 push @newdata, $data{'data'}->[$col];
107 push @newcolors, $data{'colors'}->[$col];
108 push @newlinks, $data{'links'}->[$col];
114 $data{'items'} = \@newitems;
115 $data{'item_labels'} = \@newlabels;
116 $data{'data'} = \@newdata;
117 $data{'colors'} = \@newcolors;
118 $data{'links'} = \@newlinks;
123 #warn Dumper(\%data);
129 sub invoiced { #invoiced
130 my( $self, $speriod, $eperiod, $agentnum ) = @_;
135 LEFT JOIN cust_main USING ( custnum )
136 WHERE ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum)
141 sub netsales { #net sales
142 my( $self, $speriod, $eperiod, $agentnum ) = @_;
144 $self->invoiced($speriod,$eperiod,$agentnum)
145 - $self->credits( $speriod,$eperiod,$agentnum);
151 my( $self, $speriod, $eperiod, $agentnum ) = @_;
153 $self->payments($speriod, $eperiod, $agentnum)
154 - $self->refunds( $speriod, $eperiod, $agentnum);
158 my( $self, $speriod, $eperiod, $agentnum ) = @_;
160 $self->receipts($speriod, $eperiod, $agentnum)
161 - $self->netrefunds( $speriod, $eperiod, $agentnum);
165 my( $self, $speriod, $eperiod, $agentnum ) = @_;
169 LEFT JOIN cust_main USING ( custnum )
170 WHERE ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum)
175 my( $self, $speriod, $eperiod, $agentnum ) = @_;
179 LEFT JOIN cust_main USING ( custnum )
180 WHERE ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum)
185 my( $self, $speriod, $eperiod, $agentnum ) = @_;
189 LEFT JOIN cust_main USING ( custnum )
190 WHERE ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum)
195 my( $self, $speriod, $eperiod, $agentnum ) = @_;
197 SELECT SUM(cust_credit_bill.amount)
198 FROM cust_credit_bill
199 LEFT JOIN cust_bill USING ( invnum )
200 LEFT JOIN cust_main USING ( custnum )
201 WHERE ". $self->in_time_period_and_agent( $speriod,
209 sub receipts { #net payments
210 my( $self, $speriod, $eperiod, $agentnum ) = @_;
212 SELECT SUM(cust_bill_pay.amount)
214 LEFT JOIN cust_bill USING ( invnum )
215 LEFT JOIN cust_main USING ( custnum )
216 WHERE ". $self->in_time_period_and_agent( $speriod,
225 my( $self, $speriod, $eperiod, $agentnum ) = @_;
227 SELECT SUM(cust_credit_refund.amount)
228 FROM cust_credit_refund
229 LEFT JOIN cust_credit USING ( crednum )
230 LEFT JOIN cust_main USING ( custnum )
231 WHERE ". $self->in_time_period_and_agent( $speriod,
239 #these should be auto-generated or $AUTOLOADed or something
241 my( $self, $speriod, $eperiod, $agentnum ) = @_;
242 $speriod = $self->_subtract_11mo($speriod);
243 $self->invoiced($speriod, $eperiod, $agentnum);
247 my( $self, $speriod, $eperiod, $agentnum ) = @_;
248 $speriod = $self->_subtract_11mo($speriod);
249 $self->netsales($speriod, $eperiod, $agentnum);
253 my( $self, $speriod, $eperiod, $agentnum ) = @_;
254 $speriod = $self->_subtract_11mo($speriod);
255 $self->receipts($speriod, $eperiod, $agentnum);
259 my( $self, $speriod, $eperiod, $agentnum ) = @_;
260 $speriod = $self->_subtract_11mo($speriod);
261 $self->payments($speriod, $eperiod, $agentnum);
265 my( $self, $speriod, $eperiod, $agentnum ) = @_;
266 $speriod = $self->_subtract_11mo($speriod);
267 $self->credits($speriod, $eperiod, $agentnum);
270 sub netcredits_12mo {
271 my( $self, $speriod, $eperiod, $agentnum ) = @_;
272 $speriod = $self->_subtract_11mo($speriod);
273 $self->netcredits($speriod, $eperiod, $agentnum);
277 my( $self, $speriod, $eperiod, $agentnum ) = @_;
278 $speriod = $self->_subtract_11mo($speriod);
279 $self->cashflow($speriod, $eperiod, $agentnum);
282 sub netcashflow_12mo {
283 my( $self, $speriod, $eperiod, $agentnum ) = @_;
284 $speriod = $self->_subtract_11mo($speriod);
285 $self->cashflow($speriod, $eperiod, $agentnum);
289 my( $self, $speriod, $eperiod, $agentnum ) = @_;
290 $speriod = $self->_subtract_11mo($speriod);
291 $self->refunds($speriod, $eperiod, $agentnum);
294 sub netrefunds_12mo {
295 my( $self, $speriod, $eperiod, $agentnum ) = @_;
296 $speriod = $self->_subtract_11mo($speriod);
297 $self->netrefunds($speriod, $eperiod, $agentnum);
301 #not being too bad with the false laziness
302 use Time::Local qw(timelocal);
304 my($self, $time) = @_;
305 my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($time) )[0,1,2,3,4,5];
307 if ( $mon < 0 ) { $mon+=12; $year--; }
308 timelocal($sec,$min,$hour,$mday,$mon,$year);
311 sub cust_pkg_setup_cost {
312 my( $self, $speriod, $eperiod, $agentnum, %opt ) = @_;
315 if ( $opt{'classnum'} =~ /^(\d+)$/ ) {
317 $comparison = 'IS NULL';
320 $comparison = "= $1";
322 $where = "AND part_pkg.classnum $comparison";
324 $agentnum ||= $opt{'agentnum'};
326 my $total_sql = " SELECT SUM(part_pkg.setup_cost) ";
327 $total_sql .= " FROM cust_pkg
328 LEFT JOIN cust_main USING ( custnum )
329 LEFT JOIN part_pkg USING ( pkgpart )
332 AND ".$self->in_time_period_and_agent(
333 $speriod, $eperiod, $agentnum, 'cust_pkg.setup');
334 return $self->scalar_sql($total_sql);
337 sub cust_pkg_recur_cost {
338 my( $self, $speriod, $eperiod, $agentnum, %opt ) = @_;
341 if ( $opt{'classnum'} =~ /^(\d+)$/ ) {
343 $comparison = 'IS NULL';
346 $comparison = "= $1";
348 $where = " AND part_pkg.classnum $comparison";
350 $agentnum ||= $opt{'agentnum'};
351 # duplication of in_time_period_and_agent
352 # because we do it a little differently here
353 $where .= " AND cust_main.agentnum = $agentnum" if $agentnum;
355 $FS::CurrentUser::CurrentUser->agentnums_sql('table' => 'cust_main');
357 my $total_sql = " SELECT SUM(part_pkg.recur_cost) ";
358 $total_sql .= " FROM cust_pkg
359 LEFT JOIN cust_main USING ( custnum )
360 LEFT JOIN part_pkg USING ( pkgpart )
363 AND cust_pkg.setup < $eperiod
364 AND (cust_pkg.cancel > $speriod OR cust_pkg.cancel IS NULL)
366 return $self->scalar_sql($total_sql);
370 my( $self, $speriod, $eperiod, $agentnum, %opt ) = @_;
374 if ( $opt{'classnum'} =~ /^(\d+)$/ ) {
376 $comparison = "IS NULL";
378 $comparison = "= $1";
381 if ( $opt{'use_override'} ) {
383 part_pkg.classnum $comparison AND pkgpart_override IS NULL OR
384 override.classnum $comparison AND pkgpart_override IS NOT NULL
387 $where = "AND part_pkg.classnum $comparison";
391 $agentnum ||= $opt{'agentnum'};
394 " SELECT COALESCE( SUM(cust_bill_pkg.setup + cust_bill_pkg.recur), 0 ) ";
397 " / CASE COUNT(cust_pkg.*) WHEN 0 THEN 1 ELSE COUNT(cust_pkg.*) END "
398 if $opt{average_per_cust_pkg};
402 LEFT JOIN cust_bill USING ( invnum )
403 LEFT JOIN cust_main USING ( custnum )
404 LEFT JOIN cust_pkg USING ( pkgnum )
405 LEFT JOIN part_pkg USING ( pkgpart )
406 LEFT JOIN part_pkg AS override ON pkgpart_override = override.pkgpart
409 AND ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum);
411 if ($opt{use_usage} && $opt{use_usage} eq 'recurring') {
412 my $total = $self->scalar_sql($total_sql);
413 my $usage = cust_bill_pkg_detail(@_); #$speriod, $eperiod, $agentnum, %opt
414 return $total-$usage;
415 } elsif ($opt{use_usage} && $opt{use_usage} eq 'usage') {
416 return cust_bill_pkg_detail(@_); #$speriod, $eperiod, $agentnum, %opt
418 return $self->scalar_sql($total_sql);
422 sub cust_bill_pkg_detail {
423 my( $self, $speriod, $eperiod, $agentnum, %opt ) = @_;
425 my @where = ( "cust_bill_pkg.pkgnum != 0" );
427 if ( $opt{'classnum'} =~ /^(\d+)$/ ) {
429 $comparison = "IS NULL";
431 $comparison = "= $1";
434 if ( $opt{'use_override'} ) {
436 part_pkg.classnum $comparison AND pkgpart_override IS NULL OR
437 override.classnum $comparison AND pkgpart_override IS NOT NULL
440 push @where, "part_pkg.classnum $comparison";
444 if ( $opt{'usageclass'} =~ /^(\d+)$/ ) {
446 $comparison = "IS NULL";
448 $comparison = "= $1";
451 push @where, "cust_bill_pkg_detail.classnum $comparison";
454 $agentnum ||= $opt{'agentnum'};
456 my $where = join( ' AND ', @where );
458 my $total_sql = " SELECT SUM(amount) ";
461 " / CASE COUNT(cust_pkg.*) WHEN 0 THEN 1 ELSE COUNT(cust_pkg.*) END "
462 if $opt{average_per_cust_pkg};
465 " FROM cust_bill_pkg_detail
466 LEFT JOIN cust_bill_pkg USING ( billpkgnum )
467 LEFT JOIN cust_bill ON cust_bill_pkg.invnum = cust_bill.invnum
468 LEFT JOIN cust_main USING ( custnum )
469 LEFT JOIN cust_pkg ON cust_bill_pkg.pkgnum = cust_pkg.pkgnum
470 LEFT JOIN part_pkg USING ( pkgpart )
471 LEFT JOIN part_pkg AS override ON pkgpart_override = override.pkgpart
473 AND ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum);
475 $self->scalar_sql($total_sql);
479 sub cust_bill_pkg_discount {
480 my( $self, $speriod, $eperiod, $agentnum, %opt ) = @_;
483 #my $comparison = '';
484 #if ( $opt{'classnum'} =~ /^(\d+)$/ ) {
486 # $comparison = "IS NULL";
488 # $comparison = "= $1";
491 # if ( $opt{'use_override'} ) {
493 # part_pkg.classnum $comparison AND pkgpart_override IS NULL OR
494 # override.classnum $comparison AND pkgpart_override IS NOT NULL
497 # $where = "part_pkg.classnum $comparison";
501 $agentnum ||= $opt{'agentnum'};
504 " SELECT COALESCE( SUM( cust_bill_pkg_discount.amount ), 0 ) ";
507 # " / CASE COUNT(cust_pkg.*) WHEN 0 THEN 1 ELSE COUNT(cust_pkg.*) END "
508 # if $opt{average_per_cust_pkg};
511 " FROM cust_bill_pkg_discount
512 LEFT JOIN cust_bill_pkg USING ( billpkgnum )
513 LEFT JOIN cust_bill USING ( invnum )
514 LEFT JOIN cust_main USING ( custnum )
515 WHERE ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum);
516 # LEFT JOIN cust_pkg_discount USING ( pkgdiscountnum )
517 # LEFT JOIN discount USING ( discountnum )
518 # LEFT JOIN cust_pkg USING ( pkgnum )
519 # LEFT JOIN part_pkg USING ( pkgpart )
520 # LEFT JOIN part_pkg AS override ON pkgpart_override = override.pkgpart
522 return $self->scalar_sql($total_sql);
526 sub setup_pkg { shift->pkg_field( @_, 'setup' ); }
527 sub susp_pkg { shift->pkg_field( @_, 'susp' ); }
528 sub cancel_pkg { shift->pkg_field( @_, 'cancel'); }
531 my( $self, $speriod, $eperiod, $agentnum, $field ) = @_;
533 SELECT COUNT(*) FROM cust_pkg
534 LEFT JOIN cust_main USING ( custnum )
535 WHERE ". $self->in_time_period_and_agent( $speriod,
544 #this is going to be harder..
546 # my( $self, $speriod, $eperiod, $agentnum ) = @_;
547 # $self->scalar_sql("
548 # SELECT COUNT(*) FROM h_cust_pkg
553 sub in_time_period_and_agent {
554 my( $self, $speriod, $eperiod, $agentnum ) = splice(@_, 0, 4);
555 my $col = @_ ? shift() : '_date';
557 my $sql = "$col >= $speriod AND $col < $eperiod";
560 $sql .= " AND cust_main.agentnum = $agentnum"
563 #agent virtualization
565 $FS::CurrentUser::CurrentUser->agentnums_sql( 'table'=>'cust_main' );
571 my( $self, $sql ) = ( shift, shift );
572 my $sth = dbh->prepare($sql) or die dbh->errstr;
573 warn "FS::Report::Table::Monthly\n$sql\n" if $DEBUG;
575 or die "Unexpected error executing statement $sql: ". $sth->errstr;
576 $sth->fetchrow_arrayref->[0] || 0;