new CDR detail format to summarize by accountcode, #37808
[freeside.git] / FS / FS / detail_format / sum_duration_accountcode.pm
1 package FS::detail_format::sum_duration_accountcode;
2
3 use strict;
4 use vars qw( $DEBUG );
5 use base qw(FS::detail_format);
6
7 $DEBUG = 0;
8
9 my $me = '[sum_duration_accountcode]';
10
11 sub name { 'Summary, one line per accountcode' };
12
13 sub header_detail {
14   'Account code,Calls,Duration,Price';
15 }
16
17 sub append {
18   my $self = shift;
19   my $codes = ($self->{codes} ||= {});
20   my $acctids = ($self->{acctids} ||= []);
21   foreach my $cdr (@_) {
22     my $accountcode = $cdr->accountcode || 'other';
23
24     my $object = $self->{inbound} ? $cdr->cdr_termination(1) : $cdr;
25     my $subtotal = $codes->{$accountcode}
26                ||= { count => 0, duration => 0, amount => 0.0 };
27     $subtotal->{count}++;
28     $subtotal->{duration} += $object->rated_seconds;
29     $subtotal->{amount} += $object->rated_price
30       if $object->freesidestatus ne 'no-charge';
31
32     push @$acctids, $cdr->acctid;
33   }
34 }
35
36 sub finish {
37   my $self = shift;
38   my $codes = $self->{codes};
39   foreach my $accountcode (sort { $a cmp $b } keys %$codes) {
40
41     warn "processing $accountcode\n" if $DEBUG;
42
43     my $subtotal = $codes->{$accountcode};
44
45     $self->csv->combine(
46       $accountcode,
47       $subtotal->{count},
48       sprintf('%.01f min', $subtotal->{duration}/60),
49       $self->money_char . sprintf('%.02f', $subtotal->{amount})
50     );
51
52     warn "adding detail: ".$self->csv->string."\n" if $DEBUG;
53
54     push @{ $self->{buffer} }, FS::cust_bill_pkg_detail->new({
55         amount      => $subtotal->{amount},
56         format      => 'C',
57         classnum    => '', #ignored in this format
58         duration    => $subtotal->{duration},
59         phonenum    => '', # not divided up per service
60         accountcode => $accountcode,
61         startdate   => '',
62         regionname  => '',
63         detail      => $self->csv->string,
64         acctid      => $self->{acctids},
65     });
66   } #foreach $accountcode
67 }
68
69 1;