1 package FS::Report::Table;
4 use vars qw( @ISA $DEBUG );
6 use Time::Local qw( timelocal );
7 use FS::UID qw( dbh driver_name );
11 $DEBUG = 0; # turning this on will trace all SQL statements, VERY noisy
12 @ISA = qw( FS::Report );
16 FS::Report::Table - Tables of report data
20 See the more specific report objects, currently only
21 FS::Report::Table::Monthly and FS::Report::Table::Daily.
25 The common interface for an observable named 'foo' is:
27 $report->foo($startdate, $enddate, $agentnum, %options)
29 This returns a scalar value for foo, over the period from
30 $startdate to $enddate, limited to agent $agentnum, subject to
35 =item signups: The number of customers signed up. Options are "refnum"
36 (limit by advertising source) and "indirect" (boolean, tells us to limit
37 to customers that have a referral_custnum that matches the advertising source).
42 my( $self, $speriod, $eperiod, $agentnum, %opt ) = @_;
43 my @where = ( $self->in_time_period_and_agent($speriod, $eperiod, $agentnum,
44 'cust_main.signupdate')
47 if ( $opt{'indirect'} ) {
48 $join = " JOIN cust_main AS referring_cust_main".
49 " ON (cust_main.referral_custnum = referring_cust_main.custnum)";
51 if ( $opt{'refnum'} ) {
52 push @where, "referring_cust_main.refnum = ".$opt{'refnum'};
55 elsif ( $opt{'refnum'} ) {
56 push @where, "refnum = ".$opt{'refnum'};
59 if ( $opt{'cust_classnum'} ) {
60 my $classnums = $opt{'cust_classnum'};
61 $classnums = [ $classnums ] if !ref($classnums);
62 @$classnums = grep /^\d+$/, @$classnums;
63 push @where, 'cust_main.classnum in('. join(',',@$classnums) .')';
67 "SELECT COUNT(*) FROM cust_main $join WHERE ".join(' AND ', @where)
71 =item invoiced: The total amount charged on all invoices.
75 sub invoiced { #invoiced
76 my( $self, $speriod, $eperiod, $agentnum, %opt ) = @_;
78 my $sql = 'SELECT SUM(cust_bill.charged) FROM cust_bill';
79 if ( $opt{'setuprecur'} ) {
81 FS::cust_bill_pkg->charged_sql($speriod, $eperiod, %opt).
82 ') FROM cust_bill_pkg JOIN cust_bill USING (invnum)';
87 LEFT JOIN cust_main USING ( custnum )
88 WHERE ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum).
94 =item netsales: invoiced - netcredits
98 sub netsales { #net sales
99 my( $self, $speriod, $eperiod, $agentnum, %opt ) = @_;
101 $self->invoiced( $speriod, $eperiod, $agentnum, %opt)
102 - $self->netcredits($speriod, $eperiod, $agentnum, %opt);
105 =item cashflow: payments - refunds
110 my( $self, $speriod, $eperiod, $agentnum, %opt ) = @_;
112 $self->payments($speriod, $eperiod, $agentnum, %opt)
113 - $self->refunds( $speriod, $eperiod, $agentnum, %opt);
116 =item netcashflow: payments - netrefunds
121 my( $self, $speriod, $eperiod, $agentnum, %opt ) = @_;
123 $self->receipts( $speriod, $eperiod, $agentnum, %opt)
124 - $self->netrefunds( $speriod, $eperiod, $agentnum, %opt);
127 =item payments: The sum of payments received in the period.
132 my( $self, $speriod, $eperiod, $agentnum, %opt ) = @_;
136 LEFT JOIN cust_main USING ( custnum )
137 WHERE ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum).
138 $self->for_opts(%opt)
142 =item credits: The sum of credits issued in the period.
147 my( $self, $speriod, $eperiod, $agentnum, %opt ) = @_;
151 LEFT JOIN cust_main USING ( custnum )
152 WHERE ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum).
153 $self->for_opts(%opt)
157 =item refunds: The sum of refunds paid in the period.
162 my( $self, $speriod, $eperiod, $agentnum, %opt ) = @_;
166 LEFT JOIN cust_main USING ( custnum )
167 WHERE ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum).
168 $self->for_opts(%opt)
172 =item netcredits: The sum of credit applications to invoices in the period.
177 my( $self, $speriod, $eperiod, $agentnum, %opt ) = @_;
179 my $sql = 'SELECT SUM(cust_credit_bill.amount) FROM cust_credit_bill';
180 if ( $opt{'setuprecur'} ) {
181 $sql = 'SELECT SUM('.
182 FS::cust_bill_pkg->credited_sql($speriod, $eperiod, %opt).
183 ') FROM cust_bill_pkg';
188 LEFT JOIN cust_bill USING ( invnum )
189 LEFT JOIN cust_main USING ( custnum )
190 WHERE ". $self->in_time_period_and_agent( $speriod,
195 $self->for_opts(%opt)
199 =item receipts: The sum of payment applications to invoices in the period.
203 sub receipts { #net payments
204 my( $self, $speriod, $eperiod, $agentnum, %opt ) = @_;
206 my $sql = 'SELECT SUM(cust_bill_pay.amount) FROM cust_bill_pay';
207 if ( $opt{'setuprecur'} ) {
208 $sql = 'SELECT SUM('.
209 FS::cust_bill_pkg->paid_sql($speriod, $eperiod, %opt).
210 ') FROM cust_bill_pkg';
215 LEFT JOIN cust_bill USING ( invnum )
216 LEFT JOIN cust_main USING ( custnum )
217 WHERE ". $self->in_time_period_and_agent( $speriod,
222 $self->for_opts(%opt)
226 =item netrefunds: The sum of refund applications to credits in the period.
231 my( $self, $speriod, $eperiod, $agentnum, %opt ) = @_;
233 SELECT SUM(cust_credit_refund.amount)
234 FROM cust_credit_refund
235 LEFT JOIN cust_credit USING ( crednum )
236 LEFT JOIN cust_main USING ( custnum )
237 WHERE ". $self->in_time_period_and_agent( $speriod,
242 $self->for_opts(%opt)
248 #these should be auto-generated or $AUTOLOADed or something
250 my( $self, $speriod, $eperiod, $agentnum ) = @_;
251 $speriod = $self->_subtract_11mo($speriod);
252 $self->invoiced($speriod, $eperiod, $agentnum);
256 my( $self, $speriod, $eperiod, $agentnum ) = @_;
257 $speriod = $self->_subtract_11mo($speriod);
258 $self->netsales($speriod, $eperiod, $agentnum);
262 my( $self, $speriod, $eperiod, $agentnum ) = @_;
263 $speriod = $self->_subtract_11mo($speriod);
264 $self->receipts($speriod, $eperiod, $agentnum);
268 my( $self, $speriod, $eperiod, $agentnum ) = @_;
269 $speriod = $self->_subtract_11mo($speriod);
270 $self->payments($speriod, $eperiod, $agentnum);
274 my( $self, $speriod, $eperiod, $agentnum ) = @_;
275 $speriod = $self->_subtract_11mo($speriod);
276 $self->credits($speriod, $eperiod, $agentnum);
279 sub netcredits_12mo {
280 my( $self, $speriod, $eperiod, $agentnum ) = @_;
281 $speriod = $self->_subtract_11mo($speriod);
282 $self->netcredits($speriod, $eperiod, $agentnum);
286 my( $self, $speriod, $eperiod, $agentnum ) = @_;
287 $speriod = $self->_subtract_11mo($speriod);
288 $self->cashflow($speriod, $eperiod, $agentnum);
291 sub netcashflow_12mo {
292 my( $self, $speriod, $eperiod, $agentnum ) = @_;
293 $speriod = $self->_subtract_11mo($speriod);
294 $self->cashflow($speriod, $eperiod, $agentnum);
298 my( $self, $speriod, $eperiod, $agentnum ) = @_;
299 $speriod = $self->_subtract_11mo($speriod);
300 $self->refunds($speriod, $eperiod, $agentnum);
303 sub netrefunds_12mo {
304 my( $self, $speriod, $eperiod, $agentnum ) = @_;
305 $speriod = $self->_subtract_11mo($speriod);
306 $self->netrefunds($speriod, $eperiod, $agentnum);
310 #not being too bad with the false laziness
312 my($self, $time) = @_;
313 my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($time) )[0,1,2,3,4,5];
315 if ( $mon < 0 ) { $mon+=12; $year--; }
316 timelocal($sec,$min,$hour,$mday,$mon,$year);
319 =item cust_pkg_setup_cost: The total setup costs of packages setup in the period
321 'classnum': limit to this package class.
325 sub cust_pkg_setup_cost {
326 my( $self, $speriod, $eperiod, $agentnum, %opt ) = @_;
329 if ( $opt{'classnum'} =~ /^(\d+)$/ ) {
331 $comparison = 'IS NULL';
334 $comparison = "= $1";
336 $where = "AND part_pkg.classnum $comparison";
338 $agentnum ||= $opt{'agentnum'};
340 my $total_sql = " SELECT SUM(part_pkg.setup_cost) ";
341 $total_sql .= " FROM cust_pkg
342 LEFT JOIN cust_main USING ( custnum )
343 LEFT JOIN part_pkg USING ( pkgpart )
346 AND ".$self->in_time_period_and_agent(
347 $speriod, $eperiod, $agentnum, 'cust_pkg.setup');
348 return $self->scalar_sql($total_sql);
351 =item cust_pkg_recur_cust: the total recur costs of packages in the period
353 'classnum': limit to this package class.
357 sub cust_pkg_recur_cost {
358 my( $self, $speriod, $eperiod, $agentnum, %opt ) = @_;
361 if ( $opt{'classnum'} =~ /^(\d+)$/ ) {
363 $comparison = 'IS NULL';
366 $comparison = "= $1";
368 $where = " AND part_pkg.classnum $comparison";
370 $agentnum ||= $opt{'agentnum'};
371 # duplication of in_time_period_and_agent
372 # because we do it a little differently here
373 $where .= " AND cust_main.agentnum = $agentnum" if $agentnum;
375 $FS::CurrentUser::CurrentUser->agentnums_sql('table' => 'cust_main');
377 my $total_sql = " SELECT SUM(part_pkg.recur_cost) ";
378 $total_sql .= " FROM cust_pkg
379 LEFT JOIN cust_main USING ( custnum )
380 LEFT JOIN part_pkg USING ( pkgpart )
383 AND cust_pkg.setup < $eperiod
384 AND (cust_pkg.cancel > $speriod OR cust_pkg.cancel IS NULL)
386 return $self->scalar_sql($total_sql);
389 =item cust_bill_pkg: the total package charges on invoice line items.
391 'charges': limit the type of charges included (setup, recur, usage).
392 Should be a string containing one or more of 'S', 'R', or 'U'; if
393 unspecified, defaults to all three.
395 'classnum': limit to this package class.
397 'use_override': for line items generated by an add-on package, use the class
398 of the add-on rather than the base package.
400 'freq': limit to packages with this frequency. Currently uses the part_pkg
401 frequency, so term discounted packages may give odd results.
403 'distribute': for non-monthly recurring charges, ignore the invoice
404 date. Instead, consider the line item's starting/ending dates. Determine
405 the fraction of the line item duration that falls within the specified
406 interval and return that fraction of the recurring charges. This is
407 somewhat experimental.
409 'project': enable if this is a projected period. This is very experimental.
415 my( $speriod, $eperiod, $agentnum, %opt ) = @_;
417 my %charges = map {$_=>1} split('', $opt{'charges'} || 'SRU');
420 $sum += $self->cust_bill_pkg_setup(@_) if $charges{S};
421 $sum += $self->cust_bill_pkg_recur(@_) if $charges{R};
422 $sum += $self->cust_bill_pkg_detail(@_) if $charges{U};
426 my $cust_bill_pkg_join = '
427 LEFT JOIN cust_bill USING ( invnum )
428 LEFT JOIN cust_main USING ( custnum )
429 LEFT JOIN cust_pkg USING ( pkgnum )
430 LEFT JOIN part_pkg USING ( pkgpart )
431 LEFT JOIN part_pkg AS override ON pkgpart_override = override.pkgpart';
433 sub cust_bill_pkg_setup {
435 my ($speriod, $eperiod, $agentnum, %opt) = @_;
436 # no projecting setup fees--use real invoices only
437 # but evaluate this anyway, because the design of projection is that
438 # if there are somehow real setup fees in the future, we want to count
441 $agentnum ||= $opt{'agentnum'};
445 $self->with_classnum($opt{'classnum'}, $opt{'use_override'}),
446 $self->in_time_period_and_agent($speriod, $eperiod, $agentnum),
449 # yuck, false laziness
450 push @where, "cust_main.refnum = ". $opt{'refnum'} if $opt{'refnum'};
452 if ( $opt{'cust_classnum'} ) {
453 my $classnums = $opt{'cust_classnum'};
454 $classnums = [ $classnums ] if !ref($classnums);
455 @$classnums = grep /^\d+$/, @$classnums;
456 push @where, 'cust_main.classnum in('. join(',',@$classnums) .')';
459 my $total_sql = "SELECT COALESCE(SUM(cust_bill_pkg.setup),0)
462 WHERE " . join(' AND ', grep $_, @where);
464 $self->scalar_sql($total_sql);
467 sub cust_bill_pkg_recur {
469 my ($speriod, $eperiod, $agentnum, %opt) = @_;
471 $agentnum ||= $opt{'agentnum'};
472 my $cust_bill_pkg = $opt{'project'} ? 'v_cust_bill_pkg' : 'cust_bill_pkg';
476 $self->with_classnum($opt{'classnum'}, $opt{'use_override'}),
479 push @where, 'cust_main.refnum = '. $opt{'refnum'} if $opt{'refnum'};
481 if ( $opt{'cust_classnum'} ) {
482 my $classnums = $opt{'cust_classnum'};
483 $classnums = [ $classnums ] if !ref($classnums);
484 @$classnums = grep /^\d+$/, @$classnums;
485 push @where, 'cust_main.classnum in('. join(',',@$classnums) .')';
488 # subtract all usage from the line item regardless of date
490 if ( $opt{'project'} ) {
491 $item_usage = 'usage'; #already calculated
494 $item_usage = '( SELECT COALESCE(SUM(amount),0)
495 FROM cust_bill_pkg_detail
496 WHERE cust_bill_pkg_detail.billpkgnum = cust_bill_pkg.billpkgnum )';
498 my $recur_fraction = '';
500 if ( $opt{'distribute'} ) {
501 push @where, "cust_main.agentnum = $agentnum" if $agentnum;
503 "$cust_bill_pkg.sdate < $eperiod",
504 "$cust_bill_pkg.edate >= $speriod",
506 # the fraction of edate - sdate that's within [speriod, eperiod]
507 $recur_fraction = " *
508 CAST(LEAST($eperiod, $cust_bill_pkg.edate) -
509 GREATEST($speriod, $cust_bill_pkg.sdate) AS DECIMAL) /
510 ($cust_bill_pkg.edate - $cust_bill_pkg.sdate)";
513 # we don't want to have to create v_cust_bill
514 my $_date = $opt{'project'} ? 'v_cust_bill_pkg._date' : 'cust_bill._date';
516 $self->in_time_period_and_agent($speriod, $eperiod, $agentnum, $_date);
519 my $total_sql = 'SELECT '.
520 "COALESCE(SUM(($cust_bill_pkg.recur - $item_usage) $recur_fraction),0)
523 WHERE ".join(' AND ', grep $_, @where);
525 $self->scalar_sql($total_sql);
528 =item cust_bill_pkg_detail: the total usage charges in detail lines.
530 Arguments as for C<cust_bill_pkg>, plus:
532 'usageclass': limit to this usage class number.
536 sub cust_bill_pkg_detail {
537 my( $self, $speriod, $eperiod, $agentnum, %opt ) = @_;
539 my @where = ( "cust_bill_pkg.pkgnum != 0" );
541 push @where, 'cust_main.refnum = '. $opt{'refnum'} if $opt{'refnum'};
543 if ( $opt{'cust_classnum'} ) {
544 my $classnums = $opt{'cust_classnum'};
545 $classnums = [ $classnums ] if !ref($classnums);
546 @$classnums = grep /^\d+$/, @$classnums;
547 push @where, 'cust_main.classnum in('. join(',',@$classnums) .')';
550 $agentnum ||= $opt{'agentnum'};
553 $self->with_classnum($opt{'classnum'}, $opt{'use_override'}),
554 $self->with_usageclass($opt{'usageclass'}),
557 if ( $opt{'distribute'} ) {
558 # then limit according to the usage time, not the billing date
559 push @where, $self->in_time_period_and_agent($speriod, $eperiod, $agentnum,
560 'cust_bill_pkg_detail.startdate'
564 push @where, $self->in_time_period_and_agent($speriod, $eperiod, $agentnum,
569 my $total_sql = " SELECT SUM(amount) ";
572 " / CASE COUNT(cust_pkg.*) WHEN 0 THEN 1 ELSE COUNT(cust_pkg.*) END "
573 if $opt{average_per_cust_pkg};
576 " FROM cust_bill_pkg_detail
577 LEFT JOIN cust_bill_pkg USING ( billpkgnum )
578 LEFT JOIN cust_bill ON cust_bill_pkg.invnum = cust_bill.invnum
579 LEFT JOIN cust_main USING ( custnum )
580 LEFT JOIN cust_pkg ON cust_bill_pkg.pkgnum = cust_pkg.pkgnum
581 LEFT JOIN part_pkg USING ( pkgpart )
582 LEFT JOIN part_pkg AS override ON pkgpart_override = override.pkgpart
583 WHERE ".join( ' AND ', grep $_, @where );
585 $self->scalar_sql($total_sql);
589 sub cust_bill_pkg_discount {
590 my( $self, $speriod, $eperiod, $agentnum, %opt ) = @_;
593 #my $comparison = '';
594 #if ( $opt{'classnum'} =~ /^(\d+)$/ ) {
596 # $comparison = "IS NULL";
598 # $comparison = "= $1";
601 # if ( $opt{'use_override'} ) {
603 # part_pkg.classnum $comparison AND pkgpart_override IS NULL OR
604 # override.classnum $comparison AND pkgpart_override IS NOT NULL
607 # $where = "part_pkg.classnum $comparison";
611 $agentnum ||= $opt{'agentnum'};
614 " SELECT COALESCE( SUM( cust_bill_pkg_discount.amount ), 0 ) ";
617 # " / CASE COUNT(cust_pkg.*) WHEN 0 THEN 1 ELSE COUNT(cust_pkg.*) END "
618 # if $opt{average_per_cust_pkg};
621 " FROM cust_bill_pkg_discount
622 LEFT JOIN cust_bill_pkg USING ( billpkgnum )
623 LEFT JOIN cust_bill USING ( invnum )
624 LEFT JOIN cust_main USING ( custnum )
625 WHERE ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum);
626 # LEFT JOIN cust_pkg_discount USING ( pkgdiscountnum )
627 # LEFT JOIN discount USING ( discountnum )
628 # LEFT JOIN cust_pkg USING ( pkgnum )
629 # LEFT JOIN part_pkg USING ( pkgpart )
630 # LEFT JOIN part_pkg AS override ON pkgpart_override = override.pkgpart
632 return $self->scalar_sql($total_sql);
636 sub setup_pkg { shift->pkg_field( 'setup', @_ ); }
637 sub susp_pkg { shift->pkg_field( 'susp', @_ ); }
638 sub cancel_pkg { shift->pkg_field( 'cancel', @_ ); }
641 my( $self, $field, $speriod, $eperiod, $agentnum ) = @_;
643 SELECT COUNT(*) FROM cust_pkg
644 LEFT JOIN cust_main USING ( custnum )
645 WHERE ". $self->in_time_period_and_agent( $speriod,
654 #this is going to be harder..
656 # my( $self, $speriod, $eperiod, $agentnum ) = @_;
657 # $self->scalar_sql("
658 # SELECT COUNT(*) FROM h_cust_pkg
663 sub in_time_period_and_agent {
664 my( $self, $speriod, $eperiod, $agentnum ) = splice(@_, 0, 4);
665 my $col = @_ ? shift() : '_date';
667 my $sql = "$col >= $speriod AND $col < $eperiod";
670 $sql .= " AND cust_main.agentnum = $agentnum"
673 #agent virtualization
675 $FS::CurrentUser::CurrentUser->agentnums_sql( 'table'=>'cust_main' );
681 my ( $self, %opt ) = @_;
683 if ( $opt{'custnum'} =~ /^(\d+)$/ ) {
684 $sql .= " and custnum = $1 ";
686 if ( $opt{'refnum'} =~ /^(\d+)$/ ) {
687 $sql .= " and refnum = $1 ";
689 if ( $opt{'cust_classnum'} ) {
690 my $classnums = $opt{'cust_classnum'};
691 $classnums = [ $classnums ] if !ref($classnums);
692 @$classnums = grep /^\d+$/, @$classnums;
693 $sql .= ' and cust_main.classnum in('. join(',',@$classnums) .')'
702 my ($classnum, $use_override) = @_;
703 return '' unless $classnum =~ /^\d+$/;
705 if ( $classnum == 0 ) {
706 $comparison = 'IS NULL';
709 $comparison = "= $classnum";
711 if ( $use_override ) {
713 part_pkg.classnum $comparison AND pkgpart_override IS NULL OR
714 override.classnum $comparison AND pkgpart_override IS NOT NULL
718 return "part_pkg.classnum $comparison";
722 sub with_usageclass {
724 my ($classnum, $use_override) = @_;
725 return '' unless $classnum =~ /^\d+$/;
727 if ( $classnum == 0 ) {
728 $comparison = 'IS NULL';
731 $comparison = "= $classnum";
733 return "cust_bill_pkg_detail.classnum $comparison";
737 my( $self, $sql ) = ( shift, shift );
738 my $sth = dbh->prepare($sql) or die dbh->errstr;
739 warn "FS::Report::Table\n$sql\n" if $DEBUG;
741 or die "Unexpected error executing statement $sql: ". $sth->errstr;
742 $sth->fetchrow_arrayref->[0] || 0;
751 =item init_projection
753 Sets up for future projection of all observables on the report. Currently
754 this is limited to 'cust_bill_pkg'.
758 sub init_projection {
759 # this is weird special case stuff--some redesign may be needed
760 # to use it for anything else
763 if ( driver_name ne 'Pg' ) {
764 # also database-specific for now
765 die "projection reports not supported on this platform";
768 my %items = map {$_ => 1} @{ $self->{items} };
769 if ($items{'cust_bill_pkg'}) {
773 # could use TEMPORARY TABLE but we're already transaction-protected
774 'DROP TABLE IF EXISTS v_cust_bill_pkg',
775 'CREATE TABLE v_cust_bill_pkg ' .
776 '(LIKE cust_bill_pkg,
777 usage numeric(10,2), _date integer, expire integer)',
778 # XXX this should be smart enough to take only the ones with
779 # sdate/edate overlapping the ROI, for performance
780 "INSERT INTO v_cust_bill_pkg (
781 SELECT cust_bill_pkg.*,
782 (SELECT COALESCE(SUM(amount),0) FROM cust_bill_pkg_detail
783 WHERE cust_bill_pkg_detail.billpkgnum = cust_bill_pkg.billpkgnum),
786 FROM cust_bill_pkg $cust_bill_pkg_join
789 foreach my $sql (@sql) {
790 warn "[init_projection] $sql\n" if $DEBUG;
791 $dbh->do($sql) or die $dbh->errstr;
796 =item extend_projection START END
798 Generates data for the next period of projection. This will be called
799 for sequential periods where the END of one equals the START of the next
804 sub extend_projection {
806 my ($speriod, $eperiod) = @_;
807 my %items = map {$_ => 1} @{ $self->{items} };
808 if ($items{'cust_bill_pkg'}) {
810 # Find all line items that end after the start of the period (and have
811 # recurring fees, and don't expire before they end). Choose the latest
812 # one for each package. If it ends before the end of the period, copy
813 # it forward by one billing period.
814 # Repeat this until the latest line item for each package no longer ends
815 # within the period. This is certain to happen in finitely many
816 # iterations as long as freq > 0.
817 # - Pg only, obviously.
818 # - Gives bad results if freq_override is used.
819 my @fields = ( FS::cust_bill_pkg->fields, qw( usage _date expire ) );
820 my $insert_fields = join(',', @fields);
821 my $add_freq = sub { # emulate FS::part_pkg::add_freq
823 "EXTRACT( EPOCH FROM TO_TIMESTAMP($field) + (CASE WHEN freq ~ E'\\\\D' ".
824 "THEN freq ELSE freq || 'mon' END)::INTERVAL) AS $field";
828 $_ = $add_freq->('edate');
830 elsif ($_ eq 'sdate') {
831 $_ = 'edate AS sdate'
833 elsif ($_ eq 'setup') {
834 $_ = '0 AS setup' #because recurring only
836 elsif ($_ eq '_date') {
837 $_ = $add_freq->('_date');
840 my $select_fields = join(',', @fields);
843 # Subquery here because we need to DISTINCT the whole set, select the
844 # latest charge per pkgnum, and _then_ check edate < $eperiod
845 # and edate < expire.
846 "INSERT INTO v_cust_bill_pkg ($insert_fields)
847 SELECT $select_fields FROM (
848 SELECT DISTINCT ON (pkgnum) * FROM v_cust_bill_pkg
849 WHERE edate >= $speriod
853 ORDER BY pkgnum, edate DESC
855 WHERE edate < $eperiod AND (edate < expire OR expire IS NULL)";
858 warn "[extend_projection] $sql\n" if $DEBUG;
859 $rows = $dbh->do($sql) or die $dbh->errstr;
860 warn "[extend_projection] $rows rows\n" if $DEBUG;
871 L<FS::Report::Table::Monthly>, reports in the web interface.