summaryrefslogtreecommitdiff
path: root/FS/FS/part_pkg/voip_sqlradacct.pm
blob: 3989cdba60db66ba9d030cdac661a32ec0eff412 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package FS::part_pkg::voip_sqlradacct;

use strict;
use vars qw(@ISA %info);
use FS::Record qw(qsearchs qsearch);
use FS::part_pkg;
#use FS::rate;
use FS::rate_prefix;

@ISA = qw(FS::part_pkg);

%info = (
    'name' => 'VoIP rating by plan of CDR records in an SQL RADIUS radacct table',
    'fields' => {
      'setup_fee' => { 'name' => 'Setup fee for this package',
                       'default' => 0,
                     },
      'recur_flat' => { 'name' => 'Base monthly charge for this package',
                        'default' => 0,
                      },
      'ratenum'   => { 'name' => 'Rate plan',
                       'type' => 'select',
                       'select_table' => 'rate',
                       'select_key'   => 'ratenum',
                       'select_label' => 'ratename',
                     },
    },
    'fieldorder' => [qw( setup_fee recur_flat ratenum )],
    'weight' => 40,
);

sub calc_setup {
  my($self, $cust_pkg ) = @_;
  $self->option('setup_fee');
}

sub calc_recur {
  my($self, $cust_pkg, $sdate, $details ) = @_;

  my $last_bill = $cust_pkg->last_bill;

  my $ratenum = $cust_pkg->part_pkg->option('ratenum');

  my %included_min = ();

  my $charges = 0;

  foreach my $cust_svc (
    grep { $_->part_svc->svcdb eq 'svc_acct' } $cust_pkg->cust_svc
  ) {

    foreach my $session (
      $cust_svc->get_session_history( $last_bill, $$sdate )
    ) {

      ###
      # look up rate details based on called station id
      ###

      my $dest = $session->{'calledstationid'};

      #remove non-phone# stuff and whitespace
      $dest =~ s/\s//g;
      my $proto = '';
      $dest =~ s/^(\w+):// and $proto = $1; #sip:
      my $ip = '';
      $dest =~ s/\@((\d{1,3}\.){3}\d{1,3})$// and $ip = $1; # @10.54.32.1

      #determine the country code
      my $countrycode;
      if ( $dest =~ /^011((\d\d)(\d))(\d+)$/ ) {

        my( $three, $two, $unknown, $rest ) = ( $1, $2, $3, $4 );
        #first look for 2 digit country code
        if ( qsearch('rate_prefix', { 'countrycode' => $two } ) ) {
          $countrycode = $two;
          $dest = $unknown.$rest;
        } else { #3 digit country code
          $countrycode = $three;
          $dest = $rest;
        }

      } else {
        $countrycode = '1';
      }

      #find a rate prefix, first look at most specific (4 digits) then 3, etc.,
      # finally trying the country code only
      my $rate_prefix = '';
      for my $len ( reverse(1..4) ) {
        $rate_prefix = qsearchs('rate_prefix', {
          'countrycode' => $countrycode,
          'npa'         => { op=> 'LIKE', value=> substr($dest, 0, $len) }
        } ) and last;
      }
      $rate_prefix ||= qsearchs('rate_prefix', {
        'countrycode' => $countrycode,
        'npa'         => '',
      });
      die "Can't find rate for call to countrycode $countrycode number $dest\n"
        unless $rate_prefix;

      my $regionnum = $rate_prefix->regionnum;

      my $rate_detail = qsearchs('rate_detail', {
        'ratenum'        => $ratenum,
        'dest_regionnum' => $regionnum,
      } );

      ###
      # find the price and add detail to the invoice
      ###

      $included_min{$regionnum} = $rate_detail->min_included
        unless exists $included_min{$regionnum};

      my $granularity = $rate_detail->sec_granularity;
      my $seconds = $session->{'acctsessiontime'};
      $seconds += $granularity - ( $seconds % $granularity );
      my $minutes = sprintf("%.1f", $seconds / 60);
      $minutes =~ s/\.0$// if $granularity == 60;

      $included_min{$regionnum} -= $minutes;

      my $charge = 0;
      if ( $included_min{$regionnum} < 0 ) {
        my $charge_min = 0 - $included_min{$regionnum};
        $included_min{$regionnum} = 0;
        $charge = sprintf('%.2f', $rate_detail->min_charge * $charge_min );
        $charges += $charge;
      }

      push @$details, 
        #[
        join(' - ', 
          "+$countrycode $dest",
          $rate_prefix->rate_region->regionname,
          $minutes.'m',
          '$'.$charge,
        #]
        )
      ;

    } # $session

  } # $cust_svc

  $self->option('recur_flat') + $charges;

}

sub is_free {
  0;
}

1;