use Modern::Perl; use C4::Context; use Koha::Items; my $start = time; my $dbh = C4::Context->dbh; my $sth = $dbh->prepare(q{ SELECT DISTINCT itemnumber FROM statistics WHERE ( type = "return" OR type = "localuse" ) AND itemtype IS NULL; }); $sth->execute(); my $ilevel_itypes = C4::Context->preference('item-level_itypes'); while ( my ($itemnumber) = $sth->fetchrow_array ) { my $effective_itemtype; my $itype_sth = $dbh->prepare(q{ SELECT biblionumber, itype FROM items WHERE itemnumber = ? UNION SELECT biblionumber, itype FROM deleteditems WHERE itemnumber = ? }); $itype_sth->execute($itemnumber,$itemnumber); my ($biblionumber, $item_itype) = $itype_sth->fetchrow_array; if ($ilevel_itypes and defined $item_itype) { $effective_itemtype = $item_itype; } else { warn "item-level_itypes set but no itype defined for item ($itemnumber)" if $ilevel_itypes; my $btype_sth = $dbh->prepare(q{ SELECT itemtype FROM biblioitems WHERE biblionumber = ? UNION SELECT itemtype FROM deletedbiblioitems WHERE biblionumber = ? }); $btype_sth->execute($biblionumber,$biblionumber); my ($biblio_itype) = $btype_sth->fetchrow_array; $effective_itemtype = $biblio_itype; } my $update_sth = $dbh->prepare(q{ UPDATE statistics SET itemtype=? WHERE itemnumber=? AND itemtype IS NULL }); $update_sth->execute( $effective_itemtype, $itemnumber ); say "Set itemtype $effective_itemtype to item $itemnumber"; }