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