Bug 11629: (follow-up) Add message for librarian that status was updated
[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
22 # with Koha; if not, write to the Free Software Foundation, Inc.,
23 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24
25 use strict;
26 #use warnings; FIXME - Bug 2505
27
28 require Exporter;
29
30 use C4::Context;
31 use C4::Circulation;
32
33 use DBI;
34
35 use Data::Dumper;
36
37 use vars qw($VERSION @ISA @EXPORT);
38
39 # set the version for version checking
40 $VERSION = 3.07.00.049;
41
42 =head1 NAME
43
44 C4::RotatingCollections - Functions for managing rotating collections
45
46 =head1 FUNCTIONS
47
48 =cut
49
50 @ISA = qw( Exporter );
51 @EXPORT = qw( 
52   CreateCollection
53   UpdateCollection
54   DeleteCollection
55   
56   GetItemsInCollection
57
58   GetCollection
59   GetCollections
60   
61   AddItemToCollection
62   RemoveItemFromCollection
63   TransferCollection  
64
65   GetCollectionItemBranches
66 );
67
68 =head2  CreateCollection
69  ( $success, $errorcode, $errormessage ) = CreateCollection( $title, $description );
70  Creates a new collection
71
72  Input:
73    $title: short description of the club or service
74    $description: long description of the club or service
75
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
80
81 =cut
82
83 sub CreateCollection {
84   my ( $title, $description ) = @_;
85
86   ## Check for all neccessary parameters
87   if ( ! $title ) {
88     return ( 0, 1, "No Title Given" );
89   } 
90   if ( ! $description ) {
91     return ( 0, 2, "No Description Given" );
92   } 
93
94   my $success = 1;
95
96   my $dbh = C4::Context->dbh;
97
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
103   return 1;
104  
105 }
106
107 =head2 UpdateCollection
108
109  ( $success, $errorcode, $errormessage ) = UpdateCollection( $colId, $title, $description );
110
111 Updates a collection
112
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
117
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
122
123 =cut
124
125 sub UpdateCollection {
126   my ( $colId, $title, $description ) = @_;
127
128   ## Check for all neccessary parameters
129   if ( ! $colId ) {
130     return ( 0, 1, "No Id Given" );
131   }
132   if ( ! $title ) {
133     return ( 0, 2, "No Title Given" );
134   } 
135   if ( ! $description ) {
136     return ( 0, 3, "No Description Given" );
137   } 
138
139   my $dbh = C4::Context->dbh;
140
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() );
147   
148   return 1;
149   
150 }
151
152 =head2 DeleteCollection
153
154  ( $success, $errorcode, $errormessage ) = DeleteCollection( $colId );
155  Deletes a collection of the given id
156
157  Input:
158    $colId : id of the Archtype to be deleted
159
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
164
165 =cut
166
167 sub DeleteCollection {
168   my ( $colId ) = @_;
169
170   ## Paramter check
171   if ( ! $colId ) {
172     return ( 0, 1, "No Collection Id Given" );;
173   }
174   
175   my $dbh = C4::Context->dbh;
176
177   my $sth;
178
179   $sth = $dbh->prepare("DELETE FROM collections WHERE colId = ?");
180   $sth->execute( $colId ) or return ( 0, 4, $sth->errstr() );
181
182   return 1;
183 }
184
185 =head2 GetCollections
186
187  $collections = GetCollections();
188  Returns data about all collections
189
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
196
197 =cut
198
199 sub GetCollections {
200
201   my $dbh = C4::Context->dbh;
202   
203   my $sth = $dbh->prepare("SELECT * FROM collections");
204   $sth->execute() or return ( 1, $sth->errstr() );
205   
206   my @results;
207   while ( my $row = $sth->fetchrow_hashref ) {
208     push( @results , $row );
209   }
210   
211   return \@results;
212 }
213
214 =head2 GetItemsInCollection
215
216  ( $results, $success, $errorcode, $errormessage ) = GetItemsInCollection( $colId );
217
218  Returns information about the items in the given collection
219  
220  Input:
221    $colId: The id of the collection
222
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
228
229 =cut
230
231 sub GetItemsInCollection {
232   my ( $colId ) = @_;
233
234   ## Paramter check
235   if ( ! $colId ) {
236     return ( 0, 0, 1, "No Collection Id Given" );;
237   }
238
239   my $dbh = C4::Context->dbh;
240   
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() );
251   
252   my @results;
253   while ( my $row = $sth->fetchrow_hashref ) {
254     push( @results , $row );
255   }
256   
257   return \@results;
258 }
259
260 =head2 GetCollection
261
262  ( $colId, $colTitle, $colDesc, $colBranchcode ) = GetCollection( $colId );
263
264 Returns information about a collection
265
266  Input:
267    $colId: Id of the collection
268  Output:
269    $colId, $colTitle, $colDesc, $colBranchcode
270
271 =cut
272
273 sub GetCollection {
274   my ( $colId ) = @_;
275
276   my $dbh = C4::Context->dbh;
277
278   my ( $sth, @results );
279   $sth = $dbh->prepare("SELECT * FROM collections WHERE colId = ?");
280   $sth->execute( $colId ) or return 0;
281     
282   my $row = $sth->fetchrow_hashref;
283   
284   return (
285       $$row{'colId'},
286       $$row{'colTitle'},
287       $$row{'colDesc'},
288       $$row{'colBranchcode'}
289   );
290     
291 }
292
293 =head2 AddItemToCollection
294
295  ( $success, $errorcode, $errormessage ) = AddItemToCollection( $colId, $itemnumber );
296
297 Adds an item to a rotating collection.
298
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
306
307 =cut
308
309 sub AddItemToCollection {
310   my ( $colId, $itemnumber ) = @_;
311
312   ## Check for all neccessary parameters
313   if ( ! $colId ) {
314     return ( 0, 1, "No Collection Given" );
315   } 
316   if ( ! $itemnumber ) {
317     return ( 0, 2, "No Itemnumber Given" );
318   } 
319   
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!" );
324   }
325
326   my $dbh = C4::Context->dbh;
327
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() );
332
333   return 1;
334   
335 }
336
337 =head2  RemoveItemFromCollection
338
339  ( $success, $errorcode, $errormessage ) = RemoveItemFromCollection( $colId, $itemnumber );
340
341 Removes an item to a collection
342
343  Input:
344    $colId: Collection to add the item to.
345    $itemnumber: Item to be removed from collection
346
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
351
352 =cut
353
354 sub RemoveItemFromCollection {
355   my ( $colId, $itemnumber ) = @_;
356
357   ## Check for all neccessary parameters
358   if ( ! $itemnumber ) {
359     return ( 0, 2, "No Itemnumber Given" );
360   } 
361   
362   if ( ! isItemInThisCollection( $itemnumber, $colId ) ) {
363     return ( 0, 2, "Item is not in the collection!" );
364   } 
365
366   my $dbh = C4::Context->dbh;
367
368   my $sth;
369   $sth = $dbh->prepare("DELETE FROM collections_tracking 
370                         WHERE itemnumber = ?");
371   $sth->execute( $itemnumber ) or return ( 0, 3, $sth->errstr() );
372
373   return 1;
374 }
375
376 =head2 TransferCollection
377
378  ( $success, $errorcode, $errormessage ) = TransferCollection( $colId, $colBranchcode );
379
380 Transfers a collection to another branch
381
382  Input:
383    $colId: id of the collection to be updated
384    $colBranchcode: branch where collection is moving to
385
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
390
391 =cut
392
393 sub TransferCollection {
394   my ( $colId, $colBranchcode ) = @_;
395
396   ## Check for all neccessary parameters
397   if ( ! $colId ) {
398     return ( 0, 1, "No Id Given" );
399   }
400   if ( ! $colBranchcode ) {
401     return ( 0, 2, "No Branchcode Given" );
402   } 
403
404   my $dbh = C4::Context->dbh;
405
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() );
412   
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);
420   }
421
422   return 1;
423   
424 }
425
426 =head2 GetCollectionItemBranches
427
428   my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches( $itemnumber );
429
430 =cut
431
432 sub GetCollectionItemBranches {
433   my ( $itemnumber ) = @_;
434
435   if ( ! $itemnumber ) {
436     return;
437   }
438
439   my $dbh = C4::Context->dbh;
440
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 );
447     
448   my $row = $sth->fetchrow_hashref;
449   
450   return (
451       $$row{'holdingbranch'},
452       $$row{'colBranchcode'},
453   );  
454 }
455
456 =head2 isItemInThisCollection
457
458   $inCollection = isItemInThisCollection( $itemnumber, $colId );
459
460 =cut
461
462 sub isItemInThisCollection {
463   my ( $itemnumber, $colId ) = @_;
464   
465   my $dbh = C4::Context->dbh;
466   
467   my $sth = $dbh->prepare("SELECT COUNT(*) as inCollection FROM collections_tracking WHERE itemnumber = ? AND colId = ?");
468   $sth->execute( $itemnumber, $colId ) or return( 0 );
469       
470   my $row = $sth->fetchrow_hashref;
471         
472   return $$row{'inCollection'};
473 }
474
475 =head2 isItemInAnyCollection
476
477 $inCollection = isItemInAnyCollection( $itemnumber );
478
479 =cut
480
481 sub isItemInAnyCollection {
482   my ( $itemnumber ) = @_;
483   
484   my $dbh = C4::Context->dbh;
485   
486   my $sth = $dbh->prepare("SELECT itemnumber FROM collections_tracking WHERE itemnumber = ?");
487   $sth->execute( $itemnumber ) or return( 0 );
488       
489   my $row = $sth->fetchrow_hashref;
490         
491   $itemnumber = $row->{itemnumber};
492   if ( $itemnumber ) {
493     return 1;
494   } else {
495     return 0;
496   }
497 }
498
499 1;
500
501 __END__
502
503 =head1 AUTHOR
504
505 Kyle Hall <kylemhall@gmail.com>
506
507 =cut