Merge remote branch 'koha-fbc/k_bug_5182' into to-push
[koha_fer] / C4 / Letters.pm
index 002455f..885ed5f 100644 (file)
@@ -13,19 +13,23 @@ package C4::Letters;
 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along with
-# Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
-# Suite 330, Boston, MA  02111-1307 USA
+# You should have received a copy of the GNU General Public License along
+# with Koha; if not, write to the Free Software Foundation, Inc.,
+# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
 use strict;
 use warnings;
 
 use MIME::Lite;
 use Mail::Sendmail;
+use Encode;
+use Carp;
+
 use C4::Members;
 use C4::Log;
 use C4::SMS;
 use C4::Debug;
+use Date::Calc qw( Add_Delta_Days );
 use Encode;
 use Carp;
 
@@ -37,7 +41,7 @@ BEGIN {
        $VERSION = 3.01;
        @ISA = qw(Exporter);
        @EXPORT = qw(
-       &GetLetters &getletter &addalert &getalert &delalert &findrelatedto &SendAlerts
+       &GetLetters &getletter &addalert &getalert &delalert &findrelatedto &SendAlerts GetPrintMessages
        );
 }
 
@@ -150,7 +154,7 @@ sub addalert ($$$) {
     parameters :
     - alertid : the alert id
     deletes the alert
-    
+
 =cut
 
 sub delalert ($) {
@@ -345,8 +349,6 @@ sub SendAlerts {
                 'Content-Type' => 'text/plain; charset="utf8"',
             );
             sendmail(%mail) or carp $Mail::Sendmail::error;
-            warn
-"sending to $mail{To} From $mail{From} subj $mail{Subject} Mess $mail{Message}";
         }
         if ( C4::Context->preference("LetterLog") ) {
             logaction(
@@ -435,8 +437,6 @@ sub SendAlerts {
                   . $innerletter->{content}
             ) if C4::Context->preference("LetterLog");
         }
-        warn
-"sending to From $userenv->{emailaddress} subj $innerletter->{title} Mess $innerletter->{content}";
     }    
    # send an "account details" notice to a newly created user 
     elsif ( $type eq 'members' ) {
@@ -506,7 +506,6 @@ sub parseletter {
         carp "ERROR: parseletter() 1st argument 'letter' empty";
         return;
     }
-    #  warn "Parseletter : ($letter, $table, $pk ...)";
     my $sth = parseletter_sth($table);
     unless ($sth) {
         warn "parseletter_sth('$table') failed to return a valid sth.  No substitution will be done for that table.";
@@ -519,23 +518,38 @@ sub parseletter {
     }
 
     my $values = $sth->fetchrow_hashref;
+    
+    # TEMPORARY hack until the expirationdate column is added to reserves
+    if ( $table eq 'reserves' && $values->{'waitingdate'} ) {
+        my @waitingdate = split /-/, $values->{'waitingdate'};
+
+        $values->{'expirationdate'} = C4::Dates->new(
+            sprintf(
+                '%04d-%02d-%02d',
+                Add_Delta_Days( @waitingdate, C4::Context->preference( 'ReservesMaxPickUpDelay' ) )
+            ),
+            'iso'
+        )->output();
+    }
+
 
     # and get all fields from the table
     my $columns = C4::Context->dbh->prepare("SHOW COLUMNS FROM $table");
     $columns->execute;
     while ( ( my $field ) = $columns->fetchrow_array ) {
         my $replacefield = "<<$table.$field>>";
+        $values->{$field} =~ s/\p{P}(?=$)//g if $values->{$field};
         my $replacedby   = $values->{$field} || '';
         ($letter->{title}  ) and $letter->{title}   =~ s/$replacefield/$replacedby/g;
         ($letter->{content}) and $letter->{content} =~ s/$replacefield/$replacedby/g;
     }
+    return $letter;
 }
 
 =head2 EnqueueLetter
 
-=over 4
-
-my $success = EnqueueLetter( { letter => $letter, borrowernumber => '12', message_transport_type => 'email' } )
+  my $success = EnqueueLetter( { letter => $letter, 
+        borrowernumber => '12', message_transport_type => 'email' } )
 
 places a letter in the message_queue database table, which will
 eventually get processed (sent) by the process_message_queue.pl
@@ -543,8 +557,6 @@ cronjob when it calls SendQueuedMessages.
 
 return true on success
 
-=back
-
 =cut
 
 sub EnqueueLetter ($) {
@@ -590,16 +602,12 @@ ENDSQL
 
 =head2 SendQueuedMessages ([$hashref]) 
 
-=over 4
+  my $sent = SendQueuedMessages( { verbose => 1 } );
 
 sends all of the 'pending' items in the message queue.
 
-my $sent = SendQueuedMessages( { verbose => 1 } );
-
 returns number of messages sent.
 
-=back
-
 =cut
 
 sub SendQueuedMessages (;$) {
@@ -626,14 +634,10 @@ sub SendQueuedMessages (;$) {
 
 =head2 GetRSSMessages
 
-=over 4
-
-my $message_list = GetRSSMessages( { limit => 10, borrowernumber => '14' } )
+  my $message_list = GetRSSMessages( { limit => 10, borrowernumber => '14' } )
 
 returns a listref of all queued RSS messages for a particular person.
 
-=back
-
 =cut
 
 sub GetRSSMessages {
@@ -648,19 +652,31 @@ sub GetRSSMessages {
                                    borrowernumber         => $params->{'borrowernumber'}, } );
 }
 
-=head2 GetQueuedMessages ([$hashref])
+=head2 GetPrintMessages
 
-=over 4
+  my $message_list = GetPrintMessages( { borrowernumber => $borrowernumber } )
 
-my $messages = GetQueuedMessage( { borrowernumber => '123', limit => 20 } );
+Returns a arrayref of all queued print messages (optionally, for a particular
+person).
+
+=cut
+
+sub GetPrintMessages {
+    my $params = shift || {};
+    
+    return _get_unsent_messages( { message_transport_type => 'print',
+                                   borrowernumber         => $params->{'borrowernumber'}, } );
+}
+
+=head2 GetQueuedMessages ([$hashref])
+
+  my $messages = GetQueuedMessage( { borrowernumber => '123', limit => 20 } );
 
 fetches messages out of the message queue.
 
 returns:
 list of hashes, each has represents a message in the message queue.
 
-=back
-
 =cut
 
 sub GetQueuedMessages {
@@ -770,7 +786,7 @@ ENDSQL
     return $sth->fetchall_arrayref({});
 }
 
-sub _send_message_by_email ($) {
+sub _send_message_by_email ($;$$$) {
     my $message = shift or return;
 
     my $to_address = $message->{to_address};
@@ -782,7 +798,14 @@ sub _send_message_by_email ($) {
                                    status     => 'failed' } );
             return;
         }
-        unless ($to_address = $member->{email}) {   # assigment, not comparison
+        my $which_address = C4::Context->preference('AutoEmailPrimaryAddress');
+        # If the system preference is set to 'first valid' (value == OFF), look up email address
+        if ($which_address eq 'OFF') {
+            $to_address = GetFirstValidEmailAddress( $message->{'borrowernumber'} );
+        } else {
+            $to_address = $member->{$which_address};
+        }
+        unless ($to_address) {  
             # warn "FAIL: No 'to_address' and no email for " . ($member->{surname} ||'') . ", borrowernumber ($message->{borrowernumber})";
             # warning too verbose for this more common case?
             _set_message_status( { message_id => $message->{'message_id'},