Bug 14224: Allow patron notes about item shown at check in
[koha.git] / opac / svc / patron_notes
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright 2014 BibLibre
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use Modern::Perl;
21
22 use C4::Service;
23 use C4::Auth qw /check_cookie_auth/;
24 use C4::Letters;
25 use CGI;
26 use C4::Output qw(:DEFAULT :ajax);
27 use C4::Scrubber;
28 use C4::Circulation;
29 use C4::Members;
30 use C4::Biblio;
31 use Koha::Checkouts;
32 use Koha::DateUtils;
33
34 =head1 NAME
35
36 svc/patron_notes - Web service for setting patron notes on items
37
38 =head1 DESCRIPTION
39
40 =cut
41
42 # AJAX requests
43 my $is_ajax = is_ajax();
44 my $query = new CGI;
45 my ( $auth_status, $sessionID ) = check_cookie_auth( $query->cookie('CGISESSID'), {} );
46 if ( $auth_status ne "ok" ) {
47     exit 0;
48 }
49 if ($is_ajax) {
50     my $action = $query->param('action');
51
52     # Issue Note
53     if ( $action eq 'issuenote' && C4::Context->preference('AllowIssueNotes') ) {
54         my $scrubber = C4::Scrubber->new();
55         my $note = $query->param('note');
56         my $issue_id = $query->param('issue_id');
57         my $clean_note = $scrubber->scrub($note);
58         my $status = "saved";
59         my $error = "";
60         my ($member, $issue);
61
62         my ( $template, $borrowernumber, $cookie ) = C4::Auth::get_template_and_user({
63             template_name   => "opac-user.tt",
64             query           => $query,
65             type            => "opac",
66             authnotrequired => 1,
67         });
68
69         # verify issue_id
70         if ( $issue_id =~ /\d+/ ) {
71             $member = GetMember(borrowernumber => $borrowernumber);
72             $issue = Koha::Checkouts->find($issue_id);
73             if ( $issue->borrowernumber != $borrowernumber ) {
74                 $status = "fail";
75                 $error = "Invalid issue id!";
76             }
77         } else {
78             $status = "fail";
79             $error = "Invalid issue id!";
80         }
81
82         if ( (not $error) && $issue->set({ notedate => dt_from_string(), note => $clean_note })->store ) {
83             if($clean_note) { # only send email if note not empty
84                 my $branch = Koha::Libraries->find( $issue->branchcode );
85                 my $biblio = GetBiblioFromItemNumber($issue->itemnumber);
86                 my $letter = C4::Letters::GetPreparedLetter (
87                     module => 'circulation',
88                     letter_code => 'PATRON_NOTE',
89                     branchcode => $branch,
90                     tables => {
91                         'biblio' => $biblio->{biblionumber},
92                         'borrowers' => $member->{borrowernumber},
93                     },
94                 );
95                 C4::Message->enqueue($letter, $member, 'email');
96             } else { # note empty, i.e removed
97                 $status = "removed";
98             }
99         } else {
100             $status = "fail";
101             $error = "Perhaps the item has already been checked in?";
102         }
103
104         my $response = "{\"status\": \"$status\", \"note\": \"$clean_note\", \"issue_id\": \"$issue_id\", \"error\": \"$error\"}";
105         output_with_http_headers($query, undef, $response, 'js');
106         exit;
107     } # END Issue Note
108 }