1 package FS::detail_format::sum_duration_prefix;
5 use base qw(FS::detail_format);
6 use List::Util qw(sum);
10 my $me = '[sum_duration_prefix]';
12 sub name { 'Summary, one line per destination prefix' };
13 # and also..."rate group"? what do you call the interstate/intrastate rate
17 'Destination NPA-NXX,Interstate Calls,Duration,Intrastate Calls,Duration,Price';
20 my $prefix_length = 6;
21 # possibly should use rate_prefix for this, but interstate/intrastate uses
22 # them in a strange way and we are following along
26 my $prefixes = ($self->{prefixes} ||= {});
27 foreach my $cdr (@_) {
28 my (undef, $phonenum) = $cdr->parse_number(
29 column => ( $self->{inbound} ? 'src' : 'dst' ),
32 $phonenum =~ /^(\d{$prefix_length})/;
33 my $prefix = $1 || 'other';
34 warn "$me appending ".$cdr->dst." to $prefix\n" if $DEBUG;
36 # XXX hardcoded ratenames, not the worst of evils
37 $prefixes->{$prefix} ||= {
38 Interstate => { count => 0, duration => 0, amount => 0 },
39 Intrastate => { count => 0, duration => 0, amount => 0 },
41 my $object = $self->{inbound} ? $cdr->cdr_termination(1) : $cdr;
42 # XXX using $cdr's rated_ratename instead of $object because
43 # cdr_termination doesn't have one...
44 # but interstate-ness should be symmetric, yes? if A places an
45 # interstate call to B, then B receives an interstate call from A.
46 my $subtotal = $prefixes->{$prefix}{$cdr->rated_ratename}
48 # silently skip calls that are neither interstate nor intrastate
49 #or die "unknown rated_ratename '" .$cdr->rated_ratename.
50 # "' in CDR #".$cdr->acctid."\n";
52 $subtotal->{duration} += $object->rated_seconds;
53 $subtotal->{amount} += $object->rated_price
54 if $object->freesidestatus ne 'no-charge';
60 my $prefixes = $self->{prefixes};
61 foreach my $prefix (sort { $a cmp $b } keys %$prefixes) {
63 warn "processing $prefix\n" if $DEBUG;
65 my $ratenames = $prefixes->{$prefix};
66 my @subtotals = ($ratenames->{'Interstate'}, $ratenames->{'Intrastate'});
67 my $total_amount = sum( map { $_->{'amount'} } @subtotals );
68 my $total_duration = sum( map { $_->{'duration'} } @subtotals );
69 $prefix =~ s/(...)(...)/$1 - $2/;
71 next if $total_amount < 0.01;
77 sprintf('%.01f min', $_->{duration}/60),
79 $self->money_char . sprintf('%.02f',$total_amount),
82 warn "adding detail: ".$self->csv->string."\n" if $DEBUG;
84 push @{ $self->{buffer} }, FS::cust_bill_pkg_detail->new({
85 amount => $total_amount,
87 classnum => '', #ignored in this format
88 duration => $total_duration,
89 phonenum => '', # not divided up per service
90 accountcode => '', #ignored in this format
91 startdate => '', #could use the earliest startdate in the bunch?
92 regionname => '', #no, we're using prefix instead
93 detail => $self->csv->string,