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 under the
13 # terms of the GNU General Public License as published by the Free Software
14 # Foundation; either version 2 of the License, or (at your option) any later
17 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
18 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
21 # You should have received a copy of the GNU General Public License along
22 # with Koha; if not, write to the Free Software Foundation, Inc.,
23 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 use C4::Reserves qw(GetReserveStatus);
35 use vars qw($VERSION @ISA @EXPORT);
37 # set the version for version checking
38 $VERSION = 3.07.00.049;
42 C4::RotatingCollections - Functions for managing rotating collections
50 @ISA = qw( Exporter );
62 RemoveItemFromCollection
65 GetCollectionItemBranches
69 =head2 CreateCollection
70 ( $success, $errorcode, $errormessage ) = CreateCollection( $title, $description );
71 Creates a new collection
74 $title: short description of the club or service
75 $description: long description of the club or service
78 $success: 1 if all database operations were successful, 0 otherwise
79 $errorCode: Code for reason of failure, good for translating errors in templates
80 $errorMessage: English description of error
84 sub CreateCollection {
85 my ( $title, $description ) = @_;
87 ## Check for all neccessary parameters
89 return ( 0, 1, "No Title Given" );
91 if ( !$description ) {
92 return ( 0, 2, "No Description Given" );
97 my $dbh = C4::Context->dbh;
100 $sth = $dbh->prepare(
101 "INSERT INTO collections ( colId, colTitle, colDesc )
102 VALUES ( NULL, ?, ? )"
104 $sth->execute( $title, $description ) or return ( 0, 3, $sth->errstr() );
110 =head2 UpdateCollection
112 ( $success, $errorcode, $errormessage ) = UpdateCollection( $colId, $title, $description );
117 $colId: id of the collection to be updated
118 $title: short description of the club or service
119 $description: long description of the club or service
122 $success: 1 if all database operations were successful, 0 otherwise
123 $errorCode: Code for reason of failure, good for translating errors in templates
124 $errorMessage: English description of error
128 sub UpdateCollection {
129 my ( $colId, $title, $description ) = @_;
131 ## Check for all neccessary parameters
133 return ( 0, 1, "No Id Given" );
136 return ( 0, 2, "No Title Given" );
138 if ( !$description ) {
139 return ( 0, 3, "No Description Given" );
142 my $dbh = C4::Context->dbh;
145 $sth = $dbh->prepare(
148 colTitle = ?, colDesc = ?
151 $sth->execute( $title, $description, $colId )
152 or return ( 0, 4, $sth->errstr() );
158 =head2 DeleteCollection
160 ( $success, $errorcode, $errormessage ) = DeleteCollection( $colId );
161 Deletes a collection of the given id
164 $colId : id of the Archtype to be deleted
167 $success: 1 if all database operations were successful, 0 otherwise
168 $errorCode: Code for reason of failure, good for translating errors in templates
169 $errorMessage: English description of error
173 sub DeleteCollection {
178 return ( 0, 1, "No Collection Id Given" );
181 my $dbh = C4::Context->dbh;
185 $sth = $dbh->prepare("DELETE FROM collections WHERE colId = ?");
186 $sth->execute($colId) or return ( 0, 4, $sth->errstr() );
191 =head2 GetCollections
193 $collections = GetCollections();
194 Returns data about all collections
198 $results: Reference to an array of associated arrays
200 $errorCode: Code for reason of failure, good for translating errors in templates
201 $errorMessage: English description of error
207 my $dbh = C4::Context->dbh;
209 my $sth = $dbh->prepare("SELECT * FROM collections");
210 $sth->execute() or return ( 1, $sth->errstr() );
213 while ( my $row = $sth->fetchrow_hashref ) {
214 push( @results, $row );
220 =head2 GetItemsInCollection
222 ( $results, $success, $errorcode, $errormessage ) = GetItemsInCollection( $colId );
224 Returns information about the items in the given collection
227 $colId: The id of the collection
230 $results: Reference to an array of associated arrays
231 $success: 1 if all database operations were successful, 0 otherwise
232 $errorCode: Code for reason of failure, good for translating errors in templates
233 $errorMessage: English description of error
237 sub GetItemsInCollection {
242 return ( 0, 0, 1, "No Collection Id Given" );
245 my $dbh = C4::Context->dbh;
247 my $sth = $dbh->prepare(
250 items.itemcallnumber,
252 FROM collections, collections_tracking, items, biblio
253 WHERE collections.colId = collections_tracking.colId
254 AND collections_tracking.itemnumber = items.itemnumber
255 AND items.biblionumber = biblio.biblionumber
256 AND collections.colId = ? ORDER BY biblio.title"
258 $sth->execute($colId) or return ( 0, 0, 2, $sth->errstr() );
261 while ( my $row = $sth->fetchrow_hashref ) {
262 push( @results, $row );
270 ( $colId, $colTitle, $colDesc, $colBranchcode ) = GetCollection( $colId );
272 Returns information about a collection
275 $colId: Id of the collection
277 $colId, $colTitle, $colDesc, $colBranchcode
284 my $dbh = C4::Context->dbh;
286 my ( $sth, @results );
287 $sth = $dbh->prepare("SELECT * FROM collections WHERE colId = ?");
288 $sth->execute($colId) or return 0;
290 my $row = $sth->fetchrow_hashref;
293 $$row{'colId'}, $$row{'colTitle'},
294 $$row{'colDesc'}, $$row{'colBranchcode'}
299 =head2 AddItemToCollection
301 ( $success, $errorcode, $errormessage ) = AddItemToCollection( $colId, $itemnumber );
303 Adds an item to a rotating collection.
306 $colId: Collection to add the item to.
307 $itemnumber: Item to be added to the collection
309 $success: 1 if all database operations were successful, 0 otherwise
310 $errorCode: Code for reason of failure, good for translating errors in templates
311 $errorMessage: English description of error
315 sub AddItemToCollection {
316 my ( $colId, $itemnumber ) = @_;
318 ## Check for all neccessary parameters
320 return ( 0, 1, "No Collection Given" );
322 if ( !$itemnumber ) {
323 return ( 0, 2, "No Itemnumber Given" );
326 if ( isItemInThisCollection( $itemnumber, $colId ) ) {
327 return ( 0, 2, "Item is already in the collection!" );
329 elsif ( isItemInAnyCollection($itemnumber) ) {
330 return ( 0, 3, "Item is already in a different collection!" );
333 my $dbh = C4::Context->dbh;
336 $sth = $dbh->prepare("
337 INSERT INTO collections_tracking (
342 $sth->execute( $colId, $itemnumber ) or return ( 0, 3, $sth->errstr() );
348 =head2 RemoveItemFromCollection
350 ( $success, $errorcode, $errormessage ) = RemoveItemFromCollection( $colId, $itemnumber );
352 Removes an item to a collection
355 $colId: Collection to add the item to.
356 $itemnumber: Item to be removed from collection
359 $success: 1 if all database operations were successful, 0 otherwise
360 $errorCode: Code for reason of failure, good for translating errors in templates
361 $errorMessage: English description of error
365 sub RemoveItemFromCollection {
366 my ( $colId, $itemnumber ) = @_;
368 ## Check for all neccessary parameters
369 if ( !$itemnumber ) {
370 return ( 0, 2, "No Itemnumber Given" );
373 if ( !isItemInThisCollection( $itemnumber, $colId ) ) {
374 return ( 0, 2, "Item is not in the collection!" );
377 my $dbh = C4::Context->dbh;
380 $sth = $dbh->prepare(
381 "DELETE FROM collections_tracking
382 WHERE itemnumber = ?"
384 $sth->execute($itemnumber) or return ( 0, 3, $sth->errstr() );
389 =head2 TransferCollection
391 ( $success, $errorcode, $errormessage ) = TransferCollection( $colId, $colBranchcode );
393 Transfers a collection to another branch
396 $colId: id of the collection to be updated
397 $colBranchcode: branch where collection is moving to
400 $success: 1 if all database operations were successful, 0 otherwise
401 $errorCode: Code for reason of failure, good for translating errors in templates
402 $errorMessage: English description of error
406 sub TransferCollection {
407 my ( $colId, $colBranchcode ) = @_;
409 ## Check for all neccessary parameters
411 return ( 0, 1, "No Id Given" );
413 if ( !$colBranchcode ) {
414 return ( 0, 2, "No Branchcode Given" );
417 my $dbh = C4::Context->dbh;
420 $sth = $dbh->prepare(
426 $sth->execute( $colBranchcode, $colId ) or return ( 0, 4, $sth->errstr() );
428 $sth = $dbh->prepare(q{
429 SELECT items.itemnumber, items.barcode FROM collections_tracking
430 LEFT JOIN items ON collections_tracking.itemnumber = items.itemnumber
431 LEFT JOIN issues ON items.itemnumber = issues.itemnumber
432 WHERE issues.borrowernumber IS NULL
433 AND collections_tracking.colId = ?
435 $sth->execute($colId) or return ( 0, 4, $sth->errstr );
437 while ( my $item = $sth->fetchrow_hashref ) {
438 transferbook( $colBranchcode, $item->{barcode},
439 my $ignore_reserves = 1 )
440 unless ( GetReserveStatus( $item->{itemnumber} ) eq "Waiting" );
447 =head2 GetCollectionItemBranches
449 my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches( $itemnumber );
453 sub GetCollectionItemBranches {
454 my ($itemnumber) = @_;
456 if ( !$itemnumber ) {
460 my $dbh = C4::Context->dbh;
462 my ( $sth, @results );
463 $sth = $dbh->prepare(
464 "SELECT holdingbranch, colBranchcode FROM items, collections, collections_tracking
465 WHERE items.itemnumber = collections_tracking.itemnumber
466 AND collections.colId = collections_tracking.colId
467 AND items.itemnumber = ?"
469 $sth->execute($itemnumber);
471 my $row = $sth->fetchrow_hashref;
473 return ( $$row{'holdingbranch'}, $$row{'colBranchcode'}, );
476 =head2 isItemInThisCollection
478 $inCollection = isItemInThisCollection( $itemnumber, $colId );
482 sub isItemInThisCollection {
483 my ( $itemnumber, $colId ) = @_;
485 my $dbh = C4::Context->dbh;
487 my $sth = $dbh->prepare(
488 "SELECT COUNT(*) as inCollection FROM collections_tracking WHERE itemnumber = ? AND colId = ?"
490 $sth->execute( $itemnumber, $colId ) or return (0);
492 my $row = $sth->fetchrow_hashref;
494 return $$row{'inCollection'};
497 =head2 isItemInAnyCollection
499 $inCollection = isItemInAnyCollection( $itemnumber );
503 sub isItemInAnyCollection {
504 my ($itemnumber) = @_;
506 my $dbh = C4::Context->dbh;
508 my $sth = $dbh->prepare(
509 "SELECT itemnumber FROM collections_tracking WHERE itemnumber = ?");
510 $sth->execute($itemnumber) or return (0);
512 my $row = $sth->fetchrow_hashref;
514 $itemnumber = $row->{itemnumber};
529 Kyle Hall <kylemhall@gmail.com>