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: ',
66 'use_cdrtypenum' => { 'name' => 'Only charge for CDRs where the CDR Type is set to: ',
69 'ignore_cdrtypenum' => { 'name' => 'Do not charge for CDRs where the CDR Type is set to: ',
72 'ignore_disposition' => { 'name' => 'Do not charge for CDRs where the Disposition is set to any of these (comma-separated) values: ',
75 'disposition_in' => { 'name' => 'Only charge for CDRs where the Disposition is set to any of these (comma-separated) values: ',
78 'skip_dcontext' => { 'name' => 'Do not charge for CDRs where the dcontext is set to any of these (comma-separated) values:',
81 'skip_dstchannel_prefix' => { 'name' => 'Do not charge for CDRs where the dstchannel starts with:',
84 'skip_dst_length_less' => { 'name' => 'Do not charge for CDRs where the destination is less than this many digits:',
87 'skip_lastapp' => { 'name' => 'Do not charge for CDRs where the lastapp matches this value',
90 'use_duration' => { 'name' => 'Calculate usage based on the duration field instead of the billsec field',
94 #false laziness w/cdr_termination.pm
95 'output_format' => { 'name' => 'CDR invoice display format',
97 'select_options' => { FS::cdr::invoice_formats() },
98 'default' => 'default', #XXX test
101 'usage_section' => { 'name' => 'Section in which to place usage charges (whether separated or not)',
104 'summarize_usage' => { 'name' => 'Include usage summary with recurring charges when usage is in separate section',
105 'type' => 'checkbox',
108 'usage_mandate' => { 'name' => 'Always put usage details in separate section',
109 'type' => 'checkbox',
113 'bill_every_call' => { 'name' => 'Generate an invoice immediately for every call. Useful for prepaid.',
114 'type' => 'checkbox',
117 #XXX also have option for an external db
118 # 'cdr_location' => { 'name' => 'CDR database location'
119 # 'type' => 'select',
120 # 'select_options' => \%cdr_location,
121 # 'select_callback' => {
123 # 'enable' => [ 'datasrc', 'username', 'password' ],
126 # 'disable' => [ 'datasrc', 'username', 'password' ],
130 # 'datasrc' => { 'name' => 'DBI data source for external CDR table',
133 # 'username' => { 'name' => 'External database username',
136 # 'password' => { 'name' => 'External database password',
143 recur_method cutoff_day ),
144 FS::part_pkg::prorate_Mixin::fieldorder,
145 qw( min_charge min_included sec_granularity
149 use_cdrtypenum ignore_cdrtypenum
150 ignore_disposition disposition_in
151 skip_dcontext skip_dstchannel_prefix
152 skip_dst_length_less skip_lastapp
154 output_format usage_mandate summarize_usage usage_section
163 my $str = $self->SUPER::price_info;
164 $str .= " plus usage" if $str;
170 my($cust_pkg, $sdate, $details, $param ) = @_;
174 $charges += $self->calc_usage(@_);
175 $charges += $self->calc_recur_Common(@_);
184 # my($cust_pkg, $sdate, $details, $param ) = @_;
186 # $self->calc_usage(@_);
189 #false laziness w/voip_sqlradacct calc_recur resolve it if that one ever gets used again
193 my($cust_pkg, $sdate, $details, $param ) = @_;
195 #my $last_bill = $cust_pkg->last_bill;
196 my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
199 if $self->recur_temporality eq 'preceding'
200 && ( $last_bill eq '' || $last_bill == 0 );
202 my $spool_cdr = $cust_pkg->cust_main->spool_cdr;
207 # my $downstream_cdr = '';
209 my $included_min = $self->option('min_included', 1) || 0;
210 my $use_duration = $self->option('use_duration');
211 my $output_format = $self->option('output_format', 1) || 'default';
214 FS::detail_format->new($output_format, buffer => $details, inbound => 1);
216 my $granularity = length($self->option('sec_granularity'))
217 ? $self->option('sec_granularity')
220 #for check_chargable, so we don't keep looking up options inside the loop
223 my $csv = new Text::CSV_XS;
225 foreach my $cust_svc (
226 grep { $_->part_svc->svcdb eq 'svc_phone' } $cust_pkg->cust_svc
228 my $svc_phone = $cust_svc->svc_x;
230 my $cdr_search = $svc_phone->psearch_cdrs(
232 'default_prefix' => $self->option('default_prefix'),
233 'status' => '', # unprocessed only
236 $cdr_search->limit(1000);
237 $cdr_search->increment(0);
238 while ( my $cdr = $cdr_search->fetch ) {
240 my $reason = $self->check_chargable( $cdr,
241 'option_cache' => \%opt_cache,
244 warn "not charging for CDR ($reason)\n" if $DEBUG;
245 $cdr_search->adjust(1);
250 warn "rating inbound CDR $cdr\n".
251 join('', map { " $_ => ". $cdr->{$_}. "\n" } keys %$cdr );
254 my $seconds = $use_duration ? $cdr->duration : $cdr->billsec;
256 $seconds += $granularity - ( $seconds % $granularity )
257 if $seconds # don't granular-ize 0 billsec calls (bills them)
258 && $granularity; # 0 is per call
259 my $minutes = sprintf("%.1f",$seconds / 60);
260 $minutes =~ s/\.0$// if $granularity == 60; # count whole minutes, convert to integer
261 $minutes = 1 unless $granularity; # per call
263 my $charge_min = $minutes;
266 $included_min -= $minutes;
267 if ( $included_min > 0 ) {
271 $charge_min = 0 - $included_min;
275 $charge = sprintf('%.4f', ( $self->option('min_charge') * $charge_min )
276 + 0.00000001 ); #so 1.00005 rounds to 1.0001
281 $cdr->downstream_csv( 'format' => $output_format,
283 'seconds' => ($use_duration
287 'granularity' => $granularity,
292 # detail => $call_details[0],
294 # classnum => $cdr->calltypenum, #classnum
295 # #phonenum => $self->phonenum,
296 # accountcode => $cdr->accountcode,
297 # startdate => $cdr->startdate,
298 # duration => $seconds,
299 # # regionname?? => '', #regionname, not set for inbound calls
303 # eventually use FS::cdr::rate for this
304 my $error = $cdr->set_status_and_rated_price(
308 'rated_seconds' => $use_duration ? $cdr->duration : $cdr->billsec,
309 'rated_granularity' => $granularity,
310 'rated_classnum' => $cdr->calltypenum,
313 die $error if $error;
314 $formatter->append($cdr);
316 $cdr_search->adjust(1) if $cdr->freesidestatus eq '';
320 # unshift @$details, { format => 'C',
321 # detail => FS::cdr::invoice_header($output_format),
326 unshift @$details, $formatter->header if @$details;
331 #returns a reason why not to rate this CDR, or false if the CDR is chargeable
332 sub check_chargable {
333 my( $self, $cdr, %flags ) = @_;
335 #should have some better way of checking these options from a hash
346 skip_dstchannel_prefix
350 foreach my $opt (grep !exists($flags{option_cache}->{$_}), @opt ) {
351 $flags{option_cache}->{$opt} = $self->option($opt, 1);
353 my %opt = %{ $flags{option_cache} };
355 return 'amaflags != 2'
356 if $opt{'use_amaflags'} && $cdr->amaflags != 2;
358 return "disposition NOT IN ( $opt{'disposition_in'} )"
359 if $opt{'disposition_in'} =~ /\S/
360 && !grep { $cdr->disposition eq $_ } split(/\s*,\s*/, $opt{'disposition_in'});
362 return "disposition IN ( $opt{'ignore_disposition'} )"
363 if $opt{'ignore_disposition'} =~ /\S/
364 && grep { $cdr->disposition eq $_ } split(/\s*,\s*/, $opt{'ignore_disposition'});
366 return "carrierid != $opt{'use_carrierid'}"
367 if length($opt{'use_carrierid'})
368 && $cdr->carrierid ne $opt{'use_carrierid'}; #ne otherwise 0 matches ''
370 return "cdrtypenum != $opt{'use_cdrtypenum'}"
371 if length($opt{'use_cdrtypenum'})
372 && $cdr->cdrtypenum ne $opt{'use_cdrtypenum'}; #ne otherwise 0 matches ''
374 return "cdrtypenum == $opt{'ignore_cdrtypenum'}"
375 if length($opt{'ignore_cdrtypenum'})
376 && $cdr->cdrtypenum eq $opt{'ignore_cdrtypenum'}; #eq otherwise 0 matches ''
378 return "dcontext IN ( $opt{'skip_dcontext'} )"
379 if $opt{'skip_dcontext'} =~ /\S/
380 && grep { $cdr->dcontext eq $_ } split(/\s*,\s*/, $opt{'skip_dcontext'});
382 my $len_prefix = length($opt{'skip_dstchannel_prefix'});
383 return "dstchannel starts with $opt{'skip_dstchannel_prefix'}"
385 && substr($cdr->dstchannel,0,$len_prefix) eq $opt{'skip_dstchannel_prefix'};
387 my $dst_length = $opt{'skip_dst_length_less'};
388 return "destination less than $dst_length digits"
389 if $dst_length && length($cdr->dst) < $dst_length;
391 return "lastapp is $opt{'skip_lastapp'}"
392 if length($opt{'skip_lastapp'}) && $cdr->lastapp eq $opt{'skip_lastapp'};
394 #all right then, rate it
402 # This equates svc_phone records; perhaps svc_phone should have a field
403 # to indicate it represents a line
405 my($self, $cust_pkg ) = @_;
407 scalar(grep { $_->part_svc->svcdb eq 'svc_phone' } $cust_pkg->cust_svc);