Bug 16011: $VERSION - Remove the $VERSION init
[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
13 # under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 3 of the License, or
15 # (at your option) any later version.
16 #
17 # Koha is distributed in the hope that it will be useful, but
18 # WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
21 #
22 # You should have received a copy of the GNU General Public License
23 # along with Koha; if not, see <http://www.gnu.org/licenses>.
24
25 use Modern::Perl;
26
27 use C4::Context;
28 use C4::Circulation;
29 use C4::Reserves qw(CheckReserves);
30 use Koha::Database;
31
32 use DBI;
33
34 use Data::Dumper;
35
36 use vars qw(@ISA @EXPORT);
37
38 # set the version for version checking
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     my $schema = Koha::Database->new()->schema();
88     my $duplicate_titles = $schema->resultset('Collection')->count({ colTitle => $title });
89
90     ## Check for all necessary parameters
91     if ( !$title ) {
92         return ( 0, 1, "NO_TITLE" );
93     } elsif ( $duplicate_titles ) {
94         return ( 0, 2, "DUPLICATE_TITLE" );
95     }
96
97     $description ||= q{};
98
99     my $success = 1;
100
101     my $dbh = C4::Context->dbh;
102
103     my $sth;
104     $sth = $dbh->prepare(
105         "INSERT INTO collections ( colId, colTitle, colDesc )
106                         VALUES ( NULL, ?, ? )"
107     );
108     $sth->execute( $title, $description ) or return ( 0, 3, $sth->errstr() );
109
110     return 1;
111
112 }
113
114 =head2 UpdateCollection
115
116  ( $success, $errorcode, $errormessage ) = UpdateCollection( $colId, $title, $description );
117
118 Updates a collection
119
120  Input:
121    $colId: id of the collection to be updated
122    $title: short description of the club or service
123    $description: long description of the club or service
124
125  Output:
126    $success: 1 if all database operations were successful, 0 otherwise
127    $errorCode: Code for reason of failure, good for translating errors in templates
128    $errorMessage: English description of error
129
130 =cut
131
132 sub UpdateCollection {
133     my ( $colId, $title, $description ) = @_;
134
135     my $schema = Koha::Database->new()->schema();
136     my $duplicate_titles = $schema->resultset('Collection')->count({ colTitle => $title,  -not => { colId => $colId } });
137
138     ## Check for all necessary parameters
139     if ( !$colId ) {
140         return ( 0, 1, "NO_ID" );
141     }
142     if ( !$title ) {
143         return ( 0, 2, "NO_TITLE" );
144     }
145     if ( $duplicate_titles ) {
146         return ( 0, 3, "DUPLICATE_TITLE" );
147     }
148
149     my $dbh = C4::Context->dbh;
150
151     $description ||= q{};
152
153     my $sth;
154     $sth = $dbh->prepare(
155         "UPDATE collections
156                         SET 
157                         colTitle = ?, colDesc = ? 
158                         WHERE colId = ?"
159     );
160     $sth->execute( $title, $description, $colId )
161       or return ( 0, 4, $sth->errstr() );
162
163     return 1;
164
165 }
166
167 =head2 DeleteCollection
168
169  ( $success, $errorcode, $errormessage ) = DeleteCollection( $colId );
170  Deletes a collection of the given id
171
172  Input:
173    $colId : id of the Archetype to be deleted
174
175  Output:
176    $success: 1 if all database operations were successful, 0 otherwise
177    $errorCode: Code for reason of failure, good for translating errors in templates
178    $errorMessage: English description of error
179
180 =cut
181
182 sub DeleteCollection {
183     my ($colId) = @_;
184
185     ## Parameter check
186     if ( !$colId ) {
187         return ( 0, 1, "NO_ID" );
188     }
189
190     my $dbh = C4::Context->dbh;
191
192     my $sth;
193
194     $sth = $dbh->prepare("DELETE FROM collections WHERE colId = ?");
195     $sth->execute($colId) or return ( 0, 4, $sth->errstr() );
196
197     return 1;
198 }
199
200 =head2 GetCollections
201
202  $collections = GetCollections();
203  Returns data about all collections
204
205  Output:
206   On Success:
207    $results: Reference to an array of associated arrays
208   On Failure:
209    $errorCode: Code for reason of failure, good for translating errors in templates
210    $errorMessage: English description of error
211
212 =cut
213
214 sub GetCollections {
215
216     my $dbh = C4::Context->dbh;
217
218     my $sth = $dbh->prepare("SELECT * FROM collections");
219     $sth->execute() or return ( 1, $sth->errstr() );
220
221     my @results;
222     while ( my $row = $sth->fetchrow_hashref ) {
223         push( @results, $row );
224     }
225
226     return \@results;
227 }
228
229 =head2 GetItemsInCollection
230
231  ( $results, $success, $errorcode, $errormessage ) = GetItemsInCollection( $colId );
232
233  Returns information about the items in the given collection
234  
235  Input:
236    $colId: The id of the collection
237
238  Output:
239    $results: Reference to an array of associated arrays
240    $success: 1 if all database operations were successful, 0 otherwise
241    $errorCode: Code for reason of failure, good for translating errors in templates
242    $errorMessage: English description of error
243
244 =cut
245
246 sub GetItemsInCollection {
247     my ($colId) = @_;
248
249     ## Parameter check
250     if ( !$colId ) {
251         return ( 0, 0, 1, "NO_ID" );
252     }
253
254     my $dbh = C4::Context->dbh;
255
256     my $sth = $dbh->prepare(
257         "SELECT
258                              biblio.title,
259                              biblio.biblionumber,
260                              items.itemcallnumber,
261                              items.barcode
262                            FROM collections, collections_tracking, items, biblio
263                            WHERE collections.colId = collections_tracking.colId
264                            AND collections_tracking.itemnumber = items.itemnumber
265                            AND items.biblionumber = biblio.biblionumber
266                            AND collections.colId = ? ORDER BY biblio.title"
267     );
268     $sth->execute($colId) or return ( 0, 0, 2, $sth->errstr() );
269
270     my @results;
271     while ( my $row = $sth->fetchrow_hashref ) {
272         push( @results, $row );
273     }
274
275     return \@results;
276 }
277
278 =head2 GetCollection
279
280  ( $colId, $colTitle, $colDesc, $colBranchcode ) = GetCollection( $colId );
281
282 Returns information about a collection
283
284  Input:
285    $colId: Id of the collection
286  Output:
287    $colId, $colTitle, $colDesc, $colBranchcode
288
289 =cut
290
291 sub GetCollection {
292     my ($colId) = @_;
293
294     my $dbh = C4::Context->dbh;
295
296     my ( $sth, @results );
297     $sth = $dbh->prepare("SELECT * FROM collections WHERE colId = ?");
298     $sth->execute($colId) or return 0;
299
300     my $row = $sth->fetchrow_hashref;
301
302     return (
303         $$row{'colId'},   $$row{'colTitle'},
304         $$row{'colDesc'}, $$row{'colBranchcode'}
305     );
306
307 }
308
309 =head2 AddItemToCollection
310
311  ( $success, $errorcode, $errormessage ) = AddItemToCollection( $colId, $itemnumber );
312
313 Adds an item to a rotating collection.
314
315  Input:
316    $colId: Collection to add the item to.
317    $itemnumber: Item to be added to the collection
318  Output:
319    $success: 1 if all database operations were successful, 0 otherwise
320    $errorCode: Code for reason of failure, good for translating errors in templates
321    $errorMessage: English description of error
322
323 =cut
324
325 sub AddItemToCollection {
326     my ( $colId, $itemnumber ) = @_;
327
328     ## Check for all necessary parameters
329     if ( !$colId ) {
330         return ( 0, 1, "NO_ID" );
331     }
332     if ( !$itemnumber ) {
333         return ( 0, 2, "NO_ITEM" );
334     }
335
336     if ( isItemInThisCollection( $itemnumber, $colId ) ) {
337         return ( 0, 2, "IN_COLLECTION" );
338     }
339     elsif ( isItemInAnyCollection($itemnumber) ) {
340         return ( 0, 3, "IN_COLLECTION_OTHER" );
341     }
342
343     my $dbh = C4::Context->dbh;
344
345     my $sth;
346     $sth = $dbh->prepare("
347         INSERT INTO collections_tracking (
348             colId,
349             itemnumber
350         ) VALUES ( ?, ? )
351     ");
352     $sth->execute( $colId, $itemnumber ) or return ( 0, 3, $sth->errstr() );
353
354     return 1;
355
356 }
357
358 =head2  RemoveItemFromCollection
359
360  ( $success, $errorcode, $errormessage ) = RemoveItemFromCollection( $colId, $itemnumber );
361
362 Removes an item to a collection
363
364  Input:
365    $colId: Collection to add the item to.
366    $itemnumber: Item to be removed from collection
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
373 =cut
374
375 sub RemoveItemFromCollection {
376     my ( $colId, $itemnumber ) = @_;
377
378     ## Check for all necessary parameters
379     if ( !$itemnumber ) {
380         return ( 0, 2, "NO_ITEM" );
381     }
382
383     if ( !isItemInThisCollection( $itemnumber, $colId ) ) {
384         return ( 0, 2, "NOT_IN_COLLECTION" );
385     }
386
387     my $dbh = C4::Context->dbh;
388
389     my $sth;
390     $sth = $dbh->prepare(
391         "DELETE FROM collections_tracking
392                         WHERE itemnumber = ?"
393     );
394     $sth->execute($itemnumber) or return ( 0, 3, $sth->errstr() );
395
396     return 1;
397 }
398
399 =head2 TransferCollection
400
401  ( $success, $errorcode, $errormessage ) = TransferCollection( $colId, $colBranchcode );
402
403 Transfers a collection to another branch
404
405  Input:
406    $colId: id of the collection to be updated
407    $colBranchcode: branch where collection is moving to
408
409  Output:
410    $success: 1 if all database operations were successful, 0 otherwise
411    $errorCode: Code for reason of failure, good for translating errors in templates
412    $errorMessage: English description of error
413
414 =cut
415
416 sub TransferCollection {
417     my ( $colId, $colBranchcode ) = @_;
418
419     ## Check for all necessary parameters
420     if ( !$colId ) {
421         return ( 0, 1, "NO_ID" );
422     }
423     if ( !$colBranchcode ) {
424         return ( 0, 2, "NO_BRANCHCODE" );
425     }
426
427     my $dbh = C4::Context->dbh;
428
429     my $sth;
430     $sth = $dbh->prepare(
431         "UPDATE collections
432                         SET 
433                         colBranchcode = ? 
434                         WHERE colId = ?"
435     );
436     $sth->execute( $colBranchcode, $colId ) or return ( 0, 4, $sth->errstr() );
437
438     $sth = $dbh->prepare(q{
439         SELECT items.itemnumber, items.barcode FROM collections_tracking
440         LEFT JOIN items ON collections_tracking.itemnumber = items.itemnumber
441         LEFT JOIN issues ON items.itemnumber = issues.itemnumber
442         WHERE issues.borrowernumber IS NULL
443           AND collections_tracking.colId = ?
444     });
445     $sth->execute($colId) or return ( 0, 4, $sth->errstr );
446     my @results;
447     while ( my $item = $sth->fetchrow_hashref ) {
448         my ($status) = CheckReserves( $item->{itemnumber} );
449         my @transfers = C4::Circulation::GetTransfers( $item->{itemnumber} );
450         C4::Circulation::transferbook( $colBranchcode, $item->{barcode}, my $ignore_reserves = 1 ) unless ( $status eq 'Waiting' || @transfers );
451     }
452
453     return 1;
454
455 }
456
457 =head2 GetCollectionItemBranches
458
459   my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches( $itemnumber );
460
461 =cut
462
463 sub GetCollectionItemBranches {
464     my ($itemnumber) = @_;
465
466     if ( !$itemnumber ) {
467         return;
468     }
469
470     my $dbh = C4::Context->dbh;
471
472     my ( $sth, @results );
473     $sth = $dbh->prepare(
474 "SELECT holdingbranch, colBranchcode FROM items, collections, collections_tracking
475                         WHERE items.itemnumber = collections_tracking.itemnumber
476                         AND collections.colId = collections_tracking.colId
477                         AND items.itemnumber = ?"
478     );
479     $sth->execute($itemnumber);
480
481     my $row = $sth->fetchrow_hashref;
482
483     return ( $$row{'holdingbranch'}, $$row{'colBranchcode'}, );
484 }
485
486 =head2 isItemInThisCollection
487
488   $inCollection = isItemInThisCollection( $itemnumber, $colId );
489
490 =cut
491
492 sub isItemInThisCollection {
493     my ( $itemnumber, $colId ) = @_;
494
495     my $dbh = C4::Context->dbh;
496
497     my $sth = $dbh->prepare(
498 "SELECT COUNT(*) as inCollection FROM collections_tracking WHERE itemnumber = ? AND colId = ?"
499     );
500     $sth->execute( $itemnumber, $colId ) or return (0);
501
502     my $row = $sth->fetchrow_hashref;
503
504     return $$row{'inCollection'};
505 }
506
507 =head2 isItemInAnyCollection
508
509 $inCollection = isItemInAnyCollection( $itemnumber );
510
511 =cut
512
513 sub isItemInAnyCollection {
514     my ($itemnumber) = @_;
515
516     my $dbh = C4::Context->dbh;
517
518     my $sth = $dbh->prepare(
519         "SELECT itemnumber FROM collections_tracking WHERE itemnumber = ?");
520     $sth->execute($itemnumber) or return (0);
521
522     my $row = $sth->fetchrow_hashref;
523
524     $itemnumber = $row->{itemnumber};
525     if ($itemnumber) {
526         return 1;
527     }
528     else {
529         return 0;
530     }
531 }
532
533 1;
534
535 __END__
536
537 =head1 AUTHOR
538
539 Kyle Hall <kylemhall@gmail.com>
540
541 =cut