MT#9277 Implement basic voucher management.

ipeshinskaya/9431
Andreas Granig 11 years ago
parent d5ee051bd5
commit 64d65a10a5

@ -0,0 +1,326 @@
package NGCP::Panel::Controller::Voucher;
use Sipwise::Base;
use Text::CSV_XS;
use DateTime::Format::ISO8601;
BEGIN { extends 'Catalyst::Controller'; }
use NGCP::Panel::Form::Voucher::Admin;
use NGCP::Panel::Form::Voucher::Reseller;
use NGCP::Panel::Form::Voucher::Upload;
use NGCP::Panel::Utils::Message;
use NGCP::Panel::Utils::Navigation;
use NGCP::Panel::Utils::Datatables;
use NGCP::Panel::Utils::DateTime;
sub auto :Does(ACL) :ACLDetachTo('/denied_page') :AllowedRole(admin) :AllowedRole(reseller) {
my ($self, $c) = @_;
$c->log->debug(__PACKAGE__ . '::auto');
$c->detach('/denied_page')
unless($c->config->{features}->{voucher});
NGCP::Panel::Utils::Navigation::check_redirect_chain(c => $c);
return 1;
}
sub voucher_list :Chained('/') :PathPart('voucher') :CaptureArgs(0) {
my ( $self, $c ) = @_;
my $voucher_rs = $c->model('DB')->resultset('vouchers');
if($c->user->roles eq "reseller") {
$voucher_rs = $voucher_rs->search({
reseller_id => $c->user->reseller_id,
});
}
$c->stash(voucher_rs => $voucher_rs);
$c->stash->{voucher_dt_columns} = NGCP::Panel::Utils::Datatables::set_columns($c, [
{ name => "id", "search" => 1, "title" => $c->loc("#") },
{ name => "code", "search" => 1, "title" => $c->loc("Code") },
{ name => "amount", "search" => 1, "title" => $c->loc("Amount") },
{ name => "reseller.name", "search" => 1, "title" => $c->loc("Reseller") },
{ name => "valid_until", "search" => 1, "title" => $c->loc("Valid Until") },
{ name => "used_at", "search" => 1, "title" => $c->loc("Used At") },
{ name => "used_by_subscriber.id", "search" => 1, "title" => $c->loc("Used By Subscriber #") },
]);
$c->stash(template => 'voucher/list.tt');
}
sub root :Chained('voucher_list') :PathPart('') :Args(0) {
my ($self, $c) = @_;
}
sub ajax :Chained('voucher_list') :PathPart('ajax') :Args(0) {
my ($self, $c) = @_;
my $resultset = $c->stash->{voucher_rs};
NGCP::Panel::Utils::Datatables::process($c, $resultset, $c->stash->{voucher_dt_columns});
$c->detach( $c->view("JSON") );
}
sub base :Chained('voucher_list') :PathPart('') :CaptureArgs(1) {
my ($self, $c, $voucher_id) = @_;
unless($voucher_id && $voucher_id->is_integer) {
NGCP::Panel::Utils::Message->error(
c => $c,
data => { id => $voucher_id },
desc => $c->loc('Invalid voucher id detected!'),
);
$c->response->redirect($c->uri_for());
return;
}
my $res = $c->stash->{voucher_rs}->find($voucher_id);
unless(defined($res)) {
NGCP::Panel::Utils::Message->error(
c => $c,
data => { id => $voucher_id },
desc => $c->loc('Billing Voucher does not exist!'),
);
$c->response->redirect($c->uri_for());
return;
}
$c->stash(voucher => {$res->get_inflated_columns});
$c->stash(voucher_result => $res);
}
sub delete :Chained('base') :PathPart('delete') {
my ($self, $c) = @_;
try {
$c->stash->{voucher_result}->delete;
NGCP::Panel::Utils::Message->info(
c => $c,
desc => $c->loc('Billing Voucher successfully deleted'),
);
} catch($e) {
NGCP::Panel::Utils::Message->error(
c => $c,
data => { id => $$c->stash->{voucher_result}->id },
desc => $c->loc('Failed to delete Billing Voucher'),
);
}
$c->response->redirect($c->uri_for());
return;
}
sub edit :Chained('base') :PathPart('edit') {
my ($self, $c) = @_;
my $posted = ($c->request->method eq 'POST');
my $form;
my $params = $c->stash->{voucher};
$params->{reseller}{id} = delete $params->{reseller_id};
$params = $params->merge($c->session->{created_objects});
if($c->user->is_superuser) {
$form = NGCP::Panel::Form::Voucher::Admin->new;
} else {
$form = NGCP::Panel::Form::Voucher::Reseller->new;
}
$form->process(
posted => $posted,
params => $c->request->params,
item => $params,
);
NGCP::Panel::Utils::Navigation::check_form_buttons(
c => $c,
form => $form,
fields => {
'reseller.create' => $c->uri_for('/reseller/create'),
'customer.create' => $c->uri_for('/customer/create'),
},
back_uri => $c->req->uri,
);
if($posted && $form->validated) {
try {
if($c->user->is_superuser) {
$form->values->{reseller_id} = $form->values->{reseller}{id};
} else {
$form->values->{reseller_id} = $c->user->reseller_id;
}
delete $form->values->{reseller};
$form->values->{customer_id} = $form->values->{customer}{id};
delete $form->values->{customer};
if($form->values->{valid_until} =~ /^\d{4}\-\d{2}\-\d{2}$/) {
$form->values->{valid_until} = NGCP::Panel::Utils::DateTime::from_string($form->values->{valid_until})
->add(days => 1)->subtract(seconds => 1);
}
my $schema = $c->model('DB');
$schema->txn_do(sub {
$c->stash->{voucher_result}->update($form->values);
});
delete $c->session->{created_objects}->{reseller};
NGCP::Panel::Utils::Message->info(
c => $c,
desc => $c->loc('Billing voucher successfully updated'),
);
} catch($e) {
NGCP::Panel::Utils::Message->error(
c => $c,
error => $e,
desc => $c->loc('Failed to update billing voucher'),
);
}
NGCP::Panel::Utils::Navigation::back_or($c, $c->uri_for('/voucher'));
}
$c->stash(edit_flag => 1);
$c->stash(form => $form);
}
sub create :Chained('voucher_list') :PathPart('create') :Args(0) {
my ($self, $c) = @_;
my $posted = ($c->request->method eq 'POST');
my $form;
my $params = {};
$params->{reseller}{id} = delete $params->{reseller_id};
$params = $params->merge($c->session->{created_objects});
if($c->user->is_superuser) {
$form = NGCP::Panel::Form::Voucher::Admin->new;
} else {
$form = NGCP::Panel::Form::Voucher::Reseller->new;
}
$form->process(
posted => $posted,
params => $c->request->params,
item => $params,
);
NGCP::Panel::Utils::Navigation::check_form_buttons(
c => $c,
form => $form,
fields => {
'reseller.create' => $c->uri_for('/reseller/create'),
'customer.create' => $c->uri_for('/customer/create'),
},
back_uri => $c->req->uri,
);
if($posted && $form->validated) {
try {
if($c->user->is_superuser) {
$form->values->{reseller_id} = $form->values->{reseller}{id};
} else {
$form->values->{reseller_id} = $c->user->reseller_id;
}
delete $form->values->{reseller};
$form->values->{customer_id} = $form->values->{customer}{id};
delete $form->values->{customer};
$form->values->{created_at} = NGCP::Panel::Utils::DateTime::current_local;
if($form->values->{valid_until} =~ /^\d{4}\-\d{2}\-\d{2}$/) {
$form->values->{valid_until} = NGCP::Panel::Utils::DateTime::from_string($form->values->{valid_until})
->add(days => 1)->subtract(seconds => 1);
}
my $voucher = $c->model('DB')->resultset('vouchers')->create($form->values);
$c->session->{created_objects}->{voucher} = { id => $voucher->id };
delete $c->session->{created_objects}->{reseller};
NGCP::Panel::Utils::Message->info(
c => $c,
desc => $c->loc('Billing voucher successfully created'),
);
} catch($e) {
NGCP::Panel::Utils::Message->error(
c => $c,
error => $e,
desc => $c->loc('Failed to create billing voucher'),
);
}
NGCP::Panel::Utils::Navigation::back_or($c, $c->uri_for('/voucher'));
}
$c->stash(create_flag => 1);
$c->stash(form => $form);
}
sub voucher_upload :Chained('voucher_list') :PathPart('upload') :Args(0) {
my ($self, $c) = @_;
my $form = NGCP::Panel::Form::Voucher::Upload->new;
my $upload = $c->req->upload('upload_vouchers');
my $posted = $c->req->method eq 'POST';
my @params = (
upload_fees => $posted ? $upload : undef,
);
$form->process(
posted => $posted,
params => { @params },
action => $c->uri_for_action('/voucher/voucher_upload', $c->req->captures),
);
if($posted && $form->validated) {
unless($upload) {
NGCP::Panel::Utils::Message->error(
c => $c,
desc => $c->loc('No Billing Voucher file specified!'),
);
$c->response->redirect($c->uri_for('/voucher'));
return;
}
my $csv = Text::CSV_XS->new({allow_whitespace => 1, binary => 1, keep_meta_info => 1});
my @cols = $c->config->{voucher_csv}->{element_order};
if($c->user->roles eq "admin") {
unshift @{ $cols[0] }, 'reseller_id';
}
$csv->column_names (@cols);
my @fails = ();
my $linenum = 0;
my @vouchers = ();
try {
$c->model('DB')->txn_do(sub {
if ($c->req->params->{purge_existing}) {
$c->stash->{'voucher_rs'}->delete;
}
while(my $row = $csv->getline_hr($upload->fh)) {
++$linenum;
if($csv->is_missing(1)) {
push @fails, $linenum;
next;
}
if($row->{valid_until} =~ /^\d{4}\-\d{2}\-\d{2}$/) {
$row->{valid_until} = NGCP::Panel::Utils::DateTime::from_string($row->{valid_until})
->add(days => 1)->subtract(seconds => 1);
}
$row->{customer_id} = undef if(defined $row->{customer_id} && $row->{customer_id} eq "");
push @vouchers, $row;
}
unless ($csv->eof()) {
die "Some lines could not be parsed. Did not reach eof. Last successful: $linenum.";
}
$c->stash->{voucher_rs}->populate(\@vouchers);
});
my $text = $c->loc('Billing Vouchers successfully uploaded');
if(@fails) {
$text .= $c->loc(", but skipped the following line numbers: ") . (join ", ", @fails);
}
NGCP::Panel::Utils::Message->info(
c => $c,
desc => $text,
);
} catch($e) {
NGCP::Panel::Utils::Message->error(
c => $c,
error => $e,
desc => $c->loc('Failed to upload Billing Vouchers'),
);
};
$c->response->redirect($c->uri_for('/voucher'));
return;
}
$c->stash(create_flag => 1);
$c->stash(form => $form);
}
__PACKAGE__->meta->make_immutable;
1;
# vim: set tabstop=4 expandtab:

@ -0,0 +1,23 @@
package NGCP::Panel::Form::Voucher::Admin;
use HTML::FormHandler::Moose;
extends 'NGCP::Panel::Form::Voucher::Reseller';
has_field 'reseller' => (
type => '+NGCP::Panel::Field::Reseller',
validate_when_empty => 1,
element_attr => {
rel => ['tooltip'],
title => ['The reseller id this voucher belongs to.']
},
);
has_block 'fields' => (
tag => 'div',
class => [qw/modal-body/],
render_list => [qw/reseller code amount valid_until customer/],
);
1;
# vim: set tabstop=4 expandtab:

@ -0,0 +1,85 @@
package NGCP::Panel::Form::Voucher::Reseller;
use HTML::FormHandler::Moose;
extends 'HTML::FormHandler';
use Moose::Util::TypeConstraints;
use HTML::FormHandler::Widget::Block::Bootstrap;
has '+widget_wrapper' => ( default => 'Bootstrap' );
has_field 'submitid' => ( type => 'Hidden' );
sub build_render_list {[qw/submitid fields actions/]}
sub build_form_element_class { [qw/form-horizontal/] }
has_field 'id' => (
type => 'Hidden'
);
has_field 'code' => (
type => 'Text',
required => 1,
maxlength => 255,
element_attr => {
rel => ['tooltip'],
title => ['The voucher code.']
},
);
has_field 'amount' => (
type => 'Money',
required => 1,
element_attr => {
rel => ['tooltip'],
title => ['The amount of the voucher in cents of Euro/USD/etc.']
},
default => '0',
);
has_field 'valid_until' => (
type => '+NGCP::Panel::Field::DatePicker',
required => 1,
element_attr => {
rel => ['tooltip'],
title => ['The date until this voucher is valid.']
},
);
has_field 'customer' => (
type => '+NGCP::Panel::Field::CustomerContract',
element_attr => {
rel => ['tooltip'],
title => ['The customer contract this voucher can be used by (optional).']
},
);
has_field 'save' => (
type => 'Submit',
value => 'Save',
element_class => [qw/btn btn-primary/],
label => '',
);
has_block 'fields' => (
tag => 'div',
class => [qw/modal-body/],
render_list => [qw/code amount valid_until customer/],
);
has_block 'actions' => (
tag => 'div',
class => [qw/modal-footer/],
render_list => [qw/save/],
);
sub validate_valid_until {
my ($self, $field) = @_;
unless($field->value =~ /^\d{4}\-\d{2}\-\d{2}$/) {
my $err_msg = 'Invalid date format, must be YYYY-MM-DD';
$field->add_error($err_msg);
}
}
1
# vim: set tabstop=4 expandtab:

@ -0,0 +1,45 @@
package NGCP::Panel::Form::Voucher::Upload;
use Sipwise::Base;
use HTML::FormHandler::Moose;
extends 'HTML::FormHandler';
use Moose::Util::TypeConstraints;
use HTML::FormHandler::Widget::Block::Bootstrap;
use NGCP::Panel::Field::BillingZone;
has '+widget_wrapper' => ( default => 'Bootstrap' );
has '+enctype' => ( default => 'multipart/form-data');
has_field 'submitid' => ( type => 'Hidden' );
sub build_render_list {[qw/submitid fields actions/]}
sub build_form_element_class { [qw/form-horizontal/] }
has_field 'upload_vouchers' => (
type => 'Upload',
max_size => '2000000',
);
has_field 'purge_existing' => (
type => 'Boolean',
);
has_field 'save' => (
type => 'Submit',
value => 'Upload',
element_class => [qw/btn btn-primary/],
do_label => 0,
);
has_block 'fields' => (
tag => 'div',
class => [qw/modal-body/],
render_list => [qw/ upload_vouchers purge_existing /],
);
has_block 'actions' => (
tag => 'div',
class => [qw/modal-footer/],
render_list => [qw/save/],
);
1;

@ -22,6 +22,7 @@ sub from_string {
# if date is passed like xxxx-xx (as from monthpicker field), add a day
$s = $s . "-01" if($s =~ /^\d{4}\-\d{2}$/);
$s = $s . "T00:00:00" if($s =~ /^\d{4}\-\d{2}-\d{2}$/);
# just for convenience, if date is passed like xxxx-xx-xx xx:xx:xx,
# convert it to xxxx-xx-xxTxx:xx:xx

@ -29,6 +29,7 @@ log4perl.appender.Default.layout.ConversionPattern=%d{ISO8601} [%p] [%F +%L] %m{
cloudpbx 1
callingcard 1
voucher 1
</features>
<faxserver>
@ -40,6 +41,15 @@ log4perl.appender.Default.layout.ConversionPattern=%d{ISO8601} [%p] [%F +%L] %m{
sync /etc/ngcp-panel/check-sync/check-sync
</cloudpbx>
# if uploading as admin, a column reseller_id is added at the beginning
# automatically, so the csv must in addition have reseller_id as first element
<voucher_csv>
element_order code
element_order amount
element_order valid_until
element_order customer_id
</voucher_csv>
<fees_csv>
element_order source
element_order destination

@ -0,0 +1,28 @@
[% site_config.title = c.loc('Billing Vouchers') -%]
[%
helper.name = c.loc('Billing Vouchers');
helper.identifier = 'billing_vouchers';
helper.messages = messages;
helper.dt_columns = voucher_dt_columns;
helper.length_change = 1;
helper.close_target = close_target;
helper.create_flag = create_flag;
helper.edit_flag = edit_flag;
helper.form_object = form;
helper.ajax_uri = c.uri_for( c.controller.action_for('ajax') );
UNLESS c.user.read_only;
helper.dt_buttons = [
{ name = c.loc('Edit'), uri = "/voucher/'+full[\"id\"]+'/edit", class = 'btn-small btn-primary', icon = 'icon-edit' },
{ name = c.loc('Delete'), uri = "/voucher/'+full[\"id\"]+'/delete", class = 'btn-small btn-secondary', icon = 'icon-trash' },
];
helper.top_buttons = [
{ name = c.loc('Create Billing Voucher'), uri = c.uri_for('/voucher/create'), icon = 'icon-star' },
{ name = c.loc('Upload Vouchers as CSV'), uri = c.uri_for('/voucher/upload'), icon = 'icon-star' },
];
END;
PROCESS 'helpers/datatables.tt';
-%]
[% # vim: set tabstop=4 syntax=html expandtab: -%]

@ -30,6 +30,9 @@
<li><a href="[% c.uri_for('/billing') %]">[% c.loc('Billing') %]</a></li>
<li><a href="[% c.uri_for('/invoicetemplate') %]">[% c.loc('Invoice Templates') %]</a></li>
<li><a href="[% c.uri_for('/invoice') %]">[% c.loc('Invoices') %]</a></li>
[% IF c.config.features.voucher -%]
<li><a href="[% c.uri_for('/voucher') %]">[% c.loc('Vouchers') %]</a></li>
[% END -%]
<li><a href="[% c.uri_for('/peering') %]">[% c.loc('Peerings') %]</a></li>
<li><a href="[% c.uri_for('/rewrite') %]">[% c.loc('Rewrite Rule Sets') %]</a></li>
<li><a href="[% c.uri_for('/ncos') %]">[% c.loc('NCOS Levels') %]</a></li>

@ -15,6 +15,9 @@
<li><a href="[% c.uri_for('/billing') %]">[% c.loc('Billing') %]</a></li>
<li><a href="[% c.uri_for('/invoicetemplate') %]">[% c.loc('Invoice Templates') %]</a></li>
<li><a href="[% c.uri_for('/invoice') %]">[% c.loc('Invoices') %]</a></li>
[% IF c.config.features.voucher -%]
<li><a href="[% c.uri_for('/voucher') %]">[% c.loc('Vouchers') %]</a></li>
[% END -%]
<li><a href="[% c.uri_for('/rewrite') %]">[% c.loc('Rewrite Rule Sets') %]</a></li>
<li><a href="[% c.uri_for('/ncos') %]">[% c.loc('NCOS Levels') %]</a></li>
<li><a href="[% c.uri_for('/sound') %]">[% c.loc('Sound Sets') %]</a></li>

Loading…
Cancel
Save