bug 4802 add missing order search help file
[koha.git] / C4 / RotatingCollections.pm
blob6b1383f62c43983c8c9395743df01ab442a66c52
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 = 0.01;
42 =head1 NAME
44 C4::RotatingCollections - Functions for managing rotating collections
46 =head1 FUNCTIONS
48 =over 2
50 =cut
52 @ISA = qw( Exporter );
53 @EXPORT = qw(
54 CreateCollection
55 UpdateCollection
56 DeleteCollection
58 GetItemsInCollection
60 GetCollection
61 GetCollections
63 AddItemToCollection
64 RemoveItemFromCollection
65 TransferCollection
67 GetCollectionItemBranches
70 =item CreateCollection
71 ( $success, $errorcode, $errormessage ) = CreateCollection( $title, $description );
72 Creates a new collection
74 Input:
75 $title: short description of the club or service
76 $description: long description of the club or service
78 Output:
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
82 =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() );
102 $sth->finish;
104 return 1;
108 =item UpdateCollection
109 ( $success, $errorcode, $errormessage ) = UpdateCollection( $colId, $title, $description );
110 Updates a collection
112 Input:
113 $colId: id of the collection to be updated
114 $title: short description of the club or service
115 $description: long description of the club or service
117 Output:
118 $success: 1 if all database operations were successful, 0 otherwise
119 $errorCode: Code for reason of failure, good for translating errors in templates
120 $errorMessage: English description of error
121 =cut
122 sub UpdateCollection {
123 my ( $colId, $title, $description ) = @_;
125 ## Check for all neccessary parameters
126 if ( ! $colId ) {
127 return ( 0, 1, "No Id Given" );
129 if ( ! $title ) {
130 return ( 0, 2, "No Title Given" );
132 if ( ! $description ) {
133 return ( 0, 3, "No Description Given" );
136 my $dbh = C4::Context->dbh;
138 my $sth;
139 $sth = $dbh->prepare("UPDATE collections
140 SET
141 colTitle = ?, colDesc = ?
142 WHERE colId = ?");
143 $sth->execute( $title, $description, $colId ) or return ( 0, 4, $sth->errstr() );
144 $sth->finish;
146 return 1;
150 =item DeleteCollection
151 ( $success, $errorcode, $errormessage ) = DeleteCollection( $colId );
152 Deletes a collection of the given id
154 Input:
155 $colId : id of the Archtype to be deleted
157 Output:
158 $success: 1 if all database operations were successful, 0 otherwise
159 $errorCode: Code for reason of failure, good for translating errors in templates
160 $errorMessage: English description of error
161 =cut
162 sub DeleteCollection {
163 my ( $colId ) = @_;
165 ## Paramter check
166 if ( ! $colId ) {
167 return ( 0, 1, "No Collection Id Given" );;
170 my $dbh = C4::Context->dbh;
172 my $sth;
174 $sth = $dbh->prepare("DELETE FROM collections WHERE colId = ?");
175 $sth->execute( $colId ) or return ( 0, 4, $sth->errstr() );
176 $sth->finish;
178 return 1;
181 =item GetCollections
182 $collections = GetCollections();
183 Returns data about all collections
185 Output:
186 On Success:
187 $results: Reference to an array of associated arrays
188 On Failure:
189 $errorCode: Code for reason of failure, good for translating errors in templates
190 $errorMessage: English description of error
191 =cut
192 sub GetCollections {
194 my $dbh = C4::Context->dbh;
196 my $sth = $dbh->prepare("SELECT * FROM collections");
197 $sth->execute() or return ( 1, $sth->errstr() );
199 my @results;
200 while ( my $row = $sth->fetchrow_hashref ) {
201 push( @results , $row );
204 $sth->finish;
206 return \@results;
209 =item GetItemsInCollection
210 ( $results, $success, $errorcode, $errormessage ) = GetItemsInCollection( $colId );
211 Returns information about the items in the given collection
213 Input:
214 $colId: The id of the collection
216 Output:
217 $results: Reference to an array of associated arrays
218 $success: 1 if all database operations were successful, 0 otherwise
219 $errorCode: Code for reason of failure, good for translating errors in templates
220 $errorMessage: English description of error
221 =cut
222 sub GetItemsInCollection {
223 my ( $colId ) = @_;
225 ## Paramter check
226 if ( ! $colId ) {
227 return ( 0, 0, 1, "No Collection Id Given" );;
230 my $dbh = C4::Context->dbh;
232 my $sth = $dbh->prepare("SELECT
233 biblio.title,
234 items.itemcallnumber,
235 items.barcode
236 FROM collections, collections_tracking, items, biblio
237 WHERE collections.colId = collections_tracking.colId
238 AND collections_tracking.itemnumber = items.itemnumber
239 AND items.biblionumber = biblio.biblionumber
240 AND collections.colId = ? ORDER BY biblio.title");
241 $sth->execute( $colId ) or return ( 0, 0, 2, $sth->errstr() );
243 my @results;
244 while ( my $row = $sth->fetchrow_hashref ) {
245 push( @results , $row );
248 $sth->finish;
250 return \@results;
253 =item GetCollection
254 ( $colId, $colTitle, $colDesc, $colBranchcode ) = GetCollection( $colId );
255 Returns information about a collection
257 Input:
258 $colId: Id of the collection
259 Output:
260 $colId, $colTitle, $colDesc, $colBranchcode
261 =cut
262 sub GetCollection {
263 my ( $colId ) = @_;
265 my $dbh = C4::Context->dbh;
267 my ( $sth, @results );
268 $sth = $dbh->prepare("SELECT * FROM collections WHERE colId = ?");
269 $sth->execute( $colId ) or return 0;
271 my $row = $sth->fetchrow_hashref;
273 $sth->finish;
275 return (
276 $$row{'colId'},
277 $$row{'colTitle'},
278 $$row{'colDesc'},
279 $$row{'colBranchcode'}
284 =item AddItemToCollection
285 ( $success, $errorcode, $errormessage ) = AddItemToCollection( $colId, $itemnumber );
286 Adds an item to a rotating collection.
288 Input:
289 $colId: Collection to add the item to.
290 $itemnumber: Item to be added to the collection
291 Output:
292 $success: 1 if all database operations were successful, 0 otherwise
293 $errorCode: Code for reason of failure, good for translating errors in templates
294 $errorMessage: English description of error
295 =cut
296 sub AddItemToCollection {
297 my ( $colId, $itemnumber ) = @_;
299 ## Check for all neccessary parameters
300 if ( ! $colId ) {
301 return ( 0, 1, "No Collection Given" );
303 if ( ! $itemnumber ) {
304 return ( 0, 2, "No Itemnumber Given" );
307 if ( isItemInThisCollection( $itemnumber, $colId ) ) {
308 return ( 0, 2, "Item is already in the collection!" );
309 } elsif ( isItemInAnyCollection( $itemnumber ) ) {
310 return ( 0, 3, "Item is already in a different collection!" );
313 my $dbh = C4::Context->dbh;
315 my $sth;
316 $sth = $dbh->prepare("INSERT INTO collections_tracking ( ctId, colId, itemnumber )
317 VALUES ( NULL, ?, ? )");
318 $sth->execute( $colId, $itemnumber ) or return ( 0, 3, $sth->errstr() );
319 $sth->finish;
321 return 1;
325 =item RemoveItemFromCollection
326 ( $success, $errorcode, $errormessage ) = RemoveItemFromCollection( $colId, $itemnumber );
327 Removes an item to a collection
329 Input:
330 $colId: Collection to add the item to.
331 $itemnumber: Item to be removed from collection
333 Output:
334 $success: 1 if all database operations were successful, 0 otherwise
335 $errorCode: Code for reason of failure, good for translating errors in templates
336 $errorMessage: English description of error
337 =cut
338 sub RemoveItemFromCollection {
339 my ( $colId, $itemnumber ) = @_;
341 ## Check for all neccessary parameters
342 if ( ! $itemnumber ) {
343 return ( 0, 2, "No Itemnumber Given" );
346 if ( ! isItemInThisCollection( $itemnumber, $colId ) ) {
347 return ( 0, 2, "Item is not in the collection!" );
350 my $dbh = C4::Context->dbh;
352 my $sth;
353 $sth = $dbh->prepare("DELETE FROM collections_tracking
354 WHERE itemnumber = ?");
355 $sth->execute( $itemnumber ) or return ( 0, 3, $sth->errstr() );
356 $sth->finish;
358 return 1;
361 =item TransferCollection
362 ( $success, $errorcode, $errormessage ) = TransferCollection( $colId, $colBranchcode );
363 Transfers a collection to another branch
365 Input:
366 $colId: id of the collection to be updated
367 $colBranchcode: branch where collection is moving to
369 Output:
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
373 =cut
374 sub TransferCollection {
375 my ( $colId, $colBranchcode ) = @_;
377 ## Check for all neccessary parameters
378 if ( ! $colId ) {
379 return ( 0, 1, "No Id Given" );
381 if ( ! $colBranchcode ) {
382 return ( 0, 2, "No Branchcode Given" );
385 my $dbh = C4::Context->dbh;
387 my $sth;
388 $sth = $dbh->prepare("UPDATE collections
389 SET
390 colBranchcode = ?
391 WHERE colId = ?");
392 $sth->execute( $colBranchcode, $colId ) or return ( 0, 4, $sth->errstr() );
393 $sth->finish;
395 $sth = $dbh->prepare("SELECT barcode FROM items, collections_tracking
396 WHERE items.itemnumber = collections_tracking.itemnumber
397 AND collections_tracking.colId = ?");
398 $sth->execute( $colId ) or return ( 0, 4, $sth->errstr );
399 my @results;
400 while ( my $item = $sth->fetchrow_hashref ) {
401 my ( $dotransfer, $messages, $iteminformation ) = transferbook( $colBranchcode, $item->{'barcode'}, my $ignore_reserves = 1);
406 return 1;
410 =item GetCollectionItemBranches
411 my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches( $itemnumber );
412 =cut
413 sub GetCollectionItemBranches {
414 my ( $itemnumber ) = @_;
416 if ( ! $itemnumber ) {
417 return;
420 my $dbh = C4::Context->dbh;
422 my ( $sth, @results );
423 $sth = $dbh->prepare("SELECT holdingbranch, colBranchcode FROM items, collections, collections_tracking
424 WHERE items.itemnumber = collections_tracking.itemnumber
425 AND collections.colId = collections_tracking.colId
426 AND items.itemnumber = ?");
427 $sth->execute( $itemnumber );
429 my $row = $sth->fetchrow_hashref;
431 $sth->finish;
433 return (
434 $$row{'holdingbranch'},
435 $$row{'colBranchcode'},
439 =item isItemInThisCollection
440 $inCollection = isItemInThisCollection( $itemnumber, $colId );
441 =cut
442 sub isItemInThisCollection {
443 my ( $itemnumber, $colId ) = @_;
445 my $dbh = C4::Context->dbh;
447 my $sth = $dbh->prepare("SELECT COUNT(*) as inCollection FROM collections_tracking WHERE itemnumber = ? AND colId = ?");
448 $sth->execute( $itemnumber, $colId ) or return( 0 );
450 my $row = $sth->fetchrow_hashref;
452 return $$row{'inCollection'};
455 =item isItemInAnyCollection
456 $inCollection = isItemInAnyCollection( $itemnumber );
457 =cut
458 sub isItemInAnyCollection {
459 my ( $itemnumber ) = @_;
461 my $dbh = C4::Context->dbh;
463 my $sth = $dbh->prepare("SELECT itemnumber FROM collections_tracking WHERE itemnumber = ?");
464 $sth->execute( $itemnumber ) or return( 0 );
466 my $row = $sth->fetchrow_hashref;
468 my $itemnumber = $$row{'itemnumber'};
469 $sth->finish;
471 if ( $itemnumber ) {
472 return 1;
473 } else {
474 return 0;
480 __END__
482 =back
484 =head1 AUTHOR
486 Kyle Hall <kylemhall@gmail.com>
488 =cut