add some debugging to voip_sqlradacct
[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 = 1;
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       warn "rating call to +$countrycode $dest" if $DEBUG;
94
95       #find a rate prefix, first look at most specific (4 digits) then 3, etc.,
96       # finally trying the country code only
97       my $rate_prefix = '';
98       for my $len ( reverse(1..4) ) {
99         $rate_prefix = qsearchs('rate_prefix', {
100           'countrycode' => $countrycode,
101           'npa'         => { op=> 'LIKE', value=> substr($dest, 0, $len) }
102         } ) and last;
103       }
104       $rate_prefix ||= qsearchs('rate_prefix', {
105         'countrycode' => $countrycode,
106         'npa'         => '',
107       });
108
109       unless ( $rate_prefix ) {
110         if ( $self->option('ignore_unrateable') ) {
111           warn "  skipping unrateable call to +$countrycode $dest";
112           next;
113         } else {
114           die "Can't find rate for call to +$countrycode $dest\n"
115         }
116       }
117
118       my $regionnum = $rate_prefix->regionnum;
119
120       my $rate_detail = qsearchs('rate_detail', {
121         'ratenum'        => $ratenum,
122         'dest_regionnum' => $regionnum,
123       } );
124
125       warn "  found rate for regionnum $regionnum ".
126            "and rate detail $rate_detail"
127         if $DEBUG;
128
129       ###
130       # find the price and add detail to the invoice
131       ###
132
133       $included_min{$regionnum} = $rate_detail->min_included
134         unless exists $included_min{$regionnum};
135
136       my $granularity = $rate_detail->sec_granularity;
137       my $seconds = $session->{'acctsessiontime'};
138       $seconds += $granularity - ( $seconds % $granularity );
139       my $minutes = sprintf("%.1f", $seconds / 60);
140       $minutes =~ s/\.0$// if $granularity == 60;
141
142       $included_min{$regionnum} -= $minutes;
143
144       my $charge = 0;
145       if ( $included_min{$regionnum} < 0 ) {
146         my $charge_min = 0 - $included_min{$regionnum};
147         $included_min{$regionnum} = 0;
148         $charge = sprintf('%.2f', $rate_detail->min_charge * $charge_min );
149         $charges += $charge;
150       }
151
152       warn "  adding details on charge to invoice: ".
153            join(' - ', 
154              "+$countrycode $dest",
155              $rate_prefix->rate_region->regionname,
156              $minutes.'m',
157              '$'.$charge,
158            )
159         if $DEBUG;
160
161       push @$details, 
162         #[
163         join(' - ', 
164           "+$countrycode $dest",
165           $rate_prefix->rate_region->regionname,
166           $minutes.'m',
167           '$'.$charge,
168         #]
169         )
170       ;
171
172     } # $session
173
174   } # $cust_svc
175
176   $self->option('recur_flat') + $charges;
177
178 }
179
180 sub is_free {
181   0;
182 }
183
184 1;
185