From a04191298e26b3864f9e78220fb0096640ad3778 Mon Sep 17 00:00:00 2001 From: Emmi Takkinen Date: Thu, 26 Nov 2020 14:59:42 +0200 Subject: [PATCH] Bug 26498: Set default expiration date for holds Koha doesn't automatically set expiration date for holds so they can live in the system forever. This patch adds new sysprefs to control setting of default expiration date for holds. Note that expiration date is only set if input field for it is left blank. To test: 1. Apply patch and update database. 2. Enable syspref "DefaultHoldExpirationdate" and set some values to "DefaultHoldExpirationdatePeriod" and "DefaultHoldExpirationdateUnitOfTime". 3. Add some holds for patron from staff client or OPAC and leave expiration date input field blank. => Holds expiration date should be set according your settings 4. Disable "DefaultHoldExpirationdate" and repeat step 3. => Expiration date shouldn't be set. Also prove prove t/db_dependent/Hold.t Sponsored-by: Koha-Suomi Oy Signed-off-by: Andrew Isherwood Signed-off-by: Victor Grousset/tuxayo Signed-off-by: Martin Renvoize --- Koha/Hold.pm | 15 ++++++++++++ ...Set_default_expiration_date_for_holds.perl | 12 ++++++++++ installer/data/mysql/mandatory/sysprefs.sql | 3 +++ .../admin/preferences/circulation.pref | 18 +++++++++++++++ t/db_dependent/Hold.t | 23 ++++++++++++++++++- 5 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 installer/data/mysql/atomicupdate/Bug-26498-Set_default_expiration_date_for_holds.perl diff --git a/Koha/Hold.pm b/Koha/Hold.pm index 892fb20cd5..7e28bbf672 100644 --- a/Koha/Hold.pm +++ b/Koha/Hold.pm @@ -562,6 +562,21 @@ sub cancel { return $self; } +sub store { + my ($self) = @_; + + if ( C4::Context->preference('DefaultHoldExpirationdate') + and ( not defined $self->expirationdate or $self->expirationdate eq '' ) ){ + + my $period = C4::Context->preference('DefaultHoldExpirationdatePeriod'); + my $timeunit = C4::Context->preference('DefaultHoldExpirationdateUnitOfTime'); + + $self->expirationdate( dt_from_string( $self->reservedate )->add( $timeunit => $period ) ); + } + + $self = $self->SUPER::store; +} + =head3 _move_to_old my $is_moved = $hold->_move_to_old; diff --git a/installer/data/mysql/atomicupdate/Bug-26498-Set_default_expiration_date_for_holds.perl b/installer/data/mysql/atomicupdate/Bug-26498-Set_default_expiration_date_for_holds.perl new file mode 100644 index 0000000000..0caddca733 --- /dev/null +++ b/installer/data/mysql/atomicupdate/Bug-26498-Set_default_expiration_date_for_holds.perl @@ -0,0 +1,12 @@ +$DBversion = 'XXX'; # will be replaced by the RM +if( CheckVersion( $DBversion ) ) { + # you can use $dbh here like: + # $dbh->do( "ALTER TABLE biblio ADD COLUMN badtaste int" ); + + $dbh->do(q{INSERT IGNORE INTO systempreferences (variable,value,options,explanation,type) VALUES ('DefaultHoldExpirationdate','0','','Automatically set default expiration date for holds','YesNo') }); + $dbh->do(q{INSERT IGNORE INTO systempreferences (variable,value,options,explanation,type) VALUES ('DefaultHoldExpirationdatePeriod','0','','How long into the future default expiration date is set to be.','integer') }); + $dbh->do(q{INSERT IGNORE INTO systempreferences (variable,value,options,explanation,type) VALUES ('DefaultHoldExpirationdateUnitOfTime','days','days|months|years','Which unit of time is used when setting the default expiration date. ','choice') }); + + # Always end with this (adjust the bug info) + NewVersion( $DBversion, 26498, "Bug 26498 - Add option to set a default expire date for holds at reservation time"); +} diff --git a/installer/data/mysql/mandatory/sysprefs.sql b/installer/data/mysql/mandatory/sysprefs.sql index 1034313061..54c0adb086 100644 --- a/installer/data/mysql/mandatory/sysprefs.sql +++ b/installer/data/mysql/mandatory/sysprefs.sql @@ -154,6 +154,9 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, ` ('decreaseLoanHighHoldsValue',NULL,'','Specifies a threshold for the minimum number of holds needed to trigger a reduction in loan duration (used with decreaseLoanHighHolds)','Integer'), ('DefaultClassificationSource','ddc',NULL,'Default classification scheme used by the collection. E.g., Dewey, LCC, etc.','ClassSources'), ('DefaultCountryField008','','','Fill in the default country code for field 008 Range 15-17 of MARC21 - Place of publication, production, or execution. See MARC Code List for Countries','Free'), +('DefaultHoldExpirationdate','0','','Automatically set expiration date for holds','YesNo'), +('DefaultHoldExpirationdatePeriod','0','','How long into the future default expiration date is set to be.','integer'), +('DefaultHoldExpirationdateUnitOfTime','days','days|months|years','Which unit of time is used when setting the default expiration date. ','choice'), ('DefaultLanguageField008','','','Fill in the default language for field 008 Range 35-37 of MARC21 records (e.g. eng, nor, ger, see MARC Code List for Languages)','Free'), ('DefaultLongOverdueSkipLostStatuses', '', NULL, 'Skip these lost statuses by default in longoverdue.pl', 'Free'), ('DefaultLongOverdueChargeValue', '', NULL, "Charge a lost item to the borrower's account when the LOST value of the item changes to n.", 'integer'), diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/circulation.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/circulation.pref index 9acc59f50e..f1d3ac26aa 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/circulation.pref +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/circulation.pref @@ -895,6 +895,24 @@ Circulation: - pref: UpdateItemWhenLostFromHoldList type: textarea syntax: text/x-yaml + - + - pref: DefaultHoldExpirationdate + default: 0 + choices: + yes: Set + no: Don't set + - default expiration date for holds automatically. + - If enabled, set expiration date + - pref: DefaultHoldExpirationdatePeriod + default: 0 + class: integer + - pref: DefaultHoldExpirationdateUnitOfTime + default: days + choices: + days: days + months: months + years: years + - from reserve date. Interlibrary loans: - - pref: ILLModule diff --git a/t/db_dependent/Hold.t b/t/db_dependent/Hold.t index 5b71b64292..c86807e3a9 100755 --- a/t/db_dependent/Hold.t +++ b/t/db_dependent/Hold.t @@ -29,7 +29,7 @@ use Koha::Item; use Koha::DateUtils; use t::lib::TestBuilder; -use Test::More tests => 33; +use Test::More tests => 34; use Test::Exception; use Test::Warn; @@ -142,6 +142,27 @@ ok( !$hold->is_at_destination(), "Waiting hold where hold branchcode is not the $item->holdingbranch( $branches[1]->{branchcode} ); ok( $hold->is_at_destination(), "Waiting hold where hold branchcode is the same as the item's holdingbranch is at destination" ); +# Test setting expiration date +t::lib::Mocks::mock_preference( 'DefaultHoldExpirationdate', 1 ); +t::lib::Mocks::mock_preference( 'DefaultHoldExpirationdatePeriod', 2 ); +t::lib::Mocks::mock_preference( 'DefaultHoldExpirationdateUnitOfTime', 'years' ); + +my $hold_2 = Koha::Hold->new( + { + biblionumber => $biblionumber, + itemnumber => $item->id(), + reservedate => '2020-11-30', + waitingdate => '2020-11-30', + borrowernumber => $borrower->{borrowernumber}, + branchcode => $branches[1]->{branchcode}, + suspend => 0, + } +); +$hold_2->store(); + +my $expected_date = dt_from_string( $hold_2->reservedate )->add( years => 2); +is($hold_2->expirationdate->ymd, $expected_date->ymd,'Expiration date is correctly set.'); + $schema->storage->txn_rollback(); subtest "delete() tests" => sub { -- 2.20.1