X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=FS%2FFS%2Fpart_pkg%2Fvoip_tiered.pm;h=0ad0ff6bf81e5bc28169f4a91b647f3cdd7163a1;hp=c1382fb457f4118cf348b11ce9735a8c740a5d6d;hb=f072dc42c97981167acae2e987a4e4e6a6a4d403;hpb=8291a2d7cc38b1fcbe081da2bd78d4250a993250 diff --git a/FS/FS/part_pkg/voip_tiered.pm b/FS/FS/part_pkg/voip_tiered.pm index c1382fb45..0ad0ff6bf 100644 --- a/FS/FS/part_pkg/voip_tiered.pm +++ b/FS/FS/part_pkg/voip_tiered.pm @@ -81,6 +81,7 @@ sub calc_usage { && ( $last_bill eq '' || $last_bill == 0 ); my $included_min = $self->option('min_included', 1) || 0; + $included_min *= ($cust_pkg->quantity || 1); my $cdr_svc_method = $self->option('cdr_svc_method',1)||'svc_phone.phonenum'; my $cdr_inout = ($cdr_svc_method eq 'svc_phone.phonenum') && $self->option('cdr_inout',1) @@ -98,6 +99,7 @@ sub calc_usage { my %options = ( 'disable_src' => $self->option('disable_src'), 'default_prefix' => $self->option('default_prefix'), + 'cdrtypenum' => $self->option('use_cdrtypenum'), 'status' => '', 'for_update' => 1, ); # $last_bill, $$sdate ) @@ -131,9 +133,11 @@ sub calc_usage { $options{'inbound'} = ( $pass eq 'inbound' ); - foreach my $cdr ( - $svc_x->get_cdrs( %options ) - ) { + my $cdr_search = $svc_x->psearch_cdrs(%options); + $cdr_search->limit(1000); + $cdr_search->increment(0); + while ( my $cdr = $cdr_search->fetch ) { + if ( $DEBUG > 1 ) { warn "rating CDR $cdr\n". join('', map { " $_ => ". $cdr->{$_}. "\n" } keys %$cdr ); @@ -172,6 +176,8 @@ sub calc_usage { $total += $charge_min; + $cdr_search->adjust(1) if $cdr->freesidestatus eq ''; + } # $cdr } # $pass @@ -190,10 +196,9 @@ sub calc_usage { my $output_format = $self->option('output_format', 'Hush!') || 'default'; - my $csv = new Text::CSV_XS; + my $formatter = FS::detail_format->new($output_format, buffer => $details); my $charges = 0; - my @invoice_details_sort; $options{'status'} = 'processing-tiered'; @@ -210,10 +215,13 @@ sub calc_usage { foreach my $pass (split('_', $cdr_inout)) { $options{'inbound'} = ( $pass eq 'inbound' ); + # tell the formatter what we're sending it + $formatter->inbound($options{'inbound'}); - foreach my $cdr ( - $svc_x->get_cdrs( %options ) - ) { + my $cdr_search = $svc_x->psearch_cdrs(%options); + $cdr_search->limit(1000); + $cdr_search->increment(0); + while ( my $cdr = $cdr_search->fetch ) { my $object = $options{'inbound'} ? $cdr->cdr_termination( 1 ) #1: inbound @@ -226,31 +234,6 @@ sub calc_usage { if ( $charge > 0 ) { $charges += $charge; - - my $detail = $self->sum_usage ? '' : - $cdr->downstream_csv( 'format' => $output_format, - 'charge' => $charge, - 'seconds' => ($use_duration ? - $cdr->duration : - $cdr->billsec), - 'granularity' => $granularity, - ); - - my $call_details = - { format => 'C', - detail => $detail, - amount => $charge, - #classnum => $cdr->calltypenum, #classnum - #phonenum => $phonenum, #XXX need this to sort on them - accountcode => $cdr->accountcode, - startdate => $cdr->startdate, - duration => $object->rated_seconds, - }; - - #warn " adding details on charge to invoice: [ ". - # join(', ', @{$call_details} ). " ]" - # if ( $DEBUG && ref($call_details) ); - push @invoice_details_sort, [ $call_details, $cdr->calldate_unix ]; } my $error = $cdr->set_status_and_rated_price( @@ -263,32 +246,18 @@ sub calc_usage { ); die $error if $error; + $formatter->append($cdr); + + $cdr_search->adjust(1) if $cdr->freesidestatus eq 'processing-tiered'; + } # $cdr } # $pass - if ( $self->sum_usage ) { - # then summarize all accumulated details within this svc_x - # and then flush them - push @$details, $self->sum_detail($svc_x, \@invoice_details_sort); - @invoice_details_sort = (); - } - } # $cust_svc - if ( !$self->sum_usage ) { - #sort them - my @sorted_invoice_details = - sort { ${$a}[1] <=> ${$b}[1] } @invoice_details_sort; - foreach my $sorted_call_detail ( @sorted_invoice_details ) { - push @$details, ${$sorted_call_detail}[0]; - } - } - - unshift @$details, { format => 'C', - detail => FS::cdr::invoice_header($output_format), - } - if @$details; + $formatter->finish; + unshift @$details, $formatter->header if @$details; $charges; }