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);
16 tie my %temporalities, 'Tie::IxHash',
17 'upcoming' => "Upcoming (future)",
18 'preceding' => "Preceding (past)",
21 tie my %granularity, 'Tie::IxHash', FS::rate_detail::granularities();
24 'name' => 'VoIP flat rate pricing of CDRs for inbound calls',
25 'shortname' => 'VoIP/telco CDR rating (inbound)',
26 'inherit_fields' => [ 'prorate_Mixin', 'global_Mixin' ],
28 #false laziness w/flat.pm
29 'recur_temporality' => { 'name' => 'Charge recurring fee for period',
31 'select_options' => \%temporalities,
33 'cutoff_day' => { 'name' => 'Billing Day (1 - 28) for prorating or '.
38 'recur_method' => { 'name' => 'Recurring fee method',
40 'select_options' => \%FS::part_pkg::recur_Common::recur_method,
43 'min_charge' => { 'name' => 'Charge per minute',
46 'min_included' => { 'name' => 'Minutes included',
49 'sec_granularity' => { 'name' => 'Granularity',
51 'select_options' => \%granularity,
54 'default_prefix' => { 'name' => 'Default prefix optionally prepended to customer DID numbers when searching for CDR records',
58 'use_amaflags' => { 'name' => 'Only charge for CDRs where the amaflags field is set to "2" ("BILL"/"BILLING").',
62 'use_carrierid' => { 'name' => 'Only charge for CDRs where the Carrier ID is set to: ',
65 'use_cdrtypenum' => { 'name' => 'Only charge for CDRs where the CDR Type is set to: ',
68 'ignore_cdrtypenum' => { 'name' => 'Do not charge for CDRs where the CDR Type is set to: ',
71 'ignore_disposition' => { 'name' => 'Do not charge for CDRs where the Disposition is set to any of these (comma-separated) values: ',
74 'disposition_in' => { 'name' => 'Only charge for CDRs where the Disposition is set to any of these (comma-separated) values: ',
77 'skip_dcontext' => { 'name' => 'Do not charge for CDRs where the dcontext is set to any of these (comma-separated) values:',
80 'skip_dstchannel_prefix' => { 'name' => 'Do not charge for CDRs where the dstchannel starts with:',
83 'skip_dst_length_less' => { 'name' => 'Do not charge for CDRs where the destination is less than this many digits:',
86 'skip_lastapp' => { 'name' => 'Do not charge for CDRs where the lastapp matches this value',
89 'use_duration' => { 'name' => 'Calculate usage based on the duration field instead of the billsec field',
93 #false laziness w/cdr_termination.pm
94 'output_format' => { 'name' => 'CDR invoice display format',
96 'select_options' => { FS::cdr::invoice_formats() },
97 'default' => 'default', #XXX test
100 'usage_section' => { 'name' => 'Section in which to place usage charges (whether separated or not)',
103 'summarize_usage' => { 'name' => 'Include usage summary with recurring charges when usage is in separate section',
104 'type' => 'checkbox',
107 'usage_mandate' => { 'name' => 'Always put usage details in separate section',
108 'type' => 'checkbox',
112 'bill_every_call' => { 'name' => 'Generate an invoice immediately for every call. Useful for prepaid.',
113 'type' => 'checkbox',
116 #XXX also have option for an external db
117 # 'cdr_location' => { 'name' => 'CDR database location'
118 # 'type' => 'select',
119 # 'select_options' => \%cdr_location,
120 # 'select_callback' => {
122 # 'enable' => [ 'datasrc', 'username', 'password' ],
125 # 'disable' => [ 'datasrc', 'username', 'password' ],
129 # 'datasrc' => { 'name' => 'DBI data source for external CDR table',
132 # 'username' => { 'name' => 'External database username',
135 # 'password' => { 'name' => 'External database password',
142 recur_method cutoff_day ),
143 FS::part_pkg::prorate_Mixin::fieldorder,
144 qw( min_charge min_included sec_granularity
148 use_cdrtypenum ignore_cdrtypenum
149 ignore_disposition disposition_in
150 skip_dcontext skip_dstchannel_prefix
151 skip_dst_length_less skip_lastapp
153 output_format usage_mandate summarize_usage usage_section
162 my $str = $self->SUPER::price_info;
163 $str .= " plus usage" if $str;
169 my($cust_pkg, $sdate, $details, $param ) = @_;
173 $charges += $self->calc_usage(@_);
174 $charges += $self->calc_recur_Common(@_);
183 # my($cust_pkg, $sdate, $details, $param ) = @_;
185 # $self->calc_usage(@_);
188 #false laziness w/voip_sqlradacct calc_recur resolve it if that one ever gets used again
192 my($cust_pkg, $sdate, $details, $param ) = @_;
194 #my $last_bill = $cust_pkg->last_bill;
195 my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
198 if $self->recur_temporality eq 'preceding'
199 && ( $last_bill eq '' || $last_bill == 0 );
201 my $spool_cdr = $cust_pkg->cust_main->spool_cdr;
206 # my $downstream_cdr = '';
208 my $included_min = $self->option('min_included', 1) || 0;
209 my $use_duration = $self->option('use_duration');
210 my $output_format = $self->option('output_format', 1) || 'default';
211 my $granularity = length($self->option('sec_granularity'))
212 ? $self->option('sec_granularity')
215 #for check_chargable, so we don't keep looking up options inside the loop
218 my $csv = new Text::CSV_XS;
220 foreach my $cust_svc (
221 grep { $_->part_svc->svcdb eq 'svc_phone' } $cust_pkg->cust_svc
223 my $svc_phone = $cust_svc->svc_x;
225 foreach my $cdr ( $svc_phone->get_cdrs(
227 'default_prefix' => $self->option('default_prefix'),
228 'status' => '', # unprocessed only
233 my $reason = $self->check_chargable( $cdr,
234 'option_cache' => \%opt_cache,
237 warn "not charging for CDR ($reason)\n" if $DEBUG;
242 warn "rating inbound CDR $cdr\n".
243 join('', map { " $_ => ". $cdr->{$_}. "\n" } keys %$cdr );
246 my $seconds = $use_duration ? $cdr->duration : $cdr->billsec;
248 $seconds += $granularity - ( $seconds % $granularity )
249 if $seconds # don't granular-ize 0 billsec calls (bills them)
250 && $granularity; # 0 is per call
251 my $minutes = sprintf("%.1f",$seconds / 60);
252 $minutes =~ s/\.0$// if $granularity == 60; # count whole minutes, convert to integer
253 $minutes = 1 unless $granularity; # per call
255 my $charge_min = $minutes;
258 $included_min -= $minutes;
259 if ( $included_min > 0 ) {
263 $charge_min = 0 - $included_min;
267 $charge = sprintf('%.4f', ( $self->option('min_charge') * $charge_min )
268 + 0.00000001 ); #so 1.00005 rounds to 1.0001
273 $cdr->downstream_csv( 'format' => $output_format,
275 'seconds' => ($use_duration
279 'granularity' => $granularity,
284 detail => $call_details[0],
286 classnum => $cdr->calltypenum, #classnum
287 #phonenum => $self->phonenum,
288 accountcode => $cdr->accountcode,
289 startdate => $cdr->startdate,
290 duration => $seconds,
291 # regionname?? => '', #regionname, not set for inbound calls
295 my $error = $cdr->set_status_and_rated_price( 'done',
299 die $error if $error;
303 unshift @$details, { format => 'C',
304 detail => FS::cdr::invoice_header($output_format),
311 #returns a reason why not to rate this CDR, or false if the CDR is chargeable
312 sub check_chargable {
313 my( $self, $cdr, %flags ) = @_;
315 #should have some better way of checking these options from a hash
326 skip_dstchannel_prefix
330 foreach my $opt (grep !exists($flags{option_cache}->{$_}), @opt ) {
331 $flags{option_cache}->{$opt} = $self->option($opt, 1);
333 my %opt = %{ $flags{option_cache} };
335 return 'amaflags != 2'
336 if $opt{'use_amaflags'} && $cdr->amaflags != 2;
338 return "disposition NOT IN ( $opt{'disposition_in'} )"
339 if $opt{'disposition_in'} =~ /\S/
340 && !grep { $cdr->disposition eq $_ } split(/\s*,\s*/, $opt{'disposition_in'});
342 return "disposition IN ( $opt{'ignore_disposition'} )"
343 if $opt{'ignore_disposition'} =~ /\S/
344 && grep { $cdr->disposition eq $_ } split(/\s*,\s*/, $opt{'ignore_disposition'});
346 return "carrierid != $opt{'use_carrierid'}"
347 if length($opt{'use_carrierid'})
348 && $cdr->carrierid ne $opt{'use_carrierid'}; #ne otherwise 0 matches ''
350 return "cdrtypenum != $opt{'use_cdrtypenum'}"
351 if length($opt{'use_cdrtypenum'})
352 && $cdr->cdrtypenum ne $opt{'use_cdrtypenum'}; #ne otherwise 0 matches ''
354 return "cdrtypenum == $opt{'ignore_cdrtypenum'}"
355 if length($opt{'ignore_cdrtypenum'})
356 && $cdr->cdrtypenum eq $opt{'ignore_cdrtypenum'}; #eq otherwise 0 matches ''
358 return "dcontext IN ( $opt{'skip_dcontext'} )"
359 if $opt{'skip_dcontext'} =~ /\S/
360 && grep { $cdr->dcontext eq $_ } split(/\s*,\s*/, $opt{'skip_dcontext'});
362 my $len_prefix = length($opt{'skip_dstchannel_prefix'});
363 return "dstchannel starts with $opt{'skip_dstchannel_prefix'}"
365 && substr($cdr->dstchannel,0,$len_prefix) eq $opt{'skip_dstchannel_prefix'};
367 my $dst_length = $opt{'skip_dst_length_less'};
368 return "destination less than $dst_length digits"
369 if $dst_length && length($cdr->dst) < $dst_length;
371 return "lastapp is $opt{'skip_lastapp'}"
372 if length($opt{'skip_lastapp'}) && $cdr->lastapp eq $opt{'skip_lastapp'};
374 #all right then, rate it
382 # This equates svc_phone records; perhaps svc_phone should have a field
383 # to indicate it represents a line
385 my($self, $cust_pkg ) = @_;
387 scalar(grep { $_->part_svc->svcdb eq 'svc_phone' } $cust_pkg->cust_svc);