From d495ba6e46477249e034db87d9c1d630113b2adc Mon Sep 17 00:00:00 2001 From: Baptiste Wojtkowski Date: Tue, 18 Mar 2025 14:52:35 +0100 Subject: [PATCH] Bug 37344: Add a real solution for discharge cancellation Note: if you experience issues with the behaviour of the "generate discharge" button while testing, you might want to have a look at BZ39369 TEST PLAN: Note: you will have to reset your db in the middle of the plan 0 - Set systempreference "useDischarge" to allow 1 - Add a discharge on Henry Acevedo (HA) 2 - Log in on the OPAC as HA 3 - Notice you are suspended due to discharge 4 - Remove the suspension due to the discharge 5 - Add another manual suspension to HA 6 - Log in on the OPAC as HA 7 - Notice you are still suspended due to the discharge 8 - Reset your db, Apply patch, updatedb, update db schema 9 - Add a discharge on HA, notice there are new columns 10 - Login to HA on the OPAC 11 - Notice you are suspended due to the discharge 12 - Click on the "Delete" button in the discharge panel on the intranet 13 - On the modal select "Patron asked a discharge by mistake" and submit 14 - Notice the new columns are filled (cancellation reason should be PATRON_MISTAKE 15 - Add a new manual suspension to HA 16 - Login again as HA and notice your are not seen as discharged anymore Note for the QA - I'll provide tests at the end of the week --- Koha/Patron/Discharge.pm | 62 ++++++++++++++-- .../data/mysql/atomicupdate/bug_37344.pl | 43 +++++++++++ installer/data/mysql/kohastructure.sql | 4 +- .../en/modules/admin/authorised_values.tt | 2 + .../prog/en/modules/members/discharge.tt | 72 ++++++++++++++++++- members/discharge.pl | 12 ++++ opac/opac-reserve.pl | 6 ++ 7 files changed, 195 insertions(+), 6 deletions(-) create mode 100755 installer/data/mysql/atomicupdate/bug_37344.pl diff --git a/Koha/Patron/Discharge.pm b/Koha/Patron/Discharge.pm index 6f702061..d1a256e3 100644 --- a/Koha/Patron/Discharge.pm +++ b/Koha/Patron/Discharge.pm @@ -42,21 +42,37 @@ sub can_be_discharged { return $has_pending_checkouts ? 0 : 1; } +=pod + sub is_discharged { my ($params) = @_; return unless $params->{borrowernumber}; my $borrowernumber = $params->{borrowernumber}; - my $restricted = Koha::Patrons->find($borrowernumber)->is_debarred; - my @validated = get_validated( { borrowernumber => $borrowernumber } ); + my $restricted = Koha::Patrons->find( $borrowernumber )->is_debarred; + my @validated = get_validated({borrowernumber => $borrowernumber}); - if ( $restricted && @validated ) { + if ($restricted && @validated) { return 1; } else { return 0; } } +=cut + +sub is_discharged { + my ($params) = @_; + my $discharge = Koha::Database->new->schema->resultset('Discharge')->search( + { + borrower => $params->{borrowernumber}, + validated => { "not" => undef }, + cancelled => undef, + } + ); + return $discharge->count > 0; +} + sub request { my ($params) = @_; my $borrowernumber = $params->{borrowernumber}; @@ -72,6 +88,38 @@ sub request { ); } +sub is_cancelled { + my ($params) = @_; + my $discharge = Koha::Database->new->schema->resultset('Discharge')->search( + { + discharge_id => $params->{discharge_id}, + cancelled => { "not" => undef }, + } + ); + return $discharge->count > 0; +} + +sub cancel { + my ($params) = @_; + my $discharge = Koha::Database->new->schema->resultset('Discharge')->search( + { + discharge_id => $params->{discharge_id}, + } + ); + $discharge->update( + { + cancelled => dt_from_string, + cancellation_reason => 'BORROWER_MISTAKE' + } + ); + Koha::Patron::Debarments::DelUniqueDebarment( + { + borrowernumber => $params->{borrowernumber}, + type => "DISCHARGE", + } + ); +} + sub discharge { my ($params) = @_; my $borrowernumber = $params->{borrowernumber}; @@ -96,7 +144,13 @@ sub discharge { my $rs = Koha::Database->new->schema->resultset('Discharge'); my $discharge = $rs->search( { borrower => $borrowernumber }, { order_by => { -desc => 'needed' }, rows => 1 } ); if ( $discharge->count > 0 ) { - $discharge->update( { validated => dt_from_string } ); + $discharge->update( + { + validated => dt_from_string, + cancelled => undef, + cancellation_reason => undef + } + ); } else { $rs->create( { diff --git a/installer/data/mysql/atomicupdate/bug_37344.pl b/installer/data/mysql/atomicupdate/bug_37344.pl new file mode 100755 index 00000000..f7eed7be --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_37344.pl @@ -0,0 +1,43 @@ +use Modern::Perl; +use Koha::Installer::Output qw(say_warning say_failure say_success say_info); + +return { + bug_number => "37344", + description => "Add columns cancellation_reason and cancellation_date to discharge", + up => sub { + my ($args) = @_; + my ( $dbh, $out ) = @$args{qw(dbh out)}; + + # Do you stuffs here + unless ( column_exists( 'discharges', 'cancellation_reason' ) ) { + $dbh->do( + q{ + ALTER TABLE discharges + ADD COLUMN cancelled timestamp NULL default NULL, + ADD COLUMN cancellation_reason VARCHAR(80) NULL default NULL; + } + ); + + $dbh->do( + q{ + INSERT IGNORE INTO authorised_value_categories (category_name) VALUES + ('DISCHARGE_CANCELLATION') + } + ); + $dbh->do( + q{ + INSERT IGNORE INTO authorised_values (category, authorised_value, lib) VALUES + ('DISCHARGE_CANCELLATION','BORROWER_MISTAKE', 'Borrower asked a discharge by mistake'), + ('DISCHARGE_CANCELLATION','BORROWER_STILL_NEEDS_TO_BORROW','Administration required a discharge however the borrower still needs to borrow') + } + ); + + # Print useful stuff here + # tables + say $out "Added column 'discharges.cancelled'"; + say $out "Added column 'discharges.cancellation_reason'"; + say $out "Added authorized values"; + say $out "Added authorized values"; + } + }, +}; diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql index 9b7dc7fc..4edc6054 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -2872,6 +2872,8 @@ CREATE TABLE `discharges` ( `borrower` int(11) DEFAULT NULL, `needed` timestamp NULL DEFAULT NULL, `validated` timestamp NULL DEFAULT NULL, + `cancelled` timestamp DEFAULT NULL, + `cancellation_reason`varchar(32) 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 @@ -6796,4 +6798,4 @@ CREATE TABLE `zebraqueue` ( /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; --- Dump completed on 2024-11-25 12:13:27 \ No newline at end of file +-- Dump completed on 2024-11-25 12:13:27 diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/authorised_values.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/authorised_values.tt index 519476a6..9aa93d13 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/authorised_values.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/authorised_values.tt @@ -511,6 +511,8 @@

ILL request status aliases used by the interlibrary loans module

[% CASE 'AR_CANCELLATION' %]

Reasons why an article request might have been cancelled

+ [% CASE 'DISCHARGE_CANCELLATION' %] +

Reasons why a discharge can be cancelled

[% CASE 'HSBND_FREQ' %]

Frequencies used by the housebound module. They are displayed on the housebound tab in the patron account in staff.

[% CASE 'ITEMTYPECAT' %] 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 9ebb53d7..2b036b33 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/members/discharge.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/members/discharge.tt @@ -75,6 +75,9 @@ Requested Validated + Cancelled + Reason of cancellation + Cancel @@ -82,7 +85,25 @@ [% d.needed | $KohaDates with_hours = 1 %] [% d.validated | $KohaDates with_hours = 1 %] - + [% d.cancelled | $KohaDates with_hours = 1 %] + [% d.cancellation_reason | html %] + + [% IF d.cancelled %] + Cancelled + [% ELSE %] + + + Cancel + + [% END %] + [% END %] @@ -90,9 +111,58 @@ [% END %] [% END %] + + [% MACRO jsinclude BLOCK %] [% INCLUDE 'str/members-menu.inc' %] [% Asset.js("js/members-menu.js") | $raw %] + [% END %] [% INCLUDE 'intranet-bottom.inc' %] diff --git a/members/discharge.pl b/members/discharge.pl index 7798794a..2a8b4080 100755 --- a/members/discharge.pl +++ b/members/discharge.pl @@ -52,6 +52,7 @@ my ( $template, $loggedinuser, $cookie, $flags ) = get_template_and_user( my $op = $input->param('op') // q{}; my $borrowernumber = $input->param('borrowernumber'); +my $discharge_id = $input->param('discharge_id'); unless ( C4::Context->preference('useDischarge') ) { print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber&nopermission=1"); @@ -67,6 +68,17 @@ output_and_exit_if_error( my $can_be_discharged = Koha::Patron::Discharge::can_be_discharged( { borrowernumber => $borrowernumber } ); +if ( $op eq "cud-cancel-discharge" ) { + unless ( Koha::Patron::Discharge::is_cancelled( { discharge_id => $discharge_id } ) ) { + Koha::Patron::Discharge::cancel( + { + discharge_id => $discharge_id, + borrowernumber => $borrowernumber, + } + ); + } +} + # Generating discharge if needed if ( $op eq 'cud-discharge' && $input->param('discharge') and $can_be_discharged ) { my $is_discharged = Koha::Patron::Discharge::is_discharged( diff --git a/opac/opac-reserve.pl b/opac/opac-reserve.pl index 7ffd8e08..9244c68c 100755 --- a/opac/opac-reserve.pl +++ b/opac/opac-reserve.pl @@ -157,6 +157,12 @@ if ( $patron->is_debarred ) { ); } +if ( Koha::Patron::Dicharge::is_discharged( { borrowernumber => $patron->borrowernumber } ) ) { + $template->param( + is_discharged => 1, + ); +} + my $holds = $patron->holds; my $reserves_count = $holds->count; $template->param( RESERVES => $holds->unblessed ); -- 2.30.2