Bug 18124: add to members/deletemem.pl
[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 strict;
25 #use warnings; FIXME - Bug 2505
26
27 use CGI qw ( -utf8 );
28 use Digest::MD5 qw(md5_base64);
29 use C4::Context;
30 use C4::Output;
31 use C4::Auth;
32 use C4::Members;
33 use C4::Branch; # GetBranches
34 use Module::Load;
35 use Koha::Patron::Images;
36 use Koha::Token;
37
38 if ( C4::Context->preference('NorwegianPatronDBEnable') && C4::Context->preference('NorwegianPatronDBEnable') == 1 ) {
39     load Koha::NorwegianPatronDB, qw( NLMarkForDeletion NLSync );
40 }
41
42 my $input = new CGI;
43
44 my ($template, $borrowernumber, $cookie)
45                 = get_template_and_user({template_name => "members/deletemem.tt",
46                                         query => $input,
47                                         type => "intranet",
48                                         authnotrequired => 0,
49                                         flagsrequired => {borrowers => 1},
50                                         debug => 1,
51                                         });
52
53 #print $input->header;
54 my $member       = $input->param('member');
55
56 #Do not delete yourself...
57 if ($borrowernumber == $member ) {
58     print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_YOURSELF");
59     exit 0; # Exit without error
60 }
61
62 # Handle deletion from the Norwegian national patron database, if it is enabled
63 # If the "deletelocal" parameter is set to "false", the regular deletion will be
64 # short circuited, and only a deletion from the national database can be carried
65 # out. If "deletelocal" is set to "true", or not set to anything normal
66 # deletion will be done.
67 my $deletelocal  = $input->param('deletelocal')  eq 'false' ? 0 : 1; # Deleting locally is the default
68 if ( C4::Context->preference('NorwegianPatronDBEnable') && C4::Context->preference('NorwegianPatronDBEnable') == 1 ) {
69     if ( $input->param('deleteremote') eq 'true' ) {
70         # Mark for deletion, then try a live sync
71         NLMarkForDeletion( $member );
72         NLSync({ 'borrowernumber' => $member });
73     }
74 }
75
76 my $issues = GetPendingIssues($member);     # FIXME: wasteful call when really, we only want the count
77 my $countissues = scalar(@$issues);
78
79 my ($bor)=GetMemberDetails($member,'');
80 my $flags=$bor->{flags};
81 my $userenv = C4::Context->userenv;
82
83  
84
85 if ($bor->{category_type} eq "S") {
86     unless(C4::Auth::haspermission($userenv->{'id'},{'staffaccess'=>1})) {
87         print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_STAFF");
88         exit 0; # Exit without error
89     }
90 } else {
91     unless(C4::Auth::haspermission($userenv->{'id'},{'borrowers'=>1})) {
92         print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE");
93         exit 0; # Exit without error
94     }
95 }
96
97 if (C4::Context->preference("IndependentBranches")) {
98     my $userenv = C4::Context->userenv;
99     if ( !C4::Context->IsSuperLibrarian() && $bor->{'branchcode'}){
100         unless ($userenv->{branch} eq $bor->{'branchcode'}){
101             print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_OTHERLIBRARY");
102             exit 0; # Exit without error
103         }
104     }
105 }
106
107 my $op = $input->param('op') || 'delete_confirm';
108 my $dbh = C4::Context->dbh;
109 my $is_guarantor = $dbh->selectrow_array("SELECT COUNT(*) FROM borrowers WHERE guarantorid=?", undef, $member);
110 if ( $op eq 'delete_confirm' or $countissues > 0 or $flags->{'CHARGES'}  or $is_guarantor or $deletelocal == 0) {
111     my $patron_image = Koha::Patron::Images->find($bor->{borrowernumber});
112     $template->param( picture => 1 ) if $patron_image;
113
114     $template->param( adultborrower => 1 ) if ( $bor->{category_type} eq 'A' || $bor->{category_type} eq 'I' );
115
116     $template->param(borrowernumber => $member,
117         surname => $bor->{'surname'},
118         title => $bor->{'title'},
119         cardnumber => $bor->{'cardnumber'},
120         firstname => $bor->{'firstname'},
121         categorycode => $bor->{'categorycode'},
122         category_type => $bor->{'category_type'},
123         categoryname  => $bor->{'description'},
124         address => $bor->{'address'},
125         address2 => $bor->{'address2'},
126         city => $bor->{'city'},
127         zipcode => $bor->{'zipcode'},
128         country => $bor->{'country'},
129         phone => $bor->{'phone'},
130         email => $bor->{'email'},
131         branchcode => $bor->{'branchcode'},
132         RoutingSerials => C4::Context->preference('RoutingSerials'),
133     );
134     if ($countissues >0) {
135         $template->param(ItemsOnIssues => $countissues);
136     }
137     if ($flags->{'CHARGES'} ne '') {
138         $template->param(charges => $flags->{'CHARGES'}->{'amount'});
139     }
140     if ($is_guarantor) {
141         $template->param(guarantees => 1);
142     }
143     if ($deletelocal == 0) {
144         $template->param(keeplocal => 1);
145     }
146     # This is silly written but reflect the same conditions as above
147     if ( not $countissues > 0 and not $flags->{CHARGES} ne '' and not $is_guarantor and not $deletelocal == 0 ) {
148         $template->param(
149             op         => 'delete_confirm',
150             csrf_token => Koha::Token->new->generate_csrf({ session_id => scalar $input->cookie('CGISESSID') }),
151         );
152     }
153 } elsif ( $op eq 'delete_confirmed' ) {
154
155     die "Wrong CSRF token"
156
157         unless Koha::Token->new->check_csrf( {
158             session_id => $input->cookie('CGISESSID'),
159             token  => scalar $input->param('csrf_token'),
160         });
161     MoveMemberToDeleted($member);
162     C4::Members::HandleDelBorrower($member);
163     DelMember($member);
164     # TODO Tell the user everything went ok
165     print $input->redirect("/cgi-bin/koha/members/members-home.pl");
166     exit 0; # Exit without error
167 }
168
169 output_html_with_http_headers $input, $cookie, $template->output;