bug 3651 followup: updated for new GetMember() parameter style
[koha.git] / C4 / RotatingCollections.pm
1 package C4::RotatingCollections;
2
3 # $Id: RotatingCollections.pm,v 0.1 2007/04/20 kylemhall 
4
5 # This package is inteded to keep track of what library
6 # Items of a certain collection should be at. 
7
8 # Copyright 2007 Kyle Hall
9 #
10 # This file is part of Koha.
11 #
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.
16 #
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.
20 #
21 # You should have received a copy of the GNU General Public License along with
22 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
23 # Suite 330, Boston, MA  02111-1307 USA
24
25 use strict;
26
27 require Exporter;
28
29 use C4::Context;
30 use C4::Circulation;
31
32 use DBI;
33
34 use Data::Dumper;
35
36 use vars qw($VERSION @ISA @EXPORT);
37
38 # set the version for version checking
39 $VERSION = 0.01;
40
41 =head1 NAME
42
43 C4::RotatingCollections - Functions for managing rotating collections
44
45 =head1 FUNCTIONS
46
47 =over 2
48
49 =cut
50
51 @ISA = qw( Exporter );
52 @EXPORT = qw( 
53   CreateCollection
54   UpdateCollection
55   DeleteCollection
56   
57   GetItemsInCollection
58
59   GetCollection
60   GetCollections
61   
62   AddItemToCollection
63   RemoveItemFromCollection
64   TransferCollection  
65
66   GetCollectionItemBranches
67 );
68
69 =item  CreateCollection
70  ( $success, $errorcode, $errormessage ) = CreateCollection( $title, $description );
71  Creates a new collection
72
73  Input:
74    $title: short description of the club or service
75    $description: long description of the club or service
76
77  Output:
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
81 =cut
82 sub CreateCollection {
83   my ( $title, $description ) = @_;
84
85   ## Check for all neccessary parameters
86   if ( ! $title ) {
87     return ( 0, 1, "No Title Given" );
88   } 
89   if ( ! $description ) {
90     return ( 0, 2, "No Description Given" );
91   } 
92
93   my $success = 1;
94
95   my $dbh = C4::Context->dbh;
96
97   my $sth;
98   $sth = $dbh->prepare("INSERT INTO collections ( colId, colTitle, colDesc ) 
99                         VALUES ( NULL, ?, ? )");
100   $sth->execute( $title, $description ) or return ( 0, 3, $sth->errstr() );
101   $sth->finish;
102
103   return 1;
104   
105 }
106
107 =item UpdateCollection
108  ( $success, $errorcode, $errormessage ) = UpdateCollection( $colId, $title, $description );
109  Updates a collection
110
111  Input:
112    $colId: id of the collection to be updated
113    $title: short description of the club or service
114    $description: long description of the club or service
115
116  Output:
117    $success: 1 if all database operations were successful, 0 otherwise
118    $errorCode: Code for reason of failure, good for translating errors in templates
119    $errorMessage: English description of error
120 =cut
121 sub UpdateCollection {
122   my ( $colId, $title, $description ) = @_;
123
124   ## Check for all neccessary parameters
125   if ( ! $colId ) {
126     return ( 0, 1, "No Id Given" );
127   }
128   if ( ! $title ) {
129     return ( 0, 2, "No Title Given" );
130   } 
131   if ( ! $description ) {
132     return ( 0, 3, "No Description Given" );
133   } 
134
135   my $dbh = C4::Context->dbh;
136
137   my $sth;
138   $sth = $dbh->prepare("UPDATE collections
139                         SET 
140                         colTitle = ?, colDesc = ? 
141                         WHERE colId = ?");
142   $sth->execute( $title, $description, $colId ) or return ( 0, 4, $sth->errstr() );
143   $sth->finish;
144   
145   return 1;
146   
147 }
148
149 =item DeleteCollection
150  ( $success, $errorcode, $errormessage ) = DeleteCollection( $colId );
151  Deletes a collection of the given id
152
153  Input:
154    $colId : id of the Archtype to be deleted
155
156  Output:
157    $success: 1 if all database operations were successful, 0 otherwise
158    $errorCode: Code for reason of failure, good for translating errors in templates
159    $errorMessage: English description of error
160 =cut
161 sub DeleteCollection {
162   my ( $colId ) = @_;
163
164   ## Paramter check
165   if ( ! $colId ) {
166     return ( 0, 1, "No Collection Id Given" );;
167   }
168   
169   my $dbh = C4::Context->dbh;
170
171   my $sth;
172
173   $sth = $dbh->prepare("DELETE FROM collections WHERE colId = ?");
174   $sth->execute( $colId ) or return ( 0, 4, $sth->errstr() );
175   $sth->finish;
176
177   return 1;
178 }
179
180 =item GetCollections
181  $collections = GetCollections();
182  Returns data about all collections
183
184  Output:
185   On Success:
186    $results: Reference to an array of associated arrays
187   On Failure:
188    $errorCode: Code for reason of failure, good for translating errors in templates
189    $errorMessage: English description of error
190 =cut
191 sub GetCollections {
192
193   my $dbh = C4::Context->dbh;
194   
195   my $sth = $dbh->prepare("SELECT * FROM collections");
196   $sth->execute() or return ( 1, $sth->errstr() );
197   
198   my @results;
199   while ( my $row = $sth->fetchrow_hashref ) {
200     push( @results , $row );
201   }
202   
203   $sth->finish;
204   
205   return \@results;
206 }
207
208 =item GetItemsInCollection
209  ( $results, $success, $errorcode, $errormessage ) = GetItemsInCollection( $colId );
210  Returns information about the items in the given collection
211  
212  Input:
213    $colId: The id of the collection
214
215  Output:
216    $results: Reference to an array of associated arrays
217    $success: 1 if all database operations were successful, 0 otherwise
218    $errorCode: Code for reason of failure, good for translating errors in templates
219    $errorMessage: English description of error
220 =cut
221 sub GetItemsInCollection {
222   my ( $colId ) = @_;
223
224   ## Paramter check
225   if ( ! $colId ) {
226     return ( 0, 0, 1, "No Collection Id Given" );;
227   }
228
229   my $dbh = C4::Context->dbh;
230   
231   my $sth = $dbh->prepare("SELECT 
232                              biblio.title,
233                              items.itemcallnumber,
234                              items.barcode
235                            FROM collections, collections_tracking, items, biblio
236                            WHERE collections.colId = collections_tracking.colId
237                            AND collections_tracking.itemnumber = items.itemnumber
238                            AND items.biblionumber = biblio.biblionumber
239                            AND collections.colId = ? ORDER BY biblio.title");
240   $sth->execute( $colId ) or return ( 0, 0, 2, $sth->errstr() );
241   
242   my @results;
243   while ( my $row = $sth->fetchrow_hashref ) {
244     push( @results , $row );
245   }
246   
247   $sth->finish;
248   
249   return \@results;
250 }
251
252 =item GetCollection
253  ( $colId, $colTitle, $colDesc, $colBranchcode ) = GetCollection( $colId );
254  Returns information about a collection
255
256  Input:
257    $colId: Id of the collection
258  Output:
259    $colId, $colTitle, $colDesc, $colBranchcode
260 =cut
261 sub GetCollection {
262   my ( $colId ) = @_;
263
264   my $dbh = C4::Context->dbh;
265
266   my ( $sth, @results );
267   $sth = $dbh->prepare("SELECT * FROM collections WHERE colId = ?");
268   $sth->execute( $colId ) or return 0;
269     
270   my $row = $sth->fetchrow_hashref;
271   
272   $sth->finish;
273   
274   return (
275       $$row{'colId'},
276       $$row{'colTitle'},
277       $$row{'colDesc'},
278       $$row{'colBranchcode'}
279   );
280     
281 }
282
283 =item AddItemToCollection
284  ( $success, $errorcode, $errormessage ) = AddItemToCollection( $colId, $itemnumber );
285  Adds an item to a rotating collection.
286
287  Input:
288    $colId: Collection to add the item to.
289    $itemnumber: Item to be added to the collection
290  Output:
291    $success: 1 if all database operations were successful, 0 otherwise
292    $errorCode: Code for reason of failure, good for translating errors in templates
293    $errorMessage: English description of error
294 =cut
295 sub AddItemToCollection {
296   my ( $colId, $itemnumber ) = @_;
297
298   ## Check for all neccessary parameters
299   if ( ! $colId ) {
300     return ( 0, 1, "No Collection Given" );
301   } 
302   if ( ! $itemnumber ) {
303     return ( 0, 2, "No Itemnumber Given" );
304   } 
305   
306   if ( isItemInThisCollection( $itemnumber, $colId ) ) {
307     return ( 0, 2, "Item is already in the collection!" );
308   } elsif ( isItemInAnyCollection( $itemnumber ) ) {
309     return ( 0, 3, "Item is already in a different collection!" );
310   }
311
312   my $dbh = C4::Context->dbh;
313
314   my $sth;
315   $sth = $dbh->prepare("INSERT INTO collections_tracking ( ctId, colId, itemnumber ) 
316                         VALUES ( NULL, ?, ? )");
317   $sth->execute( $colId, $itemnumber ) or return ( 0, 3, $sth->errstr() );
318   $sth->finish;
319
320   return 1;
321   
322 }
323
324 =item  RemoveItemFromCollection
325  ( $success, $errorcode, $errormessage ) = RemoveItemFromCollection( $colId, $itemnumber );
326  Removes an item to a collection
327
328  Input:
329    $colId: Collection to add the item to.
330    $itemnumber: Item to be removed from collection
331
332  Output:
333    $success: 1 if all database operations were successful, 0 otherwise
334    $errorCode: Code for reason of failure, good for translating errors in templates
335    $errorMessage: English description of error
336 =cut
337 sub RemoveItemFromCollection {
338   my ( $colId, $itemnumber ) = @_;
339
340   ## Check for all neccessary parameters
341   if ( ! $itemnumber ) {
342     return ( 0, 2, "No Itemnumber Given" );
343   } 
344   
345   if ( ! isItemInThisCollection( $itemnumber, $colId ) ) {
346     return ( 0, 2, "Item is not in the collection!" );
347   } 
348
349   my $dbh = C4::Context->dbh;
350
351   my $sth;
352   $sth = $dbh->prepare("DELETE FROM collections_tracking 
353                         WHERE itemnumber = ?");
354   $sth->execute( $itemnumber ) or return ( 0, 3, $sth->errstr() );
355   $sth->finish;
356
357   return 1;
358 }
359
360 =item TransferCollection
361  ( $success, $errorcode, $errormessage ) = TransferCollection( $colId, $colBranchcode );
362  Transfers a collection to another branch
363
364  Input:
365    $colId: id of the collection to be updated
366    $colBranchcode: branch where collection is moving to
367
368  Output:
369    $success: 1 if all database operations were successful, 0 otherwise
370    $errorCode: Code for reason of failure, good for translating errors in templates
371    $errorMessage: English description of error
372 =cut
373 sub TransferCollection {
374   my ( $colId, $colBranchcode ) = @_;
375
376   ## Check for all neccessary parameters
377   if ( ! $colId ) {
378     return ( 0, 1, "No Id Given" );
379   }
380   if ( ! $colBranchcode ) {
381     return ( 0, 2, "No Branchcode Given" );
382   } 
383
384   my $dbh = C4::Context->dbh;
385
386   my $sth;
387   $sth = $dbh->prepare("UPDATE collections
388                         SET 
389                         colBranchcode = ? 
390                         WHERE colId = ?");
391   $sth->execute( $colBranchcode, $colId ) or return ( 0, 4, $sth->errstr() );
392   $sth->finish;
393   
394   $sth = $dbh->prepare("SELECT barcode FROM items, collections_tracking 
395                         WHERE items.itemnumber = collections_tracking.itemnumber
396                         AND collections_tracking.colId = ?");
397   $sth->execute( $colId ) or return ( 0, 4, $sth->errstr );
398   my @results;
399   while ( my $item = $sth->fetchrow_hashref ) {
400     my ( $dotransfer, $messages, $iteminformation ) = transferbook( $colBranchcode, $item->{'barcode'}, my $ignore_reserves = 1);
401   }
402   
403
404   
405   return 1;
406   
407 }
408
409 =item GetCollectionItemBranches
410  my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches( $itemnumber );
411 =cut
412 sub GetCollectionItemBranches {
413   my ( $itemnumber ) = @_;
414
415   if ( ! $itemnumber ) {
416     return;
417   }
418
419   my $dbh = C4::Context->dbh;
420
421   my ( $sth, @results );
422   $sth = $dbh->prepare("SELECT holdingbranch, colBranchcode FROM items, collections, collections_tracking 
423                         WHERE items.itemnumber = collections_tracking.itemnumber
424                         AND collections.colId = collections_tracking.colId
425                         AND items.itemnumber = ?");
426   $sth->execute( $itemnumber );
427     
428   my $row = $sth->fetchrow_hashref;
429   
430   $sth->finish;
431   
432   return (
433       $$row{'holdingbranch'},
434       $$row{'colBranchcode'},
435   );  
436 }
437
438 =item isItemInThisCollection
439 $inCollection = isItemInThisCollection( $itemnumber, $colId );
440 =cut            
441 sub isItemInThisCollection {
442   my ( $itemnumber, $colId ) = @_;
443   
444   my $dbh = C4::Context->dbh;
445   
446   my $sth = $dbh->prepare("SELECT COUNT(*) as inCollection FROM collections_tracking WHERE itemnumber = ? AND colId = ?");
447   $sth->execute( $itemnumber, $colId ) or return( 0 );
448       
449   my $row = $sth->fetchrow_hashref;
450         
451   return $$row{'inCollection'};
452 }
453
454 =item isItemInAnyCollection
455 $inCollection = isItemInAnyCollection( $itemnumber );
456 =cut
457 sub isItemInAnyCollection {
458   my ( $itemnumber ) = @_;
459   
460   my $dbh = C4::Context->dbh;
461   
462   my $sth = $dbh->prepare("SELECT itemnumber FROM collections_tracking WHERE itemnumber = ?");
463   $sth->execute( $itemnumber ) or return( 0 );
464       
465   my $row = $sth->fetchrow_hashref;
466         
467   my $itemnumber = $$row{'itemnumber'};
468   $sth->finish;
469             
470   if ( $itemnumber ) {
471     return 1;
472   } else {
473     return 0;
474   }
475 }
476
477 1;
478
479 __END__
480
481 =back
482
483 =head1 AUTHOR
484
485 Kyle Hall <kylemhall@gmail.com>
486
487 =cut