Bug 16996: (follow-up) Do not explode if mandatory fields are missing
[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 C4::Context;
29 use C4::Output;
30 use C4::Auth;
31 use C4::Members;
32 use C4::Branch; # GetBranches
33 use Module::Load;
34 use Koha::Patron::Images;
35 if ( C4::Context->preference('NorwegianPatronDBEnable') && C4::Context->preference('NorwegianPatronDBEnable') == 1 ) {
36     load Koha::NorwegianPatronDB, qw( NLMarkForDeletion NLSync );
37 }
38
39 my $input = new CGI;
40
41 my ($template, $borrowernumber, $cookie)
42                 = get_template_and_user({template_name => "members/deletemem.tt",
43                                         query => $input,
44                                         type => "intranet",
45                                         authnotrequired => 0,
46                                         flagsrequired => {borrowers => 1},
47                                         debug => 1,
48                                         });
49
50 #print $input->header;
51 my $member       = $input->param('member');
52
53 #Do not delete yourself...
54 if ($borrowernumber == $member ) {
55     print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_YOURSELF");
56     exit 1;
57 }
58
59 # Handle deletion from the Norwegian national patron database, if it is enabled
60 # If the "deletelocal" parameter is set to "false", the regular deletion will be
61 # short circuited, and only a deletion from the national database can be carried
62 # out. If "deletelocal" is set to "true", or not set to anything normal
63 # deletion will be done.
64 my $deletelocal  = $input->param('deletelocal')  eq 'false' ? 0 : 1; # Deleting locally is the default
65 if ( C4::Context->preference('NorwegianPatronDBEnable') && C4::Context->preference('NorwegianPatronDBEnable') == 1 ) {
66     if ( $input->param('deleteremote') eq 'true' ) {
67         # Mark for deletion, then try a live sync
68         NLMarkForDeletion( $member );
69         NLSync({ 'borrowernumber' => $member });
70     }
71 }
72
73 my $issues = GetPendingIssues($member);     # FIXME: wasteful call when really, we only want the count
74 my $countissues = scalar(@$issues);
75
76 my ($bor)=GetMemberDetails($member,'');
77 my $flags=$bor->{flags};
78 my $userenv = C4::Context->userenv;
79
80  
81
82 if ($bor->{category_type} eq "S") {
83     unless(C4::Auth::haspermission($userenv->{'id'},{'staffaccess'=>1})) {
84         print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_STAFF");
85         exit 1;
86     }
87 } else {
88     unless(C4::Auth::haspermission($userenv->{'id'},{'borrowers'=>1})) {
89         print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE");
90         exit 1;
91     }
92 }
93
94 if (C4::Context->preference("IndependentBranches")) {
95     my $userenv = C4::Context->userenv;
96     if ( !C4::Context->IsSuperLibrarian() && $bor->{'branchcode'}){
97         unless ($userenv->{branch} eq $bor->{'branchcode'}){
98             print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_OTHERLIBRARY");
99             exit;
100         }
101     }
102 }
103
104 my $dbh = C4::Context->dbh;
105 my $sth=$dbh->prepare("Select * from borrowers where guarantorid=?");
106 $sth->execute($member);
107 my $data=$sth->fetchrow_hashref;
108 if ($countissues > 0 or $flags->{'CHARGES'}  or $data->{'borrowernumber'} or $deletelocal == 0){
109     #   print $input->header;
110
111     my $patron_image = Koha::Patron::Images->find($bor->{borrowernumber});
112     $template->param( picture => 1 ) if $patron_image;
113
114     $template->param(borrowernumber => $member,
115         surname => $bor->{'surname'},
116         title => $bor->{'title'},
117         cardnumber => $bor->{'cardnumber'},
118         firstname => $bor->{'firstname'},
119         categorycode => $bor->{'categorycode'},
120         category_type => $bor->{'category_type'},
121         categoryname  => $bor->{'description'},
122         address => $bor->{'address'},
123         address2 => $bor->{'address2'},
124         city => $bor->{'city'},
125         zipcode => $bor->{'zipcode'},
126         country => $bor->{'country'},
127         phone => $bor->{'phone'},
128         email => $bor->{'email'},
129         branchcode => $bor->{'branchcode'},
130         branchname => GetBranchName($bor->{'branchcode'}),
131                 activeBorrowerRelationship => (C4::Context->preference('borrowerRelationship') ne ''),
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 ($data->{'borrowernumber'}) {
141         $template->param(guarantees => 1);
142     }
143     if ($deletelocal == 0) {
144         $template->param(keeplocal => 1);
145     }
146 output_html_with_http_headers $input, $cookie, $template->output;
147
148 } else {
149     MoveMemberToDeleted($member);
150     C4::Members::HandleDelBorrower($member);
151     DelMember($member);
152     print $input->redirect("/cgi-bin/koha/members/members-home.pl");
153 }
154
155