diff --git a/bin/ngcp-fraud-auto-lock b/bin/ngcp-fraud-auto-lock index 1f2408f..5325ba7 100755 --- a/bin/ngcp-fraud-auto-lock +++ b/bin/ngcp-fraud-auto-lock @@ -17,7 +17,7 @@ my %LOCK = ( 2 => 'outgoing', 3 => 'incoming', 4 => 'global', -); +); my $conf = Sipwise::Provisioning::Config->new()->get_config(); @@ -29,38 +29,40 @@ my $db = $o->{database}; # However, we select all account hits and manually check them later, as they # can potentially override billing profile hits. my $a = $db->sql_get_all_arrayref(<<"!"); - SELECT 'profile_limit' as type, c.id, p.fraud_interval_lock, p.fraud_interval_notify, - b.cash_balance_interval, p.fraud_interval_limit - FROM contracts c, billing_profiles p, contract_balances b, billing_mappings bm, - products pr - WHERE bm.id = (SELECT m.id - FROM billing_mappings m - WHERE ( m.start_date IS NULL - OR m.start_date <= now()) - AND ( m.end_date IS NULL - OR m.end_date >= now()) - AND m.contract_id = c.id - ORDER BY m.start_date desc limit 1) - AND p.id = bm.billing_profile_id - AND pr.id = bm.product_id - AND (pr.class IN ('sipaccount', 'pbxaccount') OR pr.class IS NULL) - AND b.contract_id = c.id - AND b.start <= now() - AND b.end >= now() - AND c.status = 'active' - AND p.fraud_interval_limit > 0 - AND (p.fraud_interval_lock - OR p.fraud_interval_notify <> '') - AND b.cash_balance_interval >= p.fraud_interval_limit - UNION ALL - SELECT 'account_limit' as type, c.id, afp.fraud_interval_lock, afp.fraud_interval_notify, - b.cash_balance_interval, afp.fraud_interval_limit - FROM contracts c, contract_fraud_preferences afp, contract_balances b - WHERE b.contract_id = c.id - AND afp.contract_id = c.id - AND b.start <= now() - AND b.end >= now() - AND c.status = 'active' +SELECT bpinfo.type, bpinfo.id, + IF (bpinfo.fraud_use_reseller_rates > 0, SUM(cdr.source_reseller_cost), + SUM(cdr.source_customer_cost)) as interval_cost, + bpinfo.fraud_interval_limit, bpinfo.fraud_interval_lock, bpinfo.fraud_interval_notify +FROM ( + SELECT contracts.id, bp.fraud_use_reseller_rates, + CASE WHEN cfp.fraud_interval_limit > 0 THEN 'account_limit' + ELSE 'profile_limit' END as type, + IF (cfp.fraud_interval_limit > 0, cfp.fraud_interval_limit, bp.fraud_interval_limit) as fraud_interval_limit, + IF (cfp.fraud_interval_limit > 0, cfp.fraud_interval_lock, bp.fraud_interval_lock) as fraud_interval_lock, + IF (cfp.fraud_interval_limit > 0, cfp.fraud_interval_notify, bp.fraud_interval_notify) as fraud_interval_notify + FROM billing.contracts + JOIN billing.billing_profiles bp + ON (bp.id = + (SELECT m.billing_profile_id + FROM billing.billing_mappings m + JOIN products pr ON pr.id = m.product_id + WHERE ((m.start_date IS NULL) OR (m.start_date <= NOW())) + AND ((m.end_date IS NULL) OR (m.end_date >= NOW())) + AND (m.contract_id = contracts.id) + AND (pr.class IN ('sipaccount', 'pbxaccount') OR pr.class IS NULL) + ORDER BY m.start_date DESC LIMIT 1 + ) + ) + LEFT JOIN billing.contract_fraud_preferences cfp ON cfp.contract_id = contracts.id + WHERE (contracts.status = 'active') + AND (cfp.fraud_interval_limit > 0 OR bp.fraud_interval_limit > 0) +) as bpinfo +JOIN accounting.cdr ON cdr.source_account_id = bpinfo.id +WHERE cdr.start_time BETWEEN UNIX_TIMESTAMP(DATE_FORMAT(NOW(), '%Y-%m-01 00:00:00')) + AND UNIX_TIMESTAMP(DATE_FORMAT(NOW() + INTERVAL 1 MONTH, '%Y-%m-01 00:00:00'))-1 +GROUP BY bpinfo.id +HAVING interval_cost >= fraud_interval_limit + AND (fraud_interval_lock > 0 OR fraud_interval_notify <> '') ! my $x = {}; @@ -69,8 +71,8 @@ for my $e (@{ $a }) { if($x->{$e->{id}}->{type} eq 'profile_limit' && $e->{type} eq 'account_limit') { # if account limit hits and it has lock and/or notify, mark for action - if(defined $e->{fraud_interval_limit} and - int($e->{cash_balance_interval}) >= int($e->{fraud_interval_limit}) and + if(defined $e->{fraud_interval_limit} and + int($e->{interval_cost}) >= int($e->{fraud_interval_limit}) and ($e->{fraud_interval_lock} || $e->{fraud_interval_notify})) { $x->{$e->{id}} = $e; @@ -85,7 +87,7 @@ for my $e (@{ $a }) { # if account or billing profile limit hits and it has lock and/or notify, # mark for action if(defined $e->{fraud_interval_limit} and - int($e->{cash_balance_interval}) >= int($e->{fraud_interval_limit}) and + int($e->{interval_cost}) >= int($e->{fraud_interval_limit}) and ($e->{fraud_interval_lock} || $e->{fraud_interval_notify})) { $x->{$e->{id}} = $e; @@ -108,7 +110,7 @@ for my $e (values %{ $x }) { AND s.status != 'terminated' ! - my $cur = sprintf('%.2f', $e->{cash_balance_interval} / 100); + my $cur = sprintf('%.2f', $e->{interval_cost} / 100); my $max = sprintf('%.2f', $e->{fraud_interval_limit} / 100); my ($subject, $body); diff --git a/bin/ngcp-fraud-daily-lock b/bin/ngcp-fraud-daily-lock index c29ffea..3ed54d3 100755 --- a/bin/ngcp-fraud-daily-lock +++ b/bin/ngcp-fraud-daily-lock @@ -17,7 +17,7 @@ my %LOCK = ( 2 => 'outgoing', 3 => 'incoming', 4 => 'global', -); +); my $conf = Sipwise::Provisioning::Config->new()->get_config(); @@ -26,10 +26,11 @@ my $db = $o->{database}; my $a = $db->sql_get_all_arrayref(<<"!"); SELECT bpinfo.type, bpinfo.id, - SUM(cdr.source_customer_cost) as daily_cost, + IF (bpinfo.fraud_use_reseller_rates > 0, SUM(cdr.source_reseller_cost), + SUM(cdr.source_customer_cost)) as daily_cost, bpinfo.fraud_daily_limit, bpinfo.fraud_daily_lock, bpinfo.fraud_daily_notify FROM ( - SELECT contracts.id, + SELECT contracts.id, bp.fraud_use_reseller_rates, CASE WHEN cfp.fraud_daily_limit > 0 THEN 'account_limit' ELSE 'profile_limit' END as type, IF (cfp.fraud_daily_limit > 0, cfp.fraud_daily_limit, bp.fraud_daily_limit) as fraud_daily_limit, @@ -54,7 +55,7 @@ JOIN accounting.cdr ON cdr.source_account_id = bpinfo.id WHERE cdr.start_time BETWEEN UNIX_TIMESTAMP(DATE_FORMAT(NOW(), '%Y-%m-%d 00:00:00')) AND UNIX_TIMESTAMP(DATE_FORMAT(NOW(), '%Y-%m-%d 23:59:59')) GROUP BY bpinfo.id -HAVING daily_cost > fraud_daily_limit +HAVING daily_cost >= fraud_daily_limit ! my $x = {};