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