Added nicer explosions via try::tiny
This commit is contained in:
parent
d1cd30928e
commit
afc635fdb4
6 changed files with 27 additions and 33 deletions
|
@ -1,7 +1,7 @@
|
||||||
package Pear::LocalLoop::Controller::Admin::ImportFrom;
|
package Pear::LocalLoop::Controller::Admin::ImportFrom;
|
||||||
use Mojo::Base 'Mojolicious::Controller';
|
use Mojo::Base 'Mojolicious::Controller';
|
||||||
use Moo;
|
use Moo;
|
||||||
use Devel::Dwarn;
|
use Try::Tiny;
|
||||||
|
|
||||||
use Pear::LocalLoop::Import::LCCCsv::Suppliers;
|
use Pear::LocalLoop::Import::LCCCsv::Suppliers;
|
||||||
use Pear::LocalLoop::Import::LCCCsv::Transactions;
|
use Pear::LocalLoop::Import::LCCCsv::Transactions;
|
||||||
|
@ -47,19 +47,22 @@ sub post_transactions {
|
||||||
$c->flash( error => "CSV file size is too large" );
|
$c->flash( error => "CSV file size is too large" );
|
||||||
return $c->redirect_to( '/admin/import_from' );
|
return $c->redirect_to( '/admin/import_from' );
|
||||||
}
|
}
|
||||||
my $csv_import = Pear::LocalLoop::Import::LCCCsv::Transactions->new(
|
my $csv_error;
|
||||||
csv_string => $c->param('transactions_csv')->slurp,
|
try {
|
||||||
schema => $c->app->schema
|
Pear::LocalLoop::Import::LCCCsv::Transactions->new(
|
||||||
)->import_csv;
|
csv_string => $c->param('transactions_csv')->slurp,
|
||||||
|
schema => $c->app->schema
|
||||||
if ($csv_import->csv_error) {
|
)->import_csv;
|
||||||
$c->flash( error => $csv_import->csv_error );
|
} catch {
|
||||||
|
$csv_error = $_;
|
||||||
|
};
|
||||||
|
if ( $csv_error ) {
|
||||||
|
$c->flash( error => $csv_error );
|
||||||
return $c->redirect_to( '/admin/import_from' );
|
return $c->redirect_to( '/admin/import_from' );
|
||||||
} else {
|
} else {
|
||||||
$c->flash( success => "CSV imported" );
|
$c->flash( success => "CSV imported" );
|
||||||
return $c->redirect_to( '/admin/import_from' );
|
return $c->redirect_to( '/admin/import_from' );
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
1;
|
1;
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
package Pear::LocalLoop::Import::LCCCsv::Suppliers;
|
package Pear::LocalLoop::Import::LCCCsv::Suppliers;
|
||||||
use Moo;
|
use Moo;
|
||||||
use Devel::Dwarn;
|
|
||||||
|
|
||||||
extends qw/Pear::LocalLoop::Import::LCCCsv/;
|
extends qw/Pear::LocalLoop::Import::LCCCsv/;
|
||||||
|
|
||||||
|
@ -15,7 +14,6 @@ sub import_csv {
|
||||||
my ($self) = @_;
|
my ($self) = @_;
|
||||||
|
|
||||||
my $rows = $self->csv_data;
|
my $rows = $self->csv_data;
|
||||||
return $self unless $rows;
|
|
||||||
foreach my $row ( @{$rows} ) {
|
foreach my $row ( @{$rows} ) {
|
||||||
$self->_row_to_result($row);
|
$self->_row_to_result($row);
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,19 +6,19 @@ use DateTime::Format::Strptime;
|
||||||
extends qw/Pear::LocalLoop::Import::LCCCsv/;
|
extends qw/Pear::LocalLoop::Import::LCCCsv/;
|
||||||
|
|
||||||
has '+csv_required_columns' => (
|
has '+csv_required_columns' => (
|
||||||
builder => sub { return [ qw/
|
builder => sub { return [ (
|
||||||
transaction_id
|
'transaction_id',
|
||||||
supplier_id
|
'supplier_id',
|
||||||
net_amount
|
'net_amount',
|
||||||
gross_amount
|
'vat amount',
|
||||||
/ ]},
|
'gross_amount',
|
||||||
|
)]},
|
||||||
);
|
);
|
||||||
|
|
||||||
sub import_csv {
|
sub import_csv {
|
||||||
my ($self) = @_;
|
my ($self) = @_;
|
||||||
|
|
||||||
my $rows = $self->csv_data;
|
my $rows = $self->csv_data;
|
||||||
return 0 unless $rows;
|
|
||||||
my $lcc_org = $self->schema->resultset('Organisation')->find({ name => "Lancashire County Council" });
|
my $lcc_org = $self->schema->resultset('Organisation')->find({ name => "Lancashire County Council" });
|
||||||
foreach my $row ( @{$rows} ) {
|
foreach my $row ( @{$rows} ) {
|
||||||
$self->_row_to_result($row, $lcc_org);
|
$self->_row_to_result($row, $lcc_org);
|
||||||
|
@ -29,10 +29,6 @@ sub import_csv {
|
||||||
sub _row_to_result {
|
sub _row_to_result {
|
||||||
my ( $self, $row, $lcc_org ) = @_;
|
my ( $self, $row, $lcc_org ) = @_;
|
||||||
|
|
||||||
use Devel::Dwarn;
|
|
||||||
|
|
||||||
Dwarn $row;
|
|
||||||
|
|
||||||
my $supplier_id = $row->{supplier_id};
|
my $supplier_id = $row->{supplier_id};
|
||||||
|
|
||||||
my $organisation = $self->schema->resultset('Organisation')->find({
|
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 );
|
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', {
|
$self->external_result->find_or_create_related('transactions', {
|
||||||
external_id => $row->{transaction_id},
|
external_id => $row->{transaction_id},
|
||||||
transaction => {
|
transaction => {
|
||||||
|
|
|
@ -70,17 +70,8 @@ has csv_data => (
|
||||||
sub check_headers {
|
sub check_headers {
|
||||||
my $self = shift;
|
my $self = shift;
|
||||||
my $req_headers = $self->csv_required_columns;
|
my $req_headers = $self->csv_required_columns;
|
||||||
use Devel::Dwarn;
|
|
||||||
Dwarn $req_headers;
|
|
||||||
# TODO catch the boom
|
|
||||||
my @headers;
|
my @headers;
|
||||||
try {
|
@headers = $self->_text_csv->header( $self->_csv_filehandle );
|
||||||
@headers = $self->_text_csv->header( $self->_csv_filehandle );
|
|
||||||
} catch {
|
|
||||||
$self->csv_error = $_->[1];
|
|
||||||
};
|
|
||||||
return 0 unless @headers;
|
|
||||||
Dwarn \@headers;
|
|
||||||
my %header_map = ( map { $_ => 1 } @headers );
|
my %header_map = ( map { $_ => 1 } @headers );
|
||||||
for my $req_header ( @$req_headers ) {
|
for my $req_header ( @$req_headers ) {
|
||||||
next if $header_map{$req_header};
|
next if $header_map{$req_header};
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
<div class="col-12 mb-3">
|
<div class="col-12 mb-3">
|
||||||
<h3 class="float-left">CSV Import</h3>
|
<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 . '/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>
|
</div>
|
||||||
% for my $import ( $import_rs->all ) {
|
% for my $import ( $import_rs->all ) {
|
||||||
% my $total = $import_rs->get_values( $import->id, 1, 1 )->count;
|
% my $total = $import_rs->get_values( $import->id, 1, 1 )->count;
|
||||||
|
|
|
@ -18,12 +18,16 @@
|
||||||
</div>
|
</div>
|
||||||
<div class="col-12 mb-3">
|
<div class="col-12 mb-3">
|
||||||
<p>Various import options depending on source. Each one is custom. For Creditor reports from LCC.</p>
|
<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>
|
<p>Warning: Large files will take a long time to process, just leave the tab open until the Success message appears.</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="card col-md-6 m-3">
|
<div class="card col-md-6 m-3">
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<h4 class="card-title">LCC Procurement Import - Suppliers</h4>
|
<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">
|
<form action="/admin/import_from/suppliers" method="POST" enctype="multipart/form-data">
|
||||||
<input type="file" name="suppliers_csv" accept="text/csv">
|
<input type="file" name="suppliers_csv" accept="text/csv">
|
||||||
<input type="submit" value="Upload Suppliers CSV">
|
<input type="submit" value="Upload Suppliers CSV">
|
||||||
|
@ -33,6 +37,7 @@
|
||||||
<div class="card col-md-6 m-3">
|
<div class="card col-md-6 m-3">
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
<h4 class="card-title">LCC Procurement Import - Transactions</h4>
|
<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">
|
<form action="/admin/import_from/transactions" method="POST" enctype="multipart/form-data">
|
||||||
<input type="file" name="transactions_csv" accept="text/csv">
|
<input type="file" name="transactions_csv" accept="text/csv">
|
||||||
<input type="submit" value="Upload Transactions CSV">
|
<input type="submit" value="Upload Transactions CSV">
|
||||||
|
|
Reference in a new issue