Lines 27-32
use Mail::Sendmail;
Link Here
|
27 |
use Try::Tiny; |
27 |
use Try::Tiny; |
28 |
use DateTime; |
28 |
use DateTime; |
29 |
|
29 |
|
|
|
30 |
use C4::Letters; |
31 |
use C4::Members; |
30 |
use Koha::Database; |
32 |
use Koha::Database; |
31 |
use Koha::Email; |
33 |
use Koha::Email; |
32 |
use Koha::Exceptions::Ill; |
34 |
use Koha::Exceptions::Ill; |
Lines 243-248
sub status_alias {
Link Here
|
243 |
|
245 |
|
244 |
Overloaded getter/setter for request status, |
246 |
Overloaded getter/setter for request status, |
245 |
also nullifies status_alias and records the fact that the status has changed |
247 |
also nullifies status_alias and records the fact that the status has changed |
|
|
248 |
and sends a notice if appropriate |
246 |
|
249 |
|
247 |
=cut |
250 |
=cut |
248 |
|
251 |
|
Lines 276-281
sub status {
Link Here
|
276 |
}); |
279 |
}); |
277 |
} |
280 |
} |
278 |
delete $self->{previous_status}; |
281 |
delete $self->{previous_status}; |
|
|
282 |
# If status has changed to cancellation requested, send a notice |
283 |
if ($new_status eq 'CANCREQ') { |
284 |
$self->send_staff_notice('ILL_REQUEST_CANCEL'); |
285 |
} |
279 |
return $ret; |
286 |
return $ret; |
280 |
} else { |
287 |
} else { |
281 |
return $current_status; |
288 |
return $current_status; |
Lines 1039-1076
sub generic_confirm {
Link Here
|
1039 |
my $branch = Koha::Libraries->find($params->{current_branchcode}) |
1046 |
my $branch = Koha::Libraries->find($params->{current_branchcode}) |
1040 |
|| die "Invalid current branchcode. Are you logged in as the database user?"; |
1047 |
|| die "Invalid current branchcode. Are you logged in as the database user?"; |
1041 |
if ( !$params->{stage}|| $params->{stage} eq 'init' ) { |
1048 |
if ( !$params->{stage}|| $params->{stage} eq 'init' ) { |
1042 |
my $draft->{subject} = "ILL Request"; |
|
|
1043 |
$draft->{body} = <<EOF; |
1044 |
Dear Sir/Madam, |
1045 |
|
1046 |
We would like to request an interlibrary loan for a title matching the |
1047 |
following description: |
1048 |
|
1049 |
EOF |
1050 |
|
1051 |
my $details = $self->metadata; |
1052 |
while (my ($title, $value) = each %{$details}) { |
1053 |
$draft->{body} .= " - " . $title . ": " . $value . "\n" |
1054 |
if $value; |
1055 |
} |
1056 |
$draft->{body} .= <<EOF; |
1057 |
|
1058 |
Please let us know if you are able to supply this to us. |
1059 |
|
1060 |
Kind Regards |
1061 |
|
1062 |
EOF |
1063 |
|
1049 |
|
1064 |
my @address = map { $branch->$_ } |
1050 |
# Get the message body from the notice definition |
1065 |
qw/ branchname branchaddress1 branchaddress2 branchaddress3 |
1051 |
my $letter = $self->get_notice({ |
1066 |
branchzip branchcity branchstate branchcountry branchphone |
1052 |
notice_code => 'ILL_PARTNER_REQ', |
1067 |
branchillemail branchemail /; |
1053 |
transport => 'email' |
1068 |
my $address = ""; |
1054 |
}); |
1069 |
foreach my $line ( @address ) { |
|
|
1070 |
$address .= $line . "\n" if $line; |
1071 |
} |
1072 |
|
1073 |
$draft->{body} .= $address; |
1074 |
|
1055 |
|
1075 |
my $partners = Koha::Patrons->search({ |
1056 |
my $partners = Koha::Patrons->search({ |
1076 |
categorycode => $self->_config->partner_code |
1057 |
categorycode => $self->_config->partner_code |
Lines 1082-1088
EOF
Link Here
|
1082 |
method => 'generic_confirm', |
1063 |
method => 'generic_confirm', |
1083 |
stage => 'draft', |
1064 |
stage => 'draft', |
1084 |
value => { |
1065 |
value => { |
1085 |
draft => $draft, |
1066 |
draft => { |
|
|
1067 |
subject => $letter->{title}, |
1068 |
body => $letter->{content} |
1069 |
}, |
1086 |
partners => $partners, |
1070 |
partners => $partners, |
1087 |
} |
1071 |
} |
1088 |
}; |
1072 |
}; |
Lines 1098-1154
EOF
Link Here
|
1098 |
"No target email addresses found. Either select at least one partner or check your ILL partner library records.") |
1082 |
"No target email addresses found. Either select at least one partner or check your ILL partner library records.") |
1099 |
if ( !$to ); |
1083 |
if ( !$to ); |
1100 |
# Create the from, replyto and sender headers |
1084 |
# Create the from, replyto and sender headers |
1101 |
my $from = $branch->branchemail; |
1085 |
my $from = $branch->branchillemail || $branch->branchemail; |
1102 |
my $replyto = $branch->branchreplyto || $from; |
1086 |
my $replyto = $branch->branchreplyto || $from; |
1103 |
Koha::Exceptions::Ill::NoLibraryEmail->throw( |
1087 |
Koha::Exceptions::Ill::NoLibraryEmail->throw( |
1104 |
"Your library has no usable email address. Please set it.") |
1088 |
"Your library has no usable email address. Please set it.") |
1105 |
if ( !$from ); |
1089 |
if ( !$from ); |
1106 |
|
1090 |
|
1107 |
# Create the email |
1091 |
# So we get a notice hashref, then substitute the possibly |
1108 |
my $message = Koha::Email->new; |
1092 |
# modified title and body from the draft stage |
1109 |
my %mail = $message->create_message_headers( |
1093 |
my $letter = $self->get_notice({ |
1110 |
{ |
1094 |
notice_code => 'ILL_PARTNER_REQ', |
1111 |
to => $to, |
1095 |
transport => 'email' |
1112 |
from => $from, |
1096 |
}); |
1113 |
replyto => $replyto, |
1097 |
$letter->{title} = $params->{subject}; |
1114 |
subject => Encode::encode( "utf8", $params->{subject} ), |
1098 |
$letter->{content} = $params->{body}; |
1115 |
message => Encode::encode( "utf8", $params->{body} ), |
1099 |
|
1116 |
contenttype => 'text/plain', |
1100 |
# Send the email |
|
|
1101 |
my $params = { |
1102 |
letter => $letter, |
1103 |
borrowernumber => $self->borrowernumber, |
1104 |
message_transport_type => 'email', |
1105 |
to_address => $to, |
1106 |
from_address => $from |
1107 |
}; |
1108 |
|
1109 |
if ($letter) { |
1110 |
my $result = C4::Letters::EnqueueLetter($params); |
1111 |
if ( $result ) { |
1112 |
$self->status("GENREQ")->store; |
1113 |
$self->_backend_capability( |
1114 |
'set_requested_partners', |
1115 |
{ |
1116 |
request => $self, |
1117 |
to => $to |
1118 |
} |
1119 |
); |
1120 |
return { |
1121 |
error => 0, |
1122 |
status => '', |
1123 |
message => '', |
1124 |
method => 'generic_confirm', |
1125 |
stage => 'commit', |
1126 |
next => 'illview', |
1127 |
}; |
1117 |
} |
1128 |
} |
1118 |
); |
|
|
1119 |
# Send it |
1120 |
my $result = sendmail(%mail); |
1121 |
if ( $result ) { |
1122 |
$self->status("GENREQ")->store; |
1123 |
$self->_backend_capability( |
1124 |
'set_requested_partners', |
1125 |
{ |
1126 |
request => $self, |
1127 |
to => $to |
1128 |
} |
1129 |
); |
1130 |
return { |
1131 |
error => 0, |
1132 |
status => '', |
1133 |
message => '', |
1134 |
method => 'generic_confirm', |
1135 |
stage => 'commit', |
1136 |
next => 'illview', |
1137 |
}; |
1138 |
} else { |
1139 |
return { |
1140 |
error => 1, |
1141 |
status => 'email_failed', |
1142 |
message => $Mail::Sendmail::error, |
1143 |
method => 'generic_confirm', |
1144 |
stage => 'draft', |
1145 |
}; |
1146 |
} |
1129 |
} |
|
|
1130 |
return { |
1131 |
error => 1, |
1132 |
status => 'email_failed', |
1133 |
message => 'Email queueing failed', |
1134 |
method => 'generic_confirm', |
1135 |
stage => 'draft', |
1136 |
}; |
1147 |
} else { |
1137 |
} else { |
1148 |
die "Unknown stage, should not have happened." |
1138 |
die "Unknown stage, should not have happened." |
1149 |
} |
1139 |
} |
1150 |
} |
1140 |
} |
1151 |
|
1141 |
|
|
|
1142 |
=head3 get_staff_to_address |
1143 |
|
1144 |
my $email = $request->get_staff_to_address(); |
1145 |
|
1146 |
Get the email address to which staff notices should be sent |
1147 |
|
1148 |
=cut |
1149 |
|
1150 |
sub get_staff_to_address { |
1151 |
my ( $self ) = @_; |
1152 |
|
1153 |
# The various places we can get an ILL staff email address from |
1154 |
# (In order of preference) |
1155 |
# |
1156 |
# Dedicated branch address |
1157 |
my $library = Koha::Libraries->find( $self->branchcode ); |
1158 |
my $branch_ill_to = $library->branchillemail; |
1159 |
# General purpose ILL address from syspref |
1160 |
my $syspref = C4::Context->preference("ILLDefaultStaffEmail"); |
1161 |
# Branch general email address |
1162 |
my $branch_to = $library->branchemail; |
1163 |
# Last resort |
1164 |
my $koha_admin = C4::Context->preference('KohaAdminEmailAddress'); |
1165 |
|
1166 |
my $to; |
1167 |
if ($branch_ill_to) { |
1168 |
$to = $branch_ill_to; |
1169 |
} elsif ($syspref) { |
1170 |
$to = $syspref; |
1171 |
} elsif ($branch_to) { |
1172 |
$to = $branch_to; |
1173 |
} elsif ($koha_admin) { |
1174 |
$to = $koha_admin; |
1175 |
} |
1176 |
|
1177 |
# $to will not be defined if we didn't find a usable address |
1178 |
return $to; |
1179 |
} |
1180 |
|
1181 |
=head3 send_patron_notice |
1182 |
|
1183 |
my $result = $request->send_patron_notice($notice_code); |
1184 |
|
1185 |
Send a specified notice regarding this request to a patron |
1186 |
|
1187 |
=cut |
1188 |
|
1189 |
sub send_patron_notice { |
1190 |
my ( $self, $notice_code ) = @_; |
1191 |
|
1192 |
# We need a notice code |
1193 |
if (!$notice_code) { |
1194 |
return { |
1195 |
error => 'notice_no_type' |
1196 |
}; |
1197 |
} |
1198 |
|
1199 |
# Map from the notice code to the messaging preference |
1200 |
my %message_name = ( |
1201 |
ILL_PICKUP_READY => 'Ill_ready', |
1202 |
ILL_REQUEST_UNAVAIL => 'Ill_unavailable' |
1203 |
); |
1204 |
|
1205 |
# Get the patron's messaging preferences |
1206 |
my $borrower_preferences = C4::Members::Messaging::GetMessagingPreferences({ |
1207 |
borrowernumber => $self->borrowernumber, |
1208 |
message_name => $message_name{$notice_code} |
1209 |
}); |
1210 |
my @transports = keys %{ $borrower_preferences->{transports} }; |
1211 |
|
1212 |
# Send the notice to the patron via the chosen transport methods |
1213 |
# and record the results |
1214 |
my @success = (); |
1215 |
my @fail = (); |
1216 |
for my $transport (@transports) { |
1217 |
my $letter = $self->get_notice({ |
1218 |
notice_code => $notice_code, |
1219 |
transport => $transport |
1220 |
}); |
1221 |
if ($letter) { |
1222 |
my $result = C4::Letters::EnqueueLetter({ |
1223 |
letter => $letter, |
1224 |
borrowernumber => $self->borrowernumber, |
1225 |
message_transport_type => $transport, |
1226 |
}); |
1227 |
if ($result) { |
1228 |
push @success, $transport; |
1229 |
} else { |
1230 |
push @fail, $transport; |
1231 |
} |
1232 |
} else { |
1233 |
push @fail, $transport; |
1234 |
} |
1235 |
} |
1236 |
if (scalar @success > 0) { |
1237 |
my $logger = Koha::Illrequest::Logger->new; |
1238 |
$logger->log_patron_notice({ |
1239 |
request => $self, |
1240 |
notice_code => $notice_code |
1241 |
}); |
1242 |
} |
1243 |
return { |
1244 |
result => { |
1245 |
success => \@success, |
1246 |
fail => \@fail |
1247 |
} |
1248 |
}; |
1249 |
} |
1250 |
|
1251 |
=head3 send_staff_notice |
1252 |
|
1253 |
my $result = $request->send_staff_notice($notice_code); |
1254 |
|
1255 |
Send a specified notice regarding this request to staff |
1256 |
|
1257 |
=cut |
1258 |
|
1259 |
sub send_staff_notice { |
1260 |
my ( $self, $notice_code ) = @_; |
1261 |
|
1262 |
# We need a notice code |
1263 |
if (!$notice_code) { |
1264 |
return { |
1265 |
error => 'notice_no_type' |
1266 |
}; |
1267 |
} |
1268 |
|
1269 |
# Get the staff notices that have been assigned for sending in |
1270 |
# the syspref |
1271 |
my $staff_to_send = C4::Context->preference('ILLSendStaffNotices'); |
1272 |
|
1273 |
# If it hasn't been enabled in the syspref, we don't want to send it |
1274 |
if ($staff_to_send !~ /\b$notice_code\b/) { |
1275 |
return { |
1276 |
error => 'notice_not_enabled' |
1277 |
}; |
1278 |
} |
1279 |
|
1280 |
my $letter = $self->get_notice({ |
1281 |
notice_code => $notice_code, |
1282 |
transport => 'email' |
1283 |
}); |
1284 |
|
1285 |
# Try and get an address to which to send staff notices |
1286 |
my $to_address = scalar $self->get_staff_to_address; |
1287 |
|
1288 |
my $params = { |
1289 |
letter => $letter, |
1290 |
borrowernumber => $self->borrowernumber, |
1291 |
message_transport_type => 'email', |
1292 |
}; |
1293 |
|
1294 |
if ($to_address) { |
1295 |
$params->{to_address} = $to_address; |
1296 |
$params->{from_address} = $to_address; |
1297 |
} else { |
1298 |
return { |
1299 |
error => 'notice_no_create' |
1300 |
}; |
1301 |
} |
1302 |
|
1303 |
if ($letter) { |
1304 |
C4::Letters::EnqueueLetter($params) |
1305 |
or warn "can't enqueue letter $letter"; |
1306 |
return { |
1307 |
success => 'notice_queued' |
1308 |
}; |
1309 |
} else { |
1310 |
return { |
1311 |
error => 'notice_no_create' |
1312 |
}; |
1313 |
} |
1314 |
} |
1315 |
|
1316 |
=head3 get_notice |
1317 |
|
1318 |
my $notice = $request->get_notice($params); |
1319 |
|
1320 |
Return a compiled notice hashref for the passed notice code |
1321 |
and transport type |
1322 |
|
1323 |
=cut |
1324 |
|
1325 |
sub get_notice { |
1326 |
my ( $self, $params ) = @_; |
1327 |
|
1328 |
my $title = $self->illrequestattributes->find( |
1329 |
{ type => 'title' } |
1330 |
); |
1331 |
my $author = $self->illrequestattributes->find( |
1332 |
{ type => 'author' } |
1333 |
); |
1334 |
my $metahash = $self->metadata; |
1335 |
my @metaarray = (); |
1336 |
while (my($key, $value) = each %{$metahash}) { |
1337 |
push @metaarray, "- $key: $value" if $value; |
1338 |
} |
1339 |
my $metastring = join("\n", @metaarray); |
1340 |
my $letter = C4::Letters::GetPreparedLetter( |
1341 |
module => 'ill', |
1342 |
letter_code => $params->{notice_code}, |
1343 |
message_transport_type => $params->{transport}, |
1344 |
lang => $self->patron->lang, |
1345 |
tables => { |
1346 |
illrequests => $self->illrequest_id, |
1347 |
borrowers => $self->borrowernumber, |
1348 |
biblio => $self->biblio_id, |
1349 |
branches => $self->branchcode, |
1350 |
}, |
1351 |
substitute => { |
1352 |
ill_bib_title => $title ? $title->value : 'N/A', |
1353 |
ill_bib_author => $author ? $author->value : 'N/A', |
1354 |
ill_full_metadata => $metastring |
1355 |
} |
1356 |
); |
1357 |
|
1358 |
return $letter; |
1359 |
} |
1360 |
|
1152 |
=head3 id_prefix |
1361 |
=head3 id_prefix |
1153 |
|
1362 |
|
1154 |
my $prefix = $record->id_prefix; |
1363 |
my $prefix = $record->id_prefix; |