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