1 package FS::part_pkg::voip_sqlradacct;
4 use vars qw(@ISA $DEBUG %info);
6 use FS::Record qw(qsearchs qsearch);
7 use FS::part_pkg::flat;
11 @ISA = qw(FS::part_pkg::flat);
16 'name' => 'VoIP rating by plan of CDR records in an SQL RADIUS radacct table',
18 'setup_fee' => { 'name' => 'Setup fee for this package',
21 'recur_flat' => { 'name' => 'Base monthly charge for this package',
24 'unused_credit' => { 'name' => 'Credit the customer for the unused portion'.
25 ' of service at cancellation',
28 'ratenum' => { 'name' => 'Rate plan',
30 'select_table' => 'rate',
31 'select_key' => 'ratenum',
32 'select_label' => 'ratename',
35 'fieldorder' => [qw( setup_fee recur_flat unused_credit ratenum ignore_unrateable )],
40 my($self, $cust_pkg ) = @_;
41 $self->option('setup_fee');
45 my($self, $cust_pkg, $sdate, $details ) = @_;
47 my $last_bill = $cust_pkg->last_bill;
49 my $ratenum = $cust_pkg->part_pkg->option('ratenum');
51 my %included_min = ();
55 foreach my $cust_svc (
56 grep { $_->part_svc->svcdb eq 'svc_acct' } $cust_pkg->cust_svc
60 $cust_svc->get_session_history( $last_bill, $$sdate )
63 warn "rating session $session\n".
64 join('', map { " $_ => ". $session->{$_}. "\n" } keys %$session );
68 # look up rate details based on called station id
71 my $dest = $session->{'calledstationid'};
73 #remove non-phone# stuff and whitespace
76 $dest =~ s/^(\w+):// and $proto = $1; #sip:
78 $dest =~ s/\@(.*)$// and $siphost = $1; # @10.54.32.1, @sip.example.com
80 #determine the country code
82 if ( $dest =~ /^011(((\d)(\d))(\d))(\d+)$/ ) {
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 } ) ) {
88 $dest = $u1.$u2.$rest;
89 } elsif ( qsearch('rate_prefix', { 'countrycode' => $two } ) ) { #or 2
92 } else { #3 digit country code
93 $countrycode = $three;
99 $dest =~ s/^1//;# if length($dest) > 10;
102 warn "rating call to +$countrycode $dest\n" if $DEBUG;
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),
114 $rate_prefix ||= qsearchs('rate_prefix', {
115 'countrycode' => $countrycode,
119 die "Can't find rate for call to +$countrycode $dest\n"
122 my $regionnum = $rate_prefix->regionnum;
123 my $rate_detail = qsearchs('rate_detail', {
124 'ratenum' => $ratenum,
125 'dest_regionnum' => $regionnum,
128 warn " found rate for regionnum $regionnum ".
129 "and rate detail $rate_detail\n"
133 # find the price and add detail to the invoice
136 $included_min{$regionnum} = $rate_detail->min_included
137 unless exists $included_min{$regionnum};
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;
145 $included_min{$regionnum} -= $minutes;
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 );
155 my $rate_region = $rate_prefix->rate_region;
156 warn " (rate region $rate_region)\n" if $DEBUG;
159 #time2str("%Y %b %d - %r", $session->{'acctstarttime'}),
160 time2str("%c", $session->{'acctstarttime'}),
163 "+$countrycode $dest",
164 $rate_region->regionname,
167 warn " adding details on charge to invoice: ".
168 join(' - ', @call_details )
171 push @$details, join(' - ', @call_details); #\@call_details,
177 $self->option('recur_flat') + $charges;
186 my($self, $cust_pkg) = @_;
187 $self->option('recur_flat');