Bug 11401: Add support for Norwegian national library card
[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 under the
12 # terms of the GNU General Public License as published by the Free Software
13 # Foundation; either version 2 of the License, or (at your option) any later
14 # version.
15 #
16 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
17 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
18 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
19 #
20 # You should have received a copy of the GNU General Public License along
21 # with Koha; if not, write to the Free Software Foundation, Inc.,
22 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23
24 use strict;
25 #use warnings; FIXME - Bug 2505
26
27 use CGI;
28 use C4::Context;
29 use C4::Output;
30 use C4::Auth;
31 use C4::Members;
32 use C4::Branch; # GetBranches
33 use C4::VirtualShelves (); #no import
34 use Module::Load;
35 if ( 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 # Handle deletion from the Norwegian national patron database, if it is enabled
54 # If the "deletelocal" parameter is set to "false", the regular deletion will be
55 # short circuited, and only a deletion from the national database can be carried
56 # out. If "deletelocal" is set to "true", or not set to anything normal
57 # deletion will be done.
58 my $deletelocal  = $input->param('deletelocal')  eq 'false' ? 0 : 1; # Deleting locally is the default
59 if ( C4::Context->preference('NorwegianPatronDBEnable') == 1 ) {
60     if ( $input->param('deleteremote') eq 'true' ) {
61         # Mark for deletion, then try a live sync
62         NLMarkForDeletion( $member );
63         NLSync({ 'borrowernumber' => $member });
64     }
65 }
66
67 my $issues = GetPendingIssues($member);     # FIXME: wasteful call when really, we only want the count
68 my $countissues = scalar(@$issues);
69
70 my ($bor)=GetMemberDetails($member,'');
71 my $flags=$bor->{flags};
72 my $userenv = C4::Context->userenv;
73
74  
75
76 if ($bor->{category_type} eq "S") {
77     unless(C4::Auth::haspermission($userenv->{'id'},{'staffaccess'=>1})) {
78         print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_STAFF");
79         exit 1;
80     }
81 } else {
82     unless(C4::Auth::haspermission($userenv->{'id'},{'borrowers'=>1})) {
83         print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE");
84         exit 1;
85     }
86 }
87
88 if (C4::Context->preference("IndependentBranches")) {
89     my $userenv = C4::Context->userenv;
90     if ( !C4::Context->IsSuperLibrarian() && $bor->{'branchcode'}){
91         unless ($userenv->{branch} eq $bor->{'branchcode'}){
92             print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$member&error=CANT_DELETE_OTHERLIBRARY");
93             exit;
94         }
95     }
96 }
97
98 my $dbh = C4::Context->dbh;
99 my $sth=$dbh->prepare("Select * from borrowers where guarantorid=?");
100 $sth->execute($member);
101 my $data=$sth->fetchrow_hashref;
102 if ($countissues > 0 or $flags->{'CHARGES'}  or $data->{'borrowernumber'} or $deletelocal == 0){
103     #   print $input->header;
104
105     my ($picture, $dberror) = GetPatronImage($bor->{'borrowernumber'});
106     $template->param( picture => 1 ) if $picture;
107
108     $template->param(borrowernumber => $member,
109         surname => $bor->{'surname'},
110         title => $bor->{'title'},
111         cardnumber => $bor->{'cardnumber'},
112         firstname => $bor->{'firstname'},
113         categorycode => $bor->{'categorycode'},
114         category_type => $bor->{'category_type'},
115         categoryname  => $bor->{'description'},
116         address => $bor->{'address'},
117         address2 => $bor->{'address2'},
118         city => $bor->{'city'},
119         zipcode => $bor->{'zipcode'},
120         country => $bor->{'country'},
121         phone => $bor->{'phone'},
122         email => $bor->{'email'},
123         branchcode => $bor->{'branchcode'},
124         branchname => GetBranchName($bor->{'branchcode'}),
125                 activeBorrowerRelationship => (C4::Context->preference('borrowerRelationship') ne ''),
126         RoutingSerials => C4::Context->preference('RoutingSerials'),
127     );
128     if ($countissues >0) {
129         $template->param(ItemsOnIssues => $countissues);
130     }
131     if ($flags->{'CHARGES'} ne '') {
132         $template->param(charges => $flags->{'CHARGES'}->{'amount'});
133     }
134     if ($data->{'borrowernumber'}) {
135         $template->param(guarantees => 1);
136     }
137     if ($deletelocal == 0) {
138         $template->param(keeplocal => 1);
139     }
140 output_html_with_http_headers $input, $cookie, $template->output;
141
142 } else {
143     MoveMemberToDeleted($member);
144     C4::VirtualShelves::HandleDelBorrower($member);
145     DelMember($member);
146     print $input->redirect("/cgi-bin/koha/members/members-home.pl");
147 }
148
149