Bug 8391: prevent error when viewing circ history with bad dates
[koha.git] / members / memberentry.pl
blob2a391101a3a0bc6198654d1e8f0566f94bd742b3
1 #!/usr/bin/perl
3 # Copyright 2006 SAN OUEST PROVENCE et Paul POULAIN
4 # Copyright 2010 BibLibre
6 # This file is part of Koha.
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.
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.
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 # pragma
22 use strict;
23 use warnings;
25 # external modules
26 use CGI;
27 # use Digest::MD5 qw(md5_base64);
28 use List::MoreUtils qw/uniq/;
30 # internal modules
31 use C4::Auth;
32 use C4::Context;
33 use C4::Output;
34 use C4::Members;
35 use C4::Members::Attributes;
36 use C4::Members::AttributeTypes;
37 use C4::Koha;
38 use C4::Dates qw/format_date format_date_in_iso/;
39 use C4::Input;
40 use C4::Log;
41 use C4::Letters;
42 use C4::Branch; # GetBranches
43 use C4::Form::MessagingPreferences;
45 use vars qw($debug);
47 BEGIN {
48 $debug = $ENV{DEBUG} || 0;
51 my $input = new CGI;
52 ($debug) or $debug = $input->param('debug') || 0;
53 my %data;
55 my $dbh = C4::Context->dbh;
57 my ($template, $loggedinuser, $cookie)
58 = get_template_and_user({template_name => "members/memberentrygen.tmpl",
59 query => $input,
60 type => "intranet",
61 authnotrequired => 0,
62 flagsrequired => {borrowers => 1},
63 debug => ($debug) ? 1 : 0,
64 });
65 my $guarantorid = $input->param('guarantorid');
66 my $borrowernumber = $input->param('borrowernumber');
67 my $actionType = $input->param('actionType') || '';
68 my $modify = $input->param('modify');
69 my $delete = $input->param('delete');
70 my $op = $input->param('op');
71 my $destination = $input->param('destination');
72 my $cardnumber = $input->param('cardnumber');
73 my $check_member = $input->param('check_member');
74 my $nodouble = $input->param('nodouble');
75 my $duplicate = $input->param('duplicate');
76 $nodouble = 1 if ($op eq 'modify' or $op eq 'duplicate'); # FIXME hack to represent fact that if we're
77 # modifying an existing patron, it ipso facto
78 # isn't a duplicate. Marking FIXME because this
79 # script needs to be refactored.
80 my $select_city = $input->param('select_city');
81 my $nok = $input->param('nok');
82 my $guarantorinfo = $input->param('guarantorinfo');
83 my $step = $input->param('step') || 0;
84 my @errors;
85 my $default_city;
86 # $check_categorytype contains the value of duplicate borrowers category type to redirect in good template in step =2
87 my $check_categorytype=$input->param('check_categorytype');
88 # NOTE: Alert for ethnicity and ethnotes fields, they are invalid in all borrowers form
89 my $borrower_data;
90 my $NoUpdateLogin;
91 my $userenv = C4::Context->userenv;
93 $template->param("uppercasesurnames" => C4::Context->preference('uppercasesurnames'));
95 my $minpw = C4::Context->preference('minPasswordLength');
96 $template->param("minPasswordLength" => $minpw);
98 # function to designate mandatory fields (visually with css)
99 my $check_BorrowerMandatoryField=C4::Context->preference("BorrowerMandatoryField");
100 my @field_check=split(/\|/,$check_BorrowerMandatoryField);
101 foreach (@field_check) {
102 $template->param( "mandatory$_" => 1);
104 # we'll need this, later.
105 my $dateofbirthmandatory = (scalar grep {$_ eq "dateofbirth"} @field_check) ? 1 : 0;
106 # function to designate unwanted fields
107 my $check_BorrowerUnwantedField=C4::Context->preference("BorrowerUnwantedField");
108 @field_check=split(/\|/,$check_BorrowerUnwantedField);
109 foreach (@field_check) {
110 next unless m/\w/o;
111 $template->param( "no$_" => 1);
113 $template->param( "add" => 1 ) if ( $op eq 'add' );
114 $template->param( "duplicate" => 1 ) if ( $op eq 'duplicate' );
115 $template->param( "checked" => 1 ) if ( defined($nodouble) && $nodouble eq 1 );
116 ( $borrower_data = GetMember( 'borrowernumber' => $borrowernumber ) ) if ( $op eq 'modify' or $op eq 'save' or $op eq 'duplicate' );
117 my $categorycode = $input->param('categorycode') || $borrower_data->{'categorycode'};
118 my $category_type = $input->param('category_type') || '';
119 if ($category_type){
120 $template->{VARS}->{'type_only'} = 1;
122 my $new_c_type = $category_type; #if we have input param, then we've already chosen the cat_type.
123 unless ($category_type or !($categorycode)){
124 my $borrowercategory = GetBorrowercategory($categorycode);
125 $category_type = $borrowercategory->{'category_type'};
126 my $category_name = $borrowercategory->{'description'};
127 $template->param("categoryname"=>$category_name);
129 $category_type="A" unless $category_type; # FIXME we should display a error message instead of a 500 error !
131 # if a add or modify is requested => check validity of data.
132 %data = %$borrower_data if ($borrower_data);
134 # initialize %newdata
135 my %newdata; # comes from $input->param()
136 if ( $op eq 'insert' || $op eq 'modify' || $op eq 'save' || $op eq 'duplicate' ) {
137 my @names = ( $borrower_data && $op ne 'save' ) ? keys %$borrower_data : $input->param();
138 foreach my $key (@names) {
139 if (defined $input->param($key)) {
140 $newdata{$key} = $input->param($key);
141 $newdata{$key} =~ s/\"/"/g unless $key eq 'borrowernotes' or $key eq 'opacnote';
145 ## Manipulate debarred
146 if ( $newdata{debarred} ) {
147 $newdata{debarred} = $newdata{datedebarred} ? $newdata{datedebarred} : "9999-12-31";
148 } elsif ( exists( $newdata{debarred} ) && !( $newdata{debarred} ) ) {
149 undef( $newdata{debarred} );
150 undef( $newdata{debarredcomment} );
151 } elsif ( exists( $newdata{debarredcomment} ) && $newdata{debarredcomment} eq "" ) {
152 undef( $newdata{debarredcomment} );
155 my $dateobject = C4::Dates->new();
156 my $syspref = $dateobject->regexp(); # same syspref format for all 3 dates
157 my $iso = $dateobject->regexp('iso'); #
158 foreach (qw(dateenrolled dateexpiry dateofbirth)) {
159 next unless exists $newdata{$_};
160 my $userdate = $newdata{$_} or next;
161 if ($userdate =~ /$syspref/) {
162 $newdata{$_} = format_date_in_iso($userdate); # if they match syspref format, then convert to ISO
163 } elsif ($userdate =~ /$iso/) {
164 warn "Date $_ ($userdate) is already in ISO format";
165 } else {
166 ($userdate eq '0000-00-00') and warn "Data error: $_ is '0000-00-00'";
167 $template->param( "ERROR_$_" => 1 ); # else ERROR!
168 push(@errors,"ERROR_$_");
171 # check permission to modify login info.
172 if (ref($borrower_data) && ($borrower_data->{'category_type'} eq 'S') && ! (C4::Auth::haspermission($userenv->{'id'},{'staffaccess'=>1})) ) {
173 $NoUpdateLogin = 1;
177 # remove keys from %newdata that ModMember() doesn't like
179 my @keys_to_delete = (
180 qr/^BorrowerMandatoryField$/,
181 qr/^category_type$/,
182 qr/^check_member$/,
183 qr/^destination$/,
184 qr/^nodouble$/,
185 qr/^op$/,
186 qr/^save$/,
187 qr/^select_roadtype$/,
188 qr/^updtype$/,
189 qr/^SMSnumber$/,
190 qr/^setting_extended_patron_attributes$/,
191 qr/^setting_messaging_prefs$/,
192 qr/^digest$/,
193 qr/^modify$/,
194 qr/^step$/,
195 qr/^\d+$/,
196 qr/^\d+-DAYS/,
197 qr/^patron_attr_/,
199 for my $regexp (@keys_to_delete) {
200 for (keys %newdata) {
201 delete($newdata{$_}) if /$regexp/;
206 #############test for member being unique #############
207 if ( ( $op eq 'insert' ) and !$nodouble ) {
208 my $category_type_send;
209 if ( $category_type eq 'I' ) {
210 $category_type_send = $category_type;
212 my $check_category; # recover the category code of the doublon suspect borrowers
213 # ($result,$categorycode) = checkuniquemember($collectivity,$surname,$firstname,$dateofbirth)
214 ( $check_member, $check_category ) = checkuniquemember(
215 $category_type_send,
216 ( $newdata{surname} ? $newdata{surname} : $data{surname} ),
217 ( $newdata{firstname} ? $newdata{firstname} : $data{firstname} ),
218 ( $newdata{dateofbirth} ? $newdata{dateofbirth} : $data{dateofbirth} )
220 if ( !$check_member ) {
221 $nodouble = 1;
224 # recover the category type if the borrowers is a doublon
225 if ($check_category) {
226 my $tmpborrowercategory = GetBorrowercategory($check_category);
227 $check_categorytype = $tmpborrowercategory->{'category_type'};
231 #recover all data from guarantor address phone ,fax...
232 if ( $guarantorid and ( $category_type eq 'C' || $category_type eq 'P' )) {
233 if (my $guarantordata=GetMember(borrowernumber => $guarantorid)) {
234 $guarantorinfo=$guarantordata->{'surname'}." , ".$guarantordata->{'firstname'};
235 $newdata{'contactfirstname'}= $guarantordata->{'firstname'};
236 $newdata{'contactname'} = $guarantordata->{'surname'};
237 $newdata{'contacttitle'} = $guarantordata->{'title'};
238 if ( $op eq 'add' ) {
239 foreach (qw(streetnumber address streettype address2
240 zipcode country city state phone phonepro mobile fax email emailpro branchcode
241 B_streetnumber B_streettype B_address B_address2
242 B_city B_state B_zipcode B_country B_email B_phone)) {
243 $newdata{$_} = $guarantordata->{$_};
249 ###############test to take the right zipcode, country and city name ##############
250 # set only if parameter was passed from the form
251 $newdata{'city'} = $input->param('city') if defined($input->param('city'));
252 $newdata{'zipcode'} = $input->param('zipcode') if defined($input->param('zipcode'));
253 $newdata{'country'} = $input->param('country') if defined($input->param('country'));
255 #builds default userid
256 if ( (defined $newdata{'userid'}) && ($newdata{'userid'} eq '')){
257 $newdata{'userid'} = Generate_Userid($borrowernumber, $newdata{'firstname'}, $newdata{'surname'});
260 $debug and warn join "\t", map {"$_: $newdata{$_}"} qw(dateofbirth dateenrolled dateexpiry);
261 my $extended_patron_attributes = ();
262 if ($op eq 'save' || $op eq 'insert'){
263 # If the cardnumber is blank, treat it as null.
264 $newdata{'cardnumber'} = undef if $newdata{'cardnumber'} =~ /^\s*$/;
266 if (checkcardnumber($newdata{cardnumber},$newdata{borrowernumber})){
267 push @errors, 'ERROR_cardnumber';
269 if ($newdata{dateofbirth} && $dateofbirthmandatory) {
270 my $age = GetAge($newdata{dateofbirth});
271 my $borrowercategory=GetBorrowercategory($newdata{'categorycode'});
272 my ($low,$high) = ($borrowercategory->{'dateofbirthrequired'}, $borrowercategory->{'upperagelimit'});
273 if (($high && ($age > $high)) or ($age < $low)) {
274 push @errors, 'ERROR_age_limitations';
275 $template->param( age_low => $low);
276 $template->param( age_high => $high);
280 if($newdata{surname} && C4::Context->preference('uppercasesurnames')) {
281 $newdata{'surname'} = uc($newdata{'surname'});
284 if (C4::Context->preference("IndependantBranches")) {
285 if ($userenv && $userenv->{flags} % 2 != 1){
286 $debug and print STDERR " $newdata{'branchcode'} : ".$userenv->{flags}.":".$userenv->{branch};
287 unless (!$newdata{'branchcode'} || $userenv->{branch} eq $newdata{'branchcode'}){
288 push @errors, "ERROR_branch";
292 # Check if the userid is unique
293 unless (Check_Userid($newdata{'userid'},$borrowernumber)) {
294 push @errors, "ERROR_login_exist";
297 my $password = $input->param('password');
298 my $password2 = $input->param('password2');
299 push @errors, "ERROR_password_mismatch" if ( $password ne $password2 );
300 push @errors, "ERROR_short_password" if( $password && $minpw && $password ne '****' && (length($password) < $minpw) );
302 if (C4::Context->preference('ExtendedPatronAttributes')) {
303 $extended_patron_attributes = parse_extended_patron_attributes($input);
304 foreach my $attr (@$extended_patron_attributes) {
305 unless (C4::Members::Attributes::CheckUniqueness($attr->{code}, $attr->{value}, $borrowernumber)) {
306 push @errors, "ERROR_extended_unique_id_failed";
307 $template->param(ERROR_extended_unique_id_failed => "$attr->{code}/$attr->{value}");
313 if ( ($op eq 'modify' || $op eq 'insert' || $op eq 'save'|| $op eq 'duplicate') and ($step == 0 or $step == 3 )){
314 unless ($newdata{'dateexpiry'}){
315 my $arg2 = $newdata{'dateenrolled'} || C4::Dates->today('iso');
316 $newdata{'dateexpiry'} = GetExpiryDate($newdata{'categorycode'},$arg2);
320 if ( ( defined $input->param('SMSnumber') ) && ( $input->param('SMSnumber') ne $newdata{'mobile'} ) ) {
321 $newdata{smsalertnumber} = $input->param('SMSnumber');
324 ### Error checks should happen before this line.
325 $nok = $nok || scalar(@errors);
326 if ((!$nok) and $nodouble and ($op eq 'insert' or $op eq 'save')){
327 $debug and warn "$op dates: " . join "\t", map {"$_: $newdata{$_}"} qw(dateofbirth dateenrolled dateexpiry);
328 if ($op eq 'insert'){
329 # we know it's not a duplicate borrowernumber or there would already be an error
330 $borrowernumber = &AddMember(%newdata);
331 $newdata{'borrowernumber'} = $borrowernumber;
333 # If 'AutoEmailOpacUser' syspref is on, email user their account details from the 'notice' that matches the user's branchcode.
334 if ( C4::Context->preference("AutoEmailOpacUser") == 1 && $newdata{'userid'} && $newdata{'password'}) {
335 #look for defined primary email address, if blank - attempt to use borr.email and borr.emailpro instead
336 my $emailaddr;
337 if (C4::Context->preference("AutoEmailPrimaryAddress") ne 'OFF' &&
338 $newdata{C4::Context->preference("AutoEmailPrimaryAddress")} =~ /\w\@\w/ ) {
339 $emailaddr = $newdata{C4::Context->preference("AutoEmailPrimaryAddress")}
341 elsif ($newdata{email} =~ /\w\@\w/) {
342 $emailaddr = $newdata{email}
344 elsif ($newdata{emailpro} =~ /\w\@\w/) {
345 $emailaddr = $newdata{emailpro}
347 elsif ($newdata{B_email} =~ /\w\@\w/) {
348 $emailaddr = $newdata{B_email}
350 # if we manage to find a valid email address, send notice
351 if ($emailaddr) {
352 $newdata{emailaddr} = $emailaddr;
353 my $err;
354 eval {
355 $err = SendAlerts ( 'members', \%newdata, "ACCTDETAILS" );
357 if ( $@ ) {
358 $template->param(error_alert => $@);
359 } elsif ( ref($err) eq "HASH" && defined $err->{error} and $err->{error} eq "no_email" ) {
360 $template->{VARS}->{'error_alert'} = "no_email";
361 } else {
362 $template->{VARS}->{'info_alert'} = 1;
367 if ($data{'organisations'}){
368 # need to add the members organisations
369 my @orgs=split(/\|/,$data{'organisations'});
370 add_member_orgs($borrowernumber,\@orgs);
372 if (C4::Context->preference('ExtendedPatronAttributes') and $input->param('setting_extended_patron_attributes')) {
373 C4::Members::Attributes::SetBorrowerAttributes($borrowernumber, $extended_patron_attributes);
375 if (C4::Context->preference('EnhancedMessagingPreferences') and $input->param('setting_messaging_prefs')) {
376 C4::Form::MessagingPreferences::handle_form_action($input, { borrowernumber => $borrowernumber }, $template, 1, $newdata{'categorycode'});
378 } elsif ($op eq 'save'){
379 if ($NoUpdateLogin) {
380 delete $newdata{'password'};
381 delete $newdata{'userid'};
383 &ModMember(%newdata) unless scalar(keys %newdata) <= 1; # bug 4508 - avoid crash if we're not
384 # updating any columns in the borrowers table,
385 # which can happen if we're only editing the
386 # patron attributes or messaging preferences sections
387 if (C4::Context->preference('ExtendedPatronAttributes') and $input->param('setting_extended_patron_attributes')) {
388 C4::Members::Attributes::SetBorrowerAttributes($borrowernumber, $extended_patron_attributes);
390 if (C4::Context->preference('EnhancedMessagingPreferences') and $input->param('setting_messaging_prefs')) {
391 C4::Form::MessagingPreferences::handle_form_action($input, { borrowernumber => $borrowernumber }, $template);
394 print scalar ($destination eq "circ") ?
395 $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber") :
396 $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$borrowernumber") ;
397 exit; # You can only send 1 redirect! After that, content or other headers don't matter.
400 if ($delete){
401 print $input->redirect("/cgi-bin/koha/deletemem.pl?member=$borrowernumber");
402 exit; # same as above
405 if ($nok or !$nodouble){
406 $op="add" if ($op eq "insert");
407 $op="modify" if ($op eq "save");
408 %data=%newdata;
409 $template->param( updtype => ($op eq 'add' ?'I':'M')); # used to check for $op eq "insert"... but we just changed $op!
410 unless ($step){
411 $template->param( step_1 => 1,step_2 => 1,step_3 => 1, step_4 => 1, step_5 => 1, step_6 => 1);
414 if (C4::Context->preference("IndependantBranches")) {
415 my $userenv = C4::Context->userenv;
416 if ($userenv->{flags} % 2 != 1 && $data{'branchcode'}){
417 unless ($userenv->{branch} eq $data{'branchcode'}){
418 print $input->redirect("/cgi-bin/koha/members/members-home.pl");
419 exit;
423 if ($op eq 'add'){
424 $template->param( updtype => 'I', step_1=>1, step_2=>1, step_3=>1, step_4=>1, step_5 => 1, step_6 => 1);
426 if ($op eq "modify") {
427 $template->param( updtype => 'M',modify => 1 );
428 $template->param( step_1=>1, step_2=>1, step_3=>1, step_4=>1, step_5 => 1, step_6 => 1) unless $step;
429 if ( $step == 4 ) {
430 $template->param( categorycode => $borrower_data->{'categorycode'} );
433 if ( $op eq "duplicate" ) {
434 $template->param( updtype => 'I' );
435 $template->param( step_1 => 1, step_2 => 1, step_3 => 1, step_4 => 1, step_5 => 1, step_6 => 1 ) unless $step;
438 $data{'cardnumber'}=fixup_cardnumber($data{'cardnumber'}) if $op eq 'add';
439 if(!defined($data{'sex'})){
440 $template->param( none => 1);
441 } elsif($data{'sex'} eq 'F'){
442 $template->param( female => 1);
443 } elsif ($data{'sex'} eq 'M'){
444 $template->param( male => 1);
445 } else {
446 $template->param( none => 1);
449 ##Now all the data to modify a member.
450 my ($categories,$labels)=ethnicitycategories();
452 my $ethnicitycategoriescount=$#{$categories};
453 my $ethcatpopup;
454 if ($ethnicitycategoriescount>=0) {
455 $ethcatpopup = CGI::popup_menu(-name=>'ethnicity',
456 -id => 'ethnicity',
457 -tabindex=>'',
458 -values=>$categories,
459 -default=>$data{'ethnicity'},
460 -labels=>$labels);
461 $template->param(ethcatpopup => $ethcatpopup); # bad style, has to be fixed
464 my @typeloop;
465 my $no_categories = 1;
466 my $no_add;
467 foreach (qw(C A S P I X)) {
468 my $action="WHERE category_type=?";
469 ($categories,$labels)=GetborCatFromCatType($_,$action);
470 if(scalar(@$categories) > 0){ $no_categories = 0; }
471 my @categoryloop;
472 foreach my $cat (@$categories){
473 push @categoryloop,{'categorycode' => $cat,
474 'categoryname' => $labels->{$cat},
475 'categorycodeselected' => ((defined($borrower_data->{'categorycode'}) &&
476 $cat eq $borrower_data->{'categorycode'})
477 || (defined($categorycode) && $cat eq $categorycode)),
480 my %typehash;
481 $typehash{'typename'}=$_;
482 my $typedescription = "typename_".$typehash{'typename'};
483 $typehash{'categoryloop'}=\@categoryloop;
484 push @typeloop,{'typename' => $_,
485 $typedescription => 1,
486 'categoryloop' => \@categoryloop};
488 $template->param('typeloop' => \@typeloop,
489 no_categories => $no_categories);
490 if($no_categories){ $no_add = 1; }
491 # test in city
492 if ( $guarantorid ) {
493 $select_city = getidcity($data{city});
495 ($default_city=$select_city) if ($step eq 0);
496 if (!defined($select_city) or $select_city eq '' ){
497 $default_city = &getidcity($data{'city'});
500 my $city_arrayref = GetCities();
501 if (@{$city_arrayref} ) {
502 $template->param( city_cgipopup => 1);
504 if ($default_city) { # flag the current or default val
505 for my $city ( @{$city_arrayref} ) {
506 if ($default_city == $city->{cityid}) {
507 $city->{selected} = 1;
508 last;
514 my $default_roadtype;
515 $default_roadtype=$data{'streettype'} ;
516 my($roadtypeid,$road_type)=GetRoadTypes();
517 $template->param( road_cgipopup => 1) if ($roadtypeid );
518 my $roadpopup = CGI::popup_menu(-name=>'streettype',
519 -id => 'streettype',
520 -values=>$roadtypeid,
521 -labels=>$road_type,
522 -override => 1,
523 -default=>$default_roadtype
526 my $default_borrowertitle;
527 $default_borrowertitle=$data{'title'} ;
528 my($borrowertitle)=GetTitles();
529 $template->param( title_cgipopup => 1) if ($borrowertitle);
530 my $borrotitlepopup = CGI::popup_menu(-name=>'title',
531 -id => 'btitle',
532 -values=>$borrowertitle,
533 -override => 1,
534 -default=>$default_borrowertitle
537 my @relationships = split /,|\|/, C4::Context->preference('borrowerRelationship');
538 my @relshipdata;
539 while (@relationships) {
540 my $relship = shift @relationships || '';
541 my %row = ('relationship' => $relship);
542 if (defined($data{'relationship'}) and $data{'relationship'} eq $relship) {
543 $row{'selected'}=' selected';
544 } else {
545 $row{'selected'}='';
547 push(@relshipdata, \%row);
550 my %flags = ( 'gonenoaddress' => ['gonenoaddress' ],
551 'lost' => ['lost']);
554 my @flagdata;
555 foreach (keys(%flags)) {
556 my $key = $_;
557 my %row = ('key' => $key,
558 'name' => $flags{$key}[0]);
559 if ($data{$key}) {
560 $row{'yes'}=' checked';
561 $row{'no'}='';
563 else {
564 $row{'yes'}='';
565 $row{'no'}=' checked';
567 push @flagdata,\%row;
570 #get Branches
571 my @branches;
572 my @select_branch;
573 my %select_branches;
575 my $onlymine=(C4::Context->preference('IndependantBranches') &&
576 C4::Context->userenv &&
577 C4::Context->userenv->{flags} % 2 !=1 &&
578 C4::Context->userenv->{branch}?1:0);
580 my $branches=GetBranches($onlymine);
581 my $default;
582 my $CGIbranch;
583 for my $branch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
584 push @select_branch,$branch;
585 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
586 $default = C4::Context->userenv->{'branch'} if (C4::Context->userenv && C4::Context->userenv->{'branch'});
588 if(scalar(@select_branch) > 0){
589 # --------------------------------------------------------------------------------------------------------
590 #in modify mod :default value from $CGIbranch comes from borrowers table
591 #in add mod: default value come from branches table (ip correspendence)
592 if (defined ($data{'branchcode'}) and ( $op eq 'modify' || ( $op eq 'add' && $category_type eq 'C' ) )) {
593 $default = $data{'branchcode'};
595 $CGIbranch = CGI::scrolling_list(-id => 'branchcode',
596 -name => 'branchcode',
597 -values => \@select_branch,
598 -labels => \%select_branches,
599 -size => 1,
600 -override => 1,
601 -multiple =>0,
602 -default => $default,
606 if(!$CGIbranch){
607 $no_add = 1;
608 $template->param(no_branches => 1);
610 if($no_categories){
611 $no_add = 1;
612 $template->param(no_categories => 1);
614 $template->param(no_add => $no_add);
615 my $CGIorganisations;
616 my $member_of_institution;
617 if (C4::Context->preference("memberofinstitution")){
618 my $organisations=get_institutions();
619 my @orgs;
620 my %org_labels;
621 foreach my $organisation (keys %$organisations) {
622 push @orgs,$organisation;
623 $org_labels{$organisation}=$organisations->{$organisation}->{'surname'};
625 $member_of_institution=1;
627 $CGIorganisations = CGI::scrolling_list( -id => 'organisations',
628 -name => 'organisations',
629 -labels => \%org_labels,
630 -values => \@orgs,
631 -size => 5,
632 -multiple => 'true'
637 # --------------------------------------------------------------------------------------------------------
639 my $CGIsort = buildCGIsort("Bsort1","sort1",$data{'sort1'});
640 if ($CGIsort) {
641 $template->param(CGIsort1 => $CGIsort);
643 $template->param( sort1 => $data{'sort1'}); # shouldn't this be in an "else" statement like the 2nd one?
645 $CGIsort = buildCGIsort("Bsort2","sort2",$data{'sort2'});
646 if ($CGIsort) {
647 $template->param(CGIsort2 => $CGIsort);
648 } else {
649 $template->param( sort2 => $data{'sort2'});
652 if ($nok) {
653 foreach my $error (@errors) {
654 $template->param($error) || $template->param( $error => 1);
656 $template->param(nok => 1);
659 #Formatting data for display
661 if (!defined($data{'dateenrolled'}) or $data{'dateenrolled'} eq ''){
662 $data{'dateenrolled'}=C4::Dates->today('iso');
664 if ( $op eq 'duplicate' ) {
665 $data{'dateenrolled'} = C4::Dates->today('iso');
666 $data{'dateexpiry'} = GetExpiryDate( $data{'categorycode'}, $data{'dateenrolled'} );
668 if (C4::Context->preference('uppercasesurnames')) {
669 $data{'surname'} =uc($data{'surname'} );
670 $data{'contactname'}=uc($data{'contactname'});
673 $data{debarred} = C4::Overdues::CheckBorrowerDebarred($borrowernumber);
674 $data{datedebarred} = $data{debarred} if ( $data{debarred} && $data{debarred} ne "9999-12-31" );
675 foreach (qw(dateenrolled dateexpiry dateofbirth datedebarred)) {
676 $data{$_} = format_date($data{$_}); # back to syspref for display
677 $template->param( $_ => $data{$_});
680 if (C4::Context->preference('ExtendedPatronAttributes')) {
681 $template->param(ExtendedPatronAttributes => 1);
682 patron_attributes_form($template, $borrowernumber);
685 if (C4::Context->preference('EnhancedMessagingPreferences')) {
686 if ($op eq 'add') {
687 C4::Form::MessagingPreferences::set_form_values({ categorycode => $categorycode }, $template);
688 } else {
689 C4::Form::MessagingPreferences::set_form_values({ borrowernumber => $borrowernumber }, $template);
691 $template->param(SMSSendDriver => C4::Context->preference("SMSSendDriver"));
692 $template->param(SMSnumber => defined $data{'smsalertnumber'} ? $data{'smsalertnumber'} : $data{'mobile'});
693 $template->param(TalkingTechItivaPhone => C4::Context->preference("TalkingTechItivaPhoneNotification"));
696 $template->param( "showguarantor" => ($category_type=~/A|I|S|X/) ? 0 : 1); # associate with step to know where you are
697 $debug and warn "memberentry step: $step";
698 $template->param(%data);
699 $template->param( "step_$step" => 1) if $step; # associate with step to know where u are
700 $template->param( step => $step ) if $step; # associate with step to know where u are
701 $template->param( debug => $debug ) if $debug;
703 $template->param(
704 BorrowerMandatoryField => C4::Context->preference("BorrowerMandatoryField"),#field to test with javascript
705 category_type => $category_type,#to know the category type of the borrower
706 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
707 select_city => $select_city,
708 "$category_type" => 1,# associate with step to know where u are
709 destination => $destination,#to know wher u come from and wher u must go in redirect
710 check_member => $check_member,#to know if the borrower already exist(=>1) or not (=>0)
711 "op$op" => 1);
713 $template->param(CGIbranch=>$CGIbranch) if ($CGIbranch);
714 $template->param(
715 nodouble => $nodouble,
716 borrowernumber => $borrowernumber, #register number
717 guarantorid => ($borrower_data->{'guarantorid'} || $guarantorid),
718 ethcatpopup => $ethcatpopup,
719 relshiploop => \@relshipdata,
720 city_loop => $city_arrayref,
721 roadpopup => $roadpopup,
722 borrotitlepopup => $borrotitlepopup,
723 guarantorinfo => $guarantorinfo,
724 flagloop => \@flagdata,
725 dateformat => C4::Dates->new()->visual(),
726 C4::Context->preference('dateformat') => 1,
727 check_categorytype =>$check_categorytype,#to recover the category type with checkcategorytype function
728 category_type =>$category_type,
729 modify => $modify,
730 nok => $nok,#flag to konw if an error
731 memberofinstution => $member_of_institution,
732 CGIorganisations => $CGIorganisations,
733 NoUpdateLogin => $NoUpdateLogin
736 if(defined($data{'flags'})){
737 $template->param(flags=>$data{'flags'});
739 if(defined($data{'contacttitle'})){
740 $template->param("contacttitle_" . $data{'contacttitle'} => "SELECTED");
744 output_html_with_http_headers $input, $cookie, $template->output;
746 sub parse_extended_patron_attributes {
747 my ($input) = @_;
748 my @patron_attr = grep { /^patron_attr_\d+$/ } $input->param();
750 my @attr = ();
751 my %dups = ();
752 foreach my $key (@patron_attr) {
753 my $value = $input->param($key);
754 next unless defined($value) and $value ne '';
755 my $password = $input->param("${key}_password");
756 my $code = $input->param("${key}_code");
757 next if exists $dups{$code}->{$value};
758 $dups{$code}->{$value} = 1;
759 push @attr, { code => $code, value => $value, password => $password };
761 return \@attr;
764 sub patron_attributes_form {
765 my $template = shift;
766 my $borrowernumber = shift;
768 my @types = C4::Members::AttributeTypes::GetAttributeTypes();
769 if (scalar(@types) == 0) {
770 $template->param(no_patron_attribute_types => 1);
771 return;
773 my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
774 my @classes = uniq( map {$_->{class}} @$attributes );
775 @classes = sort @classes;
777 # map patron's attributes into a more convenient structure
778 my %attr_hash = ();
779 foreach my $attr (@$attributes) {
780 push @{ $attr_hash{$attr->{code}} }, $attr;
783 my @attribute_loop = ();
784 my $i = 0;
785 my %items_by_class;
786 foreach my $type_code (map { $_->{code} } @types) {
787 my $attr_type = C4::Members::AttributeTypes->fetch($type_code);
788 my $entry = {
789 class => $attr_type->class(),
790 code => $attr_type->code(),
791 description => $attr_type->description(),
792 repeatable => $attr_type->repeatable(),
793 password_allowed => $attr_type->password_allowed(),
794 category => $attr_type->authorised_value_category(),
795 category_code => $attr_type->category_code(),
796 password => '',
798 if (exists $attr_hash{$attr_type->code()}) {
799 foreach my $attr (@{ $attr_hash{$attr_type->code()} }) {
800 my $newentry = { map { $_ => $entry->{$_} } %$entry };
801 $newentry->{value} = $attr->{value};
802 $newentry->{password} = $attr->{password};
803 $newentry->{use_dropdown} = 0;
804 if ($attr_type->authorised_value_category()) {
805 $newentry->{use_dropdown} = 1;
806 $newentry->{auth_val_loop} = GetAuthorisedValues($attr_type->authorised_value_category(), $attr->{value});
808 $i++;
809 $newentry->{form_id} = "patron_attr_$i";
810 push @{$items_by_class{$attr_type->class()}}, $newentry;
812 } else {
813 $i++;
814 my $newentry = { map { $_ => $entry->{$_} } %$entry };
815 if ($attr_type->authorised_value_category()) {
816 $newentry->{use_dropdown} = 1;
817 $newentry->{auth_val_loop} = GetAuthorisedValues($attr_type->authorised_value_category());
819 $newentry->{form_id} = "patron_attr_$i";
820 push @{$items_by_class{$attr_type->class()}}, $newentry;
823 while ( my ($class, @items) = each %items_by_class ) {
824 my $lib = GetAuthorisedValueByCode( 'PA_CLASS', $class ) || $class;
825 push @attribute_loop, {
826 class => $class,
827 items => @items,
828 lib => $lib,
832 $template->param(patron_attributes => \@attribute_loop);
836 # Local Variables:
837 # tab-width: 8
838 # End: