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 my $dbh = C4::Context->dbh;
47 my ($template, $loggedinuser, $cookie)
48 = get_template_and_user({template_name => "members/memberentrygen.tmpl",
52 flagsrequired => {borrowers => 1},
55 my $guarantorid=$input->param('guarantorid');
56 my $borrowernumber=$input->param('borrowernumber');
57 my $actionType=$input->param('actionType') || '';
58 my $modify=$input->param('modify');
59 my $delete=$input->param('delete');
60 my $op=$input->param('op');
61 my $destination=$input->param('destination');
62 my $cardnumber=$input->param('cardnumber');
63 my $check_member=$input->param('check_member');
64 my $name_city=$input->param('name_city');
65 my $nodouble=$input->param('nodouble');
66 my $select_city=$input->param('select_city');
67 my $nok=$input->param('nok');
68 my $guarantorinfo=$input->param('guarantorinfo');
69 my $step=$input->param('step') || 0;
72 # $check_categorytype contains the value of duplicate borrowers category type to redirect in good template in step =2
73 my $check_categorytype=$input->param('check_categorytype');
74 # NOTE: Alert for ethnicity and ethnotes fields, they are unvalided in all borrowers form
77 my $userenv = C4::Context->userenv;
79 $template->param("uppercasesurnames" => C4::Context->preference('uppercasesurnames'));
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);
87 $template->param("add"=>1) if ($op eq 'add');
88 $template->param("checked" => 1) if ($nodouble eq 1);
89 my $categorycode=$input->param('categorycode');
90 ($borrower_data=GetMember($borrowernumber,'borrowernumber')) if ($op eq 'modify' or $op eq 'save');
91 $categorycode=$borrower_data->{'categorycode'} unless $categorycode;
93 $category_type = $input->param('category_type');
94 unless ($category_type or !($categorycode)){
95 my $borrowercategory= GetBorrowercategory($categorycode);
96 $category_type = $borrowercategory->{'category_type'};
98 $category_type="A" unless $category_type; # FIXME we should display a error message instead of a 500 error !
100 # if a add or modify is requested => check validity of data.
101 %data= %$borrower_data if ($borrower_data);
104 if ($op eq 'insert' || $op eq 'modify' || $op eq 'save') {
105 my @names= ($borrower_data && $op ne 'save') ? keys %$borrower_data : $input->param();
106 foreach my $key (@names) {
107 $newdata{$key} = $input->param($key) if (defined $input->param($key));
108 $newdata{$key} =~ s/\"/"/gg unless $key eq 'borrowernotes' or $key eq 'opacnote';
110 $newdata{'dateenrolled'}=format_date_in_iso($newdata{'dateenrolled'}) if ($newdata{dateenrolled});
111 $newdata{'dateexpiry'} =format_date_in_iso($newdata{'dateexpiry'} ) if ($newdata{dateexpiry});
112 $newdata{'dateofbirth'} =format_date_in_iso($newdata{'dateofbirth'} ) if ($newdata{dateofbirth});
113 # check permission to modify login info.
114 if (ref($borrower_data) && ($borrower_data->{'category_type'} eq 'S') && ! (C4::Auth::haspermission($dbh,$userenv->{'id'},{'staffaccess'=>1})) ) {
119 #############test for member being unique #############
120 if ($op eq 'insert'){
121 my $category_type_send=$category_type if ($category_type eq 'I');
122 my $check_category; # recover the category code of the doublon suspect borrowers
123 ($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'}));
125 # recover the category type if the borrowers is a doublon
126 my $tmpborrowercategory=GetBorrowercategory($check_category);
127 $check_categorytype=$tmpborrowercategory->{'category_type'};
130 #recover all data from guarantor address phone ,fax...
131 if (($category_type eq 'C' || $category_type eq 'P') and $guarantorid ne '' ){
132 my $guarantordata=GetMember($guarantorid);
133 $guarantorinfo=$guarantordata->{'surname'}." , ".$guarantordata->{'firstname'};
134 if (($data{'contactname'} eq '' or $data{'contactname'} ne $guarantordata->{'surname'})) {
135 $data{'contactfirstname'}=$guarantordata->{'firstname'};
136 $data{'contactname'}=$guarantordata->{'surname'};
137 $data{'contacttitle'}=$guarantordata->{'title'};
138 map {$data{$_}=$guarantordata->{$_}}('streetnumber','address','streettype','address2','zipcode','city','phone','phonepro','mobile','fax','email','emailpro');
142 ###############test to take the right zipcode and city name ##############
143 if ( $guarantorid eq ''){
145 my ($borrower_city,$borrower_zipcode)=&getzipnamecity($select_city);
146 $newdata{'city'}= $borrower_city;
147 $newdata{'zipcode'}=$borrower_zipcode;
150 #builds default userid
151 if ( (defined $newdata{'userid'}) && ($newdata{'userid'} eq '')){
152 my $onefirstnameletter=substr($data{'firstname'},0,1);
153 my $fivesurnameletter=substr($data{'surname'},0,9);
154 $newdata{'userid'}=lc($onefirstnameletter.$fivesurnameletter);
158 if ($op eq 'save' || $op eq 'insert'){
159 if (checkcardnumber($newdata{cardnumber},$newdata{borrowernumber})){
160 push @errors, 'ERROR_cardnumber';
163 my $dateofbirthmandatory=0;
164 map {$dateofbirthmandatory=1 if $_ eq "dateofbirth"} @field_check;
165 if ($newdata{dateofbirth} && $dateofbirthmandatory) {
166 my $age = GetAge($newdata{dateofbirth});
167 my $borrowercategory=GetBorrowercategory($newdata{'categorycode'});
168 if (($age > $borrowercategory->{'upperagelimit'}) or ($age < $borrowercategory->{'dateofbirthrequired'})) {
169 push @errors, 'ERROR_age_limitations';
174 if (C4::Context->preference("IndependantBranches")) {
175 if ($userenv && $userenv->{flags} != 1){
176 #warn " $newdata{'branchcode'} : ".$userenv->{flags}.":".$userenv->{branch};
177 unless (!$newdata{'branchcode'} || $userenv->{branch} eq $newdata{'branchcode'}){
178 push @errors, "ERROR_branch";
183 # Check if the userid is unique
184 if (($op eq 'insert' || $op eq 'save') && !Check_Userid($newdata{'userid'},$borrowernumber)) {
185 push @errors, "ERROR_login_exist";
191 if ($op eq 'modify' || $op eq 'insert'){
192 unless ($newdata{'dateexpiry'}){
193 my $arg2 = $newdata{'dateenrolled'} || sprintf('%04d-%02d-%02d', Today());
194 $newdata{'dateexpiry'} = GetExpiryDate($newdata{'categorycode'},$arg2);
198 if ($op eq 'insert'){
199 # Check if the userid is unique
201 $borrowernumber = &AddMember(%newdata);
202 if ($data{'organisations'}){
203 # need to add the members organisations
204 my @orgs=split(/\|/,$data{'organisations'});
205 add_member_orgs($borrowernumber,\@orgs);
207 if ($destination eq "circ") {
208 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$data{'cardnumber'}");
210 if ($loginexist == 0) {
211 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$borrowernumber");
217 # test to know if another user have the same password and same login
220 delete $newdata{'password'};
221 delete $newdata{'userid'};
223 &ModMember(%newdata);
224 if ($destination eq "circ") {
225 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$data{'cardnumber'}");
227 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$borrowernumber");
233 print $input->redirect("/cgi-bin/koha/deletemem.pl?member=$borrowernumber");
237 $op="add" if ($op eq "insert");
238 $op="modify" if ($op eq "save");
240 $template->param( updtype => ($op eq "insert"?'I':'M'));
242 $template->param( step_1 => 1,step_2 => 1,step_3 => 1);
245 if (C4::Context->preference("IndependantBranches")) {
246 my $userenv = C4::Context->userenv;
247 if ($userenv->{flags} != 1 && $data{branchcode}){
248 unless ($userenv->{branch} eq $data{'branchcode'}){
249 print $input->redirect("/cgi-bin/koha/members/members-home.pl");
254 $template->param( updtype => 'I',step_1=>1,step_2=>1,step_3=>1);
256 if ($op eq "modify") {
257 $template->param( updtype => 'M',modify => 1 );
258 $template->param( step_1=>1,step_2=>1,step_3=>1) unless $step;
260 # my $cardnumber=$data{'cardnumber'};
261 $data{'cardnumber'}=fixup_cardnumber($data{'cardnumber'}) if $op eq 'add';
262 if ($data{'sex'} eq 'F'){
263 $template->param(female => 1);
264 } elsif ($data{'sex'} eq 'M'){
265 $template->param(male => 1);
267 $template->param(none => 1);
270 ##Now all the data to modify a member.
271 my ($categories,$labels)=ethnicitycategories();
273 my $ethnicitycategoriescount=$#{$categories};
275 if ($ethnicitycategoriescount>=0) {
276 $ethcatpopup = CGI::popup_menu(-name=>'ethnicity',
279 -values=>$categories,
280 -default=>$data{'ethnicity'},
282 $template->param(ethcatpopup => $ethcatpopup); # bad style, has to be fixed
286 my $action="WHERE category_type=?";
287 ($categories,$labels)=GetborCatFromCatType($category_type,$action);
289 if(scalar(@$categories)){
290 #if you modify the borrowers you must have the right value for his category code
291 my $default_category=$newdata{'categorycode'} if ($op eq 'modify');
292 my $catcodepopup = CGI::popup_menu(
293 -name=>'categorycode',
294 -id => 'categorycode',
295 -values=>$categories,
297 -default=>$default_category
299 $template->param(catcodepopup=>$catcodepopup);
302 $select_city=getidcity($data{'city'}) if ($guarantorid ne '0');
303 ($default_city=$select_city) if ($step eq 0);
304 if ($select_city eq '' ){
305 my $selectcity=&getidcity($data{'city'});
306 $default_city=$selectcity;
309 ($cityid,$name_city)=GetCities();
310 $template->param( city_cgipopup => 1) if ($cityid );
311 my $citypopup = CGI::popup_menu(-name=>'select_city',
312 -id => 'select_city',
315 -default=>$default_city
318 my $default_roadtype;
319 $default_roadtype=$data{'streettype'} ;
320 my($roadtypeid,$road_type)=GetRoadTypes();
321 $template->param( road_cgipopup => 1) if ($roadtypeid );
322 my $roadpopup = CGI::popup_menu(-name=>'streettype',
324 -values=>$roadtypeid,
327 -default=>$default_roadtype
330 my $default_borrowertitle;
331 $default_borrowertitle=$data{'title'} ;
332 my($borrowertitle)=GetTitles();
333 my $borrotitlepopup = CGI::popup_menu(-name=>'title',
335 -values=>$borrowertitle,
337 -default=>$default_borrowertitle
341 my @relationships = split /,|\|/,C4::Context->preference('BorrowerRelationship');
343 while (@relationships) {
344 my $relship = shift @relationships || '';
345 my %row = ('relationship' => $relship);
346 if ($data{'relationship'} eq $relship) {
347 $row{'selected'}=' selected';
351 push(@relshipdata, \%row);
353 my %flags = ( 'gonenoaddress' => ['gonenoaddress', 'Gone no address '],
354 'lost' => ['lost', 'Lost'],
355 'debarred' => ['debarred', 'Debarred']);
358 foreach (keys(%flags)) {
360 my %row = ('key' => $key,
361 'name' => $flags{$key}[0],
362 'html' => $flags{$key}[1]);
364 $row{'yes'}=' checked';
368 $row{'no'}=' checked';
370 push(@flagdata, \%row);
379 my $onlymine=(C4::Context->preference('IndependantBranches') &&
380 C4::Context->userenv &&
381 C4::Context->userenv->{flags} !=1 &&
382 C4::Context->userenv->{branch}?1:0);
384 my $branches=GetBranches($onlymine);
387 foreach my $branch (sort keys %$branches) {
388 push @select_branch,$branch;
389 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
390 $default = C4::Context->userenv->{'branch'} if (C4::Context->userenv && C4::Context->userenv->{'branch'});
392 # --------------------------------------------------------------------------------------------------------
393 #in modify mod :default value from $CGIbranch comes from borrowers table
394 #in add mod: default value come from branches table (ip correspendence)
395 $default=$data{'branchcode'} if ($op eq 'modify');
396 my $CGIbranch = CGI::scrolling_list(-id => 'branchcode',
397 -name => 'branchcode',
398 -values => \@select_branch,
399 -labels => \%select_branches,
403 -default => $default,
405 my $CGIorganisations;
406 my $member_of_institution;
407 if (C4::Context->preference("memberofinstitution")){
408 my $organisations=get_institutions();
411 foreach my $organisation (keys %$organisations) {
412 push @orgs,$organisation;
413 $org_labels{$organisation}=$organisations->{$organisation}->{'surname'};
415 $member_of_institution=1;
417 $CGIorganisations = CGI::scrolling_list( -id => 'organisations',
418 -name => 'organisations',
419 -labels => \%org_labels,
428 # --------------------------------------------------------------------------------------------------------
430 my $CGIsort1 = buildCGIsort("Bsort1","sort1",$data{'sort1'});
432 $template->param(CGIsort1 => $CGIsort1);
434 $template->param( sort1 => $data{'sort1'});
436 my $CGIsort2 = buildCGIsort("Bsort2","sort2",$data{'sort2'});
438 $template->param(CGIsort2 =>$CGIsort2);
440 $template->param( sort2 => $data{'sort2'});
443 foreach my $error (@errors) {
444 $template->param( $error => 1);
446 $template->param(nok => 1);
449 #Formatting data for display
451 if ($data{'dateenrolled'} eq ''){
452 my $today= sprintf('%04d-%02d-%02d', Today());
453 $data{'dateenrolled'}=$today;
455 if (C4::Context->preference('uppercasesurnames')) {
456 $data{'surname'} =uc($data{'surname'} );
457 $data{'contactname'}=uc($data{'contactname'});
459 $data{'dateenrolled'} = format_date($data{'dateenrolled'});
460 $data{'dateexpiry'} = format_date($data{'dateexpiry'});
461 $data{'dateofbirth'} = format_date($data{'dateofbirth'});
463 $template->param( "showguarantor" => ($category_type=~/A|I|S/) ? 0 : 1); # associate with step to know where you are
465 $template->param(%data);
466 $template->param( "step_$step" => 1) if $step;# associate with step to know where u are
467 $template->param( "step" => $step) if $step;# associate with step to know where u are
469 BorrowerMandatoryField => C4::Context->preference("BorrowerMandatoryField"),#field to test with javascript
470 category_type => $category_type,#to know the category type of the borrower
471 DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),
472 select_city => $select_city,
473 "$category_type" => 1,# associate with step to know where u are
474 destination => $destination,#to know wher u come from and wher u must go in redirect
475 check_member => $check_member,#to know if the borrower already exist(=>1) or not (=>0)
476 flags =>$data{'flags'},
478 nodouble => $nodouble,
479 borrowernumber => $borrowernumber,#register number
480 "contacttitle_".$data{'contacttitle'} => "SELECTED" ,
481 guarantorid => $guarantorid,
482 ethcatpopup => $ethcatpopup,
483 relshiploop => \@relshipdata,
484 citypopup => $citypopup,
485 roadpopup => $roadpopup,
486 borrotitlepopup => $borrotitlepopup,
487 guarantorinfo => $guarantorinfo,
488 flagloop => \@flagdata,
489 dateformat => C4::Dates->new()->visual(),
490 check_categorytype =>$check_categorytype,#to recover the category type with checkcategorytype function
492 nok => $nok,#flag to konw if an error
493 CGIbranch => $CGIbranch,
494 memberofinstution => $member_of_institution,
495 CGIorganisations => $CGIorganisations,
496 NoUpdateLogin => $NoUpdateLogin
499 output_html_with_http_headers $input, $cookie, $template->output;