From 4f3827a6bc44870c0495923d1fb0abd893edff32 Mon Sep 17 00:00:00 2001 From: Fridolin Somers Date: Thu, 27 Oct 2022 21:59:26 -1000 Subject: [PATCH] Bug 32029: Automatic item modifications by age add biblio table In automatic item modifications by age missing, conditions can be on columns of tables items or biblioitems. Table biblio is missing. Test plan : 1) Create an automatic item modifications by age with a condition on a column of biblio table 2) Create a record and item matching the rule 3) Run misc/cronjobs/automatic_item_modification_by_age.pl -c -v => Check only matching items are impacted Signed-off-by: Philip Orr Bug 32029: (follow-up) unit test Signed-off-by: matthias le gac Bug 32029: (follow-up) tidy Signed-off-by: Marcel de Rooy Signed-off-by: Katrin Fischer (cherry picked from commit 4e480d48f67c2eb942be4c8516533a327c7e2ba5) Signed-off-by: Fridolin Somers (cherry picked from commit badba61f0091a2c4f04173976d6df154e4542b84) Signed-off-by: Lucas Gass --- C4/Items.pm | 43 ++++++++++--------- .../Items/AutomaticItemModificationByAge.t | 33 ++++++++++++-- tools/automatic_item_modification_by_age.pl | 19 +++++--- 3 files changed, 64 insertions(+), 31 deletions(-) diff --git a/C4/Items.pm b/C4/Items.pm index c640b63baa..d2fa257ef5 100644 --- a/C4/Items.pm +++ b/C4/Items.pm @@ -1560,23 +1560,25 @@ sub PrepareItemrecordDisplay { } sub ToggleNewStatus { - my ( $params ) = @_; - my @rules = @{ $params->{rules} }; + my ($params) = @_; + my @rules = @{ $params->{rules} }; my $report_only = $params->{report_only}; my $dbh = C4::Context->dbh; my @errors; - my @item_columns = map { "items.$_" } Koha::Items->columns; + my @item_columns = map { "items.$_" } Koha::Items->columns; my @biblioitem_columns = map { "biblioitems.$_" } Koha::Biblioitems->columns; + my @biblio_columns = map { "biblio.$_" } Koha::Biblios->columns; my $report; - for my $rule ( @rules ) { + 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 $agefield = $rule->{agefield} ? $rule->{agefield} : 'items.dateaccessioned'; + my $conditions = $rule->{conditions}; my $substitutions = $rule->{substitutions}; - foreach ( @$substitutions ) { + foreach (@$substitutions) { ( $_->{item_field} ) = ( $_->{field} =~ /items\.(.*)/ ); } my @params; @@ -1585,19 +1587,18 @@ sub ToggleNewStatus { SELECT items.* FROM items LEFT JOIN biblioitems ON biblioitems.biblionumber = items.biblionumber + LEFT JOIN biblio ON biblio.biblionumber = biblioitems.biblionumber WHERE 1 |; - for my $condition ( @$conditions ) { + for my $condition (@$conditions) { next unless $condition->{field}; - if ( - grep { $_ eq $condition->{field} } @item_columns - or grep { $_ eq $condition->{field} } @biblioitem_columns - ) { + if ( grep { $_ eq $condition->{field} } @item_columns + or grep { $_ eq $condition->{field} } @biblioitem_columns + or grep { $_ eq $condition->{field} } @biblio_columns ) + { if ( $condition->{value} =~ /\|/ ) { my @values = split /\|/, $condition->{value}; - $query .= qq| AND $condition->{field} IN (| - . join( ',', ('?') x scalar @values ) - . q|)|; + $query .= qq| AND $condition->{field} IN (| . join( ',', ('?') x scalar @values ) . q|)|; push @params, @values; } else { $query .= qq| AND $condition->{field} = ?|; @@ -1610,16 +1611,18 @@ sub ToggleNewStatus { push @params, $age; } my $sth = $dbh->prepare($query); - $sth->execute( @params ); + $sth->execute(@params); while ( my $values = $sth->fetchrow_hashref ) { my $biblionumber = $values->{biblionumber}; - my $itemnumber = $values->{itemnumber}; - my $item = Koha::Items->find($itemnumber); - for my $substitution ( @$substitutions ) { + my $itemnumber = $values->{itemnumber}; + my $item = Koha::Items->find($itemnumber); + for my $substitution (@$substitutions) { my $field = $substitution->{item_field}; my $value = $substitution->{value}; next unless $substitution->{field}; - next if ( defined $values->{ $substitution->{item_field} } and $values->{ $substitution->{item_field} } eq $substitution->{value} ); + next + if ( defined $values->{ $substitution->{item_field} } + and $values->{ $substitution->{item_field} } eq $substitution->{value} ); $item->$field($value); push @{ $report->{$itemnumber} }, $substitution; } diff --git a/t/db_dependent/Items/AutomaticItemModificationByAge.t b/t/db_dependent/Items/AutomaticItemModificationByAge.t index 60507a478a..a1c6f73004 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 => 19; +use Test::More tests => 20; use MARC::Record; use MARC::Field; use DateTime; @@ -46,9 +46,9 @@ $cache->clear_from_cache("MarcSubfieldStructure-$frameworkcode"); my $record = MARC::Record->new(); $record->append_fields( - MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'), - MARC::Field->new('245', ' ', ' ', a => 'Silence in the library'), - MARC::Field->new('942', ' ', ' ', c => 'ITEMTYPE_T'), + MARC::Field->new( '100', ' ', ' ', a => 'Moffat, Steven' ), + MARC::Field->new( '245', ' ', ' ', a => 'Silence in the library', h => 'Book' ), + MARC::Field->new( '942', ' ', ' ', c => 'ITEMTYPE_T' ), ); my ($biblionumber, undef) = C4::Biblio::AddBiblio($record, $frameworkcode); @@ -330,6 +330,31 @@ 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|); +# Condition on biblio column +@rules = ( + { + # does not exist + conditions => [ + { + field => 'biblio.medium', + value => 'Book', + }, + ], + substitutions => [ + { + field => 'items.new_status', + value => 'new_updated_value_biblio', + }, + ], + age => '0', + }, +); + +C4::Items::ToggleNewStatus( { rules => \@rules } ); + +$modified_item = Koha::Items->find( $itemnumber ); +is( $modified_item->new_status, 'new_updated_value_biblio', q|ToggleNewStatus: conditions on biblio|); + # 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 a3138091d3..ed3b84611a 100755 --- a/tools/automatic_item_modification_by_age.pl +++ b/tools/automatic_item_modification_by_age.pl @@ -42,6 +42,7 @@ use C4::Koha; use Koha::Items; use Koha::Biblioitems; +use Koha::Biblios; my $cgi = CGI->new; @@ -113,16 +114,20 @@ if ( $@ ) { exit; } -my @item_fields = map { "items.$_" } Koha::Items->columns; +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'); +my @biblio_fields = map { "biblio.$_" } Koha::Biblios->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 ], + op => $op, + messages => \@messages, + agefields => [@age_fields], + condition_fields => [ @item_fields, @biblioitem_fields, @biblio_fields ], substitution_fields => \@item_fields, - rules => $rules, + rules => $rules, ); output_html_with_http_headers $cgi, $cookie, $template->output; -- 2.39.5