Bug 22827: Add age dependency on other fields than dateaccessioned

A new agefield has been added to the 'Automatic item modifications by
age' tool. The options for the agefield are: replacementpricedate, datelastborrowed,
datelastseen, damaged_on, itemlost_on, withdrawn_on

If no option is selected then Koha will default to saving 'agefield' =
items.dateaccessioned

Similarly, if a Koha instance has an old item rule without 'agefield'
defined then Koha will default to using 'items.dateaccessioned'.
This is confirmed by the AutomaticItemModificationByAge.t unit test.

Test plan:

1. Go to: Tools > Catalog > Automatic item modifications by age
2. Observe there is a new 'Age field' dropdown in the rule form.
3. Create a rule, set the values:
- 'Age in days' = 20
- Leave 'Age field' = 'Choose an age field'
- 'Substitutions': 'items.barcode' = 'test'
- Save the rule
4. Confirm the 'List of rules' page displays 'items.dateaccessioned in the 'Age field' column
5. Add another rule:
- 'Age in days' = 2
- 'Age field' = 'items.datelastseen'
- 'Substitutions': 'items.barcode' = 'test2'
- Save the rule
6. Confirm the 'List of rules' page displays 'items.datelastseen' in
the 'Age field' column for that second rule
7. Add some more rules and confirm you can delete them
8. Edit a record:
- Make the items.dateaccessioned = 3 day ago (so rule 1 is false)
- Make the items.datelastseen = 3 days ago (so rule 2 is true)
9. Run the automatic_items_modification_by_age.pl:
- sudo koha-shell <instance>
- cd misc/cronjobs
- ./automatic_item_modification_by_age.pl -v -c
10. Confirm the item has it's barcode set to 'test2'
11. Run unit tests:
- sudo koha-shell <instance>
- prove t/db_dependent/Items/AutomaticItemModificationByAge.t -v

Sponsored-By: Catalyst IT

Signed-off-by: Andrew Fuerste-Henry <andrew@bywatersolutions.com>

Signed-off-by: Katrin Fischer <katrin.fischer.83@web.de>
Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
This commit is contained in:
Alex Buckley 2022-01-11 15:11:37 +00:00 committed by Fridolin Somers
parent 051dd6bf0b
commit bed1646444
5 changed files with 83 additions and 3 deletions

View file

@ -1834,6 +1834,9 @@ sub ToggleNewStatus {
my $report;
for my $rule ( @rules ) {
my $age = $rule->{age};
# Default to using items.dateaccessioned if there's an old item modification rule
# missing an agefield value
my $agefield = $rule->{agefield} ? $rule->{agefield} : 'items.dateaccessioned';
my $conditions = $rule->{conditions};
my $substitutions = $rule->{substitutions};
foreach ( @$substitutions ) {
@ -1865,7 +1868,7 @@ sub ToggleNewStatus {
}
}
if ( defined $age ) {
$query .= q| AND TO_DAYS(NOW()) - TO_DAYS(dateaccessioned) >= ? |;
$query .= qq| AND TO_DAYS(NOW()) - TO_DAYS($agefield) >= ? |;
push @params, $age;
}
my $sth = $dbh->prepare($query);

View file

@ -91,6 +91,20 @@
<div class="age">
<h5>Age in days</h5>
<input class="age" type="text" inputmode="numeric" pattern="[0-9]*" value="[% rule.age | html %]" name="age_[% id | html %]" />
<h5>Age field</h5>
<div class="blocks">
<select name="agefield_[% id | html %]">
<option value="">Choose an age field</option>
[% FOR field IN agefields %]
[% IF rule.agefield == field %]
<option value="[% field | html %]" selected="selected">[% field | html %]</option>
[% ELSE %]
<option value="[% field | html %]">[% field | html %]</option>
[% END %]
[% END %]
</select>
<span class="hint">If not set then items.dateaccessioned will be used</span>
</div>
</div>
<div class="blocks">
<h5>Conditions</h5>
@ -152,6 +166,16 @@
<div class="age">
<h5>Age in days</h5>
<input class="age" type="text" inputmode="numeric" pattern="[0-9]*" value="" name="age" />
<h5>Age field</h5>
<div class="block">
<select name="agefield">
<option value="">Choose an age field</option>
[% FOR field IN agefields %]
<option value="[% field | html %]">[% field | html %]</option>
[% END %]
</select>
<span class="hint">If not set then items.dateaccessioned will be used</span>
</div>
</div>
<div class="blocks">
<h5>Conditions</h5>
@ -192,6 +216,7 @@
<thead>
<tr>
<th>Age</th>
<th>Age field</th>
<th>Conditions</th>
<th>Substitutions</th>
</tr>
@ -206,6 +231,14 @@
There is no age for this rule.
[% END %]
</td>
<td>
[% IF rule.agefield %]
[% rule.agefield | html %]
[% ELSE %]
<!-- In case old rules do not contain an agefield value display that items.dateaccessioned will be used -->
items.dateaccessioned
[% END %]
</td>
<td>
[% FOR condition IN rule.conditions %]
[% IF condition.field %]

View file

@ -75,6 +75,7 @@ $(document).ready(function() {
new_rule.find("input[name='condition_value']").attr('name', 'condition_value_' + unique_id);
new_rule.find("input[name='substitution_value']").attr('name', 'substitution_value_' + unique_id);
new_rule.find("input[name='age']").attr('name', 'age_' + unique_id);
new_rule.find("select[name='agefield']").attr('name', 'agefield_' + unique_id);
new_rule.find("input[name='unique_id']").val(unique_id);
$("#rules").append(new_rule);

View file

@ -1,7 +1,7 @@
#!/usr/bin/perl
use Modern::Perl;
use Test::More tests => 17;
use Test::More tests => 19;
use MARC::Record;
use MARC::Field;
use DateTime;
@ -140,7 +140,7 @@ $modified_item->dateaccessioned($days5ago)->store;
value => 'new_updated_value',
},
],
age => '10',
age => '10', # Confirm not defining agefield, will default to using items.dateaccessioned
},
);
C4::Items::ToggleNewStatus( { rules => \@rules } );
@ -275,6 +275,7 @@ is( $modified_item->new_status, 'new_updated_value', q|ToggleNewStatus: conditio
@rules = (
{
# does not exist
conditions => [
{
field => 'biblioitems.itemtype',
@ -296,6 +297,40 @@ C4::Items::ToggleNewStatus( { rules => \@rules } );
$modified_item = Koha::Items->find( $itemnumber );
is( $modified_item->new_status, 'another_new_updated_value', q|ToggleNewStatus: conditions on biblioitems|);
# Play with the 'Age field'
my $days2ago = $dt_today->add_duration( DateTime::Duration->new( days => -10 ) );
my $days20ago = $dt_today->add_duration( DateTime::Duration->new( days => -20 ) );
$modified_item->datelastseen($days2ago)->store;
$modified_item->dateaccessioned($days20ago)->store;
# When agefield='items.datelastseen'
@rules = (
{
conditions => [
{
field => 'biblioitems.itemtype',
value => 'ITEMTYPE_T',
},
],
substitutions => [
{
field => 'items.new_status',
value => 'agefield_new_value',
},
],
age => '5',
agefield => 'items.datelastseen' # Confirm defining agefield => 'items.datelastseen' will use items.datelastseen
},
);
C4::Items::ToggleNewStatus( { rules => \@rules } );
$modified_item = Koha::Items->find( $itemnumber );
is( $modified_item->new_status, 'agefield_new_value', q|ToggleNewStatus: Age = 5, agefield = 'items.datelastseen' : The new_status value is not updated|);
$rules[0]->{age} = 2;
C4::Items::ToggleNewStatus( { rules => \@rules } );
$modified_item = Koha::Items->find( $itemnumber );
is( $modified_item->new_status, 'agefield_new_value', q|ToggleNewStatus: Age = 2, agefield = 'items.datelastseen' : The new_status value is updated|);
# Run twice
t::lib::Mocks::mock_preference('CataloguingLog', 1);
my $actions_nb = $schema->resultset('ActionLog')->count();

View file

@ -3,6 +3,7 @@
# This file is part of Koha.
#
# Copyright 2013 BibLibre
# Copyright 2021 Catalyst IT
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
@ -65,6 +66,7 @@ if ( $op eq 'update' ) {
my @substitution_values = $cgi->multi_param("substitution_value_$unique_id");
my @condition_fields = $cgi->multi_param("condition_field_$unique_id");
my @condition_values = $cgi->multi_param("condition_value_$unique_id");
my @age_fields = $cgi->multi_param("agefield_$unique_id");
my $rule = {
substitutions => [],
conditions => [],
@ -84,6 +86,10 @@ if ( $op eq 'update' ) {
push @{ $rule->{conditions} }, {}
unless @{ $rule->{conditions} };
$rule->{age} = $cgi->param("age_$unique_id");
for my $age_field ( @age_fields ) {
$rule->{agefield} = $age_field ? $age_field : "items.dateaccessioned";
}
push @rules, $rule;
}
my $syspref_content = to_json( \@rules );
@ -109,9 +115,11 @@ if ( $@ ) {
my @item_fields = map { "items.$_" } Koha::Items->columns;
my @biblioitem_fields = map { "biblioitems.$_" } Koha::Biblioitems->columns;
my @age_fields = ('items.dateaccessioned', 'items.replacementpricedate', 'items.datelastborrowed', 'items.datelastseen', 'items.damaged_on', 'items.itemlost_on', 'items.withdrawn_on');
$template->param(
op => $op,
messages => \@messages,
agefields => [ @age_fields ],
condition_fields => [ @item_fields, @biblioitem_fields ],
substitution_fields => \@item_fields,
rules => $rules,