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 );
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 );
171 Deletes a collection of the given id
174 $colId : id of the Archetype to be deleted
177 $success: 1 if all database operations were successful, 0 otherwise
178 $errorCode: Code for reason of failure, good for translating errors in templates
179 $errorMessage: English description of error
183 sub DeleteCollection
{
188 return ( 0, 1, "NO_ID" );
191 my $dbh = C4
::Context
->dbh;
195 $sth = $dbh->prepare("DELETE FROM collections WHERE colId = ?");
196 $sth->execute($colId) or return ( 0, 4, $sth->errstr() );
201 =head2 GetCollections
203 $collections = GetCollections();
205 Returns data about all collections
209 $results: Reference to an array of associated arrays
211 $errorCode: Code for reason of failure, good for translating errors in templates
212 $errorMessage: English description of error
218 my $dbh = C4
::Context
->dbh;
220 my $sth = $dbh->prepare("SELECT * FROM collections");
221 $sth->execute() or return ( 1, $sth->errstr() );
224 while ( my $row = $sth->fetchrow_hashref ) {
225 push( @results, $row );
231 =head2 GetItemsInCollection
233 ( $results, $success, $errorcode, $errormessage ) = GetItemsInCollection( $colId );
235 Returns information about the items in the given collection
238 $colId: The id of the collection
241 $results: Reference to an array of associated arrays
242 $success: 1 if all database operations were successful, 0 otherwise
243 $errorCode: Code for reason of failure, good for translating errors in templates
244 $errorMessage: English description of error
248 sub GetItemsInCollection
{
253 return ( 0, 0, 1, "NO_ID" );
256 my $dbh = C4
::Context
->dbh;
258 my $sth = $dbh->prepare(
262 items.itemcallnumber,
264 FROM collections, collections_tracking, items, biblio
265 WHERE collections.colId = collections_tracking.colId
266 AND collections_tracking.itemnumber = items.itemnumber
267 AND items.biblionumber = biblio.biblionumber
268 AND collections.colId = ? ORDER BY biblio.title"
270 $sth->execute($colId) or return ( 0, 0, 2, $sth->errstr() );
273 while ( my $row = $sth->fetchrow_hashref ) {
274 push( @results, $row );
282 ( $colId, $colTitle, $colDesc, $colBranchcode ) = GetCollection( $colId );
284 Returns information about a collection
287 $colId: Id of the collection
289 $colId, $colTitle, $colDesc, $colBranchcode
296 my $dbh = C4
::Context
->dbh;
298 my ( $sth, @results );
299 $sth = $dbh->prepare("SELECT * FROM collections WHERE colId = ?");
300 $sth->execute($colId) or return 0;
302 my $row = $sth->fetchrow_hashref;
305 $$row{'colId'}, $$row{'colTitle'},
306 $$row{'colDesc'}, $$row{'colBranchcode'}
311 =head2 AddItemToCollection
313 ( $success, $errorcode, $errormessage ) = AddItemToCollection( $colId, $itemnumber );
315 Adds an item to a rotating collection.
318 $colId: Collection to add the item to.
319 $itemnumber: Item to be added to the collection
321 $success: 1 if all database operations were successful, 0 otherwise
322 $errorCode: Code for reason of failure, good for translating errors in templates
323 $errorMessage: English description of error
327 sub AddItemToCollection
{
328 my ( $colId, $itemnumber ) = @_;
330 ## Check for all necessary parameters
332 return ( 0, 1, "NO_ID" );
334 if ( !$itemnumber ) {
335 return ( 0, 2, "NO_ITEM" );
338 if ( isItemInThisCollection
( $itemnumber, $colId ) ) {
339 return ( 0, 2, "IN_COLLECTION" );
341 elsif ( isItemInAnyCollection
($itemnumber) ) {
342 return ( 0, 3, "IN_COLLECTION_OTHER" );
345 my $dbh = C4
::Context
->dbh;
348 $sth = $dbh->prepare("
349 INSERT INTO collections_tracking (
354 $sth->execute( $colId, $itemnumber ) or return ( 0, 3, $sth->errstr() );
360 =head2 RemoveItemFromCollection
362 ( $success, $errorcode, $errormessage ) = RemoveItemFromCollection( $colId, $itemnumber );
364 Removes an item to a collection
367 $colId: Collection to add the item to.
368 $itemnumber: Item to be removed from collection
371 $success: 1 if all database operations were successful, 0 otherwise
372 $errorCode: Code for reason of failure, good for translating errors in templates
373 $errorMessage: English description of error
377 sub RemoveItemFromCollection
{
378 my ( $colId, $itemnumber ) = @_;
380 ## Check for all necessary parameters
381 if ( !$itemnumber ) {
382 return ( 0, 2, "NO_ITEM" );
385 if ( !isItemInThisCollection
( $itemnumber, $colId ) ) {
386 return ( 0, 2, "NOT_IN_COLLECTION" );
389 my $dbh = C4
::Context
->dbh;
392 $sth = $dbh->prepare(
393 "DELETE FROM collections_tracking
394 WHERE itemnumber = ?"
396 $sth->execute($itemnumber) or return ( 0, 3, $sth->errstr() );
401 =head2 TransferCollection
403 ( $success, $errorcode, $errormessage ) = TransferCollection( $colId, $colBranchcode );
405 Transfers a collection to another branch
408 $colId: id of the collection to be updated
409 $colBranchcode: branch where collection is moving to
412 $success: 1 if all database operations were successful, 0 otherwise
413 $errorCode: Code for reason of failure, good for translating errors in templates
414 $errorMessage: English description of error
418 sub TransferCollection
{
419 my ( $colId, $colBranchcode ) = @_;
421 ## Check for all necessary parameters
423 return ( 0, 1, "NO_ID" );
425 if ( !$colBranchcode ) {
426 return ( 0, 2, "NO_BRANCHCODE" );
429 my $dbh = C4
::Context
->dbh;
432 $sth = $dbh->prepare(
438 $sth->execute( $colBranchcode, $colId ) or return ( 0, 4, $sth->errstr() );
440 $sth = $dbh->prepare(q{
441 SELECT items.itemnumber, items.barcode FROM collections_tracking
442 LEFT JOIN items ON collections_tracking.itemnumber = items.itemnumber
443 LEFT JOIN issues ON items.itemnumber = issues.itemnumber
444 WHERE issues.borrowernumber IS NULL
445 AND collections_tracking.colId = ?
447 $sth->execute($colId) or return ( 0, 4, $sth->errstr );
449 while ( my $item = $sth->fetchrow_hashref ) {
450 my ($status) = CheckReserves
( $item->{itemnumber
} );
451 my @transfers = C4
::Circulation
::GetTransfers
( $item->{itemnumber
} );
452 C4
::Circulation
::transferbook
({
453 from_branch
=> $item->holdingbranch,
454 to_branch
=> $colBranchcode,
455 barcode
=> $item->{barcode
},
456 ignore_reserves
=> 1,
457 trigger
=> 'RotatingCollection'
458 }) unless ( $status eq 'Waiting' || $status eq 'Processing' || @transfers );
464 =head2 GetCollectionItemBranches
466 my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches( $itemnumber );
470 sub GetCollectionItemBranches
{
471 my ($itemnumber) = @_;
473 if ( !$itemnumber ) {
477 my $dbh = C4
::Context
->dbh;
479 my ( $sth, @results );
480 $sth = $dbh->prepare(
481 "SELECT holdingbranch, colBranchcode FROM items, collections, collections_tracking
482 WHERE items.itemnumber = collections_tracking.itemnumber
483 AND collections.colId = collections_tracking.colId
484 AND items.itemnumber = ?"
486 $sth->execute($itemnumber);
488 my $row = $sth->fetchrow_hashref;
490 return ( $$row{'holdingbranch'}, $$row{'colBranchcode'}, );
493 =head2 isItemInThisCollection
495 $inCollection = isItemInThisCollection( $itemnumber, $colId );
499 sub isItemInThisCollection
{
500 my ( $itemnumber, $colId ) = @_;
502 my $dbh = C4
::Context
->dbh;
504 my $sth = $dbh->prepare(
505 "SELECT COUNT(*) as inCollection FROM collections_tracking WHERE itemnumber = ? AND colId = ?"
507 $sth->execute( $itemnumber, $colId ) or return (0);
509 my $row = $sth->fetchrow_hashref;
511 return $$row{'inCollection'};
514 =head2 isItemInAnyCollection
516 my $inCollection = isItemInAnyCollection( $itemnumber );
520 sub isItemInAnyCollection
{
521 my ($itemnumber) = @_;
523 my $dbh = C4
::Context
->dbh;
525 my $sth = $dbh->prepare(
526 "SELECT itemnumber FROM collections_tracking WHERE itemnumber = ?");
527 $sth->execute($itemnumber) or return (0);
529 my $row = $sth->fetchrow_hashref;
531 $itemnumber = $row->{itemnumber
};
546 Kyle Hall <kylemhall@gmail.com>