Bug 22393: (follow-up) Corrections for QA feedback
[koha.git] / members / deletemem.pl
1 #!/usr/bin/perl
2
3 #script to delete items
4 #written 2/5/00
5 #by chris@katipo.co.nz
6
7 # Copyright 2000-2002 Katipo Communications
8 #
9 # This file is part of Koha.
10 #
11 # Koha is free software; you can redistribute it and/or modify it
12 # under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 3 of the License, or
14 # (at your option) any later version.
15 #
16 # Koha is distributed in the hope that it will be useful, but
17 # WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU General Public License for more details.
20 #
21 # You should have received a copy of the GNU General Public License
22 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23
24 use Modern::Perl;
25
26 use CGI qw ( -utf8 );
27 use C4::Context;
28 use C4::Output;
29 use C4::Auth;
30 use C4::Members;
31 use C4::Suggestions qw( SearchSuggestion );
32 use Koha::Patrons;
33 use Koha::Token;
34 use Koha::Patron::Categories;
35
36 my $input = new CGI;
37
38 my ($template, $loggedinuser, $cookie)
39                 = get_template_and_user({template_name => "members/deletemem.tt",
40                                         query => $input,
41                                         type => "intranet",
42                                         authnotrequired => 0,
43                                         flagsrequired => {borrowers => 'edit_borrowers'},
44                                         debug => 1,
45                                         });
46
47 #print $input->header;
48 my $member       = $input->param('member');
49
50 #Do not delete yourself...
51 if ( $loggedinuser == $member ) {
52     print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_YOURSELF");
53     exit 0; # Exit without error
54 }
55
56 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
57 my $patron         = Koha::Patrons->find( $member );
58 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
59
60 my $debits = $patron->account->outstanding_debits->total_outstanding;
61 my $credits = abs $patron->account->outstanding_credits->total_outstanding;
62 my $countissues = $patron->checkouts->count;
63 my $userenv = C4::Context->userenv;
64
65 if ($patron->category->category_type eq "S") {
66     unless(C4::Auth::haspermission($userenv->{'id'},{'staffaccess'=>1})) {
67         print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_STAFF");
68         exit 0; # Exit without error
69     }
70 } else {
71     unless(C4::Auth::haspermission($userenv->{'id'},{'borrowers'=>'edit_borrowers'})) {
72         print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE");
73         exit 0; # Exit without error
74     }
75 }
76
77 if (C4::Context->preference("IndependentBranches")) {
78     my $userenv = C4::Context->userenv;
79     if ( !C4::Context->IsSuperLibrarian() && $patron->branchcode){
80         unless ($userenv->{branch} eq $patron->branchcode){
81             print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_OTHERLIBRARY");
82             exit 0; # Exit without error
83         }
84     }
85 }
86
87 my $op = $input->param('op') || 'delete_confirm';
88 my $dbh = C4::Context->dbh;
89 my $is_guarantor = $patron->guarantee_relationships->count;
90 my $countholds = $dbh->selectrow_array("SELECT COUNT(*) FROM reserves WHERE borrowernumber=?", undef, $member);
91
92 # Add warning if patron has pending suggestions
93 $template->param(
94     pending_suggestions => scalar @{
95     C4::Suggestions::SearchSuggestion(
96             { suggestedby => $member, STATUS => 'ASKED' }
97         )
98     }
99 );
100
101 $template->param(
102     patron        => $patron,
103     ItemsOnIssues => $countissues,
104     debits        => $debits,
105     credits       => $credits,
106     is_guarantor  => $is_guarantor,
107     ItemsOnHold   => $countholds,
108 );
109
110 if ( $op eq 'delete_confirm' or $countissues > 0 or $debits or $is_guarantor ) {
111     $template->param(
112         op         => 'delete_confirm',
113         csrf_token => Koha::Token->new->generate_csrf({ session_id => scalar $input->cookie('CGISESSID') }),
114     );
115
116 } elsif ( $op eq 'delete_confirmed' ) {
117     output_and_exit( $input, $cookie, $template, 'wrong_csrf_token' )
118         unless Koha::Token->new->check_csrf( {
119             session_id => $input->cookie('CGISESSID'),
120             token  => scalar $input->param('csrf_token'),
121         });
122     my $patron = Koha::Patrons->find( $member );
123     $patron->move_to_deleted;
124     $patron->delete;
125     # TODO Tell the user everything went ok
126     print $input->redirect("/cgi-bin/koha/members/members-home.pl");
127     exit 0; # Exit without error
128 }
129
130 output_html_with_http_headers $input, $cookie, $template->output;