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 'sec_granularity' => { 'name' => 'Granularity',
53 'select_options' => \%granularity,
56 'default_prefix' => { 'name' => 'Default prefix optionally prepended to customer DID numbers when searching for CDR records',
60 'disable_tollfree' => { 'name' => 'Disable automatic toll-free processing',
64 'use_amaflags' => { 'name' => 'Do not charge for CDRs where the amaflags field is not set to "2" ("BILL"/"BILLING").',
68 'use_disposition' => { 'name' => 'Do not charge for CDRs where the disposition flag is not set to "ANSWERED".',
72 'use_disposition_taqua' => { 'name' => 'Do not charge for CDRs where the disposition is not set to "100" (Taqua).',
76 'use_carrierid' => { 'name' => 'Do not charge for CDRs where the Carrier ID is not set to: ',
79 'use_cdrtypenum' => { 'name' => 'Do not charge for CDRs where the CDR Type is not set to: ',
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 add_full_period
148 min_charge sec_granularity
151 use_amaflags use_disposition
152 use_disposition_taqua use_carrierid use_cdrtypenum
153 skip_dcontext skip_dstchannel_prefix
154 skip_dst_length_less skip_lastapp
156 output_format usage_mandate summarize_usage usage_section
164 my($self, $cust_pkg ) = @_;
165 $self->option('setup_fee');
170 my($cust_pkg, $sdate, $details, $param ) = @_;
174 $charges += $self->calc_usage(@_);
175 $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->option('recur_temporality', 1) eq 'preceding'
199 && ( $last_bill eq '' || $last_bill == 0 );
201 my $spool_cdr = $cust_pkg->cust_main->spool_cdr;
203 my %included_min = ();
207 # my $downstream_cdr = '';
209 my $disable_tollfree = $self->option('disable_tollfree');
210 my $ignore_unrateable = $self->option('ignore_unrateable', 'Hush!');
211 my $use_duration = $self->option('use_duration');
213 my $output_format = $self->option('output_format', 'Hush!') || 'default';
215 #for check_chargable, so we don't keep looking up options inside the loop
218 eval "use Text::CSV_XS;";
220 my $csv = new Text::CSV_XS;
222 foreach my $cust_svc (
223 grep { $_->part_svc->svcdb eq 'svc_phone' } $cust_pkg->cust_svc
225 my $svc_phone = $cust_svc->svc_x;
227 foreach my $cdr ( $svc_phone->get_cdrs(
229 'status' => '', # unprocessed only
230 'default_prefix' => $self->option('default_prefix'),
235 warn "rating inbound CDR $cdr\n".
236 join('', map { " $_ => ". $cdr->{$_}. "\n" } keys %$cdr );
238 my $granularity = length($self->option('sec_granularity'))
239 ? $self->option('sec_granularity')
242 my $seconds = $use_duration ? $cdr->duration : $cdr->billsec;
244 $seconds += $granularity - ( $seconds % $granularity )
245 if $seconds # don't granular-ize 0 billsec calls (bills them)
246 && $granularity; # 0 is per call
247 my $minutes = sprintf("%.1f",$seconds / 60);
248 $minutes =~ s/\.0$// if $granularity == 60; # count whole minutes, convert to integer
249 $minutes = 1 unless $granularity; # per call
250 my $charge = sprintf('%.2f', ( $self->option('min_charge') * $minutes )
251 + 0.00000001 ); #so 1.00005 rounds to 1.0001
254 my @call_details = ($cdr->downstream_csv( 'format' => $output_format,
256 'minutes' => $minutes,
257 'granularity' => $granularity,
264 $cdr->calltypenum, #classnum
267 '', #regionname, not set for inbound calls
270 my $error = $cdr->set_status_and_rated_price( 'done',
274 die $error if $error;
278 unshift @$details, [ 'C',
279 FS::cdr::invoice_header($output_format),
291 #returns a reason why not to rate this CDR, or false if the CDR is chargeable
292 sub check_chargable {
293 my( $self, $cdr, %flags ) = @_;
295 #should have some better way of checking these options from a hash
301 use_disposition_taqua
305 skip_dstchannel_prefix
309 foreach my $opt (grep !exists($flags{option_cache}->{$_}), @opt ) {
310 $flags{option_cache}->{$opt} = $self->option($opt, 1);
312 my %opt = %{ $flags{option_cache} };
314 return 'amaflags != 2'
315 if $opt{'use_amaflags'} && $cdr->amaflags != 2;
317 return 'disposition != ANSWERED'
318 if $opt{'use_disposition'} && $cdr->disposition ne 'ANSWERED';
320 return "disposition != 100"
321 if $opt{'use_disposition_taqua'} && $cdr->disposition != 100;
323 return "carrierid != $opt{'use_carrierid'}"
324 if length($opt{'use_carrierid'})
325 && $cdr->carrierid ne $opt{'use_carrierid'} #ne otherwise 0 matches ''
326 && ! $flags{'da_rewrote'};
328 return "cdrtypenum != $opt{'use_cdrtypenum'}"
329 if length($opt{'use_cdrtypenum'})
330 && $cdr->cdrtypenum ne $opt{'use_cdrtypenum'}; #ne otherwise 0 matches ''
332 return "dcontext IN ( $opt{'skip_dcontext'} )"
333 if $opt{'skip_dcontext'} =~ /\S/
334 && grep { $cdr->dcontext eq $_ } split(/\s*,\s*/, $opt{'skip_dcontext'});
336 my $len_prefix = length($opt{'skip_dstchannel_prefix'});
337 return "dstchannel starts with $opt{'skip_dstchannel_prefix'}"
339 && substr($cdr->dstchannel,0,$len_prefix) eq $opt{'skip_dstchannel_prefix'};
341 my $dst_length = $opt{'skip_dst_length_less'};
342 return "destination less than $dst_length digits"
343 if $dst_length && length($cdr->dst) < $dst_length;
345 return "lastapp is $opt{'skip_lastapp'}"
346 if length($opt{'skip_lastapp'}) && $cdr->lastapp eq $opt{'skip_lastapp'};
348 #all right then, rate it
356 # This equates svc_phone records; perhaps svc_phone should have a field
357 # to indicate it represents a line
359 my($self, $cust_pkg ) = @_;
361 scalar(grep { $_->part_svc->svcdb eq 'svc_phone' } $cust_pkg->cust_svc);