import torrus 1.0.9
[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 Date::Format;
6 use FS::Record qw(qsearchs qsearch);
7 use FS::part_pkg::flat;
8 #use FS::rate;
9 use FS::rate_prefix;
10
11 @ISA = qw(FS::part_pkg::flat);
12
13 $DEBUG = 1;
14
15 %info = (
16   'disabled' => 1, #they're sucked into our CDR table now instead
17   'name' => 'VoIP rating by plan of CDR records in an SQL RADIUS radacct table',
18   'shortname' => 'VoIP/telco CDR rating (external RADIUS)',
19   'inherit_fields' => [ 'global_Mixin' ],
20   'fields' => {
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( ratenum ignore_unrateable )],
29   'weight' => 40,
30 );
31
32 sub calc_setup {
33   my($self, $cust_pkg ) = @_;
34   $self->option('setup_fee');
35 }
36
37 #false laziness w/voip_cdr... resolve it if this one ever gets used again
38 sub calc_recur {
39   my($self, $cust_pkg, $sdate, $details ) = @_;
40
41   my $last_bill = $cust_pkg->last_bill;
42
43   my $ratenum = $cust_pkg->part_pkg->option('ratenum');
44
45   my %included_min = ();
46
47   my $charges = 0;
48
49   foreach my $cust_svc (
50     grep { $_->part_svc->svcdb eq 'svc_acct' } $cust_pkg->cust_svc
51   ) {
52
53     foreach my $session (
54       $cust_svc->get_session_history( $last_bill, $$sdate )
55     ) {
56       if ( $DEBUG > 1 ) {
57         warn "rating session $session\n".
58              join('', map { "  $_ => ". $session->{$_}. "\n" } keys %$session );
59       }
60
61       ###
62       # look up rate details based on called station id
63       ###
64
65       my $dest = $session->{'calledstationid'};
66
67       #remove non-phone# stuff and whitespace
68       $dest =~ s/\s//g;
69       my $proto = '';
70       $dest =~ s/^(\w+):// and $proto = $1; #sip:
71       my $siphost = '';
72       $dest =~ s/\@(.*)$// and $siphost = $1; # @10.54.32.1, @sip.example.com
73
74       #determine the country code
75       my $countrycode;
76       if ( $dest =~ /^011(((\d)(\d))(\d))(\d+)$/ ) {
77
78         my( $three, $two, $one, $u1, $u2, $rest ) = ( $1, $2, $3, $4, $5, $6 );
79         #first look for 1 digit country code
80         if ( qsearch('rate_prefix', { 'countrycode' => $one } ) ) {
81           $countrycode = $one;
82           $dest = $u1.$u2.$rest;
83         } elsif ( qsearch('rate_prefix', { 'countrycode' => $two } ) ) { #or 2
84           $countrycode = $two;
85           $dest = $u2.$rest;
86         } else { #3 digit country code
87           $countrycode = $three;
88           $dest = $rest;
89         }
90
91       } else {
92         $countrycode = '1';
93         $dest =~ s/^1//;# if length($dest) > 10;
94       }
95
96       warn "rating call to +$countrycode $dest\n" if $DEBUG;
97
98       #find a rate prefix, first look at most specific (4 digits) then 3, etc.,
99       # finally trying the country code only
100       my $rate_prefix = '';
101       for my $len ( reverse(1..6) ) {
102         $rate_prefix = qsearchs('rate_prefix', {
103           'countrycode' => $countrycode,
104           #'npa'         => { op=> 'LIKE', value=> substr($dest, 0, $len) }
105           'npa'         => substr($dest, 0, $len),
106         } ) and last;
107       }
108       $rate_prefix ||= qsearchs('rate_prefix', {
109         'countrycode' => $countrycode,
110         'npa'         => '',
111       });
112
113       die "Can't find rate for call to +$countrycode $dest\n"
114         unless $rate_prefix;
115
116       my $regionnum = $rate_prefix->regionnum;
117       my $rate_detail = qsearchs('rate_detail', {
118         'ratenum'        => $ratenum,
119         'dest_regionnum' => $regionnum,
120       } );
121
122       warn "  found rate for regionnum $regionnum ".
123            "and rate detail $rate_detail\n"
124         if $DEBUG;
125
126       ###
127       # find the price and add detail to the invoice
128       ###
129
130       $included_min{$regionnum} = $rate_detail->min_included
131         unless exists $included_min{$regionnum};
132
133       my $granularity = $rate_detail->sec_granularity;
134       my $seconds = $session->{'acctsessiontime'};
135       $seconds += $granularity - ( $seconds % $granularity );
136       my $minutes = sprintf("%.1f", $seconds / 60);
137       $minutes =~ s/\.0$// if $granularity == 60;
138
139       $included_min{$regionnum} -= $minutes;
140
141       my $charge = 0;
142       if ( $included_min{$regionnum} < 0 ) {
143         my $charge_min = 0 - $included_min{$regionnum};
144         $included_min{$regionnum} = 0;
145         $charge = sprintf('%.2f', $rate_detail->min_charge * $charge_min );
146         $charges += $charge;
147       }
148
149       my $rate_region = $rate_prefix->rate_region;
150       warn "  (rate region $rate_region)\n" if $DEBUG;
151
152       my @call_details = (
153         #time2str("%Y %b %d - %r", $session->{'acctstarttime'}),
154         time2str("%c", $session->{'acctstarttime'}),
155         $minutes.'m',
156         '$'.$charge,
157         "+$countrycode $dest",
158         $rate_region->regionname,
159       );
160
161       warn "  adding details on charge to invoice: ".
162            join(' - ', @call_details )
163         if $DEBUG;
164
165       push @$details, join(' - ', @call_details); #\@call_details,
166
167     } # $session
168
169   } # $cust_svc
170
171   $self->option('recur_fee') + $charges;
172
173 }
174
175 sub can_discount { 0; }
176
177 sub is_free { 0; }
178
179 sub base_recur {
180   my($self, $cust_pkg) = @_;
181   $self->option('recur_fee');
182 }
183
184 1;
185