From abfadb4988794471e26e3d5aa02718535417bce4 Mon Sep 17 00:00:00 2001 From: Gerhard Jungwirth Date: Fri, 17 May 2013 10:49:33 +0200 Subject: [PATCH] provide CSV upload of billing_fees using Text::CSV_XS --- Build.PL | 1 + lib/NGCP/Panel/Controller/Billing.pm | 53 ++++++++++++++++++- lib/NGCP/Panel/Form/BillingFeeUpload.pm | 69 +++++++++++++++++++++++++ ngcp_panel.conf | 19 +++++++ share/templates/billing/fees.tt | 2 + 5 files changed, 143 insertions(+), 1 deletion(-) create mode 100644 lib/NGCP/Panel/Form/BillingFeeUpload.pm diff --git a/Build.PL b/Build.PL index d0d4dac4d7..7784068d89 100644 --- a/Build.PL +++ b/Build.PL @@ -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, diff --git a/lib/NGCP/Panel/Controller/Billing.pm b/lib/NGCP/Panel/Controller/Billing.pm index 62d4ffc50a..34f0e19c9a 100644 --- a/lib/NGCP/Panel/Controller/Billing.pm +++ b/lib/NGCP/Panel/Controller/Billing.pm @@ -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. diff --git a/lib/NGCP/Panel/Form/BillingFeeUpload.pm b/lib/NGCP/Panel/Form/BillingFeeUpload.pm new file mode 100644 index 0000000000..94566e39c1 --- /dev/null +++ b/lib/NGCP/Panel/Form/BillingFeeUpload.pm @@ -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: diff --git a/ngcp_panel.conf b/ngcp_panel.conf index 17fdf57781..21910fa790 100644 --- a/ngcp_panel.conf +++ b/ngcp_panel.conf @@ -17,3 +17,22 @@ log4perl.appender.Default.layout.ConversionPattern=%d{ISO8601} [%p] [%F +%L] %m{ schema_class NGCP::Schema::provisioning + + + + 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 + + diff --git a/share/templates/billing/fees.tt b/share/templates/billing/fees.tt index 8d892bd80e..10a3923084 100644 --- a/share/templates/billing/fees.tt +++ b/share/templates/billing/fees.tt @@ -1,6 +1,8 @@ [% META title = 'Billing Fees' -%] Edit Zones + +Upload CSV Fees [% helper.name = 'Billing Fees'; helper.messages = messages;