Bug 21085: Fix add/edit of patrons when HouseboundModule is set
[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 use C4::Context;
28 use C4::Output;
29 use C4::Auth;
30 use C4::Members;
31 use Module::Load;
32 use Koha::Patrons;
33 use Koha::Token;
34 use Koha::Patron::Categories;
35
36 if ( C4::Context->preference('NorwegianPatronDBEnable') && C4::Context->preference('NorwegianPatronDBEnable') == 1 ) {
37     load Koha::NorwegianPatronDB, qw( NLMarkForDeletion NLSync );
38 }
39
40 my $input = new CGI;
41
42 my ($template, $loggedinuser, $cookie)
43                 = get_template_and_user({template_name => "members/deletemem.tt",
44                                         query => $input,
45                                         type => "intranet",
46                                         authnotrequired => 0,
47                                         flagsrequired => {borrowers => 'edit_borrowers'},
48                                         debug => 1,
49                                         });
50
51 #print $input->header;
52 my $member       = $input->param('member');
53
54 #Do not delete yourself...
55 if ( $loggedinuser == $member ) {
56     print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_YOURSELF");
57     exit 0; # Exit without error
58 }
59
60 my $logged_in_user = Koha::Patrons->find( $loggedinuser ) or die "Not logged in";
61 my $patron         = Koha::Patrons->find( $member );
62 output_and_exit_if_error( $input, $cookie, $template, { module => 'members', logged_in_user => $logged_in_user, current_patron => $patron } );
63
64 # Handle deletion from the Norwegian national patron database, if it is enabled
65 # If the "deletelocal" parameter is set to "false", the regular deletion will be
66 # short circuited, and only a deletion from the national database can be carried
67 # out. If "deletelocal" is set to "true", or not set to anything normal
68 # deletion will be done.
69 my $deletelocal  = $input->param('deletelocal')  eq 'false' ? 0 : 1; # Deleting locally is the default
70 if ( C4::Context->preference('NorwegianPatronDBEnable') && C4::Context->preference('NorwegianPatronDBEnable') == 1 ) {
71     if ( $input->param('deleteremote') eq 'true' ) {
72         # Mark for deletion, then try a live sync
73         NLMarkForDeletion( $member );
74         NLSync({ 'borrowernumber' => $member });
75     }
76 }
77
78 my $charges = $patron->account->non_issues_charges;
79 my $countissues = $patron->checkouts->count;
80 my $userenv = C4::Context->userenv;
81
82 if ($patron->category->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 0; # Exit without error
86     }
87 } else {
88     unless(C4::Auth::haspermission($userenv->{'id'},{'borrowers'=>'edit_borrowers'})) {
89         print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE");
90         exit 0; # Exit without error
91     }
92 }
93
94 if (C4::Context->preference("IndependentBranches")) {
95     my $userenv = C4::Context->userenv;
96     if ( !C4::Context->IsSuperLibrarian() && $patron->branchcode){
97         unless ($userenv->{branch} eq $patron->branchcode){
98             print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_OTHERLIBRARY");
99             exit 0; # Exit without error
100         }
101     }
102 }
103
104 if ( $patron->is_child ) {
105     my $patron_categories = Koha::Patron::Categories->search_limited({ category_type => 'A' }, {order_by => ['categorycode']});
106     $template->param( 'CATCODE_MULTI' => 1) if $patron_categories->count > 1;
107     $template->param( 'catcode' => $patron_categories->next->categorycode )  if $patron_categories->count == 1;
108 }
109
110 my $op = $input->param('op') || 'delete_confirm';
111 my $dbh = C4::Context->dbh;
112 my $is_guarantor = $dbh->selectrow_array("SELECT COUNT(*) FROM borrowers WHERE guarantorid=?", undef, $member);
113 if ( $op eq 'delete_confirm' or $countissues > 0 or $charges or $is_guarantor or $deletelocal == 0) {
114
115     $template->param(
116         patron => $patron,
117     );
118     if ($countissues >0) {
119         $template->param(ItemsOnIssues => $countissues);
120     }
121     if ( $charges > 0 ) {
122         $template->param(charges => $charges);
123     }
124     if ($is_guarantor) {
125         $template->param(guarantees => 1);
126     }
127     if ($deletelocal == 0) {
128         $template->param(keeplocal => 1);
129     }
130     # This is silly written but reflect the same conditions as above
131     if ( not $countissues > 0 and not $charges and not $is_guarantor and not $deletelocal == 0 ) {
132         $template->param(
133             op         => 'delete_confirm',
134             csrf_token => Koha::Token->new->generate_csrf({ session_id => scalar $input->cookie('CGISESSID') }),
135         );
136     }
137 } elsif ( $op eq 'delete_confirmed' ) {
138
139     die "Wrong CSRF token"
140         unless Koha::Token->new->check_csrf( {
141             session_id => $input->cookie('CGISESSID'),
142             token  => scalar $input->param('csrf_token'),
143         });
144     my $patron = Koha::Patrons->find( $member );
145     $patron->move_to_deleted;
146     $patron->delete;
147     # TODO Tell the user everything went ok
148     print $input->redirect("/cgi-bin/koha/members/members-home.pl");
149     exit 0; # Exit without error
150 }
151
152 output_html_with_http_headers $input, $cookie, $template->output;