Koha/tools/automatic_item_modification_by_age.pl
Alex Buckley bed1646444 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>
2022-04-08 15:49:16 +02:00

128 lines
4.1 KiB
Perl
Executable file

#!/usr/bin/perl
# 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
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
=head1 NAME
automatic_item_modification_by_age.pl: Update new status for items.
=cut
=head1 DESCRIPTION
This script allows a user to update the new status for items.
=cut
use Modern::Perl;
use CGI;
use JSON qw( to_json );
use C4::Auth qw( get_template_and_user );
use C4::Context;
use C4::Output qw( output_html_with_http_headers );
use C4::Koha;
use Koha::Items;
use Koha::Biblioitems;
my $cgi = CGI->new;
# open template
my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
{
template_name => "tools/automatic_item_modification_by_age.tt",
query => $cgi,
type => "intranet",
flagsrequired => { tools => 'items_batchmod' },
}
);
my $op = $cgi->param('op') // 'show';
my $syspref_name = q|automatic_item_modification_by_age_configuration|;
if ( $op eq 'update' ) {
my @rules;
my @unique_ids = $cgi->multi_param('unique_id');
for my $unique_id ( @unique_ids ) {
my @substitution_fields = $cgi->multi_param("substitution_field_$unique_id");
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 => [],
};
for my $value ( @substitution_values ) {
my $field = shift @substitution_fields;
last unless $field;
push @{ $rule->{substitutions} }, { field => $field, value => $value };
}
push @{ $rule->{substitutions} }, {}
unless @{ $rule->{substitutions} };
for my $value ( @condition_values ) {
my $field = shift @condition_fields;
last unless $field;
push @{ $rule->{conditions} }, { field => $field, value => $value };
}
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 );
C4::Context->set_preference($syspref_name, $syspref_content);
$op = 'show';
}
my @messages;
my $syspref_content = C4::Context->preference($syspref_name);
my $rules;
$rules = eval { JSON::from_json( $syspref_content ) }
if $syspref_content;
if ( $@ ) {
push @messages, {
type => 'error',
code => 'unable_to_load_configuration'
};
$template->param( messages => \@messages );
output_html_with_http_headers $cgi, $cookie, $template->output;
exit;
}
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,
);
output_html_with_http_headers $cgi, $cookie, $template->output;