View | Details | Raw Unified | Return to bug 40808
Collapse All | Expand All

(-)a/Koha/Account/Line.pm (+99 lines)
Lines 23-28 use C4::Log qw( logaction ); Link Here
23
23
24
use Koha::Account::CreditType;
24
use Koha::Account::CreditType;
25
use Koha::Account::DebitType;
25
use Koha::Account::DebitType;
26
use Koha::Account::Link;
27
use Koha::Account::Links;
26
use Koha::Account::Offsets;
28
use Koha::Account::Offsets;
27
use Koha::Database;
29
use Koha::Database;
28
use Koha::DateUtils qw( dt_from_string );
30
use Koha::DateUtils qw( dt_from_string );
Lines 1100-1105 sub store { Link Here
1100
    return $self->SUPER::store();
1102
    return $self->SUPER::store();
1101
}
1103
}
1102
1104
1105
=head3 add_link
1106
1107
Add a link to another entity (hold, checkout, article request, etc.)
1108
1109
    $account_line->add_link('hold', $hold_id);
1110
1111
=cut
1112
1113
sub add_link {
1114
    my ( $self, $link_type, $linked_id ) = @_;
1115
1116
    return Koha::Account::Link->new(
1117
        {
1118
            accountlines_id => $self->id,
1119
            link_type       => $link_type,
1120
            linked_id       => $linked_id,
1121
        }
1122
    )->store;
1123
}
1124
1125
=head3 links
1126
1127
Get all links for this account line
1128
1129
    my $links = $account_line->links;
1130
1131
=cut
1132
1133
sub links {
1134
    my ($self) = @_;
1135
1136
    my $links_rs = $self->_result->search_related('accountline_links');
1137
    return Koha::Account::Links->_new_from_dbic($links_rs);
1138
}
1139
1140
=head3 holds
1141
1142
Get all holds linked to this account line
1143
1144
    my @holds = $account_line->holds;
1145
1146
=cut
1147
1148
sub holds {
1149
    my ($self) = @_;
1150
1151
    my @hold_ids = $self->links->search( { link_type => [ 'hold', 'old_hold' ] } )->get_column('linked_id');
1152
1153
    return () unless @hold_ids;
1154
1155
    my @holds = Koha::Holds->search( { reserve_id => { -in => \@hold_ids } } )->as_list;
1156
1157
    my @old_holds = Koha::Old::Holds->search( { reserve_id => { -in => \@hold_ids } } )->as_list;
1158
1159
    return ( @holds, @old_holds );
1160
}
1161
1162
=head3 checkouts
1163
1164
Get all checkouts linked to this account line
1165
1166
    my @checkouts = $account_line->checkouts;
1167
1168
=cut
1169
1170
sub checkouts {
1171
    my ($self) = @_;
1172
1173
    my @checkout_ids = $self->links->search( { link_type => [ 'checkout', 'old_checkout' ] } )->get_column('linked_id');
1174
1175
    return () unless @checkout_ids;
1176
1177
    my @checkouts = Koha::Checkouts->search( { issue_id => { -in => \@checkout_ids } } )->as_list;
1178
1179
    my @old_checkouts = Koha::Old::Checkouts->search( { issue_id => { -in => \@checkout_ids } } )->as_list;
1180
1181
    return ( @checkouts, @old_checkouts );
1182
}
1183
1184
=head3 article_requests
1185
1186
Get all article requests linked to this account line
1187
1188
    my @article_requests = $account_line->article_requests;
1189
1190
=cut
1191
1192
sub article_requests {
1193
    my ($self) = @_;
1194
1195
    my @ar_ids = $self->links->search( { link_type => 'article_request' } )->get_column('linked_id');
1196
1197
    return () unless @ar_ids;
1198
1199
    return Koha::ArticleRequests->search( { id => { -in => \@ar_ids } } )->as_list;
1200
}
1201
1103
=head2 Internal methods
1202
=head2 Internal methods
1104
1203
1105
=cut
1204
=cut
(-)a/Koha/Account/Link.pm (+173 lines)
Line 0 Link Here
1
package Koha::Account::Link;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Carp qw( croak );
21
use Koha::Exceptions::Account;
22
23
use base qw(Koha::Object);
24
25
=head1 NAME
26
27
Koha::Account::Link - Koha Account Link Object class
28
29
=head1 API
30
31
=head2 Class methods
32
33
=head3 store
34
35
Validates the link before storing
36
37
=cut
38
39
sub store {
40
    my ($self) = @_;
41
42
    my $link_type = $self->link_type;
43
    my $linked_id = $self->linked_id;
44
45
    # Check for valid link type first
46
    my $validators = {
47
        'hold'            => sub { Koha::Holds->find( $_[0] ) },
48
        'old_hold'        => sub { Koha::Old::Holds->find( $_[0] ) },
49
        'checkout'        => sub { Koha::Checkouts->find( $_[0] ) },
50
        'old_checkout'    => sub { Koha::Old::Checkouts->find( $_[0] ) },
51
        'article_request' => sub { Koha::ArticleRequests->find( $_[0] ) },
52
    };
53
54
    unless ( exists $validators->{$link_type} ) {
55
        Koha::Exceptions::Account::InvalidLinkType->throw("Unknown link_type: $link_type");
56
    }
57
58
    # Validate the linked entity exists
59
    unless ( $self->_validate_link_integrity() ) {
60
        Koha::Exceptions::Account::InvalidLinkTarget->throw(
61
            "Invalid link target: " . $link_type . " with ID " . $linked_id . " does not exist" );
62
    }
63
64
    return $self->SUPER::store();
65
}
66
67
=head3 linked_object
68
69
Returns the linked object based on link_type and linked_id
70
71
=cut
72
73
sub linked_object {
74
    my ($self) = @_;
75
76
    return $self->_result->linked_object;
77
}
78
79
=head3 linked_koha_object
80
81
Returns the Koha::Object representation of the linked object
82
83
=cut
84
85
sub linked_koha_object {
86
    my ($self) = @_;
87
88
    my $class_map = {
89
        'hold'            => 'Koha::Hold',
90
        'old_hold'        => 'Koha::Old::Hold',
91
        'checkout'        => 'Koha::Checkout',
92
        'old_checkout'    => 'Koha::Old::Checkout',
93
        'article_request' => 'Koha::ArticleRequest',
94
    };
95
96
    my $class = $class_map->{ $self->link_type };
97
    return unless $class;
98
99
    # Load the class if needed
100
    eval "require $class";
101
    if ($@) {
102
        Koha::Exceptions::Account::InvalidLinkType->throw(
103
            "Could not load class $class for link_type '" . $self->link_type . "': $@" );
104
    }
105
106
    return $class->_new_from_dbic( $self->linked_object );
107
}
108
109
=head3 _validate_link_integrity
110
111
Internal method to validate that the linked entity exists
112
113
=cut
114
115
sub _validate_link_integrity {
116
    my ($self) = @_;
117
118
    my $link_type = $self->link_type;
119
    my $linked_id = $self->linked_id;
120
121
    # Define valid link types and their validation methods
122
    my $validators = {
123
        'hold'            => sub { Koha::Holds->find( $_[0] ) },
124
        'old_hold'        => sub { Koha::Old::Holds->find( $_[0] ) },
125
        'checkout'        => sub { Koha::Checkouts->find( $_[0] ) },
126
        'old_checkout'    => sub { Koha::Old::Checkouts->find( $_[0] ) },
127
        'article_request' => sub { Koha::ArticleRequests->find( $_[0] ) },
128
    };
129
130
    my $validator = $validators->{$link_type};
131
    return 0 unless $validator;    # Invalid link type
132
133
    return $validator->($linked_id) ? 1 : 0;
134
}
135
136
=head2 Class Methods
137
138
=head3 _type
139
140
=cut
141
142
sub _type {
143
    return 'AccountlineLink';
144
}
145
146
=head1 AUTHOR
147
148
Koha Development Team <http://koha-community.org/>
149
150
=head1 COPYRIGHT
151
152
Copyright 2025 Koha Development Team
153
154
=head1 LICENSE
155
156
This file is part of Koha.
157
158
Koha is free software; you can redistribute it and/or modify it
159
under the terms of the GNU General Public License as published by
160
the Free Software Foundation; either version 3 of the License, or
161
(at your option) any later version.
162
163
Koha is distributed in the hope that it will be useful, but
164
WITHOUT ANY WARRANTY; without even the implied warranty of
165
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
166
GNU General Public License for more details.
167
168
You should have received a copy of the GNU General Public License
169
along with Koha; if not, see <http://www.gnu.org/licenses>.
170
171
=cut
172
173
1;
(-)a/Koha/Account/Links.pm (+141 lines)
Line 0 Link Here
1
package Koha::Account::Links;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use base qw(Koha::Objects);
21
22
=head1 NAME
23
24
Koha::Account::Links - Koha Account Links Object set class
25
26
=head1 API
27
28
=head2 Class methods
29
30
=head3 by_link_type
31
32
Filter links by link_type
33
34
    my $reserve_links = $links->by_link_type('reserve');
35
36
=cut
37
38
sub by_link_type {
39
    my ( $self, $link_type ) = @_;
40
41
    return $self->search( { link_type => $link_type } );
42
}
43
44
=head3 by_linked_id
45
46
Filter links by linked_id
47
48
    my $links = $links->by_linked_id(12345);
49
50
=cut
51
52
sub by_linked_id {
53
    my ( $self, $linked_id ) = @_;
54
55
    return $self->search( { linked_id => $linked_id } );
56
}
57
58
=head3 holds
59
60
Get all holds linked to these account lines
61
62
=cut
63
64
sub holds {
65
    my ($self) = @_;
66
67
    my @hold_ids = $self->search( { link_type => [ 'hold', 'old_hold' ] } )->get_column('linked_id');
68
69
    return () unless @hold_ids;
70
71
    my @holds     = Koha::Holds->search( { reserve_id => { -in => \@hold_ids } } )->as_list;
72
    my @old_holds = Koha::Old::Holds->search( { reserve_id => { -in => \@hold_ids } } )->as_list;
73
74
    return ( @holds, @old_holds );
75
}
76
77
=head3 checkouts
78
79
Get all checkouts linked to these account lines
80
81
=cut
82
83
sub checkouts {
84
    my ($self) = @_;
85
86
    my @checkout_ids = $self->search( { link_type => [ 'checkout', 'old_checkout' ] } )->get_column('linked_id');
87
88
    return () unless @checkout_ids;
89
90
    my @checkouts     = Koha::Checkouts->search( { issue_id => { -in => \@checkout_ids } } )->as_list;
91
    my @old_checkouts = Koha::Old::Checkouts->search( { issue_id => { -in => \@checkout_ids } } )->as_list;
92
93
    return ( @checkouts, @old_checkouts );
94
}
95
96
=head2 Class Methods
97
98
=head3 _type
99
100
=cut
101
102
sub _type {
103
    return 'AccountlineLink';
104
}
105
106
=head3 object_class
107
108
=cut
109
110
sub object_class {
111
    return 'Koha::Account::Link';
112
}
113
114
=head1 AUTHOR
115
116
Koha Development Team <http://koha-community.org/>
117
118
=head1 COPYRIGHT
119
120
Copyright 2025 Koha Development Team
121
122
=head1 LICENSE
123
124
This file is part of Koha.
125
126
Koha is free software; you can redistribute it and/or modify it
127
under the terms of the GNU General Public License as published by
128
the Free Software Foundation; either version 3 of the License, or
129
(at your option) any later version.
130
131
Koha is distributed in the hope that it will be useful, but
132
WITHOUT ANY WARRANTY; without even the implied warranty of
133
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
134
GNU General Public License for more details.
135
136
You should have received a copy of the GNU General Public License
137
along with Koha; if not, see <http://www.gnu.org/licenses>.
138
139
=cut
140
141
1;
(-)a/Koha/Exceptions/Account.pm (+16 lines)
Lines 55-60 use Exception::Class ( Link Here
55
    'Koha::Exceptions::Account::InvalidPaymentType' => {
55
    'Koha::Exceptions::Account::InvalidPaymentType' => {
56
        isa         => 'Koha::Exceptions::Account',
56
        isa         => 'Koha::Exceptions::Account',
57
        description => 'Invalid payment type'
57
        description => 'Invalid payment type'
58
    },
59
    'Koha::Exceptions::Account::InvalidLinkType' => {
60
        isa         => 'Koha::Exceptions::Account',
61
        description => 'Invalid account line link type'
62
    },
63
    'Koha::Exceptions::Account::InvalidLinkTarget' => {
64
        isa         => 'Koha::Exceptions::Account',
65
        description => 'Invalid account line link target'
58
    }
66
    }
59
);
67
);
60
68
Lines 98-103 Exception to be used when a passed credit or debit is not of a recognised type. Link Here
98
106
99
Exception to be used when UseCashRegisters is enabled and one is not passed for a transaction.
107
Exception to be used when UseCashRegisters is enabled and one is not passed for a transaction.
100
108
109
=head2 Koha::Exceptions::Account::InvalidLinkType
110
111
Exception to be used when an invalid link type is provided to account line linking.
112
113
=head2 Koha::Exceptions::Account::InvalidLinkTarget
114
115
Exception to be used when an account line link target does not exist or is invalid.
116
101
=cut
117
=cut
102
118
103
1;
119
1;
(-)a/Koha/Hold.pm (-1 / +18 lines)
Lines 1227-1232 sub charge_hold_fee { Link Here
1227
        }
1227
        }
1228
    );
1228
    );
1229
1229
1230
    # Link the account line to this hold
1231
    $line->add_link( 'hold', $self->id );
1232
1230
    return $line;
1233
    return $line;
1231
}
1234
}
1232
1235
Lines 1450-1455 sub strings_map { Link Here
1450
    return $strings;
1453
    return $strings;
1451
}
1454
}
1452
1455
1456
=head3 debits
1457
1458
    my $fees = $hold->debits;
1459
1460
Get all debit account lines (charges) associated with this hold
1461
1462
=cut
1463
1464
sub debits {
1465
    my ($self) = @_;
1466
1467
    my $accountlines_rs = $self->_result->search_related('accountline_links')->search_related('accountline');
1468
    return Koha::Account::Debits->_new_from_dbic($accountlines_rs);
1469
}
1470
1453
=head2 Internal methods
1471
=head2 Internal methods
1454
1472
1455
=head3 _type
1473
=head3 _type
1456
- 

Return to bug 40808