css'ing & merging members management.
[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
62 my $nok;
63 # if a add or modify is requested => check validity of data.
64 if ($op eq 'add' or $op eq 'modify') {
65         my %data;
66         my @names=$input->param;
67         foreach my $key (@names){
68                 $data{$key}=$input->param($key);
69                 $data{$key}=~ s/\'/\\\'/g;
70                 $data{$key}=~ s/\"/\\\"/g;
71         }
72         my @errors;
73         if ($data{'cardnumber'} eq ''){
74                 push @errors,"ERROR_cardnumber";
75                 $nok=1;
76         } else {
77                 #check cardnumber is valid
78                 my $nounique;
79                 if ( $data{'actionType'} eq "Add" )    {
80                         $nounique = 0;
81                 } else {
82                         $nounique = 1;
83                 }
84                 my $valid=checkdigit('',$data{'cardnumber'}, $nounique);
85                 if ($valid != 1){
86                         $nok=1;
87                         push @errors, "ERROR_invalid_cardnumber";
88                 }
89         }
90         if ($data{'sex'} eq '' && $categorycode ne "I"){
91                 push @errors, "ERROR_gender";
92                 $nok=1;
93         }
94         if ($data{'firstname'} eq '' && $categorycode ne "I"){
95                 push @errors,"ERROR_firstname";
96                 $nok=1;
97         }
98         if ($data{'surname'} eq ''){
99                 push @errors,"ERROR_surname";
100                 $nok=1;
101         }
102         if ($data{'address'} eq ''){
103                 push @errors, "ERROR_address";
104                 $nok=1;
105         }
106         if ($data{'city'} eq ''){
107                 push @errors, "ERROR_city";
108                 $nok=1;
109         }
110         if ($nok) {
111                 foreach my $error (@errors) {
112                         $template->param( $error => 1);
113                 }
114                 $template->param(nok => 1);
115         } else {
116                 my $query="Select * from borrowers where borrowernumber=?";
117                 my $sth=$dbh->prepare($query);
118                 $sth->execute($data{'borrowernumber'});
119                 if (my $data2=$sth->fetchrow_hashref){
120                         $data{'dateofbirth'}=format_date_in_iso($data{'dateofbirth'});
121                         $data{'joining'}=format_date_in_iso($data{'joining'});
122                         $data{'expiry'}=format_date_in_iso($data{'expiry'});
123                         $query="update borrowers set title='$data{'title'}',expiry='$data{'expiry'}',
124                         cardnumber='$data{'cardnumber'}',sex='$data{'sex'}',ethnotes='$data{'ethnicnotes'}',
125                         streetaddress='$data{'address'}',faxnumber='$data{'faxnumber'}',firstname='$data{'firstname'}',
126                         altnotes='$data{'altnotes'}',dateofbirth='$data{'dateofbirth'}',contactname='$data{'contactname'}',
127                         emailaddress='$data{'emailaddress'}',dateenrolled='$data{'joining'}',streetcity='$data{'streetcity'}',
128                         altrelationship='$data{'altrelationship'}',othernames='$data{'othernames'}',phoneday='$data{'phoneday'}',
129                         categorycode='$data{'categorycode'}',city='$data{'city'}',area='$data{'area'}',phone='$data{'phone'}',
130                         borrowernotes='$data{'borrowernotes'}',altphone='$data{'altphone'}',surname='$data{'surname'}',
131                         initials='$data{'initials'}',physstreet='$data{'streetaddress'}',ethnicity='$data{'ethnicity'}',
132                         gonenoaddress='$data{'gna'}',lost='$data{'lost'}',debarred='$data{'debarred'}',
133                         textmessaging='$data{'textmessaging'}', branchcode = '$data{'branchcode'}',
134                         zipcode = '$data{'zipcode'}',homezipcode='$data{'homezipcode'}'
135                         where borrowernumber=$data{'borrowernumber'}";
136                 }else{
137                         $data{'dateofbirth'}=format_date_in_iso($data{'dateofbirth'});
138                         $data{'joining'}=format_date_in_iso($data{'joining'});
139                         $data{'expiry'}=format_date_in_iso($data{'expiry'});
140                         $data{'borrowernumber'}=NewBorrowerNumber();
141                         $query="insert into borrowers (title,expiry,cardnumber,sex,ethnotes,streetaddress,faxnumber,
142                         firstname,altnotes,dateofbirth,contactname,emailaddress,textmessaging,dateenrolled,streetcity,
143                         altrelationship,othernames,phoneday,categorycode,city,area,phone,borrowernotes,altphone,surname,
144                         initials,ethnicity,physstreet,branchcode,zipcode,homezipcode) values ('$data{'title'}','$data{'expiry'}','$data{'cardnumber'}',
145                         '$data{'sex'}','$data{'ethnotes'}','$data{'address'}','$data{'faxnumber'}',
146                         '$data{'firstname'}','$data{'altnotes'}','$data{'dateofbirth'}','$data{'contactname'}','$data{'emailaddress'}','$data{'textmessaging'}',
147                         '$data{'joining'}','$data{'streetcity'}','$data{'altrelationship'}','$data{'othernames'}',
148                         '$data{'phoneday'}','$data{'categorycode'}','$data{'city'}','$data{'area'}','$data{'phone'}',
149                         '$data{'borrowernotes'}','$data{'altphone'}','$data{'surname'}','$data{'initials'}',
150                         '$data{'ethnicity'}','$data{'streetaddress'}','$data{'branchcode'}','$data{'zipcode'}','$data{'homezipcode'}')";
151                 }
152                 # ok if its an adult (type) it may have borrowers that depend on it as a guarantor
153                 # so when we update information for an adult we should check for guarantees and update the relevant part
154                 # of their records, ie addresses and phone numbers
155                 if ($data{'categorycode'} eq 'A' || $data{'categorycode'} eq 'W'){
156                         # is adult check guarantees;
157                         my ($count,$guarantees)=findguarantees($data{'borrowernumber'});
158                         for (my $i=0;$i<$count;$i++){
159                                 # FIXME
160                                 # It looks like the $i is only being returned to handle walking through
161                                 # the array, which is probably better done as a foreach loop.
162                                 #
163                                 my $guaquery="update borrowers set streetaddress='$data{'address'}',faxnumber='$data{'faxnumber'}',
164                                 streetcity='$data{'streetcity'}',phoneday='$data{'phoneday'}',city='$data{'city'}',area='$data{'area'}',phone='$data{'phone'}'
165                                 ,streetaddress='$data{'address'}'
166                                 where borrowernumber='$guarantees->[$i]->{'borrowernumber'}'";
167                                 my $sth3=$dbh->prepare($guaquery);
168                                 $sth3->execute;
169                                 $sth3->finish;
170                         }
171                 }
172                 my $sth2=$dbh->prepare($query);
173                 $sth2->execute;
174                 $sth2->finish;
175                 $sth->finish;
176                 print $input->redirect("/cgi-bin/koha/members/moremember.pl?bornum=$data{'borrowernumber'}");   }
177 }
178 if ($delete){
179         print $input->redirect("/cgi-bin/koha/deletemem.pl?member=$member");
180 } else {  # this else goes down the whole script
181         if ($actionType eq 'Add'){
182                 $template->param( addAction => 1);
183         } else {
184                 $template->param( addAction =>0);
185         }
186         # retrieve previous values : either in DB or in CGI, in case of errors in values
187         my $data;
188         if ($nok) {
189                 my @names=$input->param;
190                 foreach my $key (@names){
191                         $data->{$key}=$input->param($key);
192                 }
193         } else {
194                 $data=borrdata('',$member);
195         }
196         if ($actionType eq 'Add'){
197                 $template->param( updtype => 'I');
198         } else {
199                 $template->param( updtype => 'M');
200         }
201         my $cardnumber=C4::Members::fixup_cardnumber($data->{'cardnumber'});
202         if ($data->{'sex'} eq 'F'){
203                 $template->param(female => 1);
204         }
205         my ($categories,$labels)=ethnicitycategories();
206         my $ethnicitycategoriescount=$#{$categories};
207         my $ethcatpopup;
208         if ($ethnicitycategoriescount>=0) {
209                 $ethcatpopup = CGI::popup_menu(-name=>'ethnicity',
210                                         -id => 'ethnicity',
211                                         -values=>$categories,
212                                         -default=>$data->{'ethnicity'},
213                                         -labels=>$labels);
214                 $template->param(ethcatpopup => $ethcatpopup); # bad style, has to be fixed
215         }
216
217         ($categories,$labels)=borrowercategories();
218         my $catcodepopup = CGI::popup_menu(-name=>'categorycode',
219                                         -id => 'categorycode',
220                                         -values=>$categories,
221                                         -default=>$data->{'categorycode'},
222                                         -labels=>$labels);
223
224         my @relationships = ('workplace', 'relative','friend', 'neighbour');
225         my @relshipdata;
226         while (@relationships) {
227                 my $relship = shift @relationships;
228                 my %row = ('relationship' => $relship);
229                 if ($data->{'altrelationship'} eq $relship) {
230                         $row{'selected'}=' selected';
231                 } else {
232                         $row{'selected'}='';
233                 }
234                 push(@relshipdata, \%row);
235         }
236
237         # %flags: keys=$data-keys, datas=[formname, HTML-explanation]
238         my %flags = ('gonenoaddress' => ['gna', 'Gone no address'],
239                                 'lost'          => ['lost', 'Lost'],
240                                 'debarred'      => ['debarred', 'Debarred']);
241
242         my @flagdata;
243         foreach (keys(%flags)) {
244         my $key = $_;
245         my %row =  ('key'   => $key,
246                         'name'  => $flags{$key}[0],
247                         'html'  => $flags{$key}[1]);
248         if ($data->{$key}) {
249                 $row{'yes'}=' checked';
250                 $row{'no'}='';
251         } else {
252                 $row{'yes'}='';
253                 $row{'no'}=' checked';
254         }
255         push(@flagdata, \%row);
256         }
257
258         if ($modify){
259         $template->param( modify => 1 );
260         }
261
262         #Convert dateofbirth to correct format
263         $data->{'dateofbirth'} = format_date($data->{'dateofbirth'});
264
265         my @branches;
266         my @select_branch;
267         my %select_branches;
268         my $branches=getbranches();
269         foreach my $branch (keys %$branches) {
270                 push @select_branch, $branch;
271                 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
272         }
273         my $CGIbranch=CGI::scrolling_list( -name     => 'branchcode',
274                                 -id => 'branchcode',
275                                 -values   => \@select_branch,
276                                 -default  => $data->{'branchcode'},
277                                 -labels   => \%select_branches,
278                                 -size     => 1,
279                                 -multiple => 0 );
280
281         $template->param(       actionType              => $actionType,
282                                 member          => $member,
283                                 address         => $data->{'streetaddress'},
284                                 firstname       => $data->{'firstname'},
285                                 surname         => $data->{'surname'},
286                                 othernames      => $data->{'othernames'},
287                                 initials        => $data->{'initials'},
288                                 ethcatpopup     => $ethcatpopup,
289                                 catcodepopup    => $catcodepopup,
290                                 streetaddress   => $data->{'physstreet'},
291                                 zipcode => $data->{'zipcode'},
292                                 streetcity      => $data->{'streetcity'},
293                                 homezipcode => $data->{'homezipcode'},
294                                 city            => $data->{'city'},
295                                 phone           => $data->{'phone'},
296                                 phoneday        => $data->{'phoneday'},
297                                 faxnumber       => $data->{'faxnumber'},
298                                 emailaddress    => $data->{'emailaddress'},
299                                 textmessaging   => $data->{'textmessaging'},
300                                 contactname     => $data->{'contactname'},
301                                 altphone        => $data->{'altphone'},
302                                 altnotes        => $data->{'altnotes'},
303                                 borrowernotes   => $data->{'borrowernotes'},
304                                 flagloop        => \@flagdata,
305                                 relshiploop     => \@relshipdata,
306                                 "title_".$data->{'title'} => " SELECTED ",
307                                 dateenrolled    => $data->{'dateenrolled'},
308                                 expiry          => $data->{'expiry'},
309                                 cardnumber      => $cardnumber,
310                                 dateofbirth     => $data->{'dateofbirth'},
311                                 dateformat      => display_date_format(),
312                                 modify          => $modify,
313                                 CGIbranch => $CGIbranch);
314         $template->param(Institution => 1) if ($categorycode eq "I");
315         output_html_with_http_headers $input, $cookie, $template->output;
316
317
318 }
319
320 # Local Variables:
321 # tab-width: 8
322 # End: