1 package FS::Report::Table::Monthly;
10 @ISA = qw( FS::Report::Table );
14 FS::Report::Table::Monthly - Tables of report data, indexed monthly
18 use FS::Report::Table::Monthly;
20 my $report = new FS::Report::Table::Monthly (
21 '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)
51 my $smonth = $self->{'start_month'};
52 my $syear = $self->{'start_year'};
53 my $emonth = $self->{'end_month'};
54 my $eyear = $self->{'end_year'};
55 my $agentnum = $self->{'agentnum'};
59 while ( $syear < $eyear || ( $syear == $eyear && $smonth < $emonth+1 ) ) {
61 push @{$data{label}}, "$smonth/$syear";
63 my $speriod = timelocal(0,0,0,1,$smonth-1,$syear);
64 push @{$data{speriod}}, $speriod;
65 if ( ++$smonth == 13 ) { $syear++; $smonth=1; }
66 my $eperiod = timelocal(0,0,0,1,$smonth-1,$syear);
67 push @{$data{eperiod}}, $eperiod;
71 foreach my $item ( @{$self->{'items'}} ) {
72 my @param = $self->{'params'} ? @{ $self->{'params'}[$col] }: ();
73 my $value = $self->$item($speriod, $eperiod, $agentnum, @param);
74 #push @{$data{$item}}, $value;
75 push @{$data{data}->[$col++]}, $value;
80 #these need to get generalized, sheesh
81 $data{'items'} = $self->{'items'};
82 $data{'item_labels'} = $self->{'item_labels'} || $self->{'items'};
83 $data{'colors'} = $self->{'colors'};
84 $data{'links'} = $self->{'links'} || [];
89 if ( $self->{'remove_empty'} ) {
91 #warn "removing empty rows\n";
94 #these need to get generalized, sheesh
100 foreach my $item ( @{$self->{'items'}} ) {
102 if ( grep { $_ != 0 } @{$data{'data'}->[$col]} ) {
103 push @newitems, $data{'items'}->[$col];
104 push @newlabels, $data{'item_labels'}->[$col];
105 push @newdata, $data{'data'}->[$col];
106 push @newcolors, $data{'colors'}->[$col];
107 push @newlinks, $data{'links'}->[$col];
113 $data{'items'} = \@newitems;
114 $data{'item_labels'} = \@newlabels;
115 $data{'data'} = \@newdata;
116 $data{'colors'} = \@newcolors;
117 $data{'links'} = \@newlinks;
122 #warn Dumper(\%data);
128 sub invoiced { #invoiced
129 my( $self, $speriod, $eperiod, $agentnum ) = @_;
134 LEFT JOIN cust_main USING ( custnum )
135 WHERE ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum)
140 sub netsales { #net sales
141 my( $self, $speriod, $eperiod, $agentnum ) = @_;
143 $self->invoiced($speriod,$eperiod,$agentnum)
144 - $self->credits( $speriod,$eperiod,$agentnum);
150 my( $self, $speriod, $eperiod, $agentnum ) = @_;
152 $self->payments($speriod, $eperiod, $agentnum)
153 - $self->refunds( $speriod, $eperiod, $agentnum);
157 my( $self, $speriod, $eperiod, $agentnum ) = @_;
159 $self->receipts($speriod, $eperiod, $agentnum)
160 - $self->netrefunds( $speriod, $eperiod, $agentnum);
164 my( $self, $speriod, $eperiod, $agentnum ) = @_;
168 LEFT JOIN cust_main USING ( custnum )
169 WHERE ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum)
174 my( $self, $speriod, $eperiod, $agentnum ) = @_;
178 LEFT JOIN cust_main USING ( custnum )
179 WHERE ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum)
184 my( $self, $speriod, $eperiod, $agentnum ) = @_;
188 LEFT JOIN cust_main USING ( custnum )
189 WHERE ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum)
194 my( $self, $speriod, $eperiod, $agentnum ) = @_;
196 SELECT SUM(cust_credit_bill.amount)
197 FROM cust_credit_bill
198 LEFT JOIN cust_bill USING ( invnum )
199 LEFT JOIN cust_main USING ( custnum )
200 WHERE ". $self->in_time_period_and_agent( $speriod,
208 sub receipts { #net payments
209 my( $self, $speriod, $eperiod, $agentnum ) = @_;
211 SELECT SUM(cust_bill_pay.amount)
213 LEFT JOIN cust_bill USING ( invnum )
214 LEFT JOIN cust_main USING ( custnum )
215 WHERE ". $self->in_time_period_and_agent( $speriod,
224 my( $self, $speriod, $eperiod, $agentnum ) = @_;
226 SELECT SUM(cust_credit_refund.amount)
227 FROM cust_credit_refund
228 LEFT JOIN cust_credit USING ( crednum )
229 LEFT JOIN cust_main USING ( custnum )
230 WHERE ". $self->in_time_period_and_agent( $speriod,
238 #these should be auto-generated or $AUTOLOADed or something
240 my( $self, $speriod, $eperiod, $agentnum ) = @_;
241 $speriod = $self->_subtract_11mo($speriod);
242 $self->invoiced($speriod, $eperiod, $agentnum);
246 my( $self, $speriod, $eperiod, $agentnum ) = @_;
247 $speriod = $self->_subtract_11mo($speriod);
248 $self->netsales($speriod, $eperiod, $agentnum);
252 my( $self, $speriod, $eperiod, $agentnum ) = @_;
253 $speriod = $self->_subtract_11mo($speriod);
254 $self->receipts($speriod, $eperiod, $agentnum);
258 my( $self, $speriod, $eperiod, $agentnum ) = @_;
259 $speriod = $self->_subtract_11mo($speriod);
260 $self->payments($speriod, $eperiod, $agentnum);
264 my( $self, $speriod, $eperiod, $agentnum ) = @_;
265 $speriod = $self->_subtract_11mo($speriod);
266 $self->credits($speriod, $eperiod, $agentnum);
269 sub netcredits_12mo {
270 my( $self, $speriod, $eperiod, $agentnum ) = @_;
271 $speriod = $self->_subtract_11mo($speriod);
272 $self->netcredits($speriod, $eperiod, $agentnum);
276 my( $self, $speriod, $eperiod, $agentnum ) = @_;
277 $speriod = $self->_subtract_11mo($speriod);
278 $self->cashflow($speriod, $eperiod, $agentnum);
281 sub netcashflow_12mo {
282 my( $self, $speriod, $eperiod, $agentnum ) = @_;
283 $speriod = $self->_subtract_11mo($speriod);
284 $self->cashflow($speriod, $eperiod, $agentnum);
288 my( $self, $speriod, $eperiod, $agentnum ) = @_;
289 $speriod = $self->_subtract_11mo($speriod);
290 $self->refunds($speriod, $eperiod, $agentnum);
293 sub netrefunds_12mo {
294 my( $self, $speriod, $eperiod, $agentnum ) = @_;
295 $speriod = $self->_subtract_11mo($speriod);
296 $self->netrefunds($speriod, $eperiod, $agentnum);
300 #not being too bad with the false laziness
301 use Time::Local qw(timelocal);
303 my($self, $time) = @_;
304 my ($sec,$min,$hour,$mday,$mon,$year) = (localtime($time) )[0,1,2,3,4,5];
306 if ( $mon < 0 ) { $mon+=12; $year--; }
307 timelocal($sec,$min,$hour,$mday,$mon,$year);
311 my( $self, $speriod, $eperiod, $agentnum, %opt ) = @_;
315 if ( $opt{'classnum'} =~ /^(\d+)$/ ) {
317 $comparison = "IS NULL";
319 $comparison = "= $1";
322 if ( $opt{'use_override'} ) {
324 part_pkg.classnum $comparison AND pkgpart_override IS NULL OR
325 override.classnum $comparison AND pkgpart_override IS NOT NULL
328 $where = "part_pkg.classnum $comparison";
332 $agentnum ||= $opt{'agentnum'};
335 " SELECT COALESCE( SUM(cust_bill_pkg.setup + cust_bill_pkg.recur), 0 ) ";
338 " / CASE COUNT(cust_pkg.*) WHEN 0 THEN 1 ELSE COUNT(cust_pkg.*) END "
339 if $opt{average_per_cust_pkg};
343 LEFT JOIN cust_bill USING ( invnum )
344 LEFT JOIN cust_main USING ( custnum )
345 LEFT JOIN cust_pkg USING ( pkgnum )
346 LEFT JOIN part_pkg USING ( pkgpart )
347 LEFT JOIN part_pkg AS override ON pkgpart_override = override.pkgpart
350 AND ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum);
352 if ($opt{use_usage} && $opt{use_usage} eq 'recurring') {
353 my $total = $self->scalar_sql($total_sql);
354 my $usage = cust_bill_pkg_detail(@_); #$speriod, $eperiod, $agentnum, %opt
355 return $total-$usage;
356 } elsif ($opt{use_usage} && $opt{use_usage} eq 'usage') {
357 return cust_bill_pkg_detail(@_); #$speriod, $eperiod, $agentnum, %opt
359 return $self->scalar_sql($total_sql);
363 sub cust_bill_pkg_detail {
364 my( $self, $speriod, $eperiod, $agentnum, %opt ) = @_;
366 my @where = ( "cust_bill_pkg.pkgnum != 0" );
368 if ( $opt{'classnum'} =~ /^(\d+)$/ ) {
370 $comparison = "IS NULL";
372 $comparison = "= $1";
375 if ( $opt{'use_override'} ) {
377 part_pkg.classnum $comparison AND pkgpart_override IS NULL OR
378 override.classnum $comparison AND pkgpart_override IS NOT NULL
381 push @where, "part_pkg.classnum $comparison";
385 if ( $opt{'usageclass'} =~ /^(\d+)$/ ) {
387 $comparison = "IS NULL";
389 $comparison = "= $1";
392 push @where, "cust_bill_pkg_detail.classnum $comparison";
395 $agentnum ||= $opt{'agentnum'};
397 my $where = join( ' AND ', @where );
399 my $total_sql = " SELECT SUM(amount) ";
402 " / CASE COUNT(cust_pkg.*) WHEN 0 THEN 1 ELSE COUNT(cust_pkg.*) END "
403 if $opt{average_per_cust_pkg};
406 " FROM cust_bill_pkg_detail
407 LEFT JOIN cust_bill_pkg USING ( billpkgnum )
408 LEFT JOIN cust_bill ON cust_bill_pkg.invnum = cust_bill.invnum
409 LEFT JOIN cust_main USING ( custnum )
410 LEFT JOIN cust_pkg ON cust_bill_pkg.pkgnum = cust_pkg.pkgnum
411 LEFT JOIN part_pkg USING ( pkgpart )
412 LEFT JOIN part_pkg AS override ON pkgpart_override = override.pkgpart
414 AND ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum);
416 $self->scalar_sql($total_sql);
420 sub cust_bill_pkg_discount {
421 my( $self, $speriod, $eperiod, $agentnum, %opt ) = @_;
424 #my $comparison = '';
425 #if ( $opt{'classnum'} =~ /^(\d+)$/ ) {
427 # $comparison = "IS NULL";
429 # $comparison = "= $1";
432 # if ( $opt{'use_override'} ) {
434 # part_pkg.classnum $comparison AND pkgpart_override IS NULL OR
435 # override.classnum $comparison AND pkgpart_override IS NOT NULL
438 # $where = "part_pkg.classnum $comparison";
442 $agentnum ||= $opt{'agentnum'};
445 " SELECT COALESCE( SUM( cust_bill_pkg_discount.amount ), 0 ) ";
448 # " / CASE COUNT(cust_pkg.*) WHEN 0 THEN 1 ELSE COUNT(cust_pkg.*) END "
449 # if $opt{average_per_cust_pkg};
452 " FROM cust_bill_pkg_discount
453 LEFT JOIN cust_bill_pkg USING ( billpkgnum )
454 LEFT JOIN cust_bill USING ( invnum )
455 LEFT JOIN cust_main USING ( custnum )
456 WHERE ". $self->in_time_period_and_agent($speriod, $eperiod, $agentnum);
457 # LEFT JOIN cust_pkg_discount USING ( pkgdiscountnum )
458 # LEFT JOIN discount USING ( discountnum )
459 # LEFT JOIN cust_pkg USING ( pkgnum )
460 # LEFT JOIN part_pkg USING ( pkgpart )
461 # LEFT JOIN part_pkg AS override ON pkgpart_override = override.pkgpart
463 return $self->scalar_sql($total_sql);
467 sub setup_pkg { shift->pkg_field( @_, 'setup' ); }
468 sub susp_pkg { shift->pkg_field( @_, 'susp' ); }
469 sub cancel_pkg { shift->pkg_field( @_, 'cancel'); }
472 my( $self, $speriod, $eperiod, $agentnum, $field ) = @_;
474 SELECT COUNT(*) FROM cust_pkg
475 LEFT JOIN cust_main USING ( custnum )
476 WHERE ". $self->in_time_period_and_agent( $speriod,
485 #this is going to be harder..
487 # my( $self, $speriod, $eperiod, $agentnum ) = @_;
488 # $self->scalar_sql("
489 # SELECT COUNT(*) FROM h_cust_pkg
494 sub in_time_period_and_agent {
495 my( $self, $speriod, $eperiod, $agentnum ) = splice(@_, 0, 4);
496 my $col = @_ ? shift() : '_date';
498 my $sql = "$col >= $speriod AND $col < $eperiod";
501 $sql .= " AND cust_main.agentnum = $agentnum"
504 #agent virtualization
506 $FS::CurrentUser::CurrentUser->agentnums_sql( 'table'=>'cust_main' );
512 my( $self, $sql ) = ( shift, shift );
513 my $sth = dbh->prepare($sql) or die dbh->errstr;
515 or die "Unexpected error executing statement $sql: ". $sth->errstr;
516 $sth->fetchrow_arrayref->[0] || 0;