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
changes/79/32179/4
Kirill Solomko 6 years ago
parent a9793bbb58
commit ffec7b3b54

2
.gitignore vendored

@ -3,3 +3,5 @@
Build Build
MYMETA.json MYMETA.json
MYMETA.yml MYMETA.yml
.tags
.remote-sync.json

@ -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<DBIx::Class::InflateColumn::DateTime>
=item * L<DBIx::Class::Helper::Row::ToJSON>
=back
=head1 TABLE: C<accounting.cdr_period_costs>
=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</contract_id>
=item * L</period>
=item * L</period_date>
=item * L</direction>
=back
=head1 RELATIONS
=head2 contact
Type: belongs_to
Related object: L<NGCP::Schema::Result::contacts>
=head2 first_cdr_start_time
Type: belongs_to
Related object: L<NGCP::Schema::Result::cdr>
=head2 last_cdr_start_time
Type: belongs_to
Related object: L<NGCP::Schema::Result::cdr>
=head1 AUTHOR
Sipwise Development Team C<< <support@sipwise.com> >>
=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 <https://www.gnu.org/licenses/>.

@ -36,6 +36,12 @@ __PACKAGE__->add_columns(
size => 64, size => 64,
is_nullable => 0, is_nullable => 0,
}, },
"interval_date",
{
data_type => "varchar",
size => 10,
is_nullable => 0,
},
"type", "type",
{ {
data_type => "varchar", data_type => "varchar",
@ -74,6 +80,18 @@ __PACKAGE__->add_columns(
is_nullable => 0, is_nullable => 0,
extra => { unsigned => 1 }, 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"); __PACKAGE__->set_primary_key("id");
@ -88,59 +106,57 @@ sub TO_JSON {
__PACKAGE__->result_source_instance->is_virtual(1); __PACKAGE__->result_source_instance->is_virtual(1);
__PACKAGE__->result_source_instance->view_definition(<<SQL); __PACKAGE__->result_source_instance->view_definition(<<SQL);
SELECT bpinfo.id, bpinfo.reseller_id, bpinfo.interval, bpinfo.type, SELECT bpinfo.id, bpinfo.reseller_id,
IF (bpinfo.fraud_use_reseller_rates > 0, SUM(cdr.source_reseller_cost), bpinfo.period as `interval`, bpinfo.period_date as 'interval_date', bpinfo.type,
SUM(cdr.source_customer_cost)) as interval_cost, IF (bpinfo.fraud_use_reseller_rates > 0, bpinfo.reseller_cost,
bpinfo.customer_cost) as interval_cost,
bpinfo.interval_limit, bpinfo.interval_limit,
bpinfo.interval_lock, bpinfo.interval_lock,
bpinfo.interval_notify, 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 ( FROM (
SELECT c.id, n.reseller_id, bp.fraud_use_reseller_rates, i.interval, SELECT c.id, n.reseller_id, bp.fraud_use_reseller_rates,
IF (i.interval = 'month', 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, IF (cfp.fraud_interval_limit > 0,
'account_limit', 'profile_limit'), 'account_limit', 'profile_limit'),
IF (cfp.fraud_daily_limit > 0, IF (cfp.fraud_daily_limit > 0,
'account_limit', 'profile_limit') 'account_limit', 'profile_limit')
) AS type, ) AS type,
IF (i.interval = 'month', IF (cpc.period = 'month',
IF (cfp.fraud_interval_limit > 0, IF (cfp.fraud_interval_limit > 0,
cfp.fraud_interval_limit, bp.fraud_interval_limit), cfp.fraud_interval_limit, bp.fraud_interval_limit),
IF (cfp.fraud_daily_limit > 0, IF (cfp.fraud_daily_limit > 0,
cfp.fraud_daily_limit, bp.fraud_daily_limit) cfp.fraud_daily_limit, bp.fraud_daily_limit)
) AS interval_limit, ) AS interval_limit,
IF (i.interval = 'month', IF (cpc.period = 'month',
IF (cfp.fraud_interval_limit > 0, IF (cfp.fraud_interval_limit > 0,
cfp.fraud_interval_lock, bp.fraud_interval_lock), cfp.fraud_interval_lock, bp.fraud_interval_lock),
IF (cfp.fraud_daily_limit > 0, IF (cfp.fraud_daily_limit > 0,
cfp.fraud_daily_lock, bp.fraud_daily_lock) cfp.fraud_daily_lock, bp.fraud_daily_lock)
) AS interval_lock, ) AS interval_lock,
IF (i.interval = 'month', IF (cpc.period = 'month',
IF (cfp.fraud_interval_limit > 0, IF (cfp.fraud_interval_limit > 0,
cfp.fraud_interval_notify, bp.fraud_interval_notify), cfp.fraud_interval_notify, bp.fraud_interval_notify),
IF (cfp.fraud_daily_limit > 0, IF (cfp.fraud_daily_limit > 0,
cfp.fraud_daily_notify, bp.fraud_daily_notify) cfp.fraud_daily_notify, bp.fraud_daily_notify)
) AS interval_notify ) AS interval_notify
FROM (SELECT IF(? = 'month','month','day') AS 'interval') i, FROM billing.contracts c
billing.contracts c
JOIN billing.v_actual_billing_profiles bp_actual ON bp_actual.contract_id = c.id 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.billing_profiles bp ON bp.id = bp_actual.billing_profile_id
JOIN billing.contacts n ON n.id = c.contact_id JOIN billing.contacts n ON n.id = c.contact_id
JOIN billing.resellers r ON r.id = n.reseller_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 LEFT JOIN billing.contract_fraud_preferences cfp ON cfp.contract_id = c.id
WHERE c.status = 'active' WHERE c.status = 'active'
AND cpc.fraud_limit_exceeded = 1
AND cpc.direction = 'out'
HAVING interval_limit > 0 HAVING interval_limit > 0
) AS bpinfo ) 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 HAVING interval_cost >= interval_limit
SQL SQL

Loading…
Cancel
Save