Skip to content

Commit

Permalink
[Waste] Allow two bulky collection payment bands.
Browse files Browse the repository at this point in the history
  • Loading branch information
dracos committed Oct 4, 2023
1 parent bbe5c00 commit 03abaf7
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 33 deletions.
8 changes: 5 additions & 3 deletions perllib/FixMyStreet/App/Controller/Admin/Waste.pm
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,13 @@ sub edit : Chained('body') : PathPart('') : Args(0) {
} else {
$new_cfg = $c->stash->{body}->get_extra_metadata("wasteworks_config", {});
my %keys = (
free_mode => 'bool',
per_item_costs => 'bool',
base_price => 'int',
daily_slots => 'int',
items_per_collection_max => 'int',
band1_price => 'int',
band1_max => 'int',
free_mode => 'bool',
food_bags_disabled => 'bool',
show_location_page => 'sel'
);
Expand All @@ -98,7 +100,7 @@ sub edit : Chained('body') : PathPart('') : Args(0) {
if ($keys{$_} eq 'bool') {
$new_cfg->{$_} = $val ? 1 : 0;
} elsif ($keys{$_} eq 'int') {
if ($val ne $val+0) {
if ($val && $val ne $val+0) {
$c->stash->{errors}->{site_wide} = "Not an integer";
} elsif ($_ eq 'items_per_collection_max' && $val > 200) {
$c->stash->{errors}->{site_wide} = "Maximum items per collection cannot be more than 200";
Expand Down Expand Up @@ -215,7 +217,7 @@ sub stash_body_config_json : Private {
} else {
$c->stash->{body_config_json} = JSON->new->utf8(1)->pretty->canonical->encode($cfg);
}
foreach (qw(free_mode per_item_costs base_price daily_slots items_per_collection_max food_bags_disabled show_location_page)) {
foreach (qw(free_mode per_item_costs base_price daily_slots items_per_collection_max food_bags_disabled show_location_page band1_price band1_max)) {
$c->stash->{$_} = $c->get_param($_) || $cfg->{$_};
}
}
Expand Down
28 changes: 28 additions & 0 deletions perllib/FixMyStreet/Roles/CobrandBulkyWaste.pm
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ sub bulky_show_location_page {
};
sub bulky_show_location_field_mandatory { 0 }

sub bulky_pricing_strategy {
my $self = shift;
my $base_price = $self->wasteworks_config->{base_price};
my $band1_max = $self->wasteworks_config->{band1_max};
my $max = $self->bulky_items_maximum;
if ($self->bulky_per_item_costs) {
return encode_json({ strategy => 'per_item' });
} elsif (my $band1_price = $self->wasteworks_config->{band1_price}) {
return encode_json({ strategy => 'banded', bands => [ { max => $band1_max, price => $band1_price }, { max => $max, price => $base_price } ] });
} else {
return encode_json({ strategy => 'single' });
}
}

=head2 Requirements
Users of this role must supply the following:
Expand Down Expand Up @@ -127,6 +141,8 @@ sub bulky_minimum_cost {
map { $_->{price} } @{ $self->bulky_items_master_list };

return $sorted[0] // 0;
} elsif ( $cfg->{band1_price} ) {
return $cfg->{band1_price};
} else {
return $cfg->{base_price} // 0;
}
Expand All @@ -152,6 +168,18 @@ sub bulky_total_cost {
$total += $prices{$item};
}
$c->stash->{payment} = $total;
} elsif ($cfg->{band1_price}) {
my $count = 0;
my $max = $self->bulky_items_maximum;
for (1..$max) {
my $item = $data->{"item_$_"} or next;
$count++;
}
if ($count <= $cfg->{band1_max}) {
$c->stash->{payment} = $cfg->{band1_price};
} else {
$c->stash->{payment} = $cfg->{base_price};
}
} else {
$c->stash->{payment} = $cfg->{base_price};
}
Expand Down
2 changes: 2 additions & 0 deletions t/app/controller/waste_peterborough.t
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,8 @@ FixMyStreet::override_config {
$body->discard_changes;
is_deeply $body->get_extra_metadata('wasteworks_config'), {
show_location_page => 'noshow',
band1_max => '',
band1_price => '',
daily_slots => 50,
free_mode => 0, # not checked
food_bags_disabled => 0, # not checked
Expand Down
4 changes: 2 additions & 2 deletions t/app/controller/waste_peterborough_bulky.t
Original file line number Diff line number Diff line change
Expand Up @@ -1376,7 +1376,7 @@ FixMyStreet::override_config {
$mech->submit_form_ok({ with_fields => { resident => 'Yes' } });
$mech->submit_form_ok({ with_fields => { name => 'Bob Marge', email => $user->email }});
$mech->submit_form_ok({ with_fields => { chosen_date => '2022-08-26T00:00:00' } });
$mech->content_contains('£0.00');
$mech->content_like(qr/£<[^>]*>0\.00/);
$mech->submit_form_ok({ with_fields => { 'item_1' => 'Amplifiers', 'item_2' => 'High chairs' } });
$mech->submit_form_ok({ with_fields => { tandc => 1 } });

Expand Down Expand Up @@ -1438,7 +1438,7 @@ FixMyStreet::override_config {
$mech->submit_form_ok({ with_fields => { resident => 'Yes' } });
$mech->submit_form_ok({ with_fields => { name => 'Bob Marge', email => $user->email }});
$mech->submit_form_ok({ with_fields => { chosen_date => '2022-08-26T00:00:00' } });
$mech->content_contains('£23.50');
$mech->content_like(qr/£<[^>]*>23\.50/);
$mech->submit_form_ok({ with_fields => { 'item_1' => 'Amplifiers', 'item_2' => 'High chairs' } });
$mech->submit_form_ok({ with_fields => { tandc => 1 } });
my ( $token, $report, $report_id ) = get_report_from_redirect( $sent_params->{returnUrl} );
Expand Down
42 changes: 30 additions & 12 deletions templates/web/base/admin/waste/edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,38 @@ <h3>Bulky Collections</h3>
<div class="form-error">[% errors.site_wide %]</div>
[% END %]

<p>
<label for="waste_items_per_collection_max">Maximum items per collection (1-200)</label>
<input class="form-control" type=text max=200 min=1 name="items_per_collection_max" id="waste_items_per_collection_max"
value="[% items_per_collection_max %]">

<p>
<div style="display:flex;gap:1em;">
<div>
<label for="waste_base_price">Price for a collection (in <strong>pence</strong>)</label>
<input class="form-control" type=text name="base_price" id="waste_base_price"
value="[% base_price %]">
</div>

<div>
<label for="waste_items_per_collection_max">Maximum items per collection (1-200)</label>
<input class="form-control" type=text max=200 min=1 name="items_per_collection_max" id="waste_items_per_collection_max"
value="[% items_per_collection_max %]">
</div>
</div>

<div style="display:flex;gap:1em;">
<div>
<label for="waste_band1_price">Price for a small collection (in pence)</label>
<input class="form-control" type=text name="band1_price" id="waste_band1_price"
value="[% band1_price %]">
</div>
<div>
<label for="waste_band1_max">Up to how many items</label>
<input class="form-control" type=text name="band1_max" id="waste_band1_max"
value="[% band1_max %]">
</div>
</div>

<p>
<label for="waste_daily_slots">Available slots each day</label>
<input class="form-control" type=text name="daily_slots" id="waste_daily_slots"
value="[% daily_slots %]">

<p>
<input type=checkbox name="free_mode" value="1" id="waste_free_mode"
[% 'checked' IF free_mode %]>
<label class="inline" for="waste_free_mode">Free collection allowed</label>

<p>
<input type=checkbox name="per_item_costs" value=1 id="waste_per_item_costs"
[% 'checked' IF per_item_costs %]>
Expand All @@ -59,13 +71,19 @@ <h3>Bulky Collections</h3>
[% 'checked' IF per_item_costs %]>
<label class="inline" for="waste_per_item_costs">Charge per item, not per collection</label>

[% IF c.cobrand.moniker == 'peterborough' %]
<p>
<input type=checkbox name="free_mode" value="1" id="waste_free_mode"
[% 'checked' IF free_mode %]>
<label class="inline" for="waste_free_mode">Free collection allowed</label>

<h3>General Options</h3>

<p>
<input type=checkbox name="food_bags_disabled" value="1" id="waste_food_bags_disabled"
[% 'checked' IF food_bags_disabled %]>
<label class="inline" for="waste_food_bags_disabled">Food bag ordering disabled</label>

[% END %]

<hr />

Expand Down
6 changes: 1 addition & 5 deletions templates/web/base/waste/bulky/items.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,7 @@
<button type="button" id="add-new-item" class="btn-secondary govuk-!-margin-bottom-3" aria-label="Add item">+ Add item</button>

<p>
[% IF c.cobrand.bulky_per_item_costs %]
Total cost: £<span id="js-bulky-total">[% pounds(total) %]</span>
[% ELSE %]
Total cost: £[% pounds(total) %]
[% END %]
Total cost: £<span data-pricing="[% c.cobrand.bulky_pricing_strategy %]" id="js-bulky-total">[% pounds(total) %]</span>
</p>

[% PROCESS form override_fields = [ 'continue', 'saved_data', 'token', 'process', 'unique_id' ] %]
Expand Down
45 changes: 34 additions & 11 deletions web/js/waste.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,45 @@ $(function() {
var firstItem = $('.bulky-item-wrapper').first();

function disableAddItemButton() {
// It will disable button if the first item is empty and the max number of items has been reached.
// It will disable button if the first item is empty or the max number of items has been reached.
if (numItemsVisible == maxNumItems || $('.bulky-item-wrapper').first().find('ul.autocomplete__menu').children().length == 0) {
$("#add-new-item").prop('disabled', true);
} else {
$("#add-new-item").prop('disabled', false);
}
}

function updateTotal() {
var totalId = $('#js-bulky-total');
var pricing = totalId.data('pricing');
// Update total
var total = 0;
if (pricing.strategy === 'per_item') {
$('.govuk-select[name^="item_"] option:selected').each(function(i, e) {
var extra = $(this).data('extra');
var price = extra ? parseFloat(extra.price) : 0;
if (!isNaN(price)) {
total += price;
}
});
totalId.text((total / 100).toFixed(2));
} else if (pricing.strategy === 'banded') {
var count = 0;
$('.govuk-select[name^="item_"] option:selected').each(function(i, e) {
if ($(this).val()) {
count++;
}
});
for (var i=0; i<pricing.bands.length; i++) {
if (count <= pricing.bands[i].max) {
total = pricing.bands[i].price;
break;
}
}
totalId.text((total / 100).toFixed(2));
}
}

$('.govuk-select[name^="item_"]').change(function(e) {
var $this = $(this);
disableAddItemButton();
Expand All @@ -99,16 +130,7 @@ $(function() {
$this.closest('.bulky-item-wrapper').find('.bulky-item-message').hide();
}

// Update total
var total = 0;
$('.govuk-select[name^="item_"]').each(function(i, e) {
var extra = $(this).find('option').filter(':selected').data('extra');
var price = extra ? parseFloat(extra.price) : 0;
if (!isNaN(price)) {
total += price;
}
});
$('#js-bulky-total').text((total / 100).toFixed(2));
updateTotal();
});

// If page reloads reveals any wrapper with an item already selected.
Expand Down Expand Up @@ -170,6 +192,7 @@ $(function() {
$(this).closest('.bulky-item-wrapper').find('select.js-autocomplete').val('');
numItemsVisible = $('.bulky-item-wrapper:visible').length;
disableAddItemButton();
updateTotal();
});

});

0 comments on commit 03abaf7

Please sign in to comment.