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

(-)a/C4/Reserves.pm (-70 / +6 lines)
Lines 141-147 BEGIN { Link Here
141
        IsItemOnHoldAndFound
141
        IsItemOnHoldAndFound
142
142
143
        GetMaxPatronHoldsForRecord
143
        GetMaxPatronHoldsForRecord
144
        GetBorrowersToSatisfyHold
145
    );
144
    );
146
    @EXPORT_OK = qw( MergeHolds );
145
    @EXPORT_OK = qw( MergeHolds );
147
}
146
}
Lines 255-274 sub AddReserve { Link Here
255
254
256
    # Send email to borrowers asking to return item whenever a hold is placed on it
255
    # Send email to borrowers asking to return item whenever a hold is placed on it
257
    if (C4::Context->preference("NotifyToReturnItemWhenHoldIsPlaced")) {
256
    if (C4::Context->preference("NotifyToReturnItemWhenHoldIsPlaced")) {
258
        my @borrowers = GetBorrowersToSatisfyHold($hold);
257
        my @borrowers = $hold->borrowers_to_satisfy();
259
        foreach my $borrower (@borrowers) {
258
        foreach my $borrower (@borrowers) {
260
            if ( !$borrower->{email} ) {
259
            if ( !$borrower->email ) {
261
                next;
260
                next;
262
            }
261
            }
263
262
264
            my $library = Koha::Libraries->find($borrower->{branchcode})->unblessed;
263
            my $library = Koha::Libraries->find($borrower->branchcode)->unblessed;
265
            if ( my $letter =  C4::Letters::GetPreparedLetter (
264
            if ( my $letter =  C4::Letters::GetPreparedLetter (
266
                module => 'reserves',
265
                module => 'reserves',
267
                letter_code => 'HOLDPLACED_CONTACT',
266
                letter_code => 'HOLDPLACED_CONTACT',
268
                branchcode => $branch,
267
                branchcode => $branch,
269
                tables => {
268
                tables => {
270
                    'branches'    => $library,
269
                    'branches'    => $library,
271
                    'borrowers'   => $borrower,
270
                    'borrowers'   => $borrower->unblessed,
272
                    'biblio'      => $biblionumber,
271
                    'biblio'      => $biblionumber,
273
                    'biblioitems' => $biblionumber,
272
                    'biblioitems' => $biblionumber,
274
                    'items'       => $checkitem,
273
                    'items'       => $checkitem,
Lines 279-288 sub AddReserve { Link Here
279
278
280
                C4::Letters::EnqueueLetter(
279
                C4::Letters::EnqueueLetter(
281
                    {   letter                 => $letter,
280
                    {   letter                 => $letter,
282
                        borrowernumber         => $borrower->{borrowernumber},
281
                        borrowernumber         => $borrower->borrowernumber,
283
                        message_transport_type => 'email',
282
                        message_transport_type => 'email',
284
                        from_address           => $admin_email_address,
283
                        from_address           => $admin_email_address,
285
                        to_address             => $borrower->{email},
284
                        to_address             => $borrower->email,
286
                    }
285
                    }
287
            );
286
            );
288
            }
287
            }
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 284-289 sub borrower { Link Here
284
    return $self->{_borrower};
284
    return $self->{_borrower};
285
}
285
}
286
286
287
=head3 borrowers_to_satisfy
288
289
Returns Koha::Patron objects able to return item to satisfy this Hold
290
291
=cut
292
293
sub borrowers_to_satisfy {
294
    my ($self) = @_;
295
296
    my @items = ();
297
    if ( my $item = $self->item() ) {
298
        push ( @items, $item );
299
    }
300
    elsif ( my $biblio = $self->biblio() ) {
301
        push ( @items, $biblio->items() );
302
    }
303
304
    my $library = C4::Context->preference("NotifyToReturnItemFromLibrary");
305
    my @patrons = ();
306
    foreach my $item ( @items ) {
307
308
        next unless $item->checkout();
309
310
        my $patron = $item->checkout()->patron();
311
        if ( ( ($library eq 'RequestorLibrary') && ($patron->branchcode eq $self->borrower()->branchcode) )
312
             || ( ($library eq 'ItemHomeLibrary') && ($item->homebranch eq $self->borrower()->branchcode) )
313
             || ( $library eq 'AnyLibrary' ) ) {
314
315
            push ( @patrons, $patron );
316
        }
317
    }
318
319
    return @patrons;
320
}
321
322
287
=head3 is_suspended
323
=head3 is_suspended
288
324
289
my $bool = $hold->is_suspended();
325
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