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