3 # Copyright 2006 SAN OUEST PROVENCE et Paul POULAIN
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
25 # use Digest::MD5 qw(md5_base64);
33 use C4
::Dates qw
/format_date format_date_in_iso/;
36 use C4
::Branch
; # GetBranches
43 $debug = $ENV{DEBUG} || 0;
47 ($debug) or $debug = $input->param('debug') || 0;
50 my $dbh = C4::Context->dbh;
52 my ($template, $loggedinuser, $cookie)
53 = get_template_and_user({template_name => "members/memberentrygen.tmpl",
57 flagsrequired => {borrowers => 1},
58 debug => ($debug) ? 1 : 0,
60 my $guarantorid=$input->param('guarantorid');
61 my $borrowernumber=$input->param('borrowernumber');
62 my $actionType=$input->param('actionType') || '';
63 my $modify=$input->param('modify');
64 my $delete=$input->param('delete');
65 my $op=$input->param('op');
66 my $destination=$input->param('destination');
67 my $cardnumber=$input->param('cardnumber');
68 my $check_member=$input->param('check_member');
69 my $name_city=$input->param('name_city');
70 my $nodouble=$input->param('nodouble');
71 my $select_city=$input->param('select_city');
72 my $zipcode=$input->param('zipcode');
73 my $city=$input->param('city');
74 my $nok=$input->param('nok');
75 my $guarantorinfo=$input->param('guarantorinfo');
76 my $step=$input->param('step') || 0;
79 # $check_categorytype contains the value of duplicate borrowers category type to redirect in good template in step =2
80 my $check_categorytype=$input->param('check_categorytype');
81 # NOTE: Alert for ethnicity and ethnotes fields, they are invalid in all borrowers form
84 my $userenv = C4::Context->userenv;
86 $template->param("uppercasesurnames" => C4::Context->preference('uppercasesurnames'));
88 # function to designate mandatory fields (visually with css)
89 my $check_BorrowerMandatoryField=C4::Context->preference("BorrowerMandatoryField");
90 my @field_check=split(/\|/,$check_BorrowerMandatoryField);
91 foreach (@field_check) {
92 $template->param( "mandatory$_" => 1);
94 $template->param("add"=>1) if ($op eq 'add');
95 $template->param("checked" => 1) if ($nodouble eq 1);
96 ($borrower_data = GetMember($borrowernumber,'borrowernumber')) if ($op eq 'modify' or $op eq 'save');
97 my $categorycode = $input->param('categorycode') || $borrower_data->{'categorycode'};
98 my $category_type = $input->param('category_type');
99 my $new_c_type = $category_type; #if we have input param, then we've already chosen the cat_type.
100 unless ($category_type or !($categorycode)){
101 my $borrowercategory= GetBorrowercategory($categorycode);
102 $category_type = $borrowercategory->{'category_type'};
104 $category_type="A" unless $category_type; # FIXME we should display a error message instead of a 500 error !
106 # if a add or modify is requested => check validity of data.
107 %data = %$borrower_data if ($borrower_data);
109 my %newdata; # comes from $input->param()
110 if ($op eq 'insert' || $op eq 'modify' || $op eq 'save') {
111 my @names= ($borrower_data && $op ne 'save') ? keys %$borrower_data : $input->param();
112 foreach my $key (@names) {
113 $newdata{$key} = $input->param($key) if (defined $input->param($key));
114 $newdata{$key} =~ s/\"/"/gg unless $key eq 'borrowernotes' or $key eq 'opacnote';
116 my $dateobject = C4::Dates->new();
117 my $syspref = $dateobject->regexp(); # same syspref format for all 3 dates
118 my $iso = $dateobject->regexp('iso'); #
119 foreach (qw(dateenrolled dateexpiry dateofbirth)) {
120 my $userdate = $newdata{$_} or next;
121 if ($userdate =~ /$syspref/) {
122 $newdata{$_} = format_date_in_iso
($userdate); # if they match syspref format, then convert to ISO
123 } elsif ($userdate =~ /$iso/) {
124 warn "Date $_ ($userdate) is already in ISO format";
126 ($userdate eq '0000-00-00') and warn "Data error: $_ is '0000-00-00'";
127 $template->param( "ERROR_$_" => 1 ); # else ERROR!
128 push(@errors,"ERROR_$_");
131 # check permission to modify login info.
132 if (ref($borrower_data) && ($borrower_data->{'category_type'} eq 'S') && ! (C4
::Auth
::haspermission
($dbh,$userenv->{'id'},{'staffaccess'=>1})) ) {
137 #############test for member being unique #############
138 if ($op eq 'insert'){
139 my $category_type_send=$category_type if ($category_type eq 'I');
140 my $check_category; # recover the category code of the doublon suspect borrowers
141 # ($result,$categorycode) = checkuniquemember($collectivity,$surname,$firstname,$dateofbirth)
142 ($check_member,$check_category) = checkuniquemember
(
144 ($newdata{surname
} ?
$newdata{surname
} : $data{surname
} ),
145 ($newdata{firstname
} ?
$newdata{firstname
} : $data{firstname
} ),
146 ($newdata{dateofbirth
} ?
$newdata{dateofbirth
} : $data{dateofbirth
})
149 # recover the category type if the borrowers is a doublon
150 my $tmpborrowercategory=GetBorrowercategory
($check_category);
151 $check_categorytype=$tmpborrowercategory->{'category_type'};
154 #recover all data from guarantor address phone ,fax...
155 if (($category_type eq 'C' || $category_type eq 'P') and $guarantorid ne '' ){
156 my $guarantordata=GetMember
($guarantorid);
157 $guarantorinfo=$guarantordata->{'surname'}." , ".$guarantordata->{'firstname'};
158 if (($data{'contactname'} eq '' or $data{'contactname'} ne $guarantordata->{'surname'})) {
159 $data{'contactfirstname'}= $guarantordata->{'firstname'};
160 $data{'contactname'} = $guarantordata->{'surname'};
161 $data{'contacttitle'} = $guarantordata->{'title'};
162 foreach (qw(streetnumber address streettype address2 zipcode city phone phonepro mobile fax email emailpro branchcode)) {
163 $data{$_} = $guarantordata->{$_};
168 ###############test to take the right zipcode and city name ##############
169 if ( $guarantorid eq ''){
170 $newdata{'city'}= $city;
171 $newdata{'zipcode'}=$zipcode;
174 #builds default userid
175 if ( (defined $newdata{'userid'}) && ($newdata{'userid'} eq '')){
176 my $onefirstnameletter = substr($data{'firstname'},0,1);
177 my $fivesurnameletter = substr($data{'surname'},0,9);
178 $newdata{'userid'}=lc($onefirstnameletter.$fivesurnameletter);
181 $debug and warn join "\t", map {"$_: $newdata{$_}"} qw(dateofbirth dateenrolled dateexpiry);
183 if ($op eq 'save' || $op eq 'insert'){
184 if (checkcardnumber
($newdata{cardnumber
},$newdata{borrowernumber
})){
185 push @errors, 'ERROR_cardnumber';
187 my $dateofbirthmandatory = (scalar grep {$_ eq "dateofbirth"} @field_check) ?
1 : 0;
188 if ($newdata{dateofbirth
} && $dateofbirthmandatory) {
189 my $age = GetAge
($newdata{dateofbirth
});
190 my $borrowercategory=GetBorrowercategory
($newdata{'categorycode'});
191 my ($low,$high) = ($borrowercategory->{'dateofbirthrequired'}, $borrowercategory->{'upperagelimit'});
192 if (($high && ($age > $high)) or ($age < $low)) {
193 push @errors, 'ERROR_age_limitations';
194 $template->param('ERROR_age_limitations' => "$low to $high");
197 if (C4
::Context
->preference("IndependantBranches")) {
198 if ($userenv && $userenv->{flags
} != 1){
199 $debug and print STDERR
" $newdata{'branchcode'} : ".$userenv->{flags
}.":".$userenv->{branch
};
200 unless (!$newdata{'branchcode'} || $userenv->{branch
} eq $newdata{'branchcode'}){
201 push @errors, "ERROR_branch";
205 # Check if the userid is unique
206 unless (Check_Userid
($newdata{'userid'},$borrowernumber)) {
207 push @errors, "ERROR_login_exist";
212 if ($op eq 'modify' || $op eq 'insert'){
213 unless ($newdata{'dateexpiry'}){
214 my $arg2 = $newdata{'dateenrolled'} || C4
::Dates
->today('iso');
215 $newdata{'dateexpiry'} = GetExpiryDate
($newdata{'categorycode'},$arg2);
219 ### Error checks should happen before this line.
221 $nok = $nok || scalar(@errors);
222 if ((!$nok) and ($op eq 'insert' or $op eq 'save')){
223 $debug and warn "$op dates: " . join "\t", map {"$_: $newdata{$_}"} qw(dateofbirth dateenrolled dateexpiry);
224 if ($op eq 'insert'){
225 # we know it's not a duplicate borrowernumber or there would already be an error
226 $borrowernumber = &AddMember
(%newdata);
227 if ($data{'organisations'}){
228 # need to add the members organisations
229 my @orgs=split(/\|/,$data{'organisations'});
230 add_member_orgs
($borrowernumber,\
@orgs);
232 } elsif ($op eq 'save'){
233 if ($NoUpdateLogin) {
234 delete $newdata{'password'};
235 delete $newdata{'userid'};
237 &ModMember
(%newdata);
239 print scalar ($destination eq "circ") ?
240 $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber") :
241 $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$borrowernumber") ;
242 exit; # You can only send 1 redirect! After that, content or other headers don't matter.
246 print $input->redirect("/cgi-bin/koha/deletemem.pl?member=$borrowernumber");
247 exit; # same as above
251 $op="add" if ($op eq "insert");
252 $op="modify" if ($op eq "save");
254 $template->param( updtype
=> ($op eq 'add' ?
'I':'M')); # used to check for $op eq "insert"... but we just changed $op!
256 $template->param( step_1
=> 1,step_2
=> 1,step_3
=> 1);
259 if (C4
::Context
->preference("IndependantBranches")) {
260 my $userenv = C4
::Context
->userenv;
261 if ($userenv->{flags
} != 1 && $data{branchcode
}){
262 unless ($userenv->{branch
} eq $data{'branchcode'}){
263 print $input->redirect("/cgi-bin/koha/members/members-home.pl");
269 my $arg2 = $newdata{'dateenrolled'} || C4
::Dates
->today('iso');
270 $data{'dateexpiry'} = GetExpiryDate
($newdata{'categorycode'},$arg2);
271 $template->param( updtype
=> 'I',step_1
=>1,step_2
=>1,step_3
=>1);
274 if ($op eq "modify") {
275 $template->param( updtype
=> 'M',modify
=> 1 );
276 $template->param( step_1
=>1,step_2
=>1,step_3
=>1) unless $step;
278 # my $cardnumber=$data{'cardnumber'};
279 $data{'cardnumber'}=fixup_cardnumber
($data{'cardnumber'}) if $op eq 'add';
280 if ($data{'sex'} eq 'F'){
281 $template->param(female
=> 1);
282 } elsif ($data{'sex'} eq 'M'){
283 $template->param(male
=> 1);
285 $template->param(none
=> 1);
288 ##Now all the data to modify a member.
289 my ($categories,$labels)=ethnicitycategories
();
291 my $ethnicitycategoriescount=$#{$categories};
293 if ($ethnicitycategoriescount>=0) {
294 $ethcatpopup = CGI
::popup_menu
(-name
=>'ethnicity',
297 -values=>$categories,
298 -default=>$data{'ethnicity'},
300 $template->param(ethcatpopup
=> $ethcatpopup); # bad style, has to be fixed
304 foreach ( ($new_c_type) ?
($new_c_type) : qw(C A S P I X)){
305 my $action="WHERE category_type=?";
306 ($categories,$labels)=GetborCatFromCatType
($_,$action);
308 foreach my $cat (@
$categories){
309 push @categoryloop,{'categorycode' => $cat,
310 'categoryname' => $labels->{$cat},
311 'categorycodeselected' => ($cat eq $borrower_data->{'categorycode'}),
315 $typehash{'typename'}=$_;
316 $typehash{'categoryloop'}=\
@categoryloop;
317 push @typeloop,{'typename' => $_,
318 'categoryloop' => \
@categoryloop};
320 $template->param('typeloop' => \
@typeloop);
323 $select_city=getidcity
($data{'city'}) if ($guarantorid ne '0');
324 ($default_city=$select_city) if ($step eq 0);
325 if ($select_city eq '' ){
326 $default_city = &getidcity
($data{'city'});
329 ($cityid,$name_city)=GetCities
();
330 $template->param( city_cgipopup
=> 1) if ($cityid );
331 my $citypopup = CGI
::popup_menu
(-name
=>'select_city',
332 -id
=> 'select_city',
335 -default=>$default_city,
338 my $default_roadtype;
339 $default_roadtype=$data{'streettype'} ;
340 my($roadtypeid,$road_type)=GetRoadTypes
();
341 $template->param( road_cgipopup
=> 1) if ($roadtypeid );
342 my $roadpopup = CGI
::popup_menu
(-name
=>'streettype',
344 -values=>$roadtypeid,
347 -default=>$default_roadtype
350 my $default_borrowertitle;
351 $default_borrowertitle=$data{'title'} ;
352 my($borrowertitle)=GetTitles
();
353 $template->param( title_cgipopup
=> 1) if ($borrowertitle);
354 my $borrotitlepopup = CGI
::popup_menu
(-name
=>'title',
356 -values=>$borrowertitle,
358 -default=>$default_borrowertitle
361 my @relationships = split /,|\|/,C4
::Context
->preference('BorrowerRelationship');
363 while (@relationships) {
364 my $relship = shift @relationships || '';
365 my %row = ('relationship' => $relship);
366 if ($data{'relationship'} eq $relship) {
367 $row{'selected'}=' selected';
371 push(@relshipdata, \
%row);
374 my %flags = ( 'gonenoaddress' => ['gonenoaddress' ],
376 'debarred' => ['debarred']);
380 foreach (keys(%flags)) {
382 my %row = ('key' => $key,
383 'name' => $flags{$key}[0]);
385 $row{'yes'}=' checked';
390 $row{'no'}=' checked';
392 push @flagdata,\
%row;
400 my $onlymine=(C4
::Context
->preference('IndependantBranches') &&
401 C4
::Context
->userenv &&
402 C4
::Context
->userenv->{flags
} !=1 &&
403 C4
::Context
->userenv->{branch
}?
1:0);
405 my $branches=GetBranches
($onlymine);
408 foreach my $branch (sort keys %$branches) {
409 push @select_branch,$branch;
410 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
411 $default = C4
::Context
->userenv->{'branch'} if (C4
::Context
->userenv && C4
::Context
->userenv->{'branch'});
413 # --------------------------------------------------------------------------------------------------------
414 #in modify mod :default value from $CGIbranch comes from borrowers table
415 #in add mod: default value come from branches table (ip correspendence)
416 $default=$data{'branchcode'} if ($op eq 'modify' || ($op eq 'add' && $category_type eq 'C'));
417 my $CGIbranch = CGI
::scrolling_list
(-id
=> 'branchcode',
418 -name
=> 'branchcode',
419 -values => \
@select_branch,
420 -labels
=> \
%select_branches,
424 -default => $default,
426 my $CGIorganisations;
427 my $member_of_institution;
428 if (C4
::Context
->preference("memberofinstitution")){
429 my $organisations=get_institutions
();
432 foreach my $organisation (keys %$organisations) {
433 push @orgs,$organisation;
434 $org_labels{$organisation}=$organisations->{$organisation}->{'surname'};
436 $member_of_institution=1;
438 $CGIorganisations = CGI
::scrolling_list
( -id
=> 'organisations',
439 -name
=> 'organisations',
440 -labels
=> \
%org_labels,
449 # --------------------------------------------------------------------------------------------------------
451 my $CGIsort = buildCGIsort
("Bsort1","sort1",$data{'sort1'});
453 $template->param(CGIsort1
=> $CGIsort);
455 $template->param( sort1
=> $data{'sort1'}); # shouldn't this be in an "else" statement like the 2nd one?
457 $CGIsort = buildCGIsort
("Bsort2","sort2",$data{'sort2'});
459 $template->param(CGIsort2
=> $CGIsort);
461 $template->param( sort2
=> $data{'sort2'});
465 foreach my $error (@errors) {
466 $template->param($error) || $template->param( $error => 1);
468 $template->param(nok
=> 1);
471 #Formatting data for display
473 if ($data{'dateenrolled'} eq ''){
474 $data{'dateenrolled'}=C4
::Dates
->today('iso');
476 if (C4
::Context
->preference('uppercasesurnames')) {
477 $data{'surname'} =uc($data{'surname'} );
478 $data{'contactname'}=uc($data{'contactname'});
480 foreach (qw(dateenrolled dateexpiry dateofbirth)) {
481 $data{$_} = format_date
($data{$_}); # back to syspref for display
482 $template->param( $_ => $data{$_});
485 $template->param( "showguarantor" => ($category_type=~/A|I|S|X/) ?
0 : 1); # associate with step to know where you are
486 $debug and warn "memberentry step: $step";
487 $template->param(%data);
488 $template->param( "step_$step" => 1) if $step; # associate with step to know where u are
489 $template->param( step
=> $step ) if $step; # associate with step to know where u are
490 $template->param( debug
=> $debug ) if $debug;
492 BorrowerMandatoryField
=> C4
::Context
->preference("BorrowerMandatoryField"),#field to test with javascript
493 category_type
=> $category_type,#to know the category type of the borrower
494 DHTMLcalendar_dateformat
=> C4
::Dates
->DHTMLcalendar(),
495 select_city
=> $select_city,
496 "$category_type" => 1,# associate with step to know where u are
497 destination
=> $destination,#to know wher u come from and wher u must go in redirect
498 check_member
=> $check_member,#to know if the borrower already exist(=>1) or not (=>0)
499 flags
=>$data{'flags'},
501 nodouble
=> $nodouble,
502 borrowernumber
=> $borrowernumber,#register number
503 "contacttitle_".$data{'contacttitle'} => "SELECTED" ,
504 guarantorid
=> $guarantorid,
505 ethcatpopup
=> $ethcatpopup,
506 relshiploop
=> \
@relshipdata,
507 citypopup
=> $citypopup,
508 roadpopup
=> $roadpopup,
509 borrotitlepopup
=> $borrotitlepopup,
510 guarantorinfo
=> $guarantorinfo,
511 flagloop
=> \
@flagdata,
512 dateformat
=> C4
::Dates
->new()->visual(),
513 C4
::Context
->preference('dateformat') => 1,
514 check_categorytype
=>$check_categorytype,#to recover the category type with checkcategorytype function
516 nok
=> $nok,#flag to konw if an error
517 CGIbranch
=> $CGIbranch,
518 memberofinstution
=> $member_of_institution,
519 CGIorganisations
=> $CGIorganisations,
520 NoUpdateLogin
=> $NoUpdateLogin
523 output_html_with_http_headers
$input, $cookie, $template->output;