- WHERE colId = ?");
- $sth->execute( $colBranchcode, $colId ) or return ( 0, 4, $sth->errstr() );
- $sth->finish;
-
- $sth = $dbh->prepare("SELECT barcode FROM items, collections_tracking
- WHERE items.itemnumber = collections_tracking.itemnumber
- AND collections_tracking.colId = ?");
- $sth->execute( $colId ) or return ( 0, 4, $sth->errstr );
- my @results;
- while ( my $item = $sth->fetchrow_hashref ) {
- my ( $dotransfer, $messages, $iteminformation ) = transferbook( $colBranchcode, $item->{'barcode'}, my $ignore_reserves = 1);
- }
-
-
-
- return 1;
-
+ WHERE colId = ?"
+ );
+ $sth->execute( $colBranchcode, $colId ) or return 0;
+ my $to_library = Koha::Libraries->find( $colBranchcode );
+
+ $sth = $dbh->prepare(q{
+ SELECT items.itemnumber, items.barcode FROM collections_tracking
+ LEFT JOIN items ON collections_tracking.itemnumber = items.itemnumber
+ LEFT JOIN issues ON items.itemnumber = issues.itemnumber
+ WHERE issues.borrowernumber IS NULL
+ AND collections_tracking.colId = ?
+ });
+ $sth->execute($colId) or return 0;
+ my $messages;
+ while ( my $item = $sth->fetchrow_hashref ) {
+ my $item_object = Koha::Items->find( $item->{itemnumber} );
+ try {
+ $item_object->request_transfer(
+ {
+ to => $to_library,
+ reason => 'RotatingCollection',
+ ignore_limits => 0
+ }
+ ); # Request transfer
+ }
+ catch {
+ if ( $_->isa('Koha::Exceptions::Item::Transfer::InQueue') ) {
+ my $exception = $_;
+ my $found_transfer = $_->transfer;
+ if ( $found_transfer->in_transit
+ || $found_transfer->reason eq 'Reserve' )
+ {
+ my $transfer = $item_object->request_transfer(
+ {
+ to => $to_library,
+ reason => "RotatingCollection",
+ ignore_limits => 0,
+ enqueue => 1
+ }
+ ); # Queue transfer
+ push @{$messages},
+ {
+ type => 'alert',
+ code => 'enqueued',
+ item => $item_object,
+ found_transfer => $found_transfer
+ };
+ }
+ else {
+ my $transfer = $item_object->request_transfer(
+ {
+ to => $to_library,
+ reason => "RotatingCollection",
+ ignore_limits => 0,
+ replace => 1
+ }
+ ); # Replace transfer
+ # NOTE: If we just replaced a StockRotationAdvance,
+ # it will get enqueued afresh on the next cron run
+ }
+ }
+ elsif ( $_->isa('Koha::Exceptions::Item::Transfer::Limit') ) {
+ push @{$messages},
+ {
+ type => 'error',
+ code => 'limits',
+ item => $item_object
+ };
+ }
+ else {
+ $_->rethrow();
+ }
+ };
+ }
+
+ return (1, $messages);