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