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 if ( $self->{'doublemonths'} ) {
63 my($firstLabel,$secondLabel) = @{$self->{'doublemonths'}};
64 push @{$data{label}}, "$smonth/$syear $firstLabel";
65 push @{$data{label}}, "$smonth/$syear $secondLabel";
68 push @{$data{label}}, "$smonth/$syear";
71 my $speriod = timelocal(0,0,0,1,$smonth-1,$syear);
72 push @{$data{speriod}}, $speriod;
73 if ( ++$smonth == 13 ) { $syear++; $smonth=1; }
74 my $eperiod = timelocal(0,0,0,1,$smonth-1,$syear);
75 push @{$data{eperiod}}, $eperiod;
78 my @items = @{$self->{'items'}};
80 for ( $i = 0; $i < scalar(@items); $i++ ) {
81 if ( $self->{'doublemonths'} ) {
82 my $item = $items[$i];
83 my @param = $self->{'params'} ? @{ $self->{'params'}[$i] }: ();
84 my $value = $self->$item($speriod, $eperiod, $agentnum, @param);
85 push @{$data{data}->[$col]}, $value;
87 @param = $self->{'params'} ? @{ $self->{'params'}[++$i] }: ();
88 $value = $self->$item($speriod, $eperiod, $agentnum, @param);
89 push @{$data{data}->[$col++]}, $value;
92 my $item = $items[$i];
93 my @param = $self->{'params'} ? @{ $self->{'params'}[$col] }: ();
94 my $value = $self->$item($speriod, $eperiod, $agentnum, @param);
95 push @{$data{data}->[$col++]}, $value;
101 #these need to get generalized, sheesh
102 $data{'items'} = $self->{'items'};
103 $data{'item_labels'} = $self->{'item_labels'} || $self->{'items'};
104 $data{'colors'} = $self->{'colors'};
105 $data{'links'} = $self->{'links'} || [];
108 #warn Dumper(\%data);
110 if ( $self->{'remove_empty'} ) {
112 #warn "removing empty rows\n";
115 #these need to get generalized, sheesh
121 foreach my $item ( @{$self->{'items'}} ) {
123 if ( grep { $_ != 0 } @{$data{'data'}->[$col]} ) {
124 push @newitems, $data{'items'}->[$col];
125 push @newlabels, $data{'item_labels'}->[$col];
126 push @newdata, $data{'data'}->[$col];
127 push @newcolors, $data{'colors'}->[$col];
128 push @newlinks, $data{'links'}->[$col];
134 $data{'items'} = \@newitems;
135 $data{'item_labels'} = \@newlabels;
136 $data{'data'} = \@newdata;
137 $data{'colors'} = \@newcolors;
138 $data{'links'} = \@newlinks;
143 #warn Dumper(\%data);
149 sub invoiced { #invoiced
150 my( $self, $speriod, $eperiod, $agentnum, %opt ) = @_;
155 LEFT JOIN cust_main USING ( custnum )
156 WHERE ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum)
157 . (%opt ? $self->for_custnum(%opt) : '')
162 sub netsales { #net sales
163 my( $self, $speriod, $eperiod, $agentnum, %opt ) = @_;
165 $self->invoiced($speriod,$eperiod,$agentnum,%opt)
166 - $self->netcredits($speriod,$eperiod,$agentnum,%opt);
172 my( $self, $speriod, $eperiod, $agentnum, %opt ) = @_;
174 $self->payments($speriod, $eperiod, $agentnum, %opt)
175 - $self->refunds( $speriod, $eperiod, $agentnum, %opt);
179 my( $self, $speriod, $eperiod, $agentnum ) = @_;
181 $self->receipts($speriod, $eperiod, $agentnum)
182 - $self->netrefunds( $speriod, $eperiod, $agentnum);
186 my( $self, $speriod, $eperiod, $agentnum, %opt ) = @_;
190 LEFT JOIN cust_main USING ( custnum )
191 WHERE ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum)
192 . (%opt ? $self->for_custnum(%opt) : '')
197 my( $self, $speriod, $eperiod, $agentnum ) = @_;
201 LEFT JOIN cust_main USING ( custnum )
202 WHERE ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum)
207 my( $self, $speriod, $eperiod, $agentnum, %opt ) = @_;
211 LEFT JOIN cust_main USING ( custnum )
212 WHERE ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum)
213 . (%opt ? $self->for_custnum(%opt) : '')
218 my( $self, $speriod, $eperiod, $agentnum, %opt ) = @_;
220 SELECT SUM(cust_credit_bill.amount)
221 FROM cust_credit_bill
222 LEFT JOIN cust_bill USING ( invnum )
223 LEFT JOIN cust_main USING ( custnum )
224 WHERE ". $self->in_time_period_and_agent( $speriod,
229 . (%opt ? $self->for_custnum(%opt) : '')
233 sub receipts { #net payments
234 my( $self, $speriod, $eperiod, $agentnum ) = @_;
236 SELECT SUM(cust_bill_pay.amount)
238 LEFT JOIN cust_bill USING ( invnum )
239 LEFT JOIN cust_main USING ( custnum )
240 WHERE ". $self->in_time_period_and_agent( $speriod,
249 my( $self, $speriod, $eperiod, $agentnum ) = @_;
251 SELECT SUM(cust_credit_refund.amount)
252 FROM cust_credit_refund
253 LEFT JOIN cust_credit USING ( crednum )
254 LEFT JOIN cust_main USING ( custnum )
255 WHERE ". $self->in_time_period_and_agent( $speriod,
263 #these should be auto-generated or $AUTOLOADed or something
265 my( $self, $speriod, $eperiod, $agentnum ) = @_;
266 $speriod = $self->_subtract_11mo($speriod);
267 $self->invoiced($speriod, $eperiod, $agentnum);
271 my( $self, $speriod, $eperiod, $agentnum ) = @_;
272 $speriod = $self->_subtract_11mo($speriod);
273 $self->netsales($speriod, $eperiod, $agentnum);
277 my( $self, $speriod, $eperiod, $agentnum ) = @_;
278 $speriod = $self->_subtract_11mo($speriod);
279 $self->receipts($speriod, $eperiod, $agentnum);
283 my( $self, $speriod, $eperiod, $agentnum ) = @_;
284 $speriod = $self->_subtract_11mo($speriod);
285 $self->payments($speriod, $eperiod, $agentnum);
289 my( $self, $speriod, $eperiod, $agentnum ) = @_;
290 $speriod = $self->_subtract_11mo($speriod);
291 $self->credits($speriod, $eperiod, $agentnum);
294 sub netcredits_12mo {
295 my( $self, $speriod, $eperiod, $agentnum ) = @_;
296 $speriod = $self->_subtract_11mo($speriod);
297 $self->netcredits($speriod, $eperiod, $agentnum);
301 my( $self, $speriod, $eperiod, $agentnum ) = @_;
302 $speriod = $self->_subtract_11mo($speriod);
303 $self->cashflow($speriod, $eperiod, $agentnum);
306 sub netcashflow_12mo {
307 my( $self, $speriod, $eperiod, $agentnum ) = @_;
308 $speriod = $self->_subtract_11mo($speriod);
309 $self->cashflow($speriod, $eperiod, $agentnum);
313 my( $self, $speriod, $eperiod, $agentnum ) = @_;
314 $speriod = $self->_subtract_11mo($speriod);
315 $self->refunds($speriod, $eperiod, $agentnum);
318 sub netrefunds_12mo {
319 my( $self, $speriod, $eperiod, $agentnum ) = @_;
320 $speriod = $self->_subtract_11mo($speriod);
321 $self->netrefunds($speriod, $eperiod, $agentnum);
325 #not being too bad with the false laziness
326 use Time::Local qw(timelocal);
328 my($self, $time) = @_;
329 my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($time) )[0,1,2,3,4,5];
331 if ( $mon < 0 ) { $mon+=12; $year--; }
332 timelocal($sec,$min,$hour,$mday,$mon,$year);
335 sub cust_pkg_setup_cost {
336 my( $self, $speriod, $eperiod, $agentnum, %opt ) = @_;
339 if ( $opt{'classnum'} =~ /^(\d+)$/ ) {
341 $comparison = 'IS NULL';
344 $comparison = "= $1";
346 $where = "AND part_pkg.classnum $comparison";
348 $agentnum ||= $opt{'agentnum'};
350 my $total_sql = " SELECT SUM(part_pkg.setup_cost) ";
351 $total_sql .= " FROM cust_pkg
352 LEFT JOIN cust_main USING ( custnum )
353 LEFT JOIN part_pkg USING ( pkgpart )
356 AND ".$self->in_time_period_and_agent(
357 $speriod, $eperiod, $agentnum, 'cust_pkg.setup');
358 return $self->scalar_sql($total_sql);
361 sub cust_pkg_recur_cost {
362 my( $self, $speriod, $eperiod, $agentnum, %opt ) = @_;
365 if ( $opt{'classnum'} =~ /^(\d+)$/ ) {
367 $comparison = 'IS NULL';
370 $comparison = "= $1";
372 $where = " AND part_pkg.classnum $comparison";
374 $agentnum ||= $opt{'agentnum'};
375 # duplication of in_time_period_and_agent
376 # because we do it a little differently here
377 $where .= " AND cust_main.agentnum = $agentnum" if $agentnum;
379 $FS::CurrentUser::CurrentUser->agentnums_sql('table' => 'cust_main');
381 my $total_sql = " SELECT SUM(part_pkg.recur_cost) ";
382 $total_sql .= " FROM cust_pkg
383 LEFT JOIN cust_main USING ( custnum )
384 LEFT JOIN part_pkg USING ( pkgpart )
387 AND cust_pkg.setup < $eperiod
388 AND (cust_pkg.cancel > $speriod OR cust_pkg.cancel IS NULL)
390 return $self->scalar_sql($total_sql);
394 my( $self, $speriod, $eperiod, $agentnum, %opt ) = @_;
398 if ( $opt{'classnum'} =~ /^(\d+)$/ ) {
400 $comparison = "IS NULL";
402 $comparison = "= $1";
405 if ( $opt{'use_override'} ) {
407 part_pkg.classnum $comparison AND pkgpart_override IS NULL OR
408 override.classnum $comparison AND pkgpart_override IS NOT NULL
411 $where = "AND part_pkg.classnum $comparison";
415 $agentnum ||= $opt{'agentnum'};
418 " SELECT COALESCE( SUM(cust_bill_pkg.setup + cust_bill_pkg.recur), 0 ) ";
421 " / CASE COUNT(cust_pkg.*) WHEN 0 THEN 1 ELSE COUNT(cust_pkg.*) END "
422 if $opt{average_per_cust_pkg};
426 LEFT JOIN cust_bill USING ( invnum )
427 LEFT JOIN cust_main USING ( custnum )
428 LEFT JOIN cust_pkg USING ( pkgnum )
429 LEFT JOIN part_pkg USING ( pkgpart )
430 LEFT JOIN part_pkg AS override ON pkgpart_override = override.pkgpart
433 AND ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum);
435 if ($opt{use_usage} && $opt{use_usage} eq 'recurring') {
436 my $total = $self->scalar_sql($total_sql);
437 my $usage = cust_bill_pkg_detail(@_); #$speriod, $eperiod, $agentnum, %opt
438 return $total-$usage;
439 } elsif ($opt{use_usage} && $opt{use_usage} eq 'usage') {
440 return cust_bill_pkg_detail(@_); #$speriod, $eperiod, $agentnum, %opt
442 return $self->scalar_sql($total_sql);
446 sub cust_bill_pkg_detail {
447 my( $self, $speriod, $eperiod, $agentnum, %opt ) = @_;
449 my @where = ( "cust_bill_pkg.pkgnum != 0" );
451 if ( $opt{'classnum'} =~ /^(\d+)$/ ) {
453 $comparison = "IS NULL";
455 $comparison = "= $1";
458 if ( $opt{'use_override'} ) {
460 part_pkg.classnum $comparison AND pkgpart_override IS NULL OR
461 override.classnum $comparison AND pkgpart_override IS NOT NULL
464 push @where, "part_pkg.classnum $comparison";
468 if ( $opt{'usageclass'} =~ /^(\d+)$/ ) {
470 $comparison = "IS NULL";
472 $comparison = "= $1";
475 push @where, "cust_bill_pkg_detail.classnum $comparison";
478 $agentnum ||= $opt{'agentnum'};
480 my $where = join( ' AND ', @where );
482 my $total_sql = " SELECT SUM(amount) ";
485 " / CASE COUNT(cust_pkg.*) WHEN 0 THEN 1 ELSE COUNT(cust_pkg.*) END "
486 if $opt{average_per_cust_pkg};
489 " FROM cust_bill_pkg_detail
490 LEFT JOIN cust_bill_pkg USING ( billpkgnum )
491 LEFT JOIN cust_bill ON cust_bill_pkg.invnum = cust_bill.invnum
492 LEFT JOIN cust_main USING ( custnum )
493 LEFT JOIN cust_pkg ON cust_bill_pkg.pkgnum = cust_pkg.pkgnum
494 LEFT JOIN part_pkg USING ( pkgpart )
495 LEFT JOIN part_pkg AS override ON pkgpart_override = override.pkgpart
497 AND ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum);
499 $self->scalar_sql($total_sql);
503 sub cust_bill_pkg_discount {
504 my( $self, $speriod, $eperiod, $agentnum, %opt ) = @_;
507 #my $comparison = '';
508 #if ( $opt{'classnum'} =~ /^(\d+)$/ ) {
510 # $comparison = "IS NULL";
512 # $comparison = "= $1";
515 # if ( $opt{'use_override'} ) {
517 # part_pkg.classnum $comparison AND pkgpart_override IS NULL OR
518 # override.classnum $comparison AND pkgpart_override IS NOT NULL
521 # $where = "part_pkg.classnum $comparison";
525 $agentnum ||= $opt{'agentnum'};
528 " SELECT COALESCE( SUM( cust_bill_pkg_discount.amount ), 0 ) ";
531 # " / CASE COUNT(cust_pkg.*) WHEN 0 THEN 1 ELSE COUNT(cust_pkg.*) END "
532 # if $opt{average_per_cust_pkg};
535 " FROM cust_bill_pkg_discount
536 LEFT JOIN cust_bill_pkg USING ( billpkgnum )
537 LEFT JOIN cust_bill USING ( invnum )
538 LEFT JOIN cust_main USING ( custnum )
539 WHERE ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum);
540 # LEFT JOIN cust_pkg_discount USING ( pkgdiscountnum )
541 # LEFT JOIN discount USING ( discountnum )
542 # LEFT JOIN cust_pkg USING ( pkgnum )
543 # LEFT JOIN part_pkg USING ( pkgpart )
544 # LEFT JOIN part_pkg AS override ON pkgpart_override = override.pkgpart
546 return $self->scalar_sql($total_sql);
550 sub setup_pkg { shift->pkg_field( @_, 'setup' ); }
551 sub susp_pkg { shift->pkg_field( @_, 'susp' ); }
552 sub cancel_pkg { shift->pkg_field( @_, 'cancel'); }
555 my( $self, $speriod, $eperiod, $agentnum, $field ) = @_;
557 SELECT COUNT(*) FROM cust_pkg
558 LEFT JOIN cust_main USING ( custnum )
559 WHERE ". $self->in_time_period_and_agent( $speriod,
568 #this is going to be harder..
570 # my( $self, $speriod, $eperiod, $agentnum ) = @_;
571 # $self->scalar_sql("
572 # SELECT COUNT(*) FROM h_cust_pkg
577 sub in_time_period_and_agent {
578 my( $self, $speriod, $eperiod, $agentnum ) = splice(@_, 0, 4);
579 my $col = @_ ? shift() : '_date';
581 my $sql = "$col >= $speriod AND $col < $eperiod";
584 $sql .= " AND cust_main.agentnum = $agentnum"
587 #agent virtualization
589 $FS::CurrentUser::CurrentUser->agentnums_sql( 'table'=>'cust_main' );
595 my ( $self, %opt ) = @_;
596 return '' unless $opt{'custnum'};
597 $opt{'custnum'} =~ /^\d+$/ ? " and custnum = $opt{custnum} " : '';
601 my( $self, $sql ) = ( shift, shift );
602 my $sth = dbh->prepare($sql) or die dbh->errstr;
603 warn "FS::Report::Table::Monthly\n$sql\n" if $DEBUG;
605 or die "Unexpected error executing statement $sql: ". $sth->errstr;
606 $sth->fetchrow_arrayref->[0] || 0;