X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fcdr.pm;h=4126d5f9a2eb0dc4dbb3412443bef8f1855f1a2d;hb=c2f7d8ba623194ad1fae37b231b2e29b33d05674;hp=fedf28aa643db2a06c1912c8e994280b19bae40b;hpb=85ce878a87e2da7ed3e2f927cae7745ef3b59cc0;p=freeside.git diff --git a/FS/FS/cdr.pm b/FS/FS/cdr.pm index fedf28aa6..4126d5f9a 100644 --- a/FS/FS/cdr.pm +++ b/FS/FS/cdr.pm @@ -92,6 +92,8 @@ following fields are currently supported: =item dst_ip_addr - Destination IP address (same) +=item dst_term - Terminating destination number (if different from dst) + =item startdate - Start of call (UNIX-style integer timestamp) =item answerdate - Answer time of call (UNIX-style integer timestamp) @@ -194,6 +196,7 @@ sub table_info { #'lastdata' => '', 'src_ip_addr' => 'Source IP', 'dst_ip_addr' => 'Dest. IP', + 'dst_term' => 'Termination dest.', 'startdate' => 'Start date', 'answerdate' => 'Answer date', 'enddate' => 'End date', @@ -335,7 +338,7 @@ sub check { #check the foreign keys even? #do we want to outright *reject* the CDR? my $error = - $self->ut_numbern('acctid') + $self->ut_numbern('acctid'); #add a config option to turn these back on if someone needs 'em # @@ -347,7 +350,7 @@ sub check { # # # Telstra =1, Optus = 2, RSL COM = 3 # || $self->ut_foreign_keyn('carrierid', 'cdr_carrier', 'carrierid' ) - ; + return $error if $error; $self->SUPER::check; @@ -426,12 +429,25 @@ sub set_charged_party { Sets the status to the provided string. If there is an error, returns the error, otherwise returns false. +If status is being changed from 'rated' to some other status, also removes +any usage allocations to this CDR. + =cut sub set_status { my($self, $status) = @_; + my $old_status = $self->freesidestatus; $self->freesidestatus($status); - $self->replace; + my $error = $self->replace; + if ( $old_status eq 'rated' and $status ne 'done' ) { + # deallocate any usage + foreach (qsearch('cdr_cust_pkg_usage', {acctid => $self->acctid})) { + my $cust_pkg_usage = $_->cust_pkg_usage; + $cust_pkg_usage->set('minutes', $cust_pkg_usage->minutes + $_->minutes); + $error ||= $cust_pkg_usage->replace || $_->delete; + } + } + $error; } =item set_status_and_rated_price STATUS RATED_PRICE [ SVCNUM [ OPTION => VALUE ... ] ] @@ -561,25 +577,19 @@ sub parse_number { Rates this CDR according and sets the status to 'rated'. -Available options are: part_pkg, svcnum, single_price_included_minutes, region_group, region_group_included_minutes. +Available options are: part_pkg, svcnum, plan_included_min, +detail_included_min_hashref. part_pkg is required. If svcnum is specified, will also associate this CDR with the specified svcnum. -single_price_included_minutes is requried for single_price price plans -(otherwise unused/ignored). It should be set to a scalar reference of the -number of included minutes and will be decremented by the rated minutes of this +plan_included_min should be set to a scalar reference of the number of +included minutes and will be decremented by the rated minutes of this CDR. -region_group_included_minutes is required for prefix price plans which have -included minutes (otherwise unused/ignored). It should be set to a scalar -reference of the number of included minutes and will be decremented by the -rated minutes of this CDR. - -region_group_included_minutes_hashref is required for prefix price plans which -have included minues (otehrwise unused/ignored). It should be set to an empty -hashref at the start of a month's rating and then preserved across CDRs. +detail_included_min_hashref should be set to an empty hashref at the +start of a month's rating and then preserved across CDRs. =cut @@ -603,6 +613,7 @@ our %interval_cache = (); # for timed rates sub rate_prefix { my( $self, %opt ) = @_; my $part_pkg = $opt{'part_pkg'} or return "No part_pkg specified"; + my $cust_pkg = $opt{'cust_pkg'}; my $da_rewrote = 0; # this will result in those CDRs being marked as done... is that @@ -630,14 +641,52 @@ sub rate_prefix { ); } + if ( $part_pkg->option_cacheable('skip_same_customer') + and ! $self->is_tollfree ) { + my ($dst_countrycode, $dst_number) = $self->parse_number( + column => 'dst', + international_prefix => $part_pkg->option_cacheable('international_prefix'), + domestic_prefix => $part_pkg->option_cacheable('domestic_prefix'), + ); + my $dst_same_cust = FS::Record->scalar_sql( + 'SELECT COUNT(svc_phone.svcnum) AS count '. + 'FROM cust_pkg ' . + 'JOIN cust_svc USING (pkgnum) ' . + 'JOIN svc_phone USING (svcnum) ' . + 'WHERE svc_phone.countrycode = ' . dbh->quote($dst_countrycode) . + ' AND svc_phone.phonenum = ' . dbh->quote($dst_number) . + ' AND cust_pkg.custnum = ' . $cust_pkg->custnum, + ); + if ( $dst_same_cust > 0 ) { + warn "not charging for CDR (same source and destination customer)\n" if $DEBUG; + return $self->set_status_and_rated_price( 'skipped', + 0, + $opt{'svcnum'}, + ); + } + } + + + ### # look up rate details based on called station id # (or calling station id for toll free calls) ### + my $eff_ratenum = $self->is_tollfree('accountcode') + ? $part_pkg->option_cacheable('accountcode_tollfree_ratenum') + : ''; + my( $to_or_from, $column ); - if ( $self->is_tollfree && ! $part_pkg->option_cacheable('disable_tollfree') ) + if( + ( $self->is_tollfree + && ! $part_pkg->option_cacheable('disable_tollfree') + ) + or ( $eff_ratenum + && $part_pkg->option_cacheable('accountcode_tollfree_field') eq 'src' + ) + ) { #tollfree call $to_or_from = 'from'; $column = 'src'; @@ -658,10 +707,6 @@ sub rate_prefix { #asterisks here causes inserting the detail to barf, so: $pretty_dst =~ s/\*//g; - my $eff_ratenum = $self->is_tollfree('accountcode') - ? $part_pkg->option_cacheable('accountcode_tollfree_ratenum') - : ''; - my $ratename = ''; my $intrastate_ratenum = $part_pkg->option_cacheable('intrastate_ratenum'); if ( $intrastate_ratenum && !$self->is_tollfree ) { @@ -828,11 +873,6 @@ sub rate_prefix { $seconds_left -= $charge_sec; - my $included_min = $opt{'region_group_included_min_hashref'} || {}; - - $included_min->{$regionnum}{$ratetimenum} = $rate_detail->min_included - unless exists $included_min->{$regionnum}{$ratetimenum}; - my $granularity = $rate_detail->sec_granularity; my $minutes; @@ -850,20 +890,51 @@ sub rate_prefix { $seconds += $charge_sec; + if ( $rate_detail->min_included ) { + # the old, kind of deprecated way to do this: + # + # The rate detail itself has included minutes. We MUST have a place + # to track them. + my $included_min = $opt{'detail_included_min_hashref'} + or return "unable to rate CDR: rate detail has included minutes, but ". + "no detail_included_min_hashref provided.\n"; + + # by default, set the included minutes for this region/time to + # what's in the rate_detail + $included_min->{$regionnum}{$ratetimenum} = $rate_detail->min_included + unless exists $included_min->{$regionnum}{$ratetimenum}; + + if ( $included_min->{$regionnum}{$ratetimenum} >= $minutes ) { + $charge_sec = 0; + $included_min->{$regionnum}{$ratetimenum} -= $minutes; + } else { + $charge_sec -= ($included_min->{$regionnum}{$ratetimenum} * 60); + $included_min->{$regionnum}{$ratetimenum} = 0; + } + } elsif ( $opt{plan_included_min} && ${ $opt{plan_included_min} } > 0 ) { + # The package definition has included minutes, but only for in-group + # rate details. Decrement them if this is an in-group call. + if ( $rate_detail->region_group ) { + if ( ${ $opt{'plan_included_min'} } >= $minutes ) { + $charge_sec = 0; + ${ $opt{'plan_included_min'} } -= $minutes; + } else { + $charge_sec -= (${ $opt{'plan_included_min'} } * 60); + ${ $opt{'plan_included_min'} } = 0; + } + } + } else { + # the new way! + my $applied_min = $cust_pkg->apply_usage( + 'cdr' => $self, + 'rate_detail' => $rate_detail, + 'minutes' => $minutes + ); + # for now, usage pools deal only in whole minutes + $charge_sec -= $applied_min * 60; + } - my $region_group = ($part_pkg->option_cacheable('min_included') || 0) > 0; - - ${$opt{region_group_included_min}} -= $minutes - if $region_group && $rate_detail->region_group; - - $included_min->{$regionnum}{$ratetimenum} -= $minutes; - if ( - $included_min->{$regionnum}{$ratetimenum} <= 0 - && ( ${$opt{region_group_included_min}} <= 0 - || ! $rate_detail->region_group - ) - ) - { + if ( $charge_sec > 0 ) { #NOW do connection charges here... right? #my $conn_seconds = min($seconds_left, $rate_detail->conn_sec); @@ -876,16 +947,13 @@ sub rate_prefix { } #should preserve (display?) this - my $charge_min = 0 - $included_min->{$regionnum}{$ratetimenum} - ( $conn_seconds / 60 ); - $included_min->{$regionnum}{$ratetimenum} = 0; - $charge += ($rate_detail->min_charge * $charge_min) if $charge_min > 0; #still not rounded - - } elsif ( ${$opt{region_group_included_min}} > 0 - && $region_group - && $rate_detail->region_group - ) - { - $included_min->{$regionnum}{$ratetimenum} = 0 + if ( $granularity == 0 ) { # per call rate + $charge += $rate_detail->min_charge; + } else { + my $charge_min = ( $charge_sec - $conn_seconds ) / 60; + $charge += ($rate_detail->min_charge * $charge_min) if $charge_min > 0; #still not rounded + } + } # choose next rate_detail @@ -902,9 +970,15 @@ sub rate_prefix { # this is why we need regionnum/rate_region.... warn " (rate region $rate_region)\n" if $DEBUG; + # NOW round it. + my $rounding = $part_pkg->option_cacheable('rounding') || 2; + my $sprintformat = '%.'. $rounding. 'f'; + my $roundup = 10**(-3-$rounding); + my $price = sprintf($sprintformat, $charge + $roundup); + $self->set_status_and_rated_price( 'rated', - sprintf('%.2f', $charge + 0.000001), # NOW round it. + $price, $opt{'svcnum'}, 'rated_pretty_dst' => $pretty_dst, 'rated_regionname' => $rate_region->regionname, @@ -925,6 +999,8 @@ sub rate_upstream_simple { sprintf('%.3f', $self->upstream_price), $opt{'svcnum'}, 'rated_classnum' => $self->calltypenum, + 'rated_seconds' => $self->billsec, + # others? upstream_*_regionname => rated_regionname is possible ); } @@ -951,12 +1027,12 @@ sub rate_single_price { my $charge_min = $minutes; - ${$opt{single_price_included_min}} -= $minutes; - if ( ${$opt{single_price_included_min}} > 0 ) { + ${$opt{plan_included_min}} -= $minutes; + if ( ${$opt{plan_included_min}} > 0 ) { $charge_min = 0; } else { - $charge_min = 0 - ${$opt{single_price_included_min}}; - ${$opt{single_price_included_min}} = 0; + $charge_min = 0 - ${$opt{plan_included_min}}; + ${$opt{plan_included_min}} = 0; } my $charge = @@ -1082,6 +1158,8 @@ sub calltypename { =cut +# in the future, load this dynamically from detail_format classes + my %export_names = ( 'simple' => { 'name' => 'Simple', @@ -1100,6 +1178,10 @@ my %export_names = ( 'name' => 'Basic', 'invoice_header' => "Date/Time,Called Number,Min/Sec,Price", }, + 'basic_upstream_dst_regionname' => { + 'name' => 'Basic with upstream destination name', + 'invoice_header' => "Date/Time,Called Number,Destination,Min/Sec,Price", + }, 'default' => { 'name' => 'Default', 'invoice_header' => 'Date,Time,Number,Destination,Duration,Price', @@ -1128,6 +1210,10 @@ my %export_names = ( 'name' => 'Summary, one line per destination prefix', 'invoice_header' => 'Caller,Rate,Calls,Minutes,Price', }, + 'sum_count_class' => { + 'name' => 'Summary, one line per usage class', + 'invoice_header' => 'Caller,Class,Calls,Price', + }, ); my %export_formats = (); @@ -1173,6 +1259,8 @@ sub export_formats { length($price) ? ($opt{money_char} . $price) : ''; }; + my $src_sub = sub { $_[0]->clid || $_[0]->src }; + %export_formats = ( 'simple' => [ sub { time2str($date_format, shift->calldate_unix ) }, #DATE @@ -1187,7 +1275,7 @@ sub export_formats { sub { time2str($date_format, shift->calldate_unix ) }, #DATE sub { time2str('%r', shift->calldate_unix ) }, #TIME #'userfield', #USER - 'src', #called from + $src_sub, #called from 'dst', #NUMBER_DIALED $duration_sub, #DURATION #sub { sprintf('%.3f', shift->upstream_price ) }, #PRICE @@ -1196,7 +1284,7 @@ sub export_formats { 'accountcode_simple' => [ sub { time2str($date_format, shift->calldate_unix ) }, #DATE sub { time2str('%r', shift->calldate_unix ) }, #TIME - 'src', #called from + $src_sub, #called from 'accountcode', #NUMBER_DIALED $duration_sub, #DURATION $price_sub, @@ -1204,14 +1292,14 @@ sub export_formats { 'sum_duration' => [ # for summary formats, the CDR is a fictitious object containing the # total billsec and the phone number of the service - 'src', + $src_sub, sub { my($cdr, %opt) = @_; $opt{ratename} }, sub { my($cdr, %opt) = @_; $opt{count} }, sub { my($cdr, %opt) = @_; int($opt{seconds}/60).'m' }, $price_sub, ], 'sum_count' => [ - 'src', + $src_sub, sub { my($cdr, %opt) = @_; $opt{ratename} }, sub { my($cdr, %opt) = @_; $opt{count} }, $price_sub, @@ -1245,7 +1333,7 @@ sub export_formats { $price_sub, ], ); - $export_formats{'source_default'} = [ 'src', @{ $export_formats{'default'} }, ]; + $export_formats{'source_default'} = [ $src_sub, @{ $export_formats{'default'} }, ]; $export_formats{'accountcode_default'} = [ @{ $export_formats{'default'} }[0,1], 'accountcode', @@ -1253,7 +1341,7 @@ sub export_formats { ]; my @default = @{ $export_formats{'default'} }; $export_formats{'description_default'} = - [ 'src', @default[0..2], + [ $src_sub, @default[0..2], sub { my($cdr, %opt) = @_; $cdr->description }, @default[4,5] ]; @@ -1586,9 +1674,15 @@ my %import_options = ( keys %cdr_info }, - 'format_row_callbacks' => { map { $_ => $cdr_info{$_}->{'row_callback'}; } - keys %cdr_info - }, + 'format_row_callbacks' => + { map { $_ => $cdr_info{$_}->{'row_callback'}; } + keys %cdr_info + }, + + 'format_parser_opts' => + { map { $_ => $cdr_info{$_}->{'parser_opt'}; } + keys %cdr_info + }, ); sub _import_options {