diff --git a/C4/Items.pm b/C4/Items.pm
index 6fe676814a..4d25a6ad49 100644
--- a/C4/Items.pm
+++ b/C4/Items.pm
@@ -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);
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/automatic_item_modification_by_age.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/automatic_item_modification_by_age.tt
index a6711c15af..c9670914b2 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/automatic_item_modification_by_age.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/automatic_item_modification_by_age.tt
@@ -91,6 +91,20 @@
Conditions
@@ -152,6 +166,16 @@
Age in days
+
Age field
+
+
+ If not set then items.dateaccessioned will be used
+
Conditions
@@ -192,6 +216,7 @@
| Age |
+ Age field |
Conditions |
Substitutions |
@@ -206,6 +231,14 @@
There is no age for this rule.
[% END %]
+
+ [% IF rule.agefield %]
+ [% rule.agefield | html %]
+ [% ELSE %]
+
+ items.dateaccessioned
+ [% END %]
+ |
[% FOR condition IN rule.conditions %]
[% IF condition.field %]
diff --git a/koha-tmpl/intranet-tmpl/prog/js/automatic_item_modification_by_age.js b/koha-tmpl/intranet-tmpl/prog/js/automatic_item_modification_by_age.js
index 6c37d9b210..09c4da1fc8 100644
--- a/koha-tmpl/intranet-tmpl/prog/js/automatic_item_modification_by_age.js
+++ b/koha-tmpl/intranet-tmpl/prog/js/automatic_item_modification_by_age.js
@@ -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);
diff --git a/t/db_dependent/Items/AutomaticItemModificationByAge.t b/t/db_dependent/Items/AutomaticItemModificationByAge.t
index db162d6964..61e6100362 100755
--- a/t/db_dependent/Items/AutomaticItemModificationByAge.t
+++ b/t/db_dependent/Items/AutomaticItemModificationByAge.t
@@ -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();
diff --git a/tools/automatic_item_modification_by_age.pl b/tools/automatic_item_modification_by_age.pl
index 3693f318f4..a3138091d3 100755
--- a/tools/automatic_item_modification_by_age.pl
+++ b/tools/automatic_item_modification_by_age.pl
@@ -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,
|