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::DateUtils qw/ dt_from_string /; |
32 |
use Koha::DateUtils qw/ dt_from_string /; |
31 |
use Koha::Email; |
33 |
use Koha::Email; |
Lines 248-253
sub status_alias {
Link Here
|
248 |
|
250 |
|
249 |
Overloaded getter/setter for request status, |
251 |
Overloaded getter/setter for request status, |
250 |
also nullifies status_alias and records the fact that the status has changed |
252 |
also nullifies status_alias and records the fact that the status has changed |
|
|
253 |
and sends a notice if appropriate |
251 |
|
254 |
|
252 |
=cut |
255 |
=cut |
253 |
|
256 |
|
Lines 281-286
sub status {
Link Here
|
281 |
}); |
284 |
}); |
282 |
} |
285 |
} |
283 |
delete $self->{previous_status}; |
286 |
delete $self->{previous_status}; |
|
|
287 |
# If status has changed to cancellation requested, send a notice |
288 |
if ($new_status eq 'CANCREQ') { |
289 |
$self->send_staff_notice('ILL_REQUEST_CANCEL'); |
290 |
} |
284 |
return $ret; |
291 |
return $ret; |
285 |
} else { |
292 |
} else { |
286 |
return $current_status; |
293 |
return $current_status; |
Lines 1287-1324
sub generic_confirm {
Link Here
|
1287 |
my $branch = Koha::Libraries->find($params->{current_branchcode}) |
1294 |
my $branch = Koha::Libraries->find($params->{current_branchcode}) |
1288 |
|| die "Invalid current branchcode. Are you logged in as the database user?"; |
1295 |
|| die "Invalid current branchcode. Are you logged in as the database user?"; |
1289 |
if ( !$params->{stage}|| $params->{stage} eq 'init' ) { |
1296 |
if ( !$params->{stage}|| $params->{stage} eq 'init' ) { |
1290 |
my $draft->{subject} = "ILL Request"; |
|
|
1291 |
$draft->{body} = <<EOF; |
1292 |
Dear Sir/Madam, |
1293 |
|
1294 |
We would like to request an interlibrary loan for a title matching the |
1295 |
following description: |
1296 |
|
1297 |
EOF |
1298 |
|
1299 |
my $details = $self->metadata; |
1300 |
while (my ($title, $value) = each %{$details}) { |
1301 |
$draft->{body} .= " - " . $title . ": " . $value . "\n" |
1302 |
if $value; |
1303 |
} |
1304 |
$draft->{body} .= <<EOF; |
1305 |
|
1306 |
Please let us know if you are able to supply this to us. |
1307 |
|
1308 |
Kind Regards |
1309 |
|
1310 |
EOF |
1311 |
|
1297 |
|
1312 |
my @address = map { $branch->$_ } |
1298 |
# Get the message body from the notice definition |
1313 |
qw/ branchname branchaddress1 branchaddress2 branchaddress3 |
1299 |
my $letter = $self->get_notice({ |
1314 |
branchzip branchcity branchstate branchcountry branchphone |
1300 |
notice_code => 'ILL_PARTNER_REQ', |
1315 |
branchillemail branchemail /; |
1301 |
transport => 'email' |
1316 |
my $address = ""; |
1302 |
}); |
1317 |
foreach my $line ( @address ) { |
|
|
1318 |
$address .= $line . "\n" if $line; |
1319 |
} |
1320 |
|
1321 |
$draft->{body} .= $address; |
1322 |
|
1303 |
|
1323 |
my $partners = Koha::Patrons->search({ |
1304 |
my $partners = Koha::Patrons->search({ |
1324 |
categorycode => $self->_config->partner_code |
1305 |
categorycode => $self->_config->partner_code |
Lines 1330-1336
EOF
Link Here
|
1330 |
method => 'generic_confirm', |
1311 |
method => 'generic_confirm', |
1331 |
stage => 'draft', |
1312 |
stage => 'draft', |
1332 |
value => { |
1313 |
value => { |
1333 |
draft => $draft, |
1314 |
draft => { |
|
|
1315 |
subject => $letter->{title}, |
1316 |
body => $letter->{content} |
1317 |
}, |
1334 |
partners => $partners, |
1318 |
partners => $partners, |
1335 |
} |
1319 |
} |
1336 |
}; |
1320 |
}; |
Lines 1346-1402
EOF
Link Here
|
1346 |
"No target email addresses found. Either select at least one partner or check your ILL partner library records.") |
1330 |
"No target email addresses found. Either select at least one partner or check your ILL partner library records.") |
1347 |
if ( !$to ); |
1331 |
if ( !$to ); |
1348 |
# Create the from, replyto and sender headers |
1332 |
# Create the from, replyto and sender headers |
1349 |
my $from = $branch->branchemail; |
1333 |
my $from = $branch->branchillemail || $branch->branchemail; |
1350 |
my $replyto = $branch->branchreplyto || $from; |
1334 |
my $replyto = $branch->branchreplyto || $from; |
1351 |
Koha::Exceptions::Ill::NoLibraryEmail->throw( |
1335 |
Koha::Exceptions::Ill::NoLibraryEmail->throw( |
1352 |
"Your library has no usable email address. Please set it.") |
1336 |
"Your library has no usable email address. Please set it.") |
1353 |
if ( !$from ); |
1337 |
if ( !$from ); |
1354 |
|
1338 |
|
1355 |
# Create the email |
1339 |
# So we get a notice hashref, then substitute the possibly |
1356 |
my $message = Koha::Email->new; |
1340 |
# modified title and body from the draft stage |
1357 |
my %mail = $message->create_message_headers( |
1341 |
my $letter = $self->get_notice({ |
1358 |
{ |
1342 |
notice_code => 'ILL_PARTNER_REQ', |
1359 |
to => $to, |
1343 |
transport => 'email' |
1360 |
from => $from, |
1344 |
}); |
1361 |
replyto => $replyto, |
1345 |
$letter->{title} = $params->{subject}; |
1362 |
subject => Encode::encode( "utf8", $params->{subject} ), |
1346 |
$letter->{content} = $params->{body}; |
1363 |
message => Encode::encode( "utf8", $params->{body} ), |
1347 |
|
1364 |
contenttype => 'text/plain', |
1348 |
# Send the email |
|
|
1349 |
my $params = { |
1350 |
letter => $letter, |
1351 |
borrowernumber => $self->borrowernumber, |
1352 |
message_transport_type => 'email', |
1353 |
to_address => $to, |
1354 |
from_address => $from |
1355 |
}; |
1356 |
|
1357 |
if ($letter) { |
1358 |
my $result = C4::Letters::EnqueueLetter($params); |
1359 |
if ( $result ) { |
1360 |
$self->status("GENREQ")->store; |
1361 |
$self->_backend_capability( |
1362 |
'set_requested_partners', |
1363 |
{ |
1364 |
request => $self, |
1365 |
to => $to |
1366 |
} |
1367 |
); |
1368 |
return { |
1369 |
error => 0, |
1370 |
status => '', |
1371 |
message => '', |
1372 |
method => 'generic_confirm', |
1373 |
stage => 'commit', |
1374 |
next => 'illview', |
1375 |
}; |
1365 |
} |
1376 |
} |
1366 |
); |
|
|
1367 |
# Send it |
1368 |
my $result = sendmail(%mail); |
1369 |
if ( $result ) { |
1370 |
$self->status("GENREQ")->store; |
1371 |
$self->_backend_capability( |
1372 |
'set_requested_partners', |
1373 |
{ |
1374 |
request => $self, |
1375 |
to => $to |
1376 |
} |
1377 |
); |
1378 |
return { |
1379 |
error => 0, |
1380 |
status => '', |
1381 |
message => '', |
1382 |
method => 'generic_confirm', |
1383 |
stage => 'commit', |
1384 |
next => 'illview', |
1385 |
}; |
1386 |
} else { |
1387 |
return { |
1388 |
error => 1, |
1389 |
status => 'email_failed', |
1390 |
message => $Mail::Sendmail::error, |
1391 |
method => 'generic_confirm', |
1392 |
stage => 'draft', |
1393 |
}; |
1394 |
} |
1377 |
} |
|
|
1378 |
return { |
1379 |
error => 1, |
1380 |
status => 'email_failed', |
1381 |
message => 'Email queueing failed', |
1382 |
method => 'generic_confirm', |
1383 |
stage => 'draft', |
1384 |
}; |
1395 |
} else { |
1385 |
} else { |
1396 |
die "Unknown stage, should not have happened." |
1386 |
die "Unknown stage, should not have happened." |
1397 |
} |
1387 |
} |
1398 |
} |
1388 |
} |
1399 |
|
1389 |
|
|
|
1390 |
=head3 get_staff_to_address |
1391 |
|
1392 |
my $email = $request->get_staff_to_address(); |
1393 |
|
1394 |
Get the email address to which staff notices should be sent |
1395 |
|
1396 |
=cut |
1397 |
|
1398 |
sub get_staff_to_address { |
1399 |
my ( $self ) = @_; |
1400 |
|
1401 |
# The various places we can get an ILL staff email address from |
1402 |
# (In order of preference) |
1403 |
# |
1404 |
# Dedicated branch address |
1405 |
my $library = Koha::Libraries->find( $self->branchcode ); |
1406 |
my $branch_ill_to = $library->branchillemail; |
1407 |
# General purpose ILL address from syspref |
1408 |
my $syspref = C4::Context->preference("ILLDefaultStaffEmail"); |
1409 |
# Branch general email address |
1410 |
my $branch_to = $library->branchemail; |
1411 |
# Last resort |
1412 |
my $koha_admin = C4::Context->preference('KohaAdminEmailAddress'); |
1413 |
|
1414 |
my $to; |
1415 |
if ($branch_ill_to) { |
1416 |
$to = $branch_ill_to; |
1417 |
} elsif ($syspref) { |
1418 |
$to = $syspref; |
1419 |
} elsif ($branch_to) { |
1420 |
$to = $branch_to; |
1421 |
} elsif ($koha_admin) { |
1422 |
$to = $koha_admin; |
1423 |
} |
1424 |
|
1425 |
# $to will not be defined if we didn't find a usable address |
1426 |
return $to; |
1427 |
} |
1428 |
|
1429 |
=head3 send_patron_notice |
1430 |
|
1431 |
my $result = $request->send_patron_notice($notice_code); |
1432 |
|
1433 |
Send a specified notice regarding this request to a patron |
1434 |
|
1435 |
=cut |
1436 |
|
1437 |
sub send_patron_notice { |
1438 |
my ( $self, $notice_code ) = @_; |
1439 |
|
1440 |
# We need a notice code |
1441 |
if (!$notice_code) { |
1442 |
return { |
1443 |
error => 'notice_no_type' |
1444 |
}; |
1445 |
} |
1446 |
|
1447 |
# Map from the notice code to the messaging preference |
1448 |
my %message_name = ( |
1449 |
ILL_PICKUP_READY => 'Ill_ready', |
1450 |
ILL_REQUEST_UNAVAIL => 'Ill_unavailable' |
1451 |
); |
1452 |
|
1453 |
# Get the patron's messaging preferences |
1454 |
my $borrower_preferences = C4::Members::Messaging::GetMessagingPreferences({ |
1455 |
borrowernumber => $self->borrowernumber, |
1456 |
message_name => $message_name{$notice_code} |
1457 |
}); |
1458 |
my @transports = keys %{ $borrower_preferences->{transports} }; |
1459 |
|
1460 |
# Send the notice to the patron via the chosen transport methods |
1461 |
# and record the results |
1462 |
my @success = (); |
1463 |
my @fail = (); |
1464 |
for my $transport (@transports) { |
1465 |
my $letter = $self->get_notice({ |
1466 |
notice_code => $notice_code, |
1467 |
transport => $transport |
1468 |
}); |
1469 |
if ($letter) { |
1470 |
my $result = C4::Letters::EnqueueLetter({ |
1471 |
letter => $letter, |
1472 |
borrowernumber => $self->borrowernumber, |
1473 |
message_transport_type => $transport, |
1474 |
}); |
1475 |
if ($result) { |
1476 |
push @success, $transport; |
1477 |
} else { |
1478 |
push @fail, $transport; |
1479 |
} |
1480 |
} else { |
1481 |
push @fail, $transport; |
1482 |
} |
1483 |
} |
1484 |
if (scalar @success > 0) { |
1485 |
my $logger = Koha::Illrequest::Logger->new; |
1486 |
$logger->log_patron_notice({ |
1487 |
request => $self, |
1488 |
notice_code => $notice_code |
1489 |
}); |
1490 |
} |
1491 |
return { |
1492 |
result => { |
1493 |
success => \@success, |
1494 |
fail => \@fail |
1495 |
} |
1496 |
}; |
1497 |
} |
1498 |
|
1499 |
=head3 send_staff_notice |
1500 |
|
1501 |
my $result = $request->send_staff_notice($notice_code); |
1502 |
|
1503 |
Send a specified notice regarding this request to staff |
1504 |
|
1505 |
=cut |
1506 |
|
1507 |
sub send_staff_notice { |
1508 |
my ( $self, $notice_code ) = @_; |
1509 |
|
1510 |
# We need a notice code |
1511 |
if (!$notice_code) { |
1512 |
return { |
1513 |
error => 'notice_no_type' |
1514 |
}; |
1515 |
} |
1516 |
|
1517 |
# Get the staff notices that have been assigned for sending in |
1518 |
# the syspref |
1519 |
my $staff_to_send = C4::Context->preference('ILLSendStaffNotices'); |
1520 |
|
1521 |
# If it hasn't been enabled in the syspref, we don't want to send it |
1522 |
if ($staff_to_send !~ /\b$notice_code\b/) { |
1523 |
return { |
1524 |
error => 'notice_not_enabled' |
1525 |
}; |
1526 |
} |
1527 |
|
1528 |
my $letter = $self->get_notice({ |
1529 |
notice_code => $notice_code, |
1530 |
transport => 'email' |
1531 |
}); |
1532 |
|
1533 |
# Try and get an address to which to send staff notices |
1534 |
my $to_address = scalar $self->get_staff_to_address; |
1535 |
|
1536 |
my $params = { |
1537 |
letter => $letter, |
1538 |
borrowernumber => $self->borrowernumber, |
1539 |
message_transport_type => 'email', |
1540 |
}; |
1541 |
|
1542 |
if ($to_address) { |
1543 |
$params->{to_address} = $to_address; |
1544 |
$params->{from_address} = $to_address; |
1545 |
} else { |
1546 |
return { |
1547 |
error => 'notice_no_create' |
1548 |
}; |
1549 |
} |
1550 |
|
1551 |
if ($letter) { |
1552 |
C4::Letters::EnqueueLetter($params) |
1553 |
or warn "can't enqueue letter $letter"; |
1554 |
return { |
1555 |
success => 'notice_queued' |
1556 |
}; |
1557 |
} else { |
1558 |
return { |
1559 |
error => 'notice_no_create' |
1560 |
}; |
1561 |
} |
1562 |
} |
1563 |
|
1564 |
=head3 get_notice |
1565 |
|
1566 |
my $notice = $request->get_notice($params); |
1567 |
|
1568 |
Return a compiled notice hashref for the passed notice code |
1569 |
and transport type |
1570 |
|
1571 |
=cut |
1572 |
|
1573 |
sub get_notice { |
1574 |
my ( $self, $params ) = @_; |
1575 |
|
1576 |
my $title = $self->illrequestattributes->find( |
1577 |
{ type => 'title' } |
1578 |
); |
1579 |
my $author = $self->illrequestattributes->find( |
1580 |
{ type => 'author' } |
1581 |
); |
1582 |
my $metahash = $self->metadata; |
1583 |
my @metaarray = (); |
1584 |
while (my($key, $value) = each %{$metahash}) { |
1585 |
push @metaarray, "- $key: $value" if $value; |
1586 |
} |
1587 |
my $metastring = join("\n", @metaarray); |
1588 |
my $letter = C4::Letters::GetPreparedLetter( |
1589 |
module => 'ill', |
1590 |
letter_code => $params->{notice_code}, |
1591 |
message_transport_type => $params->{transport}, |
1592 |
lang => $self->patron->lang, |
1593 |
tables => { |
1594 |
illrequests => $self->illrequest_id, |
1595 |
borrowers => $self->borrowernumber, |
1596 |
biblio => $self->biblio_id, |
1597 |
branches => $self->branchcode, |
1598 |
}, |
1599 |
substitute => { |
1600 |
ill_bib_title => $title ? $title->value : 'N/A', |
1601 |
ill_bib_author => $author ? $author->value : 'N/A', |
1602 |
ill_full_metadata => $metastring |
1603 |
} |
1604 |
); |
1605 |
|
1606 |
return $letter; |
1607 |
} |
1608 |
|
1400 |
=head3 id_prefix |
1609 |
=head3 id_prefix |
1401 |
|
1610 |
|
1402 |
my $prefix = $record->id_prefix; |
1611 |
my $prefix = $record->id_prefix; |