@@ -, +, @@ holds to fill the next hold 1) Apply this patch 2) Run updatedatabase.pl 3) Create a record with one item 4) Place two holds on that record 5) Check in the item and set it to waiting for the first patron 6) Set ReservesMaxPickUpDelay to 1 7) Enable ExpireReservesMaxPickUpDelay 8) Enable ExpireReservesAutoFill 9) Set an email address in ExpireReservesAutoFillEmail --- C4/Reserves.pm | 44 ++++- Koha/Hold.pm | 15 ++ .../data/mysql/atomicupdate/bug_14364.perl | 22 +++ .../mysql/pl-PL/mandatory/sample_notices.sql | 10 + installer/data/mysql/sysprefs.sql | 2 + .../admin/preferences/circulation.pref | 9 + t/db_dependent/Holds/ExpireReservesAutoFill.t | 186 ++++++++++++++++++ 7 files changed, 284 insertions(+), 4 deletions(-) create mode 100644 installer/data/mysql/atomicupdate/bug_14364.perl create mode 100755 t/db_dependent/Holds/ExpireReservesAutoFill.t --- a/C4/Reserves.pm +++ a/C4/Reserves.pm @@ -901,6 +901,7 @@ sub CancelExpiredReserves { if ( $hold->found eq 'W' ) { $cancel_params->{charge_cancel_fee} = 1; } + $cancel_params->{autofill} = C4::Context->preference('ExpireReservesAutoFill'); $hold->cancel( $cancel_params ); } } @@ -1101,7 +1102,10 @@ sub ModReserveStatus { =head2 ModReserveAffect - &ModReserveAffect($itemnumber,$borrowernumber,$diffBranchSend,$reserve_id); +&ModReserveAffect( + $itemnumber, $borrowernumber, $diffBranchSend, + $reserve_id, $notify_library +); This function affect an item and a status for a given reserve, either fetched directly by record_id, or by borrowernumber and itemnumber or biblionumber. If only biblionumber @@ -1115,7 +1119,7 @@ take care of the waiting status =cut sub ModReserveAffect { - my ( $itemnumber, $borrowernumber, $transferToDo, $reserve_id ) = @_; + my ( $itemnumber, $borrowernumber, $transferToDo, $reserve_id, $notify_library ) = @_; my $dbh = C4::Context->dbh; # we want to attach $itemnumber to $borrowernumber, find the biblionumber @@ -1142,8 +1146,8 @@ sub ModReserveAffect { $hold->itemnumber($itemnumber); $hold->set_waiting($transferToDo); + _koha_notify_reserve( $hold->reserve_id, $notify_library ) unless $already_on_shelf; if( !$transferToDo ){ - _koha_notify_reserve( $hold->reserve_id ) unless $already_on_shelf; my $transfers = Koha::Item::Transfers->search({ itemnumber => $itemnumber, datearrived => undef @@ -1153,7 +1157,6 @@ sub ModReserveAffect { }; } - _FixPriority( { biblionumber => $biblionumber } ); my $item = Koha::Items->find($itemnumber); if ( $item->location && $item->location eq 'CART' @@ -1750,6 +1753,8 @@ The following tables are availalbe witin the notice: sub _koha_notify_reserve { my $reserve_id = shift; + my $notify_library = shift; + my $hold = Koha::Holds->find($reserve_id); my $borrowernumber = $hold->borrowernumber; @@ -1816,6 +1821,37 @@ sub _koha_notify_reserve { &$send_notification('print', 'HOLD'); } + if ($notify_library) { + my $letter = C4::Letters::GetPreparedLetter( + module => 'reserves', + letter_code => 'HOLD_CHANGED', + branchcode => $hold->branchcode, + substitute => { today => output_pref( dt_from_string ) }, + tables => { + 'branches' => $library, + 'borrowers' => $patron->unblessed, + 'biblio' => $hold->biblionumber, + 'biblioitems' => $hold->biblionumber, + 'reserves' => $hold->unblessed, + 'items' => $hold->itemnumber, + }, + ); + + my $email = + C4::Context->preference('ExpireReservesAutoFillEmail') + || $library->{branchemail} + || C4::Context->preference('KohaAdminEmailAddress'); + + C4::Letters::EnqueueLetter( + { + letter => $letter, + borrowernumber => $borrowernumber, + message_transport_type => 'email', + from_address => $email, + to_address => $email, + } + ); + } } =head2 _ShiftPriorityByDateAndPriority --- a/Koha/Hold.pm +++ a/Koha/Hold.pm @@ -25,6 +25,7 @@ use Data::Dumper qw(Dumper); use C4::Context qw(preference); use C4::Log; +use C4::Reserves; use Koha::DateUtils qw(dt_from_string output_pref); use Koha::Patrons; @@ -352,6 +353,9 @@ Cancel a hold: sub cancel { my ( $self, $params ) = @_; + + my $autofill_next = $params->{autofill} && $self->itemnumber && $self->found && $self->found eq 'W'; + $self->_result->result_source->schema->txn_do( sub { $self->cancellationdate( dt_from_string->strftime( '%Y-%m-%d %H:%M:%S' ) ); @@ -383,6 +387,17 @@ sub cancel { if C4::Context->preference('HoldsLog'); } ); + + if ($autofill_next) { + my ( undef, $next_hold ) = C4::Reserves::CheckReserves( $self->itemnumber ); + if ($next_hold) { + my $is_transfer = $self->branchcode ne $next_hold->{branchcode}; + + C4::Reserves::ModReserveAffect( $self->itemnumber, $self->borrowernumber, $is_transfer, $next_hold->{reserve_id}, $autofill_next ); + C4::Reserves::ModItemTransfer( $self->itemnumber, $self->branchcode, $next_hold->{branchcode} ) if $is_transfer; + } + } + return $self; } --- a/installer/data/mysql/atomicupdate/bug_14364.perl +++ a/installer/data/mysql/atomicupdate/bug_14364.perl @@ -0,0 +1,22 @@ +$DBversion = 'XXX'; # will be replaced by the RM +if( CheckVersion( $DBversion ) ) { + $dbh->do(q{ + INSERT IGNORE INTO systempreferences ( `variable`, `value`, `options`, `explanation`, `type` ) VALUES + ('ExpireReservesAutoFill','0',NULL,'Automatically fill the next hold with a automatically canceled expired waiting hold.','YesNo'), + ('ExpireReservesAutoFillEmail','', NULL,'If ExpireReservesAutoFill and an email is defined here, the email notification for the change in the hold will be sent to this address.','Free'); + }); + + $dbh->do(q{ + INSERT INTO letter(module,code,branchcode,name,is_html,title,content,message_transport_type) + VALUES ( 'reserves', 'HOLD_CHANGED', '', 'Canceled Hold Available for Different Patron', '0', 'Canceled Hold Available for Different Patron', 'The patron picking up <> (<>) has changed to <> <> (<>). + +Please update the hold information for this item. + +Title: <> +Author: <> +Copy: <> +Pickup location: <>', 'email'); + }); + + NewVersion( $DBversion, 14364, "Allow automatically canceled expired waiting holds to fill the next hold"); +} --- a/installer/data/mysql/pl-PL/mandatory/sample_notices.sql +++ a/installer/data/mysql/pl-PL/mandatory/sample_notices.sql @@ -376,3 +376,13 @@ INSERT IGNORE INTO `letter` (`module`, `code`, `branchcode`, `name`, `is_html`, ', 'print', 'default'); + +INSERT INTO letter(module,code,branchcode,name,is_html,title,content,message_transport_type) + VALUES ( 'reserves', 'HOLD_CHANGED', '', 'Canceled Hold Available for Different Patron', '0', 'Canceled Hold Available for Different Patron', 'The patron picking up <> (<>) has changed to <> <> (<>). + +Please update the hold information for this item. + +Title: <> +Author: <> +Copy: <> +Pickup location: <>', 'email'); --- a/installer/data/mysql/sysprefs.sql +++ a/installer/data/mysql/sysprefs.sql @@ -184,6 +184,8 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` ('EnhancedMessagingPreferences','1','','If ON, allows patrons to select to receive additional messages about items due or nearly due.','YesNo'), ('EnhancedMessagingPreferencesOPAC', '1', NULL, 'If ON, show patrons messaging setting on the OPAC.', 'YesNo'), ('expandedSearchOption','0',NULL,'If ON, set advanced search to be expanded by default','YesNo'), +('ExpireReservesAutoFill','0',NULL,'Automatically fill the next hold with a automatically canceled expired waiting hold.','YesNo'), +('ExpireReservesAutoFillEmail','', NULL,'If ExpireReservesAutoFill and an email is defined here, the email notification for the change in the hold will be sent to this address.','Free'), ('ExpireReservesMaxPickUpDelay','0','','Enabling this allows holds to expire automatically if they have not been picked by within the time period specified in ReservesMaxPickUpDelay','YesNo'), ('ExpireReservesMaxPickUpDelayCharge','0',NULL,'If ExpireReservesMaxPickUpDelay is enabled, and this field has a non-zero value, than a borrower whose waiting hold has expired will be charged this amount.','free'), ('ExpireReservesOnHolidays', '1', NULL, 'If false, reserves at a library will not be canceled on days the library is not open.', 'YesNo'), --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/circulation.pref +++ a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/circulation.pref @@ -676,6 +676,15 @@ Circulation: yes: Allow no: "Don't allow" - "holds to expire automatically if they have not been picked by within the time period specified in ReservesMaxPickUpDelay.
NOTE: This system preference requires the misc/cronjobs/holds/cancel_expired_holds.pl cronjob. Ask your system administrator to schedule it." + - pref: ExpireReservesAutoFill + choices: + yes: "Do" + no: "Don't" + - automatically fill the next hold using the item. If enabled, an email notification will be sent to either the email address defined in ExpireReservesAutoFillEmail, the item's holding library's email address, or the email address defined in KohaAdminEmailAddress in that order. + - + - Send email notification of the new hold filled with a canceled item to + - pref: ExpireReservesAutoFillEmail + - . If no address is defined here, the email will be sent to either the item's holding library or the email address defined in KohaAdminEmailAddress in that order. - - If using ExpireReservesMaxPickUpDelay, charge a borrower who allows their waiting hold to expire a fee of - pref: ExpireReservesMaxPickUpDelayCharge --- a/t/db_dependent/Holds/ExpireReservesAutoFill.t +++ a/t/db_dependent/Holds/ExpireReservesAutoFill.t @@ -0,0 +1,186 @@ +#!/usr/bin/perl + +use Modern::Perl; + +use Test::More tests => 3; + +use t::lib::Mocks; +use t::lib::TestBuilder; + +use MARC::Record; + +use C4::Context; +use C4::Biblio; +use C4::Items; +use Koha::Database; +use Koha::Holds; + +BEGIN { + use FindBin; + use lib $FindBin::Bin; + use_ok('C4::Reserves'); +} + +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; + +my $builder = t::lib::TestBuilder->new(); +my $dbh = C4::Context->dbh; + +# Create two random branches +my $library_1 = $builder->build({ source => 'Branch' })->{ branchcode }; +my $library_2 = $builder->build({ source => 'Branch' })->{ branchcode }; + +my $biblio = $builder->build_sample_biblio({ itemtype => 'DUMMY' }); +my $biblionumber = $biblio->id; + +# Create item instance for testing. +my $itemnumber = $builder->build_sample_item({ library => $library_1, biblionumber => $biblio->biblionumber })->itemnumber; + +my $patron_1 = $builder->build( { source => 'Borrower' } ); +my $patron_2 = $builder->build( { source => 'Borrower' } ); +my $patron_3 = $builder->build( { source => 'Borrower' } ); + +subtest 'Test automatically canceled expired waiting holds to fill the next hold, without a transfer' => sub { + plan tests => 8; + + $dbh->do('DELETE FROM reserves'); + $dbh->do('DELETE FROM message_queue'); + + # Add a hold on the item for each of our patrons + my $hold_1 = Koha::Hold->new( + { + priority => 0, + borrowernumber => $patron_1->{borrowernumber}, + branchcode => $library_1, + biblionumber => $biblionumber, + itemnumber => $itemnumber, + found => 'W', + reservedate => '1900-01-01', + waitingdate => '1900-01-01', + expirationdate => '1900-01-01', + lowestPriority => 0, + suspend => 0, + } + )->store(); + my $hold_2 = Koha::Hold->new( + { + priority => 1, + borrowernumber => $patron_2->{borrowernumber}, + branchcode => $library_1, + biblionumber => $biblionumber, + itemnumber => $itemnumber, + reservedate => '1900-01-01', + expirationdate => '9999-01-01', + lowestPriority => 0, + suspend => 0, + } + )->store(); + my $hold_3 = Koha::Hold->new( + { + priority => 2, + borrowernumber => $patron_2->{borrowernumber}, + branchcode => $library_1, + biblionumber => $biblionumber, + itemnumber => $itemnumber, + reservedate => '1900-01-01', + expirationdate => '9999-01-01', + lowestPriority => 0, + suspend => 0, + } + )->store(); + + # Test CancelExpiredReserves + t::lib::Mocks::mock_preference( 'ExpireReservesMaxPickUpDelay', 1 ); + t::lib::Mocks::mock_preference( 'ReservesMaxPickUpDelay', 1 ); + t::lib::Mocks::mock_preference( 'ExpireReservesOnHolidays', 1 ); + t::lib::Mocks::mock_preference( 'ExpireReservesAutoFill', 1 ); + t::lib::Mocks::mock_preference( 'ExpireReservesAutoFillEmail', + 'kyle@example.com' ); + + CancelExpiredReserves(); + + my @holds = Koha::Holds->search( {}, { order_by => 'priority' } ); + $hold_2 = $holds[0]; + $hold_3 = $holds[1]; + + is( @holds, 2, 'Found 2 holds' ); + is( $hold_2->priority, 0, 'Next hold in line now has priority of 0' ); + is( $hold_2->found, 'W', 'Next hold in line is now set to waiting' ); + + my @messages = $schema->resultset('MessageQueue') + ->search( { letter_code => 'HOLD_CHANGED' } ); + is( @messages, 1, 'Found 1 message in the message queue' ); + is( $messages[0]->to_address, 'kyle@example.com', 'Message sent to correct email address' ); + + $hold_2->expirationdate('1900-01-01')->store(); + + CancelExpiredReserves(); + + @holds = Koha::Holds->search( {}, { order_by => 'priority' } ); + $hold_3 = $holds[0]; + + is( @holds, 1, 'Found 1 hold' ); + is( $hold_3->priority, 0, 'Next hold in line now has priority of 0' ); + is( $hold_3->found, 'W', 'Next hold in line is now set to waiting' ); +}; + +subtest 'Test automatically canceled expired waiting holds to fill the next hold, with a transfer' => sub { + plan tests => 6; + + $dbh->do('DELETE FROM reserves'); + $dbh->do('DELETE FROM message_queue'); + + # Add a hold on the item for each of our patrons + my $hold_1 = Koha::Hold->new( + { + priority => 0, + borrowernumber => $patron_1->{borrowernumber}, + branchcode => $library_1, + biblionumber => $biblionumber, + itemnumber => $itemnumber, + found => 'W', + reservedate => '1900-01-01', + waitingdate => '1900-01-01', + expirationdate => '1900-01-01', + lowestPriority => 0, + suspend => 0, + } + )->store(); + my $hold_2 = Koha::Hold->new( + { + priority => 1, + borrowernumber => $patron_2->{borrowernumber}, + branchcode => $library_2, + biblionumber => $biblionumber, + itemnumber => $itemnumber, + reservedate => '1900-01-01', + expirationdate => '9999-01-01', + lowestPriority => 0, + suspend => 0, + } + )->store(); + + # Test CancelExpiredReserves + t::lib::Mocks::mock_preference( 'ExpireReservesMaxPickUpDelay', 1 ); + t::lib::Mocks::mock_preference( 'ReservesMaxPickUpDelay', 1 ); + t::lib::Mocks::mock_preference( 'ExpireReservesOnHolidays', 1 ); + t::lib::Mocks::mock_preference( 'ExpireReservesAutoFill', 1 ); + t::lib::Mocks::mock_preference( 'ExpireReservesAutoFillEmail', + 'kyle@example.com' ); + + CancelExpiredReserves(); + + my @holds = Koha::Holds->search( {}, { order_by => 'priority' } ); + $hold_2 = $holds[0]; + + is( @holds, 1, 'Found 1 hold' ); + is( $hold_2->priority, 0, 'Next hold in line now has priority of 0' ); + is( $hold_2->found, 'T', 'Next hold in line is now set to in transit' ); + is( $hold_2->branchcode, $library_2, "Next hold in line has correct branchcode" ); + + my @messages = $schema->resultset('MessageQueue') + ->search( { letter_code => 'HOLD_CHANGED' } ); + is( @messages, 1, 'Found 1 message in the message queue' ); + is( $messages[0]->to_address, 'kyle@example.com', 'Message sent to correct email address' ); +}; --