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