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)',
28 'setup_fee' => { 'name' => 'Setup fee for this package',
31 'recur_fee' => { 'name' => 'Base recurring fee for this package',
35 #false laziness w/flat.pm
36 'recur_temporality' => { 'name' => 'Charge recurring fee for period',
38 'select_options' => \%temporalities,
41 'unused_credit' => { 'name' => 'Credit the customer for the unused portion'.
42 ' of service at cancellation',
46 'cutoff_day' => { 'name' => 'Billing Day (1 - 28) for prorating or '.
51 'recur_method' => { 'name' => 'Recurring fee method',
53 'select_options' => \%FS::part_pkg::recur_Common::recur_method,
56 'min_charge' => { 'name' => 'Charge per minute',
59 'sec_granularity' => { 'name' => 'Granularity',
61 'select_options' => \%granularity,
64 'default_prefix' => { 'name' => 'Default prefix optionally prepended to customer DID numbers when searching for CDR records',
68 'disable_tollfree' => { 'name' => 'Disable automatic toll-free processing',
72 'use_amaflags' => { 'name' => 'Do not charge for CDRs where the amaflags field is not set to "2" ("BILL"/"BILLING").',
76 'use_disposition' => { 'name' => 'Do not charge for CDRs where the disposition flag is not set to "ANSWERED".',
80 'use_disposition_taqua' => { 'name' => 'Do not charge for CDRs where the disposition is not set to "100" (Taqua).',
84 'use_carrierid' => { 'name' => 'Do not charge for CDRs where the Carrier ID is not set to: ',
87 'use_cdrtypenum' => { 'name' => 'Do not charge for CDRs where the CDR Type is not set to: ',
90 'skip_dcontext' => { 'name' => 'Do not charge for CDRs where the dcontext is set to any of these (comma-separated) values:',
93 'skip_dstchannel_prefix' => { 'name' => 'Do not charge for CDRs where the dstchannel starts with:',
96 'skip_dst_length_less' => { 'name' => 'Do not charge for CDRs where the destination is less than this many digits:',
99 'skip_lastapp' => { 'name' => 'Do not charge for CDRs where the lastapp matches this value',
102 'use_duration' => { 'name' => 'Calculate usage based on the duration field instead of the billsec field',
103 'type' => 'checkbox',
106 #false laziness w/cdr_termination.pm
107 'output_format' => { 'name' => 'CDR invoice display format',
109 'select_options' => { FS::cdr::invoice_formats() },
110 'default' => 'default', #XXX test
113 'usage_section' => { 'name' => 'Section in which to place usage charges (whether separated or not)',
116 'summarize_usage' => { 'name' => 'Include usage summary with recurring charges when usage is in separate section',
117 'type' => 'checkbox',
120 'usage_mandate' => { 'name' => 'Always put usage details in separate section',
121 'type' => 'checkbox',
125 'bill_every_call' => { 'name' => 'Generate an invoice immediately for every call. Useful for prepaid.',
126 'type' => 'checkbox',
129 #XXX also have option for an external db
130 # 'cdr_location' => { 'name' => 'CDR database location'
131 # 'type' => 'select',
132 # 'select_options' => \%cdr_location,
133 # 'select_callback' => {
135 # 'enable' => [ 'datasrc', 'username', 'password' ],
138 # 'disable' => [ 'datasrc', 'username', 'password' ],
142 # 'datasrc' => { 'name' => 'DBI data source for external CDR table',
145 # 'username' => { 'name' => 'External database username',
148 # 'password' => { 'name' => 'External database password',
154 setup_fee recur_fee recur_temporality unused_credit
155 recur_method cutoff_day
156 min_charge sec_granularity
159 use_amaflags use_disposition
160 use_disposition_taqua use_carrierid use_cdrtypenum
161 skip_dcontext skip_dstchannel_prefix
162 skip_dst_length_less skip_lastapp
164 output_format usage_mandate summarize_usage usage_section
172 my($self, $cust_pkg ) = @_;
173 $self->option('setup_fee');
178 my($cust_pkg, $sdate, $details, $param ) = @_;
182 $charges += $self->calc_usage(@_);
183 $charges += $self->calc_recur_Common(@_);
191 my($cust_pkg, $sdate, $details, $param ) = @_;
193 $self->calc_usage(@_);
196 #false laziness w/voip_sqlradacct calc_recur resolve it if that one ever gets used again
200 my($cust_pkg, $sdate, $details, $param ) = @_;
202 #my $last_bill = $cust_pkg->last_bill;
203 my $last_bill = $cust_pkg->get('last_bill'); #->last_bill falls back to setup
206 if $self->option('recur_temporality', 1) eq 'preceding'
207 && ( $last_bill eq '' || $last_bill == 0 );
209 my $spool_cdr = $cust_pkg->cust_main->spool_cdr;
211 my %included_min = ();
215 # my $downstream_cdr = '';
217 my $disable_tollfree = $self->option('disable_tollfree');
218 my $ignore_unrateable = $self->option('ignore_unrateable', 'Hush!');
219 my $use_duration = $self->option('use_duration');
221 my $output_format = $self->option('output_format', 'Hush!') || 'default';
223 #for check_chargable, so we don't keep looking up options inside the loop
226 eval "use Text::CSV_XS;";
228 my $csv = new Text::CSV_XS;
230 foreach my $cust_svc (
231 grep { $_->part_svc->svcdb eq 'svc_phone' } $cust_pkg->cust_svc
233 my $svc_phone = $cust_svc->svc_x;
235 foreach my $cdr ( $svc_phone->get_cdrs(
237 'status' => '', # unprocessed only
238 'default_prefix' => $self->option('default_prefix'),
243 warn "rating inbound CDR $cdr\n".
244 join('', map { " $_ => ". $cdr->{$_}. "\n" } keys %$cdr );
246 my $granularity = length($self->option('sec_granularity'))
247 ? $self->option('sec_granularity')
250 my $seconds = $use_duration ? $cdr->duration : $cdr->billsec;
252 $seconds += $granularity - ( $seconds % $granularity )
253 if $seconds # don't granular-ize 0 billsec calls (bills them)
254 && $granularity; # 0 is per call
255 my $minutes = sprintf("%.1f",$seconds / 60);
256 $minutes =~ s/\.0$// if $granularity == 60; # count whole minutes, convert to integer
257 $minutes = 1 unless $granularity; # per call
258 my $charge = sprintf('%.2f', ( $self->option('min_charge') * $minutes )
259 + 0.00000001 ); #so 1.00005 rounds to 1.0001
262 my @call_details = ($cdr->downstream_csv( 'format' => $output_format,
264 'minutes' => $minutes,
265 'granularity' => $granularity,
272 $cdr->calltypenum, #classnum
275 '', #regionname, not set for inbound calls
278 my $error = $cdr->set_status_and_rated_price( 'done',
282 die $error if $error;
286 unshift @$details, [ 'C',
287 FS::cdr::invoice_header($output_format),
299 #returns a reason why not to rate this CDR, or false if the CDR is chargeable
300 sub check_chargable {
301 my( $self, $cdr, %flags ) = @_;
303 #should have some better way of checking these options from a hash
309 use_disposition_taqua
313 skip_dstchannel_prefix
317 foreach my $opt (grep !exists($flags{option_cache}->{$_}), @opt ) {
318 $flags{option_cache}->{$opt} = $self->option($opt, 1);
320 my %opt = %{ $flags{option_cache} };
322 return 'amaflags != 2'
323 if $opt{'use_amaflags'} && $cdr->amaflags != 2;
325 return 'disposition != ANSWERED'
326 if $opt{'use_disposition'} && $cdr->disposition ne 'ANSWERED';
328 return "disposition != 100"
329 if $opt{'use_disposition_taqua'} && $cdr->disposition != 100;
331 return "carrierid != $opt{'use_carrierid'}"
332 if length($opt{'use_carrierid'})
333 && $cdr->carrierid ne $opt{'use_carrierid'} #ne otherwise 0 matches ''
334 && ! $flags{'da_rewrote'};
336 return "cdrtypenum != $opt{'use_cdrtypenum'}"
337 if length($opt{'use_cdrtypenum'})
338 && $cdr->cdrtypenum ne $opt{'use_cdrtypenum'}; #ne otherwise 0 matches ''
340 return "dcontext IN ( $opt{'skip_dcontext'} )"
341 if $opt{'skip_dcontext'} =~ /\S/
342 && grep { $cdr->dcontext eq $_ } split(/\s*,\s*/, $opt{'skip_dcontext'});
344 my $len_prefix = length($opt{'skip_dstchannel_prefix'});
345 return "dstchannel starts with $opt{'skip_dstchannel_prefix'}"
347 && substr($cdr->dstchannel,0,$len_prefix) eq $opt{'skip_dstchannel_prefix'};
349 my $dst_length = $opt{'skip_dst_length_less'};
350 return "destination less than $dst_length digits"
351 if $dst_length && length($cdr->dst) < $dst_length;
353 return "lastapp is $opt{'skip_lastapp'}"
354 if length($opt{'skip_lastapp'}) && $cdr->lastapp eq $opt{'skip_lastapp'};
356 #all right then, rate it
364 # This equates svc_phone records; perhaps svc_phone should have a field
365 # to indicate it represents a line
367 my($self, $cust_pkg ) = @_;
369 scalar(grep { $_->part_svc->svcdb eq 'svc_phone' } $cust_pkg->cust_svc);