From da06f6863ab65c392eeee89fc2d59476484cda30 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Mon, 8 Sep 2025 18:43:44 +0100 Subject: [PATCH] Bug 3492: Database migration for hold fees to circulation rules This commit adds the database migration to move hold fee configuration from patron categories to circulation rules, providing more granular control over hold fees based on patron category, item type, and branch. The migration: - Creates circulation rules for existing categories.reservefee values - Removes the deprecated reservefee column from categories table --- installer/data/mysql/atomicupdate/bug_3492.pl | 79 +++++++++++++++++++ installer/data/mysql/kohastructure.sql | 1 - 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 installer/data/mysql/atomicupdate/bug_3492.pl diff --git a/installer/data/mysql/atomicupdate/bug_3492.pl b/installer/data/mysql/atomicupdate/bug_3492.pl new file mode 100644 index 00000000000..f3ff016cb42 --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_3492.pl @@ -0,0 +1,79 @@ +use Modern::Perl; +use Koha::Installer::Output qw(say_warning say_failure say_success say_info); + +return { + bug_number => "3492", + description => "Migrate reservefee from categories to circulation rules and remove deprecated column", + up => sub { + my ($args) = @_; + my ( $dbh, $out ) = @$args{qw(dbh out)}; + + # Check if the reservefee column still exists + my $column_exists = $dbh->selectrow_array( + q{ + SELECT COUNT(*) + FROM INFORMATION_SCHEMA.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = 'categories' + AND COLUMN_NAME = 'reservefee' + } + ); + + if ($column_exists) { + + # Check if we have any existing reservefees to migrate + my $existing_fees = $dbh->selectall_arrayref( + q{ + SELECT categorycode, reservefee + FROM categories + WHERE reservefee IS NOT NULL AND reservefee > 0 + }, { Slice => {} } + ); + + if (@$existing_fees) { + say_info( + $out, + "Migrating " + . scalar(@$existing_fees) + . " existing hold fees from categories to circulation rules..." + ); + + # Migrate existing reservefee values to circulation_rules + my $insert_rule = $dbh->prepare( + q{ + INSERT IGNORE INTO circulation_rules + (branchcode, categorycode, itemtype, rule_name, rule_value) + VALUES (NULL, ?, NULL, 'hold_fee', ?) + } + ); + + my $migrated = 0; + for my $fee (@$existing_fees) { + $insert_rule->execute( $fee->{categorycode}, $fee->{reservefee} ); + if ( $insert_rule->rows ) { + $migrated++; + } + } + + say_success( $out, "Successfully migrated $migrated hold fee rules to circulation_rules table." ); + + if ( $migrated < @$existing_fees ) { + say_warning( $out, "Some rules may have already existed and were not overwritten." ); + } + } else { + say_info( $out, "No existing hold fees found in categories table to migrate." ); + } + + # Remove the deprecated reservefee column + say_info( $out, "Removing deprecated reservefee column from categories table..." ); + $dbh->do(q{ALTER TABLE categories DROP COLUMN reservefee}); + say_success( $out, "Successfully removed reservefee column from categories table." ); + + } else { + say_info( $out, "The reservefee column has already been removed from the categories table." ); + } + + say_info( $out, "Hold fees can now be configured in Administration > Circulation and fine rules." ); + say_success( $out, "Migration complete: Hold fees are now fully managed through circulation rules." ); + }, +}; diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql index b90542fcf4b..61cec429db6 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -1803,7 +1803,6 @@ CREATE TABLE `categories` ( `dateofbirthrequired` tinyint(1) DEFAULT NULL COMMENT 'the minimum age required for the patron category', `enrolmentfee` decimal(28,6) DEFAULT NULL COMMENT 'enrollment fee for the patron', `overduenoticerequired` tinyint(1) DEFAULT NULL COMMENT 'are overdue notices sent to this patron category (1 for yes, 0 for no)', - `reservefee` decimal(28,6) DEFAULT NULL COMMENT 'cost to place holds', `hidelostitems` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'are lost items shown to this category (1 for yes, 0 for no)', `category_type` varchar(1) NOT NULL DEFAULT 'A' COMMENT 'type of Koha patron (Adult, Child, Professional, Organizational, Statistical, Staff)', `BlockExpiredPatronOpacActions` varchar(128) NOT NULL DEFAULT 'follow_syspref_BlockExpiredPatronOpacActions' COMMENT 'specific actions expired patrons of this category are blocked from performing or if the BlockExpiredPatronOpacActions system preference is to be followed', -- 2.51.0