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},
59 my $borrowerid=$input->param('borrowerid');
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 $categorycode=$input->param('categorycode');
67 my $destination=$input->param('destination');
68 my $cardnumber=$input->param('cardnumber');
69 my $check_member=$input->param('check_member');
70 my $name_city=$input->param('name_city');
71 my $nodouble=$input->param('nodouble');
72 my $select_city=$input->param('select_city');
73 my $nok=$input->param('nok');
77 # $check_categorytype contains the value of duplicate borrowers category type to redirect in good template in step =2
78 my $check_categorytype=$input->param('check_categorytype');
79 # NOTE: Alert for ethnicity and ethnotes fields, they are unvalided in all borrowers form
82 #function to automatic setup the mandatory fields (visual with css)
83 my $check_BorrowerMandatoryField=C4::Context->preference("BorrowerMandatoryField");
84 my @field_check=split(/\|/,$check_BorrowerMandatoryField);
85 foreach (@field_check) {
86 $template->param( "mandatory$_" => 1);
89 $template->param( "checked" => 1) if ($nodouble eq 1);
91 my $borrower_data=borrdata('',$borrowernumber);
92 # if a add or modify is requested => check validity of data.
94 foreach my $column (keys %$borrower_data){
95 $data{$column}=$borrower_data->{$column};
97 $data{'borrowerid'}=$borrowernumber;
99 $borrowerid=$borrowernumber;
103 if ($op eq 'add' or $op eq 'modify') {
104 my @names=$input->param;
105 foreach my $key (@names){
106 $data{$key}=$input->param($key)||'';
107 $data{$key}=~ s/\'/\\\'/g;
108 $data{$key}=~ s/\"/\\\"/g;
111 # WARN : some tests must be done whatever the step, because the librarian can click on any tab.
112 #############test for member being unique #############
114 (my $category_type_send=$category_type ) if ($category_type eq 'I');
115 my $check_category; # recover the category code of the doublon suspect borrowers
116 ($check_member,$check_category)= checkuniquemember($category_type_send,$data{'surname'},$data{'firstname'},format_date_in_iso($data{'dateofbirth'}));
118 # recover the category type if the borrowers is a doublon
119 ($check_categorytype,undef)=getcategorytype($check_category);
123 #recover all data from guarantor address phone ,fax...
124 if ($category_type eq 'C' and $guarantorid ne '' and $data{'contactname'} eq ''){
125 my $guarantordata=getguarantordata($guarantorid);
126 if (($data{'contactname'} eq '' or $data{'contactname'} ne $guarantordata->{'surname'})) {
127 $data{'contactfirstname'}=$guarantordata->{'firstname'};
128 $data{'contactname'}=$guarantordata->{'surname'};
129 $data{'contacttitle'}=$guarantordata->{'title'};
130 $data{'streetnumber'}=$guarantordata->{'streetnumber'};
131 $data{'address'}=$guarantordata->{'address'};
132 $data{'streettype'}=$guarantordata->{'streettype'};
133 $data{'address2'}=$guarantordata->{'address2'};
134 $data{'zipcode'}=$guarantordata->{'zipcode'};
135 $data{'city'}=$guarantordata->{'city'};
136 $data{'phone'}=$guarantordata->{'phone'};
137 $data{'phonepro'}=$guarantordata->{'phonepro'};
138 $data{'mobile'}=$guarantordata->{'mobile'};
139 $data{'fax'}=$guarantordata->{'fax'};
140 $data{'email'}=$guarantordata->{'email'};
141 $data{'emailpro'}=$guarantordata->{'emailpro'};
142 $default_city=getidcity($data{'city'});
146 # CHECKS step by step
149 ###############test to take the right zipcode and city name ##############
150 if ($category_type ne 'I' and $guarantorid eq ''){
151 my ($borrower_city,$borrower_zipcode)=&getzipnamecity($select_city);
152 $data{'city'}= $borrower_city;
153 $data{'zipcode'}=$borrower_zipcode;
155 if ($categorycode ne 'I') {
156 my $age = get_age(format_date_in_iso($data{dateofbirth}));
157 my (undef,$agelimitmin,$agelimitmax)=getborrowercategory($data{'categorycode'});
158 if ($age > $agelimitmax
159 or $age < $agelimitmin
161 push @errors, 'ERROR_age_limitations';
168 if ( ($data{'userid'} eq '')){
169 my $onefirstnameletter=substr($data{'firstname'},0,1);
170 my $fivesurnameletter=substr($data{'surname'},0,5);
171 $data{'userid'}=lc($onefirstnameletter.$fivesurnameletter);
173 if ($op eq 'add' and $data{'dateenrolled'} eq ''){
174 my $today= sprintf('%04d-%02d-%02d', Today());
175 #insert ,in field "dateenrolled" , the current date
176 $data{'dateenrolled'}=$today;
177 #if date expiry is null u must calculate the value only in this case
178 $data{'dateexpiry'} = calcexpirydate($data{'categorycode'},$today);
180 if ($op eq 'modify' ){
181 my $today= sprintf('%04d-%02d-%02d', Today());
182 # if date expiry is null u must calculate the value only in this case
183 if ($data{'dateexpiry'} eq ''){
184 $data{'dateexpiry'} = calcexpirydate($data{'categorycode'},$today);
190 # this value show if the login and password are been used
191 my $loginexist=checkuserpassword($borrowernumber,$data{'userid'},$data{'password'});
192 # test to know if u must save or create the borrowers
193 if ($op eq 'modify'){
194 # test to know if another user have the same password and same login
195 if ($loginexist eq 0) {
197 logaction($loggedinuser,"MEMBERS","modify member", $borrowernumber, "");
200 push @errors, "ERROR_login_exist";
204 # test to know if another user have the same password and same login
206 push @errors, "ERROR_login_exist";
209 $borrowernumber = &newmember(%data);
210 if ($data{'organisations'}){
211 # need to add the members organisations
212 my @orgs=split(/\|/,$data{'organisations'});
213 add_member_orgs($borrowerid,\@orgs);
215 logaction($loggedinuser,"MEMBERS","add member", $borrowernumber, "");
220 if($destination eq "circ"){
221 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$data{'cardnumber'}");
223 if ($loginexist == 0) {
224 print $input->redirect("/cgi-bin/koha/members/moremember.pl?bornum=$borrowernumber");
229 if (C4::Context->preference("IndependantBranches")) {
230 my $userenv = C4::Context->userenv;
231 if ($userenv->{flags} != 1){
232 unless ($userenv->{branch} eq $data{'branchcode'}){
233 push @errors, "ERROR_branch";
241 print $input->redirect("/cgi-bin/koha/deletemem.pl?member=$borrowernumber");
242 } else { # this else goes down the whole script
243 # retrieve previous values : either in DB or in CGI, in case of errors in values
245 # test to now if u add or modify a borrower (modify =>to take all carateristic of the borrowers)
246 if (!$op and !$data{'surname'}) {
247 $data=borrdata('',$borrowernumber);
250 if (C4::Context->preference("IndependantBranches")) {
251 my $userenv = C4::Context->userenv;
252 if ($userenv->{flags} != 1 && $data{branchcode}){
253 unless ($userenv->{branch} eq $data{'branchcode'}){
254 print $input->redirect("/cgi-bin/koha/members/members-home.pl");
259 $template->param( updtype => 'I');
261 $template->param( updtype => 'M');
263 my $cardnumber=$data{'cardnumber'};
264 $cardnumber=fixup_cardnumber($data{'cardnumber'}) if $op eq 'add';
265 if ($data{'sex'} eq 'F'){
266 $template->param(female => 1);
268 my ($categories,$labels)=ethnicitycategories();
269 my $ethnicitycategoriescount=$#{$categories};
271 if ($ethnicitycategoriescount>=0) {
272 $ethcatpopup = CGI::popup_menu(-name=>'ethnicity',
274 -values=>$categories,
275 -default=>$data{'ethnicity'},
277 $template->param(ethcatpopup => $ethcatpopup); # bad style, has to be fixed
281 ($categories,$labels)=borrowercategories($category_type,$op);
283 #if u modify the borrowers u must have the right value for is category code
285 (my $default_category=$data{'categorycode'}) if ($op eq '');
286 my $catcodepopup = CGI::popup_menu(-name=>'categorycode',
287 -id => 'categorycode',
288 -values=>$categories,
289 -default=>$default_category,
294 (my $selectcity=&getidcity($data{'city'})) if ($select_city eq '');
295 $default_city=$selectcity;
297 my($cityid,$name_city)=getcities();
298 $template->param( city_cgipopup => 1) if ($cityid );
299 my $citypopup = CGI::popup_menu(-name=>'select_city',
300 -id => 'select_city',
304 -default=>$default_city
307 my $default_roadtype;
308 $default_roadtype=$data{'streettype'} ;
309 my($roadtypeid,$road_type)=getroadtypes();
310 $template->param( road_cgipopup => 1) if ($roadtypeid );
311 my $roadpopup = CGI::popup_menu(-name=>'streettype',
313 -values=>$roadtypeid,
316 -default=>$default_roadtype
320 my @relationships = split /,|\|/,C4::Context->preference('BorrowerRelationship');
322 while (@relationships) {
323 my $relship = shift @relationships || '';
324 my %row = ('relationship' => $relship);
325 if ($data{'relationship'} eq $relship) {
326 $row{'selected'}=' selected';
330 push(@relshipdata, \%row);
332 my %flags = ( 'gonenoaddress' => ['gonenoaddress', 'Adresse érronée'],
333 'lost' => ['lost', 'Carte Perdue'],
334 'debarred' => ['debarred', 'Lecteur exclu']);
337 foreach (keys(%flags)) {
339 my %row = ('key' => $key,
340 'name' => $flags{$key}[0],
341 'html' => $flags{$key}[1]);
343 $row{'yes'}=' checked';
347 $row{'no'}=' checked';
349 push(@flagdata, \%row);
353 $template->param( modify => 1 );
356 #Convert dateofbirth to correct format
357 $data{'dateofbirth'} = format_date($data{'dateofbirth'});
361 my $branches=getbranches('IS');
363 # -----------------------------------------------------
364 # the value of ip from the branches hash table
366 # $ip is the ip of user when is connect to koha
367 # my $ip = $ENV{'REMOTE_ADDR'};
369 # -----------------------------------------------------
370 foreach my $branch (keys %$branches) {
371 if ((not C4::Context->preference("IndependantBranches")) || (C4::Context->userenv->{'flags'} == 1)) {
372 push @select_branch, $branch;
373 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
374 $default = $branches->{$branch}->{'branchcode'};
376 push @select_branch,$branch if ($branch eq C4::Context->userenv->{'branch'});
377 $select_branches{$branch} = $branches->{$branch}->{'branchname'} if ($branch eq C4::Context->userenv->{'branch'});
378 $default = C4::Context->userenv->{'branch'};
382 # --------------------------------------------------------------------------------------------------------
383 #in modify mod :default value from $CGIbranch comes from borrowers table
384 #in add mod: default value come from branches table (ip correspendence)
385 $default=$data{'branchcode'} if ($op eq 'modify');
387 my $CGIbranch = CGI::scrolling_list(-id => 'branchcode',
388 -name => 'branchcode',
389 -values => \@select_branch,
390 -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);
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 borrowernumber => $borrowernumber,#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 userid => $data{'userid'},
506 password => $data{'password'},
507 opacnote => $data{'opacnote'},
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;