3 #script to delete items
7 # Copyright 2000-2002 Katipo Communications
9 # This file is part of Koha.
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.
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.
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>.
35 if ( C4::Context->preference('NorwegianPatronDBEnable') && C4::Context->preference('NorwegianPatronDBEnable') == 1 ) {
36 load Koha::NorwegianPatronDB, qw( NLMarkForDeletion NLSync );
41 my ($template, $borrowernumber, $cookie)
42 = get_template_and_user({template_name => "members/deletemem.tt",
46 flagsrequired => {borrowers => 'edit_borrowers'},
50 #print $input->header;
51 my $member = $input->param('member');
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 0; # Exit without error
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 });
73 my $issues = GetPendingIssues($member); # FIXME: wasteful call when really, we only want the count
74 my $countissues = scalar(@$issues);
76 my $patron = Koha::Patrons->find( $member );
78 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$member");
81 my $flags = C4::Members::patronflags( $patron->unblessed );
82 my $userenv = C4::Context->userenv;
86 if ($patron->category->category_type eq "S") {
87 unless(C4::Auth::haspermission($userenv->{'id'},{'staffaccess'=>1})) {
88 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_STAFF");
89 exit 0; # Exit without error
92 unless(C4::Auth::haspermission($userenv->{'id'},{'borrowers'=>'edit_borrowers'})) {
93 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE");
94 exit 0; # Exit without error
98 if (C4::Context->preference("IndependentBranches")) {
99 my $userenv = C4::Context->userenv;
100 if ( !C4::Context->IsSuperLibrarian() && $patron->branchcode){
101 unless ($userenv->{branch} eq $patron->branchcode){
102 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_OTHERLIBRARY");
103 exit 0; # Exit without error
108 my $op = $input->param('op') || 'delete_confirm';
109 my $dbh = C4::Context->dbh;
110 my $is_guarantor = $dbh->selectrow_array("SELECT COUNT(*) FROM borrowers WHERE guarantorid=?", undef, $member);
111 if ( $op eq 'delete_confirm' or $countissues > 0 or $flags->{'CHARGES'} or $is_guarantor or $deletelocal == 0) {
112 $template->param( picture => 1 ) if $patron->image;
114 $template->param( adultborrower => 1 ) if $patron->category->category_type =~ /^(A|I)$/;
117 # FIXME The patron object should be passed to the template
118 borrowernumber => $patron->borrowernumber,
119 surname => $patron->surname,
120 title => $patron->title,
121 cardnumber => $patron->cardnumber,
122 firstname => $patron->firstname,
123 categorycode => $patron->categorycode,
124 category_type => $patron->category->category_type,
125 categoryname => $patron->category->description,
126 address => $patron->address,
127 address2 => $patron->address2,
128 city => $patron->city,
129 zipcode => $patron->zipcode,
130 country => $patron->country,
131 phone => $patron->phone,
132 email => $patron->email,
133 branchcode => $patron->branchcode,
135 if ($countissues >0) {
136 $template->param(ItemsOnIssues => $countissues);
138 if ($flags->{'CHARGES'} ne '') {
139 $template->param(charges => $flags->{'CHARGES'}->{'amount'});
142 $template->param(guarantees => 1);
144 if ($deletelocal == 0) {
145 $template->param(keeplocal => 1);
147 # This is silly written but reflect the same conditions as above
148 if ( not $countissues > 0 and not $flags->{CHARGES} ne '' and not $is_guarantor and not $deletelocal == 0 ) {
150 op => 'delete_confirm',
151 csrf_token => Koha::Token->new->generate_csrf({ session_id => scalar $input->cookie('CGISESSID') }),
154 } elsif ( $op eq 'delete_confirmed' ) {
156 die "Wrong CSRF token"
157 unless Koha::Token->new->check_csrf( {
158 session_id => $input->cookie('CGISESSID'),
159 token => scalar $input->param('csrf_token'),
161 my $patron = Koha::Patrons->find( $member );
162 $patron->move_to_deleted;
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
169 output_html_with_http_headers $input, $cookie, $template->output;