Fixing bug in deletemem.pl :
[koha.git] / members / memberentry.pl
1 #!/usr/bin/perl
2 # NOTE: This file uses standard 8-space tabs
3 #       DO NOT SET TAB SIZE TO 4
4
5 # $Id$
6
7 #script to set up screen for modification of borrower details
8 #written 20/12/99 by chris@katipo.co.nz
9
10
11 # Copyright 2000-2002 Katipo Communications
12 #
13 # This file is part of Koha.
14 #
15 # Koha is free software; you can redistribute it and/or modify it under the
16 # terms of the GNU General Public License as published by the Free Software
17 # Foundation; either version 2 of the License, or (at your option) any later
18 # version.
19 #
20 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
21 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
22 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
23 #
24 # You should have received a copy of the GNU General Public License along with
25 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
26 # Suite 330, Boston, MA  02111-1307 USA
27
28 use strict;
29 use C4::Auth;
30 use C4::Context;
31 use C4::Output;
32 use C4::Interface::CGI::Output;
33 use CGI;
34 use C4::Search;
35 use C4::Members;
36 use C4::Koha;
37 use HTML::Template;
38 use Date::Manip;
39 use C4::Date;
40 use C4::Input;
41
42 my $input = new CGI;
43
44 my $dbh = C4::Context->dbh;
45
46 my ($template, $loggedinuser, $cookie)
47     = get_template_and_user({template_name => "members/memberentry.tmpl",
48                              query => $input,
49                              type => "intranet",
50                              authnotrequired => 0,
51                              flagsrequired => {borrowers => 1},
52                              debug => 1,
53                              });
54
55 my $borrowernumber=$input->param('borrowernumber');
56 my $actionType=$input->param('actionType') || '';
57 my $modify=$input->param('modify');
58 my $delete=$input->param('delete');
59 my $op=$input->param('op');
60 my $categorycode=$input->param('categorycode');
61 my $destination=$input->param('destination');
62
63
64
65 my $nok;
66 # if a add or modify is requested => check validity of data.
67 if ($op eq 'add' or $op eq 'modify') {
68         my %data;
69         my @names=$input->param;
70         foreach my $key (@names){
71                 $data{$key}=$input->param($key);
72                 $data{$key}=~ s/\'/\\\'/g;
73                 $data{$key}=~ s/\"/\\\"/g;
74         }
75         my @errors;
76         if ($data{'cardnumber'} eq ''){
77                 push @errors,"ERROR_cardnumber";
78                 $nok=1;
79         } else {
80                 #check cardnumber is valid
81                 my $nounique;
82                 if ( $op eq "add" )    {
83                         $nounique = 0;
84                 } else {
85                         $nounique = 1;
86                 }
87                 my $valid=checkdigit('',$data{'cardnumber'}, $nounique);
88                 if ($valid != 1){
89                         $nok=1;
90                         push @errors, "ERROR_invalid_cardnumber";
91                 }
92         }
93
94         if ($data{'sex'} eq '' && $categorycode ne "I"){
95                 push @errors, "ERROR_gender";
96                 $nok=1;
97         }
98         if ($data{'firstname'} eq '' && $categorycode ne "I"){
99                 push @errors,"ERROR_firstname";
100                 $nok=1;
101         }
102         if ($data{'surname'} eq ''){
103                 push @errors,"ERROR_surname";
104                 $nok=1;
105         }
106         if ($data{'streetaddress'} eq ''){
107                 push @errors, "ERROR_address";
108                 $nok=1;
109         }
110         if ($data{'city'} eq ''){
111                 push @errors, "ERROR_city";
112                 $nok=1;
113         }
114         if (C4::Context->preference("IndependantBranches")) {
115                 my $userenv = C4::Context->userenv;
116                 if ($userenv->{flags} == 1){
117                         unless ($userenv->{branch} eq $data{'branchcode'}){
118                                 push @errors, "ERROR_branch";
119                                 $nok=1;
120                         }
121                 }
122         }
123         if ($nok) {
124                 foreach my $error (@errors) {
125                         $template->param( $error => 1);
126                 }
127                 $template->param(nok => 1);
128         } else {
129                 my $query="Select * from borrowers where borrowernumber=?";
130                 my $sth=$dbh->prepare($query);
131                 $sth->execute($data{'borrowernumber'});
132                 if (my $data2=$sth->fetchrow_hashref){
133                         &modmember(%data);
134                 }else{
135                         $borrowernumber = &newmember(%data);
136                 }
137                 
138         if($destination eq "circ"){
139                 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$data{'cardnumber'}");
140         } else {
141                 print $input->redirect("/cgi-bin/koha/members/moremember.pl?bornum=$borrowernumber");
142                 }
143         }
144 }
145 if ($delete){
146         my @errors;
147         my $nok;
148         my $branch =$input->param('branchcode');
149         if (C4::Context->preference("IndependantBranches")) {
150                 my $userenv = C4::Context->userenv;
151                 if ($userenv->{flags} == 1){
152                         if ($userenv->{branch} eq $branch){
153                                 print $input->redirect("/cgi-bin/koha/deletemem.pl?member=$borrowernumber");
154                         } else {
155                                 push @errors, "ERROR_branch";
156                                 $nok=1;
157                                 print $input->redirect("/cgi-bin/koha/members/moremember.pl?bornum=$borrowernumber");
158                         }
159                 } else {
160                         print $input->redirect("/cgi-bin/koha/deletemem.pl?member=$borrowernumber");
161                 }
162         } else {
163                 print $input->redirect("/cgi-bin/koha/deletemem.pl?member=$borrowernumber");
164         }
165 } else {  # this else goes down the whole script
166         if ($actionType eq 'Add'){
167                 $template->param( addAction => 1);
168         } else {
169                 $template->param( addAction =>0);
170         }
171         # retrieve previous values : either in DB or in CGI, in case of errors in values
172         my $data;
173         if ($nok) {
174                 my @names=$input->param;
175                 foreach my $key (@names){
176                         $data->{$key}=$input->param($key);
177                 }
178         } else {
179                 $data=borrdata('',$borrowernumber);
180         }
181         if (C4::Context->preference("IndependantBranches")) {
182                 my $userenv = C4::Context->userenv;
183                 unless ($userenv->{flags} == 1){
184                         warn "userenv=".$userenv->{'branch'}."  member branch :".$data->{'branchcode'};
185                         unless ($userenv->{'branch'} eq $data->{'branchcode'}){
186                                 print $input->redirect("/cgi-bin/koha/members/moremember.pl?bornum=$borrowernumber");
187                                 exit 1;
188                         }
189                 }
190         }
191         if ($actionType eq 'Add'){
192                 $template->param( updtype => 'I');
193         } else {
194                 $template->param( updtype => 'M');
195         }
196         my $cardnumber=C4::Members::fixup_cardnumber($data->{'cardnumber'});
197         if ($data->{'sex'} eq 'F'){
198                 $template->param(female => 1);
199         }
200         my ($categories,$labels)=ethnicitycategories();
201         my $ethnicitycategoriescount=$#{$categories};
202         my $ethcatpopup;
203         if ($ethnicitycategoriescount>=0) {
204                 $ethcatpopup = CGI::popup_menu(-name=>'ethnicity',
205                                         -id => 'ethnicity',
206                                         -values=>$categories,
207                                         -default=>$data->{'ethnicity'},
208                                         -labels=>$labels);
209                 $template->param(ethcatpopup => $ethcatpopup); # bad style, has to be fixed
210         }
211
212         ($categories,$labels)=borrowercategories();
213         my $catcodepopup = CGI::popup_menu(-name=>'categorycode',
214                                         -id => 'categorycode',
215                                         -values=>$categories,
216                                         -default=>$data->{'categorycode'},
217                                         -labels=>$labels);
218
219         my @relationships = ('','workplace', 'relative','friend', 'neighbour');
220         my @relshipdata;
221         while (@relationships) {
222                 my $relship = shift @relationships;
223                 my %row = ('relationship' => $relship);
224                 if ($data->{'altrelationship'} eq $relship) {
225                         $row{'selected'}=' selected';
226                 } else {
227                         $row{'selected'}='';
228                 }
229                 push(@relshipdata, \%row);
230         }
231
232         # %flags: keys=$data-keys, datas=[formname, HTML-explanation]
233         my %flags = ('gonenoaddress' => ['gna', 'Gone no address'],
234                                 'lost'          => ['lost', 'Lost'],
235                                 'debarred'      => ['debarred', 'Debarred']);
236
237         my @flagdata;
238         foreach (keys(%flags)) {
239         my $key = $_;
240         my %row =  ('key'   => $key,
241                         'name'  => $flags{$key}[0],
242                         'html'  => $flags{$key}[1]);
243         if ($data->{$key}) {
244                 $row{'yes'}=' checked';
245                 $row{'no'}='';
246         } else {
247                 $row{'yes'}='';
248                 $row{'no'}=' checked';
249         }
250         push(@flagdata, \%row);
251         }
252
253         if ($modify){
254         $template->param( modify => 1 );
255         }
256
257         #Convert dateofbirth to correct format
258         $data->{'dateofbirth'} = format_date($data->{'dateofbirth'});
259
260         my @branches;
261         my @select_branch;
262         my %select_branches;
263         my $branches=getbranches();
264         my $default;
265         foreach my $branch (keys %$branches) {
266                 if (C4::Context->preference("IndependantBranches")) {
267                         my $userenv = C4::Context->userenv;
268                         if ($userenv->{flags} == 1){
269                                 push @select_branch, $branch;
270                                 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
271                                 $default = $data->{'branchcode'};
272                         } else {
273                                 push @select_branch, $branch if ($branch eq $userenv->{branch});
274                                 $select_branches{$branch} = $branches->{$branch}->{'branchname'} if ($branch eq $userenv->{branch});
275                                 $default = $userenv->{branch};
276                         }
277                 } else {
278                         push @select_branch, $branch;
279                         $select_branches{$branch} = $branches->{$branch}->{'branchname'};
280                         $default = $data->{'branchcode'};
281                 }
282         }
283         
284         my $CGIbranch=CGI::scrolling_list( -name     => 'branchcode',
285                                 -id => 'branchcode',
286                                 -values   => \@select_branch,
287                                 -default  => $data->{'branchcode'},
288                                 -labels   => \%select_branches,
289                                 -size     => 1,
290                                 -multiple => 0 );
291                                 
292
293         my $CGIsort1 = buildCGIsort("Bsort1","sort1",$data->{'sort1'});
294         if ($CGIsort1) {
295                 $template->param(CGIsort1 => $CGIsort1);
296         } else {
297                 $template->param( sort1 => $data->{'sort1'});
298         }
299
300         my $CGIsort2 = buildCGIsort("Bsort2","sort2",$data->{'sort2'});
301         if ($CGIsort2) {
302                 $template->param(CGIsort2 =>$CGIsort2);
303         } else {
304                 $template->param( sort2 => $data->{'sort2'});
305         }
306         
307         $template->param(       actionType              => $actionType,
308                                 destination => $destination,
309                                 borrowernumber          => $borrowernumber,
310                                 address         => $data->{'streetaddress'},
311                                 firstname       => $data->{'firstname'},
312                                 surname         => $data->{'surname'},
313                                 othernames      => $data->{'othernames'},
314                                 initials        => $data->{'initials'},
315                                 ethcatpopup     => $ethcatpopup,
316                                 catcodepopup    => $catcodepopup,
317                                 streetaddress   => $data->{'streetaddress'},
318                                 physstreet   => $data->{'physstreet'},
319                                 zipcode => $data->{'zipcode'},
320                                 streetcity      => $data->{'streetcity'},
321                                 homezipcode => $data->{'homezipcode'},
322                                 city            => $data->{'city'},
323                                 phone           => $data->{'phone'},
324                                 phoneday        => $data->{'phoneday'},
325                                 faxnumber       => $data->{'faxnumber'},
326                                 emailaddress    => $data->{'emailaddress'},
327                                 textmessaging   => $data->{'textmessaging'},
328                                 contactname     => $data->{'contactname'},
329                                 altphone        => $data->{'altphone'},
330                                 altnotes        => $data->{'altnotes'},
331                                 borrowernotes   => $data->{'borrowernotes'},
332                                 flagloop        => \@flagdata,
333                                 relshiploop     => \@relshipdata,
334                                 "title_".$data->{'title'} => " SELECTED ",
335                                 joining => format_date($data->{'dateenrolled'}),
336                                 expiry          => format_date($data->{'expiry'}),
337                                 cardnumber      => $cardnumber,
338                                 dateofbirth     => $data->{'dateofbirth'},
339                                 dateformat      => display_date_format(),
340                                 modify          => $modify,
341                                 CGIbranch => $CGIbranch);
342         $template->param(Institution => 1) if ($categorycode eq "I");
343         output_html_with_http_headers $input, $cookie, $template->output;
344
345
346 }
347
348 # Local Variables:
349 # tab-width: 8
350 # End: