You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ngcp-panel/lib/NGCP/Panel/Controller/Customer.pm

354 lines
13 KiB

package NGCP::Panel::Controller::Customer;
use Sipwise::Base;
use namespace::sweep;
BEGIN { extends 'Catalyst::Controller'; }
use NGCP::Panel::Utils::Contract;
use NGCP::Panel::Form::CustomerMonthlyFraud;
use NGCP::Panel::Form::CustomerDailyFraud;
use NGCP::Panel::Form::CustomerBalance;
use NGCP::Panel::Form::CustomerSubscriber;
use NGCP::Panel::Utils::Navigation;
use UUID qw/generate unparse/;
=head1 NAME
NGCP::Panel::Controller::Customer - Catalyst Controller
=head1 DESCRIPTION
Catalyst Controller.
=head1 METHODS
=cut
sub auto :Does(ACL) :ACLDetachTo('/denied_page') :AllowedRole(admin) :AllowedRole(reseller) {
my ($self, $c) = @_;
$c->log->debug(__PACKAGE__ . '::auto');
return 1;
}
sub list_customer :Chained('/') :PathPart('customer') :CaptureArgs(0) {
my ($self, $c) = @_;
$c->stash(
template => 'customer/list.tt'
);
NGCP::Panel::Utils::Navigation::check_redirect_chain(c => $c);
}
sub root :Chained('list_customer') :PathPart('') :Args(0) {
my ($self, $c) = @_;
}
sub base :Chained('list_customer') :PathPart('') :CaptureArgs(1) {
my ($self, $c, $contract_id) = @_;
unless($contract_id && $contract_id->is_integer) {
$c->flash(messages => [{type => 'error', text => 'Invalid contract id detected!'}]);
$c->response->redirect($c->uri_for());
return;
}
my $contract = $c->model('DB')->resultset('contracts')
->search_rs(id => $contract_id);
my $stime = DateTime->now->truncate(to => 'month');
my $etime = $stime->clone->add(months => 1);
my $balance = $contract->first->contract_balances
->find({
start => { '>=' => $stime },
end => { '<' => $etime },
});
unless($balance) {
try {
NGCP::Panel::Utils::Contract::create_contract_balance(
c => $c,
profile => $contract->first->billing_mappings->search({
-and => [
-or => [
start_date => undef,
start_date => { '<=' => DateTime->now },
],
-or => [
end_date => undef,
end_date => { '>=' => DateTime->now },
]
],
},
{
order_by => { -desc => 'start_time', -desc => 'id' }
})->first->billing_profile,
contract => $contract->first,
);
} catch($e) {
$c->log->error("Failed to create contract balance: $e");
$c->flash(messages => [{type => 'error', text => 'Failed to create contract balance!'}]);
$c->response->redirect($c->uri_for());
return;
}
$balance = $contract->first->contract_balances
->find({
start => { '>=' => $stime },
end => { '<' => $etime },
});
}
my @subscribers = ();
foreach my $s($contract->first->voip_subscribers->search_rs({ status => 'active' })->all) {
my $sub = { $s->get_columns };
$sub->{domain} = $s->domain->domain;
$sub->{primary_number} = {$s->primary_number->get_columns} if(defined $s->primary_number);
$sub->{locations} = [ map { { $_->get_columns } } $c->model('DB')->resultset('location')->
search({
username => $s->username,
domain => $s->domain->domain,
})->all ];
push @subscribers, $sub;
}
$c->stash->{subscribers} = \@subscribers;
$c->stash(balance => $balance);
$c->stash(fraud => $contract->first->contract_fraud_preference);
$c->stash(template => 'customer/details.tt');
$c->stash(contract => $contract->first);
$c->stash(contract_rs => $contract);
}
sub details :Chained('base') :PathPart('details') :Args(0) {
my ($self, $c) = @_;
}
sub subscriber_create :Chained('base') :PathPart('subscriber/create') :Args(0) {
my ($self, $c) = @_;
my $form = NGCP::Panel::Form::CustomerSubscriber->new;
$form->process(
posted => ($c->request->method eq 'POST'),
params => $c->request->params,
);
return if NGCP::Panel::Utils::Navigation::check_form_buttons(
c => $c,
form => $form,
fields => [qw/domain.create/],
back_uri => $c->uri_for_action('/customer/subscriber_create', [ $c->req->captures->[0]]),
);
if($form->validated) {
my $schema = $c->model('DB');
my $contract = $c->stash->{contract};
my $reseller = $contract->reseller;
my $billing_domain = $schema->resultset('domains')
->find($c->request->params->{'domain.id'});
my $prov_domain = $schema->resultset('voip_domains')
->find({domain => $billing_domain->domain});
try {
$schema->txn_do(sub {
my ($uuid_bin, $uuid_string);
UUID::generate($uuid_bin);
UUID::unparse($uuid_bin, $uuid_string);
# TODO: check if we find a reseller and contract and domains
my $number; my $cli = 0;
if(defined $c->request->params->{'e164.cc'} &&
$c->request->params->{'e164.cc'} ne '') {
$cli = $c->request->params->{'e164.cc'} .
($c->request->params->{'e164.ac'} || '') .
$c->request->params->{'e164.sn'};
$number = $reseller->voip_numbers->create({
cc => $c->request->params->{'e164.cc'},
ac => $c->request->params->{'e164.ac'} || '',
sn => $c->request->params->{'e164.sn'},
status => 'active',
});
}
my $billing_subscriber = $contract->voip_subscribers->create({
uuid => $uuid_string,
username => $c->request->params->{username},
domain_id => $billing_domain->id,
status => $c->request->params->{status},
primary_number_id => defined $number ? $number->id : undef,
});
if(defined $number) {
$number->update({ subscriber_id => $billing_subscriber->id });
}
my $prov_subscriber = $schema->resultset('provisioning_voip_subscribers')->create({
uuid => $uuid_string,
username => $c->request->params->{username},
password => $c->request->params->{password},
webusername => $c->request->params->{webusername} || $c->request->params->{username},
webpassword => $c->request->params->{webpassword},
admin => $c->request->params->{administrative} || 0,
account_id => $contract->id,
domain_id => $prov_domain->id,
});
my $voip_preferences = $schema->resultset('voip_preferences')->search({
'usr_pref' => 1,
});
$voip_preferences->find({ 'attribute' => 'account_id' })
->voip_usr_preferences->create({
'subscriber_id' => $prov_subscriber->id,
'value' => $contract->id,
});
$voip_preferences->find({ 'attribute' => 'ac' })
->voip_usr_preferences->create({
'subscriber_id' => $prov_subscriber->id,
'value' => $c->request->params->{'e164.ac'},
}) if (defined $c->request->params->{'e164.ac'} &&
length($c->request->params->{'e164.ac'}) > 0);
if(defined $c->request->params->{'e164.cc'} &&
length($c->request->params->{'e164.cc'}) > 0) {
$voip_preferences->find({ 'attribute' => 'cc' })
->voip_usr_preferences->create({
'subscriber_id' => $prov_subscriber->id,
'value' => $c->request->params->{'e164.cc'},
});
$cli = $c->request->params->{'e164.cc'} .
(defined $c->request->params->{'e164.ac'} &&
length($c->request->params->{'e164.ac'}) > 0 ?
$c->request->params->{'e164.ac'} : ''
) .
$c->request->params->{'e164.sn'};
$voip_preferences->find({ 'attribute' => 'cli' })
->voip_usr_preferences->create({
'subscriber_id' => $prov_subscriber->id,
'value' => $cli,
});
}
$schema->resultset('voicemail_users')->create({
customer_id => $uuid_string,
mailbox => $cli,
password => sprintf("%04d", int(rand 10000)),
email => '',
});
});
$c->flash(messages => [{type => 'success', text => 'Subscriber successfully created!'}]);
$c->response->redirect($c->uri_for_action('/customer/details', [$contract->id]));
return;
} catch($e) {
$c->log->error("Failed to create subscriber: $e");
$c->flash(messages => [{type => 'error', text => 'Creating subscriber failed!'}]);
$c->response->redirect($c->uri_for_action('/customer/details', [$contract->id]));
return;
}
}
$c->stash(close_target => $c->uri_for());
$c->stash(create_flag => 1);
$c->stash(form => $form)
}
sub edit_fraud :Chained('base') :PathPart('fraud/edit') :Args(1) {
my ($self, $c, $type) = @_;
my $posted = ($c->request->method eq 'POST');
my $form;
if($type eq "month") {
$form = NGCP::Panel::Form::CustomerMonthlyFraud->new;
} elsif($type eq "day") {
$form = NGCP::Panel::Form::CustomerDailyFraud->new;
} else {
$c->flash(messages => [{type => 'error', text => "Invalid fraud interval '$type'!"}]);
$c->response->redirect($c->uri_for_action("/customer/details", [$c->stash->{contract}->id]));
return;
}
my $fraud_prefs = $c->stash->{fraud} ||
$c->model('DB')->resultset('contract_fraud_preferences')
->new_result({ contract_id => $c->stash->{contract}->id});
$form->process(
posted => $posted,
params => $c->request->params,
action => $c->uri_for_action("/customer/edit_fraud", $c->stash->{contract}->id, $type),
item => $fraud_prefs,
);
if($posted && $form->validated) {
$c->flash(messages => [{type => 'success', text => 'Fraud settings successfully changed!'}]);
$c->response->redirect($c->uri_for_action("/customer/details", [$c->stash->{contract}->id]));
return;
}
$c->stash(close_target => $c->uri_for_action("/customer/details", [$c->stash->{contract}->id]));
$c->stash(form => $form);
$c->stash(edit_flag => 1);
}
sub delete_fraud :Chained('base') :PathPart('fraud/delete') :Args(1) {
my ($self, $c, $type) = @_;
if($type eq "month") {
$type = "interval";
} elsif($type eq "day") {
$type = "daily";
} else {
$c->flash(messages => [{type => 'error', text => "Invalid fraud interval '$type'!"}]);
$c->response->redirect($c->uri_for_action("/customer/details", [$c->stash->{contract}->id]));
return;
}
my $fraud_prefs = $c->stash->{fraud};
if($fraud_prefs) {
try {
$fraud_prefs->update({
"fraud_".$type."_limit" => undef,
"fraud_".$type."_lock" => undef,
"fraud_".$type."_notify" => undef,
});
} catch($e) {
$c->log->error("Failed to clear fraud interval: $e");
$c->flash(messages => [{type => 'error', text => "Failed to clear fraud interval!"}]);
$c->response->redirect($c->uri_for_action("/customer/details", [$c->stash->{contract}->id]));
return;
}
}
$c->flash(messages => [{type => 'success', text => "Successfully cleared fraud interval!"}]);
$c->response->redirect($c->uri_for_action("/customer/details", [$c->stash->{contract}->id]));
return;
}
sub edit_balance :Chained('base') :PathPart('balance/edit') :Args(0) {
my ($self, $c) = @_;
my $posted = ($c->request->method eq 'POST');
my $form = NGCP::Panel::Form::CustomerBalance->new;
$form->process(
posted => $posted,
params => $c->request->params,
action => $c->uri_for_action("/customer/edit_balance", [$c->stash->{contract}->id]),
item => $c->stash->{balance},
);
if($posted && $form->validated) {
$c->flash(messages => [{type => 'success', text => 'Account balance successfully changed!'}]);
$c->response->redirect($c->uri_for_action("/customer/details", [$c->stash->{contract}->id]));
return;
}
$c->stash(close_target => $c->uri_for_action("/customer/details", [$c->stash->{contract}->id]));
$c->stash(form => $form);
$c->stash(edit_flag => 1);
}
=head1 AUTHOR
Andreas Granig,,,
=head1 LICENSE
This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
__PACKAGE__->meta->make_immutable;
1;
# vim: set tabstop=4 expandtab: