From 91677034cacc1e59828f5f455f7b6740a3837c1e Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Wed, 15 Nov 2017 18:22:49 +0000 Subject: [PATCH] Finishing off CSV import functionality --- lib/Pear/LocalLoop.pm | 2 + lib/Pear/LocalLoop/Controller/Admin/Import.pm | 58 ++++++++++++++++++- lib/Pear/LocalLoop/Plugin/Currency.pm | 17 ++++++ .../LocalLoop/Schema/ResultSet/ImportSet.pm | 28 ++++++--- templates/admin/import/index.html.ep | 29 ++++++---- templates/admin/import/list.html.ep | 46 ++++++++++++--- 6 files changed, 149 insertions(+), 31 deletions(-) create mode 100644 lib/Pear/LocalLoop/Plugin/Currency.pm diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index 7038f70..59631e7 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -39,6 +39,7 @@ sub startup { $self->plugin('Pear::LocalLoop::Plugin::BootstrapPagination', { bootstrap4 => 1 } ); $self->plugin('Pear::LocalLoop::Plugin::Validators'); $self->plugin('Pear::LocalLoop::Plugin::Datetime'); + $self->plugin('Pear::LocalLoop::Plugin::Currency'); $self->plugin('Pear::LocalLoop::Plugin::Postcodes'); $self->plugin('Pear::LocalLoop::Plugin::TemplateHelpers'); @@ -209,6 +210,7 @@ sub startup { $admin_routes->get('/import/:set_id/org')->to('admin-import#get_org'); $admin_routes->get('/import/:set_id/ignore/:value_id')->to('admin-import#ignore_value'); + $admin_routes->get('/import/:set_id/import')->to('admin-import#run_import'); # my $user_routes = $r->under('/')->to('root#under'); # $user_routes->get('/home')->to('root#home'); diff --git a/lib/Pear/LocalLoop/Controller/Admin/Import.pm b/lib/Pear/LocalLoop/Controller/Admin/Import.pm index 8f40971..0416012 100644 --- a/lib/Pear/LocalLoop/Controller/Admin/Import.pm +++ b/lib/Pear/LocalLoop/Controller/Admin/Import.pm @@ -28,11 +28,12 @@ sub list { my $set_id = $c->param('set_id'); my $include_ignored = $c->param('ignored'); + my $include_imported = $c->param('imported'); my $import_set = $c->result_set->find($set_id); - my $import_value_rs = $c->result_set->get_values($set_id, $include_ignored); - my $import_users_rs = $c->result_set->get_users($set_id, $include_ignored); - my $import_org_rs = $c->result_set->get_orgs($set_id, $include_ignored); + my $import_value_rs = $c->result_set->get_values($set_id, $include_ignored, $include_imported); + my $import_users_rs = $c->result_set->get_users($set_id, $include_ignored, $include_imported); + my $import_org_rs = $c->result_set->get_orgs($set_id, $include_ignored, $include_imported); my $import_lookup_rs = $c->result_set->get_lookups($set_id); $c->stash( @@ -270,4 +271,55 @@ sub ignore_value { ); } +sub run_import { + my $c = shift; + my $set_id = $c->param('set_id'); + + my $set_result = $c->result_set->find($set_id); + unless ( defined $set_result ) { + $c->flash( error => "Set does not exist" ); + return $c->redirect_to( '/admin/import' ); + } + + my $import_value_rs = $c->result_set->get_values($set_id, undef, undef); + my $import_lookup = $c->result_set->get_lookups($set_id); + my $entity_rs = $c->schema->resultset('Entity'); + + $c->schema->txn_do( + sub { + for my $value_result ( $import_value_rs->all ) { + my $user_lookup = $import_lookup->{ $value_result->user_name }; + my $org_lookup = $import_lookup->{ $value_result->org_name }; + my $value_lookup = $c->parse_currency( $value_result->purchase_value ); + + if ( defined $user_lookup && defined $org_lookup && $value_lookup ) { + my $user_entity = $entity_rs->find($user_lookup->{entity_id}); + my $org_entity = $entity_rs->find($org_lookup->{entity_id}); + my $distance = $c->get_distance_from_coords( $user_entity->type_object, $org_entity->type_object ); + my $transaction = $c->schema->resultset('Transaction')->create( + { + buyer => $user_entity, + seller => $org_entity, + value => $value_lookup * 100000, + purchase_time => $value_result->purchase_date, + distance => $distance, + } + ); + $value_result->update({transaction_id => $transaction->id }); + } else { + $c->app->log->warn("Failed value import for value id [" . $value_result->id . "], ignoring"); + } + } + } + ); + + $c->flash( success => "Import completed for ready values" ); + my $referer = $c->req->headers->header('Referer'); + return $c->redirect_to( + defined $referer + ? $c->url_for($referer)->path_query + : '/admin/import/' . $set_id + ); +} + 1; diff --git a/lib/Pear/LocalLoop/Plugin/Currency.pm b/lib/Pear/LocalLoop/Plugin/Currency.pm new file mode 100644 index 0000000..842cf89 --- /dev/null +++ b/lib/Pear/LocalLoop/Plugin/Currency.pm @@ -0,0 +1,17 @@ +package Pear::LocalLoop::Plugin::Currency; +use Mojo::Base 'Mojolicious::Plugin'; + +sub register { + my ( $plugin, $app, $cong ) = @_; + + $app->helper( parse_currency => sub { + my ( $c, $currency_string ) = @_; + my $value; + if ( $currency_string =~ /^£([\d.]+)/ ) { + $value = $1 * 1; + } + return $value; + }); +} + +1; diff --git a/lib/Pear/LocalLoop/Schema/ResultSet/ImportSet.pm b/lib/Pear/LocalLoop/Schema/ResultSet/ImportSet.pm index 9efad48..785aa99 100644 --- a/lib/Pear/LocalLoop/Schema/ResultSet/ImportSet.pm +++ b/lib/Pear/LocalLoop/Schema/ResultSet/ImportSet.pm @@ -9,10 +9,14 @@ sub get_values { my $self = shift; my $id = shift; my $include_ignored = shift; + my $include_imported = shift; return $self->find($id)->search_related( 'values', - ( $include_ignored ? {} : { ignore_value => 0 } ), + { + ( $include_ignored ? () : ( ignore_value => 0 ) ), + ( $include_imported ? () : ( transaction_id => undef ) ), + }, { order_by => { '-asc' => 'id' }, }, @@ -21,10 +25,8 @@ sub get_values { sub get_users { my $self = shift; - my $id = shift; - my $include_ignored = shift; - return $self->get_values($id, $include_ignored)->search({}, + return $self->get_values(@_)->search({}, { group_by => 'user_name', }, @@ -33,10 +35,8 @@ sub get_users { sub get_orgs { my $self = shift; - my $id = shift; - my $include_ignored = shift; - return $self->get_values($id, $include_ignored)->search({}, + return $self->get_values(@_)->search({}, { group_by => 'org_name', }, @@ -47,13 +47,23 @@ sub get_lookups { my $self = shift; my $id = shift; - return $self->find($id)->search_related( + my $lookup_rs = $self->find($id)->search_related( 'lookups', undef, { - order_by => { '-asc' => 'id' }, + prefetch => { entity => [ qw/ organisation customer / ] }, + order_by => { '-asc' => 'me.id' }, }, ); + my $lookup_map = { + map { + $_->name => { + entity_id => $_->entity->id, + name => $_->entity->name, + }, + } $lookup_rs->all + }; + return $lookup_map; } 1; diff --git a/templates/admin/import/index.html.ep b/templates/admin/import/index.html.ep index 7d83b86..379dbdb 100644 --- a/templates/admin/import/index.html.ep +++ b/templates/admin/import/index.html.ep @@ -12,22 +12,31 @@ % }
-
-
-

- CSV Import - Import Data -

-
+
+

CSV Import

+ Import Data
% for my $import ( $import_rs->all ) { + % my $total = $import_rs->get_values( $import->id, 1, 1 )->count; + % my $unimported = $import_rs->get_values( $import->id, undef, undef )->count; + % my $with_ignored = $import_rs->get_values( $import->id, 1, undef )->count; + % my $with_imported = $import_rs->get_values( $import->id, undef, 1 )->count; + % my $ignored_total = $with_ignored - $unimported; + % my $imported_total = $with_imported - $unimported;
-
+

%= format_human_datetime $import_set->date; - Toggle show Ignored + <%= $c->param('ignored') ? 'Hide' : 'Show' %> Ignored + + <%= $c->param('imported') ? 'Hide' : 'Show' %> Imported + +

Content listed in original order of import
% for my $import_value ( $import_value_rs->all ) { + % my $user_lookup = $import_lookup_rs->{ $import_value->user_name }; + % my $purchase_lookup = parse_currency $import_value->purchase_value; + % my $org_lookup = $import_lookup_rs->{ $import_value->org_name };
<%= $import_value->user_name %> + % if ( defined $user_lookup ) { +
+ <%= $user_lookup->{name} %> + % }
<%= format_human_datetime $import_value->purchase_date %>
<%= $import_value->purchase_value %> +
+ <%= $purchase_lookup %>
<%= $import_value->org_name %> + % if ( defined $org_lookup ) { +
+ <%= $org_lookup->{name} %> + % }
- % if ( $import_value->ignore_value ) { - Un Ignore + % if ( defined $import_value->transaction_id ) { + % } else { - Ignore + % if ( defined $user_lookup && defined $org_lookup && $purchase_lookup ) { + + % } + % if ( $import_value->ignore_value ) { + Un Ignore + % } else { + Ignore + % } % }
@@ -115,4 +140,7 @@
+