X-Git-Url: http://koha-dev.rot13.org:8081/gitweb/?a=blobdiff_plain;f=C4%2FRotatingCollections.pm;h=a1f8df740d6f779a11b3d083c81f017e92d023f3;hb=2a1d85fc32a8b0df446bc32e92cbc38c3190ce7e;hp=12d835d3a65d448af981d7e79cb919dd9184cbb4;hpb=c4d3f572794704441ca4f42ac99887e1d0d4fd74;p=koha-ffzg.git diff --git a/C4/RotatingCollections.pm b/C4/RotatingCollections.pm index 12d835d3a6..a1f8df740d 100644 --- a/C4/RotatingCollections.pm +++ b/C4/RotatingCollections.pm @@ -9,33 +9,29 @@ package C4::RotatingCollections; # # This file is part of Koha. # -# 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 2 of the License, or (at your option) any later -# version. +# 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. +# 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, write to the Free Software Foundation, Inc., -# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . use Modern::Perl; use C4::Context; -use C4::Circulation; use C4::Reserves qw(CheckReserves); +use Koha::Database; -use DBI; +use Try::Tiny qw( catch try ); -use Data::Dumper; +use vars qw(@ISA @EXPORT); -use vars qw($VERSION @ISA @EXPORT); - -# set the version for version checking -$VERSION = 3.07.00.049; =head1 NAME @@ -63,12 +59,15 @@ BEGIN { TransferCollection GetCollectionItemBranches + isItemInAnyCollection + isItemInThisCollection ); } =head2 CreateCollection ( $success, $errorcode, $errormessage ) = CreateCollection( $title, $description ); - Creates a new collection + +Creates a new collection Input: $title: short description of the club or service @@ -84,13 +83,17 @@ BEGIN { sub CreateCollection { my ( $title, $description ) = @_; - ## Check for all neccessary parameters + my $schema = Koha::Database->new()->schema(); + my $duplicate_titles = $schema->resultset('Collection')->count({ colTitle => $title }); + + ## Check for all necessary parameters if ( !$title ) { return ( 0, 1, "NO_TITLE" ); + } elsif ( $duplicate_titles ) { + return ( 0, 2, "DUPLICATE_TITLE" ); } - if ( !$description ) { - return ( 0, 2, "NO_DESCRIPTION" ); - } + + $description ||= q{}; my $success = 1; @@ -128,19 +131,24 @@ Updates a collection sub UpdateCollection { my ( $colId, $title, $description ) = @_; - ## Check for all neccessary parameters + my $schema = Koha::Database->new()->schema(); + my $duplicate_titles = $schema->resultset('Collection')->count({ colTitle => $title, -not => { colId => $colId } }); + + ## Check for all necessary parameters if ( !$colId ) { return ( 0, 1, "NO_ID" ); } if ( !$title ) { return ( 0, 2, "NO_TITLE" ); } - if ( !$description ) { - return ( 0, 3, "NO_DESCRIPTION" ); + if ( $duplicate_titles ) { + return ( 0, 3, "DUPLICATE_TITLE" ); } my $dbh = C4::Context->dbh; + $description ||= q{}; + my $sth; $sth = $dbh->prepare( "UPDATE collections @@ -158,10 +166,11 @@ sub UpdateCollection { =head2 DeleteCollection ( $success, $errorcode, $errormessage ) = DeleteCollection( $colId ); - Deletes a collection of the given id + +Deletes a collection of the given id Input: - $colId : id of the Archtype to be deleted + $colId : id of the Archetype to be deleted Output: $success: 1 if all database operations were successful, 0 otherwise @@ -173,7 +182,7 @@ sub UpdateCollection { sub DeleteCollection { my ($colId) = @_; - ## Paramter check + ## Parameter check if ( !$colId ) { return ( 0, 1, "NO_ID" ); } @@ -191,7 +200,8 @@ sub DeleteCollection { =head2 GetCollections $collections = GetCollections(); - Returns data about all collections + +Returns data about all collections Output: On Success: @@ -221,8 +231,8 @@ sub GetCollections { ( $results, $success, $errorcode, $errormessage ) = GetItemsInCollection( $colId ); - Returns information about the items in the given collection - +Returns information about the items in the given collection + Input: $colId: The id of the collection @@ -237,7 +247,7 @@ sub GetCollections { sub GetItemsInCollection { my ($colId) = @_; - ## Paramter check + ## Parameter check if ( !$colId ) { return ( 0, 0, 1, "NO_ID" ); } @@ -247,6 +257,7 @@ sub GetItemsInCollection { my $sth = $dbh->prepare( "SELECT biblio.title, + biblio.biblionumber, items.itemcallnumber, items.barcode FROM collections, collections_tracking, items, biblio @@ -315,7 +326,7 @@ Adds an item to a rotating collection. sub AddItemToCollection { my ( $colId, $itemnumber ) = @_; - ## Check for all neccessary parameters + ## Check for all necessary parameters if ( !$colId ) { return ( 0, 1, "NO_ID" ); } @@ -365,7 +376,7 @@ Removes an item to a collection sub RemoveItemFromCollection { my ( $colId, $itemnumber ) = @_; - ## Check for all neccessary parameters + ## Check for all necessary parameters if ( !$itemnumber ) { return ( 0, 2, "NO_ITEM" ); } @@ -388,7 +399,7 @@ sub RemoveItemFromCollection { =head2 TransferCollection - ( $success, $errorcode, $errormessage ) = TransferCollection( $colId, $colBranchcode ); + ( $success, $messages ) = TransferCollection( $colId, $colBranchcode ); Transfers a collection to another branch @@ -398,20 +409,19 @@ Transfers a collection to another branch Output: $success: 1 if all database operations were successful, 0 otherwise - $errorCode: Code for reason of failure, good for translating errors in templates - $errorMessage: English description of error + $messages: Arrayref of messages for user feedback =cut sub TransferCollection { my ( $colId, $colBranchcode ) = @_; - ## Check for all neccessary parameters + ## Check for all necessary parameters if ( !$colId ) { - return ( 0, 1, "NO_ID" ); + return ( 0, [{ type => 'error', code => 'NO_ID' }] ); } if ( !$colBranchcode ) { - return ( 0, 2, "NO_BRANCHCODE" ); + return ( 0, [{ type => 'error', code => 'NO_BRANCHCODE' }] ); } my $dbh = C4::Context->dbh; @@ -423,7 +433,8 @@ sub TransferCollection { colBranchcode = ? WHERE colId = ?" ); - $sth->execute( $colBranchcode, $colId ) or return ( 0, 4, $sth->errstr() ); + $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 @@ -432,15 +443,70 @@ sub TransferCollection { WHERE issues.borrowernumber IS NULL AND collections_tracking.colId = ? }); - $sth->execute($colId) or return ( 0, 4, $sth->errstr ); - my @results; + $sth->execute($colId) or return 0; + my $messages; while ( my $item = $sth->fetchrow_hashref ) { - my ($status) = CheckReserves( $item->{itemnumber} ); - transferbook( $colBranchcode, $item->{barcode}, my $ignore_reserves = 1 ) unless ( $status eq 'Waiting' ); + 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; - + return (1, $messages); } =head2 GetCollectionItemBranches @@ -495,7 +561,7 @@ sub isItemInThisCollection { =head2 isItemInAnyCollection -$inCollection = isItemInAnyCollection( $itemnumber ); + my $inCollection = isItemInAnyCollection( $itemnumber ); =cut @@ -505,7 +571,7 @@ sub isItemInAnyCollection { my $dbh = C4::Context->dbh; my $sth = $dbh->prepare( - "SELECT itemnumber FROM collections_tracking WHERE itemnumber = ?"); + "SELECT itemnumber FROM collections_tracking JOIN collections USING (colId) WHERE itemnumber = ?"); $sth->execute($itemnumber) or return (0); my $row = $sth->fetchrow_hashref;