Bug 7213 - simple /svc/ HTTP example
[koha.git] / C4 / RotatingCollections.pm
blobb5768680744659c3328502a137b8f1c50279f3be
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 =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() );
102 $sth->finish;
104 return 1;
108 =head2 UpdateCollection
110 ( $success, $errorcode, $errormessage ) = UpdateCollection( $colId, $title, $description );
112 Updates a collection
114 Input:
115 $colId: id of the collection to be updated
116 $title: short description of the club or service
117 $description: long description of the club or service
119 Output:
120 $success: 1 if all database operations were successful, 0 otherwise
121 $errorCode: Code for reason of failure, good for translating errors in templates
122 $errorMessage: English description of error
124 =cut
126 sub UpdateCollection {
127 my ( $colId, $title, $description ) = @_;
129 ## Check for all neccessary parameters
130 if ( ! $colId ) {
131 return ( 0, 1, "No Id Given" );
133 if ( ! $title ) {
134 return ( 0, 2, "No Title Given" );
136 if ( ! $description ) {
137 return ( 0, 3, "No Description Given" );
140 my $dbh = C4::Context->dbh;
142 my $sth;
143 $sth = $dbh->prepare("UPDATE collections
144 SET
145 colTitle = ?, colDesc = ?
146 WHERE colId = ?");
147 $sth->execute( $title, $description, $colId ) or return ( 0, 4, $sth->errstr() );
148 $sth->finish;
150 return 1;
154 =head2 DeleteCollection
156 ( $success, $errorcode, $errormessage ) = DeleteCollection( $colId );
157 Deletes a collection of the given id
159 Input:
160 $colId : id of the Archtype to be deleted
162 Output:
163 $success: 1 if all database operations were successful, 0 otherwise
164 $errorCode: Code for reason of failure, good for translating errors in templates
165 $errorMessage: English description of error
167 =cut
169 sub DeleteCollection {
170 my ( $colId ) = @_;
172 ## Paramter check
173 if ( ! $colId ) {
174 return ( 0, 1, "No Collection Id Given" );;
177 my $dbh = C4::Context->dbh;
179 my $sth;
181 $sth = $dbh->prepare("DELETE FROM collections WHERE colId = ?");
182 $sth->execute( $colId ) or return ( 0, 4, $sth->errstr() );
183 $sth->finish;
185 return 1;
188 =head2 GetCollections
190 $collections = GetCollections();
191 Returns data about all collections
193 Output:
194 On Success:
195 $results: Reference to an array of associated arrays
196 On Failure:
197 $errorCode: Code for reason of failure, good for translating errors in templates
198 $errorMessage: English description of error
200 =cut
202 sub GetCollections {
204 my $dbh = C4::Context->dbh;
206 my $sth = $dbh->prepare("SELECT * FROM collections");
207 $sth->execute() or return ( 1, $sth->errstr() );
209 my @results;
210 while ( my $row = $sth->fetchrow_hashref ) {
211 push( @results , $row );
214 $sth->finish;
216 return \@results;
219 =head2 GetItemsInCollection
221 ( $results, $success, $errorcode, $errormessage ) = GetItemsInCollection( $colId );
223 Returns information about the items in the given collection
225 Input:
226 $colId: The id of the collection
228 Output:
229 $results: Reference to an array of associated arrays
230 $success: 1 if all database operations were successful, 0 otherwise
231 $errorCode: Code for reason of failure, good for translating errors in templates
232 $errorMessage: English description of error
234 =cut
236 sub GetItemsInCollection {
237 my ( $colId ) = @_;
239 ## Paramter check
240 if ( ! $colId ) {
241 return ( 0, 0, 1, "No Collection Id Given" );;
244 my $dbh = C4::Context->dbh;
246 my $sth = $dbh->prepare("SELECT
247 biblio.title,
248 items.itemcallnumber,
249 items.barcode
250 FROM collections, collections_tracking, items, biblio
251 WHERE collections.colId = collections_tracking.colId
252 AND collections_tracking.itemnumber = items.itemnumber
253 AND items.biblionumber = biblio.biblionumber
254 AND collections.colId = ? ORDER BY biblio.title");
255 $sth->execute( $colId ) or return ( 0, 0, 2, $sth->errstr() );
257 my @results;
258 while ( my $row = $sth->fetchrow_hashref ) {
259 push( @results , $row );
262 $sth->finish;
264 return \@results;
267 =head2 GetCollection
269 ( $colId, $colTitle, $colDesc, $colBranchcode ) = GetCollection( $colId );
271 Returns information about a collection
273 Input:
274 $colId: Id of the collection
275 Output:
276 $colId, $colTitle, $colDesc, $colBranchcode
278 =cut
280 sub GetCollection {
281 my ( $colId ) = @_;
283 my $dbh = C4::Context->dbh;
285 my ( $sth, @results );
286 $sth = $dbh->prepare("SELECT * FROM collections WHERE colId = ?");
287 $sth->execute( $colId ) or return 0;
289 my $row = $sth->fetchrow_hashref;
291 $sth->finish;
293 return (
294 $$row{'colId'},
295 $$row{'colTitle'},
296 $$row{'colDesc'},
297 $$row{'colBranchcode'}
302 =head2 AddItemToCollection
304 ( $success, $errorcode, $errormessage ) = AddItemToCollection( $colId, $itemnumber );
306 Adds an item to a rotating collection.
308 Input:
309 $colId: Collection to add the item to.
310 $itemnumber: Item to be added to the collection
311 Output:
312 $success: 1 if all database operations were successful, 0 otherwise
313 $errorCode: Code for reason of failure, good for translating errors in templates
314 $errorMessage: English description of error
316 =cut
318 sub AddItemToCollection {
319 my ( $colId, $itemnumber ) = @_;
321 ## Check for all neccessary parameters
322 if ( ! $colId ) {
323 return ( 0, 1, "No Collection Given" );
325 if ( ! $itemnumber ) {
326 return ( 0, 2, "No Itemnumber Given" );
329 if ( isItemInThisCollection( $itemnumber, $colId ) ) {
330 return ( 0, 2, "Item is already in the collection!" );
331 } elsif ( isItemInAnyCollection( $itemnumber ) ) {
332 return ( 0, 3, "Item is already in a different collection!" );
335 my $dbh = C4::Context->dbh;
337 my $sth;
338 $sth = $dbh->prepare("INSERT INTO collections_tracking ( ctId, colId, itemnumber )
339 VALUES ( NULL, ?, ? )");
340 $sth->execute( $colId, $itemnumber ) or return ( 0, 3, $sth->errstr() );
341 $sth->finish;
343 return 1;
347 =head2 RemoveItemFromCollection
349 ( $success, $errorcode, $errormessage ) = RemoveItemFromCollection( $colId, $itemnumber );
351 Removes an item to a collection
353 Input:
354 $colId: Collection to add the item to.
355 $itemnumber: Item to be removed from collection
357 Output:
358 $success: 1 if all database operations were successful, 0 otherwise
359 $errorCode: Code for reason of failure, good for translating errors in templates
360 $errorMessage: English description of error
362 =cut
364 sub RemoveItemFromCollection {
365 my ( $colId, $itemnumber ) = @_;
367 ## Check for all neccessary parameters
368 if ( ! $itemnumber ) {
369 return ( 0, 2, "No Itemnumber Given" );
372 if ( ! isItemInThisCollection( $itemnumber, $colId ) ) {
373 return ( 0, 2, "Item is not in the collection!" );
376 my $dbh = C4::Context->dbh;
378 my $sth;
379 $sth = $dbh->prepare("DELETE FROM collections_tracking
380 WHERE itemnumber = ?");
381 $sth->execute( $itemnumber ) or return ( 0, 3, $sth->errstr() );
382 $sth->finish;
384 return 1;
387 =head2 TransferCollection
389 ( $success, $errorcode, $errormessage ) = TransferCollection( $colId, $colBranchcode );
391 Transfers a collection to another branch
393 Input:
394 $colId: id of the collection to be updated
395 $colBranchcode: branch where collection is moving to
397 Output:
398 $success: 1 if all database operations were successful, 0 otherwise
399 $errorCode: Code for reason of failure, good for translating errors in templates
400 $errorMessage: English description of error
402 =cut
404 sub TransferCollection {
405 my ( $colId, $colBranchcode ) = @_;
407 ## Check for all neccessary parameters
408 if ( ! $colId ) {
409 return ( 0, 1, "No Id Given" );
411 if ( ! $colBranchcode ) {
412 return ( 0, 2, "No Branchcode Given" );
415 my $dbh = C4::Context->dbh;
417 my $sth;
418 $sth = $dbh->prepare("UPDATE collections
419 SET
420 colBranchcode = ?
421 WHERE colId = ?");
422 $sth->execute( $colBranchcode, $colId ) or return ( 0, 4, $sth->errstr() );
423 $sth->finish;
425 $sth = $dbh->prepare("SELECT barcode FROM items, collections_tracking
426 WHERE items.itemnumber = collections_tracking.itemnumber
427 AND collections_tracking.colId = ?");
428 $sth->execute( $colId ) or return ( 0, 4, $sth->errstr );
429 my @results;
430 while ( my $item = $sth->fetchrow_hashref ) {
431 my ( $dotransfer, $messages, $iteminformation ) = transferbook( $colBranchcode, $item->{'barcode'}, my $ignore_reserves = 1);
436 return 1;
440 =head2 GetCollectionItemBranches
442 my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches( $itemnumber );
444 =cut
446 sub GetCollectionItemBranches {
447 my ( $itemnumber ) = @_;
449 if ( ! $itemnumber ) {
450 return;
453 my $dbh = C4::Context->dbh;
455 my ( $sth, @results );
456 $sth = $dbh->prepare("SELECT holdingbranch, colBranchcode FROM items, collections, collections_tracking
457 WHERE items.itemnumber = collections_tracking.itemnumber
458 AND collections.colId = collections_tracking.colId
459 AND items.itemnumber = ?");
460 $sth->execute( $itemnumber );
462 my $row = $sth->fetchrow_hashref;
464 $sth->finish;
466 return (
467 $$row{'holdingbranch'},
468 $$row{'colBranchcode'},
472 =head2 isItemInThisCollection
474 $inCollection = isItemInThisCollection( $itemnumber, $colId );
476 =cut
478 sub isItemInThisCollection {
479 my ( $itemnumber, $colId ) = @_;
481 my $dbh = C4::Context->dbh;
483 my $sth = $dbh->prepare("SELECT COUNT(*) as inCollection FROM collections_tracking WHERE itemnumber = ? AND colId = ?");
484 $sth->execute( $itemnumber, $colId ) or return( 0 );
486 my $row = $sth->fetchrow_hashref;
488 return $$row{'inCollection'};
491 =head2 isItemInAnyCollection
493 $inCollection = isItemInAnyCollection( $itemnumber );
495 =cut
497 sub isItemInAnyCollection {
498 my ( $itemnumber ) = @_;
500 my $dbh = C4::Context->dbh;
502 my $sth = $dbh->prepare("SELECT itemnumber FROM collections_tracking WHERE itemnumber = ?");
503 $sth->execute( $itemnumber ) or return( 0 );
505 my $row = $sth->fetchrow_hashref;
507 $itemnumber = $row->{itemnumber};
508 $sth->finish;
510 if ( $itemnumber ) {
511 return 1;
512 } else {
513 return 0;
519 __END__
521 =head1 AUTHOR
523 Kyle Hall <kylemhall@gmail.com>
525 =cut