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/;
28 use Digest::MD5 qw(md5_base64);
34 use C4::Interface::CGI::Output;
40 use C4::Branch; # GetBranches
45 my $dbh = C4::Context->dbh;
47 my $categorycode=$input->param('categorycode');
49 $category_type = $input->param('category_type');
52 ($category_type,$desc) = getcategorytype($categorycode) unless ($category_type or !($categorycode));
54 die "NO CATEGORY TYPE !" unless $category_type; # FIXME we should display a error message instead of a 500 error !
56 my $step=$input->param('step') || 0;
57 my ($template, $loggedinuser, $cookie)
58 = get_template_and_user({template_name => "members/memberentry$category_type.tmpl",
62 flagsrequired => {borrowers => 1},
65 my $guarantorid=$input->param('guarantorid');
66 my $borrowernumber=$input->param('borrowernumber');
67 my $actionType=$input->param('actionType') || '';
68 my $modify=$input->param('modify');
69 my $delete=$input->param('delete');
70 my $op=$input->param('op');
71 my $destination=$input->param('destination');
72 my $cardnumber=$input->param('cardnumber');
73 my $check_member=$input->param('check_member');
74 my $name_city=$input->param('name_city');
75 my $nodouble=$input->param('nodouble');
76 my $select_city=$input->param('select_city');
77 my $nok=$input->param('nok');
78 my $guarantorinfo=$input->param('guarantorinfo');
81 # $check_categorytype contains the value of duplicate borrowers category type to redirect in good template in step =2
82 my $check_categorytype=$input->param('check_categorytype');
83 # NOTE: Alert for ethnicity and ethnotes fields, they are unvalided in all borrowers form
86 #function to automatic setup the mandatory fields (visual with css)
87 my $check_BorrowerMandatoryField=C4::Context->preference("BorrowerMandatoryField");
88 my @field_check=split(/\|/,$check_BorrowerMandatoryField);
89 foreach (@field_check) {
90 $template->param( "mandatory$_" => 1);
92 $template->param("add"=>1) if ($op eq 'add');
93 $template->param( "checked" => 1) if ($nodouble eq 1);
94 ($borrower_data=borrdata('',$borrowernumber)) if($op eq 'modify');
96 # if a add or modify is requested => check validity of data.
98 foreach my $column (keys %$borrower_data){
99 $data{$column}=$borrower_data->{$column};
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/\"/"/gg unless $key eq 'borrowernotes' or $key eq 'opacnote';
110 # WARN : some tests must be done whatever the step, because the librarian can click on any tab.
111 #############test for member being unique #############
113 (my $category_type_send=$category_type ) if ($category_type eq 'I');
114 my $check_category; # recover the category code of the doublon suspect borrowers
115 ($check_member,$check_category)= checkuniquemember($category_type_send,$data{'surname'},$data{'firstname'},format_date_in_iso($data{'dateofbirth'}));
117 # recover the category type if the borrowers is a doublon
118 ($check_categorytype,undef)=getcategorytype($check_category);
121 #recover all data from guarantor address phone ,fax...
122 if ($category_type eq 'C' and $guarantorid ne '' ){
123 my $guarantordata=getguarantordata($guarantorid);
124 $guarantorinfo=$guarantordata->{'surname'}." , ".$guarantordata->{'firstname'};
125 if (($data{'contactname'} eq '' or $data{'contactname'} ne $guarantordata->{'surname'})) {
126 $data{'contactfirstname'}=$guarantordata->{'firstname'};
127 $data{'contactname'}=$guarantordata->{'surname'};
128 $data{'contacttitle'}=$guarantordata->{'title'};
129 $data{'streetnumber'}=$guarantordata->{'streetnumber'};
130 $data{'address'}=$guarantordata->{'address'};
131 $data{'streettype'}=$guarantordata->{'streettype'};
132 $data{'address2'}=$guarantordata->{'address2'};
133 $data{'zipcode'}=$guarantordata->{'zipcode'};
134 $data{'city'}=$guarantordata->{'city'};
135 $data{'phone'}=$guarantordata->{'phone'};
136 $data{'phonepro'}=$guarantordata->{'phonepro'};
137 $data{'mobile'}=$guarantordata->{'mobile'};
138 $data{'fax'}=$guarantordata->{'fax'};
139 $data{'email'}=$guarantordata->{'email'};
140 $data{'emailpro'}=$guarantordata->{'emailpro'};
144 # CHECKS step by step
147 ###############test to take the right zipcode and city name ##############
148 if ( $guarantorid eq ''){
150 my ($borrower_city,$borrower_zipcode)=&getzipnamecity($select_city);
151 $data{'city'}= $borrower_city;
152 $data{'zipcode'}=$borrower_zipcode;
155 my $dateofbirthmandatory=0;
156 map {$dateofbirthmandatory=1 if $_ eq "dateofbirth"} @field_check;
157 if ($category_type ne 'I' && $data{dateofbirth} && $dateofbirthmandatory) {
158 my $age = get_age(format_date_in_iso($data{dateofbirth}));
159 my (undef,$agelimitmin,$agelimitmax,undef)=getborrowercategory($data{'categorycode'});
160 if (($age > $agelimitmax) or ($age < $agelimitmin)) {
161 push @errors, 'ERROR_age_limitations';
171 if ( ($data{'userid'} eq '')){
172 my $onefirstnameletter=substr($data{'firstname'},0,1);
173 my $fivesurnameletter=substr($data{'surname'},0,5);
174 $data{'userid'}=lc($onefirstnameletter.$fivesurnameletter);
176 if ($op eq 'add' and $data{'dateenrolled'} eq ''){
177 my $today= sprintf('%04d-%02d-%02d', Today());
178 #insert ,in field "dateenrolled" , the current date
179 $data{'dateenrolled'}=$today;
180 #if date expiry is null u must calculate the value only in this case
181 $data{'dateexpiry'} = calcexpirydate($data{'categorycode'},$today);
183 if ($op eq 'modify' ){
184 my $today= sprintf('%04d-%02d-%02d', Today());
185 # if date expiry is null u must calculate the value only in this case
186 if ($data{'dateexpiry'} eq ''){
187 $data{'dateexpiry'} = calcexpirydate($data{'categorycode'},$today);
193 # this value show if the login and password are been used
194 my $loginexist=checkuserpassword($borrowernumber,$data{'userid'},$data{'password'});
195 # test to know if u must save or create the borrowers
196 if ($op eq 'modify'){
197 # test to know if another user have the same password and same login
198 if ($loginexist eq 0) {
202 push @errors, "ERROR_login_exist";
206 # test to know if another user have the same password and same login
208 push @errors, "ERROR_login_exist";
211 $borrowernumber = &newmember(%data);
212 if ($data{'organisations'}){
213 # need to add the members organisations
214 my @orgs=split(/\|/,$data{'organisations'});
215 add_member_orgs($borrowernumber,\@orgs);
221 if($destination eq "circ"){
222 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$data{'cardnumber'}");
224 if ($loginexist == 0) {
225 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$borrowernumber");
230 if (C4::Context->preference("IndependantBranches")) {
231 my $userenv = C4::Context->userenv;
232 if ($userenv->{flags} != 1){
233 unless ($userenv->{branch} eq $data{'branchcode'}){
234 push @errors, "ERROR_branch";
242 print $input->redirect("/cgi-bin/koha/deletemem.pl?member=$borrowernumber");
243 } else { # this else goes down the whole script
244 # retrieve previous values : either in DB or in CGI, in case of errors in values
246 # test to now if u add or modify a borrower (modify =>to take all carateristic of the borrowers)
247 if (!$op and !$data{'surname'}) {
248 $data=borrdata('',$borrowernumber);
251 if (C4::Context->preference("IndependantBranches")) {
252 my $userenv = C4::Context->userenv;
253 if ($userenv->{flags} != 1 && $data{branchcode}){
254 unless ($userenv->{branch} eq $data{'branchcode'}){
255 print $input->redirect("/cgi-bin/koha/members/members-home.pl");
260 $template->param( updtype => 'I');
262 $template->param( updtype => 'M');
264 my $cardnumber=$data{'cardnumber'};
265 $cardnumber=fixup_cardnumber($data{'cardnumber'}) if $op eq 'add';
266 if ($data{'sex'} eq 'F'){
267 $template->param(female => 1);
269 my ($categories,$labels)=ethnicitycategories();
271 my $ethnicitycategoriescount=$#{$categories};
273 if ($ethnicitycategoriescount>=0) {
274 $ethcatpopup = CGI::popup_menu(-name=>'ethnicity',
277 -values=>$categories,
278 -default=>$data{'ethnicity'},
280 $template->param(ethcatpopup => $ethcatpopup); # bad style, has to be fixed
284 my $action="WHERE category_type=?";
285 ($categories,$labels)=GetborCatFromCatType($category_type,$action);
287 if(scalar(@$categories)){
288 #if you modify the borrowers you must have the right value for his category code
289 (my $default_category=$data{'categorycode'}) if ($op eq 'modify');
290 my $catcodepopup = CGI::popup_menu(
291 -name=>'categorycode',
292 -id => 'categorycode',
293 -values=>$categories,
295 -default=>$default_category
297 $template->param(catcodepopup=>$catcodepopup);
300 $select_city=getidcity($data{'city'}) if ($guarantorid ne '0');
301 ($default_city=$select_city) if ($step eq 0);
302 if ($select_city eq '' ){
303 my $selectcity=&getidcity($data{'city'});
304 $default_city=$selectcity;
306 my($cityid,$name_city)=GetCities();
307 $template->param( city_cgipopup => 1) if ($cityid );
308 my $citypopup = CGI::popup_menu(-name=>'select_city',
309 -id => 'select_city',
313 -default=>$default_city
316 my $default_roadtype;
317 $default_roadtype=$data{'streettype'} ;
318 my($roadtypeid,$road_type)=GetRoadTypes();
319 $template->param( road_cgipopup => 1) if ($roadtypeid );
320 my $roadpopup = CGI::popup_menu(-name=>'streettype',
322 -values=>$roadtypeid,
325 -default=>$default_roadtype
328 my $default_borrowertitle;
329 $default_borrowertitle=$data{'title'} ;
330 my($borrowertitle)=GetBorrowersTitles();
331 my $borrotitlepopup = CGI::popup_menu(-name=>'title',
333 -values=>$borrowertitle,
335 -default=>$default_borrowertitle
339 my @relationships = split /,|\|/,C4::Context->preference('BorrowerRelationship');
341 while (@relationships) {
342 my $relship = shift @relationships || '';
343 my %row = ('relationship' => $relship);
344 if ($data{'relationship'} eq $relship) {
345 $row{'selected'}=' selected';
349 push(@relshipdata, \%row);
351 my %flags = ( 'gonenoaddress' => ['gonenoaddress', 'Gone no address '],
352 'lost' => ['lost', 'Lost'],
353 'debarred' => ['debarred', 'Debarred']);
356 foreach (keys(%flags)) {
358 my %row = ('key' => $key,
359 'name' => $flags{$key}[0],
360 'html' => $flags{$key}[1]);
362 $row{'yes'}=' checked';
366 $row{'no'}=' checked';
368 push(@flagdata, \%row);
372 $template->param( modify => 1 );
375 #Convert dateofbirth to correct format
376 $data{'dateofbirth'} = format_date($data{'dateofbirth'});
380 my $branches=GetBranches();
382 # -----------------------------------------------------
383 # the value of ip from the branches hash table
385 # $ip is the ip of user when is connect to koha
386 # my $ip = $ENV{'REMOTE_ADDR'};
388 # -----------------------------------------------------
389 foreach my $branch (keys %$branches) {
390 if ((not C4::Context->preference("IndependantBranches")) || (C4::Context->userenv->{'flags'} == 1)) {
391 push @select_branch, $branch;
392 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
393 $default=C4::Context->userenv->{'branch'};
395 push @select_branch,$branch if ($branch eq C4::Context->userenv->{'branch'});
396 $select_branches{$branch} = $branches->{$branch}->{'branchname'} if ($branch eq C4::Context->userenv->{'branch'});
397 $default = C4::Context->userenv->{'branch'};
400 # --------------------------------------------------------------------------------------------------------
401 #in modify mod :default value from $CGIbranch comes from borrowers table
402 #in add mod: default value come from branches table (ip correspendence)
403 $default=$data{'branchcode'} if ($op eq 'modify');
404 my $CGIbranch = CGI::scrolling_list(-id => 'branchcode',
405 -name => 'branchcode',
406 -values => \@select_branch,
407 -labels => \%select_branches,
411 -default => $default,
413 my $CGIorganisations;
414 my $member_of_institution;
415 if (C4::Context->preference("memberofinstitution")){
416 my $organisations=get_institutions();
419 foreach my $organisation (keys %$organisations) {
420 push @orgs,$organisation;
421 $org_labels{$organisation}=$organisations->{$organisation}->{'surname'};
423 $member_of_institution=1;
425 $CGIorganisations = CGI::scrolling_list( -id => 'organisations',
426 -name => 'organisations',
427 -labels => \%org_labels,
437 # --------------------------------------------------------------------------------------------------------
439 my $CGIsort1 = buildCGIsort("Bsort1","sort1",$data{'sort1'});
441 $template->param(CGIsort1 => $CGIsort1);
442 $template->param( sort1 => $data{'sort1'});
444 $template->param( sort1 => $data{'sort1'});
447 my $CGIsort2 = buildCGIsort("Bsort2","sort2",$data{'sort2'});
449 $template->param(CGIsort2 =>$CGIsort2);
451 $template->param( sort2 => $data{'sort2'});
453 # increase step to see next page
455 foreach my $error (@errors) {
456 $template->param( $error => 1);
458 $template->param(nok => 1);
464 BorrowerMandatoryField => C4::Context->preference("BorrowerMandatoryField"),#field to test with javascript
465 category_type => $category_type,#to know the category type of the borrower
466 select_city => $select_city,
467 "step_$step" => 1,# associate with step to know where u are
469 destination => $destination,#to know wher u come from and wher u must go in redirect
470 check_member => $check_member,#to know if the borrower already exist(=>1) or not (=>0)
471 # flags =>$data{'flags'},
474 nodouble => $nodouble,
475 borrowernumber => $borrowernumber,#register number
476 cardnumber => $data{'cardnumber'},
477 surname => uc($data{'surname'}),
478 firstname => ucfirst(lc $data{'firstname'}),
479 title => $data{'title'},
480 othernames => $data{'othernames'},
481 initials => $data{'initials'},
482 streetnumber => $data{'streetnumber'},
483 streettype =>$data{'streettype'},
484 address => $data{'address'},
485 address2 => $data{'address2'},
486 city => $data{'city'},
487 zipcode => $data{'zipcode'},
488 email => $data{'email'},
489 phone => $data{'phone'},
490 mobile => $data{'mobile'},
492 phonepro => $data{'phonepro'},
493 emailpro => $data{'emailpro'},
494 B_address => $data{'B_address'},
495 B_city => $data{'B_city'},
496 B_zipcode => $data{'B_zipcode'},
497 B_email => $data{'B_email'},
498 B_phone => $data{'B_phone'},
499 dateofbirth => $data{'dateofbirth'},
500 branchcode => $data{'branchcode'},
501 categorycode => $data{'categorycode'},
502 dateenrolled => format_date($data{'dateenrolled'}),
503 dateexpiry => format_date($data{'dateexpiry'}),
504 debarred => $data{'debarred'},
505 gonenoaddress => $data{'gonenoaddress'},
506 lost => $data{'lost'},
507 contactname => uc($data{'contactname'}),
508 contactfirstname=> ucfirst( lc $data{'contactfirstname'}),
509 "contacttitle_".$data{'contacttitle'} => "SELECTED" ,
510 contacttitle => $data{'contacttitle'},
511 guarantorid => $guarantorid,
512 ethcatpopup => $ethcatpopup,
514 userid => $data{'userid'},
515 password => $data{'password'},
516 opacnote => $data{'opacnote'},
517 contactnote => $data{'contactnote'},
518 borrowernotes => $data{'borrowernotes'},
519 relshiploop => \@relshipdata,
520 relationship => $data{'relationship'},
521 citypopup => $citypopup,
522 roadpopup => $roadpopup,
523 borrotitlepopup => $borrotitlepopup,
524 contacttype => $data{'contacttype'},
525 organisations => $data{'organisations'},
526 guarantorinfo => $guarantorinfo,
527 flagloop => \@flagdata,
528 # "contacttype_".$data{'contacttype'} =>" SELECTED ",
529 dateformat => display_date_format(),
530 check_categorytype =>$check_categorytype,#to recover the category type with checkcategorytype function
532 # city_choice => $city_choice ,#check if the city was selected
533 nok => $nok,#flag to konw if an error
534 CGIbranch => $CGIbranch,
535 memberofinstution => $member_of_institution,
536 CGIorganisations => $CGIorganisations,
538 #$template->param(Institution => 1) if ($categorycode eq "I");
539 output_html_with_http_headers $input, $cookie, $template->output;