1 package FS::part_pkg::voip_sqlradacct;
4 use vars qw(@ISA $DEBUG %info);
5 use FS::Record qw(qsearchs qsearch);
10 @ISA = qw(FS::part_pkg);
15 'name' => 'VoIP rating by plan of CDR records in an SQL RADIUS radacct table',
17 'setup_fee' => { 'name' => 'Setup fee for this package',
20 'recur_flat' => { 'name' => 'Base monthly charge for this package',
23 'ratenum' => { 'name' => 'Rate plan',
25 'select_table' => 'rate',
26 'select_key' => 'ratenum',
27 'select_label' => 'ratename',
29 'ignore_unrateable' => { 'name' => 'Ignore calls for which not rate prefix can be found (otherwise they are fatal)',
33 'fieldorder' => [qw( setup_fee recur_flat ratenum ignore_unrateable )],
38 my($self, $cust_pkg ) = @_;
39 $self->option('setup_fee');
43 my($self, $cust_pkg, $sdate, $details ) = @_;
45 my $last_bill = $cust_pkg->last_bill;
47 my $ratenum = $cust_pkg->part_pkg->option('ratenum');
49 my %included_min = ();
53 foreach my $cust_svc (
54 grep { $_->part_svc->svcdb eq 'svc_acct' } $cust_pkg->cust_svc
58 $cust_svc->get_session_history( $last_bill, $$sdate )
60 warn "rating session $session" if $DEBUG;
63 # look up rate details based on called station id
66 my $dest = $session->{'calledstationid'};
68 #remove non-phone# stuff and whitespace
71 $dest =~ s/^(\w+):// and $proto = $1; #sip:
73 $dest =~ s/\@((\d{1,3}\.){3}\d{1,3})$// and $ip = $1; # @10.54.32.1
75 #determine the country code
77 if ( $dest =~ /^011((\d\d)(\d))(\d+)$/ ) {
79 my( $three, $two, $unknown, $rest ) = ( $1, $2, $3, $4 );
80 #first look for 2 digit country code
81 if ( qsearch('rate_prefix', { 'countrycode' => $two } ) ) {
83 $dest = $unknown.$rest;
84 } else { #3 digit country code
85 $countrycode = $three;
93 warn "rating call to +$countrycode $dest" if $DEBUG;
95 #find a rate prefix, first look at most specific (4 digits) then 3, etc.,
96 # finally trying the country code only
98 for my $len ( reverse(1..4) ) {
99 $rate_prefix = qsearchs('rate_prefix', {
100 'countrycode' => $countrycode,
101 'npa' => { op=> 'LIKE', value=> substr($dest, 0, $len) }
104 $rate_prefix ||= qsearchs('rate_prefix', {
105 'countrycode' => $countrycode,
109 unless ( $rate_prefix ) {
110 if ( $self->option('ignore_unrateable') ) {
111 warn " skipping unrateable call to +$countrycode $dest";
114 die "Can't find rate for call to +$countrycode $dest\n"
118 my $regionnum = $rate_prefix->regionnum;
120 my $rate_detail = qsearchs('rate_detail', {
121 'ratenum' => $ratenum,
122 'dest_regionnum' => $regionnum,
125 warn " found rate for regionnum $regionnum ".
126 "and rate detail $rate_detail"
130 # find the price and add detail to the invoice
133 $included_min{$regionnum} = $rate_detail->min_included
134 unless exists $included_min{$regionnum};
136 my $granularity = $rate_detail->sec_granularity;
137 my $seconds = $session->{'acctsessiontime'};
138 $seconds += $granularity - ( $seconds % $granularity );
139 my $minutes = sprintf("%.1f", $seconds / 60);
140 $minutes =~ s/\.0$// if $granularity == 60;
142 $included_min{$regionnum} -= $minutes;
145 if ( $included_min{$regionnum} < 0 ) {
146 my $charge_min = 0 - $included_min{$regionnum};
147 $included_min{$regionnum} = 0;
148 $charge = sprintf('%.2f', $rate_detail->min_charge * $charge_min );
152 warn " adding details on charge to invoice: ".
154 "+$countrycode $dest",
155 $rate_prefix->rate_region->regionname,
164 "+$countrycode $dest",
165 $rate_prefix->rate_region->regionname,
176 $self->option('recur_flat') + $charges;
185 my($self, $cust_pkg) = @_;
186 $self->option('recur_flat');