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(CheckReserves);
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" );
96 my $dbh = C4::Context->dbh;
100 "INSERT INTO collections ( colId, colTitle, colDesc )
101 VALUES ( NULL, ?, ? )"
103 $sth->execute( $title, $description ) or return ( 0, 3, $sth->errstr() );
109 =head2 UpdateCollection
111 ( $success, $errorcode, $errormessage ) = UpdateCollection( $colId, $title, $description );
116 $colId: id of the collection to be updated
117 $title: short description of the club or service
118 $description: long description of the club or service
121 $success: 1 if all database operations were successful, 0 otherwise
122 $errorCode: Code for reason of failure, good for translating errors in templates
123 $errorMessage: English description of error
127 sub UpdateCollection {
128 my ( $colId, $title, $description ) = @_;
130 ## Check for all neccessary parameters
132 return ( 0, 1, "NO_ID" );
135 return ( 0, 2, "NO_TITLE" );
138 my $dbh = C4::Context->dbh;
140 $description ||= q{};
143 $sth = $dbh->prepare(
146 colTitle = ?, colDesc = ?
149 $sth->execute( $title, $description, $colId )
150 or return ( 0, 4, $sth->errstr() );
156 =head2 DeleteCollection
158 ( $success, $errorcode, $errormessage ) = DeleteCollection( $colId );
159 Deletes a collection of the given id
162 $colId : id of the Archtype to be deleted
165 $success: 1 if all database operations were successful, 0 otherwise
166 $errorCode: Code for reason of failure, good for translating errors in templates
167 $errorMessage: English description of error
171 sub DeleteCollection {
176 return ( 0, 1, "NO_ID" );
179 my $dbh = C4::Context->dbh;
183 $sth = $dbh->prepare("DELETE FROM collections WHERE colId = ?");
184 $sth->execute($colId) or return ( 0, 4, $sth->errstr() );
189 =head2 GetCollections
191 $collections = GetCollections();
192 Returns data about all collections
196 $results: Reference to an array of associated arrays
198 $errorCode: Code for reason of failure, good for translating errors in templates
199 $errorMessage: English description of error
205 my $dbh = C4::Context->dbh;
207 my $sth = $dbh->prepare("SELECT * FROM collections");
208 $sth->execute() or return ( 1, $sth->errstr() );
211 while ( my $row = $sth->fetchrow_hashref ) {
212 push( @results, $row );
218 =head2 GetItemsInCollection
220 ( $results, $success, $errorcode, $errormessage ) = GetItemsInCollection( $colId );
222 Returns information about the items in the given collection
225 $colId: The id of the collection
228 $results: Reference to an array of associated arrays
229 $success: 1 if all database operations were successful, 0 otherwise
230 $errorCode: Code for reason of failure, good for translating errors in templates
231 $errorMessage: English description of error
235 sub GetItemsInCollection {
240 return ( 0, 0, 1, "NO_ID" );
243 my $dbh = C4::Context->dbh;
245 my $sth = $dbh->prepare(
249 items.itemcallnumber,
251 FROM collections, collections_tracking, items, biblio
252 WHERE collections.colId = collections_tracking.colId
253 AND collections_tracking.itemnumber = items.itemnumber
254 AND items.biblionumber = biblio.biblionumber
255 AND collections.colId = ? ORDER BY biblio.title"
257 $sth->execute($colId) or return ( 0, 0, 2, $sth->errstr() );
260 while ( my $row = $sth->fetchrow_hashref ) {
261 push( @results, $row );
269 ( $colId, $colTitle, $colDesc, $colBranchcode ) = GetCollection( $colId );
271 Returns information about a collection
274 $colId: Id of the collection
276 $colId, $colTitle, $colDesc, $colBranchcode
283 my $dbh = C4::Context->dbh;
285 my ( $sth, @results );
286 $sth = $dbh->prepare("SELECT * FROM collections WHERE colId = ?");
287 $sth->execute($colId) or return 0;
289 my $row = $sth->fetchrow_hashref;
292 $$row{'colId'}, $$row{'colTitle'},
293 $$row{'colDesc'}, $$row{'colBranchcode'}
298 =head2 AddItemToCollection
300 ( $success, $errorcode, $errormessage ) = AddItemToCollection( $colId, $itemnumber );
302 Adds an item to a rotating collection.
305 $colId: Collection to add the item to.
306 $itemnumber: Item to be added to the collection
308 $success: 1 if all database operations were successful, 0 otherwise
309 $errorCode: Code for reason of failure, good for translating errors in templates
310 $errorMessage: English description of error
314 sub AddItemToCollection {
315 my ( $colId, $itemnumber ) = @_;
317 ## Check for all neccessary parameters
319 return ( 0, 1, "NO_ID" );
321 if ( !$itemnumber ) {
322 return ( 0, 2, "NO_ITEM" );
325 if ( isItemInThisCollection( $itemnumber, $colId ) ) {
326 return ( 0, 2, "IN_COLLECTION" );
328 elsif ( isItemInAnyCollection($itemnumber) ) {
329 return ( 0, 3, "IN_COLLECTION_OTHER" );
332 my $dbh = C4::Context->dbh;
335 $sth = $dbh->prepare("
336 INSERT INTO collections_tracking (
341 $sth->execute( $colId, $itemnumber ) or return ( 0, 3, $sth->errstr() );
347 =head2 RemoveItemFromCollection
349 ( $success, $errorcode, $errormessage ) = RemoveItemFromCollection( $colId, $itemnumber );
351 Removes an item to a collection
354 $colId: Collection to add the item to.
355 $itemnumber: Item to be removed from collection
358 $success: 1 if all database operations were successful, 0 otherwise
359 $errorCode: Code for reason of failure, good for translating errors in templates
360 $errorMessage: English description of error
364 sub RemoveItemFromCollection {
365 my ( $colId, $itemnumber ) = @_;
367 ## Check for all neccessary parameters
368 if ( !$itemnumber ) {
369 return ( 0, 2, "NO_ITEM" );
372 if ( !isItemInThisCollection( $itemnumber, $colId ) ) {
373 return ( 0, 2, "NOT_IN_COLLECTION" );
376 my $dbh = C4::Context->dbh;
379 $sth = $dbh->prepare(
380 "DELETE FROM collections_tracking
381 WHERE itemnumber = ?"
383 $sth->execute($itemnumber) or return ( 0, 3, $sth->errstr() );
388 =head2 TransferCollection
390 ( $success, $errorcode, $errormessage ) = TransferCollection( $colId, $colBranchcode );
392 Transfers a collection to another branch
395 $colId: id of the collection to be updated
396 $colBranchcode: branch where collection is moving to
399 $success: 1 if all database operations were successful, 0 otherwise
400 $errorCode: Code for reason of failure, good for translating errors in templates
401 $errorMessage: English description of error
405 sub TransferCollection {
406 my ( $colId, $colBranchcode ) = @_;
408 ## Check for all neccessary parameters
410 return ( 0, 1, "NO_ID" );
412 if ( !$colBranchcode ) {
413 return ( 0, 2, "NO_BRANCHCODE" );
416 my $dbh = C4::Context->dbh;
419 $sth = $dbh->prepare(
425 $sth->execute( $colBranchcode, $colId ) or return ( 0, 4, $sth->errstr() );
427 $sth = $dbh->prepare(q{
428 SELECT items.itemnumber, items.barcode FROM collections_tracking
429 LEFT JOIN items ON collections_tracking.itemnumber = items.itemnumber
430 LEFT JOIN issues ON items.itemnumber = issues.itemnumber
431 WHERE issues.borrowernumber IS NULL
432 AND collections_tracking.colId = ?
434 $sth->execute($colId) or return ( 0, 4, $sth->errstr );
436 while ( my $item = $sth->fetchrow_hashref ) {
437 my ($status) = CheckReserves( $item->{itemnumber} );
438 transferbook( $colBranchcode, $item->{barcode}, my $ignore_reserves = 1 ) unless ( $status eq 'Waiting' );
445 =head2 GetCollectionItemBranches
447 my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches( $itemnumber );
451 sub GetCollectionItemBranches {
452 my ($itemnumber) = @_;
454 if ( !$itemnumber ) {
458 my $dbh = C4::Context->dbh;
460 my ( $sth, @results );
461 $sth = $dbh->prepare(
462 "SELECT holdingbranch, colBranchcode FROM items, collections, collections_tracking
463 WHERE items.itemnumber = collections_tracking.itemnumber
464 AND collections.colId = collections_tracking.colId
465 AND items.itemnumber = ?"
467 $sth->execute($itemnumber);
469 my $row = $sth->fetchrow_hashref;
471 return ( $$row{'holdingbranch'}, $$row{'colBranchcode'}, );
474 =head2 isItemInThisCollection
476 $inCollection = isItemInThisCollection( $itemnumber, $colId );
480 sub isItemInThisCollection {
481 my ( $itemnumber, $colId ) = @_;
483 my $dbh = C4::Context->dbh;
485 my $sth = $dbh->prepare(
486 "SELECT COUNT(*) as inCollection FROM collections_tracking WHERE itemnumber = ? AND colId = ?"
488 $sth->execute( $itemnumber, $colId ) or return (0);
490 my $row = $sth->fetchrow_hashref;
492 return $$row{'inCollection'};
495 =head2 isItemInAnyCollection
497 $inCollection = isItemInAnyCollection( $itemnumber );
501 sub isItemInAnyCollection {
502 my ($itemnumber) = @_;
504 my $dbh = C4::Context->dbh;
506 my $sth = $dbh->prepare(
507 "SELECT itemnumber FROM collections_tracking WHERE itemnumber = ?");
508 $sth->execute($itemnumber) or return (0);
510 my $row = $sth->fetchrow_hashref;
512 $itemnumber = $row->{itemnumber};
527 Kyle Hall <kylemhall@gmail.com>