Bug 7180: (follow-up) fix warnings
[koha.git] / C4 / RotatingCollections.pm
blobd6fad87b377d38cfd6a2f1ad58e0f5af346ed02f
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
15 # version.
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.
25 use strict;
26 #use warnings; FIXME - Bug 2505
28 require Exporter;
30 use C4::Context;
31 use C4::Circulation;
33 use DBI;
35 use Data::Dumper;
37 use vars qw($VERSION @ISA @EXPORT);
39 # set the version for version checking
40 $VERSION = 3.07.00.049;
42 =head1 NAME
44 C4::RotatingCollections - Functions for managing rotating collections
46 =head1 FUNCTIONS
48 =cut
50 @ISA = qw( Exporter );
51 @EXPORT = qw(
52 CreateCollection
53 UpdateCollection
54 DeleteCollection
56 GetItemsInCollection
58 GetCollection
59 GetCollections
61 AddItemToCollection
62 RemoveItemFromCollection
63 TransferCollection
65 GetCollectionItemBranches
68 =head2 CreateCollection
69 ( $success, $errorcode, $errormessage ) = CreateCollection( $title, $description );
70 Creates a new collection
72 Input:
73 $title: short description of the club or service
74 $description: long description of the club or service
76 Output:
77 $success: 1 if all database operations were successful, 0 otherwise
78 $errorCode: Code for reason of failure, good for translating errors in templates
79 $errorMessage: English description of error
81 =cut
83 sub CreateCollection {
84 my ( $title, $description ) = @_;
86 ## Check for all neccessary parameters
87 if ( ! $title ) {
88 return ( 0, 1, "No Title Given" );
90 if ( ! $description ) {
91 return ( 0, 2, "No Description Given" );
94 my $success = 1;
96 my $dbh = C4::Context->dbh;
98 my $sth;
99 $sth = $dbh->prepare("INSERT INTO collections ( colId, colTitle, colDesc )
100 VALUES ( NULL, ?, ? )");
101 $sth->execute( $title, $description ) or return ( 0, 3, $sth->errstr() );
103 return 1;
107 =head2 UpdateCollection
109 ( $success, $errorcode, $errormessage ) = UpdateCollection( $colId, $title, $description );
111 Updates a collection
113 Input:
114 $colId: id of the collection to be updated
115 $title: short description of the club or service
116 $description: long description of the club or service
118 Output:
119 $success: 1 if all database operations were successful, 0 otherwise
120 $errorCode: Code for reason of failure, good for translating errors in templates
121 $errorMessage: English description of error
123 =cut
125 sub UpdateCollection {
126 my ( $colId, $title, $description ) = @_;
128 ## Check for all neccessary parameters
129 if ( ! $colId ) {
130 return ( 0, 1, "No Id Given" );
132 if ( ! $title ) {
133 return ( 0, 2, "No Title Given" );
135 if ( ! $description ) {
136 return ( 0, 3, "No Description Given" );
139 my $dbh = C4::Context->dbh;
141 my $sth;
142 $sth = $dbh->prepare("UPDATE collections
143 SET
144 colTitle = ?, colDesc = ?
145 WHERE colId = ?");
146 $sth->execute( $title, $description, $colId ) or return ( 0, 4, $sth->errstr() );
148 return 1;
152 =head2 DeleteCollection
154 ( $success, $errorcode, $errormessage ) = DeleteCollection( $colId );
155 Deletes a collection of the given id
157 Input:
158 $colId : id of the Archtype to be deleted
160 Output:
161 $success: 1 if all database operations were successful, 0 otherwise
162 $errorCode: Code for reason of failure, good for translating errors in templates
163 $errorMessage: English description of error
165 =cut
167 sub DeleteCollection {
168 my ( $colId ) = @_;
170 ## Paramter check
171 if ( ! $colId ) {
172 return ( 0, 1, "No Collection Id Given" );;
175 my $dbh = C4::Context->dbh;
177 my $sth;
179 $sth = $dbh->prepare("DELETE FROM collections WHERE colId = ?");
180 $sth->execute( $colId ) or return ( 0, 4, $sth->errstr() );
182 return 1;
185 =head2 GetCollections
187 $collections = GetCollections();
188 Returns data about all collections
190 Output:
191 On Success:
192 $results: Reference to an array of associated arrays
193 On Failure:
194 $errorCode: Code for reason of failure, good for translating errors in templates
195 $errorMessage: English description of error
197 =cut
199 sub GetCollections {
201 my $dbh = C4::Context->dbh;
203 my $sth = $dbh->prepare("SELECT * FROM collections");
204 $sth->execute() or return ( 1, $sth->errstr() );
206 my @results;
207 while ( my $row = $sth->fetchrow_hashref ) {
208 push( @results , $row );
211 return \@results;
214 =head2 GetItemsInCollection
216 ( $results, $success, $errorcode, $errormessage ) = GetItemsInCollection( $colId );
218 Returns information about the items in the given collection
220 Input:
221 $colId: The id of the collection
223 Output:
224 $results: Reference to an array of associated arrays
225 $success: 1 if all database operations were successful, 0 otherwise
226 $errorCode: Code for reason of failure, good for translating errors in templates
227 $errorMessage: English description of error
229 =cut
231 sub GetItemsInCollection {
232 my ( $colId ) = @_;
234 ## Paramter check
235 if ( ! $colId ) {
236 return ( 0, 0, 1, "No Collection Id Given" );;
239 my $dbh = C4::Context->dbh;
241 my $sth = $dbh->prepare("SELECT
242 biblio.title,
243 items.itemcallnumber,
244 items.barcode
245 FROM collections, collections_tracking, items, biblio
246 WHERE collections.colId = collections_tracking.colId
247 AND collections_tracking.itemnumber = items.itemnumber
248 AND items.biblionumber = biblio.biblionumber
249 AND collections.colId = ? ORDER BY biblio.title");
250 $sth->execute( $colId ) or return ( 0, 0, 2, $sth->errstr() );
252 my @results;
253 while ( my $row = $sth->fetchrow_hashref ) {
254 push( @results , $row );
257 return \@results;
260 =head2 GetCollection
262 ( $colId, $colTitle, $colDesc, $colBranchcode ) = GetCollection( $colId );
264 Returns information about a collection
266 Input:
267 $colId: Id of the collection
268 Output:
269 $colId, $colTitle, $colDesc, $colBranchcode
271 =cut
273 sub GetCollection {
274 my ( $colId ) = @_;
276 my $dbh = C4::Context->dbh;
278 my ( $sth, @results );
279 $sth = $dbh->prepare("SELECT * FROM collections WHERE colId = ?");
280 $sth->execute( $colId ) or return 0;
282 my $row = $sth->fetchrow_hashref;
284 return (
285 $$row{'colId'},
286 $$row{'colTitle'},
287 $$row{'colDesc'},
288 $$row{'colBranchcode'}
293 =head2 AddItemToCollection
295 ( $success, $errorcode, $errormessage ) = AddItemToCollection( $colId, $itemnumber );
297 Adds an item to a rotating collection.
299 Input:
300 $colId: Collection to add the item to.
301 $itemnumber: Item to be added to the collection
302 Output:
303 $success: 1 if all database operations were successful, 0 otherwise
304 $errorCode: Code for reason of failure, good for translating errors in templates
305 $errorMessage: English description of error
307 =cut
309 sub AddItemToCollection {
310 my ( $colId, $itemnumber ) = @_;
312 ## Check for all neccessary parameters
313 if ( ! $colId ) {
314 return ( 0, 1, "No Collection Given" );
316 if ( ! $itemnumber ) {
317 return ( 0, 2, "No Itemnumber Given" );
320 if ( isItemInThisCollection( $itemnumber, $colId ) ) {
321 return ( 0, 2, "Item is already in the collection!" );
322 } elsif ( isItemInAnyCollection( $itemnumber ) ) {
323 return ( 0, 3, "Item is already in a different collection!" );
326 my $dbh = C4::Context->dbh;
328 my $sth;
329 $sth = $dbh->prepare("INSERT INTO collections_tracking ( collections_tracking_id, colId, itemnumber )
330 VALUES ( NULL, ?, ? )");
331 $sth->execute( $colId, $itemnumber ) or return ( 0, 3, $sth->errstr() );
333 return 1;
337 =head2 RemoveItemFromCollection
339 ( $success, $errorcode, $errormessage ) = RemoveItemFromCollection( $colId, $itemnumber );
341 Removes an item to a collection
343 Input:
344 $colId: Collection to add the item to.
345 $itemnumber: Item to be removed from collection
347 Output:
348 $success: 1 if all database operations were successful, 0 otherwise
349 $errorCode: Code for reason of failure, good for translating errors in templates
350 $errorMessage: English description of error
352 =cut
354 sub RemoveItemFromCollection {
355 my ( $colId, $itemnumber ) = @_;
357 ## Check for all neccessary parameters
358 if ( ! $itemnumber ) {
359 return ( 0, 2, "No Itemnumber Given" );
362 if ( ! isItemInThisCollection( $itemnumber, $colId ) ) {
363 return ( 0, 2, "Item is not in the collection!" );
366 my $dbh = C4::Context->dbh;
368 my $sth;
369 $sth = $dbh->prepare("DELETE FROM collections_tracking
370 WHERE itemnumber = ?");
371 $sth->execute( $itemnumber ) or return ( 0, 3, $sth->errstr() );
373 return 1;
376 =head2 TransferCollection
378 ( $success, $errorcode, $errormessage ) = TransferCollection( $colId, $colBranchcode );
380 Transfers a collection to another branch
382 Input:
383 $colId: id of the collection to be updated
384 $colBranchcode: branch where collection is moving to
386 Output:
387 $success: 1 if all database operations were successful, 0 otherwise
388 $errorCode: Code for reason of failure, good for translating errors in templates
389 $errorMessage: English description of error
391 =cut
393 sub TransferCollection {
394 my ( $colId, $colBranchcode ) = @_;
396 ## Check for all neccessary parameters
397 if ( ! $colId ) {
398 return ( 0, 1, "No Id Given" );
400 if ( ! $colBranchcode ) {
401 return ( 0, 2, "No Branchcode Given" );
404 my $dbh = C4::Context->dbh;
406 my $sth;
407 $sth = $dbh->prepare("UPDATE collections
408 SET
409 colBranchcode = ?
410 WHERE colId = ?");
411 $sth->execute( $colBranchcode, $colId ) or return ( 0, 4, $sth->errstr() );
413 $sth = $dbh->prepare("SELECT barcode FROM items, collections_tracking
414 WHERE items.itemnumber = collections_tracking.itemnumber
415 AND collections_tracking.colId = ?");
416 $sth->execute( $colId ) or return ( 0, 4, $sth->errstr );
417 my @results;
418 while ( my $item = $sth->fetchrow_hashref ) {
419 my ( $dotransfer, $messages, $iteminformation ) = transferbook( $colBranchcode, $item->{'barcode'}, my $ignore_reserves = 1);
422 return 1;
426 =head2 GetCollectionItemBranches
428 my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches( $itemnumber );
430 =cut
432 sub GetCollectionItemBranches {
433 my ( $itemnumber ) = @_;
435 if ( ! $itemnumber ) {
436 return;
439 my $dbh = C4::Context->dbh;
441 my ( $sth, @results );
442 $sth = $dbh->prepare("SELECT holdingbranch, colBranchcode FROM items, collections, collections_tracking
443 WHERE items.itemnumber = collections_tracking.itemnumber
444 AND collections.colId = collections_tracking.colId
445 AND items.itemnumber = ?");
446 $sth->execute( $itemnumber );
448 my $row = $sth->fetchrow_hashref;
450 return (
451 $$row{'holdingbranch'},
452 $$row{'colBranchcode'},
456 =head2 isItemInThisCollection
458 $inCollection = isItemInThisCollection( $itemnumber, $colId );
460 =cut
462 sub isItemInThisCollection {
463 my ( $itemnumber, $colId ) = @_;
465 my $dbh = C4::Context->dbh;
467 my $sth = $dbh->prepare("SELECT COUNT(*) as inCollection FROM collections_tracking WHERE itemnumber = ? AND colId = ?");
468 $sth->execute( $itemnumber, $colId ) or return( 0 );
470 my $row = $sth->fetchrow_hashref;
472 return $$row{'inCollection'};
475 =head2 isItemInAnyCollection
477 $inCollection = isItemInAnyCollection( $itemnumber );
479 =cut
481 sub isItemInAnyCollection {
482 my ( $itemnumber ) = @_;
484 my $dbh = C4::Context->dbh;
486 my $sth = $dbh->prepare("SELECT itemnumber FROM collections_tracking WHERE itemnumber = ?");
487 $sth->execute( $itemnumber ) or return( 0 );
489 my $row = $sth->fetchrow_hashref;
491 $itemnumber = $row->{itemnumber};
492 if ( $itemnumber ) {
493 return 1;
494 } else {
495 return 0;
501 __END__
503 =head1 AUTHOR
505 Kyle Hall <kylemhall@gmail.com>
507 =cut