summaryrefslogtreecommitdiff
path: root/FS/FS/detail_format/sum_count.pm
blob: 253956f0d6d6ec64ea3db629de1558f27e597c62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package FS::detail_format::sum_count;

use strict;
use vars qw( $DEBUG );
use base qw(FS::detail_format);
use FS::Record qw(qsearchs);
use FS::cust_svc;
use FS::svc_Common; # for label

$DEBUG = 0;

sub name { 'Number of calls, one line per service' };

sub header_detail {
  my $self = shift;
  if ( $self->{inbound} ) {
    'Destination,Messages,Price'
  }
  else {
    'Source,Messages,Price'
  }
}

sub append {
  my $self = shift;
  my $svcnums = ($self->{svcnums} ||= {});
  my $acctids = $self->{acctids} ||= [];
  foreach my $cdr (@_) {
    my $object = $self->{inbound} ? $cdr->cdr_termination(1) : $cdr;
    my $svcnum = $object->svcnum; # yes, $object->svcnum.

    my $subtotal = ($svcnums->{$svcnum} ||=
      { count => 0, duration => 0, amount => 0 });
    $subtotal->{count}++;
    $subtotal->{amount} += $object->rated_price
      if $object->freesidestatus ne 'no-charge';

    push @$acctids, $cdr->acctid;
  }
}

sub finish {
  my $self = shift;
  my $svcnums = $self->{svcnums};
  my $buffer = $self->{buffer};
  foreach my $svcnum (keys %$svcnums) {

    my $subtotal = $svcnums->{$svcnum};
    next if $subtotal->{amount} < 0.01;

    my $cust_svc = qsearchs('cust_svc', { svcnum => $svcnum })
      or die "svcnum #$svcnum not found";
    my $phonenum = $cust_svc->svc_x->label;
    warn "processing $phonenum\n" if $DEBUG;

    $self->csv->combine(
      $phonenum,
      $subtotal->{count},
      $self->money_char . sprintf('%.02f',$subtotal->{amount}),
    );

    warn "adding detail: ".$self->csv->string."\n" if $DEBUG;

    push @$buffer, FS::cust_bill_pkg_detail->new({
        amount      => $subtotal->{amount},
        format      => 'C',
        classnum    => '', #ignored in this format
        duration    => '',
        phonenum    => $phonenum,
        accountcode => '', #ignored in this format
        startdate   => '', #could use the earliest startdate in the bunch?
        regionname  => '', #no, we're using prefix instead
        detail      => $self->csv->string,
        acctid      => $self->{acctids},
    });
  } #foreach $svcnum

  # supposedly the compiler is smart enough to do this in place
  @$buffer = sort { $a->{Hash}->{phonenum} cmp $b->{Hash}->{phonenum} } 
              @$buffer;
}

1;