From 95042d735961e4acf3a2332043c2f18677bc2ee5 Mon Sep 17 00:00:00 2001 From: Finn Date: Wed, 10 Jul 2019 17:23:27 +0100 Subject: [PATCH] Fully added working import and API --- lib/Pear/LocalLoop/Controller/Api/External.pm | 31 ++++++++++--------- lib/Pear/LocalLoop/Import/LCCCsv/Suppliers.pm | 8 +++-- .../LocalLoop/Import/LCCCsv/Transactions.pm | 17 +++++++--- 3 files changed, 34 insertions(+), 22 deletions(-) diff --git a/lib/Pear/LocalLoop/Controller/Api/External.pm b/lib/Pear/LocalLoop/Controller/Api/External.pm index 138be8e..da08cb5 100644 --- a/lib/Pear/LocalLoop/Controller/Api/External.pm +++ b/lib/Pear/LocalLoop/Controller/Api/External.pm @@ -18,27 +18,29 @@ sub post_lcc_transactions { return $c->api_validation_error if $validation->has_error; - my $lcc_import_ext_ref = $self->schema->resultset('ExternalReference')->find_or_create({ name => 'LCC CSV' }); + my $lcc_import_ext_ref = $c->schema->resultset('ExternalReference')->find({ name => 'LCC CSV' }); - my $lcc_transactions = $lcc_import_ext_ref->search_related('transactions', - undef, - { - page => $validation->param('page') || 1, - rows => 10, - order_by => { -desc => 'purchase_time' }, - }, - ); + return 0 unless $lcc_import_ext_ref; + + my $lcc_transactions = $lcc_import_ext_ref->transactions->search( + undef, + { + page => $validation->param('page') || 1, + rows => 10, + join => 'transaction', + order_by => { -desc => 'transaction.purchase_time' }, + }); # purchase_time needs timezone attached to it my @transaction_list = ( map {{ transaction_external_id => $_->external_id, seller => $_->transaction->seller->name, - net_value => $_->transaction->value, + net_value => $_->transaction->meta->net_value, gross_value => $_->transaction->meta->gross_value, sales_tax_value => $_->transaction->meta->sales_tax_value, - purchase_time => $c->transaction->format_iso_datetime($_->purchase_time), - }} $transactions->all + purchase_time => $c->format_iso_datetime($_->transaction->purchase_time), + }} $lcc_transactions->all ); return $c->render( json => { @@ -55,7 +57,7 @@ sub post_lcc_suppliers { # TODO give an error if user is not of Lancashire County Council - my $is_lcc = $self->entity->organisation->count({ name => "Lancashire County Council" }); + my $is_lcc = $user->entity->organisation->count({ name => "Lancashire County Council" }); my $validation = $c->validation; $validation->input( $c->stash->{api_json} ); @@ -63,7 +65,7 @@ sub post_lcc_suppliers { return $c->api_validation_error if $validation->has_error; - my $lcc_import_ext_ref = $self->schema->resultset('ExternalReference')->find_or_create({ name => 'LCC CSV' }); + my $lcc_import_ext_ref = $c->schema->resultset('ExternalReference')->find_or_create({ name => 'LCC CSV' }); my $lcc_suppliers = $lcc_import_ext_ref->search_related('organisations', undef, @@ -74,7 +76,6 @@ sub post_lcc_suppliers { }, ); - # purchase_time needs timezone attached to it my @supplier_list = ( map {{ supplier_external_id => $_->external_id, diff --git a/lib/Pear/LocalLoop/Import/LCCCsv/Suppliers.pm b/lib/Pear/LocalLoop/Import/LCCCsv/Suppliers.pm index 6c2541e..d4d6f17 100644 --- a/lib/Pear/LocalLoop/Import/LCCCsv/Suppliers.pm +++ b/lib/Pear/LocalLoop/Import/LCCCsv/Suppliers.pm @@ -27,15 +27,17 @@ sub _row_to_result { 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}, + return if $self->external_result->organisations->find({external_id => $row->{supplier_id}}); + + $self->schema->resultset('Entity')->create({ + type => 'organisation', organisation => { name => $row->{name}, street_name => $row->{"address line 1"}, town => $address, postcode => $row->{post_code}, country => $row->{country_code}, - entity => { type => 'organisation' }, + external_reference => [ { external_reference => $self->external_result, external_id => $row->{supplier_id} } ], } }); } diff --git a/lib/Pear/LocalLoop/Import/LCCCsv/Transactions.pm b/lib/Pear/LocalLoop/Import/LCCCsv/Transactions.pm index e8bdd37..c81512d 100644 --- a/lib/Pear/LocalLoop/Import/LCCCsv/Transactions.pm +++ b/lib/Pear/LocalLoop/Import/LCCCsv/Transactions.pm @@ -19,9 +19,15 @@ sub import_csv { my ($self) = @_; my $rows = $self->csv_data; - # my $lcc_org = $self->schema->resultset('Organisation')->find({ name => "Lancashire County Council" }); + my $lcc_org = $self->schema->resultset('Organisation')->find({ + name => "Lancashire County Council", + street_name => "County Hall" + }); + unless ($lcc_org) { + Pear::LocalLoop::Error->throw("Cannot find LCC Organisation, please contact an admin"); + } foreach my $row ( @{$rows} ) { - $self->_row_to_result($row); + $self->_row_to_result($row, $lcc_org); } } @@ -38,6 +44,9 @@ sub _row_to_result { Pear::LocalLoop::Error->throw("Cannot find an organisation with supplier_id $supplier_id"); } + use Devel::Dwarn; + Dwarn $organisation->entity->id; + my $date_formatter = DateTime::Format::Strptime->new( pattern => '%Y/%m/%d' ); @@ -56,9 +65,9 @@ sub _row_to_result { external_id => $row->{transaction_id}, transaction => { seller => $organisation->entity, - buyer => $lcc_org, + buyer => $lcc_org->entity, purchase_time => $paid_date, - value => $row->{net_amount}, + value => $gross_value * 100000, meta => { gross_value => $gross_value * 100000, sales_tax_value => $sales_tax_value * 100000,