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

(-)a/misc/cronjobs/automatic_renewals.pl (-14 / +55 lines)
Lines 42-65 No options. Link Here
42
42
43
use Modern::Perl;
43
use Modern::Perl;
44
44
45
use C4::Branch;
45
use C4::Circulation;
46
use C4::Circulation;
46
use C4::Context;
47
use C4::Context;
47
use C4::Log;
48
use C4::Log;
49
use C4::Letters;
50
use Koha::Checkouts;
51
use Koha::Borrowers;
48
52
49
cronlogaction();
53
cronlogaction();
50
54
51
my $dbh = C4::Context->dbh;
55
my $auto_renews = Koha::Checkouts->search({ auto_renew => 1 });
52
my ( $borrowernumber, $itemnumber, $branch, $ok, $error );
56
53
57
my %report;
54
my $query =
58
while ( my $auto_renew = $auto_renews->next ) {
55
"SELECT borrowernumber, itemnumber, branchcode FROM issues WHERE auto_renew = 1";
59
56
my $sth = $dbh->prepare($query);
60
    # CanBookBeRenewed returns 'auto_renew' when the renewal should be done by this script
57
$sth->execute();
61
    my ( $ok, $error ) = CanBookBeRenewed( $auto_renew->borrowernumber, $auto_renew->itemnumber );
58
62
    if ( $error eq 'auto_renew' ) {
59
while ( ( $borrowernumber, $itemnumber, $branch ) = $sth->fetchrow_array ) {
63
        my $date_due = AddRenewal( $auto_renew->borrowernumber, $auto_renew->itemnumber, $auto_renew->branchcode );
64
        push @{ $report{ $auto_renew->borrowernumber } }, $auto_renew;
65
    } elsif ( $error eq 'too_many'
66
        or $error eq 'on_reserve'
67
        or $error eq 'restriction'
68
        or $error eq 'overdue'
69
        or $error eq 'auto_too_late'
70
        or $error eq 'auto_too_much_oweing'
71
        or $error eq 'auto_too_soon' ) {
72
        if ( not $auto_renew->auto_renew_error or $error ne $auto_renew->auto_renew_error ) {
73
            $auto_renew->auto_renew_error($error)->store;
74
            push @{ $report{ $auto_renew->borrowernumber } }, $auto_renew
75
              if $error ne 'auto_too_soon';    # Do not notify if it's too soon
76
        }
77
    }
78
}
60
79
61
# CanBookBeRenewed returns 'auto_renew' when the renewal should be done by this script
80
for my $borrowernumber ( keys %report ) {
62
    ( $ok, $error ) = CanBookBeRenewed( $borrowernumber, $itemnumber );
81
    my $patron = Koha::Borrowers->find($borrowernumber);
63
    AddRenewal( $borrowernumber, $itemnumber, $branch )
82
    my @issues;
64
      if ( $error eq "auto_renew" );
83
    for my $issue ( @{ $report{$borrowernumber} } ) {
84
        my $item   = Koha::Items->find( $issue->itemnumber );
85
        my $letter = C4::Letters::GetPreparedLetter(
86
            module      => 'circulation',
87
            letter_code => 'AUTO_RENEWALS',
88
            tables      => {
89
                borrowers => $patron->borrowernumber,
90
                issues    => $issue->itemnumber,
91
                items     => $issue->itemnumber,
92
                biblio    => $item->biblionumber,
93
            },
94
        );
95
96
        my $branch_details = C4::Branch::GetBranchDetail( $patron->branchcode );
97
        my $admin_email_address = $branch_details->{'branchemail'} || C4::Context->preference('KohaAdminEmailAddress');
98
99
        C4::Letters::EnqueueLetter(
100
            {   letter                 => $letter,
101
                borrowernumber         => $borrowernumber,
102
                message_transport_type => 'email',
103
                from_address           => $admin_email_address,
104
            }
105
        );
106
    }
65
}
107
}
66
- 

Return to bug 15705