TT#23271 include subs uuids to select prepaid_costs

Change-Id: Ie31623bf9fa822579c9749fd1af691cb7306a37b
changes/26/17026/4
Rene Krenn 8 years ago
parent a2795f0e8a
commit a7fac31974

@ -654,11 +654,14 @@ EOS
) or FATAL "Error preparing prepaid costs count statement: ".$acctdbh->errstr;
$sth_prepaid_cost = $acctdbh->prepare( #call_id index required
"SELECT * FROM accounting.prepaid_costs WHERE call_id = ? order by timestamp asc" # newer entries overwrite older ones
'SELECT * FROM accounting.prepaid_costs WHERE call_id = ? ' .
'AND source_user_id = ? AND destination_user_id = ?' .
'ORDER BY timestamp ASC' # newer entries overwrite older ones
) or FATAL "Error preparing prepaid cost statement: ".$acctdbh->errstr;
$sth_delete_prepaid_cost = $acctdbh->prepare( #call_id index required
"DELETE FROM accounting.prepaid_costs WHERE call_id = ?"
'DELETE FROM accounting.prepaid_costs WHERE call_id = ? ' .
'AND source_user_id = ? AND destination_user_id = ?'
) or FATAL "Error preparing delete prepaid costs statement: ".$acctdbh->errstr;
$sth_delete_old_prepaid = $acctdbh->prepare(
@ -848,8 +851,10 @@ sub lock_contracts {
}
my %user_ids = ();
# callee subscriber contract:
WARNING "empty source_user_id for CDR ID $cdr->{id}" unless length($cdr->{source_user_id}) > 0;
$user_ids{$cdr->{source_user_id}} = 1 if $cdr->{source_user_id} ne "0";
# (onnet) caller subscriber:
WARNING "empty destination_user_id for CDR ID $cdr->{id}" unless length($cdr->{destination_user_id}) > 0;
$user_ids{$cdr->{destination_user_id}} = 1 if $cdr->{destination_user_id} ne "0";
my @uuids = keys %user_ids;
my $uuid_count = scalar @uuids;
@ -2145,9 +2150,20 @@ sub populate_prepaid_cost_cache {
if ($count > $prepaid_costs_cache_limit) {
WARNING "over $prepaid_costs_cache_limit pending prepaid_costs records, too many to preload";
} else {
$prepaid_costs_cache = {};
$sth_prepaid_costs_cache->execute()
or FATAL "Error executing get prepaid costs cache statement: ".$sth_prepaid_costs_cache->errstr;
$prepaid_costs_cache = $sth_prepaid_costs_cache->fetchall_hashref('call_id');
while (my $prepaid_cost = $sth_prepaid_costs_cache->fetchrow_hashref()) {
$prepaid_costs_cache->{$prepaid_cost->{call_id}} //= {};
my $map = $prepaid_costs_cache->{$prepaid_cost->{call_id}};
$map->{$prepaid_cost->{source_user_id}} //= {};
$map = $map->{$prepaid_cost->{source_user_id}};
if (exists $map->{$prepaid_cost->{destination_user_id}}) {
WARNING "duplicate prepaid_costs call_id = $prepaid_cost->{call_id}, source_user_id = $prepaid_cost->{source_user_id}, destination_user_id = $prepaid_cost->{destination_user_id}";
}
$map->{$prepaid_cost->{destination_user_id}} = $prepaid_cost;
}
DEBUG "prepaid_costs cache populated, $count records";
return 1;
}
} else {
@ -2168,17 +2184,23 @@ sub get_prepaid_cost {
my $cdr = shift;
my $entry = undef;
if (defined $prepaid_costs_cache) {
if (exists($prepaid_costs_cache->{$cdr->{call_id}})) {
DEBUG "prepaid cost record for call ID $cdr->{call_id} found in cache";
$entry = $prepaid_costs_cache->{$cdr->{call_id}};
if (exists $prepaid_costs_cache->{$cdr->{call_id}}) {
my $map = $prepaid_costs_cache->{$cdr->{call_id}};
if (exists $map->{$cdr->{source_user_id}}) {
$map = $map->{$cdr->{source_user_id}};
if (exists $map->{$cdr->{destination_user_id}}) {
DEBUG "prepaid_costs call_id = $cdr->{call_id}, source_user_id = $cdr->{source_user_id}, destination_user_id = $cdr->{destination_user_id} found in cache";
$entry = $map->{$cdr->{destination_user_id}};
}
}
}
} else {
$sth_prepaid_cost->execute($cdr->{call_id})
$sth_prepaid_cost->execute($cdr->{call_id},$cdr->{source_user_id},$cdr->{destination_user_id})
or FATAL "Error executing get prepaid cost statement: ".$sth_prepaid_cost->errstr;
my $prepaid_cost = $sth_prepaid_cost->fetchall_hashref('call_id');
if ($prepaid_cost && exists($prepaid_cost->{$cdr->{call_id}})) {
DEBUG "prepaid cost record for call ID $cdr->{call_id} fetched";
$entry = $prepaid_cost->{$cdr->{call_id}};
my $prepaid_cost = $sth_prepaid_cost->fetchall_hashref('destination_user_id');
if ($prepaid_cost && exists $prepaid_cost->{$cdr->{destination_user_id}}) {
DEBUG "prepaid cost record for call ID $cdr->{call_id} retrieved";
$entry = $prepaid_cost->{$cdr->{destination_user_id}};
}
}
return $entry;
@ -2188,14 +2210,33 @@ sub get_prepaid_cost {
sub drop_prepaid_cost {
my $entry = shift;
$sth_delete_prepaid_cost->execute($entry->{call_id})
my $count = $sth_delete_prepaid_cost->execute($entry->{call_id},$entry->{source_user_id},$entry->{destination_user_id})
or FATAL "Error executing delete prepaid cost statement: ".$sth_delete_prepaid_cost->errstr;
if ($count > 1) {
WARNING "multiple prepaid_costs call_id = $entry->{call_id}, source_user_id = $entry->{source_user_id}, destination_user_id = $entry->{destination_user_id} deleted";
} elsif ($count == 1) {
DEBUG "prepaid_costs call_id = $entry->{call_id}, source_user_id = $entry->{source_user_id}, destination_user_id = $entry->{destination_user_id} deleted";
} elsif ($count == 1) {
WARNING "no prepaid_costs call_id = $entry->{call_id}, source_user_id = $entry->{source_user_id}, destination_user_id = $entry->{destination_user_id} deleted";
}
if (defined $prepaid_costs_cache) {
if (delete($prepaid_costs_cache->{$entry->{call_id}})) {
DEBUG "dropped prepaid cost record for call ID $entry->{call_id}";
if (exists $prepaid_costs_cache->{$entry->{call_id}}) {
my $map = $prepaid_costs_cache->{$entry->{call_id}};
if (exists $map->{$entry->{source_user_id}}) {
$map = $map->{$entry->{source_user_id}};
if (exists $map->{$entry->{destination_user_id}}) {
delete $map->{$entry->{destination_user_id}};
my $empty = (scalar keys %$map) == 0;
$map = $prepaid_costs_cache->{$entry->{call_id}};
delete $map->{$entry->{source_user_id}} if $empty;
$empty = (scalar keys %$map) == 0;
delete $prepaid_costs_cache->{$entry->{call_id}} if $empty;
DEBUG "dropped prepaid_costs call_id = $entry->{call_id}, source_user_id = $entry->{source_user_id}, destination_user_id = $entry->{destination_user_id} from cache";
}
}
}
}
return $sth_delete_prepaid_cost->rows;
return $count;
}

@ -319,6 +319,8 @@ sub prepare_prepaid_costs_cdr {
cost => $prepaid_cost,
free_time_used => $prepaid_free_time_used // 0,
timestamp => sprintf("%04d-%02d-%02d %02d:%02d:%02d", $Y + 1900,$m + 1, $d, $H, $M, $S),
source_user_id => $cdr->{source_user_id},
destination_user_id => $cdr->{destination_user_id},
},
};
}

@ -37,10 +37,10 @@ my $provider = Utils::Api::setup_provider('test.com',
my $call_costs = ($provider->{subscriber_fees}->[0]->{fee}->{onpeak_init_rate} *
$provider->{subscriber_fees}->[0]->{fee}->{onpeak_init_interval} +
$provider->{subscriber_fees}->[0]->{fee}->{onpeak_follow_rate} *
$provider->{subscriber_fees}->[0]->{fee}->{onpeak_follow_interval})/100.0;
$provider->{subscriber_fees}->[0]->{fee}->{onpeak_follow_interval});
my $call_count = 3;
my $balance = $call_count * $call_costs;
my $balance = $call_count * $call_costs / 100.0;
my $profiles_setup = $provider->{subscriber_fees}->[0]->{profile};
my $caller = Utils::Api::setup_subscriber($provider,$profiles_setup,$balance,{ cc => 888, ac => '1<n>', sn => '<t>' });
#my $caller2 = Utils::Api::setup_subscriber($provider,$profiles_setup,$balance,{ cc => 888, ac => '1<n>', sn => '<t>' });

@ -17,7 +17,7 @@ use Test::More;
### balance > 0.0/no balance produce correct customer/reseller call cost, cash balance
### and cash balance interval values.
$ENV{RATEOMAT_WRITE_CDR_RELATION_DATA} = 1;
local $ENV{RATEOMAT_WRITE_CDR_RELATION_DATA} = 1;
my $init_secs = 60;
my $follow_secs = 30;

Loading…
Cancel
Save