MT#5131 Make CSV fee parsing more tight.

gjungwirth/fix_tests
Andreas Granig 12 years ago
parent 272caf532e
commit b8dc8a48ce

@ -313,15 +313,23 @@ sub fees_upload :Chained('fees_list') :PathPart('upload') :Args(0) {
return; return;
} }
my $csv = Text::CSV_XS->new({allow_whitespace => 1, binary => 1}); my $csv = Text::CSV_XS->new({allow_whitespace => 1, binary => 1, keep_meta_info => 1});
my @cols = $c->config->{fees_csv}->{element_order}; my @cols = $c->config->{fees_csv}->{element_order};
$csv->column_names (@cols); $csv->column_names (@cols);
if ($c->req->params->{purge_existing}) { if ($c->req->params->{purge_existing}) {
$c->stash->{'profile_result'}->billing_fees->delete_all; $c->stash->{'profile_result'}->billing_fees->delete_all;
} }
my @fails = ();
while (my $row = $csv->getline_hr($upload->fh)) { my $linenum = 0;
try {
$c->model('DB')->txn_do(sub {
while(my $row = $csv->getline_hr($upload->fh)) {
++$linenum;
if($csv->is_missing(1)) {
push @fails, $linenum;
next;
}
my $zone = $c->stash->{'profile_result'} my $zone = $c->stash->{'profile_result'}
->billing_zones ->billing_zones
->find_or_create({ ->find_or_create({
@ -334,7 +342,18 @@ sub fees_upload :Chained('fees_list') :PathPart('upload') :Args(0) {
$c->stash->{'profile_result'} $c->stash->{'profile_result'}
->billing_fees->create($row); ->billing_fees->create($row);
} }
$c->flash(messages => [{type => 'success', text => 'Billing Fee successfully uploaded!'}]); });
my $text = "Billing Fee successfully uploaded";
if(@fails) {
$text .= ", but skipped the following line numbers: " . (join ", ", @fails);
}
$c->flash(messages => [{type => 'success', text => $text}]);
} catch($e) {
$c->log->error("failed to upload csv: $e");
$c->flash(messages => [{type => 'error', text => 'Failed to upload Billing Fees'}]);
};
$c->response->redirect($c->uri_for($c->stash->{profile}->{id}, 'fees')); $c->response->redirect($c->uri_for($c->stash->{profile}->{id}, 'fees'));
return; return;
} }

Loading…
Cancel
Save