Fully added Transaction importing

This commit is contained in:
Finn 2019-07-05 17:56:21 +01:00
parent 28c9ee8f65
commit 795543391a
4 changed files with 41 additions and 9 deletions

View file

@ -52,8 +52,14 @@ sub post_transactions {
schema => $c->app->schema schema => $c->app->schema
)->import_csv; )->import_csv;
$c->flash( success => "CSV imported" ); if ($csv_import->csv_error) {
return $c->redirect_to( '/admin/import_from' ); $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; 1;

View file

@ -15,6 +15,7 @@ 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);
} }

View file

@ -18,7 +18,8 @@ sub import_csv {
my ($self) = @_; my ($self) = @_;
my $rows = $self->csv_data; 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} ) { foreach my $row ( @{$rows} ) {
$self->_row_to_result($row, $lcc_org); $self->_row_to_result($row, $lcc_org);
} }
@ -28,9 +29,19 @@ 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; 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( my $date_formatter = DateTime::Format::Strptime->new(
pattern => '%Y/%m/%d' 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 ); 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', { $self->external_result->find_or_create_related('transactions', {
transaction_id => $row->{transaction_id}, external_id => $row->{transaction_id},
transaction => { transaction => {
seller => $organisation->entity->id, seller => $organisation->entity,
buyer => $lcc_org, buyer => $lcc_org,
purchase_time => $paid_date, purchase_time => $paid_date,
value => $row->{net_amount}, value => $row->{net_amount},
meta => { meta => {
transaction_id => $row->{transaction_id},
gross_value => $row->{gross_amount}, gross_value => $row->{gross_amount},
sales_tax_value => $row->{"vat amount"}, sales_tax_value => $row->{"vat amount"},
net_value => $row->{net_amount}, net_value => $row->{net_amount},

View file

@ -3,6 +3,8 @@ use strict;
use warnings; use warnings;
use Moo::Role; use Moo::Role;
use Text::CSV; use Text::CSV;
use Try::Tiny;
use Pear::LocalLoop::Error;
requires 'csv_required_columns'; requires 'csv_required_columns';
@ -16,6 +18,11 @@ has csv_string => (
predicate => 1, predicate => 1,
); );
has csv_error => (
is => 'ro',
predicate => 1,
);
has _csv_filehandle => ( has _csv_filehandle => (
is => 'lazy', is => 'lazy',
builder => sub { builder => sub {
@ -54,7 +61,8 @@ has csv_data => (
is => 'lazy', is => 'lazy',
builder => sub { builder => sub {
my $self = shift; 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 ); return $self->_text_csv->getline_hr_all( $self->_csv_filehandle );
} }
); );
@ -65,7 +73,13 @@ sub check_headers {
use Devel::Dwarn; use Devel::Dwarn;
Dwarn $req_headers; Dwarn $req_headers;
# TODO catch the boom # 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; 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 ) {