Bug 27797: Make POST /holds use the stashed koha.overrides
[koha-ffzg.git] / C4 / Letters.pm
index d3e354f..40d2f18 100644 (file)
@@ -31,6 +31,7 @@ use Try::Tiny;
 use C4::Members;
 use C4::Log;
 use C4::SMS;
+use C4::Templates;
 use C4::Debug;
 use Koha::DateUtils;
 use Koha::SMS::Providers;
@@ -49,7 +50,7 @@ BEGIN {
     require Exporter;
     @ISA = qw(Exporter);
     @EXPORT = qw(
-        &GetLetters &GetLettersAvailableForALibrary &GetLetterTemplates &DelLetter &GetPreparedLetter &GetWrappedLetter &SendAlerts &GetPrintMessages &GetMessageTransportTypes
+        &EnqueueLetter &GetLetters &GetLettersAvailableForALibrary &GetLetterTemplates &DelLetter &GetPreparedLetter &GetWrappedLetter &SendAlerts &GetPrintMessages &GetMessageTransportTypes
     );
 }
 
@@ -294,6 +295,8 @@ sub DelLetter {
 sub SendAlerts {
     my ( $type, $externalid, $letter_code ) = @_;
     my $dbh = C4::Context->dbh;
+    my $error;
+
     if ( $type eq 'issue' ) {
 
         # prepare the letter...
@@ -357,13 +360,19 @@ sub SendAlerts {
                 $mail->text_body( $letter->{content} );
             }
 
-            try {
+            my $success = try {
                 $mail->send_or_die({ transport => $library->smtp_server->transport });
             }
             catch {
+                # We expect ref($_) eq 'Email::Sender::Failure'
+                $error = $_->message;
+
                 carp "$_";
-                return { error => "$_" };
+                return;
             };
+
+            return { error => $error }
+                unless $success;
         }
     }
     elsif ( $type eq 'claimacquisition' or $type eq 'claimissues' or $type eq 'orderacquisition' ) {
@@ -511,14 +520,20 @@ sub SendAlerts {
             $mail->text_body( "" . $letter->{content} );
         }
 
-        try {
+        my $success = try {
             $mail->send_or_die({ transport => $library->smtp_server->transport });
         }
         catch {
+            # We expect ref($_) eq 'Email::Sender::Failure'
+            $error = $_->message;
+
             carp "$_";
-            return { error => "$_" };
+            return;
         };
 
+        return { error => $error }
+            unless $success;
+
         logaction(
             "ACQUISITION",
             $action,
@@ -547,7 +562,8 @@ sub SendAlerts {
             want_librarian => 1,
         ) or return;
         return { error => "no_email" } unless $externalid->{'emailaddr'};
-        try {
+
+        my $success = try {
 
             # FIXME: This 'default' behaviour should be moved to Koha::Email
             my $mail = Koha::Email->create(
@@ -570,9 +586,15 @@ sub SendAlerts {
             $mail->send_or_die({ transport => $library->smtp_server->transport });
         }
         catch {
+            # We expect ref($_) eq 'Email::Sender::Failure'
+            $error = $_->message;
+
             carp "$_";
-            return { error => "$_" };
+            return;
         };
+
+        return { error => $error }
+            unless $success;
     }
 
     # If we come here, return an OK status
@@ -606,13 +628,13 @@ sub GetPreparedLetter {
     my %params = @_;
 
     my $letter = $params{letter};
+    my $lang   = $params{lang} || 'default';
 
     unless ( $letter ) {
         my $module      = $params{module} or croak "No module";
         my $letter_code = $params{letter_code} or croak "No letter_code";
         my $branchcode  = $params{branchcode} || '';
         my $mtt         = $params{message_transport_type} || 'email';
-        my $lang        = $params{lang} || 'default';
 
         $letter = getletter( $module, $letter_code, $branchcode, $mtt, $lang );
 
@@ -707,6 +729,7 @@ sub GetPreparedLetter {
             tables  => $tables,
             loops  => $loops,
             substitute => $substitute,
+            lang => $lang
         }
     );
 
@@ -763,8 +786,11 @@ sub _parseletter_sth {
     #       broke things for the rest of us. prepare_cached is a better
     #       way to cache statement handles anyway.
     my $query = 
+    ($table eq 'accountlines' )    ? "SELECT * FROM $table WHERE   accountlines_id = ?"                               :
     ($table eq 'biblio'       )    ? "SELECT * FROM $table WHERE   biblionumber = ?"                                  :
     ($table eq 'biblioitems'  )    ? "SELECT * FROM $table WHERE   biblionumber = ?"                                  :
+    ($table eq 'credits'      )    ? "SELECT * FROM accountlines WHERE   accountlines_id = ?"                         :
+    ($table eq 'debits'       )    ? "SELECT * FROM accountlines WHERE   accountlines_id = ?"                         :
     ($table eq 'items'        )    ? "SELECT * FROM $table WHERE     itemnumber = ?"                                  :
     ($table eq 'issues'       )    ? "SELECT * FROM $table WHERE     itemnumber = ?"                                  :
     ($table eq 'old_issues'   )    ? "SELECT * FROM $table WHERE     itemnumber = ? ORDER BY timestamp DESC LIMIT 1"  :
@@ -839,7 +865,6 @@ sub _parseletter {
         # Dates replacement
         my $replacedby   = defined ($val) ? $val : '';
         if (    $replacedby
-            and not $replacedby =~ m|0000-00-00|
             and not $replacedby =~ m|9999-12-31|
             and $replacedby =~ m|^\d{4}-\d{2}-\d{2}( \d{2}:\d{2}:\d{2})?$| )
         {
@@ -988,6 +1013,7 @@ sub SendQueuedMessages {
     my $params = shift;
 
     my $which_unsent_messages  = {
+        'message_id'     => $params->{'message_id'},
         'limit'          => $params->{'limit'} // 0,
         'borrowernumber' => $params->{'borrowernumber'} // q{},
         'letter_code'    => $params->{'letter_code'} // q{},
@@ -1243,6 +1269,7 @@ sub _add_attachments {
    message_transport_type: method of message sending (e.g. email, sms, etc.)
    borrowernumber        : who the message is to be sent
    letter_code           : type of message being sent (e.g. PASSWORD_RESET)
+   message_id            : the message_id of the message. In that case the sub will return only 1 result
    limit                 : maximum number of messages to send
 
   This function returns an array of matching hash referenced rows from
@@ -1279,6 +1306,10 @@ sub _get_unsent_messages {
             $statement .= ' AND message_transport_type = ? ';
             push @query_params, $params->{'type'};
         }
+        if ( $params->{message_id} ) {
+            $statement .= ' AND message_id = ?';
+            push @query_params, $params->{message_id};
+        }
         if ( $params->{'limit'} ) {
             $statement .= ' limit ? ';
             push @query_params, $params->{'limit'};
@@ -1498,6 +1529,16 @@ sub _process_tt {
     my $tables = $params->{tables};
     my $loops = $params->{loops};
     my $substitute = $params->{substitute} || {};
+    my $lang = defined($params->{lang}) && $params->{lang} ne 'default' ? $params->{lang} : 'en';
+    my ($theme, $activethemes);
+
+    my $htdocs = C4::Context->config('intrahtdocs');
+    ($theme, $lang, $activethemes)= C4::Templates::activethemes( $htdocs, 'about.tt', 'intranet', $lang);
+    my @includes;
+    foreach (@$activethemes) {
+        push @includes, "$htdocs/$_/$lang/includes";
+        push @includes, "$htdocs/$_/en/includes" unless $lang eq 'en';
+    }
 
     my $use_template_cache = C4::Context->config('template_cache_dir') && defined $ENV{GATEWAY_INTERFACE};
     my $template           = Template->new(
@@ -1507,6 +1548,7 @@ sub _process_tt {
             PLUGIN_BASE  => 'Koha::Template::Plugin',
             COMPILE_EXT  => $use_template_cache ? '.ttc' : '',
             COMPILE_DIR  => $use_template_cache ? C4::Context->config('template_cache_dir') : '',
+            INCLUDE_PATH => \@includes,
             FILTERS      => {},
             ENCODING     => 'UTF-8',
         }
@@ -1566,6 +1608,18 @@ sub _get_tt_params {
             plural   => 'branches',
             pk       => 'branchcode',
         },
+        credits => {
+            module => 'Koha::Account::Lines',
+            singular => 'credit',
+            plural => 'credits',
+            pk => 'accountlines_id',
+        },
+        debits => {
+            module => 'Koha::Account::Lines',
+            singular => 'debit',
+            plural => 'debits',
+            pk => 'accountlines_id',
+        },
         items => {
             module   => 'Koha::Items',
             singular => 'item',