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