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