fix up some bugs in VoIP rating
[freeside.git] / FS / FS / part_pkg / voip_sqlradacct.pm
1 package FS::part_pkg::voip_sqlradacct;
2
3 use strict;
4 use vars qw(@ISA $DEBUG %info);
5 use FS::Record qw(qsearchs qsearch);
6 use FS::part_pkg;
7 #use FS::rate;
8 use FS::rate_prefix;
9
10 @ISA = qw(FS::part_pkg);
11
12 $DEBUG = 0;
13
14 %info = (
15     'name' => 'VoIP rating by plan of CDR records in an SQL RADIUS radacct table',
16     'fields' => {
17       'setup_fee' => { 'name' => 'Setup fee for this package',
18                        'default' => 0,
19                      },
20       'recur_flat' => { 'name' => 'Base monthly charge for this package',
21                         'default' => 0,
22                       },
23       'ratenum'   => { 'name' => 'Rate plan',
24                        'type' => 'select',
25                        'select_table' => 'rate',
26                        'select_key'   => 'ratenum',
27                        'select_label' => 'ratename',
28                      },
29     },
30     'fieldorder' => [qw( setup_fee recur_flat ratenum )],
31     'weight' => 40,
32 );
33
34 sub calc_setup {
35   my($self, $cust_pkg ) = @_;
36   $self->option('setup_fee');
37 }
38
39 sub calc_recur {
40   my($self, $cust_pkg, $sdate, $details ) = @_;
41
42   my $last_bill = $cust_pkg->last_bill;
43
44   my $ratenum = $cust_pkg->part_pkg->option('ratenum');
45
46   my %included_min = ();
47
48   my $charges = 0;
49
50   foreach my $cust_svc (
51     grep { $_->part_svc->svcdb eq 'svc_acct' } $cust_pkg->cust_svc
52   ) {
53
54     foreach my $session (
55       $cust_svc->get_session_history( $last_bill, $$sdate )
56     ) {
57       warn "rating session $session" if $DEBUG;
58
59       ###
60       # look up rate details based on called station id
61       ###
62
63       my $dest = $session->{'calledstationid'};
64
65       #remove non-phone# stuff and whitespace
66       $dest =~ s/\s//g;
67       my $proto = '';
68       $dest =~ s/^(\w+):// and $proto = $1; #sip:
69       my $ip = '';
70       $dest =~ s/\@((\d{1,3}\.){3}\d{1,3})$// and $ip = $1; # @10.54.32.1
71
72       #determine the country code
73       my $countrycode;
74       if ( $dest =~ /^011((\d\d)(\d))(\d+)$/ ) {
75
76         my( $three, $two, $unknown, $rest ) = ( $1, $2, $3, $4 );
77         #first look for 2 digit country code
78         if ( qsearch('rate_prefix', { 'countrycode' => $two } ) ) {
79           $countrycode = $two;
80           $dest = $unknown.$rest;
81         } else { #3 digit country code
82           $countrycode = $three;
83           $dest = $rest;
84         }
85
86       } else {
87         $countrycode = '1';
88       }
89
90       #find a rate prefix, first look at most specific (4 digits) then 3, etc.,
91       # finally trying the country code only
92       my $rate_prefix = '';
93       for my $len ( reverse(1..4) ) {
94         $rate_prefix = qsearchs('rate_prefix', {
95           'countrycode' => $countrycode,
96           'npa'         => { op=> 'LIKE', value=> substr($dest, 0, $len) }
97         } ) and last;
98       }
99       $rate_prefix ||= qsearchs('rate_prefix', {
100         'countrycode' => $countrycode,
101         'npa'         => '',
102       });
103       die "Can't find rate for call to countrycode $countrycode number $dest\n"
104         unless $rate_prefix;
105
106       my $regionnum = $rate_prefix->regionnum;
107
108       my $rate_detail = qsearchs('rate_detail', {
109         'ratenum'        => $ratenum,
110         'dest_regionnum' => $regionnum,
111       } );
112
113       ###
114       # find the price and add detail to the invoice
115       ###
116
117       $included_min{$regionnum} = $rate_detail->min_included
118         unless exists $included_min{$regionnum};
119
120       my $granularity = $rate_detail->sec_granularity;
121       my $seconds = $session->{'acctsessiontime'};
122       $seconds += $granularity - ( $seconds % $granularity );
123       my $minutes = sprintf("%.1f", $seconds / 60);
124       $minutes =~ s/\.0$// if $granularity == 60;
125
126       $included_min{$regionnum} -= $minutes;
127
128       my $charge = 0;
129       if ( $included_min{$regionnum} < 0 ) {
130         my $charge_min = 0 - $included_min{$regionnum};
131         $included_min{$regionnum} = 0;
132         $charge = sprintf('%.2f', $rate_detail->min_charge * $charge_min );
133         $charges += $charge;
134       }
135
136       push @$details, 
137         #[
138         join(' - ', 
139           "+$countrycode $dest",
140           $rate_prefix->rate_region->regionname,
141           $minutes.'m',
142           '$'.$charge,
143         #]
144         )
145       ;
146
147     } # $session
148
149   } # $cust_svc
150
151   $self->option('recur_flat') + $charges;
152
153 }
154
155 sub is_free {
156   0;
157 }
158
159 1;
160