Added nicer explosions via try::tiny

This commit is contained in:
Finn 2019-07-05 18:44:46 +01:00
parent d1cd30928e
commit afc635fdb4
No known key found for this signature in database
GPG key ID: 7455B4B17685B598
6 changed files with 27 additions and 33 deletions

View file

@ -1,7 +1,7 @@
package Pear::LocalLoop::Controller::Admin::ImportFrom;
use Mojo::Base 'Mojolicious::Controller';
use Moo;
use Devel::Dwarn;
use Try::Tiny;
use Pear::LocalLoop::Import::LCCCsv::Suppliers;
use Pear::LocalLoop::Import::LCCCsv::Transactions;
@ -47,19 +47,22 @@ sub post_transactions {
$c->flash( error => "CSV file size is too large" );
return $c->redirect_to( '/admin/import_from' );
}
my $csv_import = Pear::LocalLoop::Import::LCCCsv::Transactions->new(
csv_string => $c->param('transactions_csv')->slurp,
schema => $c->app->schema
)->import_csv;
if ($csv_import->csv_error) {
$c->flash( error => $csv_import->csv_error );
my $csv_error;
try {
Pear::LocalLoop::Import::LCCCsv::Transactions->new(
csv_string => $c->param('transactions_csv')->slurp,
schema => $c->app->schema
)->import_csv;
} catch {
$csv_error = $_;
};
if ( $csv_error ) {
$c->flash( error => $csv_error );
return $c->redirect_to( '/admin/import_from' );
} else {
$c->flash( success => "CSV imported" );
return $c->redirect_to( '/admin/import_from' );
}
}
1;

View file

@ -1,6 +1,5 @@
package Pear::LocalLoop::Import::LCCCsv::Suppliers;
use Moo;
use Devel::Dwarn;
extends qw/Pear::LocalLoop::Import::LCCCsv/;
@ -15,7 +14,6 @@ sub import_csv {
my ($self) = @_;
my $rows = $self->csv_data;
return $self unless $rows;
foreach my $row ( @{$rows} ) {
$self->_row_to_result($row);
}

View file

@ -6,19 +6,19 @@ use DateTime::Format::Strptime;
extends qw/Pear::LocalLoop::Import::LCCCsv/;
has '+csv_required_columns' => (
builder => sub { return [ qw/
transaction_id
supplier_id
net_amount
gross_amount
/ ]},
builder => sub { return [ (
'transaction_id',
'supplier_id',
'net_amount',
'vat amount',
'gross_amount',
)]},
);
sub import_csv {
my ($self) = @_;
my $rows = $self->csv_data;
return 0 unless $rows;
my $lcc_org = $self->schema->resultset('Organisation')->find({ name => "Lancashire County Council" });
foreach my $row ( @{$rows} ) {
$self->_row_to_result($row, $lcc_org);
@ -29,10 +29,6 @@ sub import_csv {
sub _row_to_result {
my ( $self, $row, $lcc_org ) = @_;
use Devel::Dwarn;
Dwarn $row;
my $supplier_id = $row->{supplier_id};
my $organisation = $self->schema->resultset('Organisation')->find({
@ -49,7 +45,7 @@ sub _row_to_result {
my $paid_date = ( $row->{paid_date} ? $date_formatter->parse_datetime($row->{paid_date}) : DateTime->today );
# TODO negative values
# TODO negative values are sometimes present
$self->external_result->find_or_create_related('transactions', {
external_id => $row->{transaction_id},
transaction => {

View file

@ -70,17 +70,8 @@ has csv_data => (
sub check_headers {
my $self = shift;
my $req_headers = $self->csv_required_columns;
use Devel::Dwarn;
Dwarn $req_headers;
# TODO catch the boom
my @headers;
try {
@headers = $self->_text_csv->header( $self->_csv_filehandle );
} catch {
$self->csv_error = $_->[1];
};
return 0 unless @headers;
Dwarn \@headers;
@headers = $self->_text_csv->header( $self->_csv_filehandle );
my %header_map = ( map { $_ => 1 } @headers );
for my $req_header ( @$req_headers ) {
next if $header_map{$req_header};

View file

@ -15,6 +15,7 @@
<div class="col-12 mb-3">
<h3 class="float-left">CSV Import</h3>
<a href="<%= url_for . '/add' %>" class="btn btn-success float-right">Import Data</a>
<a href="<%= url_for '/admin/import_from' %>" class="btn btn-primary float-right">Import Suppliers/Transactions Data</a>
</div>
% for my $import ( $import_rs->all ) {
% my $total = $import_rs->get_values( $import->id, 1, 1 )->count;

View file

@ -18,12 +18,16 @@
</div>
<div class="col-12 mb-3">
<p>Various import options depending on source. Each one is custom. For Creditor reports from LCC.</p>
<p><strong>NOTE:</strong> Files must be in CSV format.<br>
Redundant columns and rows above and to the left of the table in the CSV must be deleted.<br>
There can be no duplicate header columns, and there can be no fields with values outside the table.</p>
<p>Warning: Large files will take a long time to process, just leave the tab open until the Success message appears.</p>
</div>
<div class="row">
<div class="card col-md-6 m-3">
<div class="card-body">
<h4 class="card-title">LCC Procurement Import - Suppliers</h4>
<p>Expected headers at very least: "supplier_id", "name", "post_code".</p>
<form action="/admin/import_from/suppliers" method="POST" enctype="multipart/form-data">
<input type="file" name="suppliers_csv" accept="text/csv">
<input type="submit" value="Upload Suppliers CSV">
@ -33,6 +37,7 @@
<div class="card col-md-6 m-3">
<div class="card-body">
<h4 class="card-title">LCC Procurement Import - Transactions</h4>
<p>Expected headers at very least: "supplier_id", "transaction_id", "net_amount", "vat amount" , "gross_amount".</p>
<form action="/admin/import_from/transactions" method="POST" enctype="multipart/form-data">
<input type="file" name="transactions_csv" accept="text/csv">
<input type="submit" value="Upload Transactions CSV">