1 package FS::part_pkg::voip_inbound;
4 use vars qw(@ISA $DEBUG %info);
8 use FS::Record qw(qsearchs qsearch);
9 use FS::part_pkg::recur_Common;
11 use FS::part_pkg::recur_Common;
13 @ISA = qw(FS::part_pkg::recur_Common);
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 'disable_tollfree' => { 'name' => 'Disable automatic toll-free processing',
63 'use_amaflags' => { 'name' => 'Only charge for CDRs where the amaflags field is set to "2" ("BILL"/"BILLING").',
67 'use_carrierid' => { 'name' => 'Only charge for CDRs where the Carrier ID is set to: ',
70 'use_cdrtypenum' => { 'name' => 'Only charge for CDRs where the CDR Type is set to: ',
73 'ignore_cdrtypenum' => { 'name' => 'Do not charge for CDRs where the CDR Type is set to: ',
76 'ignore_disposition' => { 'name' => 'Do not charge for CDRs where the Disposition is set to any of these (comma-separated) values: ',
79 'disposition_in' => { 'name' => 'Only charge for CDRs where the Disposition is set to any of these (comma-separated) values: ',
82 'skip_dcontext' => { 'name' => 'Do not charge for CDRs where the dcontext is set to any of these (comma-separated) values:',
85 'skip_dstchannel_prefix' => { 'name' => 'Do not charge for CDRs where the dstchannel starts with:',
88 'skip_dst_length_less' => { 'name' => 'Do not charge for CDRs where the destination is less than this many digits:',
91 'skip_lastapp' => { 'name' => 'Do not charge for CDRs where the lastapp matches this value',
94 'use_duration' => { 'name' => 'Calculate usage based on the duration field instead of the billsec field',
98 #false laziness w/cdr_termination.pm
99 'output_format' => { 'name' => 'CDR invoice display format',
101 'select_options' => { FS::cdr::invoice_formats() },
102 'default' => 'default', #XXX test
105 'usage_section' => { 'name' => 'Section in which to place usage charges (whether separated or not)',
108 'summarize_usage' => { 'name' => 'Include usage summary with recurring charges when usage is in separate section',
109 'type' => 'checkbox',
112 'usage_mandate' => { 'name' => 'Always put usage details in separate section',
113 'type' => 'checkbox',
117 'bill_every_call' => { 'name' => 'Generate an invoice immediately for every call. Useful for prepaid.',
118 'type' => 'checkbox',
121 #XXX also have option for an external db
122 # 'cdr_location' => { 'name' => 'CDR database location'
123 # 'type' => 'select',
124 # 'select_options' => \%cdr_location,
125 # 'select_callback' => {
127 # 'enable' => [ 'datasrc', 'username', 'password' ],
130 # 'disable' => [ 'datasrc', 'username', 'password' ],
134 # 'datasrc' => { 'name' => 'DBI data source for external CDR table',
137 # 'username' => { 'name' => 'External database username',
140 # 'password' => { 'name' => 'External database password',
147 recur_method cutoff_day ),
148 FS::part_pkg::prorate_Mixin::fieldorder,
149 qw( min_charge min_included sec_granularity
154 use_cdrtypenum ignore_cdrtypenum
155 ignore_disposition disposition_in
156 skip_dcontext skip_dstchannel_prefix
157 skip_dst_length_less skip_lastapp
159 output_format usage_mandate summarize_usage usage_section
168 my $str = $self->SUPER::price_info;
169 $str .= " plus usage" if $str;
175 my($cust_pkg, $sdate, $details, $param ) = @_;
179 $charges += $self->calc_usage(@_);
180 $charges += $self->calc_recur_Common(@_);
188 my($cust_pkg, $sdate, $details, $param ) = @_;
190 $self->calc_usage(@_);
193 #false laziness w/voip_sqlradacct calc_recur resolve it if that one ever gets used again
197 my($cust_pkg, $sdate, $details, $param ) = @_;
199 #my $last_bill = $cust_pkg->last_bill;
200 my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
203 if $self->recur_temporality eq 'preceding'
204 && ( $last_bill eq '' || $last_bill == 0 );
206 my $spool_cdr = $cust_pkg->cust_main->spool_cdr;
208 my $included_min = ($self->option('min_included')
209 && $self->option('min_included') > 0)
210 ? $self->option('min_included') : 0;
214 # my $downstream_cdr = '';
216 my $disable_tollfree = $self->option('disable_tollfree');
217 my $ignore_unrateable = $self->option('ignore_unrateable', 'Hush!');
218 my $use_duration = $self->option('use_duration');
220 my $output_format = $self->option('output_format', 'Hush!') || 'default';
222 #for check_chargable, so we don't keep looking up options inside the loop
225 eval "use Text::CSV_XS;";
227 my $csv = new Text::CSV_XS;
229 foreach my $cust_svc (
230 grep { $_->part_svc->svcdb eq 'svc_phone' } $cust_pkg->cust_svc
232 my $svc_phone = $cust_svc->svc_x;
234 foreach my $cdr ( $svc_phone->get_cdrs(
236 'status' => '', # unprocessed only
237 'default_prefix' => $self->option('default_prefix'),
242 warn "rating inbound CDR $cdr\n".
243 join('', map { " $_ => ". $cdr->{$_}. "\n" } keys %$cdr );
245 my $granularity = length($self->option('sec_granularity'))
246 ? $self->option('sec_granularity')
249 my $seconds = $use_duration ? $cdr->duration : $cdr->billsec;
251 $seconds += $granularity - ( $seconds % $granularity )
252 if $seconds # don't granular-ize 0 billsec calls (bills them)
253 && $granularity; # 0 is per call
254 my $minutes = sprintf("%.1f",$seconds / 60);
255 $minutes =~ s/\.0$// if $granularity == 60; # count whole minutes, convert to integer
256 $minutes = 1 unless $granularity; # per call
258 my $charge_min = $minutes;
261 $included_min -= $minutes;
262 if ( $included_min > 0 ) {
266 $charge_min = 0 - $included_min;
270 $charge = sprintf('%.4f', ( $self->option('min_charge') * $charge_min )
271 + 0.00000001 ); #so 1.00005 rounds to 1.0001
276 $cdr->downstream_csv( 'format' => $output_format,
278 'seconds' => ($use_duration
282 'granularity' => $granularity,
289 $cdr->calltypenum, #classnum
294 '', #regionname, not set for inbound calls
298 my $error = $cdr->set_status_and_rated_price( 'done',
302 die $error if $error;
306 unshift @$details, [ 'C',
307 FS::cdr::invoice_header($output_format),
319 #returns a reason why not to rate this CDR, or false if the CDR is chargeable
320 sub check_chargable {
321 my( $self, $cdr, %flags ) = @_;
323 #should have some better way of checking these options from a hash
334 skip_dstchannel_prefix
338 foreach my $opt (grep !exists($flags{option_cache}->{$_}), @opt ) {
339 $flags{option_cache}->{$opt} = $self->option($opt, 1);
341 my %opt = %{ $flags{option_cache} };
343 return 'amaflags != 2'
344 if $opt{'use_amaflags'} && $cdr->amaflags != 2;
346 return "disposition NOT IN ( $opt{'disposition_in'} )"
347 if $opt{'disposition_in'} =~ /\S/
348 && !grep { $cdr->disposition eq $_ } split(/\s*,\s*/, $opt{'disposition_in'});
350 return "disposition IN ( $opt{'ignore_disposition'} )"
351 if $opt{'ignore_disposition'} =~ /\S/
352 && grep { $cdr->disposition eq $_ } split(/\s*,\s*/, $opt{'ignore_disposition'});
354 return "carrierid != $opt{'use_carrierid'}"
355 if length($opt{'use_carrierid'})
356 && $cdr->carrierid ne $opt{'use_carrierid'} #ne otherwise 0 matches ''
357 && ! $flags{'da_rewrote'};
359 return "cdrtypenum != $opt{'use_cdrtypenum'}"
360 if length($opt{'use_cdrtypenum'})
361 && $cdr->cdrtypenum ne $opt{'use_cdrtypenum'}; #ne otherwise 0 matches ''
363 return "cdrtypenum == $opt{'ignore_cdrtypenum'}"
364 if length($opt{'ignore_cdrtypenum'})
365 && $cdr->cdrtypenum eq $opt{'ignore_cdrtypenum'}; #eq otherwise 0 matches ''
367 return "dcontext IN ( $opt{'skip_dcontext'} )"
368 if $opt{'skip_dcontext'} =~ /\S/
369 && grep { $cdr->dcontext eq $_ } split(/\s*,\s*/, $opt{'skip_dcontext'});
371 my $len_prefix = length($opt{'skip_dstchannel_prefix'});
372 return "dstchannel starts with $opt{'skip_dstchannel_prefix'}"
374 && substr($cdr->dstchannel,0,$len_prefix) eq $opt{'skip_dstchannel_prefix'};
376 my $dst_length = $opt{'skip_dst_length_less'};
377 return "destination less than $dst_length digits"
378 if $dst_length && length($cdr->dst) < $dst_length;
380 return "lastapp is $opt{'skip_lastapp'}"
381 if length($opt{'skip_lastapp'}) && $cdr->lastapp eq $opt{'skip_lastapp'};
383 #all right then, rate it
391 # This equates svc_phone records; perhaps svc_phone should have a field
392 # to indicate it represents a line
394 my($self, $cust_pkg ) = @_;
396 scalar(grep { $_->part_svc->svcdb eq 'svc_phone' } $cust_pkg->cust_svc);