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(@_);
182 my($cust_pkg, $sdate, $details, $param ) = @_;
184 $self->calc_usage(@_);
187 #false laziness w/voip_sqlradacct calc_recur resolve it if that one ever gets used again
191 my($cust_pkg, $sdate, $details, $param ) = @_;
193 #my $last_bill = $cust_pkg->last_bill;
194 my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
197 if $self->recur_temporality eq 'preceding'
198 && ( $last_bill eq '' || $last_bill == 0 );
200 my $spool_cdr = $cust_pkg->cust_main->spool_cdr;
205 # my $downstream_cdr = '';
207 my $included_min = $self->option('min_included', 1) || 0;
208 my $use_duration = $self->option('use_duration');
209 my $output_format = $self->option('output_format', 1) || 'default';
210 my $granularity = length($self->option('sec_granularity'))
211 ? $self->option('sec_granularity')
214 #for check_chargable, so we don't keep looking up options inside the loop
217 my $csv = new Text::CSV_XS;
219 foreach my $cust_svc (
220 grep { $_->part_svc->svcdb eq 'svc_phone' } $cust_pkg->cust_svc
222 my $svc_phone = $cust_svc->svc_x;
224 foreach my $cdr ( $svc_phone->get_cdrs(
226 'default_prefix' => $self->option('default_prefix'),
227 'status' => '', # unprocessed only
232 my $reason = $self->check_chargable( $cdr,
233 'option_cache' => \%opt_cache,
236 warn "not charging for CDR ($reason)\n" if $DEBUG;
241 warn "rating inbound CDR $cdr\n".
242 join('', map { " $_ => ". $cdr->{$_}. "\n" } keys %$cdr );
245 my $seconds = $use_duration ? $cdr->duration : $cdr->billsec;
247 $seconds += $granularity - ( $seconds % $granularity )
248 if $seconds # don't granular-ize 0 billsec calls (bills them)
249 && $granularity; # 0 is per call
250 my $minutes = sprintf("%.1f",$seconds / 60);
251 $minutes =~ s/\.0$// if $granularity == 60; # count whole minutes, convert to integer
252 $minutes = 1 unless $granularity; # per call
254 my $charge_min = $minutes;
257 $included_min -= $minutes;
258 if ( $included_min > 0 ) {
262 $charge_min = 0 - $included_min;
266 $charge = sprintf('%.4f', ( $self->option('min_charge') * $charge_min )
267 + 0.00000001 ); #so 1.00005 rounds to 1.0001
272 $cdr->downstream_csv( 'format' => $output_format,
274 'seconds' => ($use_duration
278 'granularity' => $granularity,
283 detail => $call_details[0],
285 classnum => $cdr->calltypenum, #classnum
286 #phonenum => $self->phonenum,
287 accountcode => $cdr->accountcode,
288 startdate => $cdr->startdate,
289 duration => $seconds,
290 # regionname?? => '', #regionname, not set for inbound calls
294 my $error = $cdr->set_status_and_rated_price( 'done',
298 die $error if $error;
302 unshift @$details, { format => 'C',
303 detail => FS::cdr::invoice_header($output_format),
310 #returns a reason why not to rate this CDR, or false if the CDR is chargeable
311 sub check_chargable {
312 my( $self, $cdr, %flags ) = @_;
314 #should have some better way of checking these options from a hash
325 skip_dstchannel_prefix
329 foreach my $opt (grep !exists($flags{option_cache}->{$_}), @opt ) {
330 $flags{option_cache}->{$opt} = $self->option($opt, 1);
332 my %opt = %{ $flags{option_cache} };
334 return 'amaflags != 2'
335 if $opt{'use_amaflags'} && $cdr->amaflags != 2;
337 return "disposition NOT IN ( $opt{'disposition_in'} )"
338 if $opt{'disposition_in'} =~ /\S/
339 && !grep { $cdr->disposition eq $_ } split(/\s*,\s*/, $opt{'disposition_in'});
341 return "disposition IN ( $opt{'ignore_disposition'} )"
342 if $opt{'ignore_disposition'} =~ /\S/
343 && grep { $cdr->disposition eq $_ } split(/\s*,\s*/, $opt{'ignore_disposition'});
345 return "carrierid != $opt{'use_carrierid'}"
346 if length($opt{'use_carrierid'})
347 && $cdr->carrierid ne $opt{'use_carrierid'}; #ne otherwise 0 matches ''
349 return "cdrtypenum != $opt{'use_cdrtypenum'}"
350 if length($opt{'use_cdrtypenum'})
351 && $cdr->cdrtypenum ne $opt{'use_cdrtypenum'}; #ne otherwise 0 matches ''
353 return "cdrtypenum == $opt{'ignore_cdrtypenum'}"
354 if length($opt{'ignore_cdrtypenum'})
355 && $cdr->cdrtypenum eq $opt{'ignore_cdrtypenum'}; #eq otherwise 0 matches ''
357 return "dcontext IN ( $opt{'skip_dcontext'} )"
358 if $opt{'skip_dcontext'} =~ /\S/
359 && grep { $cdr->dcontext eq $_ } split(/\s*,\s*/, $opt{'skip_dcontext'});
361 my $len_prefix = length($opt{'skip_dstchannel_prefix'});
362 return "dstchannel starts with $opt{'skip_dstchannel_prefix'}"
364 && substr($cdr->dstchannel,0,$len_prefix) eq $opt{'skip_dstchannel_prefix'};
366 my $dst_length = $opt{'skip_dst_length_less'};
367 return "destination less than $dst_length digits"
368 if $dst_length && length($cdr->dst) < $dst_length;
370 return "lastapp is $opt{'skip_lastapp'}"
371 if length($opt{'skip_lastapp'}) && $cdr->lastapp eq $opt{'skip_lastapp'};
373 #all right then, rate it
381 # This equates svc_phone records; perhaps svc_phone should have a field
382 # to indicate it represents a line
384 my($self, $cust_pkg ) = @_;
386 scalar(grep { $_->part_svc->svcdb eq 'svc_phone' } $cust_pkg->cust_svc);