From 1f83e1aec0128a2cc52d220e5eae2c2b2fad5122 Mon Sep 17 00:00:00 2001 From: Matthew Somerville Date: Tue, 2 May 2017 10:33:36 +0100 Subject: Tidy up find_closest* functions. Allow find_closest to be called multiple times with only one lookup, and to return just its data, not a compiled string. --- perllib/FixMyStreet/App/Controller/Admin/ExorDefects.pm | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'perllib/FixMyStreet/App/Controller/Admin/ExorDefects.pm') diff --git a/perllib/FixMyStreet/App/Controller/Admin/ExorDefects.pm b/perllib/FixMyStreet/App/Controller/Admin/ExorDefects.pm index 201742c81..fd4b2be3c 100644 --- a/perllib/FixMyStreet/App/Controller/Admin/ExorDefects.pm +++ b/perllib/FixMyStreet/App/Controller/Admin/ExorDefects.pm @@ -140,6 +140,7 @@ sub download : Path('download') : Args(0) { foreach my $report (@$inspections) { my ($eastings, $northings) = $report->local_coords; + my $location = "${eastings}E ${northings}N"; my $description = sprintf("%s %s", $report->external_id || "", $report->get_extra_metadata('detailed_information') || ""); my $activity_code = $report->defect_type ? $report->defect_type->get_extra_metadata('activity_code') @@ -153,7 +154,7 @@ sub download : Path('download') : Args(0) { $activity_code, # activity code - minor carriageway, also FC (footway) "", # empty field, can also be A (seen on MC) or B (seen on FC) sprintf("%03d", ++$i), # randomised sequence number - "${eastings}E ${northings}N", # defect location field, which we don't capture from inspectors + $location, # defect location field, which we don't capture from inspectors $report->inspection_log_entry->whenedited->strftime("%H%M"), # defect time raised "","","","","","","", # empty fields $traffic_information, @@ -227,4 +228,4 @@ sub download : Path('download') : Args(0) { $c->res->body( join "", map { "\"$_\"\r\n" } @body ); } -1; \ No newline at end of file +1; -- cgit v1.2.3 From b9e02f9568c14a9a63e4e5b0a6dcedfa37eab085 Mon Sep 17 00:00:00 2001 From: Matthew Somerville Date: Tue, 2 May 2017 11:08:03 +0100 Subject: [Oxfordshire] Include nearest info in RDI output. --- perllib/FixMyStreet/App/Controller/Admin/ExorDefects.pm | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) (limited to 'perllib/FixMyStreet/App/Controller/Admin/ExorDefects.pm') diff --git a/perllib/FixMyStreet/App/Controller/Admin/ExorDefects.pm b/perllib/FixMyStreet/App/Controller/Admin/ExorDefects.pm index fd4b2be3c..31638662f 100644 --- a/perllib/FixMyStreet/App/Controller/Admin/ExorDefects.pm +++ b/perllib/FixMyStreet/App/Controller/Admin/ExorDefects.pm @@ -140,7 +140,15 @@ sub download : Path('download') : Args(0) { foreach my $report (@$inspections) { my ($eastings, $northings) = $report->local_coords; - my $location = "${eastings}E ${northings}N"; + + my $location = "${eastings}E ${northings}N."; + $location = "[DID NOT USE MAP] $location" unless $report->used_map; + my $closest_address = $c->cobrand->find_closest($report, 1); + if (%$closest_address) { + $location .= " Nearest road: $closest_address->{road}." if $closest_address->{road}; + $location .= " Nearest postcode: $closest_address->{postcode}{postcode}." if $closest_address->{postcode}; + } + my $description = sprintf("%s %s", $report->external_id || "", $report->get_extra_metadata('detailed_information') || ""); my $activity_code = $report->defect_type ? $report->defect_type->get_extra_metadata('activity_code') -- cgit v1.2.3 From 61eb613ddc31943e9d6504050c0e96f91340f752 Mon Sep 17 00:00:00 2001 From: Matthew Somerville Date: Thu, 4 May 2017 18:18:27 +0100 Subject: [Oxfordshire] Send RDI reports by email. --- .../App/Controller/Admin/ExorDefects.pm | 198 +++------------------ 1 file changed, 21 insertions(+), 177 deletions(-) (limited to 'perllib/FixMyStreet/App/Controller/Admin/ExorDefects.pm') diff --git a/perllib/FixMyStreet/App/Controller/Admin/ExorDefects.pm b/perllib/FixMyStreet/App/Controller/Admin/ExorDefects.pm index 31638662f..1711ecb10 100644 --- a/perllib/FixMyStreet/App/Controller/Admin/ExorDefects.pm +++ b/perllib/FixMyStreet/App/Controller/Admin/ExorDefects.pm @@ -2,9 +2,9 @@ package FixMyStreet::App::Controller::Admin::ExorDefects; use Moose; use namespace::autoclean; -use Text::CSV; use DateTime; -use mySociety::Random qw(random_bytes); +use Try::Tiny; +use FixMyStreet::Integrations::ExorRDI; BEGIN { extends 'Catalyst::Controller'; } @@ -54,186 +54,30 @@ sub download : Path('download') : Args(0) { my $end_date = $parser-> parse_datetime ( $c->get_param('end_date') ) ; my $one_day = DateTime::Duration->new( days => 1 ); - my %params = ( - -and => [ - state => [ 'action scheduled' ], - external_id => { '!=' => undef }, - 'admin_log_entries.action' => 'inspected', - 'admin_log_entries.whenedited' => { '>=', $start_date }, - 'admin_log_entries.whenedited' => { '<=', $end_date + $one_day }, - ] - ); - - my $user; - if ( $c->get_param('user_id') ) { - my $uid = $c->get_param('user_id'); - $params{'admin_log_entries.user_id'} = $uid; - $user = $c->model('DB::User')->find( { id => $uid } ); - } - - my $problems = $c->cobrand->problems->search( - \%params, - { - join => 'admin_log_entries', - distinct => 1, - } - ); - - if ( !$problems->count ) { - if ( defined $user ) { + my $params = { + start_date => $start_date, + end_date => $end_date + $one_day, + user => $c->get_param('user_id'), + }; + my $rdi = FixMyStreet::Integrations::ExorRDI->new($params); + + try { + my $out = $rdi->construct; + $c->res->content_type('text/csv; charset=utf-8'); + $c->res->header('content-disposition' => "attachment; filename=" . $rdi->filename); + $c->res->body( $out ); + } catch { + die $_ unless $_ =~ /FixMyStreet::Integrations::ExorRDI::Error/; + if ($params->{user}) { $c->flash->{error_message} = _("No inspections by that inspector in the selected date range."); } else { $c->flash->{error_message} = _("No inspections in the selected date range."); } - $c->flash->{start_date} = $start_date; - $c->flash->{end_date} = $end_date; - $c->flash->{user_id} = $user->id if $user; + $c->flash->{start_date} = $params->{start_date}; + $c->flash->{end_date} = $params->{end_date}; + $c->flash->{user_id} = $params->{user}; $c->res->redirect( $c->uri_for( '' ) ); - } - - # A single RDI file might contain inspections from multiple inspectors, so - # we need to group inspections by inspector within G records. - my $inspectors = {}; - my $inspector_initials = {}; - while ( my $report = $problems->next ) { - my $user = $report->inspection_log_entry->user; - $inspectors->{$user->id} ||= []; - push @{ $inspectors->{$user->id} }, $report; - unless ( $inspector_initials->{$user->id} ) { - $inspector_initials->{$user->id} = $user->get_extra_metadata('initials'); - } - } - - my $csv = Text::CSV->new({ binary => 1, eol => "" }); - - my $p_count = 0; - my $link_id = $c->cobrand->exor_rdi_link_id; - - # RDI first line is always the same - $csv->combine("1", "1.8", "1.0.0.0", "ENHN", ""); - my @body = ($csv->string); - - my $i = 0; - foreach my $inspector_id (keys %$inspectors) { - my $inspections = $inspectors->{$inspector_id}; - my $initials = $inspector_initials->{$inspector_id}; - - $csv->combine( - "G", # start of an area/sequence - $link_id, # area/link id, fixed value for our purposes - "","", # must be empty - $initials || "XX", # inspector initials - $start_date->strftime("%y%m%d"), # date of inspection yymmdd - "0700", # time of inspection hhmm, set to static value for now - "D", # inspection variant, should always be D - "INS", # inspection type, always INS - "N", # Area of the county - north (N) or south (S) - "", "", "", "" # empty fields - ); - push @body, $csv->string; - - $csv->combine( - "H", # initial inspection type - "MC" # minor carriageway (changes depending on activity code) - ); - push @body, $csv->string; - - foreach my $report (@$inspections) { - my ($eastings, $northings) = $report->local_coords; - - my $location = "${eastings}E ${northings}N."; - $location = "[DID NOT USE MAP] $location" unless $report->used_map; - my $closest_address = $c->cobrand->find_closest($report, 1); - if (%$closest_address) { - $location .= " Nearest road: $closest_address->{road}." if $closest_address->{road}; - $location .= " Nearest postcode: $closest_address->{postcode}{postcode}." if $closest_address->{postcode}; - } - - my $description = sprintf("%s %s", $report->external_id || "", $report->get_extra_metadata('detailed_information') || ""); - my $activity_code = $report->defect_type ? - $report->defect_type->get_extra_metadata('activity_code') - : 'MC'; - my $traffic_information = $report->get_extra_metadata('traffic_information') ? - 'TM ' . $report->get_extra_metadata('traffic_information') - : 'TM none'; - - $csv->combine( - "I", # beginning of defect record - $activity_code, # activity code - minor carriageway, also FC (footway) - "", # empty field, can also be A (seen on MC) or B (seen on FC) - sprintf("%03d", ++$i), # randomised sequence number - $location, # defect location field, which we don't capture from inspectors - $report->inspection_log_entry->whenedited->strftime("%H%M"), # defect time raised - "","","","","","","", # empty fields - $traffic_information, - $description, # defect description - ); - push @body, $csv->string; - - my $defect_type = $report->defect_type ? - $report->defect_type->get_extra_metadata('defect_code') - : 'SFP2'; - $csv->combine( - "J", # georeferencing record - $defect_type, # defect type - SFP2: sweep and fill <1m2, POT2 also seen - $report->response_priority ? - $report->response_priority->external_id : - "2", # priority of defect - "","", # empty fields - $eastings, # eastings - $northings, # northings - "","","","","" # empty fields - ); - push @body, $csv->string; - - $csv->combine( - "M", # bill of quantities record - "resolve", # permanent repair - "","", # empty fields - "/CMC", # /C + activity code - "", "" # empty fields - ); - push @body, $csv->string; - } - - # end this group of defects with a P record - $csv->combine( - "P", # end of area/sequence - 0, # always 0 - 999999, # charging code, always 999999 in OCC - ); - push @body, $csv->string; - $p_count++; - } - - # end the RDI file with an X record - my $record_count = $i; - $csv->combine( - "X", # end of inspection record - $p_count, - $p_count, - $record_count, # number of I records - $record_count, # number of J records - 0, 0, 0, # always zero - $record_count, # number of M records - 0, # always zero - $p_count, - 0, 0, 0 # error counts, always zero - ); - push @body, $csv->string; - - my $start = $start_date->strftime("%Y%m%d"); - my $end = $end_date->strftime("%Y%m%d"); - my $filename = sprintf("exor_defects-%s-%s.rdi", $start, $end); - if ( $user ) { - my $initials = $user->get_extra_metadata("initials") || ""; - $filename = sprintf("exor_defects-%s-%s-%s.rdi", $start, $end, $initials); - } - $c->res->content_type('text/csv; charset=utf-8'); - $c->res->header('content-disposition' => "attachment; filename=$filename"); - # The RDI format is very weird CSV - each line must be wrapped in - # double quotes. - $c->res->body( join "", map { "\"$_\"\r\n" } @body ); + }; } 1; -- cgit v1.2.3 From 3fb659de784e8cc40817f072f7181187f460f860 Mon Sep 17 00:00:00 2001 From: Dave Arter Date: Mon, 24 Jul 2017 16:09:27 +0100 Subject: [Oxfordshire] RDI file should show all inspections as happening on the same date Fixes mysociety/fixmystreetforcouncils#203 --- perllib/FixMyStreet/App/Controller/Admin/ExorDefects.pm | 1 + 1 file changed, 1 insertion(+) (limited to 'perllib/FixMyStreet/App/Controller/Admin/ExorDefects.pm') diff --git a/perllib/FixMyStreet/App/Controller/Admin/ExorDefects.pm b/perllib/FixMyStreet/App/Controller/Admin/ExorDefects.pm index 1711ecb10..b03a2ab98 100644 --- a/perllib/FixMyStreet/App/Controller/Admin/ExorDefects.pm +++ b/perllib/FixMyStreet/App/Controller/Admin/ExorDefects.pm @@ -56,6 +56,7 @@ sub download : Path('download') : Args(0) { my $params = { start_date => $start_date, + inspection_date => $start_date, end_date => $end_date + $one_day, user => $c->get_param('user_id'), }; -- cgit v1.2.3 From 137dcda8e489c5cbd2a777c83148d1e4a3bd1a60 Mon Sep 17 00:00:00 2001 From: Matthew Somerville Date: Fri, 8 Sep 2017 20:51:03 +0100 Subject: Fix bug if first page after restart is admin. If the first page looked at after server launch was an admin one, the User object was getting a different schema attached than the one used by everything else (so e.g. the cobrand was not then available to it, causing a crash on a body page). Using auto instead of begin prevents this from happening, as the setup_request auto always runs first. --- perllib/FixMyStreet/App/Controller/Admin/ExorDefects.pm | 6 ------ 1 file changed, 6 deletions(-) (limited to 'perllib/FixMyStreet/App/Controller/Admin/ExorDefects.pm') diff --git a/perllib/FixMyStreet/App/Controller/Admin/ExorDefects.pm b/perllib/FixMyStreet/App/Controller/Admin/ExorDefects.pm index b03a2ab98..b6f1e6bdf 100644 --- a/perllib/FixMyStreet/App/Controller/Admin/ExorDefects.pm +++ b/perllib/FixMyStreet/App/Controller/Admin/ExorDefects.pm @@ -9,12 +9,6 @@ use FixMyStreet::Integrations::ExorRDI; BEGIN { extends 'Catalyst::Controller'; } -sub begin : Private { - my ( $self, $c ) = @_; - - $c->forward('/admin/begin'); -} - sub index : Path : Args(0) { my ( $self, $c ) = @_; -- cgit v1.2.3 From 2cc255b912dc0f79de270be682a9e85d7fb95d7a Mon Sep 17 00:00:00 2001 From: Dave Arter Date: Tue, 5 Sep 2017 12:10:28 +0100 Subject: [Oxfordshire] Log when problems sent by RDI email. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The timestamp of when a problem was included in an RDI is shown on the problem’s inspector form. Also check back and include any inspected reports that for some reason haven't been instructed yet. Fixes mysociety/fixmystreetforcouncils#223. --- perllib/FixMyStreet/App/Controller/Admin/ExorDefects.pm | 1 + 1 file changed, 1 insertion(+) (limited to 'perllib/FixMyStreet/App/Controller/Admin/ExorDefects.pm') diff --git a/perllib/FixMyStreet/App/Controller/Admin/ExorDefects.pm b/perllib/FixMyStreet/App/Controller/Admin/ExorDefects.pm index b03a2ab98..0fd9fe023 100644 --- a/perllib/FixMyStreet/App/Controller/Admin/ExorDefects.pm +++ b/perllib/FixMyStreet/App/Controller/Admin/ExorDefects.pm @@ -59,6 +59,7 @@ sub download : Path('download') : Args(0) { inspection_date => $start_date, end_date => $end_date + $one_day, user => $c->get_param('user_id'), + mark_as_processed => 0, }; my $rdi = FixMyStreet::Integrations::ExorRDI->new($params); -- cgit v1.2.3