Bug 15548: Move new patron related code to Patron*
[koha.git] / misc / cronjobs / overdue_notices.pl
index 98b0a25..f77e3bb 100755 (executable)
@@ -18,8 +18,7 @@
 # You should have received a copy of the GNU General Public License
 # along with Koha; if not, see <http://www.gnu.org/licenses>.
 
-use strict;
-use warnings;
+use Modern::Perl;
 
 BEGIN {
 
@@ -32,20 +31,18 @@ BEGIN {
 use Getopt::Long;
 use Pod::Usage;
 use Text::CSV_XS;
-use Locale::Currency::Format 1.28;
-use Encode;
 use DateTime;
 use DateTime::Duration;
 
 use C4::Context;
-use C4::Debug;
 use C4::Letters;
 use C4::Overdues qw(GetFine GetOverdueMessageTransportTypes parse_overdues_letter);
-use C4::Budgets qw(GetCurrency);
 use C4::Log;
-use Koha::Borrower::Debarments qw(AddUniqueDebarment);
+use Koha::Patron::Debarments qw(AddUniqueDebarment);
 use Koha::DateUtils;
 use Koha::Calendar;
+use Koha::Libraries;
+use Koha::Acquisition::Currencies;
 
 =head1 NAME
 
@@ -61,6 +58,7 @@ overdue_notices.pl
  Options:
    -help                          brief help message
    -man                           full documentation
+   -v                             verbose
    -n                             No email will be sent
    -max          <days>           maximum days overdue to deal with
    -library      <branchname>     only deal with overdues from this library (repeatable : several libraries can be given)
@@ -70,6 +68,9 @@ overdue_notices.pl
    -itemscontent <list of fields> item information in templates
    -borcat       <categorycode>   category code that must be included
    -borcatout    <categorycode>   category code that must be excluded
+   -t                             only include triggered overdues
+   -list-all                      list all overdues
+   -date         <yyyy-mm-dd>     emulate overdues run for this date
    -email        <email_type>     type of email that will be used. Can be 'email', 'emailpro' or 'B_email'. Repeatable.
 
 =head1 OPTIONS
@@ -141,11 +142,11 @@ issues tables.
 
 =item B<-borcat>
 
-Repetable field, that permit to select only few of patrons categories.
+Repeatable field, that permits to select only some patron categories.
 
 =item B<-borcatout>
 
-Repetable field, permis to exclude some patrons categories.
+Repeatable field, that permits to exclude some patron categories.
 
 =item B<-t> | B<--triggered>
 
@@ -250,7 +251,7 @@ administrator email address.
 =head1 USAGE EXAMPLES
 
 C<overdue_notices.pl> - In this most basic usage, with no command line
-arguments, all libraries are procesed individually, and notices are
+arguments, all libraries are processed individually, and notices are
 prepared for all patrons with overdue items for whom we have email
 addresses. Messages for those patrons for whom we have no email
 address are sent in a single attachment to the library administrator's
@@ -439,21 +440,22 @@ foreach my $branchcode (@branches) {
         }
     }
 
-    my $branch_details      = C4::Branch::GetBranchDetail($branchcode);
-    my $admin_email_address = $branch_details->{'branchemail'}
+    my $library             = Koha::Libraries->find($branchcode);
+    my $admin_email_address = $library->branchemail
       || C4::Context->preference('KohaAdminEmailAddress');
     my @output_chunks;    # may be sent to mail or stdout or csv file.
 
     $verbose and warn sprintf "branchcode : '%s' using %s\n", $branchcode, $admin_email_address;
 
     my $sth2 = $dbh->prepare( <<"END_SQL" );
-SELECT biblio.*, items.*, issues.*, biblioitems.itemtype, TO_DAYS($date)-TO_DAYS(date_due) AS days_overdue, branchname
+SELECT biblio.*, items.*, issues.*, biblioitems.itemtype, branchname
   FROM issues,items,biblio, biblioitems, branches b
   WHERE items.itemnumber=issues.itemnumber
     AND biblio.biblionumber   = items.biblionumber
     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 +501,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 +527,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 +615,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 );
@@ -674,13 +671,17 @@ END_SQL
                 my $print_sent = 0; # A print notice is not yet sent for this patron
                 for my $mtt ( @message_transport_types ) {
 
+                    if ( ($mtt eq 'email' and not scalar @emails_to_use) or ($mtt eq 'sms' and not $data->{smsalertnumber}) ) {
+                        # email or sms is requested but not exist, do a print.
+                        $mtt = 'print';
+                    }
                     my $letter = parse_overdues_letter(
                         {   letter_code     => $overdue_rules->{"letter$i"},
                             borrowernumber  => $borrowernumber,
                             branchcode      => $branchcode,
                             items           => \@items,
                             substitute      => {    # this appears to be a hack to overcome incomplete features in this code.
-                                                bib             => $branch_details->{'branchname'}, # maybe 'bib' is a typo for 'lib<rary>'?
+                                                bib             => $library->branchname, # maybe 'bib' is a typo for 'lib<rary>'?
                                                 'items.content' => $titles,
                                                 'count'         => $itemcount,
                                                },
@@ -714,7 +715,7 @@ END_SQL
                               city           => $data->{'city'},
                               phone          => $data->{'phone'},
                               cardnumber     => $data->{'cardnumber'},
-                              branchname     => $branch_details->{'branchname'},
+                              branchname     => $library->branchname,
                               letternumber   => $i,
                               postcode       => $data->{'zipcode'},
                               country        => $data->{'country'},
@@ -726,8 +727,6 @@ END_SQL
                           );
                     } else {
                         if ( ($mtt eq 'email' and not scalar @emails_to_use) or ($mtt eq 'sms' and not $data->{smsalertnumber}) ) {
-                            # email or sms is requested but not exist, do a print.
-                            $mtt = 'print';
                             push @output_chunks,
                               prepare_letter_for_printing(
                               {   letter         => $letter,