From bed1646444a9933186ecdbf6a55d3f4c3ecc4b5f Mon Sep 17 00:00:00 2001 From: Alex Buckley Date: Tue, 11 Jan 2022 15:11:37 +0000 Subject: [PATCH] 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 - 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 - prove t/db_dependent/Items/AutomaticItemModificationByAge.t -v Sponsored-By: Catalyst IT Signed-off-by: Andrew Fuerste-Henry Signed-off-by: Katrin Fischer Signed-off-by: Fridolin Somers --- C4/Items.pm | 5 ++- .../automatic_item_modification_by_age.tt | 33 ++++++++++++++++ .../js/automatic_item_modification_by_age.js | 1 + .../Items/AutomaticItemModificationByAge.t | 39 ++++++++++++++++++- tools/automatic_item_modification_by_age.pl | 8 ++++ 5 files changed, 83 insertions(+), 3 deletions(-) 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 @@
Age in days
+
Age field
+
+ + If not set then items.dateaccessioned will be used +
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, -- 2.39.5