"use base" for compatibility
[freeside.git] / FS / FS / detail_format / sum_count.pm
1 package FS::detail_format::sum_count;
2
3 use strict;
4 use vars qw( $DEBUG );
5 use base qw(FS::detail_format);
6 use FS::Record qw(qsearchs);
7 use FS::cust_svc;
8 use FS::svc_Common; # for label
9
10 $DEBUG = 0;
11
12 sub name { 'Number of calls, one line per service' };
13
14 sub header_detail {
15   my $self = shift;
16   if ( $self->{inbound} ) {
17     'Destination,Messages,Price'
18   }
19   else {
20     'Source,Messages,Price'
21   }
22 }
23
24 sub append {
25   my $self = shift;
26   my $svcnums = ($self->{svcnums} ||= {});
27   foreach my $cdr (@_) {
28     my $object = $self->{inbound} ? $cdr->cdr_termination(1) : $cdr;
29     my $svcnum = $object->svcnum; # yes, $object->svcnum.
30
31     my $subtotal = ($svcnums->{$svcnum} ||=
32       { count => 0, duration => 0, amount => 0 });
33     $subtotal->{count}++;
34     $subtotal->{amount} += $object->rated_price;
35   }
36 }
37
38 sub finish {
39   my $self = shift;
40   my $svcnums = $self->{svcnums};
41   my $buffer = $self->{buffer};
42   foreach my $svcnum (keys %$svcnums) {
43
44     my $subtotal = $svcnums->{$svcnum};
45     next if $subtotal->{amount} < 0.01;
46
47     my $cust_svc = qsearchs('cust_svc', { svcnum => $svcnum })
48       or die "svcnum #$svcnum not found";
49     my $phonenum = $cust_svc->svc_x->label;
50     warn "processing $phonenum\n" if $DEBUG;
51
52     $self->csv->combine(
53       $phonenum,
54       $subtotal->{count},
55       $self->money_char . sprintf('%.02f',$subtotal->{amount}),
56     );
57
58     warn "adding detail: ".$self->csv->string."\n" if $DEBUG;
59
60     push @$buffer, FS::cust_bill_pkg_detail->new({
61         amount      => $subtotal->{amount},
62         format      => 'C',
63         classnum    => '', #ignored in this format
64         duration    => '',
65         phonenum    => $phonenum,
66         accountcode => '', #ignored in this format
67         startdate   => '', #could use the earliest startdate in the bunch?
68         regionname  => '', #no, we're using prefix instead
69         detail      => $self->csv->string,
70     });
71   } #foreach $svcnum
72
73   # supposedly the compiler is smart enough to do this in place
74   @$buffer = sort { $a->{Hash}->{phonenum} cmp $b->{Hash}->{phonenum} } 
75               @$buffer;
76 }
77
78 1;