4 # Copyright 2006 SAN OUEST PROVENCE et Paul POULAIN
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
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 with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA 02111-1307 USA
25 use Date::Calc qw/Today/;
29 use Digest::MD5 qw(md5_base64);
35 use C4::Interface::CGI::Output;
47 my $dbh = C4::Context->dbh;
49 my $category_type = $input->param('category_type') || die "NO CATEGORY_TYPE !"; # A, E, C, or P
50 my $step=$input->param('step') || 0;
51 my ($template, $loggedinuser, $cookie)
52 = get_template_and_user({template_name => "members/memberentry$category_type.tmpl",
56 flagsrequired => {borrowers => 1},
60 my $borrowerid=$input->param('borrowerid');
61 my $guarantorid=$input->param('guarantorid');
62 my $borrowernumber=$input->param('borrowernumber');
63 my $actionType=$input->param('actionType') || '';
64 my $modify=$input->param('modify');
65 my $delete=$input->param('delete');
66 my $op=$input->param('op');
67 my $categorycode=$input->param('categorycode');
68 my $destination=$input->param('destination');
69 my $cardnumber=$input->param('cardnumber');
70 my $check_member=$input->param('check_member');
71 my $name_city=$input->param('name_city');
72 my $nodouble=$input->param('nodouble');
73 my $select_city=$input->param('select_city');
74 my $nok=$input->param('nok');
78 # $check_categorytype contains the value of duplicate borrowers category type to redirect in good template in step =2
79 my $check_categorytype=$input->param('check_categorytype');
80 # NOTE: Alert for ethnicity and ethnotes fields, they are unvalided in all borrowers form
83 #function to automatic setup the mandatory fields (visual with css)
84 my $check_BorrowerMandatoryField=C4::Context->preference("BorrowerMandatoryField");
85 my @field_check=split(/\|/,$check_BorrowerMandatoryField);
86 foreach (@field_check) {
87 $template->param( "mandatory$_" => 1);
90 $template->param( "checked" => 1) if ($nodouble eq 1);
93 # if a add or modify is requested => check validity of data.
94 if ($op eq 'add' or $op eq 'modify') {
95 my @names=$input->param;
96 foreach my $key (@names){
97 $data{$key}=$input->param($key)||'';
98 $data{$key}=~ s/\'/\\\'/g;
99 $data{$key}=~ s/\"/\\\"/g;
102 #############test for member being unique #############
103 if ($op eq 'add' && $step eq 2){
104 (my $category_type_send=$category_type ) if ($category_type eq 'I');
105 my $check_category; # recover the category code of the doublon suspect borrowers
106 ($check_member,$check_category)= checkuniquemember($category_type_send,$data{'surname'},$data{'firstname'},format_date_in_iso($data{'dateofbirth'}));
107 # recover the category type if the borrowers is a duplicate
108 ($check_categorytype,undef)=getcategorytype($check_category);
111 # CHECKS step by step
114 ###############test to take the right zipcode and city name ##############
115 if ($category_type ne 'I' and $guarantorid){
116 my ($borrower_city,$borrower_zipcode)=&getzipnamecity($select_city);
117 $data{'city'}= $borrower_city;
118 $data{'zipcode'}=$borrower_zipcode;
120 if ($category_type eq 'C' and $guarantorid){
121 my $guarantordata=getguarantordata($guarantorid);
122 if (($data{'contactname'} eq '' or $data{'contactname'} ne $guarantordata->{'surname'})) {
123 $data{'contactfirstname'}=$guarantordata->{'firstname'};
124 $data{'contactname'}=$guarantordata->{'surname'};
125 $data{'contacttitle'}=$guarantordata->{'title'};
126 $data{'streetnumber'}=$guarantordata->{'streetnumber'};
127 $data{'address'}=$guarantordata->{'address'};
128 $data{'streettype'}=$guarantordata->{'streettype'};
129 $data{'address2'}=$guarantordata->{'address2'};
130 $data{'zipcode'}=$guarantordata->{'zipcode'};
131 $data{'city'}=$guarantordata->{'city'};
132 $data{'phone'}=$guarantordata->{'phone'};
133 $data{'phonepro'}=$guarantordata->{'phonepro'};
134 $data{'mobile'}=$guarantordata->{'mobile'};
135 $data{'fax'}=$guarantordata->{'fax'};
136 $data{'email'}=$guarantordata->{'email'};
137 $data{'emailpro'}=$guarantordata->{'emailpro'};
140 if ($categorycode ne 'I') {
141 # is the age of the borrower compatible with age limitations of
142 # the borrower category
144 SELECT upperagelimit,
147 WHERE categorycode = ?
149 my $sth=$dbh->prepare($query);
150 $sth->execute($categorycode);
151 my $category_info = $sth->fetchrow_hashref;
153 my $age = get_age(format_date_in_iso($data{dateofbirth}));
155 if ($age > $category_info->{upperagelimit}
156 or $age < $category_info->{dateofbirthrequired}
158 push @errors, 'ERROR_age_limitations';
165 if ( ($data{'login'} eq '')){
166 my $onefirstnameletter=substr($data{'firstname'},0,1);
167 my $fivesurnameletter=substr($data{'surname'},0,5);
168 $data{'login'}=lc($onefirstnameletter.$fivesurnameletter);
170 if ($op eq 'add' and $data{'dateenrolled'} eq ''){
172 #insert ,in field "dateenrolled" , the current date
173 $data{'dateenrolled'}=$today;
174 #if date expiry is null u must calculate the value only in this case
175 $data{'dateexpiry'} = calcexpirydate($data{'categorycode'},$today);
177 if ($op eq 'modify' ){
179 # if date expiry is null u must calculate the value only in this case
180 if ($data{'dateexpiry'} eq ''){
181 $data{'dateexpiry'} = calcexpirydate($data{'categorycode'},$today);
187 # this value show if the login and password are been used
188 my $loginexist=checkuserpassword($borrowerid,$data{'login'},$data{'password'});
189 # test to know if u must save or create the borrowers
190 if ($op eq 'modify'){
191 # test to know if another user have the same password and same login
192 if ($loginexist eq 0) {
194 logaction($loggedinuser,"MEMBERS","modify member", $borrowerid, "");
197 push @errors, "ERROR_login_exist";
201 # test to know if another user have the same password and same login
203 push @errors, "ERROR_login_exist";
206 $borrowerid = &newmember(%data);
207 if ($data{'organisations'}){
208 # need to add the members organisations
209 add_member_orgs($borrowerid,$data{'organisations'});
211 logaction($loggedinuser,"MEMBERS","add member", $borrowerid, "");
216 if($destination eq "circ"){
217 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$data{'cardnumber'}");
219 if ($loginexist == 0) {
220 print $input->redirect("/cgi-bin/koha/members/moremember.pl?bornum=$borrowerid");
225 if (C4::Context->preference("IndependantBranches")) {
226 my $userenv = C4::Context->userenv;
227 if ($userenv->{flags} != 1){
228 unless ($userenv->{branch} eq $data{'branchcode'}){
229 push @errors, "ERROR_branch";
237 print $input->redirect("/cgi-bin/koha/deletemem.pl?member=$borrowerid");
238 print $input->redirect("/cgi-bin/koha/deletemem.pl?member=$borrowernumber");
239 } else { # this else goes down the whole script
240 # retrieve previous values : either in DB or in CGI, in case of errors in values
242 # test to now if u add or modify a borrower (modify =>to take all carateristic of the borrowers)
243 if (!$op and !$data{'surname'}) {
244 $data=borrdata('',$borrowerid);
247 if (C4::Context->preference("IndependantBranches")) {
248 my $userenv = C4::Context->userenv;
249 if ($userenv->{flags} != 1 && $data{branchcode}){
250 unless ($userenv->{branch} eq $data{'branchcode'}){
251 print $input->redirect("/cgi-bin/koha/members/members-home.pl");
256 $template->param( updtype => 'I');
258 $template->param( updtype => 'M');
260 my $cardnumber=$data{'cardnumber'};
261 $cardnumber=fixup_cardnumber($data{'cardnumber'}) if $op eq 'add';
262 if ($data{'sex'} eq 'F'){
263 $template->param(female => 1);
265 my ($categories,$labels)=ethnicitycategories();
266 my $ethnicitycategoriescount=$#{$categories};
268 if ($ethnicitycategoriescount>=0) {
269 $ethcatpopup = CGI::popup_menu(-name=>'ethnicity',
271 -values=>$categories,
272 -default=>$data{'ethnicity'},
274 $template->param(ethcatpopup => $ethcatpopup); # bad style, has to be fixed
278 ($categories,$labels)=borrowercategories($category_type,$op);
280 #if u modify the borrowers u must have the right value for is category code
282 (my $default_category=$data{'categorycode'}) if ($op eq '');
283 my $catcodepopup = CGI::popup_menu(-name=>'categorycode',
284 -id => 'categorycode',
285 -values=>$categories,
286 -default=>$default_category,
291 (my $selectcity=&getidcity($data{'city'})) if ($select_city eq '');
292 $default_city=$selectcity;
294 my($cityid,$name_city)=getcities();
295 $template->param( city_cgipopup => 1) if ($cityid );
296 my $citypopup = CGI::popup_menu(-name=>'select_city',
297 -id => 'select_city',
301 -default=>$default_city
304 my $default_roadtype;
305 $default_roadtype=$data{'streettype'} ;
306 my($roadtypeid,$road_type)=getroadtypes();
307 $template->param( road_cgipopup => 1) if ($roadtypeid );
308 my $roadpopup = CGI::popup_menu(-name=>'streettype',
310 -values=>$roadtypeid,
313 -default=>$default_roadtype
317 my @relationships = split /,|\|/,C4::Context->preference('BorrowerRelationship');
319 while (@relationships) {
320 my $relship = shift @relationships || '';
321 my %row = ('relationship' => $relship);
322 if ($data{'relationship'} eq $relship) {
323 $row{'selected'}=' selected';
327 push(@relshipdata, \%row);
329 my %flags = ( 'gonenoaddress' => ['gonenoaddress', 'Adresse érronée'],
330 'lost' => ['lost', 'Carte Perdue'],
331 'debarred' => ['debarred', 'Lecteur exclu']);
334 foreach (keys(%flags)) {
336 my %row = ('key' => $key,
337 'name' => $flags{$key}[0],
338 'html' => $flags{$key}[1]);
340 $row{'yes'}=' checked';
344 $row{'no'}=' checked';
346 push(@flagdata, \%row);
350 $template->param( modify => 1 );
353 #Convert dateofbirth to correct format
354 $data{'dateofbirth'} = format_date($data{'dateofbirth'});
358 my $branches=getbranches();
360 # -----------------------------------------------------
361 # the value of ip from the branches hash table
363 # $ip is the ip of user when is connect to koha
364 my $ip = $ENV{'REMOTE_ADDR'};
365 # -----------------------------------------------------
366 foreach my $branch (keys %$branches) {
367 if ((not C4::Context->preference("IndependantBranches")) || (C4::Context->userenv->{'flags'} == 1)) {
368 push @select_branch, $branch;
369 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
370 # take the ip number from branches "op"
371 $select_ip = $branches->{$branch}->{'branchip'} || '';
373 # test $select_ip equal $ip to attribute the default value for the scrolling list
374 if ($select_ip eq $ip) {
375 $default = $branches->{$branch}->{'branchcode'};
378 push @select_branch, $branch if ($branch eq C4::Context->userenv->{'branch'});
379 $select_branches{$branch} = $branches->{$branch}->{'branchname'} if ($branch eq C4::Context->userenv->{'branch'});
381 $default = C4::Context->userenv->{'branch'};
385 # --------------------------------------------------------------------------------------------------------
386 my $CGIbranch = CGI::scrolling_list(-id => 'branchcode',
387 -name => 'branchcode',
388 -values => \@select_branch,
389 -labels => \%select_branches,
393 -default => $default,
395 my $CGIorganisations;
396 my $member_of_institution;
397 if (C4::Context->preference("memberofinstitution")){
398 my $organisations=get_institutions();
401 foreach my $organisation (keys %$organisations) {
402 push @orgs,$organisation;
403 $org_labels{$organisation}=$organisations->{$organisation}->{'surname'};
405 $member_of_institution=1;
407 $CGIorganisations = CGI::scrolling_list( -id => 'organisations',
408 -name => 'organisations',
409 -labels => \%org_labels,
419 # --------------------------------------------------------------------------------------------------------
421 my $CGIsort1 = buildCGIsort("Bsort1","sort1",$data{'sort1'});
423 $template->param(CGIsort1 => $CGIsort1);
424 $template->param( sort1 => $data{'sort1'});
426 $template->param( sort1 => $data{'sort1'});
429 my $CGIsort2 = buildCGIsort("Bsort2","sort2",$data{'sort2'});
431 $template->param(CGIsort2 =>$CGIsort2);
433 $template->param( sort2 => $data{'sort2'});
437 $data{'opacnotes'} =~ s/\\//g;
438 $data{'borrowernotes'} =~ s/\\//g;
440 # increase step to see next page
442 foreach my $error (@errors) {
443 $template->param( $error => 1);
445 $template->param(nok => 1);
451 warn "CITY".$data{city};
453 BorrowerMandatoryField => C4::Context->preference("BorrowerMandatoryField"),#field to test with javascript
454 category_type => $category_type,#to know the category type of the borrower
455 select_city => $select_city,
456 "step_$step" => 1,# associate with step to know where u are
458 destination => $destination,#to know wher u come from and wher u must go in redirect
459 check_member => $check_member,#to know if the borrower already exist(=>1) or not (=>0)
460 # flags =>$data{'flags'},
463 nodouble => $nodouble,
464 borrowerid => $borrowerid,#register number
465 cardnumber => $data{'cardnumber'},
466 surname => uc($data{'surname'}),
467 firstname => ucfirst($data{'firstname'}),
468 "title_".$data{'title'} => " SELECTED ",
469 title => $data{'title'},
470 othernames => $data{'othernames'},
471 initials => $data{'initials'},
472 streetnumber => $data{'streetnumber'},
473 streettype =>$data{'streettype'},
474 address => $data{'address'},
475 address2 => $data{'address2'},
476 city => $data{'city'},
477 zipcode => $data{'zipcode'},
478 email => $data{'email'},
479 phone => $data{'phone'},
480 mobile => $data{'mobile'},
482 phonepro => $data{'phonepro'},
483 emailpro => $data{'emailpro'},
484 b_address => $data{'b_address'},
485 b_city => $data{'b_city'},
486 b_zipcode => $data{'b_zipcode'},
487 b_email => $data{'b_email'},
488 b_phone => $data{'b_phone'},
489 dateofbirth => $data{'dateofbirth'},
490 branchcode => $data{'branchcode'},
491 catcodepopup => $catcodepopup,
492 categorycode => $data{'categorycode'},
493 dateenrolled => format_date($data{'dateenrolled'}),
494 dateexpiry => format_date($data{'dateexpiry'}),
495 debarred => $data{'debarred'},
496 gonenoaddress => $data{'gonenoaddress'},
497 lost => $data{'lost'},
498 contactname => uc($data{'contactname'}),
499 contactfirstname=> ucfirst($data{'contactfirstname'}),
500 "contacttitle_".$data{'contacttitle'} => "SELECTED" ,
501 contacttitle => $data{'contacttitle'},
502 guarantorid => $guarantorid,
503 ethcatpopup => $ethcatpopup,
505 login => $data{'login'},
506 password => $data{'password'},
507 opacnotes => $data{'opacnotes'},
508 contactnotes => $data{'contactnotes'},
509 borrowernotes => $data{'borrowernotes'},
510 relshiploop => \@relshipdata,
511 relationship => $data{'relationship'},
512 citypopup => $citypopup,
513 roadpopup => $roadpopup,
514 contacttype => $data{'contacttype'},
515 organisations => $data{'organisations'},
516 flagloop => \@flagdata,
517 # "contacttype_".$data{'contacttype'} =>" SELECTED ",
518 dateformat => display_date_format(),
519 check_categorytype =>$check_categorytype,#to recover the category type with checkcategorytype function
521 # city_choice => $city_choice ,#check if the city was selected
522 nok => $nok,#flag to konw if an error
523 CGIbranch => $CGIbranch,
524 memberofinstution => $member_of_institution,
525 CGIorganisations => $CGIorganisations,
527 #$template->param(Institution => 1) if ($categorycode eq "I");
528 output_html_with_http_headers $input, $cookie, $template->output;
532 my ($date, $date_ref) = @_;
534 if (not defined $date_ref) {
535 $date_ref = sprintf('%04d-%02d-%02d', Today());
538 my ($year1, $month1, $day1) = split /-/, $date;
539 my ($year2, $month2, $day2) = split /-/, $date_ref;
541 my $age = $year2 - $year1;
542 if ($month1.$day1 > $month2.$day2) {