From ffec7b3b5431db8fd5cdab919c21d11c693f14f6 Mon Sep 17 00:00:00 2001 From: Kirill Solomko Date: Sun, 4 Aug 2019 23:59:56 +0200 Subject: [PATCH] TT#63545 add cdr_period_costs, rework contract_fraud_events * add support for new table cdr_period_costs * contract_fraud_events virtual view is reworked. It now works with cdr_period_costs table instead of cdr * contract_fraud_events virtual view does not require the interval parameter anymore Change-Id: I67aac0314b303633834b3b2f1bb2e436986d0ce7 --- .gitignore | 2 + lib/NGCP/Schema/Result/cdr_period_costs.pm | 320 ++++++++++++++++++ .../Schema/Result/contract_fraud_events.pm | 58 ++-- 3 files changed, 359 insertions(+), 21 deletions(-) create mode 100644 lib/NGCP/Schema/Result/cdr_period_costs.pm diff --git a/.gitignore b/.gitignore index 97dc5e2b..4d316653 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ Build MYMETA.json MYMETA.yml +.tags +.remote-sync.json diff --git a/lib/NGCP/Schema/Result/cdr_period_costs.pm b/lib/NGCP/Schema/Result/cdr_period_costs.pm new file mode 100644 index 00000000..c303ef74 --- /dev/null +++ b/lib/NGCP/Schema/Result/cdr_period_costs.pm @@ -0,0 +1,320 @@ +package NGCP::Schema::Result::cdr_period_costs; + +use strict; +use warnings; + +use Scalar::Util qw(blessed); +use parent 'DBIx::Class::Core'; + +our $VERSION = '2.007'; + +__PACKAGE__->load_components("InflateColumn::DateTime", "Helper::Row::ToJSON"); + +__PACKAGE__->table("accounting.cdr_period_costs"); + +__PACKAGE__->add_columns( + "id", + { + data_type => "integer", + extra => { unsigned => 1 }, + is_nullable => 0, + is_auto_increment => 1, + }, + "contract_id", + { + data_type => "integer", + extra => { unsigned => 1 }, + is_nullable => 0, + }, + "period", + { + data_type => "enum", + extra => { list => ["month", "day"] }, + is_nullable => 0, + }, + "period_date", + { + data_type => "datetime", + is_nullable => 0, + }, + "direction", + { + data_type => "enum", + default_value => "out", + extra => { list => ["out", "in"] }, + is_nullable => 0, + }, + "customer_cost", + { data_type => "decimal", + default_value => "0.000000", + size => [14, 6], + is_nullable => 0, + }, + "reseller_cost", + { data_type => "decimal", + default_value => "0.000000", + size => [14, 6], + is_nullable => 0, + }, + "cdr_count", + { + data_type => "integer", + default_value => 0, + extra => { unsigned => 1 }, + is_nullable => 0, + }, + "fraud_limit_exceeded", + { + data_type => "tinyint", + is_nullable => 1, + }, + "fraud_limit_type", + { + data_type => "enum", + extra => { list => ["contract", "billing_profile"] }, + is_nullable => 1, + }, + "notify_status", + { + data_type => "enum", + default_value => "new", + extra => { list => ["new", "notified"] }, + is_nullable => 0, + }, + "notified_at", + { + data_type => "datetime", + is_nullable => 1, + }, + "first_cdr_start_time", + { data_type => "decimal", + size => [13, 3], + is_nullable => 0, + }, + "first_cdr_start_id", + { + data_type => "integer", + extra => { unsigned => 1 }, + is_nullable => 0, + }, + "last_cdr_start_time", + { data_type => "decimal", + size => [13, 3], + is_nullable => 0, + }, + "last_cdr_start_id", + { + data_type => "integer", + extra => { unsigned => 1 }, + is_nullable => 0, + }, +); + +__PACKAGE__->set_primary_key('id'); + +__PACKAGE__->belongs_to( + "contract", + "NGCP::Schema::Result::contracts", + { id => "contract_id" }, + { cascade_copy => 0, cascade_delete => 0 }, +); + +__PACKAGE__->belongs_to( + "first_cdr_start_id", + "NGCP::Schema::Result::cdr", + { id => "first_cdr_start_id" }, + { cascade_copy => 0, cascade_delete => 0 }, +); + +__PACKAGE__->belongs_to( + "last_cdr_start_id", + "NGCP::Schema::Result::cdr", + { id => "last_cdr_start_id" }, + { cascade_copy => 0, cascade_delete => 0 }, +); + +sub TO_JSON { + my ($self) = @_; + return { + map { blessed($_) && $_->isa('DateTime') ? $_->datetime : $_ } %{ $self->next::method } + }; +} + +1; + +__END__ + +=encoding UTF-8 + +=head1 NAME + +NGCP::Schema::Result::cdr_period_costs + +=head1 DESCRIPTION + +This module is a schema class for the NGCP database table "accouning.cdr_period_costs". + +=head1 COMPONENTS LOADED + +=over 4 + +=item * L + +=item * L + +=back + +=head1 TABLE: C + +=head1 ACCESSORS + +=head2 id + + data_type: 'integer' + extra: {unsigned => 1} + is_nullable: 0 + is_auto_increment: 1 + +=head2 contract_id + + data_type: 'integer' + extra: {unsigned => 1} + is_nullable: 0 + +=head2 period + + data_type: "enum" + extra: { list => ["month", "day"] } + is_nullable: 0 + +=head2 period_date + + data_type: "datetime" + is_nullable: 0 + +=head2 direction + + data_type: "enum" + default_value: "out" + extra: { list => ["out", "in"] } + is_nullable: 0 + +=head2 customer_cost + + data_type: "decimal" + default_value: "0.000000" + size: [14, 6] + is_nullable: 0 + +=head2 reseller_cost + + data_type: "decimal" + default_value: "0.000000" + size: [14, 6] + is_nullable: 0 + +=head2 cdr_count + + data_type: 'integer' + default_value: 0 + extra: {unsigned => 1} + is_nullable: 0 + +=head2 fraud_limit_exceeded + + data_type: 'tinyint' + is_nullable: 1 + +=head2 fraud_limit_type + + data_type: "enum" + extra: { list => ["contract", "billing_profile"] } + is_nullable: 1 + +=head2 notify_status + + data_type: "enum" + default_value: "new" + extra: { list => ["new", "notified"] } + is_nullable: 0 + +=head2 notified_at + + data_type: 'datetime' + is_nullable: 1 + +=head2 first_cdr_start_time + + data_type: "decimal" + size: [13, 3] + is_nullable: 0 + +=head2 first_cdr_start_id + + data_type: 'integer' + extra: {unsigned => 1} + is_nullable: 0 + +=head2 last_cdr_start_time + + data_type: "decimal" + size: [13, 3] + is_nullable: 0 + +=head2 last_cdr_start_id + + data_type: 'integer' + extra: {unsigned => 1} + is_nullable: 0 + +=head1 PRIMARY KEY + +=over 4 + +=item * L +=item * L +=item * L +=item * L + +=back + +=head1 RELATIONS + +=head2 contact + +Type: belongs_to + +Related object: L + +=head2 first_cdr_start_time + +Type: belongs_to + +Related object: L + +=head2 last_cdr_start_time + +Type: belongs_to + +Related object: L + +=head1 AUTHOR + +Sipwise Development Team C<< >> + +=head1 LICENSE + +This software is Copyright © 2019 by Sipwise GmbH, Austria. + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this package. If not, see . diff --git a/lib/NGCP/Schema/Result/contract_fraud_events.pm b/lib/NGCP/Schema/Result/contract_fraud_events.pm index 132532fc..359119a4 100644 --- a/lib/NGCP/Schema/Result/contract_fraud_events.pm +++ b/lib/NGCP/Schema/Result/contract_fraud_events.pm @@ -36,6 +36,12 @@ __PACKAGE__->add_columns( size => 64, is_nullable => 0, }, + "interval_date", + { + data_type => "varchar", + size => 10, + is_nullable => 0, + }, "type", { data_type => "varchar", @@ -74,6 +80,18 @@ __PACKAGE__->add_columns( is_nullable => 0, extra => { unsigned => 1 }, }, + "notify_status", + { + data_type => "enum", + default_value => "new", + extra => { list => ["new", "notified"] }, + is_nullable => 0, + }, + "notified_at", + { + data_type => "datetime", + is_nullable => 1, + }, ); __PACKAGE__->set_primary_key("id"); @@ -88,59 +106,57 @@ sub TO_JSON { __PACKAGE__->result_source_instance->is_virtual(1); __PACKAGE__->result_source_instance->view_definition(< 0, SUM(cdr.source_reseller_cost), - SUM(cdr.source_customer_cost)) as interval_cost, +SELECT bpinfo.id, bpinfo.reseller_id, + bpinfo.period as `interval`, bpinfo.period_date as 'interval_date', bpinfo.type, + IF (bpinfo.fraud_use_reseller_rates > 0, bpinfo.reseller_cost, + bpinfo.customer_cost) as interval_cost, bpinfo.interval_limit, bpinfo.interval_lock, bpinfo.interval_notify, - bpinfo.fraud_use_reseller_rates as use_reseller_rates + bpinfo.fraud_use_reseller_rates as use_reseller_rates, + bpinfo.notify_status, + bpinfo.notified_at FROM ( - SELECT c.id, n.reseller_id, bp.fraud_use_reseller_rates, i.interval, - IF (i.interval = 'month', + SELECT c.id, n.reseller_id, bp.fraud_use_reseller_rates, + cpc.period, cpc.period_date, + cpc.reseller_cost, cpc.customer_cost, + cpc.notify_status, cpc.notified_at, + IF (cpc.period = 'month', IF (cfp.fraud_interval_limit > 0, 'account_limit', 'profile_limit'), IF (cfp.fraud_daily_limit > 0, 'account_limit', 'profile_limit') ) AS type, - IF (i.interval = 'month', + IF (cpc.period = 'month', IF (cfp.fraud_interval_limit > 0, cfp.fraud_interval_limit, bp.fraud_interval_limit), IF (cfp.fraud_daily_limit > 0, cfp.fraud_daily_limit, bp.fraud_daily_limit) ) AS interval_limit, - IF (i.interval = 'month', + IF (cpc.period = 'month', IF (cfp.fraud_interval_limit > 0, cfp.fraud_interval_lock, bp.fraud_interval_lock), IF (cfp.fraud_daily_limit > 0, cfp.fraud_daily_lock, bp.fraud_daily_lock) ) AS interval_lock, - IF (i.interval = 'month', + IF (cpc.period = 'month', IF (cfp.fraud_interval_limit > 0, cfp.fraud_interval_notify, bp.fraud_interval_notify), IF (cfp.fraud_daily_limit > 0, cfp.fraud_daily_notify, bp.fraud_daily_notify) ) AS interval_notify - FROM (SELECT IF(? = 'month','month','day') AS 'interval') i, - billing.contracts c + FROM billing.contracts c JOIN billing.v_actual_billing_profiles bp_actual ON bp_actual.contract_id = c.id JOIN billing.billing_profiles bp ON bp.id = bp_actual.billing_profile_id JOIN billing.contacts n ON n.id = c.contact_id JOIN billing.resellers r ON r.id = n.reseller_id + JOIN accounting.cdr_period_costs cpc ON cpc.contract_id = c.id LEFT JOIN billing.contract_fraud_preferences cfp ON cfp.contract_id = c.id WHERE c.status = 'active' + AND cpc.fraud_limit_exceeded = 1 + AND cpc.direction = 'out' HAVING interval_limit > 0 ) AS bpinfo -JOIN accounting.cdr ON cdr.source_account_id = bpinfo.id -WHERE CASE WHEN bpinfo.interval = 'month' - THEN 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 - ELSE cdr.start_time - BETWEEN UNIX_TIMESTAMP(DATE_FORMAT(NOW(), '%Y-%m-%d 00:00:00')) - AND UNIX_TIMESTAMP(DATE_FORMAT(NOW() + INTERVAL 1 DAY, '%Y-%m-%d 00:00:00'))-1 - END -GROUP BY bpinfo.id HAVING interval_cost >= interval_limit SQL