From eb37a573f477f15a15ef65a3fdeed8848af2e863 Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Wed, 16 Jul 2025 10:59:18 +0000 Subject: [PATCH] Bug 40517: Add hold groups relationship to patron --- Koha/Patron.pm | 15 ++++++++ Koha/Schema/Result/Borrower.pm | 19 +++++++++-- Koha/Schema/Result/HoldGroup.pm | 34 +++++++++++++++++-- .../bug_40517_-_hold_group_table_updates.pl | 31 +++++++++++++++++ installer/data/mysql/kohastructure.sql | 3 ++ 5 files changed, 98 insertions(+), 4 deletions(-) create mode 100755 installer/data/mysql/atomicupdate/bug_40517_-_hold_group_table_updates.pl diff --git a/Koha/Patron.pm b/Koha/Patron.pm index cfa570e0f00..dda09bff4a2 100644 --- a/Koha/Patron.pm +++ b/Koha/Patron.pm @@ -46,6 +46,7 @@ use Koha::Encryption; use Koha::Exceptions; use Koha::Exceptions::Password; use Koha::Holds; +use Koha::HoldGroups; use Koha::ILL::Requests; use Koha::Old::Checkouts; use Koha::OverdueRules; @@ -1828,6 +1829,20 @@ sub old_holds { return Koha::Old::Holds->_new_from_dbic($old_holds_rs); } +=head3 hold_groups + +my $hold_groups = $patron->hold_groups + +Return all of this patron's hold groups + +=cut + +sub hold_groups { + my ($self) = @_; + my $hold_group_rs = $self->_result->hold_groups->search( {}, { order_by => 'hold_group_id' } ); + return Koha::HoldGroups->_new_from_dbic($hold_group_rs); +} + =head3 curbside_pickups my $curbside_pickups = $patron->curbside_pickups; diff --git a/Koha/Schema/Result/Borrower.pm b/Koha/Schema/Result/Borrower.pm index 1e1fa11cd42..4ea0800ab83 100644 --- a/Koha/Schema/Result/Borrower.pm +++ b/Koha/Schema/Result/Borrower.pm @@ -1391,6 +1391,21 @@ __PACKAGE__->has_many( { cascade_copy => 0, cascade_delete => 0 }, ); +=head2 hold_groups + +Type: has_many + +Related object: L + +=cut + +__PACKAGE__->has_many( + "hold_groups", + "Koha::Schema::Result::HoldGroup", + { "foreign.borrowernumber" => "self.borrowernumber" }, + { cascade_copy => 0, cascade_delete => 0 }, +); + =head2 housebound_profile Type: might_have @@ -2197,8 +2212,8 @@ Composing rels: L -> permission __PACKAGE__->many_to_many("permissions", "user_permissions", "permission"); -# Created by DBIx::Class::Schema::Loader v0.07051 @ 2025-07-10 07:11:31 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:XFOe2X4k2DUziaNS5pWd/Q +# Created by DBIx::Class::Schema::Loader v0.07051 @ 2025-07-16 10:57:50 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:1mCQb2VuHB10JWtlJXTBWw __PACKAGE__->belongs_to( "library", diff --git a/Koha/Schema/Result/HoldGroup.pm b/Koha/Schema/Result/HoldGroup.pm index e5d6b2298a3..ac2c9ffdbbf 100644 --- a/Koha/Schema/Result/HoldGroup.pm +++ b/Koha/Schema/Result/HoldGroup.pm @@ -30,6 +30,14 @@ __PACKAGE__->table("hold_groups"); is_auto_increment: 1 is_nullable: 0 +=head2 borrowernumber + + data_type: 'integer' + is_foreign_key: 1 + is_nullable: 1 + +foreign key, linking this to the borrowers table + =cut __PACKAGE__->add_columns( @@ -40,6 +48,8 @@ __PACKAGE__->add_columns( is_auto_increment => 1, is_nullable => 0, }, + "borrowernumber", + { data_type => "integer", is_foreign_key => 1, is_nullable => 1 }, ); =head1 PRIMARY KEY @@ -56,6 +66,26 @@ __PACKAGE__->set_primary_key("hold_group_id"); =head1 RELATIONS +=head2 borrowernumber + +Type: belongs_to + +Related object: L + +=cut + +__PACKAGE__->belongs_to( + "borrowernumber", + "Koha::Schema::Result::Borrower", + { borrowernumber => "borrowernumber" }, + { + is_deferrable => 1, + join_type => "LEFT", + on_delete => "CASCADE", + on_update => "RESTRICT", + }, +); + =head2 old_reserves Type: has_many @@ -87,8 +117,8 @@ __PACKAGE__->has_many( ); -# Created by DBIx::Class::Schema::Loader v0.07046 @ 2022-08-04 14:43:31 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:5Xzfn2C7H+Z8R9PUBPFkYw +# Created by DBIx::Class::Schema::Loader v0.07051 @ 2025-07-16 10:57:50 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:Y1eP6naEI2vi94gTHqtubw # You can replace this text with custom code or comments, and it will be preserved on regeneration diff --git a/installer/data/mysql/atomicupdate/bug_40517_-_hold_group_table_updates.pl b/installer/data/mysql/atomicupdate/bug_40517_-_hold_group_table_updates.pl new file mode 100755 index 00000000000..e520c378a40 --- /dev/null +++ b/installer/data/mysql/atomicupdate/bug_40517_-_hold_group_table_updates.pl @@ -0,0 +1,31 @@ +use Modern::Perl; +use Koha::Installer::Output qw(say_warning say_success say_info); + +return { + bug_number => "40517", + description => "Add 'borrower' column to hold_groups", + up => sub { + my ($args) = @_; + my ( $dbh, $out ) = @$args{qw(dbh out)}; + + unless ( column_exists( 'hold_groups', 'borrowernumber' ) ) { + $dbh->do( + q{ALTER TABLE hold_groups ADD COLUMN borrowernumber int(11) DEFAULT NULL COMMENT 'foreign key, linking this to the borrowers table' AFTER hold_group_id} + ); + $dbh->do( + q{ + ALTER TABLE hold_groups ADD KEY `hold_groups_borrowernumber` (`borrowernumber`) + } + ); + $dbh->do( + q{ + ALTER TABLE hold_groups ADD CONSTRAINT hold_groups_ibfk_1 + FOREIGN KEY(`borrowernumber`) + REFERENCES `borrowers` (`borrowernumber`) + ON DELETE CASCADE; + } + ); + say_success( $out, "Added column 'borrowernumber' to hold_groups" ); + } + }, +}; diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql index 733438e4f64..9b0ea8936ac 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -5066,6 +5066,9 @@ DROP TABLE IF EXISTS `hold_groups`; /*!40101 SET character_set_client = utf8 */; CREATE TABLE hold_groups ( hold_group_id int unsigned NOT NULL AUTO_INCREMENT, + `borrowernumber` int(11) DEFAULT NULL COMMENT 'foreign key, linking this to the borrowers table', + KEY `hold_groups_borrowernumber` (`borrowernumber`), + CONSTRAINT `hold_groups_ibfk_1` FOREIGN KEY (`borrowernumber`) REFERENCES `borrowers` (`borrowernumber`) ON DELETE CASCADE, PRIMARY KEY (hold_group_id) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; /*!40101 SET character_set_client = @saved_cs_client */; -- 2.39.5