Skip to content

Commit

Permalink
[Waste] Add separate first garden bin cost.
Browse files Browse the repository at this point in the history
  • Loading branch information
dracos committed Jan 14, 2025
1 parent e27363c commit 63b570e
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 11 deletions.
57 changes: 48 additions & 9 deletions perllib/WasteWorks/Costs.pm
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ sub _build_has_pro_rata_modify { $_[0]->cobrand->moniker eq 'bromley' }
sub bins {
my ($self, $count) = @_;
$count ||= 1;
my $cost = $self->get_cost('ggw_cost');
$cost *= $count;
return $self->apply_garden_discount($cost);
my $per_bin = $self->get_cost('ggw_cost') ;
my $first_cost = $self->get_cost('ggw_cost_first') || $per_bin;
my $cost = $self->_first_diff_calc($first_cost, $per_bin, $count);
return $cost;
}

sub sacks {
Expand All @@ -56,8 +57,11 @@ sub _renewal {
$end_date //= $self->service->{end_date};
my $cost = $self->get_cost($prefix . '_renewal', $end_date)
|| $self->get_cost($prefix, $end_date);
$cost *= $count;
return $self->apply_garden_discount($cost);
my $first_cost = $self->get_cost($prefix . '_renewal_first', $end_date)
|| $self->get_cost($prefix . '_first', $end_date)
|| $cost;
$cost = $self->_first_diff_calc($first_cost, $cost, $count);
return $cost;
} elsif ($type eq 'sacks') {
return $self->sacks($count);
} else {
Expand All @@ -83,11 +87,17 @@ sub new_bin_admin_fee {

$count ||= 0;
my $per_new_bin_cost = $self->get_cost('ggw_new_bin_cost');
my $cost = $self->_first_diff_calc($per_new_bin_first_cost, $per_new_bin_cost, $count);
return $cost;
}

sub _first_diff_calc {
my ($self, $first_cost, $rest_cost, $count) = @_;
my $cost = 0;
if ($count > 0) {
$cost += $per_new_bin_first_cost;
$cost += $first_cost;
if ($count > 1) {
$cost += $per_new_bin_cost * ($count - 1);
$cost += $rest_cost * ($count - 1);
}
}
return $self->apply_garden_discount($cost);
Expand Down Expand Up @@ -129,6 +139,9 @@ sub apply_garden_discount {
return $discounted;
}

# Next month does not currently handle a first bin being a different price, if
# those two ever get used together.

sub next_month {
my $self = shift;
my $per_bin = $self->get_cost('ggw_cost');
Expand Down Expand Up @@ -172,7 +185,16 @@ sub garden_cost_pa_in_one_month {
# Functions used for display of bin pricing/calculation during flow (all begin per_)
# $_[0] is the self instance, just without setting a variable

sub per_bin { $_[0]->bins(1) }
sub per_bin {
$_[0]->apply_garden_discount($_[0]->get_cost('ggw_cost'));
}
sub per_bin_first {
$_[0]->apply_garden_discount(
$_[0]->get_cost('ggw_cost_first')
|| $_[0]->get_cost('ggw_cost')
);
}

sub per_sack { $_[0]->sacks(1) }

sub per_new_bin_first {
Expand All @@ -182,7 +204,24 @@ sub per_new_bin {
$_[0]->apply_garden_discount($_[0]->get_cost('ggw_new_bin_cost'));
}

sub per_bin_renewal { $_[0]->bins_renewal(1) }
sub per_bin_renewal {
my $self = shift;
my $end_date;
$end_date = $self->service->{end_date} if $self->renewal_type eq 'subscription_end';
my $cost = $self->get_cost('ggw_cost_renewal', $end_date)
|| $self->get_cost('ggw_cost', $end_date);
return $self->apply_garden_discount($cost);
}
sub per_bin_renewal_first {
my $self = shift;
my $end_date;
$end_date = $self->service->{end_date} if $self->renewal_type eq 'subscription_end';
my $first_cost = $self->get_cost('ggw_cost_renewal_first', $end_date)
|| $self->get_cost('ggw_cost_first', $end_date);
return $self->per_bin_renewal unless $first_cost;
return $self->apply_garden_discount($first_cost);
}

sub per_sack_renewal { $_[0]->sacks_renewal(1) }
sub per_pro_rata_bin { $_[0]->pro_rata_cost(1) }

Expand Down
8 changes: 8 additions & 0 deletions t/wasteworks/costs.t
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ my $brent = FixMyStreet::Cobrand::Brent->new; # Discount
my $kingston = FixMyStreet::Cobrand::Kingston->new; # Renews with end date, admin fee
my $sutton = FixMyStreet::Cobrand::Sutton->new; # Next month
my $merton = FixMyStreet::Cobrand::Merton->new;
my $bexley = FixMyStreet::Cobrand::Bexley->new;

set_fixed_time("2025-01-14T12:00:00Z");

Expand All @@ -29,6 +30,7 @@ FixMyStreet::override_config {
{ start_date => '2025-01-01 00:00', cost => 1500 },
{ start_date => '2025-02-01 00:00', cost => 1700 },
] },
bexley => { ggw_cost_first => 7500, ggw_cost => 5500 },
},
waste_features => {
brent => { ggw_discount_as_percent => 20 },
Expand All @@ -51,6 +53,12 @@ FixMyStreet::override_config {
is $costs->pro_rata_cost(3), 7200;
};

subtest 'first bin a different price' => sub {
my $costs = WasteWorks::Costs->new({ cobrand => $bexley });
is $costs->bins, 7500;
is $costs->bins(2), 7500+5500;
};

subtest 'sacks' => sub {
my $costs = WasteWorks::Costs->new({ cobrand => $merton });
is $costs->sacks(1), 750;
Expand Down
1 change: 1 addition & 0 deletions templates/web/base/waste/garden/modify.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<hr class="fieldset-hr">
<div class="cost-pa__total js-bin-costs"
data-per_bin_cost="[% garden_costs.per_bin %]"
data-per_bin_first_cost="[% garden_costs.per_bin_first %]"
data-per_new_bin_first_cost="[% garden_costs.per_new_bin_first %]"
data-per_new_bin_cost="[% garden_costs.per_new_bin %]"
data-pro_rata_bin_cost="[% garden_costs.per_pro_rata_bin %]"
Expand Down
1 change: 1 addition & 0 deletions templates/web/base/waste/garden/renew.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<hr class="fieldset-hr">
<div class="cost-pa__total js-bin-costs"
data-per_bin_cost="[% garden_costs.per_bin_renewal %]"
data-per_bin_first_cost="[% garden_costs.per_bin_renewal_first %]"
data-per_new_bin_first_cost="[% garden_costs.per_new_bin_first %]"
data-per_new_bin_cost="[% garden_costs.per_new_bin %]"
>
Expand Down
1 change: 1 addition & 0 deletions templates/web/base/waste/garden/subscribe_details.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

<div class="cost-pa__total js-bin-costs"
data-per_bin_cost="[% garden_costs.per_bin %]"
data-per_bin_first_cost="[% garden_costs.per_bin_first %]"
data-per_new_bin_first_cost="[% garden_costs.per_new_bin_first %]"
data-per_new_bin_cost="[% garden_costs.per_new_bin %]"
>
Expand Down
5 changes: 3 additions & 2 deletions web/js/waste.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ $(function() {

var costs = $('.js-bin-costs'),
cost = costs.data('per_bin_cost') / 100,
first_cost = costs.data('per_bin_first_cost') / 100,
per_new_bin_first_cost = costs.data('per_new_bin_first_cost') / 100,
per_new_bin_cost = costs.data('per_new_bin_cost') / 100,
pro_rata_bin_cost = costs.data('pro_rata_bin_cost') / 100;
function bin_cost_new() {
var total_bins = parseInt($('#bins_wanted').val() || 0);
var existing_bins = parseInt($('#current_bins').val() || 0);
var new_bins = total_bins - existing_bins;
var total_per_year = total_bins * cost;
var total_per_year = (total_bins-1) * cost + first_cost;
var admin_fee = 0;
if (new_bins > 0 && per_new_bin_first_cost) {
admin_fee += per_new_bin_first_cost;
Expand All @@ -42,7 +43,7 @@ $(function() {
var existing_bins = parseInt($('#current_bins').val() || 0);
var new_bins = total_bins - existing_bins;
var pro_rata_cost = 0;
var total_per_year = total_bins * cost;
var total_per_year = (total_bins-1) * cost + first_cost;
var admin_fee = 0;
var new_bin_text = new_bins == 1 ? 'bin' : 'bins';
$('#new_bin_text').text(new_bin_text);
Expand Down

0 comments on commit 63b570e

Please sign in to comment.