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($VERSION @ISA @EXPORT);
38 # set the version for version checking
39 $VERSION = 3.07.00.049;
43 C4::RotatingCollections - Functions for managing rotating collections
51 @ISA = qw( Exporter );
63 RemoveItemFromCollection
66 GetCollectionItemBranches
70 =head2 CreateCollection
71 ( $success, $errorcode, $errormessage ) = CreateCollection( $title, $description );
72 Creates a new collection
75 $title: short description of the club or service
76 $description: long description of the club or service
79 $success: 1 if all database operations were successful, 0 otherwise
80 $errorCode: Code for reason of failure, good for translating errors in templates
81 $errorMessage: English description of error
85 sub CreateCollection
{
86 my ( $title, $description ) = @_;
88 my $schema = Koha
::Database
->new()->schema();
89 my $duplicate_titles = $schema->resultset('Collection')->count({ colTitle
=> $title });
91 ## Check for all necessary parameters
93 return ( 0, 1, "NO_TITLE" );
94 } elsif ( $duplicate_titles ) {
95 return ( 0, 2, "DUPLICATE_TITLE" );
102 my $dbh = C4
::Context
->dbh;
105 $sth = $dbh->prepare(
106 "INSERT INTO collections ( colId, colTitle, colDesc )
107 VALUES ( NULL, ?, ? )"
109 $sth->execute( $title, $description ) or return ( 0, 3, $sth->errstr() );
115 =head2 UpdateCollection
117 ( $success, $errorcode, $errormessage ) = UpdateCollection( $colId, $title, $description );
122 $colId: id of the collection to be updated
123 $title: short description of the club or service
124 $description: long description of the club or service
127 $success: 1 if all database operations were successful, 0 otherwise
128 $errorCode: Code for reason of failure, good for translating errors in templates
129 $errorMessage: English description of error
133 sub UpdateCollection
{
134 my ( $colId, $title, $description ) = @_;
136 my $schema = Koha
::Database
->new()->schema();
137 my $duplicate_titles = $schema->resultset('Collection')->count({ colTitle
=> $title, -not => { colId
=> $colId } });
139 ## Check for all necessary parameters
141 return ( 0, 1, "NO_ID" );
144 return ( 0, 2, "NO_TITLE" );
146 if ( $duplicate_titles ) {
147 return ( 0, 3, "DUPLICATE_TITLE" );
150 my $dbh = C4
::Context
->dbh;
152 $description ||= q{};
155 $sth = $dbh->prepare(
158 colTitle = ?, colDesc = ?
161 $sth->execute( $title, $description, $colId )
162 or return ( 0, 4, $sth->errstr() );
168 =head2 DeleteCollection
170 ( $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();
204 Returns data about all collections
208 $results: Reference to an array of associated arrays
210 $errorCode: Code for reason of failure, good for translating errors in templates
211 $errorMessage: English description of error
217 my $dbh = C4
::Context
->dbh;
219 my $sth = $dbh->prepare("SELECT * FROM collections");
220 $sth->execute() or return ( 1, $sth->errstr() );
223 while ( my $row = $sth->fetchrow_hashref ) {
224 push( @results, $row );
230 =head2 GetItemsInCollection
232 ( $results, $success, $errorcode, $errormessage ) = GetItemsInCollection( $colId );
234 Returns information about the items in the given collection
237 $colId: The id of the collection
240 $results: Reference to an array of associated arrays
241 $success: 1 if all database operations were successful, 0 otherwise
242 $errorCode: Code for reason of failure, good for translating errors in templates
243 $errorMessage: English description of error
247 sub GetItemsInCollection
{
252 return ( 0, 0, 1, "NO_ID" );
255 my $dbh = C4
::Context
->dbh;
257 my $sth = $dbh->prepare(
261 items.itemcallnumber,
263 FROM collections, collections_tracking, items, biblio
264 WHERE collections.colId = collections_tracking.colId
265 AND collections_tracking.itemnumber = items.itemnumber
266 AND items.biblionumber = biblio.biblionumber
267 AND collections.colId = ? ORDER BY biblio.title"
269 $sth->execute($colId) or return ( 0, 0, 2, $sth->errstr() );
272 while ( my $row = $sth->fetchrow_hashref ) {
273 push( @results, $row );
281 ( $colId, $colTitle, $colDesc, $colBranchcode ) = GetCollection( $colId );
283 Returns information about a collection
286 $colId: Id of the collection
288 $colId, $colTitle, $colDesc, $colBranchcode
295 my $dbh = C4
::Context
->dbh;
297 my ( $sth, @results );
298 $sth = $dbh->prepare("SELECT * FROM collections WHERE colId = ?");
299 $sth->execute($colId) or return 0;
301 my $row = $sth->fetchrow_hashref;
304 $$row{'colId'}, $$row{'colTitle'},
305 $$row{'colDesc'}, $$row{'colBranchcode'}
310 =head2 AddItemToCollection
312 ( $success, $errorcode, $errormessage ) = AddItemToCollection( $colId, $itemnumber );
314 Adds an item to a rotating collection.
317 $colId: Collection to add the item to.
318 $itemnumber: Item to be added to the collection
320 $success: 1 if all database operations were successful, 0 otherwise
321 $errorCode: Code for reason of failure, good for translating errors in templates
322 $errorMessage: English description of error
326 sub AddItemToCollection
{
327 my ( $colId, $itemnumber ) = @_;
329 ## Check for all necessary parameters
331 return ( 0, 1, "NO_ID" );
333 if ( !$itemnumber ) {
334 return ( 0, 2, "NO_ITEM" );
337 if ( isItemInThisCollection
( $itemnumber, $colId ) ) {
338 return ( 0, 2, "IN_COLLECTION" );
340 elsif ( isItemInAnyCollection
($itemnumber) ) {
341 return ( 0, 3, "IN_COLLECTION_OTHER" );
344 my $dbh = C4
::Context
->dbh;
347 $sth = $dbh->prepare("
348 INSERT INTO collections_tracking (
353 $sth->execute( $colId, $itemnumber ) or return ( 0, 3, $sth->errstr() );
359 =head2 RemoveItemFromCollection
361 ( $success, $errorcode, $errormessage ) = RemoveItemFromCollection( $colId, $itemnumber );
363 Removes an item to a collection
366 $colId: Collection to add the item to.
367 $itemnumber: Item to be removed from collection
370 $success: 1 if all database operations were successful, 0 otherwise
371 $errorCode: Code for reason of failure, good for translating errors in templates
372 $errorMessage: English description of error
376 sub RemoveItemFromCollection
{
377 my ( $colId, $itemnumber ) = @_;
379 ## Check for all necessary parameters
380 if ( !$itemnumber ) {
381 return ( 0, 2, "NO_ITEM" );
384 if ( !isItemInThisCollection
( $itemnumber, $colId ) ) {
385 return ( 0, 2, "NOT_IN_COLLECTION" );
388 my $dbh = C4
::Context
->dbh;
391 $sth = $dbh->prepare(
392 "DELETE FROM collections_tracking
393 WHERE itemnumber = ?"
395 $sth->execute($itemnumber) or return ( 0, 3, $sth->errstr() );
400 =head2 TransferCollection
402 ( $success, $errorcode, $errormessage ) = TransferCollection( $colId, $colBranchcode );
404 Transfers a collection to another branch
407 $colId: id of the collection to be updated
408 $colBranchcode: branch where collection is moving to
411 $success: 1 if all database operations were successful, 0 otherwise
412 $errorCode: Code for reason of failure, good for translating errors in templates
413 $errorMessage: English description of error
417 sub TransferCollection
{
418 my ( $colId, $colBranchcode ) = @_;
420 ## Check for all necessary parameters
422 return ( 0, 1, "NO_ID" );
424 if ( !$colBranchcode ) {
425 return ( 0, 2, "NO_BRANCHCODE" );
428 my $dbh = C4
::Context
->dbh;
431 $sth = $dbh->prepare(
437 $sth->execute( $colBranchcode, $colId ) or return ( 0, 4, $sth->errstr() );
439 $sth = $dbh->prepare(q{
440 SELECT items.itemnumber, items.barcode FROM collections_tracking
441 LEFT JOIN items ON collections_tracking.itemnumber = items.itemnumber
442 LEFT JOIN issues ON items.itemnumber = issues.itemnumber
443 WHERE issues.borrowernumber IS NULL
444 AND collections_tracking.colId = ?
446 $sth->execute($colId) or return ( 0, 4, $sth->errstr );
448 while ( my $item = $sth->fetchrow_hashref ) {
449 my ($status) = CheckReserves
( $item->{itemnumber
} );
450 my @transfers = GetTransfers
( $item->{itemnumber
} );
451 transferbook
( $colBranchcode, $item->{barcode
}, my $ignore_reserves = 1 ) unless ( $status eq 'Waiting' || @transfers );
458 =head2 GetCollectionItemBranches
460 my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches( $itemnumber );
464 sub GetCollectionItemBranches
{
465 my ($itemnumber) = @_;
467 if ( !$itemnumber ) {
471 my $dbh = C4
::Context
->dbh;
473 my ( $sth, @results );
474 $sth = $dbh->prepare(
475 "SELECT holdingbranch, colBranchcode FROM items, collections, collections_tracking
476 WHERE items.itemnumber = collections_tracking.itemnumber
477 AND collections.colId = collections_tracking.colId
478 AND items.itemnumber = ?"
480 $sth->execute($itemnumber);
482 my $row = $sth->fetchrow_hashref;
484 return ( $$row{'holdingbranch'}, $$row{'colBranchcode'}, );
487 =head2 isItemInThisCollection
489 $inCollection = isItemInThisCollection( $itemnumber, $colId );
493 sub isItemInThisCollection
{
494 my ( $itemnumber, $colId ) = @_;
496 my $dbh = C4
::Context
->dbh;
498 my $sth = $dbh->prepare(
499 "SELECT COUNT(*) as inCollection FROM collections_tracking WHERE itemnumber = ? AND colId = ?"
501 $sth->execute( $itemnumber, $colId ) or return (0);
503 my $row = $sth->fetchrow_hashref;
505 return $$row{'inCollection'};
508 =head2 isItemInAnyCollection
510 $inCollection = isItemInAnyCollection( $itemnumber );
514 sub isItemInAnyCollection
{
515 my ($itemnumber) = @_;
517 my $dbh = C4
::Context
->dbh;
519 my $sth = $dbh->prepare(
520 "SELECT itemnumber FROM collections_tracking WHERE itemnumber = ?");
521 $sth->execute($itemnumber) or return (0);
523 my $row = $sth->fetchrow_hashref;
525 $itemnumber = $row->{itemnumber
};
540 Kyle Hall <kylemhall@gmail.com>