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

(-)a/C4/Reserves.pm (-70 / +6 lines)
Lines 139-145 BEGIN { Link Here
139
        IsItemOnHoldAndFound
139
        IsItemOnHoldAndFound
140
140
141
        GetMaxPatronHoldsForRecord
141
        GetMaxPatronHoldsForRecord
142
        GetBorrowersToSatisfyHold
143
    );
142
    );
144
    @EXPORT_OK = qw( MergeHolds );
143
    @EXPORT_OK = qw( MergeHolds );
145
}
144
}
Lines 264-283 sub AddReserve { Link Here
264
263
265
    # Send email to borrowers asking to return item whenever a hold is placed on it
264
    # Send email to borrowers asking to return item whenever a hold is placed on it
266
    if (C4::Context->preference("NotifyToReturnItemWhenHoldIsPlaced")) {
265
    if (C4::Context->preference("NotifyToReturnItemWhenHoldIsPlaced")) {
267
        my @borrowers = GetBorrowersToSatisfyHold($hold);
266
        my @borrowers = $hold->borrowers_to_satisfy();
268
        foreach my $borrower (@borrowers) {
267
        foreach my $borrower (@borrowers) {
269
            if ( !$borrower->{email} ) {
268
            if ( !$borrower->email ) {
270
                next;
269
                next;
271
            }
270
            }
272
271
273
            my $library = Koha::Libraries->find($borrower->{branchcode})->unblessed;
272
            my $library = Koha::Libraries->find($borrower->branchcode)->unblessed;
274
            if ( my $letter =  C4::Letters::GetPreparedLetter (
273
            if ( my $letter =  C4::Letters::GetPreparedLetter (
275
                module => 'reserves',
274
                module => 'reserves',
276
                letter_code => 'HOLDPLACED_CONTACT',
275
                letter_code => 'HOLDPLACED_CONTACT',
277
                branchcode => $branch,
276
                branchcode => $branch,
278
                tables => {
277
                tables => {
279
                    'branches'    => $library,
278
                    'branches'    => $library,
280
                    'borrowers'   => $borrower,
279
                    'borrowers'   => $borrower->unblessed,
281
                    'biblio'      => $biblionumber,
280
                    'biblio'      => $biblionumber,
282
                    'biblioitems' => $biblionumber,
281
                    'biblioitems' => $biblionumber,
283
                    'items'       => $checkitem,
282
                    'items'       => $checkitem,
Lines 288-297 sub AddReserve { Link Here
288
287
289
                C4::Letters::EnqueueLetter(
288
                C4::Letters::EnqueueLetter(
290
                    {   letter                 => $letter,
289
                    {   letter                 => $letter,
291
                        borrowernumber         => $borrower->{borrowernumber},
290
                        borrowernumber         => $borrower->borrowernumber,
292
                        message_transport_type => 'email',
291
                        message_transport_type => 'email',
293
                        from_address           => $admin_email_address,
292
                        from_address           => $admin_email_address,
294
                        to_address             => $borrower->{email},
293
                        to_address             => $borrower->email,
295
                    }
294
                    }
296
            );
295
            );
297
            }
296
            }
Lines 2176-2244 sub GetHoldRule { Link Here
2176
    return $sth->fetchrow_hashref();
2175
    return $sth->fetchrow_hashref();
2177
}
2176
}
2178
2177
2179
=head2 GetBorrowersToSatisfyHold
2180
2181
my @borrowers = GetBorrowersToSatisfyHold( $hold );
2182
2183
Returns borrowers who can return item to satisfy a given hold.
2184
2185
=cut
2186
2187
sub GetBorrowersToSatisfyHold {
2188
    my ( $hold ) = @_;
2189
2190
    my $query = "";
2191
2192
    if ($hold->item()) {
2193
        $query = q{
2194
             SELECT borrowers.borrowernumber
2195
               FROM reserves
2196
               JOIN items ON reserves.itemnumber = items.itemnumber
2197
               JOIN issues ON issues.itemnumber = items.itemnumber
2198
               JOIN borrowers ON issues.borrowernumber = borrowers.borrowernumber
2199
              WHERE reserves.reserve_id = ?
2200
        };
2201
    }
2202
    elsif ($hold->biblio()) {
2203
        $query = q{
2204
             SELECT borrowers.borrowernumber
2205
               FROM reserves
2206
               JOIN biblio ON reserves.biblionumber = biblio.biblionumber
2207
               JOIN items ON items.biblionumber = biblio.biblionumber
2208
               JOIN issues ON issues.itemnumber = items.itemnumber
2209
               JOIN borrowers ON issues.borrowernumber = borrowers.borrowernumber
2210
              WHERE reserves.reserve_id = ?
2211
        };
2212
    }
2213
2214
    my $library = C4::Context->preference("NotifyToReturnItemFromLibrary");
2215
    my $dbh = C4::Context->dbh;
2216
    my $sth;
2217
2218
    if ($library eq 'RequestorLibrary') {
2219
        $query .= " AND borrowers.branchcode = ?";
2220
        $sth = $dbh->prepare( $query );
2221
        $sth->execute( $hold->id(), $hold->borrower()->branchcode );
2222
    }
2223
    elsif ($library eq 'ItemHomeLibrary') {
2224
        $query .= " AND items.homebranch = ?";
2225
        $sth = $dbh->prepare( $query );
2226
        $sth->execute( $hold->id(), $hold->borrower()->branchcode );
2227
    }
2228
    else {
2229
        $sth = $dbh->prepare( $query );
2230
        $sth->execute( $hold->id() );
2231
    }
2232
2233
    my @results = ();
2234
    while ( my $data = $sth->fetchrow_hashref ) {
2235
        my $borrower = C4::Members::GetMember(borrowernumber => $data->{borrowernumber});
2236
        push( @results, $borrower );
2237
    }
2238
2239
    return @results;
2240
}
2241
2242
=head1 AUTHOR
2178
=head1 AUTHOR
2243
2179
2244
Koha Development Team <http://koha-community.org/>
2180
Koha Development Team <http://koha-community.org/>
(-)a/Koha/Hold.pm (+36 lines)
Lines 311-316 sub borrower { Link Here
311
    return $self->{_borrower};
311
    return $self->{_borrower};
312
}
312
}
313
313
314
=head3 borrowers_to_satisfy
315
316
Returns Koha::Patron objects able to return item to satisfy this Hold
317
318
=cut
319
320
sub borrowers_to_satisfy {
321
    my ($self) = @_;
322
323
    my @items = ();
324
    if ( my $item = $self->item() ) {
325
        push ( @items, $item );
326
    }
327
    elsif ( my $biblio = $self->biblio() ) {
328
        push ( @items, $biblio->items() );
329
    }
330
331
    my $library = C4::Context->preference("NotifyToReturnItemFromLibrary");
332
    my @patrons = ();
333
    foreach my $item ( @items ) {
334
335
        next unless $item->checkout();
336
337
        my $patron = $item->checkout()->patron();
338
        if ( ( ($library eq 'RequestorLibrary') && ($patron->branchcode eq $self->borrower()->branchcode) )
339
             || ( ($library eq 'ItemHomeLibrary') && ($item->homebranch eq $self->borrower()->branchcode) )
340
             || ( $library eq 'AnyLibrary' ) ) {
341
342
            push ( @patrons, $patron );
343
        }
344
    }
345
346
    return @patrons;
347
}
348
349
314
=head3 is_suspended
350
=head3 is_suspended
315
351
316
my $bool = $hold->is_suspended();
352
my $bool = $hold->is_suspended();
(-)a/t/db_dependent/Reserves/Notification.t (-8 / +5 lines)
Lines 23-32 use Test::More tests => 5; Link Here
23
use t::lib::TestBuilder;
23
use t::lib::TestBuilder;
24
use t::lib::Mocks;
24
use t::lib::Mocks;
25
25
26
use C4::Reserves qw( GetBorrowersToSatisfyHold );
27
use Koha::Database;
26
use Koha::Database;
28
use Koha::Holds;
27
use Koha::Holds;
29
use Data::Dumper;
30
28
31
my $schema = Koha::Database->new->schema;
29
my $schema = Koha::Database->new->schema;
32
$schema->storage->txn_begin;
30
$schema->storage->txn_begin;
Lines 149-166 my $hold = Koha::Hold->new( Link Here
149
my @borrowers;
147
my @borrowers;
150
148
151
t::lib::Mocks::mock_preference('NotifyToReturnItemFromLibrary', 'AnyLibrary'); # Assuming items from any library
149
t::lib::Mocks::mock_preference('NotifyToReturnItemFromLibrary', 'AnyLibrary'); # Assuming items from any library
152
@borrowers = GetBorrowersToSatisfyHold($hold);
150
@borrowers = $hold->borrowers_to_satisfy();
153
is(scalar(grep {defined $_} @borrowers), 2, "2 borrowers with requested item found in any library");
151
is(scalar(grep {defined $_} @borrowers), 2, "2 borrowers with requested item found in any library");
154
152
155
t::lib::Mocks::mock_preference('NotifyToReturnItemFromLibrary', 'RequestorLibrary'); # Assuming items from requestor library
153
t::lib::Mocks::mock_preference('NotifyToReturnItemFromLibrary', 'RequestorLibrary'); # Assuming items from requestor library
156
@borrowers = GetBorrowersToSatisfyHold($hold);
154
@borrowers = $hold->borrowers_to_satisfy();
157
is(scalar(grep {defined $_} @borrowers), 1, "1 item found in requestor library");
155
is(scalar(grep {defined $_} @borrowers), 1, "1 item found in requestor library");
158
is($borrowers[0]->{cardnumber}, "002", "   ...and it is in 002's hands");
156
is($borrowers[0]->cardnumber, "002", "   ...and it is in 002's hands");
159
157
160
t::lib::Mocks::mock_preference('NotifyToReturnItemFromLibrary', 'ItemHomeLibrary'); # Assuming items from the same library as requested item
158
t::lib::Mocks::mock_preference('NotifyToReturnItemFromLibrary', 'ItemHomeLibrary'); # Assuming items from the same library as requested item
161
@borrowers = GetBorrowersToSatisfyHold($hold);
159
@borrowers = $hold->borrowers_to_satisfy();
162
is(scalar(grep {defined $_} @borrowers), 1, "1 item found in the same library as the requested item");
160
is(scalar(grep {defined $_} @borrowers), 1, "1 item found in the same library as the requested item");
163
is($borrowers[0]->{cardnumber}, "003", "   ...and it is in 003's hands");
161
is($borrowers[0]->cardnumber, "003", "   ...and it is in 003's hands");
164
162
165
$schema->storage->txn_rollback;
163
$schema->storage->txn_rollback;
166
164
167
- 

Return to bug 17509