1 package FS::part_pkg::voip_cdr;
4 use vars qw(@ISA $DEBUG %info);
8 use FS::Record qw(qsearchs qsearch);
9 use FS::part_pkg::flat;
13 @ISA = qw(FS::part_pkg::flat);
17 tie my %rating_method, 'Tie::IxHash',
18 'prefix' => 'Rate calls by using destination prefix to look up a region and rate according to the internal prefix and rate tables',
19 'upstream' => 'Rate calls based on upstream data: If the call type is "1", map the upstream rate ID directly to an internal rate (rate_detail), otherwise, pass the upstream price through directly.',
22 #tie my %cdr_location, 'Tie::IxHash',
23 # 'internal' => 'Internal: CDR records imported into the internal CDR table',
24 # 'external' => 'External: CDR records queried directly from an external '.
25 # 'Asterisk (or other?) CDR table',
29 'name' => 'VoIP rating by plan of CDR records in an internal (or external?) SQL table',
31 'setup_fee' => { 'name' => 'Setup fee for this package',
34 'recur_flat' => { 'name' => 'Base recurring fee for this package',
37 'unused_credit' => { 'name' => 'Credit the customer for the unused portion'.
38 ' of service at cancellation',
41 'ratenum' => { 'name' => 'Rate plan',
43 'select_table' => 'rate',
44 'select_key' => 'ratenum',
45 'select_label' => 'ratename',
47 'rating_method' => { 'name' => 'Region rating method',
49 'select_options' => \%rating_method,
52 'default_prefix' => { 'name' => 'Default prefix optionally prepended to customer DID numbers when searching for CDR records',
56 #XXX also have option for an external db??
57 # 'cdr_location' => { 'name' => 'CDR database location'
59 # 'select_options' => \%cdr_location,
60 # 'select_callback' => {
62 # 'enable' => [ 'datasrc', 'username', 'password' ],
65 # 'disable' => [ 'datasrc', 'username', 'password' ],
69 # 'datasrc' => { 'name' => 'DBI data source for external CDR table',
72 # 'username' => { 'name' => 'External database username',
75 # 'password' => { 'name' => 'External database password',
80 'fieldorder' => [qw( setup_fee recur_flat unused_credit ratenum rating_method default_prefix )],
85 my($self, $cust_pkg ) = @_;
86 $self->option('setup_fee');
89 #false laziness w/voip_sqlradacct... resolve it if that one ever gets used again
91 my($self, $cust_pkg, $sdate, $details, $param ) = @_;
93 my $last_bill = $cust_pkg->last_bill;
95 my $ratenum = $cust_pkg->part_pkg->option('ratenum');
97 my $spool_cdr = $cust_pkg->cust_main->spool_cdr;
99 my %included_min = ();
103 my $downstream_cdr = '';
105 foreach my $cust_svc (
106 grep { $_->part_svc->svcdb eq 'svc_phone' } $cust_pkg->cust_svc
110 $cust_svc->get_cdrs_for_update() # $last_bill, $$sdate )
113 warn "rating CDR $cdr\n".
114 join('', map { " $_ => ". $cdr->{$_}. "\n" } keys %$cdr );
118 my( $rate_region, $regionnum );
121 my @call_details = ();
122 if ( $self->option('rating_method') eq 'prefix'
123 || ! $self->option('rating_method')
128 # look up rate details based on called station id
129 # (or calling station id for toll free calls)
132 my( $to_or_from, $number );
133 if ( $cdr->dst =~ /^(\+?1)?8[02-8]{2}/ ) { #tollfree call
134 $to_or_from = 'from';
136 } else { #regular call
141 #remove non-phone# stuff and whitespace
144 # $dest =~ s/^(\w+):// and $proto = $1; #sip:
146 # $dest =~ s/\@(.*)$// and $siphost = $1; # @10.54.32.1, @sip.example.com
148 #determine the country code
150 if ( $number =~ /^011(((\d)(\d))(\d))(\d+)$/
151 || $number =~ /^\+(((\d)(\d))(\d))(\d+)$/
155 my( $three, $two, $one, $u1, $u2, $rest ) = ( $1,$2,$3,$4,$5,$6 );
156 #first look for 1 digit country code
157 if ( qsearch('rate_prefix', { 'countrycode' => $one } ) ) {
159 $number = $u1.$u2.$rest;
160 } elsif ( qsearch('rate_prefix', { 'countrycode' => $two } ) ) { #or 2
163 } else { #3 digit country code
164 $countrycode = $three;
170 $number =~ s/^1//;# if length($number) > 10;
173 warn "rating call $to_or_from +$countrycode $number\n" if $DEBUG;
174 $pretty_destnum = "+$countrycode $number";
176 #find a rate prefix, first look at most specific (4 digits) then 3, etc.,
177 # finally trying the country code only
178 my $rate_prefix = '';
179 for my $len ( reverse(1..6) ) {
180 $rate_prefix = qsearchs('rate_prefix', {
181 'countrycode' => $countrycode,
182 #'npa' => { op=> 'LIKE', value=> substr($number, 0, $len) }
183 'npa' => substr($number, 0, $len),
186 $rate_prefix ||= qsearchs('rate_prefix', {
187 'countrycode' => $countrycode,
192 die "Can't find rate for call $to_or_from +$countrycode $\numbern"
195 $regionnum = $rate_prefix->regionnum;
196 $rate_detail = qsearchs('rate_detail', {
197 'ratenum' => $ratenum,
198 'dest_regionnum' => $regionnum,
201 $rate_region = $rate_prefix->rate_region;
203 warn " found rate for regionnum $regionnum ".
204 "and rate detail $rate_detail\n"
207 } elsif ( $self->option('rating_method') eq 'upstream' ) {
209 if ( $cdr->cdrtypenum == 1 ) { #rate based on upstream rateid
211 $rate_detail = $cdr->cdr_upstream_rate->rate_detail;
213 $regionnum = $rate_detail->dest_regionnum;
214 $rate_region = $rate_detail->dest_region;
216 $pretty_destnum = $cdr->dst;
218 warn " found rate for regionnum $regionnum and ".
219 "rate detail $rate_detail\n"
222 } else { #pass upstream price through
224 $charge = sprintf('%.2f', $cdr->upstream_price);
227 #time2str("%Y %b %d - %r", $cdr->calldate_unix ),
228 time2str("%c", $cdr->calldate_unix), #XXX this should probably be a config option dropdown so they can select US vs- rest of world dates or whatnot
232 $cdr->description, #$rate_region->regionname,
238 die "don't know how to rate CDRs using method: ".
239 $self->option('rating_method'). "\n";
243 # find the price and add detail to the invoice
246 # if $rate_detail is not found, skip this CDR... i.e.
247 # don't add it to invoice, don't set its status to NULL,
248 # don't call downstream_csv or something on it...
249 # but DO emit a warning...
250 if ( ! $rate_detail && ! scalar(@call_details) ) {
252 warn "no rate_detail found for CDR.acctid: ". $cdr->acctid.
255 } else { # there *is* a rate_detail (or call_details), proceed...
257 unless ( @call_details ) {
259 $included_min{$regionnum} = $rate_detail->min_included
260 unless exists $included_min{$regionnum};
262 my $granularity = $rate_detail->sec_granularity;
263 my $seconds = $cdr->billsec; # |ength($cdr->billsec) ? $cdr->billsec : $cdr->duration;
264 $seconds += $granularity - ( $seconds % $granularity );
265 my $minutes = sprintf("%.1f", $seconds / 60);
266 $minutes =~ s/\.0$// if $granularity == 60;
268 $included_min{$regionnum} -= $minutes;
270 if ( $included_min{$regionnum} < 0 ) {
271 my $charge_min = 0 - $included_min{$regionnum};
272 $included_min{$regionnum} = 0;
273 $charge = sprintf('%.2f', $rate_detail->min_charge * $charge_min );
277 # this is why we need regionnum/rate_region....
278 warn " (rate region $rate_region)\n" if $DEBUG;
281 #time2str("%Y %b %d - %r", $cdr->calldate_unix ),
282 time2str("%c", $cdr->calldate_unix), #XXX this should probably be a config option dropdown so they can select US vs- rest of world dates or whatnot
286 $rate_region->regionname,
291 warn " adding details on charge to invoice: ".
292 join(' - ', @call_details )
295 push @$details, join(' - ', @call_details); #\@call_details,
297 # if the customer flag is on, call "downstream_csv" or something
298 # like it to export the call downstream!
299 # XXX price plan option to pick format, or something...
300 $downstream_cdr .= $cdr->downstream_csv( 'format' => 'convergent' )
303 my $error = $cdr->set_status_and_rated_price('done', $charge);
304 die $error if $error;
312 if ( $spool_cdr && length($downstream_cdr) ) {
314 use FS::UID qw(datasrc);
315 my $dir = '/usr/local/etc/freeside/export.'. datasrc. '/cdr';
316 mkdir $dir, 0700 unless -d $dir;
317 $dir .= '/'. $cust_pkg->custnum.
318 mkdir $dir, 0700 unless -d $dir;
319 my $filename = time2str("$dir/CDR%Y%m%d-spool.CSV", time); #XXX invoice date instead? would require changing the order things are generated in cust_main::bill insert cust_bill first - with transactions it could be done though
321 push @{ $param->{'precommit_hooks'} },
323 #lock the downstream spool file and append the records
324 use Fcntl qw(:flock);
326 my $spool = new IO::File ">>$filename"
327 or die "can't open $filename: $!\n";
328 flock( $spool, LOCK_EX)
329 or die "can't lock $filename: $!\n";
331 or die "can't seek to end of $filename: $!\n";
332 print $spool $downstream_cdr;
333 flock( $spool, LOCK_UN );
337 } #if ( $spool_cdr && length($downstream_cdr) )
339 $self->option('recur_flat') + $charges;
348 my($self, $cust_pkg) = @_;
349 $self->option('recur_flat');