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

(-)a/Koha/Checkout.pm (-1 / +43 lines)
Lines 23-29 use Modern::Perl; Link Here
23
use DateTime;
23
use DateTime;
24
use Try::Tiny qw( catch try );
24
use Try::Tiny qw( catch try );
25
25
26
use C4::Circulation qw( LostItem MarkIssueReturned );
26
use C4::Circulation qw( AddRenewal CanBookBeRenewed LostItem MarkIssueReturned );
27
use Koha::Checkouts::Renewals;
27
use Koha::Checkouts::Renewals;
28
use Koha::Checkouts::ReturnClaims;
28
use Koha::Checkouts::ReturnClaims;
29
use Koha::Database;
29
use Koha::Database;
Lines 151-156 sub renewals { Link Here
151
    return Koha::Checkouts::Renewals->_new_from_dbic( $renewals_rs );
151
    return Koha::Checkouts::Renewals->_new_from_dbic( $renewals_rs );
152
}
152
}
153
153
154
=head3 attempt_auto_renew
155
156
  my ($success, $error, $updated) = $checkout->auto_renew({ confirm => 1 });
157
158
Attempt to automatically renew a book. Return error reason if it cannot be renewed.
159
Also return whether a change has been made to avoid notifying on more than one attempt.
160
161
If not passed confirm, we will only report and no changes will be made.
162
163
=cut
164
165
sub attempt_auto_renew {
166
    my ( $self, $params ) = @_;
167
    my $confirm = $params->{confirm} // 0;
168
169
    # CanBookBeRenewed returns 'auto_renew' when the renewal should be done by this script
170
    my ( $ok, $error ) = C4::Circulation::CanBookBeRenewed( $self->patron, $self, undef, 1 );
171
    if ( $error eq 'auto_renew' ) {
172
        if ($confirm) {
173
            my $date_due = C4::Circulation::AddRenewal(
174
                {
175
                    borrowernumber => $self->borrowernumber,
176
                    itemnumber     => $self->itemnumber,
177
                    branch         => $self->branchcode,
178
                    seen           => 0,
179
                    automatic      => 1,
180
                }
181
            );
182
            $self->auto_renew_error(undef)->store;
183
        }
184
        return ( 1, undef, 1 );
185
    } else {
186
        my $updated = 0;
187
        if ( !$self->auto_renew_error || $error ne $self->auto_renew_error ) {
188
            $self->auto_renew_error($error)->store if $confirm;
189
            $updated = 1;
190
        }
191
        return ( 0, $error, $updated );
192
    }
193
194
}
195
154
=head3 to_api_mapping
196
=head3 to_api_mapping
155
197
156
This method returns the mapping for representing a Koha::Checkout object
198
This method returns the mapping for representing a Koha::Checkout object
(-)a/misc/cronjobs/automatic_renewals.pl (-33 / +19 lines)
Lines 171-222 while ( my $auto_renew = $auto_renews->next ) { Link Here
171
        $wants_digest = 0;
171
        $wants_digest = 0;
172
    }
172
    }
173
173
174
    # CanBookBeRenewed returns 'auto_renew' when the renewal should be done by this script
174
    my ( $success, $error, $updated ) = $auto_renew->attempt_auto_renew({ confirm => $confirm });
175
    my ( $ok, $error ) = CanBookBeRenewed( $auto_renew->patron, $auto_renew, undef, 1 );
175
    if ( $success ) {
176
    my $updated;
177
    if ( $error eq 'auto_renew' ) {
178
        $updated = 1;
179
        if ($verbose) {
176
        if ($verbose) {
180
            say sprintf "Issue id: %s for borrower: %s and item: %s %s be renewed.",
177
            say sprintf "Issue id: %s for borrower: %s and item: %s %s be renewed.",
181
                $auto_renew->issue_id, $auto_renew->borrowernumber, $auto_renew->itemnumber,
178
                $auto_renew->issue_id, $auto_renew->borrowernumber, $auto_renew->itemnumber,
182
                $confirm ? 'will' : 'would';
179
                $confirm ? 'will' : 'would';
183
        }
180
        }
184
        if ($confirm) {
181
        if ($confirm) {
185
            my $date_due = AddRenewal(
186
                {
187
                    borrowernumber => $auto_renew->borrowernumber,
188
                    itemnumber     => $auto_renew->itemnumber,
189
                    branch         => $auto_renew->branchcode,
190
                    seen           => 0,
191
                    automatic      => 1,
192
                }
193
            );
194
            push @item_renewal_ids, $auto_renew->itemnumber;
182
            push @item_renewal_ids, $auto_renew->itemnumber;
195
            $auto_renew->auto_renew_error(undef)->store;
196
        }
183
        }
197
        push @{ $report{ $auto_renew->borrowernumber } }, $auto_renew
184
        push @{ $report{ $auto_renew->borrowernumber } }, $auto_renew
198
            if ($wants_messages) && !$wants_digest;
185
            if ( $wants_messages ) && !$wants_digest;
199
    } elsif ( $error eq 'too_many'
186
    } elsif (
200
        || $error eq 'on_reserve'
187
        # FIXME Do we need to list every status? Why not simply else?
201
        || $error eq 'restriction'
188
        $error eq 'too_many' ||
202
        || $error eq 'overdue'
189
        $error eq 'on_reserve' ||
203
        || $error eq 'too_unseen'
190
        $error eq 'restriction' ||
204
        || $error eq 'auto_account_expired'
191
        $error eq 'overdue' ||
205
        || $error eq 'auto_too_late'
192
        $error eq 'too_unseen' ||
206
        || $error eq 'auto_too_much_oweing'
193
        $error eq 'auto_account_expired' ||
207
        || $error eq 'auto_too_soon'
194
        $error eq 'auto_too_late' ||
208
        || $error eq 'too_soon'
195
        $error eq 'auto_too_much_oweing' ||
209
        || $error eq 'item_denied_renewal'
196
        $error eq 'auto_too_soon' ||
210
        || $error eq 'item_issued_to_other_patron' )
197
        $error eq 'item_denied_renewal' ||
211
    {
198
        $error eq 'item_issued_to_other_patron'
212
        if ($verbose) {
199
    ) {
200
        if ( $verbose ) {
213
            say sprintf "Issue id: %s for borrower: %s and item: %s %s not be renewed. (%s)",
201
            say sprintf "Issue id: %s for borrower: %s and item: %s %s not be renewed. (%s)",
214
                $auto_renew->issue_id, $auto_renew->borrowernumber, $auto_renew->itemnumber,
202
                $auto_renew->issue_id, $auto_renew->borrowernumber, $auto_renew->itemnumber,
215
                $confirm ? 'will' : 'would', $error;
203
                $confirm ? 'will' : 'would', $error;
216
        }
204
        }
217
        $updated = 1 if ( !$auto_renew->auto_renew_error || $error ne $auto_renew->auto_renew_error );
205
        if ( $updated ) {
218
        if ($updated) {
219
            $auto_renew->auto_renew_error($error)->store if $confirm;
220
            push @{ $report{ $auto_renew->borrowernumber } }, $auto_renew
206
            push @{ $report{ $auto_renew->borrowernumber } }, $auto_renew
221
                if $error ne 'auto_too_soon' && ( $wants_messages && !$wants_digest );  # Do not notify if it's too soon
207
                if $error ne 'auto_too_soon' && ( $wants_messages && !$wants_digest );  # Do not notify if it's too soon
222
        }
208
        }
(-)a/t/db_dependent/Koha/Checkouts.t (-3 / +68 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 11;
22
use Test::More tests => 12;
23
use Test::MockModule;
24
use Test::Warn;
23
25
24
use C4::Circulation qw( MarkIssueReturned AddReturn );
26
use C4::Circulation qw( MarkIssueReturned AddReturn );
25
use C4::Reserves qw( AddReserve );
27
use C4::Reserves qw( AddReserve );
Lines 481-484 subtest 'automatic_checkin' => sub { Link Here
481
    };
483
    };
482
484
483
    $schema->storage->txn_rollback;
485
    $schema->storage->txn_rollback;
484
}
486
};
487
488
subtest 'attempt_auto_renew' => sub {
489
490
    plan tests => 17;
491
492
    $schema->storage->txn_begin;
493
494
    my $renew_error = 'auto_renew';
495
    my $module      = Test::MockModule->new('C4::Circulation');
496
    $module->mock( 'CanBookBeRenewed', sub { return ( 1, $renew_error ) } );
497
    my $around_now = dt_from_string();
498
    $module->mock( 'AddRenewal', sub { warn "AddRenewal called" } );
499
    my $checkout = $builder->build_object(
500
        {
501
            class => 'Koha::Checkouts',
502
            value => {
503
                date_due         => '2023-01-01 23:59:59',
504
                returndate       => undef,
505
                auto_renew       => 1,
506
                auto_renew_error => undef,
507
                onsite_checkout  => 0,
508
                renewals_count   => 0,
509
            }
510
        }
511
    );
512
513
    my ( $success, $error, $updated );
514
    warning_is {
515
        ( $success, $error, $updated ) = $checkout->attempt_auto_renew();
516
    }
517
    undef, "AddRenewal not called without confirm";
518
    ok( $success, "Issue is renewed when error is 'auto_renew'" );
519
    is( $error, undef, "No error when renewed" );
520
    ok( $updated, "Issue reported as updated when renewed" );
521
522
    warning_is {
523
        ( $success, $error, $updated ) = $checkout->attempt_auto_renew( { confirm => 1 } );
524
    }
525
    "AddRenewal called", "AddRenewal called when confirm is passed";
526
    ok( $success, "Issue is renewed when error is 'auto_renew'" );
527
    is( $error, undef, "No error when renewed" );
528
    ok( $updated, "Issue reported as updated when renewed" );
529
530
    $renew_error = 'anything_else';
531
    ( $success, $error, $updated ) = $checkout->attempt_auto_renew();
532
    ok( !$success, "Success is untrue for any other status" );
533
    is( $error, 'anything_else', "The error is passed through" );
534
    ok( $updated, "Issue reported as updated when status changes" );
535
    $checkout->discard_changes();
536
    is( $checkout->auto_renew_error, undef, "Error not updated if confirm not passed" );
537
538
    ( $success, $error, $updated ) = $checkout->attempt_auto_renew( { confirm => 1 } );
539
    ok( !$success, "Success is untrue for any other status" );
540
    is( $error, 'anything_else', "The error is passed through" );
541
    ok( $updated, "Issue updated when confirm passed" );
542
    $checkout->discard_changes();
543
    is( $checkout->auto_renew_error, 'anything_else', "Error updated if confirm passed" );
544
545
    # Error now equals 'anything_else'
546
    ( $success, $error, $updated ) = $checkout->attempt_auto_renew();
547
    ok( !$updated, "Issue not reported as updated when status has not changed" );
548
549
    $schema->storage->txn_rollback;
550
};
485
- 

Return to bug 34924