Bug 15788: Use delete_borrowers permission
[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
28 use Try::Tiny;
29
30 use C4::Context;
31 use C4::Output;
32 use C4::Auth;
33 use C4::Members;
34 use C4::Suggestions qw( SearchSuggestion );
35 use Koha::Patrons;
36 use Koha::Token;
37 use Koha::Patron::Categories;
38
39 my $input = CGI->new;
40
41 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
42     {   template_name => "members/deletemem.tt",
43         query         => $input,
44         type          => "intranet",
45         flagsrequired => { borrowers => 'delete_borrowers' },
46         debug         => 1,
47     }
48 );
49
50 #print $input->header;
51 my $member       = $input->param('member');
52
53 #Do not delete yourself...
54 if ( $loggedinuser == $member ) {
55     print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_YOURSELF");
56     exit 0; # Exit without error
57 }
58
59 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
60 my $patron         = Koha::Patrons->find( $member );
61 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
62
63 my $debits = $patron->account->outstanding_debits->total_outstanding;
64 my $credits = abs $patron->account->outstanding_credits->total_outstanding;
65 my $countissues = $patron->checkouts->count;
66 my $userenv = C4::Context->userenv;
67
68 if ($patron->category->category_type eq "S") {
69     unless(C4::Auth::haspermission($userenv->{'id'},{'staffaccess'=>1})) {
70         print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_STAFF");
71         exit 0; # Exit without error
72     }
73 } else {
74     unless(C4::Auth::haspermission($userenv->{'id'},{'borrowers'=>'delete_borrowers'})) {
75         print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE");
76         exit 0; # Exit without error
77     }
78 }
79
80 if (C4::Context->preference("IndependentBranches")) {
81     my $userenv = C4::Context->userenv;
82     if ( !C4::Context->IsSuperLibrarian() && $patron->branchcode){
83         unless ($userenv->{branch} eq $patron->branchcode){
84             print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_OTHERLIBRARY");
85             exit 0; # Exit without error
86         }
87     }
88 }
89
90 if ( my $anonymous_patron = C4::Context->preference("AnonymousPatron") ) {
91     if ( $patron->id eq $anonymous_patron ) {
92         print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_ANONYMOUS_PATRON");
93         exit 0;    # Exit without error
94     }
95 }
96
97 my $op = $input->param('op') || 'delete_confirm';
98 my $dbh = C4::Context->dbh;
99 my $is_guarantor = $patron->guarantee_relationships->count;
100 my $countholds = $dbh->selectrow_array("SELECT COUNT(*) FROM reserves WHERE borrowernumber=?", undef, $member);
101
102 # Add warning if patron has pending suggestions
103 $template->param(
104     pending_suggestions => scalar @{
105     C4::Suggestions::SearchSuggestion(
106             { suggestedby => $member, STATUS => 'ASKED' }
107         )
108     }
109 );
110
111 $template->param(
112     patron        => $patron,
113     ItemsOnIssues => $countissues,
114     debits        => $debits,
115     credits       => $credits,
116     is_guarantor  => $is_guarantor,
117     ItemsOnHold   => $countholds,
118 );
119
120 if ( $op eq 'delete_confirm' or $countissues > 0 or $debits or $is_guarantor ) {
121     $template->param(
122         op         => 'delete_confirm',
123         csrf_token => Koha::Token->new->generate_csrf({ session_id => scalar $input->cookie('CGISESSID') }),
124     );
125
126 } elsif ( $op eq 'delete_confirmed' ) {
127     output_and_exit( $input, $cookie, $template, 'wrong_csrf_token' )
128         unless Koha::Token->new->check_csrf( {
129             session_id => $input->cookie('CGISESSID'),
130             token  => scalar $input->param('csrf_token'),
131         });
132     my $patron = Koha::Patrons->find( $member );
133     $patron->move_to_deleted;
134     try {
135         $patron->delete;
136         print $input->redirect("/cgi-bin/koha/members/members-home.pl");
137     } catch {
138         print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_ANONYMOUS_PATRON");
139     };
140     # TODO Tell the user everything went ok
141     exit 0; # Exit without error
142 }
143
144 output_html_with_http_headers $input, $cookie, $template->output;