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

Sponsored-by: OpenFifth <https://openfifth.co.uk>
Signed-off-by: Jackie Usher <jackie.usher@westsussex.gov.uk>
Signed-off-by: Tomás Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
This commit is contained in:
Martin Renvoize 2025-09-08 18:43:44 +01:00 committed by Lucas Gass
parent f4ddcd98bc
commit f5bc5be08d
No known key found for this signature in database
GPG Key ID: 60B447398B0D36CA
2 changed files with 79 additions and 1 deletions

View File

@ -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." );
},
};

View File

@ -1810,7 +1810,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',