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

(-)a/C4/Reserves.pm (-70 / +6 lines)
Lines 140-146 BEGIN { Link Here
140
        IsItemOnHoldAndFound
140
        IsItemOnHoldAndFound
141
141
142
        GetMaxPatronHoldsForRecord
142
        GetMaxPatronHoldsForRecord
143
        GetBorrowersToSatisfyHold
144
    );
143
    );
145
    @EXPORT_OK = qw( MergeHolds );
144
    @EXPORT_OK = qw( MergeHolds );
146
}
145
}
Lines 265-284 sub AddReserve { Link Here
265
264
266
    # Send email to borrowers asking to return item whenever a hold is placed on it
265
    # Send email to borrowers asking to return item whenever a hold is placed on it
267
    if (C4::Context->preference("NotifyToReturnItemWhenHoldIsPlaced")) {
266
    if (C4::Context->preference("NotifyToReturnItemWhenHoldIsPlaced")) {
268
        my @borrowers = GetBorrowersToSatisfyHold($hold);
267
        my @borrowers = $hold->borrowers_to_satisfy();
269
        foreach my $borrower (@borrowers) {
268
        foreach my $borrower (@borrowers) {
270
            if ( !$borrower->{email} ) {
269
            if ( !$borrower->email ) {
271
                next;
270
                next;
272
            }
271
            }
273
272
274
            my $library = Koha::Libraries->find($borrower->{branchcode})->unblessed;
273
            my $library = Koha::Libraries->find($borrower->branchcode)->unblessed;
275
            if ( my $letter =  C4::Letters::GetPreparedLetter (
274
            if ( my $letter =  C4::Letters::GetPreparedLetter (
276
                module => 'reserves',
275
                module => 'reserves',
277
                letter_code => 'HOLDPLACED_CONTACT',
276
                letter_code => 'HOLDPLACED_CONTACT',
278
                branchcode => $branch,
277
                branchcode => $branch,
279
                tables => {
278
                tables => {
280
                    'branches'    => $library,
279
                    'branches'    => $library,
281
                    'borrowers'   => $borrower,
280
                    'borrowers'   => $borrower->unblessed,
282
                    'biblio'      => $biblionumber,
281
                    'biblio'      => $biblionumber,
283
                    'biblioitems' => $biblionumber,
282
                    'biblioitems' => $biblionumber,
284
                    'items'       => $checkitem,
283
                    'items'       => $checkitem,
Lines 289-298 sub AddReserve { Link Here
289
288
290
                C4::Letters::EnqueueLetter(
289
                C4::Letters::EnqueueLetter(
291
                    {   letter                 => $letter,
290
                    {   letter                 => $letter,
292
                        borrowernumber         => $borrower->{borrowernumber},
291
                        borrowernumber         => $borrower->borrowernumber,
293
                        message_transport_type => 'email',
292
                        message_transport_type => 'email',
294
                        from_address           => $admin_email_address,
293
                        from_address           => $admin_email_address,
295
                        to_address             => $borrower->{email},
294
                        to_address             => $borrower->email,
296
                    }
295
                    }
297
            );
296
            );
298
            }
297
            }
Lines 2202-2270 sub GetHoldRule { Link Here
2202
    return $sth->fetchrow_hashref();
2201
    return $sth->fetchrow_hashref();
2203
}
2202
}
2204
2203
2205
=head2 GetBorrowersToSatisfyHold
2206
2207
my @borrowers = GetBorrowersToSatisfyHold( $hold );
2208
2209
Returns borrowers who can return item to satisfy a given hold.
2210
2211
=cut
2212
2213
sub GetBorrowersToSatisfyHold {
2214
    my ( $hold ) = @_;
2215
2216
    my $query = "";
2217
2218
    if ($hold->item()) {
2219
        $query = q{
2220
             SELECT borrowers.borrowernumber
2221
               FROM reserves
2222
               JOIN items ON reserves.itemnumber = items.itemnumber
2223
               JOIN issues ON issues.itemnumber = items.itemnumber
2224
               JOIN borrowers ON issues.borrowernumber = borrowers.borrowernumber
2225
              WHERE reserves.reserve_id = ?
2226
        };
2227
    }
2228
    elsif ($hold->biblio()) {
2229
        $query = q{
2230
             SELECT borrowers.borrowernumber
2231
               FROM reserves
2232
               JOIN biblio ON reserves.biblionumber = biblio.biblionumber
2233
               JOIN items ON items.biblionumber = biblio.biblionumber
2234
               JOIN issues ON issues.itemnumber = items.itemnumber
2235
               JOIN borrowers ON issues.borrowernumber = borrowers.borrowernumber
2236
              WHERE reserves.reserve_id = ?
2237
        };
2238
    }
2239
2240
    my $library = C4::Context->preference("NotifyToReturnItemFromLibrary");
2241
    my $dbh = C4::Context->dbh;
2242
    my $sth;
2243
2244
    if ($library eq 'RequestorLibrary') {
2245
        $query .= " AND borrowers.branchcode = ?";
2246
        $sth = $dbh->prepare( $query );
2247
        $sth->execute( $hold->id(), $hold->borrower()->branchcode );
2248
    }
2249
    elsif ($library eq 'ItemHomeLibrary') {
2250
        $query .= " AND items.homebranch = ?";
2251
        $sth = $dbh->prepare( $query );
2252
        $sth->execute( $hold->id(), $hold->borrower()->branchcode );
2253
    }
2254
    else {
2255
        $sth = $dbh->prepare( $query );
2256
        $sth->execute( $hold->id() );
2257
    }
2258
2259
    my @results = ();
2260
    while ( my $data = $sth->fetchrow_hashref ) {
2261
        my $borrower = C4::Members::GetMember(borrowernumber => $data->{borrowernumber});
2262
        push( @results, $borrower );
2263
    }
2264
2265
    return @results;
2266
}
2267
2268
=head1 AUTHOR
2204
=head1 AUTHOR
2269
2205
2270
Koha Development Team <http://koha-community.org/>
2206
Koha Development Team <http://koha-community.org/>
(-)a/Koha/Hold.pm (+36 lines)
Lines 325-330 sub borrower { Link Here
325
    return $self->{_borrower};
325
    return $self->{_borrower};
326
}
326
}
327
327
328
=head3 borrowers_to_satisfy
329
330
Returns Koha::Patron objects able to return item to satisfy this Hold
331
332
=cut
333
334
sub borrowers_to_satisfy {
335
    my ($self) = @_;
336
337
    my @items = ();
338
    if ( my $item = $self->item() ) {
339
        push ( @items, $item );
340
    }
341
    elsif ( my $biblio = $self->biblio() ) {
342
        push ( @items, $biblio->items() );
343
    }
344
345
    my $library = C4::Context->preference("NotifyToReturnItemFromLibrary");
346
    my @patrons = ();
347
    foreach my $item ( @items ) {
348
349
        next unless $item->checkout();
350
351
        my $patron = $item->checkout()->patron();
352
        if ( ( ($library eq 'RequestorLibrary') && ($patron->branchcode eq $self->borrower()->branchcode) )
353
             || ( ($library eq 'ItemHomeLibrary') && ($item->homebranch eq $self->borrower()->branchcode) )
354
             || ( $library eq 'AnyLibrary' ) ) {
355
356
            push ( @patrons, $patron );
357
        }
358
    }
359
360
    return @patrons;
361
}
362
363
328
=head3 is_suspended
364
=head3 is_suspended
329
365
330
my $bool = $hold->is_suspended();
366
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