Skip to content

Commit

Permalink
[Waste] Way to amend existing bulky booking.
Browse files Browse the repository at this point in the history
  • Loading branch information
dracos committed Sep 21, 2023
1 parent 8becd5f commit df68e84
Show file tree
Hide file tree
Showing 14 changed files with 784 additions and 313 deletions.
5 changes: 0 additions & 5 deletions perllib/FixMyStreet/App/Controller/Waste.pm
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,6 @@ sub pay_cancel : Local : Args(2) {

my $p = $c->stash->{report};
my $saved_data = $c->cobrand->waste_reconstruct_bulky_data($p);
$saved_data->{name} = $p->name;
$saved_data->{email} = $p->user->email;
$saved_data->{phone} = $p->user->phone;
$saved_data->{resident} = 'Yes';

my $saved_data_field = FixMyStreet::App::Form::Field::JSON->new(name => 'saved_data');
$saved_data = $saved_data_field->deflate_json($saved_data);
$c->set_param(saved_data => $saved_data);
Expand Down
111 changes: 103 additions & 8 deletions perllib/FixMyStreet/App/Controller/Waste/Bulky.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ BEGIN { extends 'FixMyStreet::App::Controller::Form' }

use utf8;
use FixMyStreet::App::Form::Waste::Bulky;
use FixMyStreet::App::Form::Waste::Bulky::Amend;
use FixMyStreet::App::Form::Waste::Bulky::Cancel;

has feature => (
Expand Down Expand Up @@ -115,6 +116,34 @@ sub index : PathPart('') : Chained('setup') : Args(0) {
}
}

sub amend : Chained('setup') : Args(1) {
my ($self, $c, $id) = @_;

$c->stash->{first_page} = 'intro';
$c->stash->{form_class} = 'FixMyStreet::App::Form::Waste::Bulky::Amend';

my $collection = $c->cobrand->find_pending_bulky_collections($c->stash->{property}{uprn})->find($id);
$c->detach('/waste/property_redirect')
if !$c->cobrand->call_hook('bulky_can_amend_collection', $collection);

$c->stash->{amending_booking} = $collection;

if ( $c->req->method eq 'GET') { # XXX
my $saved_data = $c->cobrand->waste_reconstruct_bulky_data($collection);
my $saved_data_field = FixMyStreet::App::Form::Field::JSON->new(name => 'saved_data');
$saved_data = $saved_data_field->deflate_json($saved_data);
$c->set_param(saved_data => $saved_data);
}

$c->forward('item_list');
$c->forward('form');

if ( $c->stash->{form}->current_page->name eq 'intro' ) {
$c->cobrand->call_hook(
clear_cached_lookups_bulky_slots => $c->stash->{property}{id} );
}
}

# Called by F::A::Controller::Report::display if the report in question is
# a bulky goods collection.
sub view : Private {
Expand All @@ -128,13 +157,26 @@ sub view : Private {

$c->stash->{template} = 'waste/bulky/summary.html';

# And include moderation changes...
my $user_can_moderate = $c->user_exists && $c->user->can_moderate($p);
my @combined;
if ($user_can_moderate) {
my @history = $p->moderation_history;
my $last_history = $p;
foreach my $history (@history) {
push @combined, [ $history->created, {
id => 'm' . $history->id,
type => 'moderation',
last => $last_history,
entry => $history,
} ];
$last_history = $history;
}
}
@combined = map { $_->[1] } sort { $a->[0] <=> $b->[0] } @combined;
$c->stash->{updates} = \@combined;

my $saved_data = $c->cobrand->waste_reconstruct_bulky_data($p);
$saved_data->{name} = $p->name;
$saved_data->{email} = $p->user->email;
$saved_data->{phone} = $p->user->phone;
$saved_data->{resident} = 'Yes';

$c->stash->{form} = {
items_extra => $c->cobrand->call_hook('bulky_items_extra'),
saved_data => $saved_data,
Expand Down Expand Up @@ -193,15 +235,68 @@ sub process_bulky_data : Private {
return 1;
}

sub process_bulky_amend : Private {
my ($self, $c, $form) = @_;
my $data = $form->saved_data;

$c->stash->{override_confirmation_template} = 'waste/bulky/confirmation.html';

my $p = $c->stash->{amending_booking};
$p->create_related( moderation_original_data => {
title => $p->title,
detail => $p->detail,
photo => $p->photo,
anonymous => $p->anonymous,
category => $p->category,
extra => $p->extra,
});

$p->detail($p->detail . " | Previously submitted as " . $p->external_id);

$c->cobrand->waste_munge_bulky_amend($p, $data);

if ($data->{location_photo}) {
$p->set_extra_metadata(location_photo => $data->{location_photo})
} else {
$p->unset_extra_metadata('location_photo');
}

my $max = $c->cobrand->bulky_items_maximum;
for (1..$max) {
if ($data->{"item_photo_$_"}) {
$p->set_extra_metadata("item_photo_$_" => $data->{"item_photo_$_"})
} else {
$p->unset_extra_metadata("item_photo_$_");
}
}

my @bulky_photo_data;
push @bulky_photo_data, $data->{location_photo} if $data->{location_photo};
for (grep { /^item_photo_\d+$/ } sort keys %$data) {
push @bulky_photo_data, $data->{$_} if $data->{$_};
}
$p->photo( join(',', @bulky_photo_data) );

$c->forward('add_cancellation_report');

$p->resend;
$p->external_id(undef);
$p->update;

# Need to reset stashed report to the amended one, not the new cancellation one
$c->stash->{report} = $p;

return 1;
}

sub add_cancellation_report : Private {
my ($self, $c) = @_;

my $collection_report = $c->stash->{cancelling_booking};
my $collection_report = $c->stash->{cancelling_booking} || $c->stash->{amending_booking};
my %data = (
detail => $collection_report->detail,
name => $collection_report->name,
);

$c->cobrand->call_hook( "waste_munge_bulky_cancellation_data", \%data );

if ($c->cobrand->bulky_cancel_by_update) {
Expand All @@ -219,7 +314,7 @@ sub process_bulky_cancellation : Private {
my ( $self, $c, $form ) = @_;

$c->forward('add_cancellation_report') or return;
my $collection_report = $c->stash->{cancelling_booking};
my $collection_report = $c->stash->{cancelling_booking} || $c->stash->{amending_booking};

# Mark original report as closed
$collection_report->state('closed');
Expand Down
Loading

0 comments on commit df68e84

Please sign in to comment.