7364fadbda2de690326e4f85fa8c2fd94a0d5e95
[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       'ignore_unrateable' => { 'name' => 'Ignore calls for which not rate prefix can be found (otherwise they are fatal)',
30                                'type' => 'checkbox',
31                              },
32     },
33     'fieldorder' => [qw( setup_fee recur_flat ratenum ignore_unrateable )],
34     'weight' => 40,
35 );
36
37 sub calc_setup {
38   my($self, $cust_pkg ) = @_;
39   $self->option('setup_fee');
40 }
41
42 sub calc_recur {
43   my($self, $cust_pkg, $sdate, $details ) = @_;
44
45   my $last_bill = $cust_pkg->last_bill;
46
47   my $ratenum = $cust_pkg->part_pkg->option('ratenum');
48
49   my %included_min = ();
50
51   my $charges = 0;
52
53   foreach my $cust_svc (
54     grep { $_->part_svc->svcdb eq 'svc_acct' } $cust_pkg->cust_svc
55   ) {
56
57     foreach my $session (
58       $cust_svc->get_session_history( $last_bill, $$sdate )
59     ) {
60       warn "rating session $session" if $DEBUG;
61
62       ###
63       # look up rate details based on called station id
64       ###
65
66       my $dest = $session->{'calledstationid'};
67
68       #remove non-phone# stuff and whitespace
69       $dest =~ s/\s//g;
70       my $proto = '';
71       $dest =~ s/^(\w+):// and $proto = $1; #sip:
72       my $ip = '';
73       $dest =~ s/\@((\d{1,3}\.){3}\d{1,3})$// and $ip = $1; # @10.54.32.1
74
75       #determine the country code
76       my $countrycode;
77       if ( $dest =~ /^011((\d\d)(\d))(\d+)$/ ) {
78
79         my( $three, $two, $unknown, $rest ) = ( $1, $2, $3, $4 );
80         #first look for 2 digit country code
81         if ( qsearch('rate_prefix', { 'countrycode' => $two } ) ) {
82           $countrycode = $two;
83           $dest = $unknown.$rest;
84         } else { #3 digit country code
85           $countrycode = $three;
86           $dest = $rest;
87         }
88
89       } else {
90         $countrycode = '1';
91       }
92
93       #find a rate prefix, first look at most specific (4 digits) then 3, etc.,
94       # finally trying the country code only
95       my $rate_prefix = '';
96       for my $len ( reverse(1..4) ) {
97         $rate_prefix = qsearchs('rate_prefix', {
98           'countrycode' => $countrycode,
99           'npa'         => { op=> 'LIKE', value=> substr($dest, 0, $len) }
100         } ) and last;
101       }
102       $rate_prefix ||= qsearchs('rate_prefix', {
103         'countrycode' => $countrycode,
104         'npa'         => '',
105       });
106
107       unless ( $rate_prefix ) {
108         if ( $self->option('ignore_unrateable') ) {
109           next;
110         } else {
111           die "Can't find rate for call to +$countrycode $dest\n"
112         }
113       }
114
115       my $regionnum = $rate_prefix->regionnum;
116
117       my $rate_detail = qsearchs('rate_detail', {
118         'ratenum'        => $ratenum,
119         'dest_regionnum' => $regionnum,
120       } );
121
122       ###
123       # find the price and add detail to the invoice
124       ###
125
126       $included_min{$regionnum} = $rate_detail->min_included
127         unless exists $included_min{$regionnum};
128
129       my $granularity = $rate_detail->sec_granularity;
130       my $seconds = $session->{'acctsessiontime'};
131       $seconds += $granularity - ( $seconds % $granularity );
132       my $minutes = sprintf("%.1f", $seconds / 60);
133       $minutes =~ s/\.0$// if $granularity == 60;
134
135       $included_min{$regionnum} -= $minutes;
136
137       my $charge = 0;
138       if ( $included_min{$regionnum} < 0 ) {
139         my $charge_min = 0 - $included_min{$regionnum};
140         $included_min{$regionnum} = 0;
141         $charge = sprintf('%.2f', $rate_detail->min_charge * $charge_min );
142         $charges += $charge;
143       }
144
145       push @$details, 
146         #[
147         join(' - ', 
148           "+$countrycode $dest",
149           $rate_prefix->rate_region->regionname,
150           $minutes.'m',
151           '$'.$charge,
152         #]
153         )
154       ;
155
156     } # $session
157
158   } # $cust_svc
159
160   $self->option('recur_flat') + $charges;
161
162 }
163
164 sub is_free {
165   0;
166 }
167
168 1;
169