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/;
27 use Digest::MD5 qw(md5_base64);
35 use C4::Dates qw/format_date format_date_in_iso/;
38 use C4::Branch; # GetBranches
43 $debug = $ENV{DEBUG} || 0;
49 my $dbh = C4::Context->dbh;
53 my ($template, $loggedinuser, $cookie)
54 = get_template_and_user({template_name => "members/memberentrygen.tmpl",
58 flagsrequired => {borrowers => 1},
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 $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');
74 my $guarantorinfo=$input->param('guarantorinfo');
75 my $step=$input->param('step') || 0;
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 my $userenv = C4::Context->userenv;
85 $template->param("uppercasesurnames" => C4::Context->preference('uppercasesurnames'));
87 #function to automatic setup the mandatory fields (visual with css)
88 my $check_BorrowerMandatoryField=C4::Context->preference("BorrowerMandatoryField");
89 my @field_check=split(/\|/,$check_BorrowerMandatoryField);
90 foreach (@field_check) {
91 $template->param( "mandatory$_" => 1);
93 $template->param("add"=>1) if ($op eq 'add');
94 $template->param("checked" => 1) if ($nodouble eq 1);
95 ($borrower_data = GetMember($borrowernumber,'borrowernumber')) if ($op eq 'modify' or $op eq 'save');
96 my $categorycode = $input->param('categorycode') || $borrower_data->{'categorycode'};
97 my $category_type = $input->param('category_type');
98 unless ($category_type or !($categorycode)){
99 my $borrowercategory= GetBorrowercategory($categorycode);
100 $category_type = $borrowercategory->{'category_type'};
102 $category_type="A" unless $category_type; # FIXME we should display a error message instead of a 500 error !
104 # if a add or modify is requested => check validity of data.
105 %data= %$borrower_data if ($borrower_data);
108 if ($op eq 'insert' || $op eq 'modify' || $op eq 'save') {
109 my @names= ($borrower_data && $op ne 'save') ? keys %$borrower_data : $input->param();
110 foreach my $key (@names) {
111 $newdata{$key} = $input->param($key) if (defined $input->param($key));
112 $newdata{$key} =~ s/\"/"/gg unless $key eq 'borrowernotes' or $key eq 'opacnote';
114 $newdata{'dateenrolled'}=format_date_in_iso($newdata{'dateenrolled'}) if ($newdata{dateenrolled});
115 $newdata{'dateexpiry'} =format_date_in_iso($newdata{'dateexpiry'} ) if ($newdata{dateexpiry});
116 $newdata{'dateofbirth'} =format_date_in_iso($newdata{'dateofbirth'} ) if ($newdata{dateofbirth});
117 # check permission to modify login info.
118 if (ref($borrower_data) && ($borrower_data->{'category_type'} eq 'S') && ! (C4::Auth::haspermission($dbh,$userenv->{'id'},{'staffaccess'=>1})) ) {
123 #############test for member being unique #############
124 if ($op eq 'insert'){
125 my $category_type_send=$category_type if ($category_type eq 'I');
126 my $check_category; # recover the category code of the doublon suspect borrowers
127 ($check_member,$check_category)= checkuniquemember($category_type_send,($newdata{'surname'}?$newdata{'surname'}:$data{'surname'}),($newdata{'firstname'}?$newdata{'firstname'}:$data{'firstname'}),($newdata{'dateofbirth'}?$newdata{'dateofbirth'}:$data{'dateofbirth'}));
129 # recover the category type if the borrowers is a doublon
130 my $tmpborrowercategory=GetBorrowercategory($check_category);
131 $check_categorytype=$tmpborrowercategory->{'category_type'};
134 #recover all data from guarantor address phone ,fax...
135 if (($category_type eq 'C' || $category_type eq 'P') and $guarantorid ne '' ){
136 my $guarantordata=GetMember($guarantorid);
137 $guarantorinfo=$guarantordata->{'surname'}." , ".$guarantordata->{'firstname'};
138 if (($data{'contactname'} eq '' or $data{'contactname'} ne $guarantordata->{'surname'})) {
139 $data{'contactfirstname'}=$guarantordata->{'firstname'};
140 $data{'contactname'}=$guarantordata->{'surname'};
141 $data{'contacttitle'}=$guarantordata->{'title'};
142 map {$data{$_}=$guarantordata->{$_}}('streetnumber','address','streettype','address2','zipcode','city','phone','phonepro','mobile','fax','email','emailpro');
146 ###############test to take the right zipcode and city name ##############
147 if ( $guarantorid eq ''){
149 my ($borrower_city,$borrower_zipcode)=&getzipnamecity($select_city);
150 $newdata{'city'}= $borrower_city;
151 $newdata{'zipcode'}=$borrower_zipcode;
154 #builds default userid
155 if ( (defined $newdata{'userid'}) && ($newdata{'userid'} eq '')){
156 my $onefirstnameletter=substr($data{'firstname'},0,1);
157 my $fivesurnameletter=substr($data{'surname'},0,9);
158 $newdata{'userid'}=lc($onefirstnameletter.$fivesurnameletter);
162 if ($op eq 'save' || $op eq 'insert'){
163 if (checkcardnumber($newdata{cardnumber},$newdata{borrowernumber})){
164 push @errors, 'ERROR_cardnumber';
167 my $dateofbirthmandatory=0;
168 map {$dateofbirthmandatory=1 if $_ eq "dateofbirth"} @field_check;
169 if ($newdata{dateofbirth} && $dateofbirthmandatory) {
170 my $age = GetAge($newdata{dateofbirth});
171 my $borrowercategory=GetBorrowercategory($newdata{'categorycode'});
172 if (($age > $borrowercategory->{'upperagelimit'}) or ($age < $borrowercategory->{'dateofbirthrequired'})) {
173 push @errors, 'ERROR_age_limitations';
178 if (C4::Context->preference("IndependantBranches")) {
179 if ($userenv && $userenv->{flags} != 1){
180 $debug and print STDERR " $newdata{'branchcode'} : ".$userenv->{flags}.":".$userenv->{branch};
181 unless (!$newdata{'branchcode'} || $userenv->{branch} eq $newdata{'branchcode'}){
182 push @errors, "ERROR_branch";
187 # Check if the userid is unique
188 if (($op eq 'insert' || $op eq 'save') && !Check_Userid($newdata{'userid'},$borrowernumber)) {
189 push @errors, "ERROR_login_exist";
195 if ($op eq 'modify' || $op eq 'insert'){
196 unless ($newdata{'dateexpiry'}){
197 my $arg2 = $newdata{'dateenrolled'} || sprintf('%04d-%02d-%02d', Today());
198 $newdata{'dateexpiry'} = GetExpiryDate($newdata{'categorycode'},$arg2);
202 if ($op eq 'insert'){
203 # Check if the userid is unique
205 $borrowernumber = &AddMember(%newdata);
206 if ($data{'organisations'}){
207 # need to add the members organisations
208 my @orgs=split(/\|/,$data{'organisations'});
209 add_member_orgs($borrowernumber,\@orgs);
211 if ($destination eq "circ") {
212 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$data{'cardnumber'}");
214 if ($loginexist == 0) {
215 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$borrowernumber");
221 # test to know if another user have the same password and same login
224 delete $newdata{'password'};
225 delete $newdata{'userid'};
227 &ModMember(%newdata);
228 if ($destination eq "circ") {
229 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$data{'cardnumber'}");
231 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$borrowernumber");
237 print $input->redirect("/cgi-bin/koha/deletemem.pl?member=$borrowernumber");
241 $op="add" if ($op eq "insert");
242 $op="modify" if ($op eq "save");
244 $template->param( updtype => ($op eq "insert"?'I':'M'));
246 $template->param( step_1 => 1,step_2 => 1,step_3 => 1);
249 if (C4::Context->preference("IndependantBranches")) {
250 my $userenv = C4::Context->userenv;
251 if ($userenv->{flags} != 1 && $data{branchcode}){
252 unless ($userenv->{branch} eq $data{'branchcode'}){
253 print $input->redirect("/cgi-bin/koha/members/members-home.pl");
258 $template->param( updtype => 'I',step_1=>1,step_2=>1,step_3=>1);
260 if ($op eq "modify") {
261 $template->param( updtype => 'M',modify => 1 );
262 $template->param( step_1=>1,step_2=>1,step_3=>1) unless $step;
264 # my $cardnumber=$data{'cardnumber'};
265 $data{'cardnumber'}=fixup_cardnumber($data{'cardnumber'}) if $op eq 'add';
266 if ($data{'sex'} eq 'F'){
267 $template->param(female => 1);
268 } elsif ($data{'sex'} eq 'M'){
269 $template->param(male => 1);
271 $template->param(none => 1);
274 ##Now all the data to modify a member.
275 my ($categories,$labels)=ethnicitycategories();
277 my $ethnicitycategoriescount=$#{$categories};
279 if ($ethnicitycategoriescount>=0) {
280 $ethcatpopup = CGI::popup_menu(-name=>'ethnicity',
283 -values=>$categories,
284 -default=>$data{'ethnicity'},
286 $template->param(ethcatpopup => $ethcatpopup); # bad style, has to be fixed
290 my $action="WHERE category_type=?";
291 ($categories,$labels)=GetborCatFromCatType($category_type,$action);
293 if(scalar(@$categories)){
294 #if you modify the borrowers you must have the right value for his category code
295 my $default_category=$newdata{'categorycode'} if ($op eq 'modify');
296 my $catcodepopup = CGI::popup_menu(
297 -name=>'categorycode',
298 -id => 'categorycode',
299 -values=>$categories,
301 -default=>$default_category
303 $template->param(catcodepopup=>$catcodepopup);
306 $select_city=getidcity($data{'city'}) if ($guarantorid ne '0');
307 ($default_city=$select_city) if ($step eq 0);
308 if ($select_city eq '' ){
309 my $selectcity=&getidcity($data{'city'});
310 $default_city=$selectcity;
313 ($cityid,$name_city)=GetCities();
314 $template->param( city_cgipopup => 1) if ($cityid );
315 my $citypopup = CGI::popup_menu(-name=>'select_city',
316 -id => 'select_city',
319 -default=>$default_city,
320 -onChange => "javascript:changecity()",
323 my $default_roadtype;
324 $default_roadtype=$data{'streettype'} ;
325 my($roadtypeid,$road_type)=GetRoadTypes();
326 $template->param( road_cgipopup => 1) if ($roadtypeid );
327 my $roadpopup = CGI::popup_menu(-name=>'streettype',
329 -values=>$roadtypeid,
332 -default=>$default_roadtype
335 my $default_borrowertitle;
336 $default_borrowertitle=$data{'title'} ;
337 my($borrowertitle)=GetTitles();
338 my $borrotitlepopup = CGI::popup_menu(-name=>'title',
340 -values=>$borrowertitle,
342 -default=>$default_borrowertitle
346 my @relationships = split /,|\|/,C4::Context->preference('BorrowerRelationship');
348 while (@relationships) {
349 my $relship = shift @relationships || '';
350 my %row = ('relationship' => $relship);
351 if ($data{'relationship'} eq $relship) {
352 $row{'selected'}=' selected';
356 push(@relshipdata, \%row);
358 my %flags = ( 'gonenoaddress' => ['gonenoaddress', 'Gone no address '],
359 'lost' => ['lost', 'Lost'],
360 'debarred' => ['debarred', 'Debarred']);
363 foreach (keys(%flags)) {
365 my %row = ('key' => $key,
366 'name' => $flags{$key}[0],
367 'html' => $flags{$key}[1]);
369 $row{'yes'}=' checked';
373 $row{'no'}=' checked';
375 push(@flagdata, \%row);
384 my $onlymine=(C4::Context->preference('IndependantBranches') &&
385 C4::Context->userenv &&
386 C4::Context->userenv->{flags} !=1 &&
387 C4::Context->userenv->{branch}?1:0);
389 my $branches=GetBranches($onlymine);
392 foreach my $branch (sort keys %$branches) {
393 push @select_branch,$branch;
394 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
395 $default = C4::Context->userenv->{'branch'} if (C4::Context->userenv && C4::Context->userenv->{'branch'});
397 # --------------------------------------------------------------------------------------------------------
398 #in modify mod :default value from $CGIbranch comes from borrowers table
399 #in add mod: default value come from branches table (ip correspendence)
400 $default=$data{'branchcode'} if ($op eq 'modify');
401 my $CGIbranch = CGI::scrolling_list(-id => 'branchcode',
402 -name => 'branchcode',
403 -values => \@select_branch,
404 -labels => \%select_branches,
408 -default => $default,
410 my $CGIorganisations;
411 my $member_of_institution;
412 if (C4::Context->preference("memberofinstitution")){
413 my $organisations=get_institutions();
416 foreach my $organisation (keys %$organisations) {
417 push @orgs,$organisation;
418 $org_labels{$organisation}=$organisations->{$organisation}->{'surname'};
420 $member_of_institution=1;
422 $CGIorganisations = CGI::scrolling_list( -id => 'organisations',
423 -name => 'organisations',
424 -labels => \%org_labels,
433 # --------------------------------------------------------------------------------------------------------
435 my $CGIsort1 = buildCGIsort("Bsort1","sort1",$data{'sort1'});
437 $template->param(CGIsort1 => $CGIsort1);
439 $template->param( sort1 => $data{'sort1'});
441 my $CGIsort2 = buildCGIsort("Bsort2","sort2",$data{'sort2'});
443 $template->param(CGIsort2 =>$CGIsort2);
445 $template->param( sort2 => $data{'sort2'});
448 foreach my $error (@errors) {
449 $template->param( $error => 1);
451 $template->param(nok => 1);
454 #Formatting data for display
456 if ($data{'dateenrolled'} eq ''){
457 my $today= sprintf('%04d-%02d-%02d', Today());
458 $data{'dateenrolled'}=$today;
460 if (C4::Context->preference('uppercasesurnames')) {
461 $data{'surname'} =uc($data{'surname'} );
462 $data{'contactname'}=uc($data{'contactname'});
464 $data{'dateenrolled'} = format_date($data{'dateenrolled'});
465 $data{'dateexpiry'} = format_date($data{'dateexpiry'});
466 $data{'dateofbirth'} = format_date($data{'dateofbirth'});
468 $template->param( "showguarantor" => ($category_type=~/A|I|S/) ? 0 : 1); # associate with step to know where you are
469 $debug and warn "memberentry step: $step";
470 $template->param(%data);
471 $template->param( "step_$step" => 1) if $step;# associate with step to know where u are
472 $template->param( "step" => $step) if $step;# associate with step to know where u are
474 BorrowerMandatoryField => C4::Context->preference("BorrowerMandatoryField"),#field to test with javascript
475 category_type => $category_type,#to know the category type of the borrower
476 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
477 select_city => $select_city,
478 "$category_type" => 1,# associate with step to know where u are
479 destination => $destination,#to know wher u come from and wher u must go in redirect
480 check_member => $check_member,#to know if the borrower already exist(=>1) or not (=>0)
481 flags =>$data{'flags'},
483 nodouble => $nodouble,
484 borrowernumber => $borrowernumber,#register number
485 "contacttitle_".$data{'contacttitle'} => "SELECTED" ,
486 guarantorid => $guarantorid,
487 ethcatpopup => $ethcatpopup,
488 relshiploop => \@relshipdata,
489 citypopup => $citypopup,
490 roadpopup => $roadpopup,
491 borrotitlepopup => $borrotitlepopup,
492 guarantorinfo => $guarantorinfo,
493 flagloop => \@flagdata,
494 dateformat => C4::Dates->new()->visual(),
495 check_categorytype =>$check_categorytype,#to recover the category type with checkcategorytype function
497 nok => $nok,#flag to konw if an error
498 CGIbranch => $CGIbranch,
499 memberofinstution => $member_of_institution,
500 CGIorganisations => $CGIorganisations,
501 NoUpdateLogin => $NoUpdateLogin
504 output_html_with_http_headers $input, $cookie, $template->output;