RotatingCollections.pm 13.1 KB
Newer Older
1 2
package C4::RotatingCollections;

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 9 10 11

# Copyright 2007 Kyle Hall
#
# This file is part of Koha.
#
12 13 14 15
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
16
#
17 18 19 20
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
21
#
22 23
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
24

25
use Modern::Perl;
26 27 28

use C4::Context;
use C4::Circulation;
Kyle M Hall's avatar
Kyle M Hall committed
29
use C4::Reserves qw(CheckReserves);
30
use Koha::Database;
31 32 33 34 35

use DBI;

use Data::Dumper;

36
use vars qw(@ISA @EXPORT);
37 38 39 40 41 42 43 44 45 46


=head1 NAME

C4::RotatingCollections - Functions for managing rotating collections

=head1 FUNCTIONS

=cut

47 48 49 50 51 52 53 54 55 56 57 58
BEGIN {
    require Exporter;
    @ISA    = qw( Exporter );
    @EXPORT = qw(
      CreateCollection
      UpdateCollection
      DeleteCollection

      GetItemsInCollection

      GetCollection
      GetCollections
59

60 61 62
      AddItemToCollection
      RemoveItemFromCollection
      TransferCollection
63

64 65 66
      GetCollectionItemBranches
    );
}
67

Andrew Elwell's avatar
Andrew Elwell committed
68
=head2  CreateCollection
69 70 71 72 73 74 75 76 77 78 79
 ( $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
Andrew Elwell's avatar
Andrew Elwell committed
80

81
=cut
Andrew Elwell's avatar
Andrew Elwell committed
82

83
sub CreateCollection {
84
    my ( $title, $description ) = @_;
85

86 87 88
    my $schema = Koha::Database->new()->schema();
    my $duplicate_titles = $schema->resultset('Collection')->count({ colTitle => $title });

89
    ## Check for all necessary parameters
90
    if ( !$title ) {
Kyle M Hall's avatar
Kyle M Hall committed
91
        return ( 0, 1, "NO_TITLE" );
92 93
    } elsif ( $duplicate_titles ) {
        return ( 0, 2, "DUPLICATE_TITLE" );
94
    }
95 96

    $description ||= q{};
97

98
    my $success = 1;
99

100
    my $dbh = C4::Context->dbh;
101

102 103 104 105 106 107 108 109
    my $sth;
    $sth = $dbh->prepare(
        "INSERT INTO collections ( colId, colTitle, colDesc )
                        VALUES ( NULL, ?, ? )"
    );
    $sth->execute( $title, $description ) or return ( 0, 3, $sth->errstr() );

    return 1;
110 111 112

}

Andrew Elwell's avatar
Andrew Elwell committed
113 114
=head2 UpdateCollection

115
 ( $success, $errorcode, $errormessage ) = UpdateCollection( $colId, $title, $description );
Andrew Elwell's avatar
Andrew Elwell committed
116 117

Updates a collection
118 119 120 121 122 123 124 125 126 127

 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
Andrew Elwell's avatar
Andrew Elwell committed
128

129
=cut
Andrew Elwell's avatar
Andrew Elwell committed
130

131
sub UpdateCollection {
132 133
    my ( $colId, $title, $description ) = @_;

134 135 136
    my $schema = Koha::Database->new()->schema();
    my $duplicate_titles = $schema->resultset('Collection')->count({ colTitle => $title,  -not => { colId => $colId } });

137
    ## Check for all necessary parameters
138
    if ( !$colId ) {
Kyle M Hall's avatar
Kyle M Hall committed
139
        return ( 0, 1, "NO_ID" );
140 141
    }
    if ( !$title ) {
Kyle M Hall's avatar
Kyle M Hall committed
142
        return ( 0, 2, "NO_TITLE" );
143
    }
144 145 146
    if ( $duplicate_titles ) {
        return ( 0, 3, "DUPLICATE_TITLE" );
    }
147 148 149

    my $dbh = C4::Context->dbh;

150 151
    $description ||= q{};

152 153 154
    my $sth;
    $sth = $dbh->prepare(
        "UPDATE collections
155 156
                        SET 
                        colTitle = ?, colDesc = ? 
157 158 159 160 161 162 163
                        WHERE colId = ?"
    );
    $sth->execute( $title, $description, $colId )
      or return ( 0, 4, $sth->errstr() );

    return 1;

164 165
}

Andrew Elwell's avatar
Andrew Elwell committed
166 167
=head2 DeleteCollection

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

 Input:
172
   $colId : id of the Archetype to be deleted
173 174 175 176 177

 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
Andrew Elwell's avatar
Andrew Elwell committed
178

179
=cut
Andrew Elwell's avatar
Andrew Elwell committed
180

181
sub DeleteCollection {
182 183
    my ($colId) = @_;

184
    ## Parameter check
185
    if ( !$colId ) {
Kyle M Hall's avatar
Kyle M Hall committed
186
        return ( 0, 1, "NO_ID" );
187
    }
188

189
    my $dbh = C4::Context->dbh;
190

191
    my $sth;
192

193 194
    $sth = $dbh->prepare("DELETE FROM collections WHERE colId = ?");
    $sth->execute($colId) or return ( 0, 4, $sth->errstr() );
195

196
    return 1;
197 198
}

Andrew Elwell's avatar
Andrew Elwell committed
199 200
=head2 GetCollections

201 202 203 204 205 206 207 208 209
 $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
Andrew Elwell's avatar
Andrew Elwell committed
210

211
=cut
Andrew Elwell's avatar
Andrew Elwell committed
212

213 214
sub GetCollections {

215 216 217 218 219 220 221 222 223 224 225
    my $dbh = C4::Context->dbh;

    my $sth = $dbh->prepare("SELECT * FROM collections");
    $sth->execute() or return ( 1, $sth->errstr() );

    my @results;
    while ( my $row = $sth->fetchrow_hashref ) {
        push( @results, $row );
    }

    return \@results;
226 227
}

Andrew Elwell's avatar
Andrew Elwell committed
228 229
=head2 GetItemsInCollection

230
 ( $results, $success, $errorcode, $errormessage ) = GetItemsInCollection( $colId );
Andrew Elwell's avatar
Andrew Elwell committed
231

232 233 234 235 236 237 238 239 240 241
 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
Andrew Elwell's avatar
Andrew Elwell committed
242

243
=cut
Andrew Elwell's avatar
Andrew Elwell committed
244

245
sub GetItemsInCollection {
246 247
    my ($colId) = @_;

248
    ## Parameter check
249
    if ( !$colId ) {
Kyle M Hall's avatar
Kyle M Hall committed
250
        return ( 0, 0, 1, "NO_ID" );
251
    }
252

253
    my $dbh = C4::Context->dbh;
254

255 256
    my $sth = $dbh->prepare(
        "SELECT
257
                             biblio.title,
258
                             biblio.biblionumber,
259 260 261 262 263 264
                             items.itemcallnumber,
                             items.barcode
                           FROM collections, collections_tracking, items, biblio
                           WHERE collections.colId = collections_tracking.colId
                           AND collections_tracking.itemnumber = items.itemnumber
                           AND items.biblionumber = biblio.biblionumber
265 266 267 268 269 270 271 272 273 274
                           AND collections.colId = ? ORDER BY biblio.title"
    );
    $sth->execute($colId) or return ( 0, 0, 2, $sth->errstr() );

    my @results;
    while ( my $row = $sth->fetchrow_hashref ) {
        push( @results, $row );
    }

    return \@results;
275 276
}

Andrew Elwell's avatar
Andrew Elwell committed
277 278
=head2 GetCollection

279
 ( $colId, $colTitle, $colDesc, $colBranchcode ) = GetCollection( $colId );
Andrew Elwell's avatar
Andrew Elwell committed
280 281

Returns information about a collection
282 283 284 285 286

 Input:
   $colId: Id of the collection
 Output:
   $colId, $colTitle, $colDesc, $colBranchcode
Andrew Elwell's avatar
Andrew Elwell committed
287

288
=cut
Andrew Elwell's avatar
Andrew Elwell committed
289

290
sub GetCollection {
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
    my ($colId) = @_;

    my $dbh = C4::Context->dbh;

    my ( $sth, @results );
    $sth = $dbh->prepare("SELECT * FROM collections WHERE colId = ?");
    $sth->execute($colId) or return 0;

    my $row = $sth->fetchrow_hashref;

    return (
        $$row{'colId'},   $$row{'colTitle'},
        $$row{'colDesc'}, $$row{'colBranchcode'}
    );

306 307
}

Andrew Elwell's avatar
Andrew Elwell committed
308 309
=head2 AddItemToCollection

310
 ( $success, $errorcode, $errormessage ) = AddItemToCollection( $colId, $itemnumber );
Andrew Elwell's avatar
Andrew Elwell committed
311 312

Adds an item to a rotating collection.
313 314 315 316 317 318 319 320

 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
Andrew Elwell's avatar
Andrew Elwell committed
321

322
=cut
Andrew Elwell's avatar
Andrew Elwell committed
323

324
sub AddItemToCollection {
325 326
    my ( $colId, $itemnumber ) = @_;

327
    ## Check for all necessary parameters
328
    if ( !$colId ) {
Kyle M Hall's avatar
Kyle M Hall committed
329
        return ( 0, 1, "NO_ID" );
330 331
    }
    if ( !$itemnumber ) {
Kyle M Hall's avatar
Kyle M Hall committed
332
        return ( 0, 2, "NO_ITEM" );
333 334 335
    }

    if ( isItemInThisCollection( $itemnumber, $colId ) ) {
Kyle M Hall's avatar
Kyle M Hall committed
336
        return ( 0, 2, "IN_COLLECTION" );
337 338
    }
    elsif ( isItemInAnyCollection($itemnumber) ) {
Kyle M Hall's avatar
Kyle M Hall committed
339
        return ( 0, 3, "IN_COLLECTION_OTHER" );
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
    }

    my $dbh = C4::Context->dbh;

    my $sth;
    $sth = $dbh->prepare("
        INSERT INTO collections_tracking (
            colId,
            itemnumber
        ) VALUES ( ?, ? )
    ");
    $sth->execute( $colId, $itemnumber ) or return ( 0, 3, $sth->errstr() );

    return 1;

355 356
}

Andrew Elwell's avatar
Andrew Elwell committed
357 358
=head2  RemoveItemFromCollection

359
 ( $success, $errorcode, $errormessage ) = RemoveItemFromCollection( $colId, $itemnumber );
Andrew Elwell's avatar
Andrew Elwell committed
360 361

Removes an item to a collection
362 363 364 365 366 367 368 369 370

 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
Andrew Elwell's avatar
Andrew Elwell committed
371

372
=cut
Andrew Elwell's avatar
Andrew Elwell committed
373

374
sub RemoveItemFromCollection {
375 376
    my ( $colId, $itemnumber ) = @_;

377
    ## Check for all necessary parameters
378
    if ( !$itemnumber ) {
Kyle M Hall's avatar
Kyle M Hall committed
379
        return ( 0, 2, "NO_ITEM" );
380
    }
381

382
    if ( !isItemInThisCollection( $itemnumber, $colId ) ) {
Kyle M Hall's avatar
Kyle M Hall committed
383
        return ( 0, 2, "NOT_IN_COLLECTION" );
384
    }
385

386
    my $dbh = C4::Context->dbh;
387

388 389 390 391 392 393
    my $sth;
    $sth = $dbh->prepare(
        "DELETE FROM collections_tracking
                        WHERE itemnumber = ?"
    );
    $sth->execute($itemnumber) or return ( 0, 3, $sth->errstr() );
394

395
    return 1;
396 397
}

Andrew Elwell's avatar
Andrew Elwell committed
398 399
=head2 TransferCollection

400
 ( $success, $errorcode, $errormessage ) = TransferCollection( $colId, $colBranchcode );
Andrew Elwell's avatar
Andrew Elwell committed
401 402

Transfers a collection to another branch
403 404 405 406 407 408 409 410 411

 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
Andrew Elwell's avatar
Andrew Elwell committed
412

413
=cut
Andrew Elwell's avatar
Andrew Elwell committed
414

415
sub TransferCollection {
416
    my ( $colId, $colBranchcode ) = @_;
417

418
    ## Check for all necessary parameters
419
    if ( !$colId ) {
Kyle M Hall's avatar
Kyle M Hall committed
420
        return ( 0, 1, "NO_ID" );
421 422
    }
    if ( !$colBranchcode ) {
Kyle M Hall's avatar
Kyle M Hall committed
423
        return ( 0, 2, "NO_BRANCHCODE" );
424
    }
425

426
    my $dbh = C4::Context->dbh;
427

428 429 430
    my $sth;
    $sth = $dbh->prepare(
        "UPDATE collections
431 432
                        SET 
                        colBranchcode = ? 
433 434 435 436 437 438 439 440 441 442 443 444 445 446
                        WHERE colId = ?"
    );
    $sth->execute( $colBranchcode, $colId ) or return ( 0, 4, $sth->errstr() );

    $sth = $dbh->prepare(q{
        SELECT items.itemnumber, items.barcode FROM collections_tracking
        LEFT JOIN items ON collections_tracking.itemnumber = items.itemnumber
        LEFT JOIN issues ON items.itemnumber = issues.itemnumber
        WHERE issues.borrowernumber IS NULL
          AND collections_tracking.colId = ?
    });
    $sth->execute($colId) or return ( 0, 4, $sth->errstr );
    my @results;
    while ( my $item = $sth->fetchrow_hashref ) {
Kyle M Hall's avatar
Kyle M Hall committed
447
        my ($status) = CheckReserves( $item->{itemnumber} );
448 449
        my @transfers = C4::Circulation::GetTransfers( $item->{itemnumber} );
        C4::Circulation::transferbook( $colBranchcode, $item->{barcode}, my $ignore_reserves = 1 ) unless ( $status eq 'Waiting' || @transfers );
450 451 452 453
    }

    return 1;

454 455
}

Andrew Elwell's avatar
Andrew Elwell committed
456 457 458 459
=head2 GetCollectionItemBranches

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

Kyle M Hall's avatar
Kyle M Hall committed
460
=cut
Andrew Elwell's avatar
Andrew Elwell committed
461

Kyle M Hall's avatar
Kyle M Hall committed
462
sub GetCollectionItemBranches {
463
    my ($itemnumber) = @_;
Kyle M Hall's avatar
Kyle M Hall committed
464

465 466 467
    if ( !$itemnumber ) {
        return;
    }
Kyle M Hall's avatar
Kyle M Hall committed
468

469
    my $dbh = C4::Context->dbh;
Kyle M Hall's avatar
Kyle M Hall committed
470

471 472 473
    my ( $sth, @results );
    $sth = $dbh->prepare(
"SELECT holdingbranch, colBranchcode FROM items, collections, collections_tracking
Kyle M Hall's avatar
Kyle M Hall committed
474 475
                        WHERE items.itemnumber = collections_tracking.itemnumber
                        AND collections.colId = collections_tracking.colId
476 477 478 479 480 481 482
                        AND items.itemnumber = ?"
    );
    $sth->execute($itemnumber);

    my $row = $sth->fetchrow_hashref;

    return ( $$row{'holdingbranch'}, $$row{'colBranchcode'}, );
Kyle M Hall's avatar
Kyle M Hall committed
483
}
484

Andrew Elwell's avatar
Andrew Elwell committed
485 486 487 488 489 490
=head2 isItemInThisCollection

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

=cut

491
sub isItemInThisCollection {
492 493 494 495 496 497 498 499 500 501 502 503
    my ( $itemnumber, $colId ) = @_;

    my $dbh = C4::Context->dbh;

    my $sth = $dbh->prepare(
"SELECT COUNT(*) as inCollection FROM collections_tracking WHERE itemnumber = ? AND colId = ?"
    );
    $sth->execute( $itemnumber, $colId ) or return (0);

    my $row = $sth->fetchrow_hashref;

    return $$row{'inCollection'};
504 505
}

Andrew Elwell's avatar
Andrew Elwell committed
506 507
=head2 isItemInAnyCollection

508
$inCollection = isItemInAnyCollection( $itemnumber );
Andrew Elwell's avatar
Andrew Elwell committed
509

510
=cut
Andrew Elwell's avatar
Andrew Elwell committed
511

512
sub isItemInAnyCollection {
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
    my ($itemnumber) = @_;

    my $dbh = C4::Context->dbh;

    my $sth = $dbh->prepare(
        "SELECT itemnumber FROM collections_tracking WHERE itemnumber = ?");
    $sth->execute($itemnumber) or return (0);

    my $row = $sth->fetchrow_hashref;

    $itemnumber = $row->{itemnumber};
    if ($itemnumber) {
        return 1;
    }
    else {
        return 0;
    }
530 531 532 533 534 535 536 537 538 539 540
}

1;

__END__

=head1 AUTHOR

Kyle Hall <kylemhall@gmail.com>

=cut