ffzg/recall_notices.pl: added --interval and --dedup
[koha.git] / misc / cronjobs / thirdparty / TalkingTech_itiva_outbound.pl
index 0e3cc91..d1ff392 100755 (executable)
@@ -37,6 +37,7 @@ use C4::Letters;
 use C4::Overdues;
 use Koha::Calendar;
 use Koha::DateUtils;
+use Koha::Patrons;
 
 sub usage {
     pod2usage( -verbose => 2 );
@@ -57,6 +58,7 @@ my @holds_waiting_days_to_call;
 my $library_code;
 my $help;
 my $outfile;
+my $skip_patrons_with_email;
 
 # maps to convert I-tiva terms to Koha terms
 my $type_module_map = {
@@ -78,6 +80,7 @@ GetOptions(
     'type:s'                => \@types,
     'w|waiting-hold-day:s'  => \@holds_waiting_days_to_call,
     'c|code|library-code:s' => \$library_code,
+    's|skip-patrons-with-email' => \$skip_patrons_with_email,
     'help|h'                => \$help,
 );
 
@@ -93,7 +96,7 @@ if ( defined $outfile ) {
 } else {
     print "No output file defined; printing to STDOUT\n"
       if ( defined $verbose );
-    open( $OUT, '>', "&STDOUT" ) || die("Couldn't duplicate STDOUT: $!");
+    $OUT = *STDOUT || die "Couldn't duplicate STDOUT: $!";
 }
 
 my $format = 'V';    # format for phone notifications
@@ -116,13 +119,18 @@ foreach my $type (@types) {
         next;
     }
 
+    my $patrons;
     foreach my $issues (@loop) {
+        $patrons->{$issues->{borrowernumber}} ||= Koha::Patrons->find( $issues->{borrowernumber} ) if $skip_patrons_with_email;
+        next if $skip_patrons_with_email && $patrons->{$issues->{borrowernumber}}->notice_email_address;
+
         my $date_dt = dt_from_string ( $issues->{'date_due'} );
         my $due_date = output_pref( { dt => $date_dt, dateonly => 1, dateformat =>'metric' } );
 
         my $letter = C4::Letters::GetPreparedLetter(
             module      => $module,
             letter_code => $code,
+            lang        => 'default', # It does not sound useful to send a lang here
             tables      => {
                 borrowers   => $issues->{'borrowernumber'},
                 biblio      => $issues->{'biblionumber'},
@@ -220,7 +228,9 @@ sub GetOverdueIssues {
                 JOIN biblio USING (biblionumber)
                 JOIN branches ON (issues.branchcode = branches.branchcode)
                 JOIN overduerules USING (categorycode)
+                JOIN overduerules_transport_types USING ( overduerules_id )
                 WHERE ( overduerules.branchcode = borrowers.branchcode or overduerules.branchcode = '')
+                AND overduerules_transport_types.message_transport_type = 'phone'
                 AND ( (TO_DAYS(NOW())-TO_DAYS(date_due) ) = delay1
                   OR  (TO_DAYS(NOW())-TO_DAYS(date_due) ) = delay2
                   OR  (TO_DAYS(NOW())-TO_DAYS(date_due) ) = delay3 )
@@ -291,16 +301,23 @@ sub GetWaitingHolds {
     $sth->execute();
     my @results;
     while ( my $issue = $sth->fetchrow_hashref() ) {
-        my @waitingdate = split( /-/, $issue->{'waitingdate'} );
-        my @date_due = Add_Delta_Days( $waitingdate[0], $waitingdate[1], $waitingdate[2], $pickupdelay );
-        $issue->{'date_due'} = sprintf( "%04d-%02d-%02d", $date_due[0], $date_due[1], $date_due[2] );
+        my $calendar = Koha::Calendar->new( branchcode => $issue->{'site'} );
+
+        my $waiting_date = dt_from_string( $issue->{waitingdate}, 'sql' );
+        my $pickup_date = $waiting_date->clone->add( days => $pickupdelay );
+        if ( $calendar->is_holiday($pickup_date) ) {
+            $pickup_date = $calendar->next_open_day( $pickup_date );
+        }
+
+        $issue->{'date_due'} = output_pref({dt => $pickup_date, dateformat => 'iso' });
         $issue->{'level'} = 1;    # only one level for Hold Waiting notifications
 
         my $days_to_subtract = 0;
-        my $calendar = C4::Calendar->new( branchcode => $issue->{'site'} );
-        while ( $calendar->isHoliday( reverse( Add_Delta_Days( $waitingdate[0], $waitingdate[1], $waitingdate[2], $days_to_subtract ) ) ) ) {
-            $days_to_subtract++;
+        if ( $calendar->is_holiday($waiting_date) ) {
+            my $next_open_day = $calendar->next_open_day( $waiting_date );
+            $days_to_subtract = $calendar->days_between($waiting_date, $next_open_day)->days;
         }
+
         $issue->{'days_since_waiting'} = $issue->{'days_since_waiting'} - $days_to_subtract;
 
         if ( ( grep $_ eq $issue->{'days_since_waiting'}, @holds_waiting_days_to_call )