modification: removed fieldset and label CSS properties. Modifications
[koha.git] / members / memberentry.pl
1 #!/usr/bin/perl
2 # $Id$
3
4 # Copyright 2006 SAN OUEST PROVENCE et Paul POULAIN
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 use strict;
22 use C4::Auth;
23 use C4::Context;
24 use C4::Output;
25 use C4::Interface::CGI::Output;
26 use CGI;
27 use C4::Search;
28 use C4::Members;
29 use C4::Koha;
30 use HTML::Template;
31 use Date::Manip;
32 use C4::Date;
33 use C4::Input;
34 use C4::Log;
35 use Digest::MD5 qw(md5_base64);
36
37 my $input = new CGI;
38 my %data;
39
40
41 my $dbh = C4::Context->dbh;
42
43 my $category_type = $input->param('category_type') || die "NO CATEGORY_TYPE !"; # A, E, C, or P
44 my $step=$input->param('step') || 0;
45 my ($template, $loggedinuser, $cookie)
46     = get_template_and_user({template_name => "members/memberentry$category_type.tmpl",
47                              query => $input,
48                              type => "intranet",
49                              authnotrequired => 0,
50                              flagsrequired => {borrowers => 1},
51                              debug => 1,
52                              });
53
54 my $borrowerid=$input->param('borrowerid');
55 my $guarantorid=$input->param('guarantorid');
56 my $modify=$input->param('modify');
57 my $delete=$input->param('delete');
58 my $op=$input->param('op');
59 my $categorycode=$input->param('categorycode');
60 my $destination=$input->param('destination');
61 my $cardnumber=$input->param('cardnumber');
62 my $check_member=$input->param('check_member');
63 my $name_city=$input->param('name_city');
64 my $nodouble=$input->param('nodouble');
65 my $select_city=$input->param('select_city');
66 my $nok=$input->param('nok');
67
68 # $check_categorytype contains the value of duplicate borrowers category type to redirect in good template in step =2
69 my $check_categorytype=$input->param('check_categorytype');
70 # NOTE: Alert for ethnicity and ethnotes fields, they are unvalided in all borrowers form
71
72
73 #function  to automatic setup the mandatory  fields (visual with css)
74 my $check_BorrowerMandatoryField=C4::Context->preference("BorrowerMandatoryField");
75 my @field_check=split(/\|/,$check_BorrowerMandatoryField);
76 foreach (@field_check) {
77 $template->param( "mandatory$_" => 1);          
78 }       
79
80 $template->param( "checked" => 1) if ($nodouble eq 1);
81
82 # if a add or modify is requested => check validity of data.
83 if ($op eq 'add' or $op eq 'modify') {
84         my @names=$input->param;
85         foreach my $key (@names){
86                 $data{$key}=$input->param($key)||'';
87                 $data{$key}=~ s/\'/\\\'/g;
88                 $data{$key}=~ s/\"/\\\"/g;
89         }
90         my @errors;
91         #############test for member being unique #############
92         if ($op eq 'add' && $step eq 2){
93                 (my $category_type_send=$category_type ) if ($category_type eq 'I'); 
94                 my $check_category; # recover the category code of the doublon suspect borrowers
95            ($check_member,$check_category)= checkuniquemember($category_type_send,$data{'surname'},$data{'firstname'},format_date_in_iso($data{'dateofbirth'}));
96 #       recover the category type if the borrowers is a duplicate
97         ($check_categorytype,undef)=getcategorytype($check_category);
98         }
99
100 # CHECKS step by step
101 # STEP 1
102         if ($step eq 1) {
103                 ###############test to take the right zipcode and city name ##############
104                 if ($category_type ne 'I' and $guarantorid){
105                         my ($borrower_city,$borrower_zipcode)=&getzipnamecity($select_city);
106                         $data{'city'}= $borrower_city;
107                         $data{'zipcode'}=$borrower_zipcode;
108                 }
109                 if ($category_type eq 'C' and $guarantorid){
110                         my $guarantordata=getguarantordata($guarantorid);
111                         if (($data{'contactname'} eq '' or $data{'contactname'} ne $guarantordata->{'surname'})) {
112                                 $data{'contactfirstname'}=$guarantordata->{'firstname'};        
113                                 $data{'contactname'}=$guarantordata->{'surname'};
114                                 $data{'contacttitle'}=$guarantordata->{'title'};
115                                 $data{'streetnumber'}=$guarantordata->{'streetnumber'};
116                                 $data{'address'}=$guarantordata->{'address'};
117                                 $data{'streettype'}=$guarantordata->{'streettype'};
118                                 $data{'address2'}=$guarantordata->{'address2'};
119                                 $data{'zipcode'}=$guarantordata->{'zipcode'};
120                                 $data{'city'}=$guarantordata->{'city'};
121                                 $data{'phone'}=$guarantordata->{'phone'};
122                                 $data{'phonepro'}=$guarantordata->{'phonepro'};
123                                 $data{'mobile'}=$guarantordata->{'mobile'};
124                                 $data{'fax'}=$guarantordata->{'fax'};
125                                 $data{'email'}=$guarantordata->{'email'};
126                                 $data{'emailpro'}=$guarantordata->{'emailpro'};
127                         }
128                 }
129         }
130 # STEP 2
131         if ($step eq 2) {
132                         if ( ($data{'login'} eq '')){
133                                 my $onefirstnameletter=substr($data{'firstname'},0,1);
134                                 my $fivesurnameletter=substr($data{'surname'},0,5);
135                                 $data{'login'}=lc($onefirstnameletter.$fivesurnameletter);
136                         }
137                         if ($op eq 'add' and $data{'dateenrolled'} eq ''){
138                                 my $today=today();
139                                 #insert ,in field "dateenrolled" , the current date
140                                 $data{'dateenrolled'}=$today;
141                                 #if date expiry is null u must calculate the value only in this case
142                                 $data{'dateexpiry'} = calcexpirydate($data{'categorycode'},$today);
143                         }
144                         if ($op eq 'modify' ){
145                         my $today=today();
146 #                       if date expiry is null u must calculate the value only in this case
147                         if ($data{'dateexpiry'} eq ''){
148                         $data{'dateexpiry'} = calcexpirydate($data{'categorycode'},$today);
149                         }
150                 }
151         }
152 # STEP 3
153         if ($step eq 3) {
154                 # this value show if the login and password are been used
155                 my $loginexist=checkuserpassword($borrowerid,$data{'login'},$data{'password'});
156                 # test to know if u must save or create the borrowers
157                 if ($op eq 'modify'){
158                         # test to know if another user have the same password and same login            
159                         if ($loginexist eq 0) {
160                                 &modmember(%data);              
161                                 logaction($loggedinuser,"MEMBERS","modify member", $borrowerid, "");
162                         }
163                         else {
164                                 push @errors, "ERROR_login_exist";
165                                 $nok=1;
166                         }
167                 }else{
168                         # test to know if another user have the same password and same login     
169                         if ($loginexist) {
170                                 push @errors, "ERROR_login_exist";
171                                 $nok=1;
172                         } else {
173                                 $borrowerid = &newmember(%data);
174                                 logaction($loggedinuser,"MEMBERS","add member", $borrowerid, "");
175                         }
176                 }
177                 
178                 if ($nok) {
179                         foreach my $error (@errors) {
180                                 $template->param( $error => 1);
181                         }
182                         $template->param(nok => 1);
183                         $step--; # decrease step : go back to step 2, the step++ just before showing the template will go again to 3
184                 }
185
186                 unless ($nok) {
187                         if($destination eq "circ"){
188                                 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$data{'cardnumber'}");
189                         } else {
190                                 if ($loginexist == 0) {
191                                 print $input->redirect("/cgi-bin/koha/members/moremember.pl?bornum=$borrowerid");
192                                 }
193                         }
194                 }
195         }
196         if (C4::Context->preference("IndependantBranches")) {
197                 my $userenv = C4::Context->userenv;
198                 if ($userenv->{flags} != 1){
199                         unless ($userenv->{branch} eq $data{'branchcode'}){
200                                 push @errors, "ERROR_branch";
201                                 $nok=1;
202                         }
203                 }
204         }
205 }
206
207 if ($delete){
208         print $input->redirect("/cgi-bin/koha/deletemem.pl?member=$borrowerid");
209 } else {  # this else goes down the whole script
210         # retrieve previous values : either in DB or in CGI, in case of errors in values
211         my $data;
212 # test to now if u add or modify a borrower (modify =>to take all carateristic of the borrowers)
213         if (!$op and !$data{'surname'}) {
214                 $data=borrdata('',$borrowerid);
215                 %data=%$data;
216         }
217         if (C4::Context->preference("IndependantBranches")) {
218                 my $userenv = C4::Context->userenv;
219                 if ($userenv->{flags} != 1 && $data{branchcode}){
220                         unless ($userenv->{branch} eq $data{'branchcode'}){
221                                 print $input->redirect("/cgi-bin/koha/members/members-home.pl");
222                         }
223                 }
224         }
225         if ($op eq 'add'){
226                 $template->param( updtype => 'I');
227         } else {
228                 $template->param( updtype => 'M');
229         }
230         my $cardnumber=$data{'cardnumber'};
231         $cardnumber=fixup_cardnumber($data{'cardnumber'}) if $op eq 'add';
232         if ($data{'sex'} eq 'F'){
233                 $template->param(female => 1);
234         }
235         my ($categories,$labels)=ethnicitycategories();
236         my $ethnicitycategoriescount=$#{$categories};
237         my $ethcatpopup;
238         if ($ethnicitycategoriescount>=0) {
239                 $ethcatpopup = CGI::popup_menu(-name=>'ethnicity',
240                                         -id => 'ethnicity',
241                                         -values=>$categories,
242                                         -default=>$data{'ethnicity'},
243                                         -labels=>$labels);
244                 $template->param(ethcatpopup => $ethcatpopup); # bad style, has to be fixed
245         }
246         
247         
248         ($categories,$labels)=borrowercategories($category_type,$op);
249         
250         #if u modify the borrowers u must have the right value for is category code
251         
252         (my $default_category=$data{'categorycode'}) if ($op  eq '');
253         my $catcodepopup = CGI::popup_menu(-name=>'categorycode',
254                                         -id => 'categorycode',
255                                         -values=>$categories,
256                                         -default=>$default_category,
257                                         -labels=>$labels);
258         #test in city
259         my $default_city;
260         if ($op eq ''){
261         (my $selectcity=&getidcity($data{'city'})) if ($select_city eq '');
262         $default_city=$selectcity;
263         }
264         my($cityid,$name_city)=getcities();
265         $template->param( city_cgipopup => 1) if ($cityid );
266         my $citypopup = CGI::popup_menu(-name=>'select_city',
267                                         -id => 'select_city',
268                                         -values=>$cityid,
269                                         -labels=>$name_city,
270 #                                       -override => 1,
271                                         -default=>$default_city
272                                         );      
273         
274         my $default_roadtype;
275         $default_roadtype=$data{'streettype'} ;
276         my($roadtypeid,$road_type)=getroadtypes();
277         $template->param( road_cgipopup => 1) if ($roadtypeid );
278         my $roadpopup = CGI::popup_menu(-name=>'streettype',
279                                         -id => 'streettype',
280                                         -values=>$roadtypeid,
281                                         -labels=>$road_type,
282                                         -override => 1,
283                                         -default=>$default_roadtype
284                                         );      
285
286         
287         my @relationships = split /,|\|/,C4::Context->preference('BorrowerRelationship');
288         my @relshipdata;
289         while (@relationships) {
290                 my $relship = shift @relationships || '';
291                 my %row = ('relationship' => $relship);
292                 if ($data{'relationship'} eq $relship) {
293                         $row{'selected'}=' selected';
294                 } else {
295                         $row{'selected'}='';
296                 }
297                 push(@relshipdata, \%row);
298         }
299         my %flags = ( 'gonenoaddress' => ['gonenoaddress', 'Adresse érronée'],
300                       'lost'          => ['lost', 'Carte Perdue'],
301                       'debarred'      => ['debarred', 'Lecteur exclu']);
302
303         my @flagdata;
304         foreach (keys(%flags)) {
305         my $key = $_;
306         my %row =  ('key'   => $key,
307                         'name'  => $flags{$key}[0],
308                         'html'  => $flags{$key}[1]);
309         if ($data{$key}) {
310                 $row{'yes'}=' checked';
311                 $row{'no'}='';
312         } else {
313                 $row{'yes'}='';
314                 $row{'no'}=' checked';
315         }
316         push(@flagdata, \%row);
317         }
318
319         if ($modify){
320         $template->param( modify => 1 );
321         }
322
323         #Convert dateofbirth to correct format
324         $data{'dateofbirth'} = format_date($data{'dateofbirth'});
325         my @branches;
326         my @select_branch;
327         my %select_branches;
328         my $branches=getbranches();
329         my $default;
330         # -----------------------------------------------------
331         #  the value of ip from the branches hash table
332                 my $select_ip;
333         # $ip is the ip of user when is connect to koha 
334                 my $ip = $ENV{'REMOTE_ADDR'};
335         # -----------------------------------------------------
336         foreach my $branch (keys %$branches) {
337                 if ((not C4::Context->preference("IndependantBranches")) || (C4::Context->userenv->{'flags'} == 1)) {
338                         push @select_branch, $branch;
339                         $select_branches{$branch} = $branches->{$branch}->{'branchname'};
340 #                take the ip number from branches "op"
341                         $select_ip = $branches->{$branch}->{'branchip'} || '';
342                                 
343 #               test $select_ip equal $ip to attribute the default value for the scrolling list
344                         if ($select_ip eq $ip)  {
345                                                 $default = $branches->{$branch}->{'branchcode'};
346                                                 }
347                         } else {
348                                 push @select_branch, $branch if ($branch eq C4::Context->userenv->{'branch'});
349                                 $select_branches{$branch} = $branches->{$branch}->{'branchname'} if ($branch eq C4::Context->userenv->{'branch'});
350                                         
351                                 $default = C4::Context->userenv->{'branch'};
352                                         
353                                 }
354         }
355 # --------------------------------------------------------------------------------------------------------
356         my $CGIbranch = CGI::scrolling_list(-id    => 'branchcode',
357                                            -name   => 'branchcode',
358                                            -values => \@select_branch,
359                                            -labels => \%select_branches,
360                                            -size   => 1,
361                                            -multiple =>0,
362                                            -override => 1,      
363                                            -default => $default,
364                                         );
365
366
367 # --------------------------------------------------------------------------------------------------------
368         
369         my $CGIsort1 = buildCGIsort("Bsort1","sort1",$data{'sort1'});
370         if ($CGIsort1) {
371                 $template->param(CGIsort1 => $CGIsort1);
372                 $template->param( sort1 => $data{'sort1'});
373         } else {
374                 $template->param( sort1 => $data{'sort1'});
375         }
376         
377         my $CGIsort2 = buildCGIsort("Bsort2","sort2",$data{'sort2'});
378         if ($CGIsort2) {
379                 $template->param(CGIsort2 =>$CGIsort2);
380         } else {
381                 $template->param( sort2 => $data{'sort2'});
382         }
383
384         
385         $data{'opacnotes'} =~ s/\\//g;
386         $data{'borrowernotes'} =~ s/\\//g;
387         # increase step to see next page
388         $step++;
389         warn "CITY".$data{city};
390         $template->param(
391                 BorrowerMandatoryField => C4::Context->preference("BorrowerMandatoryField"),#field to test with javascript
392                 category_type   => $category_type,#to know the category type of the borrower
393                 select_city     => $select_city,
394                 "step_$step"    => 1,# associate with step to know where u are
395                 step            => $step,
396                 destination     => $destination,#to know wher u come from and wher u must go in redirect
397                 check_member    => $check_member,#to know if the borrower already exist(=>1) or not (=>0) 
398 #                               flags           =>$data{'flags'},               
399                 "op$op"         => 1,
400 #               op                      => $op,
401                 nodouble        => $nodouble,
402                 borrowerid      => $borrowerid,#register number
403                 cardnumber      => $data{'cardnumber'},
404                 surname         => uc($data{'surname'}),
405                 firstname       => ucfirst($data{'firstname'}),
406                 "title_".$data{'title'}   => " SELECTED ",
407                 title           => $data{'title'},
408                 othernames      => $data{'othernames'},
409                 initials        => $data{'initials'},
410                 streetnumber    => $data{'streetnumber'},
411                 streettype      =>$data{'streettype'},
412                 address          => $data{'address'},
413                 address2        => $data{'address2'},   
414                 city            => $data{'city'},
415                 zipcode         => $data{'zipcode'},
416                 email           => $data{'email'},
417                 phone           => $data{'phone'},
418                 mobile          => $data{'mobile'},
419                 fax             => $data{'fax'},
420                 phonepro        => $data{'phonepro'},
421                 emailpro        => $data{'emailpro'},
422                 b_address       => $data{'b_address'},
423                 b_city          => $data{'b_city'},
424                 b_zipcode       => $data{'b_zipcode'},
425                 b_email         => $data{'b_email'},
426                 b_phone        => $data{'b_phone'},
427                 dateofbirth     => $data{'dateofbirth'},
428                 branchcode      => $data{'branchcode'},
429                 catcodepopup    => $catcodepopup,
430                 categorycode    => $data{'categorycode'},
431                 dateenrolled    => format_date($data{'dateenrolled'}),
432                 dateexpiry              => format_date($data{'dateexpiry'}),
433                 debarred        => $data{'debarred'},
434                 gonenoaddress   => $data{'gonenoaddress'}, 
435                 lost    => $data{'lost'},
436                 contactname     => uc($data{'contactname'}),
437                 contactfirstname=> ucfirst($data{'contactfirstname'}),
438                 "contacttitle_".$data{'contacttitle'} => "SELECTED" ,
439                 contacttitle    => $data{'contacttitle'},
440                 guarantorid     => $guarantorid,
441                 ethcatpopup     => $ethcatpopup,
442                 sex             => $data{'sex'},
443                 login           => $data{'login'},      
444                 password        => $data{'password'},   
445                 opacnotes       => $data{'opacnotes'},  
446                 contactnotes    => $data{'contactnotes'},
447                 borrowernotes   => $data{'borrowernotes'},
448                 relshiploop     => \@relshipdata,
449                 relationship    => $data{'relationship'},
450                 citypopup       => $citypopup,
451                 roadpopup       => $roadpopup,  
452                 contacttype     => $data{'contacttype'},
453                 flagloop        => \@flagdata,
454 #                               "contacttype_".$data{'contacttype'} =>" SELECTED ",
455                 dateformat      => display_date_format(),
456                 check_categorytype =>$check_categorytype,#to recover the category type with checkcategorytype function
457                         modify          => $modify,
458 #                               city_choice       => $city_choice ,#check if the city was selected
459                 nok             => $nok,#flag to konw if an error 
460                 CGIbranch => $CGIbranch,
461                 );
462         #$template->param(Institution => 1) if ($categorycode eq "I");
463         output_html_with_http_headers $input, $cookie, $template->output;
464 }
465 # Local Variables:
466 # tab-width: 8
467 # End: