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' => [ '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 '.
38 'add_full_period'=> { 'name' => 'When prorating first month, also bill '.
39 'for one full period after that',
43 'recur_method' => { 'name' => 'Recurring fee method',
45 'select_options' => \%FS::part_pkg::recur_Common::recur_method,
48 'min_charge' => { 'name' => 'Charge per minute',
51 'min_included' => { 'name' => 'Minutes included',
54 'sec_granularity' => { 'name' => 'Granularity',
56 'select_options' => \%granularity,
59 'default_prefix' => { 'name' => 'Default prefix optionally prepended to customer DID numbers when searching for CDR records',
63 'disable_tollfree' => { 'name' => 'Disable automatic toll-free processing',
67 'use_amaflags' => { 'name' => 'Do not charge for CDRs where the amaflags field is not set to "2" ("BILL"/"BILLING").',
71 'use_disposition' => { 'name' => 'Do not charge for CDRs where the disposition flag is not set to "ANSWERED".',
75 'use_disposition_taqua' => { 'name' => 'Do not charge for CDRs where the disposition is not set to "100" (Taqua).',
79 'use_carrierid' => { 'name' => 'Do not charge for CDRs where the Carrier ID is not set to: ',
82 'use_cdrtypenum' => { 'name' => 'Do not charge for CDRs where the CDR Type is not set to: ',
85 'ignore_cdrtypenum' => { 'name' => 'Do not charge for CDRs where the CDR Type is set to: ',
88 'ignore_disposition' => { 'name' => 'Do not charge for CDRs where the Disposition is set to any of these (comma-separated) values: ',
91 'skip_dcontext' => { 'name' => 'Do not charge for CDRs where the dcontext is set to any of these (comma-separated) values:',
94 'skip_dstchannel_prefix' => { 'name' => 'Do not charge for CDRs where the dstchannel starts with:',
97 'skip_dst_length_less' => { 'name' => 'Do not charge for CDRs where the destination is less than this many digits:',
100 'skip_lastapp' => { 'name' => 'Do not charge for CDRs where the lastapp matches this value',
103 'use_duration' => { 'name' => 'Calculate usage based on the duration field instead of the billsec field',
104 'type' => 'checkbox',
107 #false laziness w/cdr_termination.pm
108 'output_format' => { 'name' => 'CDR invoice display format',
110 'select_options' => { FS::cdr::invoice_formats() },
111 'default' => 'default', #XXX test
114 'usage_section' => { 'name' => 'Section in which to place usage charges (whether separated or not)',
117 'summarize_usage' => { 'name' => 'Include usage summary with recurring charges when usage is in separate section',
118 'type' => 'checkbox',
121 'usage_mandate' => { 'name' => 'Always put usage details in separate section',
122 'type' => 'checkbox',
126 'bill_every_call' => { 'name' => 'Generate an invoice immediately for every call. Useful for prepaid.',
127 'type' => 'checkbox',
130 #XXX also have option for an external db
131 # 'cdr_location' => { 'name' => 'CDR database location'
132 # 'type' => 'select',
133 # 'select_options' => \%cdr_location,
134 # 'select_callback' => {
136 # 'enable' => [ 'datasrc', 'username', 'password' ],
139 # 'disable' => [ 'datasrc', 'username', 'password' ],
143 # 'datasrc' => { 'name' => 'DBI data source for external CDR table',
146 # 'username' => { 'name' => 'External database username',
149 # 'password' => { 'name' => 'External database password',
156 recur_method cutoff_day add_full_period
157 min_charge min_included sec_granularity
160 use_amaflags use_disposition
161 use_disposition_taqua use_carrierid
162 use_cdrtypenum ignore_cdrtypenum
164 skip_dcontext skip_dstchannel_prefix
165 skip_dst_length_less skip_lastapp
167 output_format usage_mandate summarize_usage usage_section
176 my $str = $self->SUPER::price_info;
177 $str .= " plus usage" if $str;
182 my($self, $cust_pkg ) = @_;
183 $self->option('setup_fee');
188 my($cust_pkg, $sdate, $details, $param ) = @_;
192 $charges += $self->calc_usage(@_);
193 $charges += $self->calc_recur_Common(@_);
201 my($cust_pkg, $sdate, $details, $param ) = @_;
203 $self->calc_usage(@_);
206 #false laziness w/voip_sqlradacct calc_recur resolve it if that one ever gets used again
210 my($cust_pkg, $sdate, $details, $param ) = @_;
212 #my $last_bill = $cust_pkg->last_bill;
213 my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
216 if $self->option('recur_temporality', 1) eq 'preceding'
217 && ( $last_bill eq '' || $last_bill == 0 );
219 my $spool_cdr = $cust_pkg->cust_main->spool_cdr;
221 my $included_min = ($self->option('min_included')
222 && $self->option('min_included') > 0)
223 ? $self->option('min_included') : 0;
227 # my $downstream_cdr = '';
229 my $disable_tollfree = $self->option('disable_tollfree');
230 my $ignore_unrateable = $self->option('ignore_unrateable', 'Hush!');
231 my $use_duration = $self->option('use_duration');
233 my $output_format = $self->option('output_format', 'Hush!') || 'default';
235 #for check_chargable, so we don't keep looking up options inside the loop
238 eval "use Text::CSV_XS;";
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;
247 foreach my $cdr ( $svc_phone->get_cdrs(
249 'status' => '', # unprocessed only
250 'default_prefix' => $self->option('default_prefix'),
255 warn "rating inbound CDR $cdr\n".
256 join('', map { " $_ => ". $cdr->{$_}. "\n" } keys %$cdr );
258 my $granularity = length($self->option('sec_granularity'))
259 ? $self->option('sec_granularity')
262 my $seconds = $use_duration ? $cdr->duration : $cdr->billsec;
264 $seconds += $granularity - ( $seconds % $granularity )
265 if $seconds # don't granular-ize 0 billsec calls (bills them)
266 && $granularity; # 0 is per call
267 my $minutes = sprintf("%.1f",$seconds / 60);
268 $minutes =~ s/\.0$// if $granularity == 60; # count whole minutes, convert to integer
269 $minutes = 1 unless $granularity; # per call
271 my $charge_min = $minutes;
274 $included_min -= $minutes;
275 if ( $included_min > 0 ) {
279 $charge_min = 0 - $included_min;
283 $charge = sprintf('%.2f', ( $self->option('min_charge') * $charge_min )
284 + 0.00000001 ); #so 1.00005 rounds to 1.0001
289 $cdr->downstream_csv( 'format' => $output_format,
291 'seconds' => ($use_duration
295 'granularity' => $granularity,
302 $cdr->calltypenum, #classnum
305 '', #regionname, not set for inbound calls
309 my $error = $cdr->set_status_and_rated_price( 'done',
313 die $error if $error;
317 unshift @$details, [ 'C',
318 FS::cdr::invoice_header($output_format),
330 #returns a reason why not to rate this CDR, or false if the CDR is chargeable
331 sub check_chargable {
332 my( $self, $cdr, %flags ) = @_;
334 #should have some better way of checking these options from a hash
340 use_disposition_taqua
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 != ANSWERED'
359 if $opt{'use_disposition'} && $cdr->disposition ne 'ANSWERED';
361 return "disposition != 100"
362 if $opt{'use_disposition_taqua'} && $cdr->disposition != 100;
364 return "disposition IN ( $opt{'ignore_disposition'} )"
365 if $opt{'ignore_disposition'} =~ /\S/
366 && grep { $cdr->disposition eq $_ } split(/\s*,\s*/, $opt{'ignore_disposition'});
368 return "carrierid != $opt{'use_carrierid'}"
369 if length($opt{'use_carrierid'})
370 && $cdr->carrierid ne $opt{'use_carrierid'} #ne otherwise 0 matches ''
371 && ! $flags{'da_rewrote'};
373 return "cdrtypenum != $opt{'use_cdrtypenum'}"
374 if length($opt{'use_cdrtypenum'})
375 && $cdr->cdrtypenum ne $opt{'use_cdrtypenum'}; #ne otherwise 0 matches ''
377 return "cdrtypenum == $opt{'ignore_cdrtypenum'}"
378 if length($opt{'ignore_cdrtypenum'})
379 && $cdr->cdrtypenum eq $opt{'ignore_cdrtypenum'}; #eq otherwise 0 matches ''
381 return "dcontext IN ( $opt{'skip_dcontext'} )"
382 if $opt{'skip_dcontext'} =~ /\S/
383 && grep { $cdr->dcontext eq $_ } split(/\s*,\s*/, $opt{'skip_dcontext'});
385 my $len_prefix = length($opt{'skip_dstchannel_prefix'});
386 return "dstchannel starts with $opt{'skip_dstchannel_prefix'}"
388 && substr($cdr->dstchannel,0,$len_prefix) eq $opt{'skip_dstchannel_prefix'};
390 my $dst_length = $opt{'skip_dst_length_less'};
391 return "destination less than $dst_length digits"
392 if $dst_length && length($cdr->dst) < $dst_length;
394 return "lastapp is $opt{'skip_lastapp'}"
395 if length($opt{'skip_lastapp'}) && $cdr->lastapp eq $opt{'skip_lastapp'};
397 #all right then, rate it
405 # This equates svc_phone records; perhaps svc_phone should have a field
406 # to indicate it represents a line
408 my($self, $cust_pkg ) = @_;
410 scalar(grep { $_->part_svc->svcdb eq 'svc_phone' } $cust_pkg->cust_svc);