From 8af52ee39075b85ea377af65ebc4b6056e2e85e8 Mon Sep 17 00:00:00 2001 From: Josef Moravec Date: Wed, 17 May 2017 12:32:10 +0000 Subject: [PATCH] Bug 18606: Get rid of TransferCollection --- C4/RotatingCollections.pm | 59 ---------------------- Koha/RotatingCollection.pm | 48 +++++++++++++++++- .../rotating_collections/transferCollection.tt | 6 +-- rotating_collections/transferCollection.pl | 29 ++++------- 4 files changed, 60 insertions(+), 82 deletions(-) diff --git a/C4/RotatingCollections.pm b/C4/RotatingCollections.pm index 466eca5..cc45eb7 100644 --- a/C4/RotatingCollections.pm +++ b/C4/RotatingCollections.pm @@ -48,68 +48,9 @@ BEGIN { require Exporter; @ISA = qw( Exporter ); @EXPORT = qw( - TransferCollection ); } -=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 - -sub TransferCollection { - my ( $colId, $colBranchcode ) = @_; - - ## Check for all necessary parameters - if ( !$colId ) { - return ( 0, 1, "NO_ID" ); - } - if ( !$colBranchcode ) { - return ( 0, 2, "NO_BRANCHCODE" ); - } - - my $dbh = C4::Context->dbh; - - my $sth; - $sth = $dbh->prepare( - "UPDATE collections - SET - colBranchcode = ? - 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 ) { - my ($status) = CheckReserves( $item->{itemnumber} ); - my @transfers = C4::Circulation::GetTransfers( $item->{itemnumber} ); - C4::Circulation::transferbook( $colBranchcode, $item->{barcode}, my $ignore_reserves = 1 ) unless ( $status eq 'Waiting' || @transfers ); - } - - return 1; - -} - =head2 isItemInThisCollection $inCollection = isItemInThisCollection( $itemnumber, $colId ); diff --git a/Koha/RotatingCollection.pm b/Koha/RotatingCollection.pm index c00f80e..7e4c2ef 100644 --- a/Koha/RotatingCollection.pm +++ b/Koha/RotatingCollection.pm @@ -1,6 +1,6 @@ package Koha::RotatingCollection; -# Copyright Josef Moravec 2016 +# Copyright Josef Moravec 2017 # # This file is part of Koha. # @@ -22,7 +22,9 @@ use Modern::Perl; use Carp; use Koha::Database; +use Koha::DateUtils; use Koha::Exceptions; +use Koha::Holds; use Koha::Items; use Koha::RotatingCollection::Trackings; @@ -115,6 +117,50 @@ sub remove_item { return $collection_tracking->delete; } +=head3 transfer + +$collection->transfer( $library_object ) + +throws + Koha::Exceptions::MissingParameter + Koha::Exceptions::ObjectNotFound + +=cut + +sub transfer { + my ( $self, $library ) = @_; + + Koha::Exceptions::MissingParameter->throw if not defined $library; + + Koha::Exceptions::ObjectNotFound->throw if ref($library) ne 'Koha::Library'; + + $self->colBranchcode( $library->branchcode )->store; + + my $from = C4::Context->userenv->{'branch'} if C4::Context->userenv; + + my $items = $self->items; + while ( my $item = $items->next ) { + my $holds = Koha::Holds->search( { + itemnumber => $item->itemnumber, + found => 'W', + } ); + + + # If no user context is defined the default from library for transfer will be the 'holding' one + $from = $item->holdingbranch if not defined $from; + unless ($holds->count || $item->get_transfer) { + my $transfer = Koha::Item::Transfer->new( { + frombranch => $from, + tobranch => $library->branchcode, + itemnumber => $item->itemnumber, + datesent => output_pref({ dt => dt_from_string, dateformat => 'iso', dateonly => 1 }), + comments => $self->colTitle, + } )->store; + $item->holdingbranch( $library->branchcode )->store; + } + } +} + =head3 type =cut diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/rotating_collections/transferCollection.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/rotating_collections/transferCollection.tt index 43f76a4..4552721 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/rotating_collections/transferCollection.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/rotating_collections/transferCollection.tt @@ -7,14 +7,14 @@ [% INCLUDE 'header.inc' %] [% INCLUDE 'cat-search.inc' %] - +
-

Transfer collection [% colTitle %]

+

Transfer collection [% collection.colTitle %]

[% IF ( transferSuccess ) %]
@@ -32,7 +32,7 @@ [% ELSE %]
- +
  1. diff --git a/rotating_collections/transferCollection.pl b/rotating_collections/transferCollection.pl index 8c48ed9..c7795bd 100755 --- a/rotating_collections/transferCollection.pl +++ b/rotating_collections/transferCollection.pl @@ -21,8 +21,8 @@ use Modern::Perl; use C4::Output; use C4::Auth; use C4::Context; -use C4::RotatingCollections; +use Koha::Libraries; use Koha::RotatingCollections; use CGI qw ( -utf8 ); @@ -32,6 +32,8 @@ my $query = new CGI; my $colId = $query->param('colId'); my $toBranch = $query->param('toBranch'); +my $collection = Koha::RotatingCollections->find( $colId ); + my ( $template, $loggedinuser, $cookie ) = get_template_and_user( { template_name => "rotating_collections/transferCollection.tt", @@ -44,31 +46,20 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user( ); ## Transfer collection -my ( $success, $errorCode, $errorMessage ); if ($toBranch) { - ( $success, $errorCode, $errorMessage ) = - TransferCollection( $colId, $toBranch ); + my $branch = Koha::Libraries->find( $toBranch ); + + my $transferred = eval ( $collection->transfer( $branch ) ); - if ($success) { + if ( $@ and not $transferred ) { + $template->param( transferFailure => 1 ); + } else { $template->param( transferSuccess => 1 ); } - else { - $template->param( - transferFailure => 1, - errorCode => $errorCode, - errorMessage => $errorMessage - ); - } } -## Get data about collection -my $collection = Koha::RotatingCollections->find($colId); - $template->param( - colId => $collection->colId, - colTitle => $collection->colTitle, - colDesc => $collection->colDesc, - colBranchcode => $collection->colBranchcode, + collection => $collection, ); output_html_with_http_headers $query, $cookie, $template->output; -- 2.1.4