Bug 14674: koha-create should populate upload_path
[koha.git] / C4 / RotatingCollections.pm
blobdd80cdd9b087475fbeea72fa07d600d6f0969c39
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($VERSION @ISA @EXPORT);
38 # set the version for version checking
39 $VERSION = 3.07.00.049;
41 =head1 NAME
43 C4::RotatingCollections - Functions for managing rotating collections
45 =head1 FUNCTIONS
47 =cut
49 BEGIN {
50 require Exporter;
51 @ISA = qw( Exporter );
52 @EXPORT = qw(
53 CreateCollection
54 UpdateCollection
55 DeleteCollection
57 GetItemsInCollection
59 GetCollection
60 GetCollections
62 AddItemToCollection
63 RemoveItemFromCollection
64 TransferCollection
66 GetCollectionItemBranches
70 =head2 CreateCollection
71 ( $success, $errorcode, $errormessage ) = CreateCollection( $title, $description );
72 Creates a new collection
74 Input:
75 $title: short description of the club or service
76 $description: long description of the club or service
78 Output:
79 $success: 1 if all database operations were successful, 0 otherwise
80 $errorCode: Code for reason of failure, good for translating errors in templates
81 $errorMessage: English description of error
83 =cut
85 sub CreateCollection {
86 my ( $title, $description ) = @_;
88 my $schema = Koha::Database->new()->schema();
89 my $duplicate_titles = $schema->resultset('Collection')->count({ colTitle => $title });
91 ## Check for all necessary parameters
92 if ( !$title ) {
93 return ( 0, 1, "NO_TITLE" );
94 } elsif ( $duplicate_titles ) {
95 return ( 0, 2, "DUPLICATE_TITLE" );
98 $description ||= q{};
100 my $success = 1;
102 my $dbh = C4::Context->dbh;
104 my $sth;
105 $sth = $dbh->prepare(
106 "INSERT INTO collections ( colId, colTitle, colDesc )
107 VALUES ( NULL, ?, ? )"
109 $sth->execute( $title, $description ) or return ( 0, 3, $sth->errstr() );
111 return 1;
115 =head2 UpdateCollection
117 ( $success, $errorcode, $errormessage ) = UpdateCollection( $colId, $title, $description );
119 Updates a collection
121 Input:
122 $colId: id of the collection to be updated
123 $title: short description of the club or service
124 $description: long description of the club or service
126 Output:
127 $success: 1 if all database operations were successful, 0 otherwise
128 $errorCode: Code for reason of failure, good for translating errors in templates
129 $errorMessage: English description of error
131 =cut
133 sub UpdateCollection {
134 my ( $colId, $title, $description ) = @_;
136 my $schema = Koha::Database->new()->schema();
137 my $duplicate_titles = $schema->resultset('Collection')->count({ colTitle => $title, -not => { colId => $colId } });
139 ## Check for all necessary parameters
140 if ( !$colId ) {
141 return ( 0, 1, "NO_ID" );
143 if ( !$title ) {
144 return ( 0, 2, "NO_TITLE" );
146 if ( $duplicate_titles ) {
147 return ( 0, 3, "DUPLICATE_TITLE" );
150 my $dbh = C4::Context->dbh;
152 $description ||= q{};
154 my $sth;
155 $sth = $dbh->prepare(
156 "UPDATE collections
157 SET
158 colTitle = ?, colDesc = ?
159 WHERE colId = ?"
161 $sth->execute( $title, $description, $colId )
162 or return ( 0, 4, $sth->errstr() );
164 return 1;
168 =head2 DeleteCollection
170 ( $success, $errorcode, $errormessage ) = DeleteCollection( $colId );
171 Deletes a collection of the given id
173 Input:
174 $colId : id of the Archetype to be deleted
176 Output:
177 $success: 1 if all database operations were successful, 0 otherwise
178 $errorCode: Code for reason of failure, good for translating errors in templates
179 $errorMessage: English description of error
181 =cut
183 sub DeleteCollection {
184 my ($colId) = @_;
186 ## Parameter check
187 if ( !$colId ) {
188 return ( 0, 1, "NO_ID" );
191 my $dbh = C4::Context->dbh;
193 my $sth;
195 $sth = $dbh->prepare("DELETE FROM collections WHERE colId = ?");
196 $sth->execute($colId) or return ( 0, 4, $sth->errstr() );
198 return 1;
201 =head2 GetCollections
203 $collections = GetCollections();
204 Returns data about all collections
206 Output:
207 On Success:
208 $results: Reference to an array of associated arrays
209 On Failure:
210 $errorCode: Code for reason of failure, good for translating errors in templates
211 $errorMessage: English description of error
213 =cut
215 sub GetCollections {
217 my $dbh = C4::Context->dbh;
219 my $sth = $dbh->prepare("SELECT * FROM collections");
220 $sth->execute() or return ( 1, $sth->errstr() );
222 my @results;
223 while ( my $row = $sth->fetchrow_hashref ) {
224 push( @results, $row );
227 return \@results;
230 =head2 GetItemsInCollection
232 ( $results, $success, $errorcode, $errormessage ) = GetItemsInCollection( $colId );
234 Returns information about the items in the given collection
236 Input:
237 $colId: The id of the collection
239 Output:
240 $results: Reference to an array of associated arrays
241 $success: 1 if all database operations were successful, 0 otherwise
242 $errorCode: Code for reason of failure, good for translating errors in templates
243 $errorMessage: English description of error
245 =cut
247 sub GetItemsInCollection {
248 my ($colId) = @_;
250 ## Parameter check
251 if ( !$colId ) {
252 return ( 0, 0, 1, "NO_ID" );
255 my $dbh = C4::Context->dbh;
257 my $sth = $dbh->prepare(
258 "SELECT
259 biblio.title,
260 biblio.biblionumber,
261 items.itemcallnumber,
262 items.barcode
263 FROM collections, collections_tracking, items, biblio
264 WHERE collections.colId = collections_tracking.colId
265 AND collections_tracking.itemnumber = items.itemnumber
266 AND items.biblionumber = biblio.biblionumber
267 AND collections.colId = ? ORDER BY biblio.title"
269 $sth->execute($colId) or return ( 0, 0, 2, $sth->errstr() );
271 my @results;
272 while ( my $row = $sth->fetchrow_hashref ) {
273 push( @results, $row );
276 return \@results;
279 =head2 GetCollection
281 ( $colId, $colTitle, $colDesc, $colBranchcode ) = GetCollection( $colId );
283 Returns information about a collection
285 Input:
286 $colId: Id of the collection
287 Output:
288 $colId, $colTitle, $colDesc, $colBranchcode
290 =cut
292 sub GetCollection {
293 my ($colId) = @_;
295 my $dbh = C4::Context->dbh;
297 my ( $sth, @results );
298 $sth = $dbh->prepare("SELECT * FROM collections WHERE colId = ?");
299 $sth->execute($colId) or return 0;
301 my $row = $sth->fetchrow_hashref;
303 return (
304 $$row{'colId'}, $$row{'colTitle'},
305 $$row{'colDesc'}, $$row{'colBranchcode'}
310 =head2 AddItemToCollection
312 ( $success, $errorcode, $errormessage ) = AddItemToCollection( $colId, $itemnumber );
314 Adds an item to a rotating collection.
316 Input:
317 $colId: Collection to add the item to.
318 $itemnumber: Item to be added to the collection
319 Output:
320 $success: 1 if all database operations were successful, 0 otherwise
321 $errorCode: Code for reason of failure, good for translating errors in templates
322 $errorMessage: English description of error
324 =cut
326 sub AddItemToCollection {
327 my ( $colId, $itemnumber ) = @_;
329 ## Check for all necessary parameters
330 if ( !$colId ) {
331 return ( 0, 1, "NO_ID" );
333 if ( !$itemnumber ) {
334 return ( 0, 2, "NO_ITEM" );
337 if ( isItemInThisCollection( $itemnumber, $colId ) ) {
338 return ( 0, 2, "IN_COLLECTION" );
340 elsif ( isItemInAnyCollection($itemnumber) ) {
341 return ( 0, 3, "IN_COLLECTION_OTHER" );
344 my $dbh = C4::Context->dbh;
346 my $sth;
347 $sth = $dbh->prepare("
348 INSERT INTO collections_tracking (
349 colId,
350 itemnumber
351 ) VALUES ( ?, ? )
353 $sth->execute( $colId, $itemnumber ) or return ( 0, 3, $sth->errstr() );
355 return 1;
359 =head2 RemoveItemFromCollection
361 ( $success, $errorcode, $errormessage ) = RemoveItemFromCollection( $colId, $itemnumber );
363 Removes an item to a collection
365 Input:
366 $colId: Collection to add the item to.
367 $itemnumber: Item to be removed from collection
369 Output:
370 $success: 1 if all database operations were successful, 0 otherwise
371 $errorCode: Code for reason of failure, good for translating errors in templates
372 $errorMessage: English description of error
374 =cut
376 sub RemoveItemFromCollection {
377 my ( $colId, $itemnumber ) = @_;
379 ## Check for all necessary parameters
380 if ( !$itemnumber ) {
381 return ( 0, 2, "NO_ITEM" );
384 if ( !isItemInThisCollection( $itemnumber, $colId ) ) {
385 return ( 0, 2, "NOT_IN_COLLECTION" );
388 my $dbh = C4::Context->dbh;
390 my $sth;
391 $sth = $dbh->prepare(
392 "DELETE FROM collections_tracking
393 WHERE itemnumber = ?"
395 $sth->execute($itemnumber) or return ( 0, 3, $sth->errstr() );
397 return 1;
400 =head2 TransferCollection
402 ( $success, $errorcode, $errormessage ) = TransferCollection( $colId, $colBranchcode );
404 Transfers a collection to another branch
406 Input:
407 $colId: id of the collection to be updated
408 $colBranchcode: branch where collection is moving to
410 Output:
411 $success: 1 if all database operations were successful, 0 otherwise
412 $errorCode: Code for reason of failure, good for translating errors in templates
413 $errorMessage: English description of error
415 =cut
417 sub TransferCollection {
418 my ( $colId, $colBranchcode ) = @_;
420 ## Check for all necessary parameters
421 if ( !$colId ) {
422 return ( 0, 1, "NO_ID" );
424 if ( !$colBranchcode ) {
425 return ( 0, 2, "NO_BRANCHCODE" );
428 my $dbh = C4::Context->dbh;
430 my $sth;
431 $sth = $dbh->prepare(
432 "UPDATE collections
433 SET
434 colBranchcode = ?
435 WHERE colId = ?"
437 $sth->execute( $colBranchcode, $colId ) or return ( 0, 4, $sth->errstr() );
439 $sth = $dbh->prepare(q{
440 SELECT items.itemnumber, items.barcode FROM collections_tracking
441 LEFT JOIN items ON collections_tracking.itemnumber = items.itemnumber
442 LEFT JOIN issues ON items.itemnumber = issues.itemnumber
443 WHERE issues.borrowernumber IS NULL
444 AND collections_tracking.colId = ?
446 $sth->execute($colId) or return ( 0, 4, $sth->errstr );
447 my @results;
448 while ( my $item = $sth->fetchrow_hashref ) {
449 my ($status) = CheckReserves( $item->{itemnumber} );
450 my @transfers = GetTransfers( $item->{itemnumber} );
451 transferbook( $colBranchcode, $item->{barcode}, my $ignore_reserves = 1 ) unless ( $status eq 'Waiting' || @transfers );
454 return 1;
458 =head2 GetCollectionItemBranches
460 my ( $holdingBranch, $collectionBranch ) = GetCollectionItemBranches( $itemnumber );
462 =cut
464 sub GetCollectionItemBranches {
465 my ($itemnumber) = @_;
467 if ( !$itemnumber ) {
468 return;
471 my $dbh = C4::Context->dbh;
473 my ( $sth, @results );
474 $sth = $dbh->prepare(
475 "SELECT holdingbranch, colBranchcode FROM items, collections, collections_tracking
476 WHERE items.itemnumber = collections_tracking.itemnumber
477 AND collections.colId = collections_tracking.colId
478 AND items.itemnumber = ?"
480 $sth->execute($itemnumber);
482 my $row = $sth->fetchrow_hashref;
484 return ( $$row{'holdingbranch'}, $$row{'colBranchcode'}, );
487 =head2 isItemInThisCollection
489 $inCollection = isItemInThisCollection( $itemnumber, $colId );
491 =cut
493 sub isItemInThisCollection {
494 my ( $itemnumber, $colId ) = @_;
496 my $dbh = C4::Context->dbh;
498 my $sth = $dbh->prepare(
499 "SELECT COUNT(*) as inCollection FROM collections_tracking WHERE itemnumber = ? AND colId = ?"
501 $sth->execute( $itemnumber, $colId ) or return (0);
503 my $row = $sth->fetchrow_hashref;
505 return $$row{'inCollection'};
508 =head2 isItemInAnyCollection
510 $inCollection = isItemInAnyCollection( $itemnumber );
512 =cut
514 sub isItemInAnyCollection {
515 my ($itemnumber) = @_;
517 my $dbh = C4::Context->dbh;
519 my $sth = $dbh->prepare(
520 "SELECT itemnumber FROM collections_tracking WHERE itemnumber = ?");
521 $sth->execute($itemnumber) or return (0);
523 my $row = $sth->fetchrow_hashref;
525 $itemnumber = $row->{itemnumber};
526 if ($itemnumber) {
527 return 1;
529 else {
530 return 0;
536 __END__
538 =head1 AUTHOR
540 Kyle Hall <kylemhall@gmail.com>
542 =cut