1 package FS::part_pkg::voip_cdr;
2 use base qw( FS::part_pkg::recur_Common );
5 use vars qw( $DEBUG %info );
10 use FS::Record qw(qsearchs qsearch);
12 use FS::detail_format;
19 tie my %cdr_svc_method, 'Tie::IxHash',
20 'svc_phone.phonenum' => 'Phone numbers (svc_phone.phonenum)',
21 'svc_pbx.title' => 'PBX name (svc_pbx.title)',
22 'svc_pbx.svcnum' => 'Freeside service # (svc_pbx.svcnum)',
25 tie my %rating_method, 'Tie::IxHash',
26 'prefix' => 'Rate calls by using destination prefix to look up a region and rate according to the internal prefix and rate tables',
27 # '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.',
28 'upstream_simple' => 'Simply pass through and charge the "upstream_price" amount.',
29 'single_price' => 'A single price per minute for all calls.',
32 #tie my %cdr_location, 'Tie::IxHash',
33 # 'internal' => 'Internal: CDR records imported into the internal CDR table',
34 # 'external' => 'External: CDR records queried directly from an external '.
35 # 'Asterisk (or other?) CDR table',
38 tie my %temporalities, 'Tie::IxHash',
39 'upcoming' => "Upcoming (future)",
40 'preceding' => "Preceding (past)",
43 tie my %granularity, 'Tie::IxHash', FS::rate_detail::granularities();
45 # previously "1" was "ignore"
46 tie my %unrateable_opts, 'Tie::IxHash',
47 '' => 'Exit with a fatal error',
48 1 => 'Ignore and continue',
49 2 => 'Flag for later review',
53 'name' => 'VoIP rating by plan of CDR records in an internal (or external) SQL table',
54 'shortname' => 'VoIP/telco CDR rating (standard)',
55 'inherit_fields' => [ 'prorate_Mixin', 'global_Mixin' ],
57 'suspend_bill' => { 'name' => 'Continue recurring billing while suspended',
60 #false laziness w/flat.pm
61 'recur_temporality' => { 'name' => 'Charge recurring fee for period',
63 'select_options' => \%temporalities,
66 'cutoff_day' => { 'name' => 'Billing Day (1 - 28) for prorating or '.
70 'recur_method' => { 'name' => 'Recurring fee method',
72 #'options' => \%recur_method,
74 'select_options' => \%FS::part_pkg::recur_Common::recur_method,
77 'cdr_svc_method' => { 'name' => 'CDR service matching method',
79 'options' => \%cdr_svc_method,
82 'rating_method' => { 'name' => 'Rating method',
84 'options' => \%rating_method,
87 'ratenum' => { 'name' => 'Rate plan',
89 'select_table' => 'rate',
90 'select_key' => 'ratenum',
91 'select_label' => 'ratename',
94 'intrastate_ratenum' => { 'name' => 'Optional alternate intrastate rate plan',
96 'select_table' => 'rate',
97 'select_key' => 'ratenum',
98 'select_label' => 'ratename',
103 'min_included' => { 'name' => 'Minutes included when using the "single price per minute" rating method or when using the "prefix" rating method ("region group" billing)',
106 'min_charge' => { 'name' => 'Charge per minute when using "single price per minute" rating method',
109 'sec_granularity' => { 'name' => 'Granularity when using "single price per minute" rating method',
111 'select_options' => \%granularity,
114 'ignore_unrateable' => { 'name' => 'Handling of calls without a rate in the rate table',
116 'select_options' => \%unrateable_opts,
119 'default_prefix' => { 'name' => 'Default prefix optionally prepended to customer DID numbers when searching for CDR records',
123 'disable_src' => { 'name' => 'Disable rating of CDR records based on the "src" field in addition to "charged_party"',
127 'domestic_prefix' => { 'name' => 'Destination prefix for domestic CDR records',
131 # 'domestic_prefix_required' => { 'name' => 'Require explicit destination prefix for domestic CDR records',
132 # 'type' => 'checkbox',
135 'international_prefix' => { 'name' => 'Destination prefix for international CDR records',
139 'disable_tollfree' => { 'name' => 'Disable automatic toll-free processing',
140 'type' => 'checkbox',
143 'use_amaflags' => { 'name' => 'Only charge for CDRs where the amaflags field is set to "2" ("BILL"/"BILLING").',
144 'type' => 'checkbox',
147 'use_carrierid' => { 'name' => 'Only charge for CDRs where the Carrier ID is set to: ',
150 'use_cdrtypenum' => { 'name' => 'Only charge for CDRs where the CDR Type is set to: ',
153 'ignore_cdrtypenum' => { 'name' => 'Do not charge for CDRs where the CDR Type is set to: ',
156 'ignore_disposition' => { 'name' => 'Do not charge for CDRs where the Disposition is set to any of these (comma-separated) values: ',
159 'disposition_in' => { 'name' => 'Only charge for CDRs where the Disposition is set to any of these (comma-separated) values: ',
162 'skip_dst_prefix' => { 'name' => 'Do not charge for CDRs where the destination number starts with any of these values: ',
165 'skip_dcontext' => { 'name' => 'Do not charge for CDRs where the dcontext is set to any of these (comma-separated) values: ',
168 'skip_dstchannel_prefix' => { 'name' => 'Do not charge for CDRs where the dstchannel starts with:',
171 'skip_src_length_more' => { 'name' => 'Do not charge for CDRs where the source is more than this many digits:',
174 'noskip_src_length_accountcode_tollfree' => { 'name' => 'Do charge for CDRs where source is equal or greater than the specified digits, when accountcode is toll free',
175 'type' => 'checkbox',
178 'accountcode_tollfree_ratenum' => {
179 'name' => 'Optional alternate rate plan when accountcode is toll free: ',
181 'select_table' => 'rate',
182 'select_key' => 'ratenum',
183 'select_label' => 'ratename',
184 'disable_empty' => 0,
188 'skip_dst_length_less' => { 'name' => 'Do not charge for CDRs where the destination is less than this many digits:',
191 'noskip_dst_length_accountcode_tollfree' => { 'name' => 'Do charge for CDRs where dst is less than the specified digits, when accountcode is toll free',
192 'type' => 'checkbox',
195 'skip_lastapp' => { 'name' => 'Do not charge for CDRs where the lastapp matches this value: ',
198 'skip_max_callers' => { 'name' => 'Do not charge for CDRs where max_callers is less than or equal to this value: ',
201 'use_duration' => { 'name' => 'Calculate usage based on the duration field instead of the billsec field',
202 'type' => 'checkbox',
205 '411_rewrite' => { 'name' => 'Rewrite these (comma-separated) destination numbers to 411 for rating purposes (also ignore any carrierid check): ',
208 #false laziness w/cdr_termination.pm
209 'output_format' => { 'name' => 'CDR invoice display format',
211 'select_options' => { FS::cdr::invoice_formats() },
212 'default' => 'default', #XXX test
215 'usage_section' => { 'name' => 'Section in which to place usage charges (whether separated or not): ',
218 'summarize_usage' => { 'name' => 'Include usage summary with recurring charges when usage is in separate section',
219 'type' => 'checkbox',
222 'usage_mandate' => { 'name' => 'Always put usage details in separate section',
223 'type' => 'checkbox',
227 'bill_every_call' => { 'name' => 'Generate an invoice immediately for every call (as well any setup fee, upon first payment). Useful for prepaid.',
228 'type' => 'checkbox',
231 'bill_inactive_svcs' => { 'name' => 'Bill for all phone numbers that were active during the billing period',
232 'type' => 'checkbox',
235 'count_available_phones' => { 'name' => 'Consider for tax purposes the number of lines to be svc_phones that may be provisioned rather than those that actually are.',
236 'type' => 'checkbox',
239 #XXX also have option for an external db? these days we suck them into ours
240 # 'cdr_location' => { 'name' => 'CDR database location'
241 # 'type' => 'select',
242 # 'select_options' => \%cdr_location,
243 # 'select_callback' => {
245 # 'enable' => [ 'datasrc', 'username', 'password' ],
248 # 'disable' => [ 'datasrc', 'username', 'password' ],
252 # 'datasrc' => { 'name' => 'DBI data source for external CDR table',
255 # 'username' => { 'name' => 'External database username',
258 # 'password' => { 'name' => 'External database password',
265 recur_method cutoff_day ),
266 FS::part_pkg::prorate_Mixin::fieldorder,
269 rating_method ratenum intrastate_ratenum
270 min_charge min_included sec_granularity
274 domestic_prefix international_prefix
278 use_cdrtypenum ignore_cdrtypenum
279 ignore_disposition disposition_in
280 skip_dcontext skip_dst_prefix
281 skip_dstchannel_prefix skip_src_length_more
282 noskip_src_length_accountcode_tollfree
283 accountcode_tollfree_ratenum
285 noskip_dst_length_accountcode_tollfree
290 output_format usage_mandate summarize_usage usage_section
291 bill_every_call bill_inactive_svcs
292 count_available_phones suspend_bill
300 my $str = $self->SUPER::price_info;
301 $str .= " plus usage" if $str;
307 my($cust_pkg, $sdate, $details, $param ) = @_;
311 $charges += $self->calc_usage(@_);
312 $charges += $self->calc_recur_Common(@_);
321 # my($cust_pkg, $sdate, $details, $param ) = @_;
323 # $self->calc_usage(@_);
326 #false laziness w/voip_sqlradacct calc_recur resolve it if that one ever gets used again
330 my($cust_pkg, $sdate, $details, $param ) = @_;
332 #my $last_bill = $cust_pkg->last_bill;
333 my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
336 if $self->recur_temporality eq 'preceding'
337 && ( $last_bill eq '' || $last_bill == 0 );
341 my $included_min = $self->option('min_included', 1) || 0; #single price rating
343 my $cdr_svc_method = $self->option('cdr_svc_method',1)||'svc_phone.phonenum';
344 my $rating_method = $self->option('rating_method') || 'prefix';
345 my $region_group_included_min = $self->option('min_included',1) || 0;
346 my %region_group_included_min = ();
348 my $output_format = $self->option('output_format', 'Hush!')
349 || ( $rating_method eq 'upstream_simple'
354 my $formatter = FS::detail_format->new($output_format, buffer => $details);
356 my $use_duration = $self->option('use_duration');
358 my($svc_table, $svc_field) = split('\.', $cdr_svc_method);
361 if( $self->option('bill_inactive_svcs',1) ) {
362 #XXX in this mode do we need to restrict the set of CDRs by date also?
363 @cust_svc = $cust_pkg->h_cust_svc($$sdate, $last_bill);
366 @cust_svc = $cust_pkg->cust_svc;
368 @cust_svc = grep { $_->part_svc->svcdb eq $svc_table } @cust_svc;
370 foreach my $cust_svc (@cust_svc) {
373 if( $self->option('bill_inactive_svcs',1) ) {
374 $svc_x = $cust_svc->h_svc_x($$sdate, $last_bill);
377 $svc_x = $cust_svc->svc_x;
381 'disable_src' => $self->option('disable_src'),
382 'default_prefix' => $self->option('default_prefix'),
383 'cdrtypenum' => $self->option('use_cdrtypenum'),
386 ); # $last_bill, $$sdate )
387 $options{'by_svcnum'} = 1 if $svc_field eq 'svcnum';
389 #my @invoice_details_sort;
391 #first rate any outstanding CDRs not yet rated
393 $svc_x->get_cdrs( %options )
396 my $error = $cdr->rate(
398 'svcnum' => $svc_x->svcnum,
399 'single_price_included_min' => \$included_min,
400 'region_group_included_min' => \$region_group_included_min,
401 'region_group_included_min_hashref' => \%region_group_included_min,
403 die $error if $error; #??
407 #then add details to invoices & get a total
408 $options{'status'} = 'rated';
411 $svc_x->get_cdrs( %options )
413 # at this point we officially Do Not Care about the rating method
414 $charges += $cdr->rated_price;
415 $formatter->append($cdr);
416 my $error = $cdr->set_status('done');
417 die $error if $error;
421 $formatter->finish; #writes into $details
422 unshift @$details, $formatter->header if @$details;
427 #returns a reason why not to rate this CDR, or false if the CDR is chargeable
428 sub check_chargable {
429 my( $self, $cdr, %flags ) = @_;
431 return 'amaflags != 2'
432 if $self->option_cacheable('use_amaflags') && $cdr->amaflags != 2;
434 return "disposition NOT IN ( $self->option_cacheable('disposition_in') )"
435 if $self->option_cacheable('disposition_in') =~ /\S/
436 && !grep { $cdr->disposition eq $_ } split(/\s*,\s*/, $self->option_cacheable('disposition_in'));
438 return "disposition IN ( $self->option_cacheable('ignore_disposition') )"
439 if $self->option_cacheable('ignore_disposition') =~ /\S/
440 && grep { $cdr->disposition eq $_ } split(/\s*,\s*/, $self->option_cacheable('ignore_disposition'));
442 foreach(split(/\s*,\s*/, $self->option_cacheable('skip_dst_prefix'))) {
443 return "dst starts with '$_'"
444 if length($_) && substr($cdr->dst,0,length($_)) eq $_;
447 return "carrierid != $self->option_cacheable('use_carrierid')"
448 if length($self->option_cacheable('use_carrierid'))
449 && $cdr->carrierid ne $self->option_cacheable('use_carrierid') #ne otherwise 0 matches ''
450 && ! $flags{'da_rewrote'};
452 # unlike everything else, use_cdrtypenum is applied in FS::svc_x::get_cdrs.
453 return "cdrtypenum != $self->option_cacheable('use_cdrtypenum')"
454 if length($self->option_cacheable('use_cdrtypenum'))
455 && $cdr->cdrtypenum ne $self->option_cacheable('use_cdrtypenum'); #ne otherwise 0 matches ''
457 return "cdrtypenum == $self->option_cacheable('ignore_cdrtypenum')"
458 if length($self->option_cacheable('ignore_cdrtypenum'))
459 && $cdr->cdrtypenum eq $self->option_cacheable('ignore_cdrtypenum'); #eq otherwise 0 matches ''
461 return "dcontext IN ( $self->option_cacheable('skip_dcontext') )"
462 if $self->option_cacheable('skip_dcontext') =~ /\S/
463 && grep { $cdr->dcontext eq $_ } split(/\s*,\s*/, $self->option_cacheable('skip_dcontext'));
465 my $len_prefix = length($self->option_cacheable('skip_dstchannel_prefix'));
466 return "dstchannel starts with $self->option_cacheable('skip_dstchannel_prefix')"
468 && substr($cdr->dstchannel,0,$len_prefix) eq $self->option_cacheable('skip_dstchannel_prefix');
470 my $dst_length = $self->option_cacheable('skip_dst_length_less');
471 return "destination less than $dst_length digits"
472 if $dst_length && length($cdr->dst) < $dst_length
473 && ! ( $self->option_cacheable('noskip_dst_length_accountcode_tollfree')
474 && $cdr->is_tollfree('accountcode')
477 return "lastapp is $self->option_cacheable('skip_lastapp')"
478 if length($self->option_cacheable('skip_lastapp')) && $cdr->lastapp eq $self->option_cacheable('skip_lastapp');
480 my $src_length = $self->option_cacheable('skip_src_length_more');
483 if ( $self->option_cacheable('noskip_src_length_accountcode_tollfree') ) {
485 if ( $cdr->is_tollfree('accountcode') ) {
486 return "source less than or equal to $src_length digits"
487 if length($cdr->src) <= $src_length;
489 return "source more than $src_length digits"
490 if length($cdr->src) > $src_length;
494 return "source more than $src_length digits"
495 if length($cdr->src) > $src_length;
500 return "max_callers <= ". $self->option_cacheable('skip_max_callers')
501 if length($self->option_cacheable('skip_max_callers'))
502 and length($cdr->max_callers)
503 and $cdr->max_callers <= $self->option_cacheable('skip_max_callers');
505 #all right then, rate it
513 # This equates svc_phone records; perhaps svc_phone should have a field
514 # to indicate it represents a line
516 my($self, $cust_pkg ) = @_;
518 if ( $self->option('count_available_phones', 1)) {
519 map { $count += ( $_->quantity || 0 ) }
520 grep { $_->part_svc->svcdb eq 'svc_phone' }
521 $cust_pkg->part_pkg->pkg_svc;
524 scalar(grep { $_->part_svc->svcdb eq 'svc_phone' } $cust_pkg->cust_svc);
529 # tells whether cust_bill_pkg_detail should return a single line for
533 $self->option('output_format') =~ /^sum_/;
536 # and whether cust_bill should show a detail line for the service label
537 # (separate from usage details)
538 sub hide_svc_detail {
540 $self->option('output_format') =~ /^sum_/;