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