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 $guarantorid=$input->param('guarantorid');
60 my $borrowernumber=$input->param('borrowernumber');
61 my $actionType=$input->param('actionType') || '';
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("add"=>1) if ($op eq 'add');
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};
98 if ($op eq 'add' or $op eq 'modify') {
99 my @names=$input->param;
100 foreach my $key (@names){
101 $data{$key}=$input->param($key)||'';
102 $data{$key}=~ s/\'/\\\'/g;
103 $data{$key}=~ s/\"/\\\"/g;
106 # WARN : some tests must be done whatever the step, because the librarian can click on any tab.
107 #############test for member being unique #############
109 (my $category_type_send=$category_type ) if ($category_type eq 'I');
110 my $check_category; # recover the category code of the doublon suspect borrowers
111 ($check_member,$check_category)= checkuniquemember($category_type_send,$data{'surname'},$data{'firstname'},format_date_in_iso($data{'dateofbirth'}));
113 # recover the category type if the borrowers is a doublon
114 ($check_categorytype,undef)=getcategorytype($check_category);
117 #recover all data from guarantor address phone ,fax...
118 if ($category_type eq 'C' and $guarantorid ne '' ){
119 my $guarantordata=getguarantordata($guarantorid);
120 if (($data{'contactname'} eq '' or $data{'contactname'} ne $guarantordata->{'surname'})) {
121 $data{'contactfirstname'}=$guarantordata->{'firstname'};
122 $data{'contactname'}=$guarantordata->{'surname'};
123 $data{'contacttitle'}=$guarantordata->{'title'};
124 $data{'streetnumber'}=$guarantordata->{'streetnumber'};
125 $data{'address'}=$guarantordata->{'address'};
126 $data{'streettype'}=$guarantordata->{'streettype'};
127 $data{'address2'}=$guarantordata->{'address2'};
128 $data{'zipcode'}=$guarantordata->{'zipcode'};
129 $data{'city'}=$guarantordata->{'city'};
130 $data{'phone'}=$guarantordata->{'phone'};
131 $data{'phonepro'}=$guarantordata->{'phonepro'};
132 $data{'mobile'}=$guarantordata->{'mobile'};
133 $data{'fax'}=$guarantordata->{'fax'};
134 $data{'email'}=$guarantordata->{'email'};
135 $data{'emailpro'}=$guarantordata->{'emailpro'};
139 # CHECKS step by step
142 ###############test to take the right zipcode and city name ##############
143 if ( $guarantorid eq ''){
144 my ($borrower_city,$borrower_zipcode)=&getzipnamecity($select_city);
145 $data{'city'}= $borrower_city;
146 $data{'zipcode'}=$borrower_zipcode;
148 if ($category_type ne 'I') {
149 my $age = get_age(format_date_in_iso($data{dateofbirth}));
150 my (undef,$agelimitmin,$agelimitmax,undef)=getborrowercategory($data{'categorycode'});
151 if ($age > $agelimitmax
152 or $age < $agelimitmin
154 push @errors, 'ERROR_age_limitations';
162 if ( ($data{'userid'} eq '')){
163 my $onefirstnameletter=substr($data{'firstname'},0,1);
164 my $fivesurnameletter=substr($data{'surname'},0,5);
165 $data{'userid'}=lc($onefirstnameletter.$fivesurnameletter);
167 if ($op eq 'add' and $data{'dateenrolled'} eq ''){
168 my $today= sprintf('%04d-%02d-%02d', Today());
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' ){
175 my $today= sprintf('%04d-%02d-%02d', Today());
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($borrowernumber,$data{'userid'},$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", $borrowernumber, "");
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 $borrowernumber = &newmember(%data);
204 if ($data{'organisations'}){
205 # need to add the members organisations
206 my @orgs=split(/\|/,$data{'organisations'});
207 add_member_orgs($borrowernumber,\@orgs);
209 logaction($loggedinuser,"MEMBERS","add member", $borrowernumber, "");
214 if($destination eq "circ"){
215 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$data{'cardnumber'}");
217 if ($loginexist == 0) {
218 print $input->redirect("/cgi-bin/koha/members/moremember.pl?bornum=$borrowernumber");
223 if (C4::Context->preference("IndependantBranches")) {
224 my $userenv = C4::Context->userenv;
225 if ($userenv->{flags} != 1){
226 unless ($userenv->{branch} eq $data{'branchcode'}){
227 push @errors, "ERROR_branch";
235 print $input->redirect("/cgi-bin/koha/deletemem.pl?member=$borrowernumber");
236 } else { # this else goes down the whole script
237 # retrieve previous values : either in DB or in CGI, in case of errors in values
239 # test to now if u add or modify a borrower (modify =>to take all carateristic of the borrowers)
240 if (!$op and !$data{'surname'}) {
241 $data=borrdata('',$borrowernumber);
244 if (C4::Context->preference("IndependantBranches")) {
245 my $userenv = C4::Context->userenv;
246 if ($userenv->{flags} != 1 && $data{branchcode}){
247 unless ($userenv->{branch} eq $data{'branchcode'}){
248 print $input->redirect("/cgi-bin/koha/members/members-home.pl");
253 $template->param( updtype => 'I');
255 $template->param( updtype => 'M');
257 my $cardnumber=$data{'cardnumber'};
258 $cardnumber=fixup_cardnumber($data{'cardnumber'}) if $op eq 'add';
259 if ($data{'sex'} eq 'F'){
260 $template->param(female => 1);
262 my ($categories,$labels)=ethnicitycategories();
263 my $ethnicitycategoriescount=$#{$categories};
265 if ($ethnicitycategoriescount>=0) {
266 $ethcatpopup = CGI::popup_menu(-name=>'ethnicity',
269 -values=>$categories,
270 -default=>$data{'ethnicity'},
272 $template->param(ethcatpopup => $ethcatpopup); # bad style, has to be fixed
276 ($categories,$labels)=borrowercategories($category_type,$op);
278 #if u modify the borrowers u must have the right value for is category code
280 (my $default_category=$data{'categorycode'}) if ($op eq '');
281 my $catcodepopup = CGI::popup_menu(-name=>'categorycode',
282 -id => 'categorycode',
283 -values=>$categories,
284 -default=>$default_category,
287 $select_city=getidcity($data{'city'}) if ($guarantorid ne '0');
288 ($default_city=$select_city) if ($step eq 0);
289 if ($select_city eq '' ){
290 my $selectcity=&getidcity($data{'city'});
291 $default_city=$selectcity;
293 my($cityid,$name_city)=getcities();
294 $template->param( city_cgipopup => 1) if ($cityid );
295 my $citypopup = CGI::popup_menu(-name=>'select_city',
296 -id => 'select_city',
300 -default=>$default_city
303 my $default_roadtype;
304 $default_roadtype=$data{'streettype'} ;
305 my($roadtypeid,$road_type)=getroadtypes();
306 $template->param( road_cgipopup => 1) if ($roadtypeid );
307 my $roadpopup = CGI::popup_menu(-name=>'streettype',
309 -values=>$roadtypeid,
312 -default=>$default_roadtype
316 my @relationships = split /,|\|/,C4::Context->preference('BorrowerRelationship');
318 while (@relationships) {
319 my $relship = shift @relationships || '';
320 my %row = ('relationship' => $relship);
321 if ($data{'relationship'} eq $relship) {
322 $row{'selected'}=' selected';
326 push(@relshipdata, \%row);
328 my %flags = ( 'gonenoaddress' => ['gonenoaddress', 'Gone no address '],
329 'lost' => ['lost', 'Lost'],
330 'debarred' => ['debarred', 'Debarred']);
333 foreach (keys(%flags)) {
335 my %row = ('key' => $key,
336 'name' => $flags{$key}[0],
337 'html' => $flags{$key}[1]);
339 $row{'yes'}=' checked';
343 $row{'no'}=' checked';
345 push(@flagdata, \%row);
349 $template->param( modify => 1 );
352 #Convert dateofbirth to correct format
353 $data{'dateofbirth'} = format_date($data{'dateofbirth'});
357 my $branches=GetBranches('IS');
359 # -----------------------------------------------------
360 # the value of ip from the branches hash table
362 # $ip is the ip of user when is connect to koha
363 # 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 $default = $branches->{$branch}->{'branchcode'};
372 push @select_branch,$branch if ($branch eq C4::Context->userenv->{'branch'});
373 $select_branches{$branch} = $branches->{$branch}->{'branchname'} if ($branch eq C4::Context->userenv->{'branch'});
374 $default = C4::Context->userenv->{'branch'};
378 # --------------------------------------------------------------------------------------------------------
379 #in modify mod :default value from $CGIbranch comes from borrowers table
380 #in add mod: default value come from branches table (ip correspendence)
381 $default=$data{'branchcode'} if ($op eq 'modify');
383 my $CGIbranch = CGI::scrolling_list(-id => 'branchcode',
384 -name => 'branchcode',
385 -values => \@select_branch,
386 -labels => \%select_branches,
389 -default => $default,
391 my $CGIorganisations;
392 my $member_of_institution;
393 if (C4::Context->preference("memberofinstitution")){
394 my $organisations=get_institutions();
397 foreach my $organisation (keys %$organisations) {
398 push @orgs,$organisation;
399 $org_labels{$organisation}=$organisations->{$organisation}->{'surname'};
401 $member_of_institution=1;
403 $CGIorganisations = CGI::scrolling_list( -id => 'organisations',
404 -name => 'organisations',
405 -labels => \%org_labels,
415 # --------------------------------------------------------------------------------------------------------
417 my $CGIsort1 = buildCGIsort("Bsort1","sort1",$data{'sort1'});
419 $template->param(CGIsort1 => $CGIsort1);
420 $template->param( sort1 => $data{'sort1'});
422 $template->param( sort1 => $data{'sort1'});
425 my $CGIsort2 = buildCGIsort("Bsort2","sort2",$data{'sort2'});
427 $template->param(CGIsort2 =>$CGIsort2);
429 $template->param( sort2 => $data{'sort2'});
433 $data{'opacnotes'} =~ s/\\//g;
434 $data{'borrowernotes'} =~ s/\\//g;
436 # increase step to see next page
438 foreach my $error (@errors) {
439 $template->param( $error => 1);
441 $template->param(nok => 1);
449 BorrowerMandatoryField => C4::Context->preference("BorrowerMandatoryField"),#field to test with javascript
450 category_type => $category_type,#to know the category type of the borrower
451 select_city => $select_city,
452 "step_$step" => 1,# associate with step to know where u are
454 destination => $destination,#to know wher u come from and wher u must go in redirect
455 check_member => $check_member,#to know if the borrower already exist(=>1) or not (=>0)
456 # flags =>$data{'flags'},
459 nodouble => $nodouble,
460 borrowernumber => $borrowernumber,#register number
461 cardnumber => $data{'cardnumber'},
462 surname => uc($data{'surname'}),
463 firstname => ucfirst($data{'firstname'}),
464 "title_".$data{'title'} => " SELECTED ",
465 title => $data{'title'},
466 othernames => $data{'othernames'},
467 initials => $data{'initials'},
468 streetnumber => $data{'streetnumber'},
469 streettype =>$data{'streettype'},
470 address => $data{'address'},
471 address2 => $data{'address2'},
472 city => $data{'city'},
473 zipcode => $data{'zipcode'},
474 email => $data{'email'},
475 phone => $data{'phone'},
476 mobile => $data{'mobile'},
478 phonepro => $data{'phonepro'},
479 emailpro => $data{'emailpro'},
480 B_address => $data{'B_address'},
481 B_city => $data{'B_city'},
482 B_zipcode => $data{'B_zipcode'},
483 B_email => $data{'B_email'},
484 B_phone => $data{'B_phone'},
485 dateofbirth => $data{'dateofbirth'},
486 branchcode => $data{'branchcode'},
487 catcodepopup => $catcodepopup,
488 categorycode => $data{'categorycode'},
489 dateenrolled => format_date($data{'dateenrolled'}),
490 dateexpiry => format_date($data{'dateexpiry'}),
491 debarred => $data{'debarred'},
492 gonenoaddress => $data{'gonenoaddress'},
493 lost => $data{'lost'},
494 contactname => uc($data{'contactname'}),
495 contactfirstname=> ucfirst($data{'contactfirstname'}),
496 "contacttitle_".$data{'contacttitle'} => "SELECTED" ,
497 contacttitle => $data{'contacttitle'},
498 guarantorid => $guarantorid,
499 ethcatpopup => $ethcatpopup,
501 userid => $data{'userid'},
502 password => $data{'password'},
503 opacnote => $data{'opacnote'},
504 contactnotes => $data{'contactnotes'},
505 borrowernotes => $data{'borrowernotes'},
506 relshiploop => \@relshipdata,
507 relationship => $data{'relationship'},
508 citypopup => $citypopup,
509 roadpopup => $roadpopup,
510 contacttype => $data{'contacttype'},
511 organisations => $data{'organisations'},
512 flagloop => \@flagdata,
513 # "contacttype_".$data{'contacttype'} =>" SELECTED ",
514 dateformat => display_date_format(),
515 check_categorytype =>$check_categorytype,#to recover the category type with checkcategorytype function
517 # city_choice => $city_choice ,#check if the city was selected
518 nok => $nok,#flag to konw if an error
519 CGIbranch => $CGIbranch,
520 memberofinstution => $member_of_institution,
521 CGIorganisations => $CGIorganisations,
523 #$template->param(Institution => 1) if ($categorycode eq "I");
524 output_html_with_http_headers $input, $cookie, $template->output;