Bug 15240: Do not process issues with a date due later than today
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Wed, 9 Dec 2015 11:10:33 +0000 (11:10 +0000)
committerBrendan A Gallagher <brendan@bywatersolutions.com>
Thu, 28 Jan 2016 19:15:34 +0000 (19:15 +0000)
There is no need to do this job in Perl, MySQL could do it instead.
The idea is to only retrieve the issues info which could be overdued.

To test:
1/ Run the script
2/ Apply the patch
3/ Run the script again, notice the exact same results but execution time is faster

Signed-off-by: Chris Cormack <chrisc@catalyst.net.nz>
Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Brendan A Gallagher <brendan@bywatersolutions.com>
misc/cronjobs/overdue_notices.pl

index e184edb..c7a5873 100755 (executable)
@@ -454,6 +454,7 @@ SELECT biblio.*, items.*, issues.*, biblioitems.itemtype, TO_DAYS($date)-TO_DAYS
     AND b.branchcode = items.homebranch
     AND biblio.biblionumber   = biblioitems.biblionumber
     AND issues.borrowernumber = ?
+    AND TO_DAYS($date)-TO_DAYS(issues.date_due) >= 0
 END_SQL
 
     my $query = "SELECT * FROM overduerules WHERE delay1 IS NOT NULL AND branchcode = ? ";
@@ -499,15 +500,14 @@ END_SQL
            # itemcount is interpreted here as the number of items in the overdue range defined by the current notice or all overdues < max if(-list-all).
             # <date> <itemcount> <firstname> <lastname> <address1> <address2> <address3> <city> <postcode> <country>
 
-            my $borrower_sql = <<'END_SQL';
-SELECT issues.borrowernumber, firstname, surname, address, address2, city, zipcode, country, email, emailpro, B_email, smsalertnumber, phone, cardnumber,
-TO_DAYS(?)-TO_DAYS(date_due) as difference, date_due
+            my $borrower_sql = <<"END_SQL";
+SELECT issues.borrowernumber, firstname, surname, address, address2, city, zipcode, country, email, emailpro, B_email, smsalertnumber, phone, cardnumber, date_due
 FROM   issues,borrowers,categories
 WHERE  issues.borrowernumber=borrowers.borrowernumber
 AND    borrowers.categorycode=categories.categorycode
+AND    TO_DAYS($date)-TO_DAYS(issues.date_due) >= 0
 END_SQL
             my @borrower_parameters;
-            push @borrower_parameters, $date_to_run->datetime();
             if ($branchcode) {
                 $borrower_sql .= ' AND issues.branchcode=? ';
                 push @borrower_parameters, $branchcode;
@@ -526,8 +526,6 @@ END_SQL
             my $borrowernumber;
             while ( my $data = $sth->fetchrow_hashref ) {
 
-                next unless ( DateTime->compare( $date_to_run, dt_from_string($data->{date_due})) ) == 1;
-
                 # check the borrower has at least one item that matches
                 my $days_between;
                 if ( C4::Context->preference('OverdueNoticeCalendar') )
@@ -616,8 +614,6 @@ END_SQL
                 my $j = 0;
                 my $exceededPrintNoticesMaxLines = 0;
                 while ( my $item_info = $sth2->fetchrow_hashref() ) {
-                    next unless ( DateTime->compare( $date_to_run,  dt_from_string($item_info->{date_due})) ) == 1;
-
                     if ( C4::Context->preference('OverdueNoticeCalendar') ) {
                         my $calendar =
                           Koha::Calendar->new( branchcode => $branchcode );