From d1cd30928e666200e70b397971fcc8e22288163a Mon Sep 17 00:00:00 2001 From: Finn Date: Fri, 5 Jul 2019 17:56:21 +0100 Subject: [PATCH] Fully added Transaction importing --- .../LocalLoop/Controller/Admin/ImportFrom.pm | 10 +++++++-- lib/Pear/LocalLoop/Import/LCCCsv/Suppliers.pm | 1 + .../LocalLoop/Import/LCCCsv/Transactions.pm | 21 ++++++++++++++----- lib/Pear/LocalLoop/Import/Role/CSV.pm | 18 ++++++++++++++-- 4 files changed, 41 insertions(+), 9 deletions(-) diff --git a/lib/Pear/LocalLoop/Controller/Admin/ImportFrom.pm b/lib/Pear/LocalLoop/Controller/Admin/ImportFrom.pm index ae755d5..484ea6d 100644 --- a/lib/Pear/LocalLoop/Controller/Admin/ImportFrom.pm +++ b/lib/Pear/LocalLoop/Controller/Admin/ImportFrom.pm @@ -52,8 +52,14 @@ sub post_transactions { schema => $c->app->schema )->import_csv; - $c->flash( success => "CSV imported" ); - return $c->redirect_to( '/admin/import_from' ); + if ($csv_import->csv_error) { + $c->flash( error => $csv_import->csv_error ); + return $c->redirect_to( '/admin/import_from' ); + } else { + $c->flash( success => "CSV imported" ); + return $c->redirect_to( '/admin/import_from' ); + } + } 1; diff --git a/lib/Pear/LocalLoop/Import/LCCCsv/Suppliers.pm b/lib/Pear/LocalLoop/Import/LCCCsv/Suppliers.pm index 0df784d..5ffe96a 100644 --- a/lib/Pear/LocalLoop/Import/LCCCsv/Suppliers.pm +++ b/lib/Pear/LocalLoop/Import/LCCCsv/Suppliers.pm @@ -15,6 +15,7 @@ sub import_csv { my ($self) = @_; my $rows = $self->csv_data; + return $self unless $rows; foreach my $row ( @{$rows} ) { $self->_row_to_result($row); } diff --git a/lib/Pear/LocalLoop/Import/LCCCsv/Transactions.pm b/lib/Pear/LocalLoop/Import/LCCCsv/Transactions.pm index 8711bf6..ce671da 100644 --- a/lib/Pear/LocalLoop/Import/LCCCsv/Transactions.pm +++ b/lib/Pear/LocalLoop/Import/LCCCsv/Transactions.pm @@ -18,7 +18,8 @@ sub import_csv { my ($self) = @_; my $rows = $self->csv_data; - my $lcc_org = $self->schema->resultset('Organisation')->find( name => "Lancashire County Council" ); + 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); } @@ -28,9 +29,19 @@ sub import_csv { sub _row_to_result { my ( $self, $row, $lcc_org ) = @_; + use Devel::Dwarn; + Dwarn $row; - my $organisation = $self->schema->resultset('Organisation')->find( external_id => $row->{supplier_id} ); + my $supplier_id = $row->{supplier_id}; + + my $organisation = $self->schema->resultset('Organisation')->find({ + 'external_reference.external_id' => $supplier_id + }, { join => 'external_reference' }); + + unless ($organisation) { + Pear::LocalLoop::Error->throw("Cannot find an organisation with supplier_id $supplier_id"); + } my $date_formatter = DateTime::Format::Strptime->new( pattern => '%Y/%m/%d' @@ -38,15 +49,15 @@ sub _row_to_result { my $paid_date = ( $row->{paid_date} ? $date_formatter->parse_datetime($row->{paid_date}) : DateTime->today ); + # TODO negative values $self->external_result->find_or_create_related('transactions', { - transaction_id => $row->{transaction_id}, + external_id => $row->{transaction_id}, transaction => { - seller => $organisation->entity->id, + seller => $organisation->entity, buyer => $lcc_org, purchase_time => $paid_date, value => $row->{net_amount}, meta => { - transaction_id => $row->{transaction_id}, gross_value => $row->{gross_amount}, sales_tax_value => $row->{"vat amount"}, net_value => $row->{net_amount}, diff --git a/lib/Pear/LocalLoop/Import/Role/CSV.pm b/lib/Pear/LocalLoop/Import/Role/CSV.pm index d953ddb..018ea05 100644 --- a/lib/Pear/LocalLoop/Import/Role/CSV.pm +++ b/lib/Pear/LocalLoop/Import/Role/CSV.pm @@ -3,6 +3,8 @@ use strict; use warnings; use Moo::Role; use Text::CSV; +use Try::Tiny; +use Pear::LocalLoop::Error; requires 'csv_required_columns'; @@ -16,6 +18,11 @@ has csv_string => ( predicate => 1, ); +has csv_error => ( + is => 'ro', + predicate => 1, +); + has _csv_filehandle => ( is => 'lazy', builder => sub { @@ -54,7 +61,8 @@ has csv_data => ( is => 'lazy', builder => sub { my $self = shift; - $self->check_headers; + my $header_check = $self->check_headers; + return 0 unless $header_check; return $self->_text_csv->getline_hr_all( $self->_csv_filehandle ); } ); @@ -65,7 +73,13 @@ sub check_headers { use Devel::Dwarn; Dwarn $req_headers; # TODO catch the boom - my @headers = $self->_text_csv->header( $self->_csv_filehandle ); + my @headers; + try { + @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 ); for my $req_header ( @$req_headers ) {