merging 2.2 branch with head. Sorry for not making it before, many many commits done...
[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 $member=$input->param('bornum');
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 my $nok;
64 # if a add or modify is requested => check validity of data.
65 if ($op eq 'add' or $op eq 'modify') {
66         my %data;
67         my @names=$input->param;
68         foreach my $key (@names){
69                 $data{$key}=$input->param($key);
70                 $data{$key}=~ s/\'/\\\'/g;
71                 $data{$key}=~ s/\"/\\\"/g;
72         }
73         my @errors;
74         if ($data{'cardnumber'} eq ''){
75                 push @errors,"ERROR_cardnumber";
76                 $nok=1;
77         } else {
78                 #check cardnumber is valid
79                 my $nounique;
80                 if ( $op eq "add" )    {
81                         $nounique = 0;
82                 } else {
83                         $nounique = 1;
84                 }
85                 my $valid=checkdigit('',$data{'cardnumber'}, $nounique);
86                 if ($valid != 1){
87                         $nok=1;
88                         push @errors, "ERROR_invalid_cardnumber";
89                 }
90         }
91         if ($data{'sex'} eq '' && $categorycode ne "I"){
92                 push @errors, "ERROR_gender";
93                 $nok=1;
94         }
95         if ($data{'firstname'} eq '' && $categorycode ne "I"){
96                 push @errors,"ERROR_firstname";
97                 $nok=1;
98         }
99         if ($data{'surname'} eq ''){
100                 push @errors,"ERROR_surname";
101                 $nok=1;
102         }
103         if ($data{'address'} eq ''){
104                 push @errors, "ERROR_address";
105                 $nok=1;
106         }
107         if ($data{'city'} eq ''){
108                 push @errors, "ERROR_city";
109                 $nok=1;
110         }
111         if ($nok) {
112                 foreach my $error (@errors) {
113                         $template->param( $error => 1);
114                 }
115                 $template->param(nok => 1);
116         } else {
117                 my $query="Select * from borrowers where borrowernumber=?";
118                 my $sth=$dbh->prepare($query);
119                 $sth->execute($data{'borrowernumber'});
120                 if (my $data2=$sth->fetchrow_hashref){
121                         &modmember(%data);
122                 }else{
123                         $data{borrowernumber} = &newmember(%data);
124                 }
125                 
126         if($destination eq "circ"){
127                 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$data{'cardnumber'}");
128         } else {
129                 print $input->redirect("/cgi-bin/koha/members/moremember.pl?bornum=$data{'borrowernumber'}");
130                 }
131         }
132 }
133 if ($delete){
134         print $input->redirect("/cgi-bin/koha/deletemem.pl?member=$member");
135 } else {  # this else goes down the whole script
136         if ($actionType eq 'Add'){
137                 $template->param( addAction => 1);
138         } else {
139                 $template->param( addAction =>0);
140         }
141         # retrieve previous values : either in DB or in CGI, in case of errors in values
142         my $data;
143         if ($nok) {
144                 my @names=$input->param;
145                 foreach my $key (@names){
146                         $data->{$key}=$input->param($key);
147                 }
148         } else {
149                 $data=borrdata('',$member);
150         }
151         if ($actionType eq 'Add'){
152                 $template->param( updtype => 'I');
153         } else {
154                 $template->param( updtype => 'M');
155         }
156         my $cardnumber=C4::Members::fixup_cardnumber($data->{'cardnumber'});
157         if ($data->{'sex'} eq 'F'){
158                 $template->param(female => 1);
159         }
160         my ($categories,$labels)=ethnicitycategories();
161         my $ethnicitycategoriescount=$#{$categories};
162         my $ethcatpopup;
163         if ($ethnicitycategoriescount>=0) {
164                 $ethcatpopup = CGI::popup_menu(-name=>'ethnicity',
165                                         -id => 'ethnicity',
166                                         -values=>$categories,
167                                         -default=>$data->{'ethnicity'},
168                                         -labels=>$labels);
169                 $template->param(ethcatpopup => $ethcatpopup); # bad style, has to be fixed
170         }
171
172         ($categories,$labels)=borrowercategories();
173         my $catcodepopup = CGI::popup_menu(-name=>'categorycode',
174                                         -id => 'categorycode',
175                                         -values=>$categories,
176                                         -default=>$data->{'categorycode'},
177                                         -labels=>$labels);
178
179         my @relationships = ('','workplace', 'relative','friend', 'neighbour');
180         my @relshipdata;
181         while (@relationships) {
182                 my $relship = shift @relationships;
183                 my %row = ('relationship' => $relship);
184                 if ($data->{'altrelationship'} eq $relship) {
185                         $row{'selected'}=' selected';
186                 } else {
187                         $row{'selected'}='';
188                 }
189                 push(@relshipdata, \%row);
190         }
191
192         # %flags: keys=$data-keys, datas=[formname, HTML-explanation]
193         my %flags = ('gonenoaddress' => ['gna', 'Gone no address'],
194                                 'lost'          => ['lost', 'Lost'],
195                                 'debarred'      => ['debarred', 'Debarred']);
196
197         my @flagdata;
198         foreach (keys(%flags)) {
199         my $key = $_;
200         my %row =  ('key'   => $key,
201                         'name'  => $flags{$key}[0],
202                         'html'  => $flags{$key}[1]);
203         if ($data->{$key}) {
204                 $row{'yes'}=' checked';
205                 $row{'no'}='';
206         } else {
207                 $row{'yes'}='';
208                 $row{'no'}=' checked';
209         }
210         push(@flagdata, \%row);
211         }
212
213         if ($modify){
214         $template->param( modify => 1 );
215         }
216
217         #Convert dateofbirth to correct format
218         $data->{'dateofbirth'} = format_date($data->{'dateofbirth'});
219
220         my @branches;
221         my @select_branch;
222         my %select_branches;
223         my $branches=getbranches();
224         foreach my $branch (keys %$branches) {
225                 push @select_branch, $branch;
226                 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
227         }
228         my $CGIbranch=CGI::scrolling_list( -name     => 'branchcode',
229                                 -id => 'branchcode',
230                                 -values   => \@select_branch,
231                                 -default  => $data->{'branchcode'},
232                                 -labels   => \%select_branches,
233                                 -size     => 1,
234                                 -multiple => 0 );
235
236         $template->param(       actionType              => $actionType,
237                                 destination => $destination,
238                                 member          => $member,
239                                 address         => $data->{'streetaddress'},
240                                 firstname       => $data->{'firstname'},
241                                 surname         => $data->{'surname'},
242                                 othernames      => $data->{'othernames'},
243                                 initials        => $data->{'initials'},
244                                 ethcatpopup     => $ethcatpopup,
245                                 catcodepopup    => $catcodepopup,
246                                 streetaddress   => $data->{'physstreet'},
247                                 zipcode => $data->{'zipcode'},
248                                 streetcity      => $data->{'streetcity'},
249                                 homezipcode => $data->{'homezipcode'},
250                                 city            => $data->{'city'},
251                                 phone           => $data->{'phone'},
252                                 phoneday        => $data->{'phoneday'},
253                                 faxnumber       => $data->{'faxnumber'},
254                                 emailaddress    => $data->{'emailaddress'},
255                                 textmessaging   => $data->{'textmessaging'},
256                                 contactname     => $data->{'contactname'},
257                                 altphone        => $data->{'altphone'},
258                                 altnotes        => $data->{'altnotes'},
259                                 borrowernotes   => $data->{'borrowernotes'},
260                                 flagloop        => \@flagdata,
261                                 relshiploop     => \@relshipdata,
262                                 "title_".$data->{'title'} => " SELECTED ",
263                                 dateenrolled    => $data->{'dateenrolled'},
264                                 expiry          => $data->{'expiry'},
265                                 cardnumber      => $cardnumber,
266                                 dateofbirth     => $data->{'dateofbirth'},
267                                 sort1 => $data->{'sort1'},
268                                 sort2 => $data->{'sort2'},
269                                 dateformat      => display_date_format(),
270                                 modify          => $modify,
271                                 CGIbranch => $CGIbranch);
272         $template->param(Institution => 1) if ($categorycode eq "I");
273         output_html_with_http_headers $input, $cookie, $template->output;
274
275
276 }
277
278 # Local Variables:
279 # tab-width: 8
280 # End: