Bug 19532: (RM follow-up) More use of system preference
[koha.git] / svc / recall
1 #!/usr/bin/perl
2
3 # Copyright 2020 Aleisha Amohia <aleisha@catalyst.net.nz>
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18
19 use Modern::Perl;
20
21 use CGI;
22 use JSON qw(encode_json);
23
24 use C4::Context;
25 use C4::Auth qw(check_cookie_auth);
26 use C4::Output qw(output_with_http_headers);
27 use C4::Circulation qw(AddReturn);
28 use Koha::Recalls;
29
30 my $input = CGI->new;
31 my $json = encode_json({ success => 0 });
32
33 my ( $auth_status, $sessionID ) = check_cookie_auth( $input->cookie('CGISESSID'), { recall => 'manage_recalls' } );
34
35 if ( $auth_status ne "ok" ) {
36     print $input->header(-type => 'text/plain', -status => '403 Forbidden');
37     exit 0;
38 }
39
40 my $recall_id = $input->param('recall_id');
41 my $recall = Koha::Recalls->find( $recall_id );
42 unless ( $recall ) {
43     my $json = encode_json({ success => 0 });
44     output_with_http_headers( $input, undef, $json, "json" );
45     exit;
46 }
47
48 my $op = $input->param('action');
49
50 if ( $op eq 'cancel' ) {
51     # cancel recall
52     $recall->set_cancelled;
53     if ( $recall->cancelled ){
54         $json = encode_json({ success => 1 });
55     }
56
57 } elsif ( $op eq 'expire' ) {
58     # expire recall
59     $recall->set_expired({ interface => 'INTRANET' });
60     if ( $recall->expired ){
61         $json = encode_json({ success => 1 });
62     }
63
64 } elsif ( $op eq 'revert' ) {
65     # revert recall waiting status
66     $recall->revert_waiting;
67     if ( $recall->requested ){
68         $json = encode_json({ success => 1 });
69     }
70
71 } elsif ( $op eq 'overdue' ) {
72     # mark recall as overdue
73     $recall->set_overdue({ interface => 'INTRANET' });
74     if ( $recall->overdue ){
75         $json = encode_json({ success => 1 });
76     }
77
78 } elsif ( $op eq 'transit' ) {
79     # cancel recall and return item to home library
80     if ( $recall->in_transit ) {
81         C4::Items::ModItemTransfer( $recall->item->itemnumber, $recall->item->holdingbranch, $recall->item->homebranch, 'CancelRecall' );
82     }
83     $recall->set_cancelled;
84     if ( $recall->cancelled ){
85         $json = encode_json({ success => 1 });
86     }
87 }
88
89 output_with_http_headers( $input, undef, $json, "json" );