|
Line 0
Link Here
|
|
|
1 |
use Modern::Perl; |
| 2 |
use Koha::Installer::Output qw(say_warning say_failure say_success say_info); |
| 3 |
|
| 4 |
return { |
| 5 |
bug_number => "3492", |
| 6 |
description => "Migrate reservefee from categories to circulation rules and remove deprecated column", |
| 7 |
up => sub { |
| 8 |
my ($args) = @_; |
| 9 |
my ( $dbh, $out ) = @$args{qw(dbh out)}; |
| 10 |
|
| 11 |
# Check if the reservefee column still exists |
| 12 |
my $column_exists = $dbh->selectrow_array( |
| 13 |
q{ |
| 14 |
SELECT COUNT(*) |
| 15 |
FROM INFORMATION_SCHEMA.COLUMNS |
| 16 |
WHERE TABLE_SCHEMA = DATABASE() |
| 17 |
AND TABLE_NAME = 'categories' |
| 18 |
AND COLUMN_NAME = 'reservefee' |
| 19 |
} |
| 20 |
); |
| 21 |
|
| 22 |
if ($column_exists) { |
| 23 |
|
| 24 |
# Check if we have any existing reservefees to migrate |
| 25 |
my $existing_fees = $dbh->selectall_arrayref( |
| 26 |
q{ |
| 27 |
SELECT categorycode, reservefee |
| 28 |
FROM categories |
| 29 |
WHERE reservefee IS NOT NULL AND reservefee > 0 |
| 30 |
}, { Slice => {} } |
| 31 |
); |
| 32 |
|
| 33 |
if (@$existing_fees) { |
| 34 |
say_info( |
| 35 |
$out, |
| 36 |
"Migrating " |
| 37 |
. scalar(@$existing_fees) |
| 38 |
. " existing hold fees from categories to circulation rules..." |
| 39 |
); |
| 40 |
|
| 41 |
# Migrate existing reservefee values to circulation_rules |
| 42 |
my $insert_rule = $dbh->prepare( |
| 43 |
q{ |
| 44 |
INSERT IGNORE INTO circulation_rules |
| 45 |
(branchcode, categorycode, itemtype, rule_name, rule_value) |
| 46 |
VALUES (NULL, ?, NULL, 'hold_fee', ?) |
| 47 |
} |
| 48 |
); |
| 49 |
|
| 50 |
my $migrated = 0; |
| 51 |
for my $fee (@$existing_fees) { |
| 52 |
$insert_rule->execute( $fee->{categorycode}, $fee->{reservefee} ); |
| 53 |
if ( $insert_rule->rows ) { |
| 54 |
$migrated++; |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
say_success( $out, "Successfully migrated $migrated hold fee rules to circulation_rules table." ); |
| 59 |
|
| 60 |
if ( $migrated < @$existing_fees ) { |
| 61 |
say_warning( $out, "Some rules may have already existed and were not overwritten." ); |
| 62 |
} |
| 63 |
} else { |
| 64 |
say_info( $out, "No existing hold fees found in categories table to migrate." ); |
| 65 |
} |
| 66 |
|
| 67 |
# Remove the deprecated reservefee column |
| 68 |
say_info( $out, "Removing deprecated reservefee column from categories table..." ); |
| 69 |
$dbh->do(q{ALTER TABLE categories DROP COLUMN reservefee}); |
| 70 |
say_success( $out, "Successfully removed reservefee column from categories table." ); |
| 71 |
|
| 72 |
} else { |
| 73 |
say_info( $out, "The reservefee column has already been removed from the categories table." ); |
| 74 |
} |
| 75 |
|
| 76 |
say_info( $out, "Hold fees can now be configured in Administration > Circulation and fine rules." ); |
| 77 |
say_success( $out, "Migration complete: Hold fees are now fully managed through circulation rules." ); |
| 78 |
}, |
| 79 |
}; |