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 $modify=$input->param('modify');
63 my $delete=$input->param('delete');
64 my $op=$input->param('op');
65 my $categorycode=$input->param('categorycode');
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 $nok=$input->param('nok');
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 unvalided in all borrowers form
81 #function to automatic setup the mandatory fields (visual with css)
82 my $check_BorrowerMandatoryField=C4::Context->preference("BorrowerMandatoryField");
83 my @field_check=split(/\|/,$check_BorrowerMandatoryField);
84 foreach (@field_check) {
85 $template->param( "mandatory$_" => 1);
88 $template->param( "checked" => 1) if ($nodouble eq 1);
90 # if a add or modify is requested => check validity of data.
91 if ($op eq 'add' or $op eq 'modify') {
92 my @names=$input->param;
93 foreach my $key (@names){
94 $data{$key}=$input->param($key)||'';
95 $data{$key}=~ s/\'/\\\'/g;
96 $data{$key}=~ s/\"/\\\"/g;
99 #############test for member being unique #############
100 if ($op eq 'add' && $step eq 2){
101 (my $category_type_send=$category_type ) if ($category_type eq 'I');
102 my $check_category; # recover the category code of the doublon suspect borrowers
103 ($check_member,$check_category)= checkuniquemember($category_type_send,$data{'surname'},$data{'firstname'},format_date_in_iso($data{'dateofbirth'}));
104 # recover the category type if the borrowers is a duplicate
105 ($check_categorytype,undef)=getcategorytype($check_category);
108 # CHECKS step by step
111 ###############test to take the right zipcode and city name ##############
112 if ($category_type ne 'I' and $guarantorid){
113 my ($borrower_city,$borrower_zipcode)=&getzipnamecity($select_city);
114 $data{'city'}= $borrower_city;
115 $data{'zipcode'}=$borrower_zipcode;
117 if ($category_type eq 'C' and $guarantorid){
118 my $guarantordata=getguarantordata($guarantorid);
119 if (($data{'contactname'} eq '' or $data{'contactname'} ne $guarantordata->{'surname'})) {
120 $data{'contactfirstname'}=$guarantordata->{'firstname'};
121 $data{'contactname'}=$guarantordata->{'surname'};
122 $data{'contacttitle'}=$guarantordata->{'title'};
123 $data{'streetnumber'}=$guarantordata->{'streetnumber'};
124 $data{'address'}=$guarantordata->{'address'};
125 $data{'streettype'}=$guarantordata->{'streettype'};
126 $data{'address2'}=$guarantordata->{'address2'};
127 $data{'zipcode'}=$guarantordata->{'zipcode'};
128 $data{'city'}=$guarantordata->{'city'};
129 $data{'phone'}=$guarantordata->{'phone'};
130 $data{'phonepro'}=$guarantordata->{'phonepro'};
131 $data{'mobile'}=$guarantordata->{'mobile'};
132 $data{'fax'}=$guarantordata->{'fax'};
133 $data{'email'}=$guarantordata->{'email'};
134 $data{'emailpro'}=$guarantordata->{'emailpro'};
137 if ($categorycode ne 'I') {
138 # is the age of the borrower compatible with age limitations of
139 # the borrower category
141 SELECT upperagelimit,
144 WHERE categorycode = ?
146 my $sth=$dbh->prepare($query);
147 $sth->execute($categorycode);
148 my $category_info = $sth->fetchrow_hashref;
150 my $age = get_age(format_date_in_iso($data{dateofbirth}));
152 if ($age > $category_info->{upperagelimit}
153 or $age < $category_info->{dateofbirthrequired}
155 push @errors, 'ERROR_age_limitations';
162 if ( ($data{'login'} eq '')){
163 my $onefirstnameletter=substr($data{'firstname'},0,1);
164 my $fivesurnameletter=substr($data{'surname'},0,5);
165 $data{'login'}=lc($onefirstnameletter.$fivesurnameletter);
167 if ($op eq 'add' and $data{'dateenrolled'} eq ''){
169 #insert ,in field "dateenrolled" , the current date
170 $data{'dateenrolled'}=$today;
171 #if date expiry is null u must calculate the value only in this case
172 $data{'dateexpiry'} = calcexpirydate($data{'categorycode'},$today);
174 if ($op eq 'modify' ){
176 # if date expiry is null u must calculate the value only in this case
177 if ($data{'dateexpiry'} eq ''){
178 $data{'dateexpiry'} = calcexpirydate($data{'categorycode'},$today);
184 # this value show if the login and password are been used
185 my $loginexist=checkuserpassword($borrowerid,$data{'login'},$data{'password'});
186 # test to know if u must save or create the borrowers
187 if ($op eq 'modify'){
188 # test to know if another user have the same password and same login
189 if ($loginexist eq 0) {
191 logaction($loggedinuser,"MEMBERS","modify member", $borrowerid, "");
194 push @errors, "ERROR_login_exist";
198 # test to know if another user have the same password and same login
200 push @errors, "ERROR_login_exist";
203 $borrowerid = &newmember(%data);
204 logaction($loggedinuser,"MEMBERS","add member", $borrowerid, "");
209 if($destination eq "circ"){
210 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$data{'cardnumber'}");
212 if ($loginexist == 0) {
213 print $input->redirect("/cgi-bin/koha/members/moremember.pl?bornum=$borrowerid");
218 if (C4::Context->preference("IndependantBranches")) {
219 my $userenv = C4::Context->userenv;
220 if ($userenv->{flags} != 1){
221 unless ($userenv->{branch} eq $data{'branchcode'}){
222 push @errors, "ERROR_branch";
230 print $input->redirect("/cgi-bin/koha/deletemem.pl?member=$borrowerid");
231 } else { # this else goes down the whole script
232 # retrieve previous values : either in DB or in CGI, in case of errors in values
234 # test to now if u add or modify a borrower (modify =>to take all carateristic of the borrowers)
235 if (!$op and !$data{'surname'}) {
236 $data=borrdata('',$borrowerid);
239 if (C4::Context->preference("IndependantBranches")) {
240 my $userenv = C4::Context->userenv;
241 if ($userenv->{flags} != 1 && $data{branchcode}){
242 unless ($userenv->{branch} eq $data{'branchcode'}){
243 print $input->redirect("/cgi-bin/koha/members/members-home.pl");
248 $template->param( updtype => 'I');
250 $template->param( updtype => 'M');
252 my $cardnumber=$data{'cardnumber'};
253 $cardnumber=fixup_cardnumber($data{'cardnumber'}) if $op eq 'add';
254 if ($data{'sex'} eq 'F'){
255 $template->param(female => 1);
257 my ($categories,$labels)=ethnicitycategories();
258 my $ethnicitycategoriescount=$#{$categories};
260 if ($ethnicitycategoriescount>=0) {
261 $ethcatpopup = CGI::popup_menu(-name=>'ethnicity',
263 -values=>$categories,
264 -default=>$data{'ethnicity'},
266 $template->param(ethcatpopup => $ethcatpopup); # bad style, has to be fixed
270 ($categories,$labels)=borrowercategories($category_type,$op);
272 #if u modify the borrowers u must have the right value for is category code
274 (my $default_category=$data{'categorycode'}) if ($op eq '');
275 my $catcodepopup = CGI::popup_menu(-name=>'categorycode',
276 -id => 'categorycode',
277 -values=>$categories,
278 -default=>$default_category,
283 (my $selectcity=&getidcity($data{'city'})) if ($select_city eq '');
284 $default_city=$selectcity;
286 my($cityid,$name_city)=getcities();
287 $template->param( city_cgipopup => 1) if ($cityid );
288 my $citypopup = CGI::popup_menu(-name=>'select_city',
289 -id => 'select_city',
293 -default=>$default_city
296 my $default_roadtype;
297 $default_roadtype=$data{'streettype'} ;
298 my($roadtypeid,$road_type)=getroadtypes();
299 $template->param( road_cgipopup => 1) if ($roadtypeid );
300 my $roadpopup = CGI::popup_menu(-name=>'streettype',
302 -values=>$roadtypeid,
305 -default=>$default_roadtype
309 my @relationships = split /,|\|/,C4::Context->preference('BorrowerRelationship');
311 while (@relationships) {
312 my $relship = shift @relationships || '';
313 my %row = ('relationship' => $relship);
314 if ($data{'relationship'} eq $relship) {
315 $row{'selected'}=' selected';
319 push(@relshipdata, \%row);
321 my %flags = ( 'gonenoaddress' => ['gonenoaddress', 'Adresse érronée'],
322 'lost' => ['lost', 'Carte Perdue'],
323 'debarred' => ['debarred', 'Lecteur exclu']);
326 foreach (keys(%flags)) {
328 my %row = ('key' => $key,
329 'name' => $flags{$key}[0],
330 'html' => $flags{$key}[1]);
332 $row{'yes'}=' checked';
336 $row{'no'}=' checked';
338 push(@flagdata, \%row);
342 $template->param( modify => 1 );
345 #Convert dateofbirth to correct format
346 $data{'dateofbirth'} = format_date($data{'dateofbirth'});
350 my $branches=getbranches();
352 # -----------------------------------------------------
353 # the value of ip from the branches hash table
355 # $ip is the ip of user when is connect to koha
356 my $ip = $ENV{'REMOTE_ADDR'};
357 # -----------------------------------------------------
358 foreach my $branch (keys %$branches) {
359 if ((not C4::Context->preference("IndependantBranches")) || (C4::Context->userenv->{'flags'} == 1)) {
360 push @select_branch, $branch;
361 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
362 # take the ip number from branches "op"
363 $select_ip = $branches->{$branch}->{'branchip'} || '';
365 # test $select_ip equal $ip to attribute the default value for the scrolling list
366 if ($select_ip eq $ip) {
367 $default = $branches->{$branch}->{'branchcode'};
370 push @select_branch, $branch if ($branch eq C4::Context->userenv->{'branch'});
371 $select_branches{$branch} = $branches->{$branch}->{'branchname'} if ($branch eq C4::Context->userenv->{'branch'});
373 $default = C4::Context->userenv->{'branch'};
377 # --------------------------------------------------------------------------------------------------------
378 my $CGIbranch = CGI::scrolling_list(-id => 'branchcode',
379 -name => 'branchcode',
380 -values => \@select_branch,
381 -labels => \%select_branches,
385 -default => $default,
389 # --------------------------------------------------------------------------------------------------------
391 my $CGIsort1 = buildCGIsort("Bsort1","sort1",$data{'sort1'});
393 $template->param(CGIsort1 => $CGIsort1);
394 $template->param( sort1 => $data{'sort1'});
396 $template->param( sort1 => $data{'sort1'});
399 my $CGIsort2 = buildCGIsort("Bsort2","sort2",$data{'sort2'});
401 $template->param(CGIsort2 =>$CGIsort2);
403 $template->param( sort2 => $data{'sort2'});
407 $data{'opacnotes'} =~ s/\\//g;
408 $data{'borrowernotes'} =~ s/\\//g;
410 # increase step to see next page
412 foreach my $error (@errors) {
413 $template->param( $error => 1);
415 $template->param(nok => 1);
421 warn "CITY".$data{city};
423 BorrowerMandatoryField => C4::Context->preference("BorrowerMandatoryField"),#field to test with javascript
424 category_type => $category_type,#to know the category type of the borrower
425 select_city => $select_city,
426 "step_$step" => 1,# associate with step to know where u are
428 destination => $destination,#to know wher u come from and wher u must go in redirect
429 check_member => $check_member,#to know if the borrower already exist(=>1) or not (=>0)
430 # flags =>$data{'flags'},
433 nodouble => $nodouble,
434 borrowerid => $borrowerid,#register number
435 cardnumber => $data{'cardnumber'},
436 surname => uc($data{'surname'}),
437 firstname => ucfirst($data{'firstname'}),
438 "title_".$data{'title'} => " SELECTED ",
439 title => $data{'title'},
440 othernames => $data{'othernames'},
441 initials => $data{'initials'},
442 streetnumber => $data{'streetnumber'},
443 streettype =>$data{'streettype'},
444 address => $data{'address'},
445 address2 => $data{'address2'},
446 city => $data{'city'},
447 zipcode => $data{'zipcode'},
448 email => $data{'email'},
449 phone => $data{'phone'},
450 mobile => $data{'mobile'},
452 phonepro => $data{'phonepro'},
453 emailpro => $data{'emailpro'},
454 b_address => $data{'b_address'},
455 b_city => $data{'b_city'},
456 b_zipcode => $data{'b_zipcode'},
457 b_email => $data{'b_email'},
458 b_phone => $data{'b_phone'},
459 dateofbirth => $data{'dateofbirth'},
460 branchcode => $data{'branchcode'},
461 catcodepopup => $catcodepopup,
462 categorycode => $data{'categorycode'},
463 dateenrolled => format_date($data{'dateenrolled'}),
464 dateexpiry => format_date($data{'dateexpiry'}),
465 debarred => $data{'debarred'},
466 gonenoaddress => $data{'gonenoaddress'},
467 lost => $data{'lost'},
468 contactname => uc($data{'contactname'}),
469 contactfirstname=> ucfirst($data{'contactfirstname'}),
470 "contacttitle_".$data{'contacttitle'} => "SELECTED" ,
471 contacttitle => $data{'contacttitle'},
472 guarantorid => $guarantorid,
473 ethcatpopup => $ethcatpopup,
475 login => $data{'login'},
476 password => $data{'password'},
477 opacnotes => $data{'opacnotes'},
478 contactnotes => $data{'contactnotes'},
479 borrowernotes => $data{'borrowernotes'},
480 relshiploop => \@relshipdata,
481 relationship => $data{'relationship'},
482 citypopup => $citypopup,
483 roadpopup => $roadpopup,
484 contacttype => $data{'contacttype'},
485 flagloop => \@flagdata,
486 # "contacttype_".$data{'contacttype'} =>" SELECTED ",
487 dateformat => display_date_format(),
488 check_categorytype =>$check_categorytype,#to recover the category type with checkcategorytype function
490 # city_choice => $city_choice ,#check if the city was selected
491 nok => $nok,#flag to konw if an error
492 CGIbranch => $CGIbranch,
494 #$template->param(Institution => 1) if ($categorycode eq "I");
495 output_html_with_http_headers $input, $cookie, $template->output;
499 my ($date, $date_ref) = @_;
501 if (not defined $date_ref) {
502 $date_ref = sprintf('%04d-%02d-%02d', Today());
505 my ($year1, $month1, $day1) = split /-/, $date;
506 my ($year2, $month2, $day2) = split /-/, $date_ref;
508 my $age = $year2 - $year1;
509 if ($month1.$day1 > $month2.$day2) {