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
24 use Date::Calc qw/Today/;
26 # use Digest::MD5 qw(md5_base64);
34 use C4::Dates qw/format_date format_date_in_iso/;
37 use C4::Branch; # GetBranches
42 $debug = $ENV{DEBUG} || 0;
46 ($debug) or $debug = $input->param('debug') || 0;
49 my $dbh = C4::Context->dbh;
51 my ($template, $loggedinuser, $cookie)
52 = get_template_and_user({template_name => "members/memberentrygen.tmpl",
56 flagsrequired => {borrowers => 1},
57 debug => ($debug) ? 1 : 0,
59 my $guarantorid=$input->param('guarantorid');
60 my $borrowernumber=$input->param('borrowernumber');
61 my $actionType=$input->param('actionType') || '';
62 my $modify=$input->param('modify');
63 my $delete=$input->param('delete');
64 my $op=$input->param('op');
65 my $destination=$input->param('destination');
66 my $cardnumber=$input->param('cardnumber');
67 my $check_member=$input->param('check_member');
68 my $name_city=$input->param('name_city');
69 my $nodouble=$input->param('nodouble');
70 my $select_city=$input->param('select_city');
71 my $nok=$input->param('nok');
72 my $guarantorinfo=$input->param('guarantorinfo');
73 my $step=$input->param('step') || 0;
76 # $check_categorytype contains the value of duplicate borrowers category type to redirect in good template in step =2
77 my $check_categorytype=$input->param('check_categorytype');
78 # NOTE: Alert for ethnicity and ethnotes fields, they are invalid in all borrowers form
81 my $userenv = C4::Context->userenv;
83 $template->param("uppercasesurnames" => C4::Context->preference('uppercasesurnames'));
85 # function to designate mandatory fields (visually with css)
86 my $check_BorrowerMandatoryField=C4::Context->preference("BorrowerMandatoryField");
87 my @field_check=split(/\|/,$check_BorrowerMandatoryField);
88 foreach (@field_check) {
89 $template->param( "mandatory$_" => 1);
91 $template->param("add"=>1) if ($op eq 'add');
92 $template->param("checked" => 1) if ($nodouble eq 1);
93 ($borrower_data = GetMember($borrowernumber,'borrowernumber')) if ($op eq 'modify' or $op eq 'save');
94 my $categorycode = $input->param('categorycode') || $borrower_data->{'categorycode'};
95 my $category_type = $input->param('category_type');
96 unless ($category_type or !($categorycode)){
97 my $borrowercategory= GetBorrowercategory($categorycode);
98 $category_type = $borrowercategory->{'category_type'};
100 $category_type="A" unless $category_type; # FIXME we should display a error message instead of a 500 error !
102 # if a add or modify is requested => check validity of data.
103 %data = %$borrower_data if ($borrower_data);
105 my %newdata; # comes from $input->param()
106 if ($op eq 'insert' || $op eq 'modify' || $op eq 'save') {
107 my @names= ($borrower_data && $op ne 'save') ? keys %$borrower_data : $input->param();
108 foreach my $key (@names) {
109 $newdata{$key} = $input->param($key) if (defined $input->param($key));
110 $newdata{$key} =~ s/\"/"/gg unless $key eq 'borrowernotes' or $key eq 'opacnote';
112 my $dateobject = C4::Dates->new();
113 my $syspref = $dateobject->regexp(); # same syspref format for all 3 dates
114 my $iso = $dateobject->regexp('iso'); #
115 foreach (qw(dateenrolled dateexpiry dateofbirth)) {
116 my $userdate = $newdata{$_} or next;
117 if ($userdate =~ /$syspref/) {
118 $newdata{$_} = format_date_in_iso($userdate); # if they match syspref format, then convert to ISO
119 } elsif ($userdate =~ /$iso/) {
120 warn "Date $_ ($userdate) is already in ISO format";
122 ($userdate eq '0000-00-00') and warn "Data error: $_ is '0000-00-00'";
123 $template->param( "ERROR_$_" => 1 ); # else ERROR!
124 push(@errors,"ERROR_$_");
127 # check permission to modify login info.
128 if (ref($borrower_data) && ($borrower_data->{'category_type'} eq 'S') && ! (C4::Auth::haspermission($dbh,$userenv->{'id'},{'staffaccess'=>1})) ) {
133 #############test for member being unique #############
134 if ($op eq 'insert'){
135 my $category_type_send=$category_type if ($category_type eq 'I');
136 my $check_category; # recover the category code of the doublon suspect borrowers
137 # ($result,$categorycode) = checkuniquemember($collectivity,$surname,$firstname,$dateofbirth)
138 ($check_member,$check_category) = checkuniquemember(
140 ($newdata{surname} ? $newdata{surname} : $data{surname} ),
141 ($newdata{firstname} ? $newdata{firstname} : $data{firstname} ),
142 ($newdata{dateofbirth} ? $newdata{dateofbirth} : $data{dateofbirth})
145 # recover the category type if the borrowers is a doublon
146 my $tmpborrowercategory=GetBorrowercategory($check_category);
147 $check_categorytype=$tmpborrowercategory->{'category_type'};
150 #recover all data from guarantor address phone ,fax...
151 if (($category_type eq 'C' || $category_type eq 'P') and $guarantorid ne '' ){
152 my $guarantordata=GetMember($guarantorid);
153 $guarantorinfo=$guarantordata->{'surname'}." , ".$guarantordata->{'firstname'};
154 if (($data{'contactname'} eq '' or $data{'contactname'} ne $guarantordata->{'surname'})) {
155 $data{'contactfirstname'}= $guarantordata->{'firstname'};
156 $data{'contactname'} = $guarantordata->{'surname'};
157 $data{'contacttitle'} = $guarantordata->{'title'};
158 foreach (qw(streetnumber address streettype address2 zipcode city phone phonepro mobile fax email emailpro branchcode)) {
159 $data{$_} = $guarantordata->{$_};
164 ###############test to take the right zipcode and city name ##############
165 if ( $guarantorid eq ''){
167 my ($borrower_city,$borrower_zipcode)=&getzipnamecity($select_city);
168 $newdata{'city'}= $borrower_city;
169 $newdata{'zipcode'}=$borrower_zipcode;
172 #builds default userid
173 if ( (defined $newdata{'userid'}) && ($newdata{'userid'} eq '')){
174 my $onefirstnameletter = substr($data{'firstname'},0,1);
175 my $fivesurnameletter = substr($data{'surname'},0,9);
176 $newdata{'userid'}=lc($onefirstnameletter.$fivesurnameletter);
179 $debug and warn join "\t", map {"$_: $newdata{$_}"} qw(dateofbirth dateenrolled dateexpiry);
181 if ($op eq 'save' || $op eq 'insert'){
182 if (checkcardnumber($newdata{cardnumber},$newdata{borrowernumber})){
183 push @errors, 'ERROR_cardnumber';
185 my $dateofbirthmandatory = (scalar grep {$_ eq "dateofbirth"} @field_check) ? 1 : 0;
186 if ($newdata{dateofbirth} && $dateofbirthmandatory) {
187 my $age = GetAge($newdata{dateofbirth});
188 my $borrowercategory=GetBorrowercategory($newdata{'categorycode'});
189 my ($low,$high) = ($borrowercategory->{'dateofbirthrequired'}, $borrowercategory->{'upperagelimit'});
190 if (($age > $high) or ($age < $low)) {
191 push @errors, 'ERROR_age_limitations';
192 $template->param('ERROR_age_limitations' => "$low to $high");
195 if (C4::Context->preference("IndependantBranches")) {
196 if ($userenv && $userenv->{flags} != 1){
197 $debug and print STDERR " $newdata{'branchcode'} : ".$userenv->{flags}.":".$userenv->{branch};
198 unless (!$newdata{'branchcode'} || $userenv->{branch} eq $newdata{'branchcode'}){
199 push @errors, "ERROR_branch";
203 # Check if the userid is unique
204 unless (Check_Userid($newdata{'userid'},$borrowernumber)) {
205 push @errors, "ERROR_login_exist";
210 if ($op eq 'modify' || $op eq 'insert'){
211 unless ($newdata{'dateexpiry'}){
212 my $arg2 = $newdata{'dateenrolled'} || sprintf('%04d-%02d-%02d', Today());
213 $newdata{'dateexpiry'} = GetExpiryDate($newdata{'categorycode'},$arg2);
217 ### Error checks should happen before this line.
219 $nok = $nok || scalar(@errors);
220 if ((!$nok) and ($op eq 'insert' or $op eq 'save')){
221 $debug and warn "$op dates: " . join "\t", map {"$_: $newdata{$_}"} qw(dateofbirth dateenrolled dateexpiry);
222 if ($op eq 'insert'){
223 # we know it's not a duplicate borrowernumber or there would already be an error
224 $borrowernumber = &AddMember(%newdata);
225 if ($data{'organisations'}){
226 # need to add the members organisations
227 my @orgs=split(/\|/,$data{'organisations'});
228 add_member_orgs($borrowernumber,\@orgs);
230 } elsif ($op eq 'save'){
231 if ($NoUpdateLogin) {
232 delete $newdata{'password'};
233 delete $newdata{'userid'};
235 &ModMember(%newdata);
237 print scalar ($destination eq "circ") ?
238 $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$data{'cardnumber'}") :
239 $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$borrowernumber") ;
240 exit; # You can only send 1 redirect! After that, content or other headers don't matter.
244 print $input->redirect("/cgi-bin/koha/deletemem.pl?member=$borrowernumber");
245 exit; # same as above
249 $op="add" if ($op eq "insert");
250 $op="modify" if ($op eq "save");
252 $template->param( updtype => ($op eq 'add' ?'I':'M')); # used to check for $op eq "insert"... but we just changed $op!
254 $template->param( step_1 => 1,step_2 => 1,step_3 => 1);
257 if (C4::Context->preference("IndependantBranches")) {
258 my $userenv = C4::Context->userenv;
259 if ($userenv->{flags} != 1 && $data{branchcode}){
260 unless ($userenv->{branch} eq $data{'branchcode'}){
261 print $input->redirect("/cgi-bin/koha/members/members-home.pl");
267 $template->param( updtype => 'I',step_1=>1,step_2=>1,step_3=>1);
269 if ($op eq "modify") {
270 $template->param( updtype => 'M',modify => 1 );
271 $template->param( step_1=>1,step_2=>1,step_3=>1) unless $step;
273 # my $cardnumber=$data{'cardnumber'};
274 $data{'cardnumber'}=fixup_cardnumber($data{'cardnumber'}) if $op eq 'add';
275 if ($data{'sex'} eq 'F'){
276 $template->param(female => 1);
277 } elsif ($data{'sex'} eq 'M'){
278 $template->param(male => 1);
280 $template->param(none => 1);
283 ##Now all the data to modify a member.
284 my ($categories,$labels)=ethnicitycategories();
286 my $ethnicitycategoriescount=$#{$categories};
288 if ($ethnicitycategoriescount>=0) {
289 $ethcatpopup = CGI::popup_menu(-name=>'ethnicity',
292 -values=>$categories,
293 -default=>$data{'ethnicity'},
295 $template->param(ethcatpopup => $ethcatpopup); # bad style, has to be fixed
299 foreach (qw(C A S P I)){
300 my $action="WHERE category_type=?";
301 ($categories,$labels)=GetborCatFromCatType($_,$action);
303 foreach my $cat (@$categories){
304 push @categoryloop,{'categorycode' => $cat,
305 'categoryname' => $labels->{$cat},
306 'categorycodeselected' => ($cat eq $borrower_data->{'categorycode'}),
310 $typehash{'typename'}=$_;
311 $typehash{'categoryloop'}=\@categoryloop;
312 push @typeloop,{'typename' => $_,
313 'categoryloop' => \@categoryloop};
315 $template->param('typeloop' => \@typeloop);
318 $select_city=getidcity($data{'city'}) if ($guarantorid ne '0');
319 ($default_city=$select_city) if ($step eq 0);
320 if ($select_city eq '' ){
321 $default_city = &getidcity($data{'city'});
324 ($cityid,$name_city)=GetCities();
325 $template->param( city_cgipopup => 1) if ($cityid );
326 my $citypopup = CGI::popup_menu(-name=>'select_city',
327 -id => 'select_city',
330 -default=>$default_city,
333 my $default_roadtype;
334 $default_roadtype=$data{'streettype'} ;
335 my($roadtypeid,$road_type)=GetRoadTypes();
336 $template->param( road_cgipopup => 1) if ($roadtypeid );
337 my $roadpopup = CGI::popup_menu(-name=>'streettype',
339 -values=>$roadtypeid,
342 -default=>$default_roadtype
345 my $default_borrowertitle;
346 $default_borrowertitle=$data{'title'} ;
347 my($borrowertitle)=GetTitles();
348 $template->param( title_cgipopup => 1) if ($borrowertitle);
349 my $borrotitlepopup = CGI::popup_menu(-name=>'title',
351 -values=>$borrowertitle,
353 -default=>$default_borrowertitle
356 my @relationships = split /,|\|/,C4::Context->preference('BorrowerRelationship');
358 while (@relationships) {
359 my $relship = shift @relationships || '';
360 my %row = ('relationship' => $relship);
361 if ($data{'relationship'} eq $relship) {
362 $row{'selected'}=' selected';
366 push(@relshipdata, \%row);
369 my %flags = ( 'gonenoaddress' => ['gonenoaddress' ],
371 'debarred' => ['debarred']);
375 foreach (keys(%flags)) {
377 my %row = ('key' => $key,
378 'name' => $flags{$key}[0]);
380 $row{'yes'}=' checked';
385 $row{'no'}=' checked';
387 push @flagdata,\%row;
395 my $onlymine=(C4::Context->preference('IndependantBranches') &&
396 C4::Context->userenv &&
397 C4::Context->userenv->{flags} !=1 &&
398 C4::Context->userenv->{branch}?1:0);
400 my $branches=GetBranches($onlymine);
403 foreach my $branch (sort keys %$branches) {
404 push @select_branch,$branch;
405 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
406 $default = C4::Context->userenv->{'branch'} if (C4::Context->userenv && C4::Context->userenv->{'branch'});
408 # --------------------------------------------------------------------------------------------------------
409 #in modify mod :default value from $CGIbranch comes from borrowers table
410 #in add mod: default value come from branches table (ip correspendence)
411 $default=$data{'branchcode'} if ($op eq 'modify' || ($op eq 'add' && $category_type eq 'C'));
412 my $CGIbranch = CGI::scrolling_list(-id => 'branchcode',
413 -name => 'branchcode',
414 -values => \@select_branch,
415 -labels => \%select_branches,
419 -default => $default,
421 my $CGIorganisations;
422 my $member_of_institution;
423 if (C4::Context->preference("memberofinstitution")){
424 my $organisations=get_institutions();
427 foreach my $organisation (keys %$organisations) {
428 push @orgs,$organisation;
429 $org_labels{$organisation}=$organisations->{$organisation}->{'surname'};
431 $member_of_institution=1;
433 $CGIorganisations = CGI::scrolling_list( -id => 'organisations',
434 -name => 'organisations',
435 -labels => \%org_labels,
444 # --------------------------------------------------------------------------------------------------------
446 my $CGIsort = buildCGIsort("Bsort1","sort1",$data{'sort1'});
448 $template->param(CGIsort1 => $CGIsort);
450 $template->param( sort1 => $data{'sort1'}); # shouldn't this be in an "else" statement like the 2nd one?
452 $CGIsort = buildCGIsort("Bsort2","sort2",$data{'sort2'});
454 $template->param(CGIsort2 => $CGIsort);
456 $template->param( sort2 => $data{'sort2'});
460 foreach my $error (@errors) {
461 $template->param($error) || $template->param( $error => 1);
463 $template->param(nok => 1);
466 #Formatting data for display
468 if ($data{'dateenrolled'} eq ''){
469 my $today = sprintf('%04d-%02d-%02d', Today()); # ISO format
470 $data{'dateenrolled'}=$today;
472 if (C4::Context->preference('uppercasesurnames')) {
473 $data{'surname'} =uc($data{'surname'} );
474 $data{'contactname'}=uc($data{'contactname'});
476 foreach (qw(dateenrolled dateexpiry dateofbirth)) {
477 $data{$_} = format_date($data{$_}); # back to syspref for display
478 $template->param( $_ => $data{$_});
481 $template->param( "showguarantor" => ($category_type=~/A|I|S/) ? 0 : 1); # associate with step to know where you are
482 $debug and warn "memberentry step: $step";
483 $template->param(%data);
484 $template->param( "step_$step" => 1) if $step; # associate with step to know where u are
485 $template->param( step => $step ) if $step; # associate with step to know where u are
486 $template->param( debug => $debug ) if $debug;
488 BorrowerMandatoryField => C4::Context->preference("BorrowerMandatoryField"),#field to test with javascript
489 category_type => $category_type,#to know the category type of the borrower
490 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
491 select_city => $select_city,
492 "$category_type" => 1,# associate with step to know where u are
493 destination => $destination,#to know wher u come from and wher u must go in redirect
494 check_member => $check_member,#to know if the borrower already exist(=>1) or not (=>0)
495 flags =>$data{'flags'},
497 nodouble => $nodouble,
498 borrowernumber => $borrowernumber,#register number
499 "contacttitle_".$data{'contacttitle'} => "SELECTED" ,
500 guarantorid => $guarantorid,
501 ethcatpopup => $ethcatpopup,
502 relshiploop => \@relshipdata,
503 citypopup => $citypopup,
504 roadpopup => $roadpopup,
505 borrotitlepopup => $borrotitlepopup,
506 guarantorinfo => $guarantorinfo,
507 flagloop => \@flagdata,
508 dateformat => C4::Dates->new()->visual(),
509 C4::Context->preference('dateformat') => 1,
510 check_categorytype =>$check_categorytype,#to recover the category type with checkcategorytype function
512 nok => $nok,#flag to konw if an error
513 CGIbranch => $CGIbranch,
514 memberofinstution => $member_of_institution,
515 CGIorganisations => $CGIorganisations,
516 NoUpdateLogin => $NoUpdateLogin
519 output_html_with_http_headers $input, $cookie, $template->output;