porting 22 fixes to head
[koha_fer] / 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 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
92         if ($data{'sex'} eq '' && $categorycode ne "I"){
93                 push @errors, "ERROR_gender";
94                 $nok=1;
95         }
96         if ($data{'firstname'} eq '' && $categorycode ne "I"){
97                 push @errors,"ERROR_firstname";
98                 $nok=1;
99         }
100         if ($data{'surname'} eq ''){
101                 push @errors,"ERROR_surname";
102                 $nok=1;
103         }
104         if ($data{'streetaddress'} eq ''){
105                 push @errors, "ERROR_address";
106                 $nok=1;
107         }
108         if ($data{'city'} eq ''){
109                 push @errors, "ERROR_city";
110                 $nok=1;
111         }
112         if (C4::Context->preference("IndependantBranches")) {
113                 my $userenv = C4::Context->userenv;
114                 if ($userenv->{flags} == 1){
115                         unless ($userenv->{branch} eq $data{'branchcode'}){
116                                 push @errors, "ERROR_branch";
117                                 $nok=1;
118                         }
119                 }
120         }
121         if ($nok) {
122                 foreach my $error (@errors) {
123                         $template->param( $error => 1);
124                 }
125                 $template->param(nok => 1);
126         } else {
127                 my $query="Select * from borrowers where borrowernumber=?";
128                 my $sth=$dbh->prepare($query);
129                 $sth->execute($data{'borrowernumber'});
130                 if (my $data2=$sth->fetchrow_hashref){
131                         &modmember(%data);
132                 }else{
133                         $borrowernumber = &newmember(%data);
134                 }
135                 
136         if($destination eq "circ"){
137                 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$data{'cardnumber'}");
138         } else {
139                 print $input->redirect("/cgi-bin/koha/members/moremember.pl?bornum=$borrowernumber");
140                 }
141         }
142 }
143 if ($delete){
144         print $input->redirect("/cgi-bin/koha/deletemem.pl?member=$borrowernumber");
145 } else {  # this else goes down the whole script
146         if ($actionType eq 'Add'){
147                 $template->param( addAction => 1);
148         } else {
149                 $template->param( addAction =>0);
150         }
151         # retrieve previous values : either in DB or in CGI, in case of errors in values
152         my $data;
153         if ($nok) {
154                 my @names=$input->param;
155                 foreach my $key (@names){
156                         $data->{$key}=$input->param($key);
157                 }
158         } else {
159                 $data=borrdata('',$borrowernumber);
160         }
161         if (C4::Context->preference("IndependantBranches")) {
162                 my $userenv = C4::Context->userenv;
163                 if ($userenv->{flags} != 1 && $data->{branchcode}){
164                         unless ($userenv->{branch} eq $data->{'branchcode'}){
165                                 print $input->redirect("/cgi-bin/koha/members/members-home.pl");
166                         }
167                 }
168         }
169         if ($actionType eq 'Add'){
170                 $template->param( updtype => 'I');
171         } else {
172                 $template->param( updtype => 'M');
173         }
174         my $cardnumber=C4::Members::fixup_cardnumber($data->{'cardnumber'});
175         if ($data->{'sex'} eq 'F'){
176                 $template->param(female => 1);
177         }
178         my ($categories,$labels)=ethnicitycategories();
179         my $ethnicitycategoriescount=$#{$categories};
180         my $ethcatpopup;
181         if ($ethnicitycategoriescount>=0) {
182                 $ethcatpopup = CGI::popup_menu(-name=>'ethnicity',
183                                         -id => 'ethnicity',
184                                         -values=>$categories,
185                                         -default=>$data->{'ethnicity'},
186                                         -labels=>$labels);
187                 $template->param(ethcatpopup => $ethcatpopup); # bad style, has to be fixed
188         }
189
190         ($categories,$labels)=borrowercategories();
191         my $catcodepopup = CGI::popup_menu(-name=>'categorycode',
192                                         -id => 'categorycode',
193                                         -values=>$categories,
194                                         -default=>$data->{'categorycode'},
195                                         -labels=>$labels);
196
197         my @relationships = ('','workplace', 'relative','friend', 'neighbour');
198         my @relshipdata;
199         while (@relationships) {
200                 my $relship = shift @relationships;
201                 my %row = ('relationship' => $relship);
202                 if ($data->{'altrelationship'} eq $relship) {
203                         $row{'selected'}=' selected';
204                 } else {
205                         $row{'selected'}='';
206                 }
207                 push(@relshipdata, \%row);
208         }
209
210         # %flags: keys=$data-keys, datas=[formname, HTML-explanation]
211         my %flags = ('gonenoaddress' => ['gna', 'Gone no address'],
212                                 'lost'          => ['lost', 'Lost'],
213                                 'debarred'      => ['debarred', 'Debarred']);
214
215         my @flagdata;
216         foreach (keys(%flags)) {
217         my $key = $_;
218         my %row =  ('key'   => $key,
219                         'name'  => $flags{$key}[0],
220                         'html'  => $flags{$key}[1]);
221         if ($data->{$key}) {
222                 $row{'yes'}=' checked';
223                 $row{'no'}='';
224         } else {
225                 $row{'yes'}='';
226                 $row{'no'}=' checked';
227         }
228         push(@flagdata, \%row);
229         }
230
231         if ($modify){
232         $template->param( modify => 1 );
233         }
234
235         #Convert dateofbirth to correct format
236         $data->{'dateofbirth'} = format_date($data->{'dateofbirth'});
237
238         my @branches;
239         my @select_branch;
240         my %select_branches;
241         my $branches=getbranches();
242         my $default;
243         foreach my $branch (keys %$branches) {
244                 if (C4::Context->preference("IndependantBranches")) {
245                         my $userenv = C4::Context->userenv;
246                         if ($userenv->{flags} == 1){
247                                 push @select_branch, $branch;
248                                 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
249                                 $default = $data->{'branchcode'};
250                         } else {
251                                 push @select_branch, $branch if ($branch eq $userenv->{branch});
252                                 $select_branches{$branch} = $branches->{$branch}->{'branchname'} if ($branch eq $userenv->{branch});
253                                 $default = $userenv->{branch};
254                         }
255                 } else {
256                         push @select_branch, $branch;
257                         $select_branches{$branch} = $branches->{$branch}->{'branchname'};
258                         $default = $data->{'branchcode'};
259                 }
260         }
261         
262         my $CGIbranch=CGI::scrolling_list( -name     => 'branchcode',
263                                 -id => 'branchcode',
264                                 -values   => \@select_branch,
265                                 -default  => $data->{'branchcode'},
266                                 -labels   => \%select_branches,
267                                 -size     => 1,
268                                 -multiple => 0 );
269         
270         my $CGIsort1 = buildCGIsort("Bsort1","sort1",$data->{'sort1'});
271         if ($CGIsort1) {
272                 $template->param(CGIsort1 => $CGIsort1);
273         } else {
274                 $template->param( sort1 => $data->{'sort1'});
275         }
276         
277         my $CGIsort2 = buildCGIsort("Bsort2","sort2",$data->{'sort2'});
278         if ($CGIsort2) {
279                 $template->param(CGIsort2 =>$CGIsort2);
280         } else {
281                 $template->param( sort2 => $data->{'sort2'});
282         }
283
284         my $CGIsort1 = buildCGIsort("Bsort1","sort1",$data->{'sort1'});
285         if ($CGIsort1) {
286                 $template->param(CGIsort1 => $CGIsort1);
287         } else {
288                 $template->param( sort1 => $data->{'sort1'});
289         }
290
291         my $CGIsort2 = buildCGIsort("Bsort2","sort2",$data->{'sort2'});
292         if ($CGIsort2) {
293                 $template->param(CGIsort2 =>$CGIsort2);
294         } else {
295                 $template->param( sort2 => $data->{'sort2'});
296         }
297         
298         $template->param(       actionType              => $actionType,
299                                 destination => $destination,
300                                 borrowernumber          => $borrowernumber,
301                                 address         => $data->{'streetaddress'},
302                                 firstname       => $data->{'firstname'},
303                                 surname         => $data->{'surname'},
304                                 othernames      => $data->{'othernames'},
305                                 initials        => $data->{'initials'},
306                                 ethcatpopup     => $ethcatpopup,
307                                 catcodepopup    => $catcodepopup,
308                                 streetaddress   => $data->{'streetaddress'},
309                                 physstreet   => $data->{'physstreet'},
310                                 zipcode => $data->{'zipcode'},
311                                 streetcity      => $data->{'streetcity'},
312                                 homezipcode => $data->{'homezipcode'},
313                                 city            => $data->{'city'},
314                                 phone           => $data->{'phone'},
315                                 phoneday        => $data->{'phoneday'},
316                                 faxnumber       => $data->{'faxnumber'},
317                                 emailaddress    => $data->{'emailaddress'},
318                                 textmessaging   => $data->{'textmessaging'},
319                                 contactname     => $data->{'contactname'},
320                                 altphone        => $data->{'altphone'},
321                                 altnotes        => $data->{'altnotes'},
322                                 borrowernotes   => $data->{'borrowernotes'},
323                                 flagloop        => \@flagdata,
324                                 relshiploop     => \@relshipdata,
325                                 "title_".$data->{'title'} => " SELECTED ",
326                                 joining => format_date($data->{'dateenrolled'}),
327                                 expiry          => format_date($data->{'expiry'}),
328                                 cardnumber      => $cardnumber,
329                                 dateofbirth     => $data->{'dateofbirth'},
330                                 dateformat      => display_date_format(),
331                                 modify          => $modify,
332                                 CGIbranch => $CGIbranch);
333         $template->param(Institution => 1) if ($categorycode eq "I");
334         output_html_with_http_headers $input, $cookie, $template->output;
335
336
337 }
338
339 # Local Variables:
340 # tab-width: 8
341 # End: