Koha/tools/letter.pl
Aleisha Amohia db095c3a7b Bug 19532: Recalls on intranet
See recalls on Intranet
- old recalls (all inactive recalls)
- recalls queue (all active recalls) - cancel, expire, revert waiting status, multiple cancel, mark overdue
- recalls to pull (available but not yet waiting) - cancel
- recalls awaiting pickup (awaiting pickup, awaiting pickup more than RecallMaxPickUpDelay days) - expire, revert waiting status
- overdue recalls (overdue to be returned) - cancel, multiple cancel
- biblio recalls tab (all active recalls relevant to this bib) - cancel, expire, revert waiting status, mark overdue
- patron recalls tab (all active recalls relevant to this patron) - cancel, expire, revert waiting status, mark overdue
- patron recalls history tab (all recalls relevant to this patron) - cancel, expire, revert waiting status, mark overdue
- log viewer

and the general circulation of recalls

== TEST PLAN FOR RECALLS ==

ADMINISTRATION

1. Apply all patches
2. Run database updates, update schema files and confirm everything applies cleanly
3. Run tests and confirm everything passes:
t/db_dependent/Koha/Recall.t
t/db_dependent/Koha/Recalls.t
t/db_dependent/Stats.t
t/db_dependent/Circulation/CalcFine.t
t/db_dependent/Koha/Item.t
t/db_dependent/Koha/Biblio.t
t/db_dependent/Koha/Patron.t
t/db_dependent/XSLT.t
t/db_dependent/Search.t
t/db_dependent/Holds.t
t/db_dependent/Circulation/transferbook.t
t/db_dependent/Circulation.t

4. Go to Administration -> system preferences. Find the UseRecalls system preference. It should be DISABLED. Confirm RecallsMaxPickUpDelay is set to 7 by default.
5. Go to Administration -> circulation rules. Confirm there are no recalls circulation rules showing.
6. Test a few circulation flows: checking out, placing a reserve, checking in, fulfilling a reserve, etc. Confirm everything works as normal.

7. Go to Administration -> system preferences. Enable the UseRecalls system preference.
8. Go to Administration -> circulation rules. Set the following rules:
Recalls allowed (count) = 0
Recalls per record (count) = 0
On shelf recalls allowed ( If any unavailable / If all unavailable ) = If any unavailable
Recall due date interval (days) = 3
Recall overdue fine amount = (something different to your normal fine amount)
Recall pickup period (days) = 1

Throughout your testing, try with different combinations of these rules and itemtype / branchcode / categorycode. Also try with null values. Keep the circulation rules open in another tab so you can refer to and update these easily. You should also have at least one other tab open for the staff client, and a third tab open for the OPAC, for ease of testing.

9. Go to your account -> More -> Set permissions. Confirm the recalls permission is checked.
10. Set up a test user with OPAC login details (Borrower A). This could also be your own user, as long as you have OPAC login access.
11. Set up a test record (Biblio A) with at least two items (Item A and Item B) of the same item type (or an item type with the same recall circ rules).

PLACING A RECALL

12. Log in to the OPAC as Borrower A. Do a catalogue search with a term that will return multiple results, including Biblio A.
13. Click on Biblio A.
14. Notice there is a 'Place recall' button on the sidebar menu. Click this button. There will be a message saying that there are no items to recall - this is because all items are available.

15. Check out Item A to another borrower (Borrower B).
16. Refresh the 'Place recall' page. You will still NOT be able to place a recall - this is because Recalls allowed = 0 and Recalls per record = 0.

17. Edit the circulation rules to have the following values:
Recalls allowed (count) = 1
Recalls per record (count) = 1
18. Refresh the 'Place recall' page. You will now see the form to place a recall.

BIBLIO-LEVEL RECALL, NO TRANSFER

19. Place a biblio-level recall.
Pickup location: Branch A, the set branch when you are logged into the staff client
Recall not needed after (expiration date): whatever you want
Select 'recall next available item'
Click confirm
20. Confirm the recall is placed successfully. Confirm that the new due date displayed is correctly calculated to be today's date, plus 3 days (taken from the 'recall due date interval' circ rule)
21. In the staff client, look at Borrower B's account, and go to their Notices tab. Confirm they have received a 'Notification to return recalled item' notice.
22. Look at Borrower B's checkouts table. Notice the due date for their checkout has been adjusted, and there is now a note to say that the item was recalled and the due date adjusted.
23. Log in to the OPAC as Borrower B and go to your summary tab. Notice there is a note under their checkout to say the item had been recalled.

24. Log out of the OPAC and log back in as Borrower A.
25. Go to your summary tab. Confirm there is a Recalls tab with a count of 1.
26. Cancel the recall using the button. Confirm it cancels and the Recalls tab disappears.

27. Do a catalogue search with a term that will return multiple results, including Biblio A.
28. When the results load, notice there is a 'Place recall' button next to the 'Place hold' button. Click this 'Place recall' button.
29. Notice you are redirected straight to the form to place a recall.
30. Place a biblio-level recall again, following the steps in Step 19.

31. Go to your recalls history tab. Notice your first cancelled recall shows here.
32. Cancel the recall you just created, using the button. Confirm it cancels and you are redirected to your summary tab.

33. In the staff client, enable the UseCourseReserves system preference.
34. Go to the main menu, click Course Reserves.
35. Add a new course. (You may also have to define an authorised value for DEPARTMENT.)
36. Add Item A as a reserve to this course.

37. View Course Reserves in the OPAC. Click the course you just created.
38. Notice the reserve has a Recall button underneath it's 'Checked out' status. Click this button.
39. Place a biblio-level recall again, following the steps in Step 19.

40. Click the 'Place recall' link in the breadcrumbs.
41. Notice there is a message saying that you have reached the max number of recalls on this record. This is because Recalls allowed = 1 and Recalls per record = 1.
42. Edit the circulation rules to have the following values:
Recalls allowed (count) = 10
Recalls per record (count) = 5
43. Refresh the 'Place recall' page. You will now see the form to place a recall.

44. Create another test record (Biblio B) with at least one item (Item C).
45. Find this record on the OPAC and place a biblio-level recall again, following the steps in Step 19.

46. In the staff client, go to Circulation -> Old recalls. You should be able to see your two cancelled recalls.
47. Go to Circulation -> Recalls queue. Your current recalls should show here.
48. Use the 'Select all' checkbox to select all recalls.
49. Cancel the recalls using the 'Cancel selected recalls' button.

50. Go to the OPAC and place a biblio-level recall on Biblio A again, following the steps in Step 19.

51. In the staff client, check in Item A, which should still be checked out to Borrower B.
52. A box should pop-up asking you to confirm Borrower A's recall. Click ignore.
53. Click the link to go view Biblio A's details in the catalogue.
54. Click the recalls tab. Notice Borrower A's recall is displayed, and shows it is still Requested (has not been confirmed waiting).

55. Check in Item A again. This time, confirm the recall as waiting using the "Confirm recall" button.
56. Go to Borrower A's Notices tab. Confirm there is a notice "Recalled item awaiting pickup".
57. Go to Borrower A's checkouts. Notice there is a recalls tab. Confirm the recall is showing as "Ready for pickup".
58. Click the 'Actions' dropdown. Click the "Revert waiting" button. The page should show a message that the waiting status has been reverted, without reloading.

59. This time, check in Item B. The recall confirmation box should show again, because this a biblio-level recall that any recallable item under Biblio A can fill. Click the "Print slip and confirm" button.
60. Check the slip that is generated. Confirm it contains Borrower A's correct details, and the details of the recall are correct.
61. Go to Circulation -> Recalls awaiting pickup. Confirm the recall is now waiting and shows in this list.
(You could also try this with Item B having a different item type to Item A, and circ rules not allowing Item B's item type to have recalls. When checking in Item A, it should not trigger the recall box).

62. Go to Borrower A's checkouts. Check out Item B.
63. Confirm the checkout is successful and the recall is removed from the Recalls tab.
64. Go to Circulation -> Old recalls. The fulfilled recall should show.

65. Check in Item B.

BIBLIO-LEVEL RECALL, TRANSFER REQUIRED

66. Check out Item A to Borrower B.
67. Log in to the OPAC as Borrower A.
68. Find Biblio A and place a biblio-level recall.
Pickup location: Branch B, a different branch from your logged in branch. This recall will require a transfer.
Recall not needed after (expiration date): whatever you want
Select 'recall next available item'
Click confirm

69. In the staff client, check in Item A at Branch A. Notice the box that pops up shows that a transfer is required.
70. Click "confirm recall and transfer" and confirm the transfer.
71. Go to your account and click the Recalls tab.
72. Confirm the recall status now shows the item is in transit to Branch B.
73. In the drop-down top-right of your window, select 'Set library'.
74. Set your library to Branch B.

75. Go to Circulation -> Transfers to receive. Notice that the recall is showing here.
76. Click 'Cancel transfer'.
77. Go to Circulation -> Recalls queue
78. Confirm the recall status has been reverted to Requested.

79. Set your library back to Branch A.
80. Check in Item A and trigger the transfer.
81. Set your library back to Branch B.

82. Check in Item A at Branch B.
83. When the 'Recall found' box pops up, click Ignore.
84. Go to Circulation -> Recalls to pull. The recall should show here, with a button to "Cancel recall and return to: Branch A"
85. Click the button to cancel the recall.

86. Repeat Steps 66-70.
87. Check in Item A at Branch B. Confirm the recall as waiting.
88. Check out Item A to Borrower A to fulfill the recall.

89. Set your library back to Branch A and check in Item A.

ITEM-LEVEL RECALL, NO TRANSFER

90. Go to Administration -> circulation rules. Set the following rules:
On shelf recalls allowed ( If any unavailable / If all unavailable ) = If all unavailable

91. Check out Item A to Borrower B.
92. Log in to the OPAC as Borrower A and go to Biblio A.
93. Click the 'Place recall' button. Confirm there is a message that there are no items to recall. This is because On shelf recalls allowed = If all unavailable, and there is still one item (Item B) available.
94. In the staff client, edit Item B to have a withdrawn, item lost or not for loan status.
95. Refresh the 'Place recall' page. Confirm you can now see the form to place a recall.

96. Place an item-level recall.
Pickup location: Branch A.
Recall not needed after (expiration date): whatever you want
Select 'recall a specific item'
Item B will not be selectable, and Item A should be selected by default.
Click confirm

97. In the staff client, edit Item B and remove the lost or missing status.
98. Check in Item B. Confirm the recall box does not pop up, because it cannot fill the item-level recall.
99. Check in Item A. Confirm the recall as waiting.
100. Go to Circulation -> Recalls awaiting pickup
101. Expire the recall. Confirm it expires as expected.

ITEM-LEVEL RECALL, TRANSFER REQUIRED

102. Repeat steps 91 to 95.
103. Place an item-level recall.
Pickup location: Branch B, we will require a transfer.
Recall not needed after (expiration date): whatever you want
Select 'recall a specific item'
Item B will not be selectable, and Item A should be selected by default.
Click confirm
104. In the staff client, check in Item A. Confirm the recall and trigger the transfer.
105. Set your library to Branch B and check in Item A.
106. Confirm the recall as waiting.
107. Check out Item A to Borrower A and fulfill the recall.

108. Set your library back to Branch A and check in Item A.

CRONJOBS: EXPIRING RECALL

109. Check out Item A to Borrower B.
110. Log in to the OPAC as Borrower A. Place a recall (any level) on Biblio A.
111. In your terminal, enter mysql and edit the expiration date of your recall to be before today
UPDATE recalls SET expirationdate = NOW()-2 WHERE recall_id = X;
112. Run the expiry cronjob from within your shell
perl misc/cronjobs/recalls/expire_recalls.pl
113. Go to Borrower A's account and go to the Recalls history tab
114. Confirm the recall has been expired because the current date surpassed the specified expiration date

115. Check out Item A to Borrower B.
116. Log in to the OPAC as Borrower A. Place a recall (any level) on Biblio A.
117. In the staff client, check in Item A and confirm the recall as waiting.
118. In your terminal, enter mysql and edit the waiting date of your recall to be before today
UPDATE recalls SET waitingdate = NOW() - interval 5 day WHERE recall_id = X;
119. Run the expiry cronjob from within your shell
perl misc/cronjobs/recalls/expire_recalls.pl
120. Go to Borrower A's account and go to the Recalls history tab
121. Confirm the recall has been expired because the recall had been waiting for more days than the Recall pickup period

122. Go to Administration -> circulation rules. Set the following rules:
Recall pickup period (days) = 0
123. Set the RecallsMaxPickUpDelay system preference = 1.
124. Check out Item A to Borrower B.
125. Log in to the OPAC as Borrower A. Place a recall (any level) on Biblio A.
126. In the staff client, check in Item A and confirm the recall as waiting.
127. In your terminal, enter mysql and edit the waiting date of your recall to be before today
UPDATE recalls SET waitingdate = NOW()-2 WHERE recall_id = X;
128. Run the expiry cronjob from within your shell
perl misc/cronjobs/recalls/expire_recalls.pl
129. Go to Borrower A's account and go to the Recalls history tab
130. Confirm the recall has been expired because the recall had been waiting for more days than the RecallsMaxPickUpDelay syspref

CRONJOBS: OVERDUE RECALL

131. Check out Item A to Borrower B
132. Log in to the OPAC as Borrower A. Place a recall (any level) on Biblio A.
133. In your terminal, enter mysql and edit the due date of the checkout to Borrower B to be before today
UPDATE issues SET date_due = NOW()-2 WHERE issue_id = X;
134. Run the overdue cronjob from within your shell
perl misc/cronjobs/recall/overdue_recalls.pl
135. Go to Circulation -> Overdue recalls
136. Confirm your recall is showing here now as the recall has been marked Overdue

CIRCULATION

137. Check in Item A.
138. When the recall box pops up, click Ignore.
139. Check out Item A to Borrower B. You should see a yellow confirmation box, saying that another borrower has recalled the item you are trying to check out.
140. Click "No don't check out" and confirm the item isn't checked out and the recall remains.
141. Repeat Step 139.
142. Click "Yes check out" and confirm the item is checked out and the recall remains.
143. When Borrower B's checkout table loads, confirm that you cannot renew or check in the item from the Checkouts table because there is a 'Recalled' link which takes you to the recalls tab for that biblio.
144. Repeat Steps 137-139.
145. Select "Cancel recall" and click "Yes check out" and confirm the item is checked out and the recall has been cancelled.
146. Log in to the OPAC as Borrower A. Place a recall (any level) on Biblio A.
147. Check in Item A. Confirm the recall as waiting.
148. Check out Item A to Borrower B. You should see a yellow confirmation box, saying that that another borrower has recalled the item that you are trying to check out.
149. Select "Revert waiting status" and click "Yes check out" and confirm the item is checked out and the recall status has reverted to requested.

OTHER
150. In your terminal, enter mysql and edit the due date of the checkout to Borrower B to be before today
UPDATE issues SET date_due = NOW()-2 WHERE issue_id = X;
151. Go to Borrower A's recalls and click the Actions dropdown.
152. Click "Mark as overdue" and confirm the recall is marked as overdue manually.

153. Go to Tools -> Log Viewer. Check only the Recalls module, and leave all other parameters, and click Submit.
154. Confirm all of the recalls actions that have been made are correctly logged.

Note: recalls messaging preferences are introduced in Bug 23781.
The recall feature is fully documented at: https://wiki.koha-community.org/wiki/Catalyst_IT_Recalls

Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: David Nind <david@davidnind.com>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>
Signed-off-by: Fridolin Somers <fridolin.somers@biblibre.com>
2022-03-14 22:45:51 -10:00

518 lines
17 KiB
Perl
Executable file

#!/usr/bin/perl
# Copyright 2000-2002 Katipo Communications
#
# This file is part of Koha.
#
# Koha is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Koha is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Koha; if not, see <http://www.gnu.org/licenses>.
=head1 tools/letter.pl
ALGO :
this script use an $op to know what to do.
if $op is empty or none of the values listed below,
- the default screen is built (with all or filtered (if search string is set) records).
- the user can click on add, modify or delete record.
- filtering is done on the code field
if $op=add_form
- if primary key (module + code) exists, this is a modification,so we read the required record
- builds the add/modify form
if $op=add_validate
- the user has just send data, so we create/modify the record
if $op=delete_form
- we show the record selected and ask for confirmation
if $op=delete_confirm
- we delete the designated record
=cut
# TODO This script drives the CRUD operations on the letter table
# The DB interaction should be handled by calls to C4/Letters.pm
use Modern::Perl;
use CGI qw ( -utf8 );
use C4::Auth qw( get_template_and_user );
use C4::Context;
use C4::Output qw( output_html_with_http_headers );
use C4::Letters qw( GetMessageTransportTypes );
use C4::Log qw( logaction );
use Koha::Notice::Templates;
use Koha::Patron::Attribute::Types;
# $protected_letters = protected_letters()
# - return a hashref of letter_codes representing letters that should never be deleted
sub protected_letters {
my $dbh = C4::Context->dbh;
my $codes = $dbh->selectall_arrayref(q{SELECT DISTINCT letter_code FROM message_transports});
return { map { $_->[0] => 1 } @{$codes} };
}
our $input = CGI->new;
my $searchfield = $input->param('searchfield');
my $script_name = '/cgi-bin/koha/tools/letter.pl';
our $branchcode = $input->param('branchcode');
$branchcode = '' if defined $branchcode and $branchcode eq '*';
my $code = $input->param('code');
my $module = $input->param('module') || '';
my $content = $input->param('content');
my $op = $input->param('op') || '';
my $redirect = $input->param('redirect');
my $section = $input->param('section');
my $dbh = C4::Context->dbh;
our ( $template, $borrowernumber, $cookie, $staffflags ) = get_template_and_user(
{
template_name => 'tools/letter.tt',
query => $input,
type => 'intranet',
flagsrequired => { tools => 'edit_notices' },
}
);
our $my_branch = C4::Context->preference("IndependentBranches") && !$staffflags->{'superlibrarian'}
? C4::Context->userenv()->{'branch'}
: undef;
# we show only the TMPL_VAR names $op
$template->param(
independant_branch => $my_branch,
script_name => $script_name,
searchfield => $searchfield,
branchcode => $branchcode,
section => $section,
action => $script_name
);
if ( $op eq 'add_validate' or $op eq 'copy_validate' ) {
add_validate();
if( $redirect eq "just_save" ){
print $input->redirect("/cgi-bin/koha/tools/letter.pl?op=add_form&branchcode=$branchcode&module=$module&code=$code&redirect=done&section=$section");
exit;
} else {
$op = q{}; # we return to the default screen for the next operation
}
}
if ($op eq 'copy_form') {
my $oldbranchcode = $input->param('oldbranchcode') || q||;
my $branchcode = $input->param('branchcode');
add_form($oldbranchcode, $module, $code);
$template->param(
oldbranchcode => $oldbranchcode,
branchcode => $branchcode,
copying => 1,
modify => 0,
);
}
elsif ( $op eq 'add_form' ) {
add_form($branchcode, $module, $code);
}
elsif ( $op eq 'delete_confirm' ) {
delete_confirm($branchcode, $module, $code);
}
elsif ( $op eq 'delete_confirmed' ) {
delete_confirmed($branchcode, $module, $code);
$op = q{}; # next operation is to return to default screen
}
else {
default_display($branchcode,$searchfield);
}
# Do this last as delete_confirmed resets
if ($op) {
$template->param($op => 1);
} else {
$template->param(no_op_set => 1);
}
output_html_with_http_headers $input, $cookie, $template->output;
sub add_form {
my ( $branchcode,$module, $code ) = @_;
my $letters;
# if code has been passed we can identify letter and its an update action
if ($code) {
$letters = C4::Letters::GetLetterTemplates(
{
branchcode => $branchcode,
module => $module,
code => $code,
}
);
}
my $message_transport_types = GetMessageTransportTypes();
my $templates = { map { $_ => { message_transport_type => $_ } } sort @$message_transport_types };
my %letters = ( default => { templates => $templates } );
if ( C4::Context->preference('TranslateNotices') ) {
my $translated_languages =
C4::Languages::getTranslatedLanguages( 'opac',
C4::Context->preference('template') );
for my $language (@$translated_languages) {
for my $sublanguage( @{ $language->{sublanguages_loop} } ) {
if ( $language->{plural} ) {
$letters{ $sublanguage->{rfc4646_subtag} } = {
description => $sublanguage->{native_description}
. ' '
. $sublanguage->{region_description} . ' ('
. $sublanguage->{rfc4646_subtag} . ')',
templates => { %$templates },
};
}
else {
$letters{ $sublanguage->{rfc4646_subtag} } = {
description => $sublanguage->{native_description}
. ' ('
. $sublanguage->{rfc4646_subtag} . ')',
templates => { %$templates },
};
}
}
}
$template->param( languages => $translated_languages );
}
if ($letters) {
$template->param(
modify => 1,
code => $code,
);
my $first_flag_name = 1;
my $lang;
# The letter name is contained into each mtt row.
# So we can only sent the first one to the template.
for my $letter ( @$letters ) {
# The letter_name
if ( $first_flag_name and $letter->{name} ) {
$template->param(
letter_name=> $letter->{name},
);
$first_flag_name = 0;
}
my $lang = $letter->{lang};
my $mtt = $letter->{message_transport_type};
$letters{ $lang }{templates}{$mtt} = {
message_transport_type => $letter->{message_transport_type},
is_html => $letter->{is_html},
updated_on => $letter->{updated_on},
title => $letter->{title},
content => $letter->{content} // '',
};
}
}
else {
$template->param( adding => 1 );
}
$template->param(
letters => \%letters,
);
my $field_selection;
push @{$field_selection}, add_fields('branches');
if ($module eq 'reserves') {
push @{$field_selection}, add_fields('borrowers', 'reserves', 'biblio', 'biblioitems', 'items');
}
elsif ( $module eq 'acquisition' ) {
push @{$field_selection}, add_fields('aqbooksellers', 'aqorders', 'biblio', 'items');
}
elsif ($module eq 'claimacquisition' || $module eq 'orderacquisition') {
push @{$field_selection}, add_fields('aqbooksellers', 'aqbasket', 'aqorders', 'biblio', 'biblioitems');
}
elsif ($module eq 'claimissues') {
push @{$field_selection}, add_fields('aqbooksellers', 'serial', 'subscription', 'biblio', 'biblioitems');
}
elsif ($module eq 'serial') {
push @{$field_selection}, add_fields('branches', 'biblio', 'biblioitems', 'borrowers', 'subscription', 'serial');
}
elsif ($module eq 'suggestions') {
push @{$field_selection}, add_fields('suggestions', 'borrowers', 'biblio');
}
else {
push @{$field_selection}, add_fields('biblio','biblioitems'),
add_fields('items'),
{value => 'items.content', text => 'items.content'},
{value => 'items.fine', text => 'items.fine'},
add_fields('borrowers');
if ($module eq 'circulation') {
push @{$field_selection}, add_fields('additional_contents', 'recalls');
}
if ( $module eq 'circulation' and $code and ( $code eq "CHECKIN" or $code eq "CHECKINSLIP" ) ) {
push @{$field_selection}, add_fields('old_issues');
} else {
push @{$field_selection}, add_fields('issues');
}
if ( $module eq 'circulation' and $code and $code =~ /^AR_/ ) {
push @{$field_selection}, add_fields('article_requests');
}
if ( $module eq 'members' and $code and $code eq 'PROBLEM_REPORT' ) {
push @{$field_selection}, add_fields('problem_reports');
}
if ( $module eq 'ill' ) {
push @{$field_selection}, add_fields('illrequests');
}
}
my $preview_is_available = 0;
if ($code) {
$preview_is_available = grep {$_ eq $code } qw( CHECKIN CHECKOUT HOLD_SLIP );
}
$template->param(
module => $module,
SQLfieldnames => $field_selection,
branchcode => $branchcode,
preview_is_available => $preview_is_available,
);
return;
}
sub add_validate {
my $dbh = C4::Context->dbh;
my $branchcode = $input->param('branchcode');
my $module = $input->param('module');
my $oldmodule = $input->param('oldmodule');
my $code = $input->param('code');
my $name = $input->param('name');
my @mtt = $input->multi_param('message_transport_type');
my @title = $input->multi_param('title');
my @content = $input->multi_param('content');
my @lang = $input->multi_param('lang');
for my $mtt ( @mtt ) {
my $lang = shift @lang;
my $is_html = $input->param("is_html_$mtt\_$lang");
my $title = shift @title;
my $content = shift @content;
my $letter = Koha::Notice::Templates->find(
{
module => $oldmodule,
code => $code,
branchcode => $branchcode,
message_transport_type => $mtt,
lang => $lang
}
);
unless ( $title and $content ) {
# Delete this mtt if no title or content given
delete_confirmed( $branchcode, $oldmodule, $code, $mtt, $lang );
next;
}
elsif ( $letter ) {
logaction( 'NOTICES', 'MODIFY', $letter->id, $content,
'Intranet' )
if ( C4::Context->preference("NoticesLog")
&& $content ne $letter->content );
$letter->set(
{
branchcode => $branchcode || '',
module => $module,
name => $name,
is_html => $is_html || 0,
title => $title,
content => $content,
lang => $lang
}
)->store;
} else {
my $letter = Koha::Notice::Template->new(
{
branchcode => $branchcode,
module => $module,
code => $code,
name => $name,
is_html => $is_html,
title => $title,
content => $content,
message_transport_type => $mtt,
lang => $lang
}
)->store;
logaction( 'NOTICES', 'CREATE', $letter->id, $letter->content,
'Intranet' )
if C4::Context->preference("NoticesLog");
}
}
# set up default display
default_display($branchcode);
return 1;
}
sub delete_confirm {
my ($branchcode, $module, $code) = @_;
my $dbh = C4::Context->dbh;
my $letter = Koha::Notice::Templates->search(
{ module => $module, code => $code, branchcode => $branchcode } );
$template->param(
letter => $letter ? $letter->next : undef,
);
return;
}
sub delete_confirmed {
my ( $branchcode, $module, $code, $mtt, $lang ) = @_;
my $letters = Koha::Notice::Templates->search(
{
branchcode => $branchcode || '',
module => $module,
code => $code,
( $mtt ? ( message_transport_type => $mtt ) : () ),
( $lang ? ( lang => $lang ) : () ),
}
);
while ( my $letter = $letters->next ) {
logaction( 'NOTICES', 'DELETE', $letter->id, $letter->content,
'Intranet' )
if C4::Context->preference("NoticesLog");
$letter->delete;
}
# setup default display for screen
default_display($branchcode);
return;
}
sub retrieve_letters {
my ($branchcode, $searchstring) = @_;
$branchcode = $my_branch if $branchcode && $my_branch;
my $dbh = C4::Context->dbh;
my ($sql, @where, @args);
$sql = "SELECT branchcode, module, code, name, branchname, MAX(updated_on) as updated_on
FROM letter
LEFT OUTER JOIN branches USING (branchcode)
";
if ($searchstring && $searchstring=~m/(\S+)/) {
$searchstring = $1 . q{%};
push @where, 'code LIKE ?';
push @args, $searchstring;
}
elsif ($branchcode) {
push @where, 'branchcode = ?';
push @args, $branchcode || '';
}
elsif ($my_branch) {
push @where, "(branchcode = ? OR branchcode = '')";
push @args, $my_branch;
}
$sql .= " WHERE ".join(" AND ", @where) if @where;
$sql .= " GROUP BY branchcode,module,code,name,branchname";
$sql .= " ORDER BY module, code, branchcode";
return $dbh->selectall_arrayref($sql, { Slice => {} }, @args);
}
sub default_display {
my ($branchcode, $searchfield) = @_;
unless ( defined $branchcode ) {
if ( C4::Context->preference('DefaultToLoggedInLibraryNoticesSlips') ) {
$branchcode = C4::Context::mybranch();
}
}
if ( $searchfield ) {
$template->param( search => 1 );
}
my $results = retrieve_letters($branchcode,$searchfield);
my $loop_data = [];
my $protected_letters = protected_letters();
foreach my $row (@{$results}) {
$row->{protected} = !$row->{branchcode} && $protected_letters->{ $row->{code} };
push @{$loop_data}, $row;
}
$template->param(
letter => $loop_data,
branchcode => $branchcode,
);
}
sub add_fields {
my @tables = @_;
my @fields = ();
for my $table (@tables) {
push @fields, get_columns_for($table);
}
return @fields;
}
sub get_columns_for {
my $table = shift;
# FIXME untranslatable
my %column_map = (
aqbooksellers => '---BOOKSELLERS---',
aqorders => '---ORDERS---',
serial => '---SERIALS---',
reserves => '---HOLDS---',
suggestions => '---SUGGESTIONS---',
);
my @fields = ();
if (exists $column_map{$table} ) {
push @fields, {
value => q{},
text => $column_map{$table} ,
};
}
else {
my $tlabel = '---' . uc $table;
$tlabel.= '---';
push @fields, {
value => q{},
text => $tlabel,
};
}
my $sql = "SHOW COLUMNS FROM $table";# TODO not db agnostic
my $table_prefix = $table . q|.|;
my $rows = C4::Context->dbh->selectall_arrayref($sql, { Slice => {} });
for my $row (@{$rows}) {
next if $row->{'Field'} eq 'timestamp'; # this is really an irrelevant field and there may be other common fields that should be excluded from the list
push @fields, {
value => $table_prefix . $row->{Field},
text => $table_prefix . $row->{Field},
}
}
if ($table eq 'borrowers') {
my $attribute_types = Koha::Patron::Attribute::Types->search(
{},
{ order_by => 'code' },
);
while ( my $at = $attribute_types->next ) {
push @fields, {
value => "borrower-attribute:" . $at->code,
text => "attribute:" . $at->code,
}
}
}
return @fields;
}