1 package C4::RotatingCollections;
3 # $Id: RotatingCollections.pm,v 0.1 2007/04/20 kylemhall
5 # This package is inteded to keep track of what library
6 # Items of a certain collection should be at.
8 # Copyright 2007 Kyle Hall
10 # This file is part of Koha.
12 # Koha is free software; you can redistribute it and/or modify it
13 # under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 3 of the License, or
15 # (at your option) any later version.
17 # Koha is distributed in the hope that it will be useful, but
18 # WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
22 # You should have received a copy of the GNU General Public License
23 # along with Koha; if not, see <http://www.gnu.org/licenses>.
29 use C4::Reserves qw(CheckReserves);
36 use vars qw(@ISA @EXPORT);
41 C4::RotatingCollections - Functions for managing rotating collections
49 @ISA = qw( Exporter );
61 RemoveItemFromCollection
64 GetCollectionItemBranches
68 =head2 CreateCollection
69 ( $success, $errorcode, $errormessage ) = CreateCollection( $title, $description );
70 Creates a new collection
73 $title: short description of the club or service
74 $description: long description of the club or service
77 $success: 1 if all database operations were successful, 0 otherwise
78 $errorCode: Code for reason of failure, good for translating errors in templates
79 $errorMessage: English description of error
83 sub CreateCollection {
84 my ( $title, $description ) = @_;
86 my $schema = Koha::Database->new()->schema();
87 my $duplicate_titles = $schema->resultset('Collection')->count({ colTitle => $title });
89 ## Check for all necessary parameters
91 return ( 0, 1, "NO_TITLE" );
92 } elsif ( $duplicate_titles ) {
93 return ( 0, 2, "DUPLICATE_TITLE" );
100 my $dbh = C4::Context->dbh;
103 $sth = $dbh->prepare(
104 "INSERT INTO collections ( colId, colTitle, colDesc )
105 VALUES ( NULL, ?, ? )"
107 $sth->execute( $title, $description ) or return ( 0, 3, $sth->errstr() );
113 =head2 UpdateCollection
115 ( $success, $errorcode, $errormessage ) = UpdateCollection( $colId, $title, $description );
120 $colId: id of the collection to be updated
121 $title: short description of the club or service
122 $description: long description of the club or service
125 $success: 1 if all database operations were successful, 0 otherwise
126 $errorCode: Code for reason of failure, good for translating errors in templates
127 $errorMessage: English description of error
131 sub UpdateCollection {
132 my ( $colId, $title, $description ) = @_;
134 my $schema = Koha::Database->new()->schema();
135 my $duplicate_titles = $schema->resultset('Collection')->count({ colTitle => $title, -not => { colId => $colId } });
137 ## Check for all necessary parameters
139 return ( 0, 1, "NO_ID" );
142 return ( 0, 2, "NO_TITLE" );
144 if ( $duplicate_titles ) {
145 return ( 0, 3, "DUPLICATE_TITLE" );
148 my $dbh = C4::Context->dbh;
150 $description ||= q{};
153 $sth = $dbh->prepare(
156 colTitle = ?, colDesc = ?
159 $sth->execute( $title, $description, $colId )
160 or return ( 0, 4, $sth->errstr() );
166 =head2 DeleteCollection
168 ( $success, $errorcode, $errormessage ) = DeleteCollection( $colId );
169 Deletes a collection of the given id
172 $colId : id of the Archetype to be deleted
175 $success: 1 if all database operations were successful, 0 otherwise
176 $errorCode: Code for reason of failure, good for translating errors in templates
177 $errorMessage: English description of error
181 sub DeleteCollection {
186 return ( 0, 1, "NO_ID" );
189 my $dbh = C4::Context->dbh;
193 $sth = $dbh->prepare("DELETE FROM collections WHERE colId = ?");
194 $sth->execute($colId) or return ( 0, 4, $sth->errstr() );
199 =head2 GetCollections
201 $collections = GetCollections();
202 Returns data about all collections
206 $results: Reference to an array of associated arrays
208 $errorCode: Code for reason of failure, good for translating errors in templates
209 $errorMessage: English description of error
215 my $dbh = C4::Context->dbh;
217 my $sth = $dbh->prepare("SELECT * FROM collections");
218 $sth->execute() or return ( 1, $sth->errstr() );
221 while ( my $row = $sth->fetchrow_hashref ) {
222 push( @results, $row );
228 =head2 GetItemsInCollection
230 ( $results, $success, $errorcode, $errormessage ) = GetItemsInCollection( $colId );
232 Returns information about the items in the given collection
235 $colId: The id of the collection
238 $results: Reference to an array of associated arrays
239 $success: 1 if all database operations were successful, 0 otherwise
240 $errorCode: Code for reason of failure, good for translating errors in templates
241 $errorMessage: English description of error
245 sub GetItemsInCollection {
250 return ( 0, 0, 1, "NO_ID" );
253 my $dbh = C4::Context->dbh;
255 my $sth = $dbh->prepare(
259 items.itemcallnumber,
261 FROM collections, collections_tracking, items, biblio
262 WHERE collections.colId = collections_tracking.colId
263 AND collections_tracking.itemnumber = items.itemnumber
264 AND items.biblionumber = biblio.biblionumber
265 AND collections.colId = ? ORDER BY biblio.title"
267 $sth->execute($colId) or return ( 0, 0, 2, $sth->errstr() );
270 while ( my $row = $sth->fetchrow_hashref ) {
271 push( @results, $row );
279 ( $colId, $colTitle, $colDesc, $colBranchcode ) = GetCollection( $colId );
281 Returns information about a collection
284 $colId: Id of the collection
286 $colId, $colTitle, $colDesc, $colBranchcode
293 my $dbh = C4::Context->dbh;
295 my ( $sth, @results );
296 $sth = $dbh->prepare("SELECT * FROM collections WHERE colId = ?");
297 $sth->execute($colId) or return 0;
299 my $row = $sth->fetchrow_hashref;
302 $$row{'colId'}, $$row{'colTitle'},
303 $$row{'colDesc'}, $$row{'colBranchcode'}
308 =head2 AddItemToCollection
310 ( $success, $errorcode, $errormessage ) = AddItemToCollection( $colId, $itemnumber );
312 Adds an item to a rotating collection.
315 $colId: Collection to add the item to.
316 $itemnumber: Item to be added to the collection
318 $success: 1 if all database operations were successful, 0 otherwise
319 $errorCode: Code for reason of failure, good for translating errors in templates
320 $errorMessage: English description of error
324 sub AddItemToCollection {
325 my ( $colId, $itemnumber ) = @_;
327 ## Check for all necessary parameters
329 return ( 0, 1, "NO_ID" );
331 if ( !$itemnumber ) {
332 return ( 0, 2, "NO_ITEM" );
335 if ( isItemInThisCollection( $itemnumber, $colId ) ) {
336 return ( 0, 2, "IN_COLLECTION" );
338 elsif ( isItemInAnyCollection($itemnumber) ) {
339 return ( 0, 3, "IN_COLLECTION_OTHER" );
342 my $dbh = C4::Context->dbh;
345 $sth = $dbh->prepare("
346 INSERT INTO collections_tracking (
351 $sth->execute( $colId, $itemnumber ) or return ( 0, 3, $sth->errstr() );
357 =head2 RemoveItemFromCollection
359 ( $success, $errorcode, $errormessage ) = RemoveItemFromCollection( $colId, $itemnumber );
361 Removes an item to a collection
364 $colId: Collection to add the item to.
365 $itemnumber: Item to be removed from collection
368 $success: 1 if all database operations were successful, 0 otherwise
369 $errorCode: Code for reason of failure, good for translating errors in templates
370 $errorMessage: English description of error
374 sub RemoveItemFromCollection {
375 my ( $colId, $itemnumber ) = @_;
377 ## Check for all necessary parameters
378 if ( !$itemnumber ) {
379 return ( 0, 2, "NO_ITEM" );
382 if ( !isItemInThisCollection( $itemnumber, $colId ) ) {
383 return ( 0, 2, "NOT_IN_COLLECTION" );
386 my $dbh = C4::Context->dbh;
389 $sth = $dbh->prepare(
390 "DELETE FROM collections_tracking
391 WHERE itemnumber = ?"
393 $sth->execute($itemnumber) or return ( 0, 3, $sth->errstr() );
398 =head2 TransferCollection
400 ( $success, $errorcode, $errormessage ) = TransferCollection( $colId, $colBranchcode );
402 Transfers a collection to another branch
405 $colId: id of the collection to be updated
406 $colBranchcode: branch where collection is moving to
409 $success: 1 if all database operations were successful, 0 otherwise
410 $errorCode: Code for reason of failure, good for translating errors in templates
411 $errorMessage: English description of error
415 sub TransferCollection {
416 my ( $colId, $colBranchcode ) = @_;
418 ## Check for all necessary parameters
420 return ( 0, 1, "NO_ID" );
422 if ( !$colBranchcode ) {
423 return ( 0, 2, "NO_BRANCHCODE" );
426 my $dbh = C4::Context->dbh;
429 $sth = $dbh->prepare(
435 $sth->execute( $colBranchcode, $colId ) or return ( 0, 4, $sth->errstr() );
437 $sth = $dbh->prepare(q{
438 SELECT items.itemnumber, items.barcode FROM collections_tracking
439 LEFT JOIN items ON collections_tracking.itemnumber = items.itemnumber
440 LEFT JOIN issues ON items.itemnumber = issues.itemnumber
441 WHERE issues.borrowernumber IS NULL
442 AND collections_tracking.colId = ?
444 $sth->execute($colId) or return ( 0, 4, $sth->errstr );
446 while ( my $item = $sth->fetchrow_hashref ) {
447 my ($status) = CheckReserves( $item->{itemnumber} );
448 my @transfers = C4::Circulation::GetTransfers( $item->{itemnumber} );
449 C4::Circulation::transferbook( $colBranchcode, $item->{barcode}, 1, 'RotatingCollection' ) unless ( $status eq 'Waiting' || @transfers );
455 =head2 GetCollectionItemBranches
457 my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches( $itemnumber );
461 sub GetCollectionItemBranches {
462 my ($itemnumber) = @_;
464 if ( !$itemnumber ) {
468 my $dbh = C4::Context->dbh;
470 my ( $sth, @results );
471 $sth = $dbh->prepare(
472 "SELECT holdingbranch, colBranchcode FROM items, collections, collections_tracking
473 WHERE items.itemnumber = collections_tracking.itemnumber
474 AND collections.colId = collections_tracking.colId
475 AND items.itemnumber = ?"
477 $sth->execute($itemnumber);
479 my $row = $sth->fetchrow_hashref;
481 return ( $$row{'holdingbranch'}, $$row{'colBranchcode'}, );
484 =head2 isItemInThisCollection
486 $inCollection = isItemInThisCollection( $itemnumber, $colId );
490 sub isItemInThisCollection {
491 my ( $itemnumber, $colId ) = @_;
493 my $dbh = C4::Context->dbh;
495 my $sth = $dbh->prepare(
496 "SELECT COUNT(*) as inCollection FROM collections_tracking WHERE itemnumber = ? AND colId = ?"
498 $sth->execute( $itemnumber, $colId ) or return (0);
500 my $row = $sth->fetchrow_hashref;
502 return $$row{'inCollection'};
505 =head2 isItemInAnyCollection
507 $inCollection = isItemInAnyCollection( $itemnumber );
511 sub isItemInAnyCollection {
512 my ($itemnumber) = @_;
514 my $dbh = C4::Context->dbh;
516 my $sth = $dbh->prepare(
517 "SELECT itemnumber FROM collections_tracking WHERE itemnumber = ?");
518 $sth->execute($itemnumber) or return (0);
520 my $row = $sth->fetchrow_hashref;
522 $itemnumber = $row->{itemnumber};
537 Kyle Hall <kylemhall@gmail.com>