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 <philip.orr@lmscloud.de>

Bug 32029: (follow-up) unit test

Signed-off-by: matthias le gac <matthias.le-gac@inlibro.com>

Bug 32029: (follow-up) tidy

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Katrin Fischer <katrin.fischer@bsz-bw.de>
(cherry picked from commit 4e480d48f6)
Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
(cherry picked from commit badba61f00)
Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
This commit is contained in:
Fridolin Somers 2022-10-27 21:59:26 -10:00 committed by Lucas Gass
parent 0b89414d0c
commit 4f3827a6bc
3 changed files with 64 additions and 31 deletions

View file

@ -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;
}

View file

@ -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();

View file

@ -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;