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