Skip to content

Commit

Permalink
[Waste] [Bulky] Add report ID to cancel URLs.
Browse files Browse the repository at this point in the history
In preparation for potentially having more than one booking.
  • Loading branch information
dracos committed Sep 12, 2023
1 parent 65240c9 commit 267b53e
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 84 deletions.
49 changes: 29 additions & 20 deletions perllib/FixMyStreet/App/Controller/Waste/Bulky.pm
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ has index_template => (
sub setup : Chained('/waste/property') : PathPart('bulky') : CaptureArgs(0) {
my ($self, $c) = @_;

if ( !$c->stash->{property}{show_bulky_waste}
|| $c->stash->{property}{pending_bulky_collection} )
{
if ( !$c->stash->{property}{show_bulky_waste} ) {
$c->detach('/waste/property_redirect');
}
}
Expand All @@ -43,12 +41,9 @@ sub bulky_item_options_method {
return \@options;
};

sub index : PathPart('') : Chained('setup') : Args(0) {
sub item_list : Private {
my ($self, $c) = @_;

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

my $max_items = $c->cobrand->bulky_items_maximum;
my $field_list = [];
for my $num ( 1 .. $max_items ) {
Expand Down Expand Up @@ -99,7 +94,18 @@ sub index : PathPart('') : Chained('setup') : Args(0) {
},
];
$c->stash->{field_list} = $field_list;
}

sub index : PathPart('') : Chained('setup') : Args(0) {
my ($self, $c) = @_;

if ($c->stash->{property}{pending_bulky_collection}) {
$c->detach('/waste/property_redirect');
}

$c->stash->{first_page} = 'intro';
$c->stash->{form_class} = 'FixMyStreet::App::Form::Waste::Bulky';
$c->forward('item_list');
$c->forward('form');

if ( $c->stash->{form}->current_page->name eq 'intro' ) {
Expand Down Expand Up @@ -128,27 +134,23 @@ sub view : Private {
$saved_data->{phone} = $p->user->phone;
$saved_data->{resident} = 'Yes';

my $items_list = $c->cobrand->call_hook('bulky_items_master_list');
my $per_item = $c->cobrand->bulky_per_item_costs;

$c->stash->{form} = {
items_extra => $c->cobrand->call_hook('bulky_items_extra'),
saved_data => $saved_data,
};
}

sub cancel : PathPart('bulky_cancel') : Chained('/waste/property') : Args(0) {
my ( $self, $c ) = @_;
sub cancel : Chained('setup') : Args(1) {
my ( $self, $c, $id ) = @_;

$c->detach( '/auth/redirect' ) unless $c->user_exists;

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_enabled')
|| !$c->cobrand->call_hook( 'bulky_can_view_collection',
$c->stash->{property}{pending_bulky_collection} )
|| !$c->cobrand->call_hook( 'bulky_collection_can_be_cancelled',
$c->stash->{property}{pending_bulky_collection} );
if !$c->cobrand->call_hook('bulky_can_view_collection', $collection)
|| !$c->cobrand->call_hook('bulky_collection_can_be_cancelled', $collection);

$c->stash->{cancelling_booking} = $collection;
$c->stash->{first_page} = 'intro';
$c->stash->{form_class} = 'FixMyStreet::App::Form::Waste::Bulky::Cancel';
$c->stash->{entitled_to_refund} = $c->cobrand->call_hook('bulky_can_refund');
Expand Down Expand Up @@ -190,10 +192,10 @@ sub process_bulky_data : Private {
return 1;
}

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

my $collection_report = $c->stash->{property}{pending_bulky_collection};
my $collection_report = $c->stash->{cancelling_booking};
my %data = (
detail => $collection_report->detail,
name => $collection_report->name,
Expand All @@ -202,6 +204,13 @@ sub process_bulky_cancellation : Private {
$c->cobrand->call_hook( "waste_munge_bulky_cancellation_data", \%data );

$c->forward( '/waste/add_report', [ \%data ] ) or return;
}

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

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

# Mark original report as closed
$collection_report->state('closed');
Expand Down
15 changes: 7 additions & 8 deletions perllib/FixMyStreet/Cobrand/Peterborough/Bulky.pm
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,12 @@ sub bulky_can_refund {
# Skip refund eligibility check for bulky goods soft launch; just
# assume if a collection can be cancelled, it can be refunded
# (see https://3.basecamp.com/4020879/buckets/26662378/todos/5870058641)
return $self->within_bulky_cancel_window
return $self->within_bulky_cancel_window($c->stash->{cancelling_booking})
if $self->bulky_enabled_staff_only;

return $c->stash->{property}{pending_bulky_collection}
return $c->stash->{cancelling_booking}
->get_extra_field_value('CHARGEABLE') ne 'FREE'
&& $self->within_bulky_refund_window;
&& $self->within_bulky_refund_window($c->stash->{cancelling_booking});
}

# A cancellation made less than 24 hours before the collection is scheduled to
Expand Down Expand Up @@ -310,15 +310,14 @@ sub waste_munge_bulky_cancellation_data {
my ( $self, $data ) = @_;

my $c = $self->{c};
my $collection_report = $c->stash->{property}{pending_bulky_collection};
my $collection_report = $c->stash->{cancelling_booking};
my $original_sr_number = $collection_report->external_id =~ s/Bartec-//r;

$data->{title} = 'Bulky goods cancellation';
$data->{category} = 'Bulky cancel';
$data->{detail} .= " | Original report ID: " . $collection_report->id;
$data->{detail} .= " | Original report ID: $original_sr_number (WasteWorks " . $collection_report->id . ")";

$c->set_param( 'COMMENTS', 'Cancellation at user request' );

my $original_sr_number = $collection_report->external_id =~ s/Bartec-//r;
$c->set_param( 'ORIGINAL_SR_NUMBER', $original_sr_number );
}

Expand Down Expand Up @@ -383,7 +382,7 @@ sub unset_free_bulky_used {
my $c = $self->{c};

return
unless $c->stash->{property}{pending_bulky_collection}
unless $c->stash->{cancelling_booking}
->get_extra_field_value('CHARGEABLE') eq 'FREE';

my $bartec = $self->feature('bartec');
Expand Down
34 changes: 16 additions & 18 deletions perllib/FixMyStreet/Roles/CobrandBulkyWaste.pm
Original file line number Diff line number Diff line change
Expand Up @@ -150,18 +150,24 @@ sub bulky_total_cost {

# Should only be a single open collection for a given property, but in case
# there isn't, return the most recent
sub find_pending_bulky_collections {
my ( $self, $uprn ) = @_;

return $self->problems->search({
category => 'Bulky collection',
extra => { '@>' => encode_json({ "_fields" => [ { name => 'uprn', value => $uprn } ] }) },
state => { '=', [ FixMyStreet::DB::Result::Problem->open_states ] },
}, {
order_by => { -desc => 'id' }
});
}

# Originally was only a single open collection for a given property,
# so this returns the most recent
sub find_pending_bulky_collection {
my ( $self, $property ) = @_;

return FixMyStreet::DB->resultset('Problem')->to_body( $self->body )
->find(
{ category => 'Bulky collection',
extra => { '@>' => encode_json({ "_fields" => [ { name => 'uprn', value => $property->{uprn} } ] }) },
state =>
{ '=', [ FixMyStreet::DB::Result::Problem->open_states ] },
},
{ order_by => { -desc => 'id' } },
);
return $self->find_pending_bulky_collections($property->{uprn})->first;
}

sub _bulky_collection_window {
Expand Down Expand Up @@ -250,10 +256,6 @@ sub bulky_collection_can_be_cancelled {
sub within_bulky_cancel_window {
my ( $self, $collection ) = @_;

my $c = $self->{c};
$collection //= $c->stash->{property}{pending_bulky_collection};
return 0 unless $collection;

my $now_dt = DateTime->now( time_zone => FixMyStreet->local_time_zone );
my $collection_date = $self->collection_date($collection);
return $self->_check_within_bulky_cancel_window($now_dt, $collection_date);
Expand All @@ -273,11 +275,7 @@ sub bulky_can_refund {
}

sub within_bulky_refund_window {
my $self = shift;
my $c = $self->{c};

my $open_collection = $c->stash->{property}{pending_bulky_collection};
return 0 unless $open_collection;
my ($self, $open_collection) = @_;

my $now_dt = DateTime->now( time_zone => FixMyStreet->local_time_zone );
my $collection_dt = $self->collection_date($open_collection);
Expand Down
Loading

0 comments on commit 267b53e

Please sign in to comment.