From 28c9ee8f652de1bf610ad08c347f1f928c401583 Mon Sep 17 00:00:00 2001 From: Finn Date: Fri, 5 Jul 2019 16:52:32 +0100 Subject: [PATCH] Added submitting Transactions (currently breaking on headers) --- .../LocalLoop/Controller/Admin/ImportFrom.pm | 7 ++- lib/Pear/LocalLoop/Import/LCCCsv/Suppliers.pm | 26 ++++----- .../LocalLoop/Import/LCCCsv/Transactions.pm | 54 +++++++++++++++++-- 3 files changed, 69 insertions(+), 18 deletions(-) diff --git a/lib/Pear/LocalLoop/Controller/Admin/ImportFrom.pm b/lib/Pear/LocalLoop/Controller/Admin/ImportFrom.pm index 447da7b..ae755d5 100644 --- a/lib/Pear/LocalLoop/Controller/Admin/ImportFrom.pm +++ b/lib/Pear/LocalLoop/Controller/Admin/ImportFrom.pm @@ -4,6 +4,7 @@ use Moo; use Devel::Dwarn; use Pear::LocalLoop::Import::LCCCsv::Suppliers; +use Pear::LocalLoop::Import::LCCCsv::Transactions; sub index { my $c = shift; @@ -46,8 +47,10 @@ 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::Suppliers->import( $c->param('transactions_csv') ); + my $csv_import = Pear::LocalLoop::Import::LCCCsv::Transactions->new( + csv_string => $c->param('transactions_csv')->slurp, + schema => $c->app->schema + )->import_csv; $c->flash( success => "CSV imported" ); return $c->redirect_to( '/admin/import_from' ); diff --git a/lib/Pear/LocalLoop/Import/LCCCsv/Suppliers.pm b/lib/Pear/LocalLoop/Import/LCCCsv/Suppliers.pm index 5e43c4a..0df784d 100644 --- a/lib/Pear/LocalLoop/Import/LCCCsv/Suppliers.pm +++ b/lib/Pear/LocalLoop/Import/LCCCsv/Suppliers.pm @@ -24,21 +24,21 @@ sub import_csv { sub _row_to_result { my ( $self, $row ) = @_; - my $addr2 = $row->{post_town}; + my $addr2 = $row->{post_town}; - my $address = ( defined $addr2 ? ( $row->{"address line 2"} . ' ' . $addr2) : $row->{"address line 2"} ); + my $address = ( defined $addr2 ? ( $row->{"address line 2"} . ' ' . $addr2) : $row->{"address line 2"} ); - $self->external_result->find_or_create_related('organisations', { - external_id => $row->{supplier_id}, - organisation => { - name => $row->{name}, - street_name => $row->{"address line 1"}, - town => $address, - postcode => $row->{post_code}, - country => $row->{country_code}, - entity => { type => 'organisation' }, - } - }); + $self->external_result->find_or_create_related('organisations', { + external_id => $row->{supplier_id}, + organisation => { + name => $row->{name}, + street_name => $row->{"address line 1"}, + town => $address, + postcode => $row->{post_code}, + country => $row->{country_code}, + entity => { type => 'organisation' }, + } + }); } 1; diff --git a/lib/Pear/LocalLoop/Import/LCCCsv/Transactions.pm b/lib/Pear/LocalLoop/Import/LCCCsv/Transactions.pm index e784c41..8711bf6 100644 --- a/lib/Pear/LocalLoop/Import/LCCCsv/Transactions.pm +++ b/lib/Pear/LocalLoop/Import/LCCCsv/Transactions.pm @@ -1,10 +1,58 @@ package Pear::LocalLoop::Import::LCCCsv::Transactions; use Moo; +use DateTime; +use DateTime::Format::Strptime; extends qw/Pear::LocalLoop::Import::LCCCsv/; -sub import { - my $self = shift; +has '+csv_required_columns' => ( + builder => sub { return [ qw/ + transaction_id + supplier_id + net_amount + gross_amount + / ]}, +); + +sub import_csv { + my ($self) = @_; + + my $rows = $self->csv_data; + my $lcc_org = $self->schema->resultset('Organisation')->find( name => "Lancashire County Council" ); + foreach my $row ( @{$rows} ) { + $self->_row_to_result($row, $lcc_org); + } + return 1; } -1; \ No newline at end of file +sub _row_to_result { + my ( $self, $row, $lcc_org ) = @_; + + Dwarn $row; + + my $organisation = $self->schema->resultset('Organisation')->find( external_id => $row->{supplier_id} ); + + my $date_formatter = DateTime::Format::Strptime->new( + pattern => '%Y/%m/%d' + ); + + my $paid_date = ( $row->{paid_date} ? $date_formatter->parse_datetime($row->{paid_date}) : DateTime->today ); + + $self->external_result->find_or_create_related('transactions', { + transaction_id => $row->{transaction_id}, + transaction => { + seller => $organisation->entity->id, + 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}, + }, + } + }); +} + +1;