File Coverage

File:C4/RotatingCollections.pm
Coverage:10.8%

linestmtbrancondsubtimecode
1package 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
3
3
3
21890
20
128
use strict;
26#use warnings; FIXME - Bug 2505
27
28require Exporter;
29
30
3
3
3
230
23
59
use C4::Context;
31
3
3
3
250
4
905
use C4::Circulation;
32
33
3
3
3
17
4
627
use DBI;
34
35
3
3
3
15
4
200
use Data::Dumper;
36
37
3
3
3
13
3
5405
use vars qw($VERSION @ISA @EXPORT);
38
39# set the version for version checking
40$VERSION = 0.01;
41
42 - 48
=head1 NAME

C4::RotatingCollections - Functions for managing rotating collections

=head1 FUNCTIONS

=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 - 81
=head2  CreateCollection
 ( $success, $errorcode, $errormessage ) = CreateCollection( $title, $description );
 Creates a new collection

 Input:
   $title: short description of the club or service
   $description: long description of the club or service

 Output:
   $success: 1 if all database operations were successful, 0 otherwise
   $errorCode: Code for reason of failure, good for translating errors in templates
   $errorMessage: English description of error

=cut
82
83sub CreateCollection {
84
0
  my ( $title, $description ) = @_;
85
86  ## Check for all neccessary parameters
87
0
  if ( ! $title ) {
88
0
    return ( 0, 1, "No Title Given" );
89  }
90
0
  if ( ! $description ) {
91
0
    return ( 0, 2, "No Description Given" );
92  }
93
94
0
  my $success = 1;
95
96
0
  my $dbh = C4::Context->dbh;
97
98
0
  my $sth;
99
0
  $sth = $dbh->prepare("INSERT INTO collections ( colId, colTitle, colDesc )
100                        VALUES ( NULL, ?, ? )");
101
0
  $sth->execute( $title, $description ) or return ( 0, 3, $sth->errstr() );
102
0
  $sth->finish;
103
104
0
  return 1;
105
106}
107
108 - 124
=head2 UpdateCollection

 ( $success, $errorcode, $errormessage ) = UpdateCollection( $colId, $title, $description );

Updates a collection

 Input:
   $colId: id of the collection to be updated
   $title: short description of the club or service
   $description: long description of the club or service

 Output:
   $success: 1 if all database operations were successful, 0 otherwise
   $errorCode: Code for reason of failure, good for translating errors in templates
   $errorMessage: English description of error

=cut
125
126sub UpdateCollection {
127
0
  my ( $colId, $title, $description ) = @_;
128
129  ## Check for all neccessary parameters
130
0
  if ( ! $colId ) {
131
0
    return ( 0, 1, "No Id Given" );
132  }
133
0
  if ( ! $title ) {
134
0
    return ( 0, 2, "No Title Given" );
135  }
136
0
  if ( ! $description ) {
137
0
    return ( 0, 3, "No Description Given" );
138  }
139
140
0
  my $dbh = C4::Context->dbh;
141
142
0
  my $sth;
143
0
  $sth = $dbh->prepare("UPDATE collections
144                        SET
145                        colTitle = ?, colDesc = ?
146                        WHERE colId = ?");
147
0
  $sth->execute( $title, $description, $colId ) or return ( 0, 4, $sth->errstr() );
148
0
  $sth->finish;
149
150
0
  return 1;
151
152}
153
154 - 167
=head2 DeleteCollection

 ( $success, $errorcode, $errormessage ) = DeleteCollection( $colId );
 Deletes a collection of the given id

 Input:
   $colId : id of the Archtype to be deleted

 Output:
   $success: 1 if all database operations were successful, 0 otherwise
   $errorCode: Code for reason of failure, good for translating errors in templates
   $errorMessage: English description of error

=cut
168
169sub DeleteCollection {
170
0
  my ( $colId ) = @_;
171
172  ## Paramter check
173
0
  if ( ! $colId ) {
174
0
    return ( 0, 1, "No Collection Id Given" );;
175  }
176
177
0
  my $dbh = C4::Context->dbh;
178
179
0
  my $sth;
180
181
0
  $sth = $dbh->prepare("DELETE FROM collections WHERE colId = ?");
182
0
  $sth->execute( $colId ) or return ( 0, 4, $sth->errstr() );
183
0
  $sth->finish;
184
185
0
  return 1;
186}
187
188 - 200
=head2 GetCollections

 $collections = GetCollections();
 Returns data about all collections

 Output:
  On Success:
   $results: Reference to an array of associated arrays
  On Failure:
   $errorCode: Code for reason of failure, good for translating errors in templates
   $errorMessage: English description of error

=cut
201
202sub GetCollections {
203
204
0
  my $dbh = C4::Context->dbh;
205
206
0
  my $sth = $dbh->prepare("SELECT * FROM collections");
207
0
  $sth->execute() or return ( 1, $sth->errstr() );
208
209
0
  my @results;
210
0
  while ( my $row = $sth->fetchrow_hashref ) {
211
0
    push( @results , $row );
212  }
213
214
0
  $sth->finish;
215
216
0
  return \@results;
217}
218
219 - 234
=head2 GetItemsInCollection

 ( $results, $success, $errorcode, $errormessage ) = GetItemsInCollection( $colId );

 Returns information about the items in the given collection
 
 Input:
   $colId: The id of the collection

 Output:
   $results: Reference to an array of associated arrays
   $success: 1 if all database operations were successful, 0 otherwise
   $errorCode: Code for reason of failure, good for translating errors in templates
   $errorMessage: English description of error

=cut
235
236sub GetItemsInCollection {
237
0
  my ( $colId ) = @_;
238
239  ## Paramter check
240
0
  if ( ! $colId ) {
241
0
    return ( 0, 0, 1, "No Collection Id Given" );;
242  }
243
244
0
  my $dbh = C4::Context->dbh;
245
246
0
  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
0
  $sth->execute( $colId ) or return ( 0, 0, 2, $sth->errstr() );
256
257
0
  my @results;
258
0
  while ( my $row = $sth->fetchrow_hashref ) {
259
0
    push( @results , $row );
260  }
261
262
0
  $sth->finish;
263
264
0
  return \@results;
265}
266
267 - 278
=head2 GetCollection

 ( $colId, $colTitle, $colDesc, $colBranchcode ) = GetCollection( $colId );

Returns information about a collection

 Input:
   $colId: Id of the collection
 Output:
   $colId, $colTitle, $colDesc, $colBranchcode

=cut
279
280sub GetCollection {
281
0
  my ( $colId ) = @_;
282
283
0
  my $dbh = C4::Context->dbh;
284
285
0
  my ( $sth, @results );
286
0
  $sth = $dbh->prepare("SELECT * FROM collections WHERE colId = ?");
287
0
  $sth->execute( $colId ) or return 0;
288
289
0
  my $row = $sth->fetchrow_hashref;
290
291
0
  $sth->finish;
292
293  return (
294
0
      $$row{'colId'},
295      $$row{'colTitle'},
296      $$row{'colDesc'},
297      $$row{'colBranchcode'}
298  );
299
300}
301
302 - 316
=head2 AddItemToCollection

 ( $success, $errorcode, $errormessage ) = AddItemToCollection( $colId, $itemnumber );

Adds an item to a rotating collection.

 Input:
   $colId: Collection to add the item to.
   $itemnumber: Item to be added to the collection
 Output:
   $success: 1 if all database operations were successful, 0 otherwise
   $errorCode: Code for reason of failure, good for translating errors in templates
   $errorMessage: English description of error

=cut
317
318sub AddItemToCollection {
319
0
  my ( $colId, $itemnumber ) = @_;
320
321  ## Check for all neccessary parameters
322
0
  if ( ! $colId ) {
323
0
    return ( 0, 1, "No Collection Given" );
324  }
325
0
  if ( ! $itemnumber ) {
326
0
    return ( 0, 2, "No Itemnumber Given" );
327  }
328
329
0
  if ( isItemInThisCollection( $itemnumber, $colId ) ) {
330
0
    return ( 0, 2, "Item is already in the collection!" );
331  } elsif ( isItemInAnyCollection( $itemnumber ) ) {
332
0
    return ( 0, 3, "Item is already in a different collection!" );
333  }
334
335
0
  my $dbh = C4::Context->dbh;
336
337
0
  my $sth;
338
0
  $sth = $dbh->prepare("INSERT INTO collections_tracking ( ctId, colId, itemnumber )
339                        VALUES ( NULL, ?, ? )");
340
0
  $sth->execute( $colId, $itemnumber ) or return ( 0, 3, $sth->errstr() );
341
0
  $sth->finish;
342
343
0
  return 1;
344
345}
346
347 - 362
=head2  RemoveItemFromCollection

 ( $success, $errorcode, $errormessage ) = RemoveItemFromCollection( $colId, $itemnumber );

Removes an item to a collection

 Input:
   $colId: Collection to add the item to.
   $itemnumber: Item to be removed from collection

 Output:
   $success: 1 if all database operations were successful, 0 otherwise
   $errorCode: Code for reason of failure, good for translating errors in templates
   $errorMessage: English description of error

=cut
363
364sub RemoveItemFromCollection {
365
0
  my ( $colId, $itemnumber ) = @_;
366
367  ## Check for all neccessary parameters
368
0
  if ( ! $itemnumber ) {
369
0
    return ( 0, 2, "No Itemnumber Given" );
370  }
371
372
0
  if ( ! isItemInThisCollection( $itemnumber, $colId ) ) {
373
0
    return ( 0, 2, "Item is not in the collection!" );
374  }
375
376
0
  my $dbh = C4::Context->dbh;
377
378
0
  my $sth;
379
0
  $sth = $dbh->prepare("DELETE FROM collections_tracking
380                        WHERE itemnumber = ?");
381
0
  $sth->execute( $itemnumber ) or return ( 0, 3, $sth->errstr() );
382
0
  $sth->finish;
383
384
0
  return 1;
385}
386
387 - 402
=head2 TransferCollection

 ( $success, $errorcode, $errormessage ) = TransferCollection( $colId, $colBranchcode );

Transfers a collection to another branch

 Input:
   $colId: id of the collection to be updated
   $colBranchcode: branch where collection is moving to

 Output:
   $success: 1 if all database operations were successful, 0 otherwise
   $errorCode: Code for reason of failure, good for translating errors in templates
   $errorMessage: English description of error

=cut
403
404sub TransferCollection {
405
0
  my ( $colId, $colBranchcode ) = @_;
406
407  ## Check for all neccessary parameters
408
0
  if ( ! $colId ) {
409
0
    return ( 0, 1, "No Id Given" );
410  }
411
0
  if ( ! $colBranchcode ) {
412
0
    return ( 0, 2, "No Branchcode Given" );
413  }
414
415
0
  my $dbh = C4::Context->dbh;
416
417
0
  my $sth;
418
0
  $sth = $dbh->prepare("UPDATE collections
419                        SET
420                        colBranchcode = ?
421                        WHERE colId = ?");
422
0
  $sth->execute( $colBranchcode, $colId ) or return ( 0, 4, $sth->errstr() );
423
0
  $sth->finish;
424
425
0
  $sth = $dbh->prepare("SELECT barcode FROM items, collections_tracking
426                        WHERE items.itemnumber = collections_tracking.itemnumber
427                        AND collections_tracking.colId = ?");
428
0
  $sth->execute( $colId ) or return ( 0, 4, $sth->errstr );
429
0
  my @results;
430
0
  while ( my $item = $sth->fetchrow_hashref ) {
431
0
    my ( $dotransfer, $messages, $iteminformation ) = transferbook( $colBranchcode, $item->{'barcode'}, my $ignore_reserves = 1);
432  }
433
434
435
436
0
  return 1;
437
438}
439
440 - 444
=head2 GetCollectionItemBranches

  my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches( $itemnumber );

=cut
445
446sub GetCollectionItemBranches {
447
0
  my ( $itemnumber ) = @_;
448
449
0
  if ( ! $itemnumber ) {
450
0
    return;
451  }
452
453
0
  my $dbh = C4::Context->dbh;
454
455
0
  my ( $sth, @results );
456
0
  $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
0
  $sth->execute( $itemnumber );
461
462
0
  my $row = $sth->fetchrow_hashref;
463
464
0
  $sth->finish;
465
466  return (
467
0
      $$row{'holdingbranch'},
468      $$row{'colBranchcode'},
469  );
470}
471
472 - 476
=head2 isItemInThisCollection

  $inCollection = isItemInThisCollection( $itemnumber, $colId );

=cut
477
478sub isItemInThisCollection {
479
0
  my ( $itemnumber, $colId ) = @_;
480
481
0
  my $dbh = C4::Context->dbh;
482
483
0
  my $sth = $dbh->prepare("SELECT COUNT(*) as inCollection FROM collections_tracking WHERE itemnumber = ? AND colId = ?");
484
0
  $sth->execute( $itemnumber, $colId ) or return( 0 );
485
486
0
  my $row = $sth->fetchrow_hashref;
487
488
0
  return $$row{'inCollection'};
489}
490
491 - 495
=head2 isItemInAnyCollection

$inCollection = isItemInAnyCollection( $itemnumber );

=cut
496
497sub isItemInAnyCollection {
498
0
  my ( $itemnumber ) = @_;
499
500
0
  my $dbh = C4::Context->dbh;
501
502
0
  my $sth = $dbh->prepare("SELECT itemnumber FROM collections_tracking WHERE itemnumber = ?");
503
0
  $sth->execute( $itemnumber ) or return( 0 );
504
505
0
  my $row = $sth->fetchrow_hashref;
506
507
0
  $itemnumber = $row->{itemnumber};
508
0
  $sth->finish;
509
510
0
  if ( $itemnumber ) {
511
0
    return 1;
512  } else {
513
0
    return 0;
514  }
515}
516
5171;
518