Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Waste] Factor out garden costs to single package. #5324

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 26 additions & 35 deletions perllib/FixMyStreet/App/Controller/Waste.pm
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use FixMyStreet::App::Form::Waste::Garden::Cancel;
use FixMyStreet::App::Form::Waste::Garden::Renew;
use FixMyStreet::App::Form::Waste::Garden::Sacks::Purchase;
use FixMyStreet::App::Form::Waste::Garden::Transfer;
use WasteWorks::Costs;
use Memcached;
use JSON::MaybeXS;

Expand Down Expand Up @@ -1175,8 +1176,7 @@ sub garden_setup : Chained('property') : PathPart('') : CaptureArgs(0) {

$c->detach('property_redirect') if $c->stash->{waste_features}->{garden_disabled};

$c->stash->{per_new_bin_cost} = $c->cobrand->_get_cost('ggw_new_bin_cost');
$c->stash->{per_new_bin_first_cost} = $c->cobrand->_get_cost('ggw_new_bin_first_cost') || $c->stash->{per_new_bin_cost};
$c->stash->{garden_costs} = WasteWorks::Costs->new({ cobrand => $c->cobrand });
}

sub garden_check : Chained('garden_setup') : Args(0) {
Expand All @@ -1200,17 +1200,13 @@ sub garden : Chained('garden_setup') : Args(0) {
$c->detach('property_redirect') if $c->stash->{waste_features}->{garden_new_disabled};
$c->detach('property_redirect') if $c->cobrand->garden_current_subscription;

$c->stash->{per_bin_cost} = $c->cobrand->garden_waste_cost_pa;
$c->stash->{per_sack_cost} = $c->cobrand->garden_waste_sacks_cost_pa;
$c->stash->{next_month_cost} = $c->cobrand->garden_waste_cost_pa_in_one_month($c->stash->{per_bin_cost});

$c->stash->{first_page} = 'intro';
$c->stash->{garden_form_data} = {
max_bins => $c->cobrand->waste_garden_maximum
};
$c->stash->{form_class} = 'FixMyStreet::App::Form::Waste::Garden';
$c->cobrand->call_hook('waste_garden_subscribe_form_setup');
$c->forward('form');
$c->forward('garden_form');
}

sub garden_modify : Chained('garden_setup') : Args(0) {
Expand All @@ -1222,8 +1218,6 @@ sub garden_modify : Chained('garden_setup') : Args(0) {

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

$c->stash->{per_bin_cost} = $c->cobrand->garden_waste_cost_pa;

if ($c->stash->{slwp_garden_sacks} && $service->{garden_container} == 28) { # SLWP Sack
if ($c->cobrand->moniker eq 'kingston') {
my $payment_method = 'credit_card';
Expand Down Expand Up @@ -1253,10 +1247,8 @@ sub garden_modify : Chained('garden_setup') : Args(0) {

$c->stash->{display_end_date} = DateTime::Format::W3CDTF->parse_datetime($service->{end_date});
$c->stash->{garden_form_data} = {
pro_rata_bin_cost => $c->cobrand->waste_get_pro_rata_cost(1, $service->{end_date}),
max_bins => $max_bins,
bins => $service->{garden_bins},
end_date => $service->{end_date},
payment_method => $payment_method,
};

Expand All @@ -1269,7 +1261,7 @@ sub garden_modify : Chained('garden_setup') : Args(0) {
$c->stash->{first_page} = 'alter';
}

$c->forward('form');
$c->forward('garden_form');
}

sub garden_cancel : Chained('garden_setup') : Args(0) {
Expand Down Expand Up @@ -1317,15 +1309,11 @@ sub garden_renew : Chained('garden_setup') : Args(0) {
$c->stash->{garden_form_data} = {
max_bins => $max_bins,
bins => $service->{garden_bins},
end_date => $service->{end_date},
};

$c->stash->{per_bin_renewal_cost} = $c->cobrand->garden_waste_renewal_cost_pa($service->{end_date});
$c->stash->{per_sack_renewal_cost} = $c->cobrand->garden_waste_renewal_sacks_cost_pa($service->{end_date});

$c->stash->{form_class} = 'FixMyStreet::App::Form::Waste::Garden::Renew';
$c->cobrand->call_hook('waste_garden_renew_form_setup');
$c->forward('form');
$c->forward('garden_form');
}

sub garden_transfer : Chained('garden_setup') : Args(0) {
Expand All @@ -1337,6 +1325,17 @@ sub garden_transfer : Chained('garden_setup') : Args(0) {
$c->forward('form');
}

sub garden_form : Private {
my ($self, $c) = @_;
$c->forward('form');

# We need to inspect the form to see if a discount has been applied, and if so,
# adjust the already fetched cost data for display on the site
my $data = $c->stash->{form}->saved_data;
my $costs = $c->stash->{garden_costs};
$costs->discount($data->{apply_discount});
}

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

Expand Down Expand Up @@ -1450,31 +1449,27 @@ sub process_garden_modification : Private {
my $payment_method;
# Needs to check current subscription too
my $service = $c->cobrand->garden_current_subscription;
my $costs = WasteWorks::Costs->new({ cobrand => $c->cobrand, discount => $data->{apply_discount} });
if ($c->stash->{slwp_garden_sacks} && $service->{garden_container} == 28) { # SLWP Sack
$data->{bins_wanted} = 1;
$data->{new_bins} = 1;
$payment = $c->cobrand->garden_waste_sacks_cost_pa();
$payment = $costs->sacks($data->{bins_wanted});
$payment_method = 'credit_card';
($payment) = $c->cobrand->apply_garden_waste_discount($payment) if $data->{apply_discount};
$pro_rata = $payment; # Set so goes through flow below
} else {
my $bin_count = $data->{bins_wanted};
my $new_bins = $bin_count - $data->{current_bins};
$data->{new_bins} = $new_bins;

my $cost_pa = $c->cobrand->garden_waste_cost_pa($bin_count);
($cost_pa) = $c->cobrand->apply_garden_waste_discount($cost_pa) if $data->{apply_discount};
# One-off ad-hoc payment to be made now
if ( $new_bins > 0 ) {
my $cost_now_admin = $c->cobrand->garden_waste_new_bin_admin_fee($new_bins);
$pro_rata = $c->cobrand->waste_get_pro_rata_cost( $new_bins, $c->stash->{garden_form_data}->{end_date});
($cost_now_admin, $pro_rata) = $c->cobrand->apply_garden_waste_discount(
$cost_now_admin, $pro_rata) if $data->{apply_discount};
my $cost_now_admin = $costs->new_bin_admin_fee($new_bins);
$pro_rata = $costs->pro_rata_cost($new_bins);
$c->set_param('pro_rata', $pro_rata);
$c->set_param('admin_fee', $cost_now_admin);
}
$payment_method = $c->stash->{garden_form_data}->{payment_method};
$payment = $cost_pa;
$payment = $costs->bins($bin_count);
$payment = 0 if $payment_method ne 'direct_debit' && $new_bins < 0;

}
Expand Down Expand Up @@ -1595,6 +1590,7 @@ sub garden_calculate_subscription_payment : Private {

# Sack form handling
my $container = $data->{container_choice} || '';
my $costs = WasteWorks::Costs->new({ cobrand => $c->cobrand, discount => $data->{apply_discount} });
if ($container eq 'sack') {
if ($c->cobrand->moniker eq 'merton') {
# If renewing from bin to sacks, need to know bins to remove - better place for this?
Expand All @@ -1603,23 +1599,18 @@ sub garden_calculate_subscription_payment : Private {
}
$data->{new_bins} = $data->{bins_wanted}; # Always want all of them delivered

my $cost_pa = $c->cobrand->garden_waste_sacks_cost_pa() * $data->{bins_wanted};
($cost_pa) = $c->cobrand->apply_garden_waste_discount($cost_pa) if $data->{apply_discount};
$c->set_param('payment', $cost_pa);
$c->set_param('payment', $costs->sacks($data->{bins_wanted}));
} else {
my $bin_count = $data->{bins_wanted};
$data->{new_bins} = $bin_count - ($data->{current_bins} || 0);

my $cost_pa;
if ($type eq 'renew') {
my $service = $c->cobrand->garden_current_subscription;
$cost_pa = $c->cobrand->garden_waste_renewal_cost_pa($service->{end_date}, $bin_count);
$cost_pa = $costs->bins_renewal($bin_count);
} else {
$cost_pa = $c->cobrand->garden_waste_cost_pa($bin_count);
$cost_pa = $costs->bins($bin_count);
}
my $cost_now_admin = $c->cobrand->garden_waste_new_bin_admin_fee($data->{new_bins});
($cost_pa, $cost_now_admin) = $c->cobrand->apply_garden_waste_discount(
$cost_pa, $cost_now_admin) if $data->{apply_discount};
my $cost_now_admin = $costs->new_bin_admin_fee($data->{new_bins});

$c->set_param('payment', $cost_pa);
$c->set_param('admin_fee', $cost_now_admin);
Expand Down
33 changes: 8 additions & 25 deletions perllib/FixMyStreet/App/Form/Waste/Garden.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package FixMyStreet::App::Form::Waste::Garden;
use utf8;
use HTML::FormHandler::Moose;
extends 'FixMyStreet::App::Form::Waste';
use WasteWorks::Costs;

has_field service_id => ( type => 'Hidden' );

Expand All @@ -20,26 +21,11 @@ sub details_update_fields {
my $bin_count = $c->get_param('bins_wanted') || $form->saved_data->{bins_wanted} || $existing;
my $new_bins = $bin_count - $current_bins;

my $cost_pa = $bin_count == 0 ? 0 : $form->{c}->cobrand->garden_waste_cost_pa($bin_count);
my $cost_now_admin = $form->{c}->cobrand->garden_waste_new_bin_admin_fee($new_bins);
my $costs = WasteWorks::Costs->new({ cobrand => $c->cobrand, discount => $data->{apply_discount} });
my $cost_pa = $bin_count == 0 ? 0 : $costs->bins($bin_count);
my $cost_now_admin = $costs->new_bin_admin_fee($new_bins);
$c->stash->{cost_pa} = $cost_pa / 100;
$c->stash->{cost_now_admin} = $cost_now_admin / 100;
if ($data->{apply_discount}) {
(
$c->stash->{cost_pa},
$c->stash->{cost_now_admin},
$c->stash->{per_bin_cost},
$c->stash->{per_new_bin_cost},
$c->stash->{per_new_bin_first_cost},
) =
$c->cobrand->apply_garden_waste_discount(
$c->stash->{cost_pa},
$c->stash->{cost_now_admin},
$c->stash->{per_bin_cost},
$c->stash->{per_new_bin_cost},
$c->stash->{per_new_bin_first_cost},
);
}
$c->stash->{cost_now} = $c->stash->{cost_now_admin} + $c->stash->{cost_pa};
my $max_bins = $c->stash->{garden_form_data}->{max_bins};

Expand Down Expand Up @@ -112,16 +98,13 @@ has_page summary => (
my $bin_count = $data->{bins_wanted} || 1;
my $new_bins = $bin_count - $current_bins;
my $cost_pa;
my $costs = WasteWorks::Costs->new({ cobrand => $c->cobrand, discount => $data->{apply_discount} });
if (($data->{container_choice}||'') eq 'sack') {
$cost_pa = $c->cobrand->garden_waste_sacks_cost_pa() * $bin_count;
$cost_pa = $costs->sacks($bin_count);
} else {
$cost_pa = $c->cobrand->garden_waste_cost_pa($bin_count);
}
my $cost_now_admin = $c->cobrand->garden_waste_new_bin_admin_fee($new_bins);
if ($data->{apply_discount}) {
($cost_pa, $cost_now_admin) = $c->cobrand->apply_garden_waste_discount(
$cost_pa, $cost_now_admin);
$cost_pa = $costs->bins($bin_count);
}
my $cost_now_admin = $costs->new_bin_admin_fee($new_bins);

my $total = $cost_now_admin + $cost_pa;

Expand Down
23 changes: 9 additions & 14 deletions perllib/FixMyStreet/App/Form/Waste/Garden/Modify.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package FixMyStreet::App::Form::Waste::Garden::Modify;
use utf8;
use HTML::FormHandler::Moose;
extends 'FixMyStreet::App::Form::Waste';
use WasteWorks::Costs;

has_page intro => (
title => 'Change your garden waste subscription',
Expand Down Expand Up @@ -36,26 +37,19 @@ has_page alter => (
my $new_bins = $bins_wanted - $current_bins;

my $edit_current_allowed = $c->cobrand->call_hook('waste_allow_current_bins_edit');
my $cost_pa = $c->cobrand->garden_waste_cost_pa($bins_wanted);
my $cost_now_admin = $c->cobrand->garden_waste_new_bin_admin_fee($new_bins);
my $costs = WasteWorks::Costs->new({ cobrand => $c->cobrand, discount => $form->saved_data->{apply_discount} });
my $cost_pa = $costs->bins($bins_wanted);
my $cost_now_admin = $costs->new_bin_admin_fee($new_bins);
$c->stash->{cost_pa} = $cost_pa / 100;
$c->stash->{cost_now_admin} = $cost_now_admin / 100;

$c->stash->{new_bin_count} = 0;
$c->stash->{pro_rata} = 0;
if ($new_bins > 0) {
$c->stash->{new_bin_count} = $new_bins;
my $cost_pro_rata = $c->cobrand->waste_get_pro_rata_cost($new_bins, $data->{end_date});
my $cost_pro_rata = $costs->pro_rata_cost($new_bins);
$c->stash->{pro_rata} = ($cost_now_admin + $cost_pro_rata) / 100;
}
if ($form->saved_data->{apply_discount}) {
($c->stash->{cost_pa}, $c->stash->{cost_now_admin}, $c->stash->{pro_rata}) =
$c->cobrand->apply_garden_waste_discount(
$c->stash->{cost_pa},
$c->stash->{cost_now_admin},
$c->stash->{pro_rata},
);
}

my $max_bins = $data->{max_bins};
my %bin_params = ( default => $data->{bins}, range_end => $max_bins );
Expand All @@ -81,9 +75,10 @@ has_page summary => (
my $current_bins = $data->{current_bins};
my $bin_count = $data->{bins_wanted};
my $new_bins = $bin_count - $current_bins;
my $pro_rata = $c->cobrand->waste_get_pro_rata_cost( $new_bins, $c->stash->{garden_form_data}->{end_date});
my $cost_pa = $c->cobrand->garden_waste_cost_pa($bin_count);
my $cost_now_admin = $c->cobrand->garden_waste_new_bin_admin_fee($new_bins);
my $costs = WasteWorks::Costs->new({ cobrand => $c->cobrand, discount => $data->{apply_discount} });
my $pro_rata = $costs->pro_rata_cost($new_bins);
my $cost_pa = $costs->bins($bin_count);
my $cost_now_admin = $costs->new_bin_admin_fee($new_bins);
my $total = $cost_pa;
$pro_rata += $cost_now_admin;

Expand Down
22 changes: 8 additions & 14 deletions perllib/FixMyStreet/App/Form/Waste/Garden/Renew.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package FixMyStreet::App::Form::Waste::Garden::Renew;
use utf8;
use HTML::FormHandler::Moose;
extends 'FixMyStreet::App::Form::Waste';
use WasteWorks::Costs;

has_page discount => (
next => 'intro',
Expand Down Expand Up @@ -33,12 +34,9 @@ has_page intro => (

my $edit_current_allowed = $c->cobrand->call_hook('waste_allow_current_bins_edit');
my $bins_wanted_disabled = $c->cobrand->call_hook('waste_renewal_bins_wanted_disabled');
my $cost_pa = $c->cobrand->garden_waste_renewal_cost_pa($data->{end_date}, $bin_count);
my $cost_now_admin = $c->cobrand->garden_waste_new_bin_admin_fee($new_bins);
if ($form->saved_data->{apply_discount}) {
($cost_pa, $cost_now_admin) = $c->cobrand->apply_garden_waste_discount(
$cost_pa, $cost_now_admin);
}
my $costs = WasteWorks::Costs->new({ cobrand => $c->cobrand, discount => $form->saved_data->{apply_discount} });
my $cost_pa = $costs->bins_renewal($bin_count);
my $cost_now_admin = $costs->new_bin_admin_fee($new_bins);
$form->{c}->stash->{cost_pa} = $cost_pa / 100;
$form->{c}->stash->{cost_now_admin} = $cost_now_admin / 100;
$form->{c}->stash->{cost_now} = ($cost_now_admin + $cost_pa) / 100;
Expand All @@ -65,21 +63,17 @@ has_page summary => (
my $c = $form->{c};
my $data = $form->saved_data;

my $end_date = $c->stash->{garden_form_data}->{end_date};
my $current_bins = $data->{current_bins} || 0;
my $bin_count = $data->{bins_wanted} || 1;
my $new_bins = $bin_count - $current_bins;
my $cost_pa;
my $costs = WasteWorks::Costs->new({ cobrand => $c->cobrand, discount => $form->saved_data->{apply_discount} });
if (($data->{container_choice}||'') eq 'sack') {
$cost_pa = $c->cobrand->garden_waste_renewal_sacks_cost_pa($end_date) * $bin_count;
$cost_pa = $costs->sacks_renewal($bin_count);
} else {
$cost_pa = $form->{c}->cobrand->garden_waste_renewal_cost_pa($end_date, $bin_count);
}
my $cost_now_admin = $form->{c}->cobrand->garden_waste_new_bin_admin_fee($new_bins);
if ($data->{apply_discount}) {
($cost_pa, $cost_now_admin) = $c->cobrand->apply_garden_waste_discount(
$cost_pa, $cost_now_admin);
$cost_pa = $costs->bins_renewal($bin_count);
}
my $cost_now_admin = $costs->new_bin_admin_fee($new_bins);
my $total = $cost_now_admin + $cost_pa;

$data->{cost_now_admin} = $cost_now_admin / 100;
Expand Down
8 changes: 3 additions & 5 deletions perllib/FixMyStreet/App/Form/Waste/Garden/Sacks.pm
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package FixMyStreet::App::Form::Waste::Garden::Sacks;
use utf8;
use HTML::FormHandler::Moose;
extends 'FixMyStreet::App::Form::Waste::Garden';
use WasteWorks::Costs;

sub with_sacks_choice { 1 }
sub with_bins_wanted {
Expand Down Expand Up @@ -50,11 +51,8 @@ has_page sacks_details => (
my $data = $form->saved_data;
my $c = $form->{c};
my $count = $c->get_param('bins_wanted') || $data->{bins_wanted} || 1;
my $cost_pa = $c->cobrand->garden_waste_sacks_cost_pa() * $count;
if ($data->{apply_discount}) {
($cost_pa, $c->stash->{per_sack_cost}) =
$c->cobrand->apply_garden_waste_discount($cost_pa, $c->stash->{per_sack_cost});
}
my $costs = WasteWorks::Costs->new({ cobrand => $c->cobrand, discount => $form->saved_data->{apply_discount} });
my $cost_pa = $costs->sacks($count);
$c->stash->{cost_pa} = $cost_pa / 100;

my $bins_wanted_opts = { default => $count };
Expand Down
9 changes: 5 additions & 4 deletions perllib/FixMyStreet/App/Form/Waste/Garden/Sacks/Purchase.pm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package FixMyStreet::App::Form::Waste::Garden::Sacks::Purchase;
use utf8;
use HTML::FormHandler::Moose;
extends 'FixMyStreet::App::Form::Waste';
use WasteWorks::Costs;

has_page intro => (
title => 'Order more garden waste sacks',
Expand All @@ -16,8 +17,8 @@ has_page intro => (
update_field_list => sub {
my $form = shift;
my $c = $form->c;
my $cost = $c->cobrand->garden_waste_sacks_cost_pa();
$c->stash->{cost_pa} = $cost / 100;
my $costs = WasteWorks::Costs->new({ cobrand => $c->cobrand });
$c->stash->{cost_pa} = $costs->sacks(1) / 100;
return {
name => { default => $c->stash->{is_staff} ? '' : $c->user->name },
};
Expand All @@ -35,9 +36,9 @@ has_page summary => (
my $form = shift;
my $c = $form->{c};
my $data = $form->saved_data;
my $cost_pa = $c->cobrand->garden_waste_sacks_cost_pa();
my $costs = WasteWorks::Costs->new({ cobrand => $c->cobrand });

$data->{display_total} = $cost_pa / 100;
$data->{display_total} = $costs->sacks(1) / 100;

unless ( $c->stash->{is_staff} ) {
$data->{name} ||= $c->user->name;
Expand Down
Loading