1 package FS::part_pkg::voip_inbound;
2 use base qw( FS::part_pkg::recur_Common );
5 use vars qw($DEBUG %info);
10 use FS::Record qw(qsearchs qsearch);
13 use FS::detail_format;
17 tie my %temporalities, 'Tie::IxHash',
18 'upcoming' => "Upcoming (future)",
19 'preceding' => "Preceding (past)",
22 tie my %granularity, 'Tie::IxHash', FS::rate_detail::granularities();
25 'name' => 'VoIP flat rate pricing of CDRs for inbound calls',
26 'shortname' => 'VoIP/telco CDR rating (inbound)',
27 'inherit_fields' => [ 'prorate_Mixin', 'global_Mixin' ],
29 #false laziness w/flat.pm
30 'recur_temporality' => { 'name' => 'Charge recurring fee for period',
32 'select_options' => \%temporalities,
34 'cutoff_day' => { 'name' => 'Billing Day (1 - 28) for prorating or '.
39 'recur_method' => { 'name' => 'Recurring fee method',
41 'select_options' => \%FS::part_pkg::recur_Common::recur_method,
44 'min_charge' => { 'name' => 'Charge per minute',
47 'min_included' => { 'name' => 'Minutes included',
50 'sec_granularity' => { 'name' => 'Granularity',
52 'select_options' => \%granularity,
55 'default_prefix' => { 'name' => 'Default prefix optionally prepended to customer DID numbers when searching for CDR records',
59 'use_amaflags' => { 'name' => 'Only charge for CDRs where the amaflags field is set to "2" ("BILL"/"BILLING").',
63 'use_carrierid' => { 'name' => 'Only charge for CDRs where the Carrier ID is set to any of these (comma-separated) values: ',
66 'use_cdrtypenum' => { 'name' => 'Only charge for CDRs where the CDR Type is set to this cdrtypenum: ',
69 'ignore_cdrtypenum' => { 'name' => 'Do not charge for CDRs where the CDR Type is set to this cdrtypenum: ',
72 'use_calltypenum' => { 'name' => 'Only charge for CDRs where the CDR Call Type is set to this cdrtypenum: ',
75 'ignore_calltypenum' => { 'name' => 'Do not charge for CDRs where the CDR Call Type is set to this cdrtypenum: ',
78 'ignore_disposition' => { 'name' => 'Do not charge for CDRs where the Disposition is set to any of these (comma-separated) values: ',
81 'disposition_in' => { 'name' => 'Only charge for CDRs where the Disposition is set to any of these (comma-separated) values: ',
84 'skip_dcontext' => { 'name' => 'Do not charge for CDRs where the dcontext is set to any of these (comma-separated) values:',
87 'skip_dstchannel_prefix' => { 'name' => 'Do not charge for CDRs where the dstchannel starts with:',
90 'skip_dst_length_less' => { 'name' => 'Do not charge for CDRs where the destination is less than this many digits:',
93 'skip_lastapp' => { 'name' => 'Do not charge for CDRs where the lastapp matches this value',
96 'use_duration' => { 'name' => 'Calculate usage based on the duration field instead of the billsec field',
100 #false laziness w/cdr_termination.pm
101 'output_format' => { 'name' => 'CDR invoice display format',
103 'select_options' => { FS::cdr::invoice_formats() },
104 'default' => 'default', #XXX test
107 'usage_section' => { 'name' => 'Section in which to place usage charges (whether separated or not)',
110 'summarize_usage' => { 'name' => 'Include usage summary with recurring charges when usage is in separate section',
111 'type' => 'checkbox',
114 'usage_mandate' => { 'name' => 'Always put usage details in separate section',
115 'type' => 'checkbox',
119 'bill_every_call' => { 'name' => 'Generate an invoice immediately for every call. Useful for prepaid.',
120 'type' => 'checkbox',
123 'bill_only_pkg_dates' => { 'name' => 'Only bill CDRs with a date during the package billing period',
124 'type' => 'checkbox',
127 #XXX also have option for an external db
128 # 'cdr_location' => { 'name' => 'CDR database location'
129 # 'type' => 'select',
130 # 'select_options' => \%cdr_location,
131 # 'select_callback' => {
133 # 'enable' => [ 'datasrc', 'username', 'password' ],
136 # 'disable' => [ 'datasrc', 'username', 'password' ],
140 # 'datasrc' => { 'name' => 'DBI data source for external CDR table',
143 # 'username' => { 'name' => 'External database username',
146 # 'password' => { 'name' => 'External database password',
153 recur_method cutoff_day ),
154 FS::part_pkg::prorate_Mixin::fieldorder,
155 qw( min_charge min_included sec_granularity
159 use_cdrtypenum ignore_cdrtypenum
160 use_calltypenum ignore_calltypenum
161 ignore_disposition disposition_in
162 skip_dcontext skip_dstchannel_prefix
163 skip_dst_length_less skip_lastapp
165 output_format usage_mandate summarize_usage usage_section
175 my $str = $self->SUPER::price_info;
176 $str .= " plus usage" if $str;
182 my($cust_pkg, $sdate, $details, $param ) = @_;
186 $charges += $self->calc_usage(@_);
187 $charges += ($cust_pkg->quantity || 1) * $self->calc_recur_Common(@_);
196 # my($cust_pkg, $sdate, $details, $param ) = @_;
198 # $self->calc_usage(@_);
201 #false laziness w/voip_sqlradacct calc_recur resolve it if that one ever gets used again
205 my($cust_pkg, $sdate, $details, $param ) = @_;
207 #my $last_bill = $cust_pkg->last_bill;
208 my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
211 if $self->recur_temporality eq 'preceding'
212 && ( $last_bill eq '' || $last_bill == 0 );
214 my $spool_cdr = $cust_pkg->cust_main->spool_cdr;
219 # my $downstream_cdr = '';
221 my $included_min = $self->option('min_included', 1) || 0;
222 $included_min *= ($cust_pkg->quantity || 1);
223 my $use_duration = $self->option('use_duration');
224 my $output_format = $self->option('output_format', 1) || 'default';
227 FS::detail_format->new($output_format,
230 locale => $cust_pkg->cust_main->locale
233 my $granularity = length($self->option('sec_granularity'))
234 ? $self->option('sec_granularity')
237 #for check_chargable, so we don't keep looking up options inside the loop
240 my $csv = new Text::CSV_XS;
242 foreach my $cust_svc (
243 grep { $_->part_svc->svcdb eq 'svc_phone' } $cust_pkg->cust_svc
245 my $svc_phone = $cust_svc->svc_x;
249 'default_prefix' => $self->option('default_prefix'),
250 'status' => '', # unprocessed only
253 if ( $self->option('bill_only_pkg_dates') ) {
254 $options{'begin'} = $last_bill;
255 $options{'end'} = $$sdate;
258 my $cdr_search = $svc_phone->psearch_cdrs(%options);
259 $cdr_search->limit(1000);
260 $cdr_search->increment(0);
261 while ( my $cdr = $cdr_search->fetch ) {
263 my $reason = $self->check_chargable( $cdr,
264 'option_cache' => \%opt_cache,
267 warn "not charging for CDR ($reason)\n" if $DEBUG;
268 $cdr_search->adjust(1);
273 warn "rating inbound CDR $cdr\n".
274 join('', map { " $_ => ". $cdr->{$_}. "\n" } keys %$cdr );
277 my $seconds = $use_duration ? $cdr->duration : $cdr->billsec;
279 $seconds += $granularity - ( $seconds % $granularity )
280 if $seconds # don't granular-ize 0 billsec calls (bills them)
281 && $granularity; # 0 is per call
282 my $minutes = sprintf("%.1f",$seconds / 60);
283 $minutes =~ s/\.0$// if $granularity == 60; # count whole minutes, convert to integer
284 $minutes = 1 unless $granularity; # per call
286 my $charge_min = $minutes;
289 $included_min -= $minutes;
290 if ( $included_min > 0 ) {
294 $charge_min = 0 - $included_min;
298 $charge = sprintf('%.4f', ( $self->option('min_charge') * $charge_min )
299 + 0.00000001 ); #so 1.00005 rounds to 1.0001
304 $cdr->downstream_csv( 'format' => $output_format,
306 'seconds' => $seconds,
307 'granularity' => $granularity,
312 # detail => $call_details[0],
314 # classnum => $cdr->calltypenum, #classnum
315 # #phonenum => $self->phonenum,
316 # accountcode => $cdr->accountcode,
317 # startdate => $cdr->startdate,
318 # duration => $seconds,
319 # # regionname?? => '', #regionname, not set for inbound calls
323 # eventually use FS::cdr::rate for this
324 my $error = $cdr->set_status_and_rated_price(
328 'rated_seconds' => $seconds,
329 'rated_granularity' => $granularity,
330 'rated_classnum' => $cdr->calltypenum,
331 'inbound' => 1, # to update cdr_termination, not cdr
333 die $error if $error;
334 $formatter->append($cdr);
336 $cdr_search->adjust(1) if $cdr->freesidestatus eq '';
340 # unshift @$details, { format => 'C',
341 # detail => FS::cdr::invoice_header($output_format),
346 unshift @$details, $formatter->header if @$details;
351 #returns a reason why not to rate this CDR, or false if the CDR is chargeable
352 # lots of false laziness w/voip_cdr...
353 sub check_chargable {
354 my( $self, $cdr, %flags ) = @_;
356 return 'amaflags != 2'
357 if $self->option_cacheable('use_amaflags') && $cdr->amaflags != 2;
359 return "disposition NOT IN ( ". $self->option_cacheable('disposition_in')." )"
360 if $self->option_cacheable('disposition_in') =~ /\S/
361 && !grep { $cdr->disposition eq $_ } split(/\s*,\s*/, $self->option_cacheable('disposition_in'));
363 return "disposition IN ( ". $self->option_cacheable('ignore_disposition')." )"
364 if $self->option_cacheable('ignore_disposition') =~ /\S/
365 && grep { $cdr->disposition eq $_ } split(/\s*,\s*/, $self->option_cacheable('ignore_disposition'));
367 return "carrierid NOT IN ( ". $self->option_cacheable('use_carrierid'). " )"
368 if $self->option_cacheable('use_carrierid') =~ /\S/
369 && !grep { $cdr->carrierid eq $_ } split(/\s*,\s*/, $self->option_cacheable('use_carrierid')); #eq otherwise 0 matches ''
371 # unlike everything else, use_cdrtypenum is applied in FS::svc_x::get_cdrs.
372 return "cdrtypenum != ". $self->option_cacheable('use_cdrtypenum')
373 if length($self->option_cacheable('use_cdrtypenum'))
374 && $cdr->cdrtypenum ne $self->option_cacheable('use_cdrtypenum'); #ne otherwise 0 matches ''
376 return "cdrtypenum == ". $self->option_cacheable('ignore_cdrtypenum')
377 if length($self->option_cacheable('ignore_cdrtypenum'))
378 && $cdr->cdrtypenum eq $self->option_cacheable('ignore_cdrtypenum'); #eq otherwise 0 matches ''
380 # unlike everything else, use_calltypenum is applied in FS::svc_x::get_cdrs.
381 return "calltypenum != ". $self->option_cacheable('use_calltypenum')
382 if length($self->option_cacheable('use_calltypenum'))
383 && $cdr->calltypenum ne $self->option_cacheable('use_calltypenum'); #ne otherwise 0 matches ''
385 return "calltypenum == ". $self->option_cacheable('ignore_calltypenum')
386 if length($self->option_cacheable('ignore_calltypenum'))
387 && $cdr->calltypenum eq $self->option_cacheable('ignore_calltypenum'); #eq otherwise 0 matches ''
389 return "dcontext IN ( ". $self->option_cacheable('skip_dcontext'). " )"
390 if $self->option_cacheable('skip_dcontext') =~ /\S/
391 && grep { $cdr->dcontext eq $_ } split(/\s*,\s*/, $self->option_cacheable('skip_dcontext'));
393 my $len_prefix = length($self->option_cacheable('skip_dstchannel_prefix'));
394 return "dstchannel starts with ". $self->option_cacheable('skip_dstchannel_prefix')
396 && substr($cdr->dstchannel,0,$len_prefix) eq $self->option_cacheable('skip_dstchannel_prefix');
398 my $dst_length = $self->option_cacheable('skip_dst_length_less');
399 return "destination less than $dst_length digits"
400 if $dst_length && length($cdr->dst) < $dst_length;
402 return "lastapp is ". $self->option_cacheable('skip_lastapp')
403 if length($self->option_cacheable('skip_lastapp')) && $cdr->lastapp eq $self->option_cacheable('skip_lastapp');
405 #all right then, rate it
413 # This equates svc_phone records; perhaps svc_phone should have a field
414 # to indicate it represents a line
415 # #XXX no count_available_phones?
417 my($self, $cust_pkg ) = @_;
419 scalar(grep { $_->part_svc->svcdb eq 'svc_phone' } $cust_pkg->cust_svc);