provide CSV upload of billing_fees

using Text::CSV_XS
agranig/1_0_subfix
Gerhard Jungwirth 12 years ago
parent e7d92ff670
commit abfadb4988

@ -42,6 +42,7 @@ my $builder = Local::Module::Build->new(
'strict' => 0,
'Template' => 0,
'warnings' => 0,
'Text::CSV_XS' => 0,
},
test_requires => {
'Catalyst::Test' => 0,

@ -1,6 +1,7 @@
package NGCP::Panel::Controller::Billing;
use Sipwise::Base;
use namespace::autoclean;
use Text::CSV_XS;
BEGIN { extends 'Catalyst::Controller'; }
@ -10,6 +11,7 @@ use NGCP::Panel::Form::BillingZone;
use NGCP::Panel::Form::BillingPeaktimeWeekdays;
use NGCP::Panel::Utils;
use NGCP::Panel::Form::BillingPeaktimeSpecial;
use NGCP::Panel::Form::BillingFeeUpload;
my @WEEKDAYS = qw(Monday Tuesday Wednesday Thursday Friday Saturday Sunday);
@ -185,6 +187,50 @@ sub fees_create :Chained('fees_list') :PathPart('create') :Args(0) {
$c->stash(form => $form);
}
sub fees_upload :Chained('fees_list') :PathPart('upload') :Args(0) {
my ($self, $c) = @_;
my $form = NGCP::Panel::Form::BillingFeeUpload->new;
my $upload = $c->req->upload('upload_fees');
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('/billing/fees_upload', $c->req->captures),
);
if($form->validated) {
my $csv = Text::CSV_XS->new({allow_whitespace => 1, binary => 1});
my @cols = $c->config->{fees_csv}->{element_order};
$csv->column_names (@cols);
if ($c->req->params->{purge_existing}) {
$c->stash->{'profile_result'}->billing_fees->delete_all;
}
while (my $row = $csv->getline_hr($upload->fh)) {
my $zone = $c->stash->{'profile_result'}
->billing_zones
->find_or_create({
zone => $row->{zone},
detail => $row->{zone_detail}
});
$row->{billing_zone_id} = $zone->id;
delete $row->{zone};
delete $row->{zone_detail};
$c->stash->{'profile_result'}
->billing_fees->create($row);
}
$c->flash(messages => [{type => 'success', text => 'Billing Fee successfully uploaded!'}]);
$c->response->redirect($c->uri_for($c->stash->{profile}->{id}, 'fees'));
return;
}
$c->stash(close_target => $c->uri_for($c->stash->{profile}->{id}, 'fees'));
$c->stash(create_flag => 1);
$c->stash(form => $form);
}
sub fees_edit :Chained('fees_base') :PathPart('edit') :Args(0) {
my ($self, $c) = @_;
@ -505,7 +551,7 @@ sub peaktime_specials_create :Chained('peaktimes_list') :PathPart('create') :Arg
$c->stash(peaktimes_special_createflag => 1);
}
__PACKAGE__->meta->make_immutable;
$CLASS->meta->make_immutable;
1;
@ -566,6 +612,11 @@ Get billing_fees and output them as JSON.
Show a modal to add a new billing_fee.
=head2 fees_upload
Show a modal to upload a CSV file of billing_fees and add them to the
Database.
=head2 fees_edit
Show a modal to edit a billing_fee.

@ -0,0 +1,69 @@
package NGCP::Panel::Form::BillingFeeUpload;
use HTML::FormHandler::Moose;
use 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');
sub build_render_list {[qw/fields actions/]}
sub build_form_element_class { [qw/form-horizontal/] }
has_field 'upload_fees' => (
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_fees purge_existing /],
);
has_block 'actions' => (
tag => 'div',
class => [qw/modal-footer/],
render_list => [qw/save/],
);
1;
__END__
=head1 NAME
NGCP::Panel::Form::BillingPeaktimeSpecial
=head1 DESCRIPTION
Preferences Form.
=head1 METHODS
=head1 AUTHOR
Gerhard Jungwirth
=head1 LICENSE
This library is free software. You can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
# vim: set tabstop=4 expandtab:

@ -17,3 +17,22 @@ log4perl.appender.Default.layout.ConversionPattern=%d{ISO8601} [%p] [%F +%L] %m{
<Model::provisioning>
schema_class NGCP::Schema::provisioning
</Model::provisioning>
<fees_csv>
element_order source
element_order destination
element_order direction
element_order zone
element_order zone_detail
element_order onpeak_init_rate
element_order onpeak_init_interval
element_order onpeak_follow_rate
element_order onpeak_follow_interval
element_order offpeak_init_rate
element_order offpeak_init_interval
element_order offpeak_follow_rate
element_order offpeak_follow_interval
element_order use_free_time
</fees_csv>

@ -1,6 +1,8 @@
[% META title = 'Billing Fees' -%]
<a href="[% c.uri_for_action('/billing/zones',[c.req.captures.0]) %]" class="btn btn-tertiary btn-large">Edit Zones</a>
<a href="[% c.uri_for_action('/billing/fees_upload',[c.req.captures.0]) %]" class="btn btn-tertiary btn-large">Upload CSV Fees</a>
[%
helper.name = 'Billing Fees';
helper.messages = messages;

Loading…
Cancel
Save