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);
38 # set the version for version checking
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 my $schema = Koha::Database->new()->schema();
88 my $duplicate_titles = $schema->resultset('Collection')->count({ colTitle => $title });
90 ## Check for all necessary parameters
92 return ( 0, 1, "NO_TITLE" );
93 } elsif ( $duplicate_titles ) {
94 return ( 0, 2, "DUPLICATE_TITLE" );
101 my $dbh = C4::Context->dbh;
104 $sth = $dbh->prepare(
105 "INSERT INTO collections ( colId, colTitle, colDesc )
106 VALUES ( NULL, ?, ? )"
108 $sth->execute( $title, $description ) or return ( 0, 3, $sth->errstr() );
114 =head2 UpdateCollection
116 ( $success, $errorcode, $errormessage ) = UpdateCollection( $colId, $title, $description );
121 $colId: id of the collection to be updated
122 $title: short description of the club or service
123 $description: long description of the club or service
126 $success: 1 if all database operations were successful, 0 otherwise
127 $errorCode: Code for reason of failure, good for translating errors in templates
128 $errorMessage: English description of error
132 sub UpdateCollection {
133 my ( $colId, $title, $description ) = @_;
135 my $schema = Koha::Database->new()->schema();
136 my $duplicate_titles = $schema->resultset('Collection')->count({ colTitle => $title, -not => { colId => $colId } });
138 ## Check for all necessary parameters
140 return ( 0, 1, "NO_ID" );
143 return ( 0, 2, "NO_TITLE" );
145 if ( $duplicate_titles ) {
146 return ( 0, 3, "DUPLICATE_TITLE" );
149 my $dbh = C4::Context->dbh;
151 $description ||= q{};
154 $sth = $dbh->prepare(
157 colTitle = ?, colDesc = ?
160 $sth->execute( $title, $description, $colId )
161 or return ( 0, 4, $sth->errstr() );
167 =head2 DeleteCollection
169 ( $success, $errorcode, $errormessage ) = DeleteCollection( $colId );
170 Deletes a collection of the given id
173 $colId : id of the Archetype to be deleted
176 $success: 1 if all database operations were successful, 0 otherwise
177 $errorCode: Code for reason of failure, good for translating errors in templates
178 $errorMessage: English description of error
182 sub DeleteCollection {
187 return ( 0, 1, "NO_ID" );
190 my $dbh = C4::Context->dbh;
194 $sth = $dbh->prepare("DELETE FROM collections WHERE colId = ?");
195 $sth->execute($colId) or return ( 0, 4, $sth->errstr() );
200 =head2 GetCollections
202 $collections = GetCollections();
203 Returns data about all collections
207 $results: Reference to an array of associated arrays
209 $errorCode: Code for reason of failure, good for translating errors in templates
210 $errorMessage: English description of error
216 my $dbh = C4::Context->dbh;
218 my $sth = $dbh->prepare("SELECT * FROM collections");
219 $sth->execute() or return ( 1, $sth->errstr() );
222 while ( my $row = $sth->fetchrow_hashref ) {
223 push( @results, $row );
229 =head2 GetItemsInCollection
231 ( $results, $success, $errorcode, $errormessage ) = GetItemsInCollection( $colId );
233 Returns information about the items in the given collection
236 $colId: The id of the collection
239 $results: Reference to an array of associated arrays
240 $success: 1 if all database operations were successful, 0 otherwise
241 $errorCode: Code for reason of failure, good for translating errors in templates
242 $errorMessage: English description of error
246 sub GetItemsInCollection {
251 return ( 0, 0, 1, "NO_ID" );
254 my $dbh = C4::Context->dbh;
256 my $sth = $dbh->prepare(
260 items.itemcallnumber,
262 FROM collections, collections_tracking, items, biblio
263 WHERE collections.colId = collections_tracking.colId
264 AND collections_tracking.itemnumber = items.itemnumber
265 AND items.biblionumber = biblio.biblionumber
266 AND collections.colId = ? ORDER BY biblio.title"
268 $sth->execute($colId) or return ( 0, 0, 2, $sth->errstr() );
271 while ( my $row = $sth->fetchrow_hashref ) {
272 push( @results, $row );
280 ( $colId, $colTitle, $colDesc, $colBranchcode ) = GetCollection( $colId );
282 Returns information about a collection
285 $colId: Id of the collection
287 $colId, $colTitle, $colDesc, $colBranchcode
294 my $dbh = C4::Context->dbh;
296 my ( $sth, @results );
297 $sth = $dbh->prepare("SELECT * FROM collections WHERE colId = ?");
298 $sth->execute($colId) or return 0;
300 my $row = $sth->fetchrow_hashref;
303 $$row{'colId'}, $$row{'colTitle'},
304 $$row{'colDesc'}, $$row{'colBranchcode'}
309 =head2 AddItemToCollection
311 ( $success, $errorcode, $errormessage ) = AddItemToCollection( $colId, $itemnumber );
313 Adds an item to a rotating collection.
316 $colId: Collection to add the item to.
317 $itemnumber: Item to be added to the collection
319 $success: 1 if all database operations were successful, 0 otherwise
320 $errorCode: Code for reason of failure, good for translating errors in templates
321 $errorMessage: English description of error
325 sub AddItemToCollection {
326 my ( $colId, $itemnumber ) = @_;
328 ## Check for all necessary parameters
330 return ( 0, 1, "NO_ID" );
332 if ( !$itemnumber ) {
333 return ( 0, 2, "NO_ITEM" );
336 if ( isItemInThisCollection( $itemnumber, $colId ) ) {
337 return ( 0, 2, "IN_COLLECTION" );
339 elsif ( isItemInAnyCollection($itemnumber) ) {
340 return ( 0, 3, "IN_COLLECTION_OTHER" );
343 my $dbh = C4::Context->dbh;
346 $sth = $dbh->prepare("
347 INSERT INTO collections_tracking (
352 $sth->execute( $colId, $itemnumber ) or return ( 0, 3, $sth->errstr() );
358 =head2 RemoveItemFromCollection
360 ( $success, $errorcode, $errormessage ) = RemoveItemFromCollection( $colId, $itemnumber );
362 Removes an item to a collection
365 $colId: Collection to add the item to.
366 $itemnumber: Item to be removed from collection
369 $success: 1 if all database operations were successful, 0 otherwise
370 $errorCode: Code for reason of failure, good for translating errors in templates
371 $errorMessage: English description of error
375 sub RemoveItemFromCollection {
376 my ( $colId, $itemnumber ) = @_;
378 ## Check for all necessary parameters
379 if ( !$itemnumber ) {
380 return ( 0, 2, "NO_ITEM" );
383 if ( !isItemInThisCollection( $itemnumber, $colId ) ) {
384 return ( 0, 2, "NOT_IN_COLLECTION" );
387 my $dbh = C4::Context->dbh;
390 $sth = $dbh->prepare(
391 "DELETE FROM collections_tracking
392 WHERE itemnumber = ?"
394 $sth->execute($itemnumber) or return ( 0, 3, $sth->errstr() );
399 =head2 TransferCollection
401 ( $success, $errorcode, $errormessage ) = TransferCollection( $colId, $colBranchcode );
403 Transfers a collection to another branch
406 $colId: id of the collection to be updated
407 $colBranchcode: branch where collection is moving to
410 $success: 1 if all database operations were successful, 0 otherwise
411 $errorCode: Code for reason of failure, good for translating errors in templates
412 $errorMessage: English description of error
416 sub TransferCollection {
417 my ( $colId, $colBranchcode ) = @_;
419 ## Check for all necessary parameters
421 return ( 0, 1, "NO_ID" );
423 if ( !$colBranchcode ) {
424 return ( 0, 2, "NO_BRANCHCODE" );
427 my $dbh = C4::Context->dbh;
430 $sth = $dbh->prepare(
436 $sth->execute( $colBranchcode, $colId ) or return ( 0, 4, $sth->errstr() );
438 $sth = $dbh->prepare(q{
439 SELECT items.itemnumber, items.barcode FROM collections_tracking
440 LEFT JOIN items ON collections_tracking.itemnumber = items.itemnumber
441 LEFT JOIN issues ON items.itemnumber = issues.itemnumber
442 WHERE issues.borrowernumber IS NULL
443 AND collections_tracking.colId = ?
445 $sth->execute($colId) or return ( 0, 4, $sth->errstr );
447 while ( my $item = $sth->fetchrow_hashref ) {
448 my ($status) = CheckReserves( $item->{itemnumber} );
449 my @transfers = C4::Circulation::GetTransfers( $item->{itemnumber} );
450 C4::Circulation::transferbook( $colBranchcode, $item->{barcode}, my $ignore_reserves = 1 ) unless ( $status eq 'Waiting' || @transfers );
457 =head2 GetCollectionItemBranches
459 my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches( $itemnumber );
463 sub GetCollectionItemBranches {
464 my ($itemnumber) = @_;
466 if ( !$itemnumber ) {
470 my $dbh = C4::Context->dbh;
472 my ( $sth, @results );
473 $sth = $dbh->prepare(
474 "SELECT holdingbranch, colBranchcode FROM items, collections, collections_tracking
475 WHERE items.itemnumber = collections_tracking.itemnumber
476 AND collections.colId = collections_tracking.colId
477 AND items.itemnumber = ?"
479 $sth->execute($itemnumber);
481 my $row = $sth->fetchrow_hashref;
483 return ( $$row{'holdingbranch'}, $$row{'colBranchcode'}, );
486 =head2 isItemInThisCollection
488 $inCollection = isItemInThisCollection( $itemnumber, $colId );
492 sub isItemInThisCollection {
493 my ( $itemnumber, $colId ) = @_;
495 my $dbh = C4::Context->dbh;
497 my $sth = $dbh->prepare(
498 "SELECT COUNT(*) as inCollection FROM collections_tracking WHERE itemnumber = ? AND colId = ?"
500 $sth->execute( $itemnumber, $colId ) or return (0);
502 my $row = $sth->fetchrow_hashref;
504 return $$row{'inCollection'};
507 =head2 isItemInAnyCollection
509 $inCollection = isItemInAnyCollection( $itemnumber );
513 sub isItemInAnyCollection {
514 my ($itemnumber) = @_;
516 my $dbh = C4::Context->dbh;
518 my $sth = $dbh->prepare(
519 "SELECT itemnumber FROM collections_tracking WHERE itemnumber = ?");
520 $sth->execute($itemnumber) or return (0);
522 my $row = $sth->fetchrow_hashref;
524 $itemnumber = $row->{itemnumber};
539 Kyle Hall <kylemhall@gmail.com>