@@ -, +, @@ dateaccessioned - 'Age in days' = 20 - Leave 'Age field' = 'Choose an age field' - 'Substitutions': 'items.barcode' = 'test' - Save the rule - 'Age in days' = 2 - 'Age field' = 'items.datelastseen' - 'Substitutions': 'items.barcode' = 'test2' - Save the rule - Make the items.dateaccessioned = 3 day ago (so rule 1 is false) - Make the items.datelastseen = 3 days ago (so rule 2 is true) - sudo koha-shell - cd misc/cronjobs - ./automatic_item_modification_by_age.pl -v -c - sudo koha-shell - prove t/db_dependent/Items/AutomaticItemModificationByAge.t -v --- 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(-) --- a/C4/Items.pm +++ a/C4/Items.pm @@ -1820,6 +1820,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 ) { @@ -1851,7 +1854,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); --- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/automatic_item_modification_by_age.tt +++ a/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 %] --- a/koha-tmpl/intranet-tmpl/prog/js/automatic_item_modification_by_age.js +++ a/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); --- a/t/db_dependent/Items/AutomaticItemModificationByAge.t +++ a/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(); --- a/tools/automatic_item_modification_by_age.pl +++ a/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, --