Show enumchron, copynumber in opac detail iff present.
[koha.git] / members / memberentry.pl
blob5cc8c004e009f2579c4670c9de5de20bcd9c3130
1 #!/usr/bin/perl
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
10 # version.
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
20 # pragma
21 use strict;
23 # external modules
24 use CGI;
25 # use Digest::MD5 qw(md5_base64);
27 # internal modules
28 use C4::Auth;
29 use C4::Context;
30 use C4::Output;
31 use C4::Members;
32 use C4::Koha;
33 use C4::Dates qw/format_date format_date_in_iso/;
34 use C4::Input;
35 use C4::Log;
36 use C4::Branch; # GetBranches
38 #use Smart::Comments;
40 use vars qw($debug);
42 BEGIN {
43 $debug = $ENV{DEBUG} || 0;
46 my $input = new CGI;
47 ($debug) or $debug = $input->param('debug') || 0;
48 my %data;
50 my $dbh = C4::Context->dbh;
52 my ($template, $loggedinuser, $cookie)
53 = get_template_and_user({template_name => "members/memberentrygen.tmpl",
54 query => $input,
55 type => "intranet",
56 authnotrequired => 0,
57 flagsrequired => {borrowers => 1},
58 debug => ($debug) ? 1 : 0,
59 });
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;
77 my @errors;
78 my $default_city;
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
82 my $borrower_data;
83 my $NoUpdateLogin;
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";
125 } else {
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})) ) {
133 $NoUpdateLogin =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(
143 $category_type_send,
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);
182 my $loginexist=0;
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";
208 $loginexist=1;
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.
245 if ($delete){
246 print $input->redirect("/cgi-bin/koha/deletemem.pl?member=$borrowernumber");
247 exit; # same as above
250 if ($nok){
251 $op="add" if ($op eq "insert");
252 $op="modify" if ($op eq "save");
253 %data=%newdata;
254 $template->param( updtype => ($op eq 'add' ?'I':'M')); # used to check for $op eq "insert"... but we just changed $op!
255 unless ($step){
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");
264 exit;
268 if ($op eq 'add'){
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);
284 } else {
285 $template->param(none => 1);
288 ##Now all the data to modify a member.
289 my ($categories,$labels)=ethnicitycategories();
291 my $ethnicitycategoriescount=$#{$categories};
292 my $ethcatpopup;
293 if ($ethnicitycategoriescount>=0) {
294 $ethcatpopup = CGI::popup_menu(-name=>'ethnicity',
295 -id => 'ethnicity',
296 -tabindex=>'',
297 -values=>$categories,
298 -default=>$data{'ethnicity'},
299 -labels=>$labels);
300 $template->param(ethcatpopup => $ethcatpopup); # bad style, has to be fixed
303 my @typeloop;
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);
307 my @categoryloop;
308 foreach my $cat (@$categories){
309 push @categoryloop,{'categorycode' => $cat,
310 'categoryname' => $labels->{$cat},
311 'categorycodeselected' => ($cat eq $borrower_data->{'categorycode'}),
314 my %typehash;
315 $typehash{'typename'}=$_;
316 $typehash{'categoryloop'}=\@categoryloop;
317 push @typeloop,{'typename' => $_,
318 'categoryloop' => \@categoryloop};
320 $template->param('typeloop' => \@typeloop);
322 # test in city
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'});
328 my($cityid);
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',
333 -values=>$name_city,
334 -labels=>$name_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',
343 -id => 'streettype',
344 -values=>$roadtypeid,
345 -labels=>$road_type,
346 -override => 1,
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',
355 -id => 'btitle',
356 -values=>$borrowertitle,
357 -override => 1,
358 -default=>$default_borrowertitle
361 my @relationships = split /,|\|/,C4::Context->preference('BorrowerRelationship');
362 my @relshipdata;
363 while (@relationships) {
364 my $relship = shift @relationships || '';
365 my %row = ('relationship' => $relship);
366 if ($data{'relationship'} eq $relship) {
367 $row{'selected'}=' selected';
368 } else {
369 $row{'selected'}='';
371 push(@relshipdata, \%row);
374 my %flags = ( 'gonenoaddress' => ['gonenoaddress' ],
375 'lost' => ['lost'],
376 'debarred' => ['debarred']);
379 my @flagdata;
380 foreach (keys(%flags)) {
381 my $key = $_;
382 my %row = ('key' => $key,
383 'name' => $flags{$key}[0]);
384 if ($data{$key}) {
385 $row{'yes'}=' checked';
386 $row{'no'}='';
388 else {
389 $row{'yes'}='';
390 $row{'no'}=' checked';
392 push @flagdata,\%row;
395 #get Branches
396 my @branches;
397 my @select_branch;
398 my %select_branches;
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);
406 my $default;
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,
421 -size => 1,
422 -override => 1,
423 -multiple =>0,
424 -default => $default,
426 my $CGIorganisations;
427 my $member_of_institution;
428 if (C4::Context->preference("memberofinstitution")){
429 my $organisations=get_institutions();
430 my @orgs;
431 my %org_labels;
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,
441 -values => \@orgs,
442 -size => 5,
443 -multiple => 'true'
449 # --------------------------------------------------------------------------------------------------------
451 my $CGIsort = buildCGIsort("Bsort1","sort1",$data{'sort1'});
452 if ($CGIsort) {
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'});
458 if ($CGIsort) {
459 $template->param(CGIsort2 => $CGIsort);
460 } else {
461 $template->param( sort2 => $data{'sort2'});
464 if ($nok) {
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;
491 $template->param(
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'},
500 "op$op" => 1,
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
515 modify => $modify,
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;
525 # Local Variables:
526 # tab-width: 8
527 # End: