Change-Id: I738868ae7e8e0e9b665464596aac86ec30856238changes/52/19152/14
parent
b8111d9d91
commit
f8f1e28349
@ -0,0 +1,144 @@
|
||||
package NGCP::BulkProcessor::Dao::Trunk::billing::profile_packages;
|
||||
use strict;
|
||||
|
||||
## no critic
|
||||
|
||||
use NGCP::BulkProcessor::ConnectorPool qw(
|
||||
get_billing_db
|
||||
|
||||
);
|
||||
|
||||
use NGCP::BulkProcessor::SqlProcessor qw(
|
||||
checktableinfo
|
||||
copy_row
|
||||
);
|
||||
use NGCP::BulkProcessor::SqlRecord qw();
|
||||
|
||||
require Exporter;
|
||||
our @ISA = qw(Exporter NGCP::BulkProcessor::SqlRecord);
|
||||
our @EXPORT_OK = qw(
|
||||
gettablename
|
||||
check_table
|
||||
|
||||
findby_id
|
||||
findall
|
||||
|
||||
$CARRY_OVER_MODE
|
||||
$CARRY_OVER_TIMELY_MODE
|
||||
$DISCARD_MODE
|
||||
$DEFAULT_CARRY_OVER_MODE
|
||||
$DEFAULT_INITIAL_BALANCE
|
||||
);
|
||||
|
||||
my $tablename = 'profile_packages';
|
||||
my $get_db = \&get_billing_db;
|
||||
|
||||
my $expected_fieldnames = [
|
||||
'id',
|
||||
'reseller_id',
|
||||
'name',
|
||||
'description',
|
||||
'initial_balance',
|
||||
'service_charge',
|
||||
'balance_interval_unit',
|
||||
'balance_interval_value',
|
||||
'balance_interval_start_mode',
|
||||
'carry_over_mode',
|
||||
'timely_duration_unit',
|
||||
'timely_duration_value',
|
||||
'notopup_discard_intervals',
|
||||
'underrun_lock_threshold',
|
||||
'underrun_lock_level',
|
||||
'underrun_profile_threshold',
|
||||
'topup_lock_level',
|
||||
];
|
||||
|
||||
my $indexes = {};
|
||||
|
||||
our $CARRY_OVER_MODE = 'carry_over';
|
||||
our $CARRY_OVER_TIMELY_MODE = 'carry_over_timely';
|
||||
our $DISCARD_MODE = 'discard';
|
||||
our $DEFAULT_CARRY_OVER_MODE = $CARRY_OVER_MODE;
|
||||
our $DEFAULT_INITIAL_BALANCE = 0.0;
|
||||
|
||||
sub new {
|
||||
|
||||
my $class = shift;
|
||||
my $self = NGCP::BulkProcessor::SqlRecord->new($class,$get_db,
|
||||
$tablename,$expected_fieldnames,$indexes);
|
||||
|
||||
copy_row($self,shift,$expected_fieldnames);
|
||||
|
||||
return $self;
|
||||
|
||||
}
|
||||
|
||||
sub findby_id {
|
||||
|
||||
my ($id,$load_recursive) = @_;
|
||||
|
||||
check_table();
|
||||
my $db = &$get_db();
|
||||
my $table = $db->tableidentifier($tablename);
|
||||
|
||||
my $stmt = 'SELECT * FROM ' . $table . ' WHERE ' .
|
||||
$db->columnidentifier('id') . ' = ?';
|
||||
my @params = ($id);
|
||||
my $rows = $db->db_get_all_arrayref($stmt,@params);
|
||||
|
||||
return buildrecords_fromrows($rows,$load_recursive)->[0];
|
||||
|
||||
}
|
||||
|
||||
sub findall {
|
||||
|
||||
my ($load_recursive) = @_;
|
||||
|
||||
check_table();
|
||||
my $db = &$get_db();
|
||||
my $table = $db->tableidentifier($tablename);
|
||||
|
||||
my $stmt = 'SELECT * FROM ' . $table;
|
||||
my $rows = $db->db_get_all_arrayref($stmt);
|
||||
|
||||
return buildrecords_fromrows($rows,$load_recursive);
|
||||
|
||||
}
|
||||
|
||||
sub buildrecords_fromrows {
|
||||
|
||||
my ($rows,$load_recursive) = @_;
|
||||
|
||||
my @records = ();
|
||||
my $record;
|
||||
|
||||
if (defined $rows and ref $rows eq 'ARRAY') {
|
||||
foreach my $row (@$rows) {
|
||||
$record = __PACKAGE__->new($row);
|
||||
|
||||
# transformations go here ...
|
||||
|
||||
push @records,$record;
|
||||
}
|
||||
}
|
||||
|
||||
return \@records;
|
||||
|
||||
}
|
||||
|
||||
sub gettablename {
|
||||
|
||||
return $tablename;
|
||||
|
||||
}
|
||||
|
||||
sub check_table {
|
||||
|
||||
return checktableinfo($get_db,
|
||||
__PACKAGE__,$tablename,
|
||||
$expected_fieldnames,
|
||||
$indexes);
|
||||
|
||||
}
|
||||
|
||||
1;
|
||||
@ -0,0 +1,232 @@
|
||||
package NGCP::BulkProcessor::Dao::Trunk::billing::topup_log;
|
||||
use strict;
|
||||
|
||||
## no critic
|
||||
|
||||
use NGCP::BulkProcessor::Logging qw(
|
||||
getlogger
|
||||
rowinserted
|
||||
);
|
||||
|
||||
use NGCP::BulkProcessor::ConnectorPool qw(
|
||||
get_billing_db
|
||||
|
||||
);
|
||||
|
||||
use NGCP::BulkProcessor::SqlProcessor qw(
|
||||
checktableinfo
|
||||
copy_row
|
||||
|
||||
insert_record
|
||||
);
|
||||
use NGCP::BulkProcessor::SqlRecord qw();
|
||||
|
||||
require Exporter;
|
||||
our @ISA = qw(Exporter NGCP::BulkProcessor::SqlRecord);
|
||||
our @EXPORT_OK = qw(
|
||||
gettablename
|
||||
check_table
|
||||
|
||||
insert_row
|
||||
|
||||
findby_contractidfromto
|
||||
findby_contractbalanceid
|
||||
findby_id
|
||||
|
||||
$OK_OUTCOME
|
||||
$FAILED_OUTCOME
|
||||
|
||||
$VOUCHER_TYPE
|
||||
$CASH_TYPE
|
||||
$SET_BALANCE_TYPE
|
||||
);
|
||||
|
||||
my $tablename = 'topup_log';
|
||||
my $get_db = \&get_billing_db;
|
||||
|
||||
my $expected_fieldnames = [
|
||||
'id',
|
||||
'username',
|
||||
'timestamp',
|
||||
'type',
|
||||
'outcome',
|
||||
'message',
|
||||
'subscriber_id',
|
||||
'contract_id',
|
||||
'amount',
|
||||
'voucher_id',
|
||||
'cash_balance_before',
|
||||
'cash_balance_after',
|
||||
'package_before_id',
|
||||
'package_after_id',
|
||||
'profile_before_id',
|
||||
'profile_after_id',
|
||||
'lock_level_before',
|
||||
'lock_level_after',
|
||||
'contract_balance_before_id',
|
||||
'contract_balance_after_id',
|
||||
'request_token',
|
||||
];
|
||||
|
||||
my $indexes = {};
|
||||
|
||||
my $insert_unique_fields = [];
|
||||
|
||||
our $OK_OUTCOME = 'ok';
|
||||
our $FAILED_OUTCOME = 'failed';
|
||||
|
||||
our $VOUCHER_TYPE = 'voucher';
|
||||
our $CASH_TYPE = 'cash';
|
||||
our $SET_BALANCE_TYPE = 'set_balance';
|
||||
|
||||
sub new {
|
||||
|
||||
my $class = shift;
|
||||
my $self = NGCP::BulkProcessor::SqlRecord->new($class,$get_db,
|
||||
$tablename,$expected_fieldnames,$indexes);
|
||||
|
||||
copy_row($self,shift,$expected_fieldnames);
|
||||
|
||||
return $self;
|
||||
|
||||
}
|
||||
|
||||
sub findby_contractidfromto {
|
||||
|
||||
my ($contract_id,$from,$to,$outcome,$load_recursive) = @_;
|
||||
|
||||
check_table();
|
||||
my $db = &$get_db();
|
||||
my $table = $db->tableidentifier($tablename);
|
||||
|
||||
my $stmt = 'SELECT * FROM ' . $table . ' WHERE ' .
|
||||
$db->columnidentifier('contract_id') . ' = ?';
|
||||
my @params = ($contract_id);
|
||||
if ($from) {
|
||||
$stmt .= ' AND ' . $db->columnidentifier('timestamp') . ' >= ?';
|
||||
push(@params,$from->epoch());
|
||||
}
|
||||
if ($to) {
|
||||
$stmt .= ' AND ' . $db->columnidentifier('timestamp') . ' <= ?';
|
||||
push(@params,$to->epoch());
|
||||
}
|
||||
if ($outcome) {
|
||||
$stmt .= ' AND ' . $db->columnidentifier('outcome') . ' = ?';
|
||||
push(@params,$outcome);
|
||||
}
|
||||
my $rows = $db->db_get_all_arrayref($stmt,@params);
|
||||
|
||||
return buildrecords_fromrows($rows,$load_recursive);
|
||||
|
||||
}
|
||||
|
||||
sub findby_contractbalanceid {
|
||||
|
||||
my ($id,$outcome,$load_recursive) = @_;
|
||||
|
||||
check_table();
|
||||
my $db = &$get_db();
|
||||
my $table = $db->tableidentifier($tablename);
|
||||
|
||||
my $stmt = 'SELECT * FROM ' . $table . ' WHERE ' .
|
||||
$db->columnidentifier('contract_balance_before_id') . ' = ?';
|
||||
my @params = ($id);
|
||||
if ($outcome) {
|
||||
$stmt .= ' AND ' . $db->columnidentifier('outcome') . ' = ?';
|
||||
push(@params,$outcome);
|
||||
}
|
||||
my $rows = $db->db_get_all_arrayref($stmt,@params);
|
||||
|
||||
return buildrecords_fromrows($rows,$load_recursive);
|
||||
|
||||
}
|
||||
|
||||
sub findby_id {
|
||||
|
||||
my ($id,$load_recursive) = @_;
|
||||
|
||||
check_table();
|
||||
my $db = &$get_db();
|
||||
my $table = $db->tableidentifier($tablename);
|
||||
|
||||
my $stmt = 'SELECT * FROM ' . $table . ' WHERE ' .
|
||||
$db->columnidentifier('id') . ' = ?';
|
||||
my @params = ($id);
|
||||
my $rows = $db->db_get_all_arrayref($stmt,@params);
|
||||
|
||||
return buildrecords_fromrows($rows,$load_recursive)->[0];
|
||||
|
||||
}
|
||||
|
||||
sub insert_row {
|
||||
|
||||
my $db = &$get_db();
|
||||
my $xa_db = shift // $db;
|
||||
if ('HASH' eq ref $_[0]) {
|
||||
my ($data,$insert_ignore) = @_;
|
||||
check_table();
|
||||
if (insert_record($db,$xa_db,__PACKAGE__,$data,$insert_ignore,$insert_unique_fields)) {
|
||||
return $xa_db->db_last_insert_id();
|
||||
}
|
||||
} else {
|
||||
my %params = @_;
|
||||
my ($contract_id) = @params{qw/
|
||||
contract_id
|
||||
/};
|
||||
|
||||
if ($xa_db->db_do('INSERT INTO ' . $db->tableidentifier($tablename) . ' (' .
|
||||
$db->columnidentifier('timestamp') . ', ' .
|
||||
$db->columnidentifier('type') . ', ' .
|
||||
$db->columnidentifier('outcome') .', ' .
|
||||
$db->columnidentifier('contract_id') . ') VALUES (' .
|
||||
'UNIX_TIMESTAMP(NOW()), ' .
|
||||
'\'' . $SET_BALANCE_TYPE . '\', ' .
|
||||
'\'' . $FAILED_OUTCOME . '\', ' .
|
||||
'?)',
|
||||
$contract_id
|
||||
)) {
|
||||
rowinserted($db,$tablename,getlogger(__PACKAGE__));
|
||||
return $xa_db->db_last_insert_id();
|
||||
}
|
||||
}
|
||||
return undef;
|
||||
|
||||
}
|
||||
|
||||
sub buildrecords_fromrows {
|
||||
|
||||
my ($rows,$load_recursive) = @_;
|
||||
|
||||
my @records = ();
|
||||
my $record;
|
||||
|
||||
if (defined $rows and ref $rows eq 'ARRAY') {
|
||||
foreach my $row (@$rows) {
|
||||
$record = __PACKAGE__->new($row);
|
||||
|
||||
# transformations go here ...
|
||||
|
||||
push @records,$record;
|
||||
}
|
||||
}
|
||||
|
||||
return \@records;
|
||||
|
||||
}
|
||||
|
||||
sub gettablename {
|
||||
|
||||
return $tablename;
|
||||
|
||||
}
|
||||
|
||||
sub check_table {
|
||||
|
||||
return checktableinfo($get_db,
|
||||
__PACKAGE__,$tablename,
|
||||
$expected_fieldnames,
|
||||
$indexes);
|
||||
|
||||
}
|
||||
|
||||
1;
|
||||
@ -0,0 +1,168 @@
|
||||
package NGCP::BulkProcessor::Dao::mr457::billing::billing_mappings;
|
||||
use strict;
|
||||
|
||||
## no critic
|
||||
|
||||
use NGCP::BulkProcessor::Logging qw(
|
||||
getlogger
|
||||
rowinserted
|
||||
);
|
||||
|
||||
use NGCP::BulkProcessor::ConnectorPool qw(
|
||||
get_billing_db
|
||||
);
|
||||
|
||||
use NGCP::BulkProcessor::SqlProcessor qw(
|
||||
checktableinfo
|
||||
insert_record
|
||||
copy_row
|
||||
);
|
||||
use NGCP::BulkProcessor::SqlRecord qw();
|
||||
|
||||
require Exporter;
|
||||
our @ISA = qw(Exporter NGCP::BulkProcessor::SqlRecord);
|
||||
our @EXPORT_OK = qw(
|
||||
gettablename
|
||||
check_table
|
||||
insert_row
|
||||
|
||||
findby_contractid_ts
|
||||
);
|
||||
|
||||
my $tablename = 'billing_mappings';
|
||||
my $get_db = \&get_billing_db;
|
||||
|
||||
my $expected_fieldnames = [
|
||||
'id',
|
||||
'start_date',
|
||||
'end_date',
|
||||
'billing_profile_id',
|
||||
'contract_id',
|
||||
'product_id',
|
||||
'network_id',
|
||||
];
|
||||
|
||||
my $indexes = {};
|
||||
|
||||
my $insert_unique_fields = [];
|
||||
|
||||
sub new {
|
||||
|
||||
my $class = shift;
|
||||
my $self = NGCP::BulkProcessor::SqlRecord->new($class,$get_db,
|
||||
$tablename,$expected_fieldnames,$indexes);
|
||||
|
||||
copy_row($self,shift,$expected_fieldnames);
|
||||
|
||||
return $self;
|
||||
|
||||
}
|
||||
|
||||
sub findby_contractid_ts {
|
||||
|
||||
my ($xa_db,$contract_id,$dt,$load_recursive) = @_;
|
||||
|
||||
check_table();
|
||||
my $db = &$get_db();
|
||||
$xa_db //= $db;
|
||||
my $table = $db->tableidentifier($tablename);
|
||||
|
||||
my $stmt = 'SELECT * FROM ' . $table . ' WHERE ' .
|
||||
$db->columnidentifier('contract_id') . ' = ?';
|
||||
my @params = ($contract_id);
|
||||
if (defined $dt) {
|
||||
$stmt .= ' AND (' . $db->columnidentifier('start_date') . ' IS NULL OR ' . $db->columnidentifier('start_date') . ' <= ? ) ' .
|
||||
'AND (' . $db->columnidentifier('end_date') . ' IS NULL OR ' . $db->columnidentifier('end_date') . ' >= ? ) ' .
|
||||
'ORDER BY ' . $db->columnidentifier('start_date') . ' DESC, ' . $db->columnidentifier('id') . ' DESC LIMIT 1';
|
||||
push(@params, $db->datetime_to_string($dt) );
|
||||
push(@params, $db->datetime_to_string($dt) );
|
||||
}
|
||||
|
||||
my $rows = $xa_db->db_get_all_arrayref($stmt,@params);
|
||||
|
||||
return buildrecords_fromrows($rows,$load_recursive);
|
||||
|
||||
}
|
||||
|
||||
sub insert_row {
|
||||
|
||||
my $db = &$get_db();
|
||||
my $xa_db = shift // $db;
|
||||
if ('HASH' eq ref $_[0]) {
|
||||
my ($data,$insert_ignore) = @_;
|
||||
check_table();
|
||||
if (insert_record($db,$xa_db,__PACKAGE__,$data,$insert_ignore,$insert_unique_fields)) {
|
||||
return $xa_db->db_last_insert_id();
|
||||
}
|
||||
} else {
|
||||
my %params = @_;
|
||||
my ($billing_profile_id,
|
||||
$contract_id,
|
||||
$product_id) = @params{qw/
|
||||
billing_profile_id
|
||||
contract_id
|
||||
product_id
|
||||
/};
|
||||
|
||||
if ($xa_db->db_do('INSERT INTO ' . $db->tableidentifier($tablename) . ' (' .
|
||||
$db->columnidentifier('billing_profile_id') . ', ' .
|
||||
$db->columnidentifier('contract_id') . ', ' .
|
||||
$db->columnidentifier('end_date') . ', ' .
|
||||
$db->columnidentifier('network_id') . ', ' .
|
||||
$db->columnidentifier('product_id') . ', ' .
|
||||
$db->columnidentifier('start_date') . ') VALUES (' .
|
||||
'?, ' .
|
||||
'?, ' .
|
||||
'NULL, ' .
|
||||
'NULL, ' .
|
||||
'?, ' .
|
||||
'NULL)',
|
||||
$billing_profile_id,
|
||||
$contract_id,
|
||||
$product_id,
|
||||
)) {
|
||||
rowinserted($db,$tablename,getlogger(__PACKAGE__));
|
||||
return $xa_db->db_last_insert_id();
|
||||
}
|
||||
}
|
||||
return undef;
|
||||
|
||||
}
|
||||
|
||||
sub buildrecords_fromrows {
|
||||
|
||||
my ($rows,$load_recursive) = @_;
|
||||
|
||||
my @records = ();
|
||||
my $record;
|
||||
|
||||
if (defined $rows and ref $rows eq 'ARRAY') {
|
||||
foreach my $row (@$rows) {
|
||||
$record = __PACKAGE__->new($row);
|
||||
|
||||
# transformations go here ...
|
||||
|
||||
push @records,$record;
|
||||
}
|
||||
}
|
||||
|
||||
return \@records;
|
||||
|
||||
}
|
||||
|
||||
sub gettablename {
|
||||
|
||||
return $tablename;
|
||||
|
||||
}
|
||||
|
||||
sub check_table {
|
||||
|
||||
return checktableinfo($get_db,
|
||||
__PACKAGE__,$tablename,
|
||||
$expected_fieldnames,
|
||||
$indexes);
|
||||
|
||||
}
|
||||
|
||||
1;
|
||||
@ -0,0 +1,176 @@
|
||||
package NGCP::BulkProcessor::Dao::mr457::billing::billing_profiles;
|
||||
use strict;
|
||||
|
||||
## no critic
|
||||
|
||||
use NGCP::BulkProcessor::ConnectorPool qw(
|
||||
get_billing_db
|
||||
|
||||
);
|
||||
|
||||
use NGCP::BulkProcessor::SqlProcessor qw(
|
||||
checktableinfo
|
||||
copy_row
|
||||
);
|
||||
use NGCP::BulkProcessor::SqlRecord qw();
|
||||
|
||||
require Exporter;
|
||||
our @ISA = qw(Exporter NGCP::BulkProcessor::SqlRecord);
|
||||
our @EXPORT_OK = qw(
|
||||
gettablename
|
||||
check_table
|
||||
|
||||
findby_id
|
||||
findall
|
||||
findby_resellerid_name_handle
|
||||
|
||||
$DEFAULT_PROFILE_FREE_CASH
|
||||
$DEFAULT_PROFILE_FREE_TIME
|
||||
);
|
||||
|
||||
my $tablename = 'billing_profiles';
|
||||
my $get_db = \&get_billing_db;
|
||||
|
||||
my $expected_fieldnames = [
|
||||
'id',
|
||||
'reseller_id',
|
||||
'handle',
|
||||
'name',
|
||||
'prepaid',
|
||||
'interval_charge',
|
||||
'interval_free_time',
|
||||
'interval_free_cash',
|
||||
'interval_unit',
|
||||
'interval_count',
|
||||
'fraud_interval_limit',
|
||||
'fraud_interval_lock',
|
||||
'fraud_interval_notify',
|
||||
'fraud_daily_limit',
|
||||
'fraud_daily_lock',
|
||||
'fraud_daily_notify',
|
||||
'fraud_use_reseller_rates',
|
||||
'currency',
|
||||
'status',
|
||||
'modify_timestamp',
|
||||
'create_timestamp',
|
||||
'terminate_timestamp',
|
||||
];
|
||||
|
||||
my $indexes = {};
|
||||
|
||||
our $DEFAULT_PROFILE_FREE_CASH = 0.0;
|
||||
our $DEFAULT_PROFILE_FREE_TIME = 0;
|
||||
|
||||
sub new {
|
||||
|
||||
my $class = shift;
|
||||
my $self = NGCP::BulkProcessor::SqlRecord->new($class,$get_db,
|
||||
$tablename,$expected_fieldnames,$indexes);
|
||||
|
||||
copy_row($self,shift,$expected_fieldnames);
|
||||
|
||||
return $self;
|
||||
|
||||
}
|
||||
|
||||
sub findby_id {
|
||||
|
||||
my ($id,$load_recursive) = @_;
|
||||
|
||||
check_table();
|
||||
my $db = &$get_db();
|
||||
my $table = $db->tableidentifier($tablename);
|
||||
|
||||
my $stmt = 'SELECT * FROM ' . $table . ' WHERE ' .
|
||||
$db->columnidentifier('id') . ' = ?';
|
||||
my @params = ($id);
|
||||
my $rows = $db->db_get_all_arrayref($stmt,@params);
|
||||
|
||||
return buildrecords_fromrows($rows,$load_recursive)->[0];
|
||||
|
||||
}
|
||||
|
||||
sub findall {
|
||||
|
||||
my ($load_recursive) = @_;
|
||||
|
||||
check_table();
|
||||
my $db = &$get_db();
|
||||
my $table = $db->tableidentifier($tablename);
|
||||
|
||||
my $stmt = 'SELECT * FROM ' . $table;
|
||||
my $rows = $db->db_get_all_arrayref($stmt);
|
||||
|
||||
return buildrecords_fromrows($rows,$load_recursive);
|
||||
|
||||
}
|
||||
|
||||
sub findby_resellerid_name_handle {
|
||||
|
||||
my ($reseller_id,$name,$handle,$load_recursive) = @_;
|
||||
|
||||
check_table();
|
||||
my $db = &$get_db();
|
||||
my $table = $db->tableidentifier($tablename);
|
||||
|
||||
my $stmt = 'SELECT * FROM ' . $table;
|
||||
my @params = ();
|
||||
my @terms = ();
|
||||
if ($reseller_id) {
|
||||
push(@terms,$db->columnidentifier('reseller_id') . ' = ?');
|
||||
push(@params,$reseller_id);
|
||||
}
|
||||
if ($name) {
|
||||
push(@terms,$db->columnidentifier('name') . ' = ?');
|
||||
push(@params,$name);
|
||||
}
|
||||
if ($handle) {
|
||||
push(@terms,$db->columnidentifier('handle') . ' = ?');
|
||||
push(@params,$handle);
|
||||
}
|
||||
if ((scalar @terms) > 0) {
|
||||
$stmt .= ' WHERE ' . join(' AND ',@terms);
|
||||
}
|
||||
my $rows = $db->db_get_all_arrayref($stmt,@params);
|
||||
|
||||
return buildrecords_fromrows($rows,$load_recursive);
|
||||
|
||||
}
|
||||
|
||||
sub buildrecords_fromrows {
|
||||
|
||||
my ($rows,$load_recursive) = @_;
|
||||
|
||||
my @records = ();
|
||||
my $record;
|
||||
|
||||
if (defined $rows and ref $rows eq 'ARRAY') {
|
||||
foreach my $row (@$rows) {
|
||||
$record = __PACKAGE__->new($row);
|
||||
|
||||
# transformations go here ...
|
||||
|
||||
push @records,$record;
|
||||
}
|
||||
}
|
||||
|
||||
return \@records;
|
||||
|
||||
}
|
||||
|
||||
sub gettablename {
|
||||
|
||||
return $tablename;
|
||||
|
||||
}
|
||||
|
||||
sub check_table {
|
||||
|
||||
return checktableinfo($get_db,
|
||||
__PACKAGE__,$tablename,
|
||||
$expected_fieldnames,
|
||||
$indexes);
|
||||
|
||||
}
|
||||
|
||||
1;
|
||||
@ -0,0 +1,324 @@
|
||||
package NGCP::BulkProcessor::Dao::mr457::billing::contract_balances;
|
||||
use strict;
|
||||
|
||||
## no critic
|
||||
|
||||
use DateTime qw();
|
||||
|
||||
use NGCP::BulkProcessor::Logging qw(
|
||||
getlogger
|
||||
rowinserted
|
||||
);
|
||||
|
||||
use NGCP::BulkProcessor::ConnectorPool qw(
|
||||
get_billing_db
|
||||
);
|
||||
|
||||
use NGCP::BulkProcessor::SqlProcessor qw(
|
||||
checktableinfo
|
||||
insert_record
|
||||
update_record
|
||||
copy_row
|
||||
);
|
||||
use NGCP::BulkProcessor::SqlRecord qw();
|
||||
use NGCP::BulkProcessor::Calendar qw(is_infinite_future infinite_future set_timezone);
|
||||
|
||||
use NGCP::BulkProcessor::Dao::mr457::billing::billing_profiles qw();
|
||||
use NGCP::BulkProcessor::Dao::mr457::billing::profile_packages qw();
|
||||
|
||||
require Exporter;
|
||||
our @ISA = qw(Exporter NGCP::BulkProcessor::SqlRecord);
|
||||
our @EXPORT_OK = qw(
|
||||
gettablename
|
||||
check_table
|
||||
insert_row
|
||||
update_row
|
||||
findby_contractid
|
||||
sort_by_end_desc
|
||||
sort_by_end_asc
|
||||
get_new_balance_values
|
||||
get_free_ratio
|
||||
);
|
||||
|
||||
my $tablename = 'contract_balances';
|
||||
my $get_db = \&get_billing_db;
|
||||
|
||||
my $expected_fieldnames = [
|
||||
'id',
|
||||
'contract_id',
|
||||
'cash_balance',
|
||||
'cash_balance_interval',
|
||||
'free_time_balance',
|
||||
'free_time_balance_interval',
|
||||
'topup_count',
|
||||
'timely_topup_count',
|
||||
'start',
|
||||
'end',
|
||||
'invoice_id',
|
||||
'underrun_profiles',
|
||||
'underrun_lock',
|
||||
#'initial_cash_balance',
|
||||
#'initial_free_time_balance',
|
||||
];
|
||||
|
||||
my $indexes = {};
|
||||
|
||||
my $insert_unique_fields = [];
|
||||
|
||||
sub new {
|
||||
|
||||
my $class = shift;
|
||||
my $self = NGCP::BulkProcessor::SqlRecord->new($class,$get_db,
|
||||
$tablename,$expected_fieldnames,$indexes);
|
||||
|
||||
copy_row($self,shift,$expected_fieldnames);
|
||||
|
||||
return $self;
|
||||
|
||||
}
|
||||
|
||||
sub findby_contractid {
|
||||
|
||||
my ($xa_db,$contract_id,$load_recursive) = @_;
|
||||
|
||||
check_table();
|
||||
my $db = &$get_db();
|
||||
$xa_db //= $db;
|
||||
my $table = $db->tableidentifier($tablename);
|
||||
|
||||
my $stmt = 'SELECT * FROM ' . $table . ' WHERE ' .
|
||||
$db->columnidentifier('contract_id') . ' = ?';
|
||||
my @params = ($contract_id);
|
||||
|
||||
my $rows = $xa_db->db_get_all_arrayref($stmt,@params);
|
||||
|
||||
return buildrecords_fromrows($rows,$load_recursive);
|
||||
|
||||
}
|
||||
|
||||
sub insert_row {
|
||||
|
||||
my $db = &$get_db();
|
||||
my $xa_db = shift // $db;
|
||||
if ('HASH' eq ref $_[0]) {
|
||||
my ($data,$insert_ignore) = @_;
|
||||
check_table();
|
||||
if (insert_record($db,$xa_db,__PACKAGE__,$data,$insert_ignore,$insert_unique_fields)) {
|
||||
return $xa_db->db_last_insert_id();
|
||||
}
|
||||
} else {
|
||||
my %params = @_;
|
||||
my ($contract_id) = @params{qw/
|
||||
contract_id
|
||||
/};
|
||||
|
||||
if ($xa_db->db_do('INSERT INTO ' . $db->tableidentifier($tablename) . ' (' .
|
||||
$db->columnidentifier('cash_balance') . ', ' .
|
||||
$db->columnidentifier('cash_balance_interval') . ', ' .
|
||||
$db->columnidentifier('contract_id') . ', ' .
|
||||
$db->columnidentifier('end') . ', ' .
|
||||
$db->columnidentifier('free_time_balance') . ', ' .
|
||||
$db->columnidentifier('free_time_balance_interval') . ', ' .
|
||||
$db->columnidentifier('start') . ', ' .
|
||||
$db->columnidentifier('underrun_lock') . ', ' .
|
||||
$db->columnidentifier('underrun_profiles') . ') VALUES (' .
|
||||
'0.0, ' .
|
||||
'0.0, ' .
|
||||
'?, ' .
|
||||
'CONCAT(LAST_DAY(NOW()),\' 23:59:59\'), ' .
|
||||
'0, ' .
|
||||
'0, ' .
|
||||
'CONCAT(SUBDATE(CURDATE(),(DAY(CURDATE())-1)),\' 00:00:00\'), ' .
|
||||
'NULL, ' .
|
||||
'NULL)',
|
||||
$contract_id,
|
||||
)) {
|
||||
rowinserted($db,$tablename,getlogger(__PACKAGE__));
|
||||
return $xa_db->db_last_insert_id();
|
||||
}
|
||||
}
|
||||
return undef;
|
||||
|
||||
}
|
||||
|
||||
sub update_row {
|
||||
|
||||
my ($xa_db,$data) = @_;
|
||||
|
||||
check_table();
|
||||
return update_record($get_db,$xa_db,__PACKAGE__,$data);
|
||||
|
||||
}
|
||||
|
||||
sub buildrecords_fromrows {
|
||||
|
||||
my ($rows,$load_recursive) = @_;
|
||||
|
||||
my @records = ();
|
||||
my $record;
|
||||
|
||||
if (defined $rows and ref $rows eq 'ARRAY') {
|
||||
my $db = &$get_db();
|
||||
foreach my $row (@$rows) {
|
||||
$record = __PACKAGE__->new($row);
|
||||
|
||||
# transformations go here ...
|
||||
my $end = $db->datetime_from_string($record->{end},undef);
|
||||
if (is_infinite_future($end)) {
|
||||
$record->{_end} = infinite_future();
|
||||
} else {
|
||||
$record->{_end} = set_timezone($end);
|
||||
}
|
||||
|
||||
$record->{_start} = $db->datetime_from_string($record->{start},'local');
|
||||
|
||||
push @records,$record;
|
||||
}
|
||||
}
|
||||
|
||||
return \@records;
|
||||
|
||||
}
|
||||
|
||||
sub sort_by_end_asc ($$) {
|
||||
return _sort_by_date('_end',0,@_);
|
||||
}
|
||||
|
||||
sub sort_by_end_desc ($$) {
|
||||
return _sort_by_date('_end',1,@_);
|
||||
}
|
||||
|
||||
sub _sort_by_date {
|
||||
my ($ts_field,$desc,$a,$b) = @_;
|
||||
if ($desc) {
|
||||
$desc = -1;
|
||||
} else {
|
||||
$desc = 1;
|
||||
}
|
||||
#use Data::Dumper;
|
||||
#print Dumper($a);
|
||||
#print Dumper($b);
|
||||
my $a_inf = is_infinite_future($a->{$ts_field});
|
||||
my $b_inf = is_infinite_future($b->{$ts_field});
|
||||
if ($a_inf and $b_inf) {
|
||||
return 0;
|
||||
} elsif ($a_inf) {
|
||||
return 1 * $desc;
|
||||
} elsif ($b_inf) {
|
||||
return -1 * $desc;
|
||||
} else {
|
||||
return DateTime->compare($a->{$ts_field}, $b->{$ts_field}) * $desc;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
sub get_new_balance_values {
|
||||
my %params = @_;
|
||||
my ($contract_create,
|
||||
$last_balance,
|
||||
$balance,
|
||||
$initial_balance,
|
||||
$carry_over_mode,
|
||||
$notopup_expiration,
|
||||
$last_cash_balance,
|
||||
$last_cash_balance_interval) = @params{qw/
|
||||
contract_create
|
||||
last_balance
|
||||
balance
|
||||
initial_balance
|
||||
carry_over_mode
|
||||
notopup_expiration
|
||||
last_cash_balance
|
||||
last_cash_balance_interval
|
||||
/};
|
||||
my ($cash_balance, $cash_balance_interval, $free_time_balance, $free_time_balance_interval) = (0.0,0.0,0,0);
|
||||
|
||||
$carry_over_mode //= $NGCP::BulkProcessor::Dao::Trunk::billing::profile_packages::DEFAULT_CARRY_OVER_MODE;
|
||||
my $ratio;
|
||||
if ($last_balance) {
|
||||
$last_cash_balance //= $last_balance->{cash_balance};
|
||||
$last_cash_balance_interval //= $last_balance->{cash_balance_interval};
|
||||
if (($NGCP::BulkProcessor::Dao::Trunk::billing::profile_packages::CARRY_OVER_MODE eq $carry_over_mode
|
||||
|| ($NGCP::BulkProcessor::Dao::Trunk::billing::profile_packages::CARRY_OVER_TIMELY_MODE eq $carry_over_mode && $last_balance->{timely_topup_count} > 0)
|
||||
) && (!defined $notopup_expiration || $balance->{_start} < $notopup_expiration)) {
|
||||
#if (!defined $last_profile) {
|
||||
# my $bm_last = get_actual_billing_mapping(schema => $schema, contract => $contract, now => $last_balance->start); #end); !?
|
||||
# $last_profile = $bm_last->billing_mappings->first->billing_profile;
|
||||
#}
|
||||
#my $contract_create = NGCP::Panel::Utils::DateTime::set_local_tz($contract->create_timestamp // $contract->modify_timestamp);
|
||||
$ratio = 1.0;
|
||||
if ($last_balance->{_start} <= $contract_create && $last_balance->{_end} >= $contract_create) { #$last_balance->end is never +inf here
|
||||
$ratio = get_free_ratio($contract_create,$last_balance->{_start},$last_balance->{_end});
|
||||
}
|
||||
my $old_free_cash = $ratio * ($last_balance->{_profile}->{interval_free_cash} // $NGCP::BulkProcessor::Dao::Trunk::billing::billing_profiles::DEFAULT_PROFILE_FREE_CASH);
|
||||
$cash_balance = $last_cash_balance;
|
||||
if ($last_cash_balance_interval < $old_free_cash) {
|
||||
$cash_balance += $last_cash_balance_interval - $old_free_cash;
|
||||
}
|
||||
#$ratio * $last_profile->interval_free_time // _DEFAULT_PROFILE_FREE_TIME
|
||||
#} else {
|
||||
# $c->log->debug('discarding contract ' . $contract->id . " cash balance (mode '$carry_over_mode'" . (defined $notopup_expiration ? ', notopup expiration ' . NGCP::Panel::Utils::DateTime::to_string($notopup_expiration) : '') . ')') if $c;
|
||||
}
|
||||
$ratio = 1.0;
|
||||
} else {
|
||||
$cash_balance = (defined $initial_balance ? $initial_balance : $NGCP::BulkProcessor::Dao::Trunk::billing::profile_packages::DEFAULT_INITIAL_BALANCE);
|
||||
$ratio = get_free_ratio($contract_create,$balance->{_start},$balance->{_end});
|
||||
}
|
||||
|
||||
my $free_cash = $ratio * ($balance->{_profile}->{interval_free_cash} // $NGCP::BulkProcessor::Dao::Trunk::billing::billing_profiles::DEFAULT_PROFILE_FREE_CASH);
|
||||
$cash_balance += $free_cash;
|
||||
$cash_balance_interval = 0.0;
|
||||
|
||||
my $free_time = $ratio * ($balance->{_profile}->{interval_free_time} // $NGCP::BulkProcessor::Dao::Trunk::billing::billing_profiles::DEFAULT_PROFILE_FREE_TIME);
|
||||
$free_time_balance = $free_time;
|
||||
$free_time_balance_interval = 0;
|
||||
|
||||
#$c->log->debug("ratio: $ratio, free cash: $free_cash, cash balance: $cash_balance, free time: $free_time, free time balance: $free_time_balance");
|
||||
|
||||
return {cash_balance => sprintf("%.4f",$cash_balance),
|
||||
initial_cash_balance => sprintf("%.4f",$cash_balance),
|
||||
cash_balance_interval => sprintf("%.4f",$cash_balance_interval),
|
||||
free_time_balance => sprintf("%.0f",$free_time_balance),
|
||||
initial_free_time_balance => sprintf("%.0f",$free_time_balance),
|
||||
free_time_balance_interval => sprintf("%.0f",$free_time_balance_interval)};
|
||||
|
||||
}
|
||||
|
||||
sub get_free_ratio {
|
||||
my ($contract_create,$stime,$etime) = @_;
|
||||
if (!is_infinite_future($etime)) {
|
||||
my $ctime = ($contract_create->clone->truncate(to => 'day') > $stime ? $contract_create->clone->truncate(to => 'day') : $contract_create);
|
||||
my $start_of_next_interval = _add_second($etime->clone,1);
|
||||
#$c->log->debug("ratio = " . ($start_of_next_interval->epoch - $ctime->epoch) . ' / ' . ($start_of_next_interval->epoch - $stime->epoch)) if $c;
|
||||
return ($start_of_next_interval->epoch - $ctime->epoch) / ($start_of_next_interval->epoch - $stime->epoch);
|
||||
}
|
||||
return 1.0;
|
||||
}
|
||||
|
||||
sub _add_second {
|
||||
|
||||
my ($dt,$skip_leap_seconds) = @_;
|
||||
$dt->add(seconds => 1);
|
||||
while ($skip_leap_seconds and $dt->second() >= 60) {
|
||||
$dt->add(seconds => 1);
|
||||
}
|
||||
return $dt;
|
||||
|
||||
}
|
||||
|
||||
sub gettablename {
|
||||
|
||||
return $tablename;
|
||||
|
||||
}
|
||||
|
||||
sub check_table {
|
||||
|
||||
return checktableinfo($get_db,
|
||||
__PACKAGE__,$tablename,
|
||||
$expected_fieldnames,
|
||||
$indexes);
|
||||
|
||||
}
|
||||
|
||||
1;
|
||||
@ -0,0 +1,345 @@
|
||||
package NGCP::BulkProcessor::Dao::mr457::billing::contracts;
|
||||
use strict;
|
||||
|
||||
## no critic
|
||||
|
||||
use NGCP::BulkProcessor::Logging qw(
|
||||
getlogger
|
||||
rowinserted
|
||||
);
|
||||
|
||||
use NGCP::BulkProcessor::ConnectorPool qw(
|
||||
get_billing_db
|
||||
destroy_dbs
|
||||
);
|
||||
|
||||
use NGCP::BulkProcessor::SqlProcessor qw(
|
||||
checktableinfo
|
||||
insert_record
|
||||
copy_row
|
||||
|
||||
process_table
|
||||
);
|
||||
use NGCP::BulkProcessor::SqlRecord qw();
|
||||
|
||||
use NGCP::BulkProcessor::Dao::mr457::billing::billing_mappings qw();
|
||||
use NGCP::BulkProcessor::Dao::mr457::billing::billing_profiles qw();
|
||||
|
||||
require Exporter;
|
||||
our @ISA = qw(Exporter NGCP::BulkProcessor::SqlRecord);
|
||||
our @EXPORT_OK = qw(
|
||||
gettablename
|
||||
check_table
|
||||
insert_row
|
||||
|
||||
countby_status_resellerid
|
||||
findby_contactid
|
||||
findby_id
|
||||
forupdate_id
|
||||
|
||||
process_records
|
||||
process_free_cash_contracts
|
||||
|
||||
countby_free_cash
|
||||
|
||||
$ACTIVE_STATE
|
||||
$TERMINATED_STATE
|
||||
);
|
||||
|
||||
my $tablename = 'contracts';
|
||||
my $get_db = \&get_billing_db;
|
||||
|
||||
my $expected_fieldnames = [
|
||||
'id',
|
||||
'customer_id',
|
||||
'contact_id',
|
||||
'order_id',
|
||||
'profile_package_id',
|
||||
'status',
|
||||
'external_id',
|
||||
'modify_timestamp',
|
||||
'create_timestamp',
|
||||
'activate_timestamp',
|
||||
'terminate_timestamp',
|
||||
'max_subscribers',
|
||||
'send_invoice',
|
||||
'subscriber_email_template_id',
|
||||
'passreset_email_template_id',
|
||||
'invoice_email_template_id',
|
||||
'invoice_template_id',
|
||||
'vat_rate',
|
||||
'add_vat',
|
||||
];
|
||||
|
||||
my $indexes = {};
|
||||
|
||||
my $insert_unique_fields = [];
|
||||
|
||||
our $ACTIVE_STATE = 'active';
|
||||
our $TERMINATED_STATE = 'terminated';
|
||||
|
||||
sub new {
|
||||
|
||||
my $class = shift;
|
||||
my $self = NGCP::BulkProcessor::SqlRecord->new($class,$get_db,
|
||||
$tablename,$expected_fieldnames,$indexes);
|
||||
|
||||
copy_row($self,shift,$expected_fieldnames);
|
||||
|
||||
return $self;
|
||||
|
||||
}
|
||||
|
||||
sub findby_contactid {
|
||||
|
||||
my ($contact_id,$load_recursive) = @_;
|
||||
|
||||
check_table();
|
||||
my $db = &$get_db();
|
||||
my $table = $db->tableidentifier($tablename);
|
||||
|
||||
my $stmt = 'SELECT * FROM ' . $table . ' WHERE ' .
|
||||
$db->columnidentifier('contact_id') . ' = ?';
|
||||
my @params = ($contact_id);
|
||||
my $rows = $db->db_get_all_arrayref($stmt,@params);
|
||||
|
||||
return buildrecords_fromrows($rows,$load_recursive);
|
||||
|
||||
}
|
||||
|
||||
sub findby_id {
|
||||
|
||||
my ($id,$load_recursive) = @_;
|
||||
|
||||
check_table();
|
||||
my $db = &$get_db();
|
||||
my $table = $db->tableidentifier($tablename);
|
||||
|
||||
my $stmt = 'SELECT * FROM ' . $table . ' WHERE ' .
|
||||
$db->columnidentifier('id') . ' = ?';
|
||||
my @params = ($id);
|
||||
my $rows = $db->db_get_all_arrayref($stmt,@params);
|
||||
|
||||
return buildrecords_fromrows($rows,$load_recursive)->[0];
|
||||
|
||||
}
|
||||
|
||||
sub forupdate_id {
|
||||
|
||||
my ($xa_db,$id,$load_recursive) = @_;
|
||||
|
||||
check_table();
|
||||
my $db = &$get_db();
|
||||
$xa_db //= $db;
|
||||
my $table = $db->tableidentifier($tablename);
|
||||
|
||||
my $stmt = 'SELECT * FROM ' . $table . ' WHERE ' .
|
||||
$db->columnidentifier('id') . ' = ? FOR UPDATE';
|
||||
my @params = ($id);
|
||||
my $rows = $xa_db->db_get_all_arrayref($stmt,@params);
|
||||
|
||||
return buildrecords_fromrows($rows,$load_recursive)->[0];
|
||||
|
||||
}
|
||||
|
||||
sub countby_status_resellerid {
|
||||
|
||||
my ($status,$reseller_id) = @_;
|
||||
|
||||
check_table();
|
||||
my $db = &$get_db();
|
||||
my $table = $db->tableidentifier($tablename);
|
||||
|
||||
my $stmt = 'SELECT COUNT(*) FROM ' . $table . ' AS contract' .
|
||||
' INNER JOIN ' . $db->tableidentifier(NGCP::BulkProcessor::Dao::mr457::billing::contacts::gettablename()) . ' AS contact ON contract.contact_id = contact.id';
|
||||
my @params = ();
|
||||
my @terms = ();
|
||||
if ($status) {
|
||||
push(@terms,'contract.status = ?');
|
||||
push(@params,$status);
|
||||
}
|
||||
if ($reseller_id) {
|
||||
if ('ARRAY' eq ref $reseller_id) {
|
||||
push(@terms,'contact.reseller_id IN (' . substr(',?' x scalar @$reseller_id,1) . ')');
|
||||
push(@params,@$reseller_id);
|
||||
} else {
|
||||
push(@terms,'contact.reseller_id = ?');
|
||||
push(@params,$reseller_id);
|
||||
}
|
||||
}
|
||||
if ((scalar @terms) > 0) {
|
||||
$stmt .= ' WHERE ' . join(' AND ',@terms);
|
||||
}
|
||||
|
||||
return $db->db_get_value($stmt,@params);
|
||||
|
||||
}
|
||||
|
||||
sub countby_free_cash {
|
||||
|
||||
check_table();
|
||||
my $db = &$get_db();
|
||||
my $table = $db->tableidentifier($tablename);
|
||||
|
||||
my $stmt = 'SELECT COUNT(DISTINCT c.id) FROM ' . $table . ' AS c' .
|
||||
' INNER JOIN ' . $db->tableidentifier(NGCP::BulkProcessor::Dao::mr457::billing::billing_mappings::gettablename()) . ' AS bm ON bm.contract_id = c.id' .
|
||||
' INNER JOIN ' . $db->tableidentifier(NGCP::BulkProcessor::Dao::mr457::billing::billing_profiles::gettablename()) . ' AS bp ON bp.id = bm.billing_profile_id' .
|
||||
' WHERE c.status != "terminated" AND bp.interval_free_cash <> 0.0';
|
||||
|
||||
return $db->db_get_value($stmt);
|
||||
|
||||
}
|
||||
|
||||
sub insert_row {
|
||||
|
||||
my $db = &$get_db();
|
||||
my $xa_db = shift // $db;
|
||||
if ('HASH' eq ref $_[0]) {
|
||||
my ($data,$insert_ignore) = @_;
|
||||
check_table();
|
||||
if (insert_record($db,$xa_db,__PACKAGE__,$data,$insert_ignore,$insert_unique_fields)) {
|
||||
return $xa_db->db_last_insert_id();
|
||||
}
|
||||
} else {
|
||||
my %params = @_;
|
||||
my ($contact_id) = @params{qw/
|
||||
contact_id
|
||||
/};
|
||||
|
||||
if ($xa_db->db_do('INSERT INTO ' . $db->tableidentifier($tablename) . ' (' .
|
||||
$db->columnidentifier('contact_id') . ', ' .
|
||||
$db->columnidentifier('create_timestamp') . ', ' .
|
||||
$db->columnidentifier('modify_timestamp') . ', ' .
|
||||
$db->columnidentifier('status') . ') VALUES (' .
|
||||
'?, ' .
|
||||
'NOW(), ' .
|
||||
'NOW(), ' .
|
||||
'\'' . $ACTIVE_STATE . '\')',
|
||||
$contact_id,
|
||||
)) {
|
||||
rowinserted($db,$tablename,getlogger(__PACKAGE__));
|
||||
return $xa_db->db_last_insert_id();
|
||||
}
|
||||
}
|
||||
return undef;
|
||||
|
||||
}
|
||||
|
||||
sub process_records {
|
||||
|
||||
my %params = @_;
|
||||
my ($process_code,
|
||||
$static_context,
|
||||
$init_process_context_code,
|
||||
$uninit_process_context_code,
|
||||
$multithreading,
|
||||
$numofthreads,
|
||||
$load_recursive) = @params{qw/
|
||||
process_code
|
||||
static_context
|
||||
init_process_context_code
|
||||
uninit_process_context_code
|
||||
multithreading
|
||||
numofthreads
|
||||
load_recursive
|
||||
/};
|
||||
|
||||
check_table();
|
||||
|
||||
return process_table(
|
||||
get_db => $get_db,
|
||||
class => __PACKAGE__,
|
||||
process_code => sub {
|
||||
my ($context,$rowblock,$row_offset) = @_;
|
||||
return &$process_code($context,buildrecords_fromrows($rowblock,$load_recursive),$row_offset);
|
||||
},
|
||||
static_context => $static_context,
|
||||
init_process_context_code => $init_process_context_code,
|
||||
uninit_process_context_code => $uninit_process_context_code,
|
||||
destroy_reader_dbs_code => \&destroy_dbs,
|
||||
multithreading => $multithreading,
|
||||
tableprocessing_threads => $numofthreads,
|
||||
);
|
||||
}
|
||||
|
||||
sub process_free_cash_contracts {
|
||||
|
||||
my %params = @_;
|
||||
my ($process_code,
|
||||
$static_context,
|
||||
$init_process_context_code,
|
||||
$uninit_process_context_code,
|
||||
$multithreading,
|
||||
$numofthreads) = @params{qw/
|
||||
process_code
|
||||
static_context
|
||||
init_process_context_code
|
||||
uninit_process_context_code
|
||||
multithreading
|
||||
numofthreads
|
||||
/};
|
||||
|
||||
check_table();
|
||||
my $db = &$get_db();
|
||||
my $table = $db->tableidentifier($tablename);
|
||||
|
||||
my $stmt = 'FROM ' . $table . ' AS c' .
|
||||
' INNER JOIN ' . $db->tableidentifier(NGCP::BulkProcessor::Dao::mr457::billing::billing_mappings::gettablename()) . ' AS bm ON bm.contract_id = c.id' .
|
||||
' INNER JOIN ' . $db->tableidentifier(NGCP::BulkProcessor::Dao::mr457::billing::billing_profiles::gettablename()) . ' AS bp ON bp.id = bm.billing_profile_id' .
|
||||
' WHERE c.status != "terminated" AND bp.interval_free_cash <> 0.0';
|
||||
|
||||
return process_table(
|
||||
get_db => $get_db,
|
||||
class => __PACKAGE__,
|
||||
process_code => sub {
|
||||
my ($context,$rowblock,$row_offset) = @_;
|
||||
return &$process_code($context,$rowblock,$row_offset);
|
||||
},
|
||||
static_context => $static_context,
|
||||
init_process_context_code => $init_process_context_code,
|
||||
uninit_process_context_code => $uninit_process_context_code,
|
||||
destroy_reader_dbs_code => \&destroy_dbs,
|
||||
multithreading => $multithreading,
|
||||
tableprocessing_threads => $numofthreads,
|
||||
select => $db->paginate_sort_query("SELECT DISTINCT c.id " . $stmt,undef,undef,[{ column => 'c.id', numeric => 1, dir => 1 }]),
|
||||
selectcount => "SELECT COUNT(DISTINCT c.id) " . $stmt,
|
||||
);
|
||||
}
|
||||
|
||||
sub buildrecords_fromrows {
|
||||
|
||||
my ($rows,$load_recursive) = @_;
|
||||
|
||||
my @records = ();
|
||||
my $record;
|
||||
|
||||
if (defined $rows and ref $rows eq 'ARRAY') {
|
||||
foreach my $row (@$rows) {
|
||||
$record = __PACKAGE__->new($row);
|
||||
|
||||
# transformations go here ...
|
||||
|
||||
push @records,$record;
|
||||
}
|
||||
}
|
||||
|
||||
return \@records;
|
||||
|
||||
}
|
||||
|
||||
sub gettablename {
|
||||
|
||||
return $tablename;
|
||||
|
||||
}
|
||||
|
||||
sub check_table {
|
||||
|
||||
return checktableinfo($get_db,
|
||||
__PACKAGE__,$tablename,
|
||||
$expected_fieldnames,
|
||||
$indexes);
|
||||
|
||||
}
|
||||
|
||||
1;
|
||||
@ -0,0 +1,144 @@
|
||||
package NGCP::BulkProcessor::Dao::mr457::billing::profile_packages;
|
||||
use strict;
|
||||
|
||||
## no critic
|
||||
|
||||
use NGCP::BulkProcessor::ConnectorPool qw(
|
||||
get_billing_db
|
||||
|
||||
);
|
||||
|
||||
use NGCP::BulkProcessor::SqlProcessor qw(
|
||||
checktableinfo
|
||||
copy_row
|
||||
);
|
||||
use NGCP::BulkProcessor::SqlRecord qw();
|
||||
|
||||
require Exporter;
|
||||
our @ISA = qw(Exporter NGCP::BulkProcessor::SqlRecord);
|
||||
our @EXPORT_OK = qw(
|
||||
gettablename
|
||||
check_table
|
||||
|
||||
findby_id
|
||||
findall
|
||||
|
||||
$CARRY_OVER_MODE
|
||||
$CARRY_OVER_TIMELY_MODE
|
||||
$DISCARD_MODE
|
||||
$DEFAULT_CARRY_OVER_MODE
|
||||
$DEFAULT_INITIAL_BALANCE
|
||||
);
|
||||
|
||||
my $tablename = 'profile_packages';
|
||||
my $get_db = \&get_billing_db;
|
||||
|
||||
my $expected_fieldnames = [
|
||||
'id',
|
||||
'reseller_id',
|
||||
'name',
|
||||
'description',
|
||||
'initial_balance',
|
||||
'service_charge',
|
||||
'balance_interval_unit',
|
||||
'balance_interval_value',
|
||||
'balance_interval_start_mode',
|
||||
'carry_over_mode',
|
||||
'timely_duration_unit',
|
||||
'timely_duration_value',
|
||||
'notopup_discard_intervals',
|
||||
'underrun_lock_threshold',
|
||||
'underrun_lock_level',
|
||||
'underrun_profile_threshold',
|
||||
'topup_lock_level',
|
||||
];
|
||||
|
||||
my $indexes = {};
|
||||
|
||||
our $CARRY_OVER_MODE = 'carry_over';
|
||||
our $CARRY_OVER_TIMELY_MODE = 'carry_over_timely';
|
||||
our $DISCARD_MODE = 'discard';
|
||||
our $DEFAULT_CARRY_OVER_MODE = $CARRY_OVER_MODE;
|
||||
our $DEFAULT_INITIAL_BALANCE = 0.0;
|
||||
|
||||
sub new {
|
||||
|
||||
my $class = shift;
|
||||
my $self = NGCP::BulkProcessor::SqlRecord->new($class,$get_db,
|
||||
$tablename,$expected_fieldnames,$indexes);
|
||||
|
||||
copy_row($self,shift,$expected_fieldnames);
|
||||
|
||||
return $self;
|
||||
|
||||
}
|
||||
|
||||
sub findby_id {
|
||||
|
||||
my ($id,$load_recursive) = @_;
|
||||
|
||||
check_table();
|
||||
my $db = &$get_db();
|
||||
my $table = $db->tableidentifier($tablename);
|
||||
|
||||
my $stmt = 'SELECT * FROM ' . $table . ' WHERE ' .
|
||||
$db->columnidentifier('id') . ' = ?';
|
||||
my @params = ($id);
|
||||
my $rows = $db->db_get_all_arrayref($stmt,@params);
|
||||
|
||||
return buildrecords_fromrows($rows,$load_recursive)->[0];
|
||||
|
||||
}
|
||||
|
||||
sub findall {
|
||||
|
||||
my ($load_recursive) = @_;
|
||||
|
||||
check_table();
|
||||
my $db = &$get_db();
|
||||
my $table = $db->tableidentifier($tablename);
|
||||
|
||||
my $stmt = 'SELECT * FROM ' . $table;
|
||||
my $rows = $db->db_get_all_arrayref($stmt);
|
||||
|
||||
return buildrecords_fromrows($rows,$load_recursive);
|
||||
|
||||
}
|
||||
|
||||
sub buildrecords_fromrows {
|
||||
|
||||
my ($rows,$load_recursive) = @_;
|
||||
|
||||
my @records = ();
|
||||
my $record;
|
||||
|
||||
if (defined $rows and ref $rows eq 'ARRAY') {
|
||||
foreach my $row (@$rows) {
|
||||
$record = __PACKAGE__->new($row);
|
||||
|
||||
# transformations go here ...
|
||||
|
||||
push @records,$record;
|
||||
}
|
||||
}
|
||||
|
||||
return \@records;
|
||||
|
||||
}
|
||||
|
||||
sub gettablename {
|
||||
|
||||
return $tablename;
|
||||
|
||||
}
|
||||
|
||||
sub check_table {
|
||||
|
||||
return checktableinfo($get_db,
|
||||
__PACKAGE__,$tablename,
|
||||
$expected_fieldnames,
|
||||
$indexes);
|
||||
|
||||
}
|
||||
|
||||
1;
|
||||
@ -0,0 +1,232 @@
|
||||
package NGCP::BulkProcessor::Dao::mr457::billing::topup_log;
|
||||
use strict;
|
||||
|
||||
## no critic
|
||||
|
||||
use NGCP::BulkProcessor::Logging qw(
|
||||
getlogger
|
||||
rowinserted
|
||||
);
|
||||
|
||||
use NGCP::BulkProcessor::ConnectorPool qw(
|
||||
get_billing_db
|
||||
|
||||
);
|
||||
|
||||
use NGCP::BulkProcessor::SqlProcessor qw(
|
||||
checktableinfo
|
||||
copy_row
|
||||
|
||||
insert_record
|
||||
);
|
||||
use NGCP::BulkProcessor::SqlRecord qw();
|
||||
|
||||
require Exporter;
|
||||
our @ISA = qw(Exporter NGCP::BulkProcessor::SqlRecord);
|
||||
our @EXPORT_OK = qw(
|
||||
gettablename
|
||||
check_table
|
||||
|
||||
insert_row
|
||||
|
||||
findby_contractidfromto
|
||||
findby_contractbalanceid
|
||||
findby_id
|
||||
|
||||
$OK_OUTCOME
|
||||
$FAILED_OUTCOME
|
||||
|
||||
$VOUCHER_TYPE
|
||||
$CASH_TYPE
|
||||
|
||||
);
|
||||
|
||||
my $tablename = 'topup_log';
|
||||
my $get_db = \&get_billing_db;
|
||||
|
||||
my $expected_fieldnames = [
|
||||
'id',
|
||||
'username',
|
||||
'timestamp',
|
||||
'type',
|
||||
'outcome',
|
||||
'message',
|
||||
'subscriber_id',
|
||||
'contract_id',
|
||||
'amount',
|
||||
'voucher_id',
|
||||
'cash_balance_before',
|
||||
'cash_balance_after',
|
||||
'package_before_id',
|
||||
'package_after_id',
|
||||
'profile_before_id',
|
||||
'profile_after_id',
|
||||
'lock_level_before',
|
||||
'lock_level_after',
|
||||
'contract_balance_before_id',
|
||||
'contract_balance_after_id',
|
||||
'request_token',
|
||||
];
|
||||
|
||||
my $indexes = {};
|
||||
|
||||
my $insert_unique_fields = [];
|
||||
|
||||
our $OK_OUTCOME = 'ok';
|
||||
our $FAILED_OUTCOME = 'failed';
|
||||
|
||||
our $VOUCHER_TYPE = 'voucher';
|
||||
our $CASH_TYPE = 'cash';
|
||||
#our $SET_BALANCE_TYPE = 'set_balance';
|
||||
|
||||
sub new {
|
||||
|
||||
my $class = shift;
|
||||
my $self = NGCP::BulkProcessor::SqlRecord->new($class,$get_db,
|
||||
$tablename,$expected_fieldnames,$indexes);
|
||||
|
||||
copy_row($self,shift,$expected_fieldnames);
|
||||
|
||||
return $self;
|
||||
|
||||
}
|
||||
|
||||
sub findby_contractidfromto {
|
||||
|
||||
my ($contract_id,$from,$to,$outcome,$load_recursive) = @_;
|
||||
|
||||
check_table();
|
||||
my $db = &$get_db();
|
||||
my $table = $db->tableidentifier($tablename);
|
||||
|
||||
my $stmt = 'SELECT * FROM ' . $table . ' WHERE ' .
|
||||
$db->columnidentifier('contract_id') . ' = ?';
|
||||
my @params = ($contract_id);
|
||||
if ($from) {
|
||||
$stmt .= ' AND ' . $db->columnidentifier('timestamp') . ' >= ?';
|
||||
push(@params,$from->epoch());
|
||||
}
|
||||
if ($to) {
|
||||
$stmt .= ' AND ' . $db->columnidentifier('timestamp') . ' <= ?';
|
||||
push(@params,$to->epoch());
|
||||
}
|
||||
if ($outcome) {
|
||||
$stmt .= ' AND ' . $db->columnidentifier('outcome') . ' = ?';
|
||||
push(@params,$outcome);
|
||||
}
|
||||
my $rows = $db->db_get_all_arrayref($stmt,@params);
|
||||
|
||||
return buildrecords_fromrows($rows,$load_recursive);
|
||||
|
||||
}
|
||||
|
||||
sub findby_contractbalanceid {
|
||||
|
||||
my ($id,$outcome,$load_recursive) = @_;
|
||||
|
||||
check_table();
|
||||
my $db = &$get_db();
|
||||
my $table = $db->tableidentifier($tablename);
|
||||
|
||||
my $stmt = 'SELECT * FROM ' . $table . ' WHERE ' .
|
||||
$db->columnidentifier('contract_balance_before_id') . ' = ?';
|
||||
my @params = ($id);
|
||||
if ($outcome) {
|
||||
$stmt .= ' AND ' . $db->columnidentifier('outcome') . ' = ?';
|
||||
push(@params,$outcome);
|
||||
}
|
||||
my $rows = $db->db_get_all_arrayref($stmt,@params);
|
||||
|
||||
return buildrecords_fromrows($rows,$load_recursive);
|
||||
|
||||
}
|
||||
|
||||
sub findby_id {
|
||||
|
||||
my ($id,$load_recursive) = @_;
|
||||
|
||||
check_table();
|
||||
my $db = &$get_db();
|
||||
my $table = $db->tableidentifier($tablename);
|
||||
|
||||
my $stmt = 'SELECT * FROM ' . $table . ' WHERE ' .
|
||||
$db->columnidentifier('id') . ' = ?';
|
||||
my @params = ($id);
|
||||
my $rows = $db->db_get_all_arrayref($stmt,@params);
|
||||
|
||||
return buildrecords_fromrows($rows,$load_recursive)->[0];
|
||||
|
||||
}
|
||||
|
||||
sub insert_row {
|
||||
|
||||
my $db = &$get_db();
|
||||
my $xa_db = shift // $db;
|
||||
if ('HASH' eq ref $_[0]) {
|
||||
my ($data,$insert_ignore) = @_;
|
||||
check_table();
|
||||
if (insert_record($db,$xa_db,__PACKAGE__,$data,$insert_ignore,$insert_unique_fields)) {
|
||||
return $xa_db->db_last_insert_id();
|
||||
}
|
||||
} else {
|
||||
my %params = @_;
|
||||
my ($contract_id) = @params{qw/
|
||||
contract_id
|
||||
/};
|
||||
|
||||
if ($xa_db->db_do('INSERT INTO ' . $db->tableidentifier($tablename) . ' (' .
|
||||
$db->columnidentifier('timestamp') . ', ' .
|
||||
$db->columnidentifier('type') . ', ' .
|
||||
$db->columnidentifier('outcome') .', ' .
|
||||
$db->columnidentifier('contract_id') . ') VALUES (' .
|
||||
'UNIX_TIMESTAMP(NOW()), ' .
|
||||
'\'' . $CASH_TYPE . '\', ' .
|
||||
'\'' . $FAILED_OUTCOME . '\', ' .
|
||||
'?)',
|
||||
$contract_id
|
||||
)) {
|
||||
rowinserted($db,$tablename,getlogger(__PACKAGE__));
|
||||
return $xa_db->db_last_insert_id();
|
||||
}
|
||||
}
|
||||
return undef;
|
||||
|
||||
}
|
||||
|
||||
sub buildrecords_fromrows {
|
||||
|
||||
my ($rows,$load_recursive) = @_;
|
||||
|
||||
my @records = ();
|
||||
my $record;
|
||||
|
||||
if (defined $rows and ref $rows eq 'ARRAY') {
|
||||
foreach my $row (@$rows) {
|
||||
$record = __PACKAGE__->new($row);
|
||||
|
||||
# transformations go here ...
|
||||
|
||||
push @records,$record;
|
||||
}
|
||||
}
|
||||
|
||||
return \@records;
|
||||
|
||||
}
|
||||
|
||||
sub gettablename {
|
||||
|
||||
return $tablename;
|
||||
|
||||
}
|
||||
|
||||
sub check_table {
|
||||
|
||||
return checktableinfo($get_db,
|
||||
__PACKAGE__,$tablename,
|
||||
$expected_fieldnames,
|
||||
$indexes);
|
||||
|
||||
}
|
||||
|
||||
1;
|
||||
@ -0,0 +1,62 @@
|
||||
##general settings:
|
||||
working_path = /home/rkrenn/temp/soco
|
||||
cpucount = 4
|
||||
enablemultithreading = 0
|
||||
|
||||
##gearman/service listener config:
|
||||
jobservers = 127.0.0.1:4730
|
||||
|
||||
#provisioning_conf = /etc/ngcp-panel/provisioning.conf
|
||||
|
||||
##NGCP MySQL connectivity - "accounting" db:
|
||||
accounting_host = 192.168.0.84
|
||||
accounting_port = 3306
|
||||
accounting_databasename = accounting
|
||||
accounting_username = root
|
||||
accounting_password =
|
||||
|
||||
##NGCP MySQL connectivity - "billing" db:
|
||||
billing_host = 192.168.0.84
|
||||
billing_port = 3306
|
||||
billing_databasename = billing
|
||||
billing_username = root
|
||||
billing_password =
|
||||
|
||||
##NGCP MySQL connectivity - "provisioning" db:
|
||||
provisioning_host = 192.168.0.84
|
||||
provisioning_port = 3306
|
||||
provisioning_databasename = provisioning
|
||||
provisioning_username = root
|
||||
provisioning_password =
|
||||
|
||||
##NGCP MySQL connectivity - "kamailio" db:
|
||||
kamailio_host = 192.168.0.84
|
||||
kamailio_port = 3306
|
||||
kamailio_databasename = kamailio
|
||||
kamailio_username = root
|
||||
kamailio_password =
|
||||
|
||||
##NGCP MySQL connectivity - default db for distributed transactions (XA) to connect to:
|
||||
xa_host = 192.168.0.84
|
||||
xa_port = 3306
|
||||
xa_databasename = ngcp
|
||||
xa_username = root
|
||||
xa_password =
|
||||
|
||||
##NGCP REST-API connectivity:
|
||||
ngcprestapi_uri = https://127.0.0.1:1443
|
||||
ngcprestapi_username = administrator
|
||||
ngcprestapi_password = administrator
|
||||
ngcprestapi_realm = api_admin_http
|
||||
|
||||
##sending email:
|
||||
emailenable = 0
|
||||
erroremailrecipient =
|
||||
warnemailrecipient =
|
||||
completionemailrecipient = rkrenn@sipwise.com
|
||||
doneemailrecipient =
|
||||
|
||||
##logging:
|
||||
fileloglevel = DEBUG
|
||||
screenloglevel = INFO
|
||||
emailloglevel = OFF
|
||||
@ -0,0 +1,9 @@
|
||||
|
||||
#dry=0
|
||||
#skip_errors=0
|
||||
|
||||
fix_contract_balance_gaps_numofthreads=2
|
||||
fix_contract_balance_gaps_multithreading=1
|
||||
|
||||
fix_free_cash_numofthreads=2
|
||||
fix_free_cash_multithreading=1
|
||||
Loading…
Reference in new issue