Bug 18811: Atomic update to print warning at upgrade time
[koha.git] / C4 / RotatingCollections.pm
blob709eaf21644609031e9edeac1355f7ac8cbed470
1 package C4::RotatingCollections;
3 # $Id: RotatingCollections.pm,v 0.1 2007/04/20 kylemhall
5 # This package is inteded to keep track of what library
6 # Items of a certain collection should be at.
8 # Copyright 2007 Kyle Hall
10 # This file is part of Koha.
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.
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.
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>.
25 use Modern::Perl;
27 use C4::Context;
28 use C4::Circulation;
29 use C4::Reserves qw(CheckReserves);
30 use Koha::Database;
32 use DBI;
34 use Data::Dumper;
36 use vars qw(@ISA @EXPORT);
39 =head1 NAME
41 C4::RotatingCollections - Functions for managing rotating collections
43 =head1 FUNCTIONS
45 =cut
47 BEGIN {
48 require Exporter;
49 @ISA = qw( Exporter );
50 @EXPORT = qw(
51 CreateCollection
52 UpdateCollection
53 DeleteCollection
55 GetItemsInCollection
57 GetCollection
58 GetCollections
60 AddItemToCollection
61 RemoveItemFromCollection
62 TransferCollection
64 GetCollectionItemBranches
68 =head2 CreateCollection
69 ( $success, $errorcode, $errormessage ) = CreateCollection( $title, $description );
70 Creates a new collection
72 Input:
73 $title: short description of the club or service
74 $description: long description of the club or service
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
81 =cut
83 sub CreateCollection {
84 my ( $title, $description ) = @_;
86 my $schema = Koha::Database->new()->schema();
87 my $duplicate_titles = $schema->resultset('Collection')->count({ colTitle => $title });
89 ## Check for all necessary parameters
90 if ( !$title ) {
91 return ( 0, 1, "NO_TITLE" );
92 } elsif ( $duplicate_titles ) {
93 return ( 0, 2, "DUPLICATE_TITLE" );
96 $description ||= q{};
98 my $success = 1;
100 my $dbh = C4::Context->dbh;
102 my $sth;
103 $sth = $dbh->prepare(
104 "INSERT INTO collections ( colId, colTitle, colDesc )
105 VALUES ( NULL, ?, ? )"
107 $sth->execute( $title, $description ) or return ( 0, 3, $sth->errstr() );
109 return 1;
113 =head2 UpdateCollection
115 ( $success, $errorcode, $errormessage ) = UpdateCollection( $colId, $title, $description );
117 Updates a collection
119 Input:
120 $colId: id of the collection to be updated
121 $title: short description of the club or service
122 $description: long description of the club or service
124 Output:
125 $success: 1 if all database operations were successful, 0 otherwise
126 $errorCode: Code for reason of failure, good for translating errors in templates
127 $errorMessage: English description of error
129 =cut
131 sub UpdateCollection {
132 my ( $colId, $title, $description ) = @_;
134 my $schema = Koha::Database->new()->schema();
135 my $duplicate_titles = $schema->resultset('Collection')->count({ colTitle => $title, -not => { colId => $colId } });
137 ## Check for all necessary parameters
138 if ( !$colId ) {
139 return ( 0, 1, "NO_ID" );
141 if ( !$title ) {
142 return ( 0, 2, "NO_TITLE" );
144 if ( $duplicate_titles ) {
145 return ( 0, 3, "DUPLICATE_TITLE" );
148 my $dbh = C4::Context->dbh;
150 $description ||= q{};
152 my $sth;
153 $sth = $dbh->prepare(
154 "UPDATE collections
155 SET
156 colTitle = ?, colDesc = ?
157 WHERE colId = ?"
159 $sth->execute( $title, $description, $colId )
160 or return ( 0, 4, $sth->errstr() );
162 return 1;
166 =head2 DeleteCollection
168 ( $success, $errorcode, $errormessage ) = DeleteCollection( $colId );
169 Deletes a collection of the given id
171 Input:
172 $colId : id of the Archetype to be deleted
174 Output:
175 $success: 1 if all database operations were successful, 0 otherwise
176 $errorCode: Code for reason of failure, good for translating errors in templates
177 $errorMessage: English description of error
179 =cut
181 sub DeleteCollection {
182 my ($colId) = @_;
184 ## Parameter check
185 if ( !$colId ) {
186 return ( 0, 1, "NO_ID" );
189 my $dbh = C4::Context->dbh;
191 my $sth;
193 $sth = $dbh->prepare("DELETE FROM collections WHERE colId = ?");
194 $sth->execute($colId) or return ( 0, 4, $sth->errstr() );
196 return 1;
199 =head2 GetCollections
201 $collections = GetCollections();
202 Returns data about all collections
204 Output:
205 On Success:
206 $results: Reference to an array of associated arrays
207 On Failure:
208 $errorCode: Code for reason of failure, good for translating errors in templates
209 $errorMessage: English description of error
211 =cut
213 sub GetCollections {
215 my $dbh = C4::Context->dbh;
217 my $sth = $dbh->prepare("SELECT * FROM collections");
218 $sth->execute() or return ( 1, $sth->errstr() );
220 my @results;
221 while ( my $row = $sth->fetchrow_hashref ) {
222 push( @results, $row );
225 return \@results;
228 =head2 GetItemsInCollection
230 ( $results, $success, $errorcode, $errormessage ) = GetItemsInCollection( $colId );
232 Returns information about the items in the given collection
234 Input:
235 $colId: The id of the collection
237 Output:
238 $results: Reference to an array of associated arrays
239 $success: 1 if all database operations were successful, 0 otherwise
240 $errorCode: Code for reason of failure, good for translating errors in templates
241 $errorMessage: English description of error
243 =cut
245 sub GetItemsInCollection {
246 my ($colId) = @_;
248 ## Parameter check
249 if ( !$colId ) {
250 return ( 0, 0, 1, "NO_ID" );
253 my $dbh = C4::Context->dbh;
255 my $sth = $dbh->prepare(
256 "SELECT
257 biblio.title,
258 biblio.biblionumber,
259 items.itemcallnumber,
260 items.barcode
261 FROM collections, collections_tracking, items, biblio
262 WHERE collections.colId = collections_tracking.colId
263 AND collections_tracking.itemnumber = items.itemnumber
264 AND items.biblionumber = biblio.biblionumber
265 AND collections.colId = ? ORDER BY biblio.title"
267 $sth->execute($colId) or return ( 0, 0, 2, $sth->errstr() );
269 my @results;
270 while ( my $row = $sth->fetchrow_hashref ) {
271 push( @results, $row );
274 return \@results;
277 =head2 GetCollection
279 ( $colId, $colTitle, $colDesc, $colBranchcode ) = GetCollection( $colId );
281 Returns information about a collection
283 Input:
284 $colId: Id of the collection
285 Output:
286 $colId, $colTitle, $colDesc, $colBranchcode
288 =cut
290 sub GetCollection {
291 my ($colId) = @_;
293 my $dbh = C4::Context->dbh;
295 my ( $sth, @results );
296 $sth = $dbh->prepare("SELECT * FROM collections WHERE colId = ?");
297 $sth->execute($colId) or return 0;
299 my $row = $sth->fetchrow_hashref;
301 return (
302 $$row{'colId'}, $$row{'colTitle'},
303 $$row{'colDesc'}, $$row{'colBranchcode'}
308 =head2 AddItemToCollection
310 ( $success, $errorcode, $errormessage ) = AddItemToCollection( $colId, $itemnumber );
312 Adds an item to a rotating collection.
314 Input:
315 $colId: Collection to add the item to.
316 $itemnumber: Item to be added to the collection
317 Output:
318 $success: 1 if all database operations were successful, 0 otherwise
319 $errorCode: Code for reason of failure, good for translating errors in templates
320 $errorMessage: English description of error
322 =cut
324 sub AddItemToCollection {
325 my ( $colId, $itemnumber ) = @_;
327 ## Check for all necessary parameters
328 if ( !$colId ) {
329 return ( 0, 1, "NO_ID" );
331 if ( !$itemnumber ) {
332 return ( 0, 2, "NO_ITEM" );
335 if ( isItemInThisCollection( $itemnumber, $colId ) ) {
336 return ( 0, 2, "IN_COLLECTION" );
338 elsif ( isItemInAnyCollection($itemnumber) ) {
339 return ( 0, 3, "IN_COLLECTION_OTHER" );
342 my $dbh = C4::Context->dbh;
344 my $sth;
345 $sth = $dbh->prepare("
346 INSERT INTO collections_tracking (
347 colId,
348 itemnumber
349 ) VALUES ( ?, ? )
351 $sth->execute( $colId, $itemnumber ) or return ( 0, 3, $sth->errstr() );
353 return 1;
357 =head2 RemoveItemFromCollection
359 ( $success, $errorcode, $errormessage ) = RemoveItemFromCollection( $colId, $itemnumber );
361 Removes an item to a collection
363 Input:
364 $colId: Collection to add the item to.
365 $itemnumber: Item to be removed from collection
367 Output:
368 $success: 1 if all database operations were successful, 0 otherwise
369 $errorCode: Code for reason of failure, good for translating errors in templates
370 $errorMessage: English description of error
372 =cut
374 sub RemoveItemFromCollection {
375 my ( $colId, $itemnumber ) = @_;
377 ## Check for all necessary parameters
378 if ( !$itemnumber ) {
379 return ( 0, 2, "NO_ITEM" );
382 if ( !isItemInThisCollection( $itemnumber, $colId ) ) {
383 return ( 0, 2, "NOT_IN_COLLECTION" );
386 my $dbh = C4::Context->dbh;
388 my $sth;
389 $sth = $dbh->prepare(
390 "DELETE FROM collections_tracking
391 WHERE itemnumber = ?"
393 $sth->execute($itemnumber) or return ( 0, 3, $sth->errstr() );
395 return 1;
398 =head2 TransferCollection
400 ( $success, $errorcode, $errormessage ) = TransferCollection( $colId, $colBranchcode );
402 Transfers a collection to another branch
404 Input:
405 $colId: id of the collection to be updated
406 $colBranchcode: branch where collection is moving to
408 Output:
409 $success: 1 if all database operations were successful, 0 otherwise
410 $errorCode: Code for reason of failure, good for translating errors in templates
411 $errorMessage: English description of error
413 =cut
415 sub TransferCollection {
416 my ( $colId, $colBranchcode ) = @_;
418 ## Check for all necessary parameters
419 if ( !$colId ) {
420 return ( 0, 1, "NO_ID" );
422 if ( !$colBranchcode ) {
423 return ( 0, 2, "NO_BRANCHCODE" );
426 my $dbh = C4::Context->dbh;
428 my $sth;
429 $sth = $dbh->prepare(
430 "UPDATE collections
431 SET
432 colBranchcode = ?
433 WHERE colId = ?"
435 $sth->execute( $colBranchcode, $colId ) or return ( 0, 4, $sth->errstr() );
437 $sth = $dbh->prepare(q{
438 SELECT items.itemnumber, items.barcode FROM collections_tracking
439 LEFT JOIN items ON collections_tracking.itemnumber = items.itemnumber
440 LEFT JOIN issues ON items.itemnumber = issues.itemnumber
441 WHERE issues.borrowernumber IS NULL
442 AND collections_tracking.colId = ?
444 $sth->execute($colId) or return ( 0, 4, $sth->errstr );
445 my @results;
446 while ( my $item = $sth->fetchrow_hashref ) {
447 my ($status) = CheckReserves( $item->{itemnumber} );
448 my @transfers = C4::Circulation::GetTransfers( $item->{itemnumber} );
449 C4::Circulation::transferbook( $colBranchcode, $item->{barcode}, my $ignore_reserves = 1 ) unless ( $status eq 'Waiting' || @transfers );
452 return 1;
456 =head2 GetCollectionItemBranches
458 my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches( $itemnumber );
460 =cut
462 sub GetCollectionItemBranches {
463 my ($itemnumber) = @_;
465 if ( !$itemnumber ) {
466 return;
469 my $dbh = C4::Context->dbh;
471 my ( $sth, @results );
472 $sth = $dbh->prepare(
473 "SELECT holdingbranch, colBranchcode FROM items, collections, collections_tracking
474 WHERE items.itemnumber = collections_tracking.itemnumber
475 AND collections.colId = collections_tracking.colId
476 AND items.itemnumber = ?"
478 $sth->execute($itemnumber);
480 my $row = $sth->fetchrow_hashref;
482 return ( $$row{'holdingbranch'}, $$row{'colBranchcode'}, );
485 =head2 isItemInThisCollection
487 $inCollection = isItemInThisCollection( $itemnumber, $colId );
489 =cut
491 sub isItemInThisCollection {
492 my ( $itemnumber, $colId ) = @_;
494 my $dbh = C4::Context->dbh;
496 my $sth = $dbh->prepare(
497 "SELECT COUNT(*) as inCollection FROM collections_tracking WHERE itemnumber = ? AND colId = ?"
499 $sth->execute( $itemnumber, $colId ) or return (0);
501 my $row = $sth->fetchrow_hashref;
503 return $$row{'inCollection'};
506 =head2 isItemInAnyCollection
508 $inCollection = isItemInAnyCollection( $itemnumber );
510 =cut
512 sub isItemInAnyCollection {
513 my ($itemnumber) = @_;
515 my $dbh = C4::Context->dbh;
517 my $sth = $dbh->prepare(
518 "SELECT itemnumber FROM collections_tracking WHERE itemnumber = ?");
519 $sth->execute($itemnumber) or return (0);
521 my $row = $sth->fetchrow_hashref;
523 $itemnumber = $row->{itemnumber};
524 if ($itemnumber) {
525 return 1;
527 else {
528 return 0;
534 __END__
536 =head1 AUTHOR
538 Kyle Hall <kylemhall@gmail.com>
540 =cut