communigate provisioning phase 2: add svc_domain.trailer -> communigate TrailerText...
[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   'fields' => {
20     'setup_fee'     => { 'name' => 'Setup fee for this package',
21                          'default' => 0,
22                        },
23     'recur_fee'     => { 'name' => 'Base recurring fee for this package',
24                          'default' => 0,
25                        },
26     'unused_credit' => { 'name' => 'Credit the customer for the unused portion'.
27                                    ' of service at cancellation',
28                          'type' => 'checkbox',
29                        },
30     'ratenum'   => { 'name' => 'Rate plan',
31                      'type' => 'select',
32                      'select_table' => 'rate',
33                      'select_key'   => 'ratenum',
34                      'select_label' => 'ratename',
35                    },
36   },
37   'fieldorder' => [qw( setup_fee recur_fee unused_credit ratenum ignore_unrateable )],
38   'weight' => 40,
39 );
40
41 sub calc_setup {
42   my($self, $cust_pkg ) = @_;
43   $self->option('setup_fee');
44 }
45
46 #false laziness w/voip_cdr... resolve it if this one ever gets used again
47 sub calc_recur {
48   my($self, $cust_pkg, $sdate, $details ) = @_;
49
50   my $last_bill = $cust_pkg->last_bill;
51
52   my $ratenum = $cust_pkg->part_pkg->option('ratenum');
53
54   my %included_min = ();
55
56   my $charges = 0;
57
58   foreach my $cust_svc (
59     grep { $_->part_svc->svcdb eq 'svc_acct' } $cust_pkg->cust_svc
60   ) {
61
62     foreach my $session (
63       $cust_svc->get_session_history( $last_bill, $$sdate )
64     ) {
65       if ( $DEBUG > 1 ) {
66         warn "rating session $session\n".
67              join('', map { "  $_ => ". $session->{$_}. "\n" } keys %$session );
68       }
69
70       ###
71       # look up rate details based on called station id
72       ###
73
74       my $dest = $session->{'calledstationid'};
75
76       #remove non-phone# stuff and whitespace
77       $dest =~ s/\s//g;
78       my $proto = '';
79       $dest =~ s/^(\w+):// and $proto = $1; #sip:
80       my $siphost = '';
81       $dest =~ s/\@(.*)$// and $siphost = $1; # @10.54.32.1, @sip.example.com
82
83       #determine the country code
84       my $countrycode;
85       if ( $dest =~ /^011(((\d)(\d))(\d))(\d+)$/ ) {
86
87         my( $three, $two, $one, $u1, $u2, $rest ) = ( $1, $2, $3, $4, $5, $6 );
88         #first look for 1 digit country code
89         if ( qsearch('rate_prefix', { 'countrycode' => $one } ) ) {
90           $countrycode = $one;
91           $dest = $u1.$u2.$rest;
92         } elsif ( qsearch('rate_prefix', { 'countrycode' => $two } ) ) { #or 2
93           $countrycode = $two;
94           $dest = $u2.$rest;
95         } else { #3 digit country code
96           $countrycode = $three;
97           $dest = $rest;
98         }
99
100       } else {
101         $countrycode = '1';
102         $dest =~ s/^1//;# if length($dest) > 10;
103       }
104
105       warn "rating call to +$countrycode $dest\n" if $DEBUG;
106
107       #find a rate prefix, first look at most specific (4 digits) then 3, etc.,
108       # finally trying the country code only
109       my $rate_prefix = '';
110       for my $len ( reverse(1..6) ) {
111         $rate_prefix = qsearchs('rate_prefix', {
112           'countrycode' => $countrycode,
113           #'npa'         => { op=> 'LIKE', value=> substr($dest, 0, $len) }
114           'npa'         => substr($dest, 0, $len),
115         } ) and last;
116       }
117       $rate_prefix ||= qsearchs('rate_prefix', {
118         'countrycode' => $countrycode,
119         'npa'         => '',
120       });
121
122       die "Can't find rate for call to +$countrycode $dest\n"
123         unless $rate_prefix;
124
125       my $regionnum = $rate_prefix->regionnum;
126       my $rate_detail = qsearchs('rate_detail', {
127         'ratenum'        => $ratenum,
128         'dest_regionnum' => $regionnum,
129       } );
130
131       warn "  found rate for regionnum $regionnum ".
132            "and rate detail $rate_detail\n"
133         if $DEBUG;
134
135       ###
136       # find the price and add detail to the invoice
137       ###
138
139       $included_min{$regionnum} = $rate_detail->min_included
140         unless exists $included_min{$regionnum};
141
142       my $granularity = $rate_detail->sec_granularity;
143       my $seconds = $session->{'acctsessiontime'};
144       $seconds += $granularity - ( $seconds % $granularity );
145       my $minutes = sprintf("%.1f", $seconds / 60);
146       $minutes =~ s/\.0$// if $granularity == 60;
147
148       $included_min{$regionnum} -= $minutes;
149
150       my $charge = 0;
151       if ( $included_min{$regionnum} < 0 ) {
152         my $charge_min = 0 - $included_min{$regionnum};
153         $included_min{$regionnum} = 0;
154         $charge = sprintf('%.2f', $rate_detail->min_charge * $charge_min );
155         $charges += $charge;
156       }
157
158       my $rate_region = $rate_prefix->rate_region;
159       warn "  (rate region $rate_region)\n" if $DEBUG;
160
161       my @call_details = (
162         #time2str("%Y %b %d - %r", $session->{'acctstarttime'}),
163         time2str("%c", $session->{'acctstarttime'}),
164         $minutes.'m',
165         '$'.$charge,
166         "+$countrycode $dest",
167         $rate_region->regionname,
168       );
169
170       warn "  adding details on charge to invoice: ".
171            join(' - ', @call_details )
172         if $DEBUG;
173
174       push @$details, join(' - ', @call_details); #\@call_details,
175
176     } # $session
177
178   } # $cust_svc
179
180   $self->option('recur_fee') + $charges;
181
182 }
183
184 sub can_discount { 0; }
185
186 sub is_free { 0; }
187
188 sub base_recur {
189   my($self, $cust_pkg) = @_;
190   $self->option('recur_fee');
191 }
192
193 1;
194