From c34c8ee853c4ec6a457c5ae8f75729507e242207 Mon Sep 17 00:00:00 2001 From: Matthias Meusburger Date: Tue, 9 Jun 2015 14:35:07 +0200 Subject: [PATCH] Bug 14368: Add discharges history. Currently, once a user is discharged, lifting the discharge debarment does not allow the user to ask for a discharge again. This patch adds the ability for a user to be discharged several times and to keep an history of the discharges. Test plan: - As a user, ask for a discharge. - As a librarian, allow the discharge. - Check that the requested and validated dates appear on the discharge panel of the borrower in the admin interface. - Lift the discharge debarment of the user. - Check that you can repeat these steps and that the new discharges are added in the discharge panel of the borrower in the admin interface. Please note that you will need to run misc/devel/update_dbix_class_files.pl after the SQL update. Adding a primary key to the discharges table was mandatory for DBIX::Class to be able to query with order_by. Signed-off-by: Indranil Das Gupta (L2C2 Technologies) Signed-off-by: Jonathan Druart Signed-off-by: Jonathan Druart Signed-off-by: Tomas Cohen Arazi --- Koha/Borrower/Discharge.pm | 35 +++++++++++++++++-- .../bug_14368_Add_discharges_history.sql | 1 + installer/data/mysql/kohastructure.sql | 2 ++ .../prog/en/modules/members/discharge.tt | 22 ++++++++++++ members/discharge.pl | 6 ++++ opac/opac-discharge.pl | 2 +- t/db_dependent/Borrower_Discharge.t | 7 ++++ 7 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 installer/data/mysql/atomicupdate/bug_14368_Add_discharges_history.sql diff --git a/Koha/Borrower/Discharge.pm b/Koha/Borrower/Discharge.pm index ba0216da1e..bbfab56d77 100644 --- a/Koha/Borrower/Discharge.pm +++ b/Koha/Borrower/Discharge.pm @@ -45,12 +45,27 @@ sub can_be_discharged { } } +sub is_discharged { + my ($params) = @_; + return unless $params->{borrowernumber}; + my $borrowernumber = $params->{borrowernumber}; + + + my $restricted = Koha::Borrower::Debarments::IsDebarred($borrowernumber); + my $validated = get_validated({borrowernumber => $borrowernumber}); + + if ($restricted && $validated) { + return 1; + } else { + return 0; + } +} + sub request { my ($params) = @_; my $borrowernumber = $params->{borrowernumber}; return unless $borrowernumber; return unless can_be_discharged({ borrowernumber => $borrowernumber }); - return if count($params); return $rs->create({ borrower => $borrowernumber, @@ -76,7 +91,7 @@ sub discharge { }); # Generate the discharge - my $discharge = $rs->search({ borrower => $borrowernumber }); + my $discharge = $rs->search({ borrower => $borrowernumber }, { order_by => { -desc => 'needed' }, rows => 1 }); if( $discharge->count > 0 ) { $discharge->update({ validated => dt_from_string }); } @@ -149,4 +164,20 @@ sub get_pendings { return \@rs; } +sub get_validated { + my ($params) = @_; + my $branchcode = $params->{branchcode}; + my $borrowernumber = $params->{borrowernumber}; + + my $cond = { + 'me.validated' => { '!=', undef }, + ( defined $borrowernumber ? ( 'me.borrower' => $borrowernumber ) : () ), + ( defined $branchcode ? ( 'borrower.branchcode' => $branchcode ) : () ), + }; + + my @rs = $rs->search( $cond, { join => 'borrower' } ); + return \@rs; +} + + 1; diff --git a/installer/data/mysql/atomicupdate/bug_14368_Add_discharges_history.sql b/installer/data/mysql/atomicupdate/bug_14368_Add_discharges_history.sql new file mode 100644 index 0000000000..2c822ebdf1 --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_14368_Add_discharges_history.sql @@ -0,0 +1 @@ +ALTER TABLE discharges ADD COLUMN discharge_id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST; diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql index be15117972..dab7538aed 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -3531,9 +3531,11 @@ CREATE TABLE items_search_fields ( DROP TABLE IF EXISTS discharges; CREATE TABLE discharges ( + discharge_id int(11) NOT NULL AUTO_INCREMENT, borrower int(11) DEFAULT NULL, needed timestamp NULL DEFAULT NULL, validated timestamp NULL DEFAULT NULL, + PRIMARY KEY (discharge_id), KEY borrower_discharges_ibfk1 (borrower), CONSTRAINT borrower_discharges_ibfk1 FOREIGN KEY (borrower) REFERENCES borrowers (borrowernumber) ON DELETE CASCADE ON UPDATE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/members/discharge.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/members/discharge.tt index 00d3a3a3ea..e239a3e57e 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/members/discharge.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/members/discharge.tt @@ -1,3 +1,4 @@ +[% USE KohaDates %] [% INCLUDE 'doc-head-open.inc' %] Koha › Patrons › [% IF (unknowuser) %]Patron does not exist[% ELSE %]Discharge for [% firstname %] [% surname %] ([% cardnumber %])[% END %] [% INCLUDE 'doc-head-close.inc' %] @@ -37,6 +38,27 @@ [% END %] + +[% IF validated_discharges %] +

Already validated discharges

+ + + + + + + + + [% FOR d IN validated_discharges %] + + + + + [% END %] + +
RequestedValidated
[% d.needed | $KohaDates with_hours = 1 %][% d.validated | $KohaDates with_hours = 1 %]
+[% END %] + diff --git a/members/discharge.pl b/members/discharge.pl index 7c73af9985..9c0fdef27d 100755 --- a/members/discharge.pl +++ b/members/discharge.pl @@ -98,6 +98,11 @@ if ( $input->param('borrowernumber') ) { } } + # Already generated discharges + my $validated_discharges = Koha::Borrower::Discharge::get_validated({ + branchcode => $data->{'branchcode'}, + }); + $template->param( borrowernumber => $borrowernumber, biblionumber => $data->{'biblionumber'}, @@ -120,6 +125,7 @@ if ( $input->param('borrowernumber') ) { branchcode => $data->{'branchcode'}, has_reserves => $has_reserves, can_be_discharged => $can_be_discharged, + validated_discharges => $validated_discharges, ); } diff --git a/opac/opac-discharge.pl b/opac/opac-discharge.pl index 5955a3e720..b8d8a4fe6a 100755 --- a/opac/opac-discharge.pl +++ b/opac/opac-discharge.pl @@ -92,7 +92,7 @@ else { validated => 1, }); $template->param( - available => $available, + available => $available && Koha::Borrower::Discharge::is_discharged({borrowernumber => $loggedinuser}), pending => $pending, ); } diff --git a/t/db_dependent/Borrower_Discharge.t b/t/db_dependent/Borrower_Discharge.t index b585db29a2..6a4b8063e2 100644 --- a/t/db_dependent/Borrower_Discharge.t +++ b/t/db_dependent/Borrower_Discharge.t @@ -49,6 +49,13 @@ is( Koha::Borrower::Discharge::can_be_discharged({ borrowernumber => $borrowernu is(Koha::Borrower::Discharge::generate_as_pdf,undef,"Confirm failure when lacking borrower number"); +# Verify that the user is not discharged anymore if the restriction has been lifted +Koha::Borrower::Discharge::discharge({ borrowernumber => $borrowernumber }); +is( Koha::Borrower::Discharge::is_discharged({ borrowernumber => $borrowernumber }), 1, 'The patron has been discharged' ); +is(Koha::Borrower::Debarments::IsDebarred($borrowernumber), '9999-12-31', 'The patron has been debarred after discharge'); +Koha::Borrower::Debarments::DelDebarment($borrowernumber); +is( Koha::Borrower::Discharge::is_discharged({ borrowernumber => $borrowernumber }), 1, 'The patron is not discharged after the restriction has been lifted' ); + # Check if PDF::FromHTML is installed. my $check = eval { require PDF::FromHTML; }; -- 2.39.5