summaryrefslogtreecommitdiff
path: root/FS/FS/part_pkg/voip_sqlradacct.pm
blob: bf18003ab6eeb3a6341d716670acbbac12910699 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package FS::part_pkg::voip_sqlradacct;

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

@ISA = qw(FS::part_pkg::flat);

$DEBUG = 1;

%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 recurring fee for this package',
                          'default' => 0,
                        },
    'unused_credit' => { 'name' => 'Credit the customer for the unused portion'.
                                   ' of service at cancellation',
                         'type' => 'checkbox',
                       },
    'ratenum'   => { 'name' => 'Rate plan',
                     'type' => 'select',
                     'select_table' => 'rate',
                     'select_key'   => 'ratenum',
                     'select_label' => 'ratename',
                   },
  },
  'fieldorder' => [qw( setup_fee recur_flat unused_credit ratenum ignore_unrateable )],
  'weight' => 40,
);

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

#false laziness w/voip_cdr... resolve it if this one ever gets used again
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 )
    ) {
      if ( $DEBUG > 1 ) {
        warn "rating session $session\n".
             join('', map { "  $_ => ". $session->{$_}. "\n" } keys %$session );
      }

      ###
      # 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 $siphost = '';
      $dest =~ s/\@(.*)$// and $siphost = $1; # @10.54.32.1, @sip.example.com

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

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

      } else {
        $countrycode = '1';
        $dest =~ s/^1//;# if length($dest) > 10;
      }

      warn "rating call to +$countrycode $dest\n" if $DEBUG;

      #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..6) ) {
        $rate_prefix = qsearchs('rate_prefix', {
          'countrycode' => $countrycode,
          #'npa'         => { op=> 'LIKE', value=> substr($dest, 0, $len) }
          'npa'         => substr($dest, 0, $len),
        } ) and last;
      }
      $rate_prefix ||= qsearchs('rate_prefix', {
        'countrycode' => $countrycode,
        'npa'         => '',
      });

      die "Can't find rate for call to +$countrycode $dest\n"
        unless $rate_prefix;

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

      warn "  found rate for regionnum $regionnum ".
           "and rate detail $rate_detail\n"
        if $DEBUG;

      ###
      # 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;
      }

      my $rate_region = $rate_prefix->rate_region;
      warn "  (rate region $rate_region)\n" if $DEBUG;

      my @call_details = (
        #time2str("%Y %b %d - %r", $session->{'acctstarttime'}),
        time2str("%c", $session->{'acctstarttime'}),
        $minutes.'m',
        '$'.$charge,
        "+$countrycode $dest",
        $rate_region->regionname,
      );

      warn "  adding details on charge to invoice: ".
           join(' - ', @call_details )
        if $DEBUG;

      push @$details, join(' - ', @call_details); #\@call_details,

    } # $session

  } # $cust_svc

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

}

sub is_free {
  0;
}

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

1;