Bug 8836 [QA Followup] - Allow collection description to be optional
[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 Modern::Perl;
26
27 use C4::Context;
28 use C4::Circulation;
29 use C4::Reserves qw(CheckReserves);
30
31 use DBI;
32
33 use Data::Dumper;
34
35 use vars qw($VERSION @ISA @EXPORT);
36
37 # set the version for version checking
38 $VERSION = 3.07.00.049;
39
40 =head1 NAME
41
42 C4::RotatingCollections - Functions for managing rotating collections
43
44 =head1 FUNCTIONS
45
46 =cut
47
48 BEGIN {
49     require Exporter;
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
69 =head2  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
82 =cut
83
84 sub CreateCollection {
85     my ( $title, $description ) = @_;
86
87     ## Check for all neccessary parameters
88     if ( !$title ) {
89         return ( 0, 1, "NO_TITLE" );
90     }
91
92     $description ||= q{};
93
94     my $success = 1;
95
96     my $dbh = C4::Context->dbh;
97
98     my $sth;
99     $sth = $dbh->prepare(
100         "INSERT INTO collections ( colId, colTitle, colDesc )
101                         VALUES ( NULL, ?, ? )"
102     );
103     $sth->execute( $title, $description ) or return ( 0, 3, $sth->errstr() );
104
105     return 1;
106
107 }
108
109 =head2 UpdateCollection
110
111  ( $success, $errorcode, $errormessage ) = UpdateCollection( $colId, $title, $description );
112
113 Updates a collection
114
115  Input:
116    $colId: id of the collection to be updated
117    $title: short description of the club or service
118    $description: long description of the club or service
119
120  Output:
121    $success: 1 if all database operations were successful, 0 otherwise
122    $errorCode: Code for reason of failure, good for translating errors in templates
123    $errorMessage: English description of error
124
125 =cut
126
127 sub UpdateCollection {
128     my ( $colId, $title, $description ) = @_;
129
130     ## Check for all neccessary parameters
131     if ( !$colId ) {
132         return ( 0, 1, "NO_ID" );
133     }
134     if ( !$title ) {
135         return ( 0, 2, "NO_TITLE" );
136     }
137
138     my $dbh = C4::Context->dbh;
139
140     $description ||= q{};
141
142     my $sth;
143     $sth = $dbh->prepare(
144         "UPDATE collections
145                         SET 
146                         colTitle = ?, colDesc = ? 
147                         WHERE colId = ?"
148     );
149     $sth->execute( $title, $description, $colId )
150       or return ( 0, 4, $sth->errstr() );
151
152     return 1;
153
154 }
155
156 =head2 DeleteCollection
157
158  ( $success, $errorcode, $errormessage ) = DeleteCollection( $colId );
159  Deletes a collection of the given id
160
161  Input:
162    $colId : id of the Archtype to be deleted
163
164  Output:
165    $success: 1 if all database operations were successful, 0 otherwise
166    $errorCode: Code for reason of failure, good for translating errors in templates
167    $errorMessage: English description of error
168
169 =cut
170
171 sub DeleteCollection {
172     my ($colId) = @_;
173
174     ## Paramter check
175     if ( !$colId ) {
176         return ( 0, 1, "NO_ID" );
177     }
178
179     my $dbh = C4::Context->dbh;
180
181     my $sth;
182
183     $sth = $dbh->prepare("DELETE FROM collections WHERE colId = ?");
184     $sth->execute($colId) or return ( 0, 4, $sth->errstr() );
185
186     return 1;
187 }
188
189 =head2 GetCollections
190
191  $collections = GetCollections();
192  Returns data about all collections
193
194  Output:
195   On Success:
196    $results: Reference to an array of associated arrays
197   On Failure:
198    $errorCode: Code for reason of failure, good for translating errors in templates
199    $errorMessage: English description of error
200
201 =cut
202
203 sub GetCollections {
204
205     my $dbh = C4::Context->dbh;
206
207     my $sth = $dbh->prepare("SELECT * FROM collections");
208     $sth->execute() or return ( 1, $sth->errstr() );
209
210     my @results;
211     while ( my $row = $sth->fetchrow_hashref ) {
212         push( @results, $row );
213     }
214
215     return \@results;
216 }
217
218 =head2 GetItemsInCollection
219
220  ( $results, $success, $errorcode, $errormessage ) = GetItemsInCollection( $colId );
221
222  Returns information about the items in the given collection
223  
224  Input:
225    $colId: The id of the collection
226
227  Output:
228    $results: Reference to an array of associated arrays
229    $success: 1 if all database operations were successful, 0 otherwise
230    $errorCode: Code for reason of failure, good for translating errors in templates
231    $errorMessage: English description of error
232
233 =cut
234
235 sub GetItemsInCollection {
236     my ($colId) = @_;
237
238     ## Paramter check
239     if ( !$colId ) {
240         return ( 0, 0, 1, "NO_ID" );
241     }
242
243     my $dbh = C4::Context->dbh;
244
245     my $sth = $dbh->prepare(
246         "SELECT
247                              biblio.title,
248                              biblio.biblionumber,
249                              items.itemcallnumber,
250                              items.barcode
251                            FROM collections, collections_tracking, items, biblio
252                            WHERE collections.colId = collections_tracking.colId
253                            AND collections_tracking.itemnumber = items.itemnumber
254                            AND items.biblionumber = biblio.biblionumber
255                            AND collections.colId = ? ORDER BY biblio.title"
256     );
257     $sth->execute($colId) or return ( 0, 0, 2, $sth->errstr() );
258
259     my @results;
260     while ( my $row = $sth->fetchrow_hashref ) {
261         push( @results, $row );
262     }
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     return (
292         $$row{'colId'},   $$row{'colTitle'},
293         $$row{'colDesc'}, $$row{'colBranchcode'}
294     );
295
296 }
297
298 =head2 AddItemToCollection
299
300  ( $success, $errorcode, $errormessage ) = AddItemToCollection( $colId, $itemnumber );
301
302 Adds an item to a rotating collection.
303
304  Input:
305    $colId: Collection to add the item to.
306    $itemnumber: Item to be added to the collection
307  Output:
308    $success: 1 if all database operations were successful, 0 otherwise
309    $errorCode: Code for reason of failure, good for translating errors in templates
310    $errorMessage: English description of error
311
312 =cut
313
314 sub AddItemToCollection {
315     my ( $colId, $itemnumber ) = @_;
316
317     ## Check for all neccessary parameters
318     if ( !$colId ) {
319         return ( 0, 1, "NO_ID" );
320     }
321     if ( !$itemnumber ) {
322         return ( 0, 2, "NO_ITEM" );
323     }
324
325     if ( isItemInThisCollection( $itemnumber, $colId ) ) {
326         return ( 0, 2, "IN_COLLECTION" );
327     }
328     elsif ( isItemInAnyCollection($itemnumber) ) {
329         return ( 0, 3, "IN_COLLECTION_OTHER" );
330     }
331
332     my $dbh = C4::Context->dbh;
333
334     my $sth;
335     $sth = $dbh->prepare("
336         INSERT INTO collections_tracking (
337             colId,
338             itemnumber
339         ) VALUES ( ?, ? )
340     ");
341     $sth->execute( $colId, $itemnumber ) or return ( 0, 3, $sth->errstr() );
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_ITEM" );
370     }
371
372     if ( !isItemInThisCollection( $itemnumber, $colId ) ) {
373         return ( 0, 2, "NOT_IN_COLLECTION" );
374     }
375
376     my $dbh = C4::Context->dbh;
377
378     my $sth;
379     $sth = $dbh->prepare(
380         "DELETE FROM collections_tracking
381                         WHERE itemnumber = ?"
382     );
383     $sth->execute($itemnumber) or return ( 0, 3, $sth->errstr() );
384
385     return 1;
386 }
387
388 =head2 TransferCollection
389
390  ( $success, $errorcode, $errormessage ) = TransferCollection( $colId, $colBranchcode );
391
392 Transfers a collection to another branch
393
394  Input:
395    $colId: id of the collection to be updated
396    $colBranchcode: branch where collection is moving to
397
398  Output:
399    $success: 1 if all database operations were successful, 0 otherwise
400    $errorCode: Code for reason of failure, good for translating errors in templates
401    $errorMessage: English description of error
402
403 =cut
404
405 sub TransferCollection {
406     my ( $colId, $colBranchcode ) = @_;
407
408     ## Check for all neccessary parameters
409     if ( !$colId ) {
410         return ( 0, 1, "NO_ID" );
411     }
412     if ( !$colBranchcode ) {
413         return ( 0, 2, "NO_BRANCHCODE" );
414     }
415
416     my $dbh = C4::Context->dbh;
417
418     my $sth;
419     $sth = $dbh->prepare(
420         "UPDATE collections
421                         SET 
422                         colBranchcode = ? 
423                         WHERE colId = ?"
424     );
425     $sth->execute( $colBranchcode, $colId ) or return ( 0, 4, $sth->errstr() );
426
427     $sth = $dbh->prepare(q{
428         SELECT items.itemnumber, items.barcode FROM collections_tracking
429         LEFT JOIN items ON collections_tracking.itemnumber = items.itemnumber
430         LEFT JOIN issues ON items.itemnumber = issues.itemnumber
431         WHERE issues.borrowernumber IS NULL
432           AND collections_tracking.colId = ?
433     });
434     $sth->execute($colId) or return ( 0, 4, $sth->errstr );
435     my @results;
436     while ( my $item = $sth->fetchrow_hashref ) {
437         my ($status) = CheckReserves( $item->{itemnumber} );
438         transferbook( $colBranchcode, $item->{barcode}, my $ignore_reserves = 1 ) unless ( $status eq 'Waiting' );
439     }
440
441     return 1;
442
443 }
444
445 =head2 GetCollectionItemBranches
446
447   my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches( $itemnumber );
448
449 =cut
450
451 sub GetCollectionItemBranches {
452     my ($itemnumber) = @_;
453
454     if ( !$itemnumber ) {
455         return;
456     }
457
458     my $dbh = C4::Context->dbh;
459
460     my ( $sth, @results );
461     $sth = $dbh->prepare(
462 "SELECT holdingbranch, colBranchcode FROM items, collections, collections_tracking
463                         WHERE items.itemnumber = collections_tracking.itemnumber
464                         AND collections.colId = collections_tracking.colId
465                         AND items.itemnumber = ?"
466     );
467     $sth->execute($itemnumber);
468
469     my $row = $sth->fetchrow_hashref;
470
471     return ( $$row{'holdingbranch'}, $$row{'colBranchcode'}, );
472 }
473
474 =head2 isItemInThisCollection
475
476   $inCollection = isItemInThisCollection( $itemnumber, $colId );
477
478 =cut
479
480 sub isItemInThisCollection {
481     my ( $itemnumber, $colId ) = @_;
482
483     my $dbh = C4::Context->dbh;
484
485     my $sth = $dbh->prepare(
486 "SELECT COUNT(*) as inCollection FROM collections_tracking WHERE itemnumber = ? AND colId = ?"
487     );
488     $sth->execute( $itemnumber, $colId ) or return (0);
489
490     my $row = $sth->fetchrow_hashref;
491
492     return $$row{'inCollection'};
493 }
494
495 =head2 isItemInAnyCollection
496
497 $inCollection = isItemInAnyCollection( $itemnumber );
498
499 =cut
500
501 sub isItemInAnyCollection {
502     my ($itemnumber) = @_;
503
504     my $dbh = C4::Context->dbh;
505
506     my $sth = $dbh->prepare(
507         "SELECT itemnumber FROM collections_tracking WHERE itemnumber = ?");
508     $sth->execute($itemnumber) or return (0);
509
510     my $row = $sth->fetchrow_hashref;
511
512     $itemnumber = $row->{itemnumber};
513     if ($itemnumber) {
514         return 1;
515     }
516     else {
517         return 0;
518     }
519 }
520
521 1;
522
523 __END__
524
525 =head1 AUTHOR
526
527 Kyle Hall <kylemhall@gmail.com>
528
529 =cut