Bug 4333 Fix erroneous message re Active Currency
[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 = 0.01;
41
42 =head1 NAME
43
44 C4::RotatingCollections - Functions for managing rotating collections
45
46 =head1 FUNCTIONS
47
48 =over 2
49
50 =cut
51
52 @ISA = qw( Exporter );
53 @EXPORT = qw( 
54   CreateCollection
55   UpdateCollection
56   DeleteCollection
57   
58   GetItemsInCollection
59
60   GetCollection
61   GetCollections
62   
63   AddItemToCollection
64   RemoveItemFromCollection
65   TransferCollection  
66
67   GetCollectionItemBranches
68 );
69
70 =item  CreateCollection
71  ( $success, $errorcode, $errormessage ) = CreateCollection( $title, $description );
72  Creates a new collection
73
74  Input:
75    $title: short description of the club or service
76    $description: long description of the club or service
77
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 ) = @_;
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   $sth->finish;
103
104   return 1;
105   
106 }
107
108 =item UpdateCollection
109  ( $success, $errorcode, $errormessage ) = UpdateCollection( $colId, $title, $description );
110  Updates a collection
111
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
116
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 ) = @_;
124
125   ## Check for all neccessary parameters
126   if ( ! $colId ) {
127     return ( 0, 1, "No Id Given" );
128   }
129   if ( ! $title ) {
130     return ( 0, 2, "No Title Given" );
131   } 
132   if ( ! $description ) {
133     return ( 0, 3, "No Description Given" );
134   } 
135
136   my $dbh = C4::Context->dbh;
137
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;
145   
146   return 1;
147   
148 }
149
150 =item DeleteCollection
151  ( $success, $errorcode, $errormessage ) = DeleteCollection( $colId );
152  Deletes a collection of the given id
153
154  Input:
155    $colId : id of the Archtype to be deleted
156
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 ) = @_;
164
165   ## Paramter check
166   if ( ! $colId ) {
167     return ( 0, 1, "No Collection Id Given" );;
168   }
169   
170   my $dbh = C4::Context->dbh;
171
172   my $sth;
173
174   $sth = $dbh->prepare("DELETE FROM collections WHERE colId = ?");
175   $sth->execute( $colId ) or return ( 0, 4, $sth->errstr() );
176   $sth->finish;
177
178   return 1;
179 }
180
181 =item GetCollections
182  $collections = GetCollections();
183  Returns data about all collections
184
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 {
193
194   my $dbh = C4::Context->dbh;
195   
196   my $sth = $dbh->prepare("SELECT * FROM collections");
197   $sth->execute() or return ( 1, $sth->errstr() );
198   
199   my @results;
200   while ( my $row = $sth->fetchrow_hashref ) {
201     push( @results , $row );
202   }
203   
204   $sth->finish;
205   
206   return \@results;
207 }
208
209 =item GetItemsInCollection
210  ( $results, $success, $errorcode, $errormessage ) = GetItemsInCollection( $colId );
211  Returns information about the items in the given collection
212  
213  Input:
214    $colId: The id of the collection
215
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 ) = @_;
224
225   ## Paramter check
226   if ( ! $colId ) {
227     return ( 0, 0, 1, "No Collection Id Given" );;
228   }
229
230   my $dbh = C4::Context->dbh;
231   
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() );
242   
243   my @results;
244   while ( my $row = $sth->fetchrow_hashref ) {
245     push( @results , $row );
246   }
247   
248   $sth->finish;
249   
250   return \@results;
251 }
252
253 =item GetCollection
254  ( $colId, $colTitle, $colDesc, $colBranchcode ) = GetCollection( $colId );
255  Returns information about a collection
256
257  Input:
258    $colId: Id of the collection
259  Output:
260    $colId, $colTitle, $colDesc, $colBranchcode
261 =cut
262 sub GetCollection {
263   my ( $colId ) = @_;
264
265   my $dbh = C4::Context->dbh;
266
267   my ( $sth, @results );
268   $sth = $dbh->prepare("SELECT * FROM collections WHERE colId = ?");
269   $sth->execute( $colId ) or return 0;
270     
271   my $row = $sth->fetchrow_hashref;
272   
273   $sth->finish;
274   
275   return (
276       $$row{'colId'},
277       $$row{'colTitle'},
278       $$row{'colDesc'},
279       $$row{'colBranchcode'}
280   );
281     
282 }
283
284 =item AddItemToCollection
285  ( $success, $errorcode, $errormessage ) = AddItemToCollection( $colId, $itemnumber );
286  Adds an item to a rotating collection.
287
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 ) = @_;
298
299   ## Check for all neccessary parameters
300   if ( ! $colId ) {
301     return ( 0, 1, "No Collection Given" );
302   } 
303   if ( ! $itemnumber ) {
304     return ( 0, 2, "No Itemnumber Given" );
305   } 
306   
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!" );
311   }
312
313   my $dbh = C4::Context->dbh;
314
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;
320
321   return 1;
322   
323 }
324
325 =item  RemoveItemFromCollection
326  ( $success, $errorcode, $errormessage ) = RemoveItemFromCollection( $colId, $itemnumber );
327  Removes an item to a collection
328
329  Input:
330    $colId: Collection to add the item to.
331    $itemnumber: Item to be removed from collection
332
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 ) = @_;
340
341   ## Check for all neccessary parameters
342   if ( ! $itemnumber ) {
343     return ( 0, 2, "No Itemnumber Given" );
344   } 
345   
346   if ( ! isItemInThisCollection( $itemnumber, $colId ) ) {
347     return ( 0, 2, "Item is not in the collection!" );
348   } 
349
350   my $dbh = C4::Context->dbh;
351
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;
357
358   return 1;
359 }
360
361 =item TransferCollection
362  ( $success, $errorcode, $errormessage ) = TransferCollection( $colId, $colBranchcode );
363  Transfers a collection to another branch
364
365  Input:
366    $colId: id of the collection to be updated
367    $colBranchcode: branch where collection is moving to
368
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 ) = @_;
376
377   ## Check for all neccessary parameters
378   if ( ! $colId ) {
379     return ( 0, 1, "No Id Given" );
380   }
381   if ( ! $colBranchcode ) {
382     return ( 0, 2, "No Branchcode Given" );
383   } 
384
385   my $dbh = C4::Context->dbh;
386
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;
394   
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);
402   }
403   
404
405   
406   return 1;
407   
408 }
409
410 =item GetCollectionItemBranches
411  my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches( $itemnumber );
412 =cut
413 sub GetCollectionItemBranches {
414   my ( $itemnumber ) = @_;
415
416   if ( ! $itemnumber ) {
417     return;
418   }
419
420   my $dbh = C4::Context->dbh;
421
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 );
428     
429   my $row = $sth->fetchrow_hashref;
430   
431   $sth->finish;
432   
433   return (
434       $$row{'holdingbranch'},
435       $$row{'colBranchcode'},
436   );  
437 }
438
439 =item isItemInThisCollection
440 $inCollection = isItemInThisCollection( $itemnumber, $colId );
441 =cut            
442 sub isItemInThisCollection {
443   my ( $itemnumber, $colId ) = @_;
444   
445   my $dbh = C4::Context->dbh;
446   
447   my $sth = $dbh->prepare("SELECT COUNT(*) as inCollection FROM collections_tracking WHERE itemnumber = ? AND colId = ?");
448   $sth->execute( $itemnumber, $colId ) or return( 0 );
449       
450   my $row = $sth->fetchrow_hashref;
451         
452   return $$row{'inCollection'};
453 }
454
455 =item isItemInAnyCollection
456 $inCollection = isItemInAnyCollection( $itemnumber );
457 =cut
458 sub isItemInAnyCollection {
459   my ( $itemnumber ) = @_;
460   
461   my $dbh = C4::Context->dbh;
462   
463   my $sth = $dbh->prepare("SELECT itemnumber FROM collections_tracking WHERE itemnumber = ?");
464   $sth->execute( $itemnumber ) or return( 0 );
465       
466   my $row = $sth->fetchrow_hashref;
467         
468   my $itemnumber = $$row{'itemnumber'};
469   $sth->finish;
470             
471   if ( $itemnumber ) {
472     return 1;
473   } else {
474     return 0;
475   }
476 }
477
478 1;
479
480 __END__
481
482 =back
483
484 =head1 AUTHOR
485
486 Kyle Hall <kylemhall@gmail.com>
487
488 =cut