Merge remote-tracking branch 'origin/new/bug_7143'
[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 =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   $sth->finish;
103
104   return 1;
105  
106 }
107
108 =head2 UpdateCollection
109
110  ( $success, $errorcode, $errormessage ) = UpdateCollection( $colId, $title, $description );
111
112 Updates a collection
113
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
118
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
123
124 =cut
125
126 sub UpdateCollection {
127   my ( $colId, $title, $description ) = @_;
128
129   ## Check for all neccessary parameters
130   if ( ! $colId ) {
131     return ( 0, 1, "No Id Given" );
132   }
133   if ( ! $title ) {
134     return ( 0, 2, "No Title Given" );
135   } 
136   if ( ! $description ) {
137     return ( 0, 3, "No Description Given" );
138   } 
139
140   my $dbh = C4::Context->dbh;
141
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;
149   
150   return 1;
151   
152 }
153
154 =head2 DeleteCollection
155
156  ( $success, $errorcode, $errormessage ) = DeleteCollection( $colId );
157  Deletes a collection of the given id
158
159  Input:
160    $colId : id of the Archtype to be deleted
161
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
166
167 =cut
168
169 sub DeleteCollection {
170   my ( $colId ) = @_;
171
172   ## Paramter check
173   if ( ! $colId ) {
174     return ( 0, 1, "No Collection Id Given" );;
175   }
176   
177   my $dbh = C4::Context->dbh;
178
179   my $sth;
180
181   $sth = $dbh->prepare("DELETE FROM collections WHERE colId = ?");
182   $sth->execute( $colId ) or return ( 0, 4, $sth->errstr() );
183   $sth->finish;
184
185   return 1;
186 }
187
188 =head2 GetCollections
189
190  $collections = GetCollections();
191  Returns data about all collections
192
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
199
200 =cut
201
202 sub GetCollections {
203
204   my $dbh = C4::Context->dbh;
205   
206   my $sth = $dbh->prepare("SELECT * FROM collections");
207   $sth->execute() or return ( 1, $sth->errstr() );
208   
209   my @results;
210   while ( my $row = $sth->fetchrow_hashref ) {
211     push( @results , $row );
212   }
213   
214   $sth->finish;
215   
216   return \@results;
217 }
218
219 =head2 GetItemsInCollection
220
221  ( $results, $success, $errorcode, $errormessage ) = GetItemsInCollection( $colId );
222
223  Returns information about the items in the given collection
224  
225  Input:
226    $colId: The id of the collection
227
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
233
234 =cut
235
236 sub GetItemsInCollection {
237   my ( $colId ) = @_;
238
239   ## Paramter check
240   if ( ! $colId ) {
241     return ( 0, 0, 1, "No Collection Id Given" );;
242   }
243
244   my $dbh = C4::Context->dbh;
245   
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() );
256   
257   my @results;
258   while ( my $row = $sth->fetchrow_hashref ) {
259     push( @results , $row );
260   }
261   
262   $sth->finish;
263   
264   return \@results;
265 }
266
267 =head2 GetCollection
268
269  ( $colId, $colTitle, $colDesc, $colBranchcode ) = GetCollection( $colId );
270
271 Returns information about a collection
272
273  Input:
274    $colId: Id of the collection
275  Output:
276    $colId, $colTitle, $colDesc, $colBranchcode
277
278 =cut
279
280 sub GetCollection {
281   my ( $colId ) = @_;
282
283   my $dbh = C4::Context->dbh;
284
285   my ( $sth, @results );
286   $sth = $dbh->prepare("SELECT * FROM collections WHERE colId = ?");
287   $sth->execute( $colId ) or return 0;
288     
289   my $row = $sth->fetchrow_hashref;
290   
291   $sth->finish;
292   
293   return (
294       $$row{'colId'},
295       $$row{'colTitle'},
296       $$row{'colDesc'},
297       $$row{'colBranchcode'}
298   );
299     
300 }
301
302 =head2 AddItemToCollection
303
304  ( $success, $errorcode, $errormessage ) = AddItemToCollection( $colId, $itemnumber );
305
306 Adds an item to a rotating collection.
307
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
315
316 =cut
317
318 sub AddItemToCollection {
319   my ( $colId, $itemnumber ) = @_;
320
321   ## Check for all neccessary parameters
322   if ( ! $colId ) {
323     return ( 0, 1, "No Collection Given" );
324   } 
325   if ( ! $itemnumber ) {
326     return ( 0, 2, "No Itemnumber Given" );
327   } 
328   
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!" );
333   }
334
335   my $dbh = C4::Context->dbh;
336
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;
342
343   return 1;
344   
345 }
346
347 =head2  RemoveItemFromCollection
348
349  ( $success, $errorcode, $errormessage ) = RemoveItemFromCollection( $colId, $itemnumber );
350
351 Removes an item to a collection
352
353  Input:
354    $colId: Collection to add the item to.
355    $itemnumber: Item to be removed from collection
356
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
361
362 =cut
363
364 sub RemoveItemFromCollection {
365   my ( $colId, $itemnumber ) = @_;
366
367   ## Check for all neccessary parameters
368   if ( ! $itemnumber ) {
369     return ( 0, 2, "No Itemnumber Given" );
370   } 
371   
372   if ( ! isItemInThisCollection( $itemnumber, $colId ) ) {
373     return ( 0, 2, "Item is not in the collection!" );
374   } 
375
376   my $dbh = C4::Context->dbh;
377
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;
383
384   return 1;
385 }
386
387 =head2 TransferCollection
388
389  ( $success, $errorcode, $errormessage ) = TransferCollection( $colId, $colBranchcode );
390
391 Transfers a collection to another branch
392
393  Input:
394    $colId: id of the collection to be updated
395    $colBranchcode: branch where collection is moving to
396
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
401
402 =cut
403
404 sub TransferCollection {
405   my ( $colId, $colBranchcode ) = @_;
406
407   ## Check for all neccessary parameters
408   if ( ! $colId ) {
409     return ( 0, 1, "No Id Given" );
410   }
411   if ( ! $colBranchcode ) {
412     return ( 0, 2, "No Branchcode Given" );
413   } 
414
415   my $dbh = C4::Context->dbh;
416
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;
424   
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);
432   }
433   
434
435   
436   return 1;
437   
438 }
439
440 =head2 GetCollectionItemBranches
441
442   my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches( $itemnumber );
443
444 =cut
445
446 sub GetCollectionItemBranches {
447   my ( $itemnumber ) = @_;
448
449   if ( ! $itemnumber ) {
450     return;
451   }
452
453   my $dbh = C4::Context->dbh;
454
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 );
461     
462   my $row = $sth->fetchrow_hashref;
463   
464   $sth->finish;
465   
466   return (
467       $$row{'holdingbranch'},
468       $$row{'colBranchcode'},
469   );  
470 }
471
472 =head2 isItemInThisCollection
473
474   $inCollection = isItemInThisCollection( $itemnumber, $colId );
475
476 =cut
477
478 sub isItemInThisCollection {
479   my ( $itemnumber, $colId ) = @_;
480   
481   my $dbh = C4::Context->dbh;
482   
483   my $sth = $dbh->prepare("SELECT COUNT(*) as inCollection FROM collections_tracking WHERE itemnumber = ? AND colId = ?");
484   $sth->execute( $itemnumber, $colId ) or return( 0 );
485       
486   my $row = $sth->fetchrow_hashref;
487         
488   return $$row{'inCollection'};
489 }
490
491 =head2 isItemInAnyCollection
492
493 $inCollection = isItemInAnyCollection( $itemnumber );
494
495 =cut
496
497 sub isItemInAnyCollection {
498   my ( $itemnumber ) = @_;
499   
500   my $dbh = C4::Context->dbh;
501   
502   my $sth = $dbh->prepare("SELECT itemnumber FROM collections_tracking WHERE itemnumber = ?");
503   $sth->execute( $itemnumber ) or return( 0 );
504       
505   my $row = $sth->fetchrow_hashref;
506         
507   $itemnumber = $row->{itemnumber};
508   $sth->finish;
509             
510   if ( $itemnumber ) {
511     return 1;
512   } else {
513     return 0;
514   }
515 }
516
517 1;
518
519 __END__
520
521 =head1 AUTHOR
522
523 Kyle Hall <kylemhall@gmail.com>
524
525 =cut