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 C4::Interface::CGI::Output;
35 use Digest::MD5 qw(md5_base64);
41 my $dbh = C4::Context->dbh;
43 my $category_type = $input->param('category_type') || die "NO CATEGORY_TYPE !"; # A, E, C, or P
44 my $step=$input->param('step') || 0;
45 my ($template, $loggedinuser, $cookie)
46 = get_template_and_user({template_name => "members/memberentry$category_type.tmpl",
50 flagsrequired => {borrowers => 1},
54 my $borrowerid=$input->param('borrowerid');
55 my $guarantorid=$input->param('guarantorid');
56 my $modify=$input->param('modify');
57 my $delete=$input->param('delete');
58 my $op=$input->param('op');
59 my $categorycode=$input->param('categorycode');
60 my $destination=$input->param('destination');
61 my $cardnumber=$input->param('cardnumber');
62 my $check_member=$input->param('check_member');
63 my $name_city=$input->param('name_city');
64 my $nodouble=$input->param('nodouble');
65 my $select_city=$input->param('select_city');
66 my $nok=$input->param('nok');
68 # $check_categorytype contains the value of duplicate borrowers category type to redirect in good template in step =2
69 my $check_categorytype=$input->param('check_categorytype');
70 # NOTE: Alert for ethnicity and ethnotes fields, they are unvalided in all borrowers form
73 #function to automatic setup the mandatory fields (visual with css)
74 my $check_BorrowerMandatoryField=C4::Context->preference("BorrowerMandatoryField");
75 my @field_check=split(/\|/,$check_BorrowerMandatoryField);
76 foreach (@field_check) {
77 $template->param( "mandatory$_" => 1);
80 $template->param( "checked" => 1) if ($nodouble eq 1);
82 # if a add or modify is requested => check validity of data.
83 if ($op eq 'add' or $op eq 'modify') {
84 my @names=$input->param;
85 foreach my $key (@names){
86 $data{$key}=$input->param($key)||'';
87 $data{$key}=~ s/\'/\\\'/g;
88 $data{$key}=~ s/\"/\\\"/g;
91 #############test for member being unique #############
92 if ($op eq 'add' && $step eq 2){
93 (my $category_type_send=$category_type ) if ($category_type eq 'I');
94 my $check_category; # recover the category code of the doublon suspect borrowers
95 ($check_member,$check_category)= checkuniquemember($category_type_send,$data{'surname'},$data{'firstname'},format_date_in_iso($data{'dateofbirth'}));
96 # recover the category type if the borrowers is a duplicate
97 ($check_categorytype,undef)=getcategorytype($check_category);
100 # CHECKS step by step
103 ###############test to take the right zipcode and city name ##############
104 if ($category_type ne 'I' and $guarantorid){
105 my ($borrower_city,$borrower_zipcode)=&getzipnamecity($select_city);
106 $data{'city'}= $borrower_city;
107 $data{'zipcode'}=$borrower_zipcode;
109 if ($category_type eq 'C' and $guarantorid){
110 my $guarantordata=getguarantordata($guarantorid);
111 if (($data{'contactname'} eq '' or $data{'contactname'} ne $guarantordata->{'surname'})) {
112 $data{'contactfirstname'}=$guarantordata->{'firstname'};
113 $data{'contactname'}=$guarantordata->{'surname'};
114 $data{'contacttitle'}=$guarantordata->{'title'};
115 $data{'streetnumber'}=$guarantordata->{'streetnumber'};
116 $data{'address'}=$guarantordata->{'address'};
117 $data{'streettype'}=$guarantordata->{'streettype'};
118 $data{'address2'}=$guarantordata->{'address2'};
119 $data{'zipcode'}=$guarantordata->{'zipcode'};
120 $data{'city'}=$guarantordata->{'city'};
121 $data{'phone'}=$guarantordata->{'phone'};
122 $data{'phonepro'}=$guarantordata->{'phonepro'};
123 $data{'mobile'}=$guarantordata->{'mobile'};
124 $data{'fax'}=$guarantordata->{'fax'};
125 $data{'email'}=$guarantordata->{'email'};
126 $data{'emailpro'}=$guarantordata->{'emailpro'};
132 if ( ($data{'login'} eq '')){
133 my $onefirstnameletter=substr($data{'firstname'},0,1);
134 my $fivesurnameletter=substr($data{'surname'},0,5);
135 $data{'login'}=lc($onefirstnameletter.$fivesurnameletter);
137 if ($op eq 'add' and $data{'dateenrolled'} eq ''){
139 #insert ,in field "dateenrolled" , the current date
140 $data{'dateenrolled'}=$today;
141 #if date expiry is null u must calculate the value only in this case
142 $data{'dateexpiry'} = calcexpirydate($data{'categorycode'},$today);
144 if ($op eq 'modify' ){
146 # if date expiry is null u must calculate the value only in this case
147 if ($data{'dateexpiry'} eq ''){
148 $data{'dateexpiry'} = calcexpirydate($data{'categorycode'},$today);
154 # this value show if the login and password are been used
155 my $loginexist=checkuserpassword($borrowerid,$data{'login'},$data{'password'});
156 # test to know if u must save or create the borrowers
157 if ($op eq 'modify'){
158 # test to know if another user have the same password and same login
159 if ($loginexist eq 0) {
161 logaction($loggedinuser,"MEMBERS","modify member", $borrowerid, "");
164 push @errors, "ERROR_login_exist";
168 # test to know if another user have the same password and same login
170 push @errors, "ERROR_login_exist";
173 $borrowerid = &newmember(%data);
174 logaction($loggedinuser,"MEMBERS","add member", $borrowerid, "");
179 foreach my $error (@errors) {
180 $template->param( $error => 1);
182 $template->param(nok => 1);
183 $step--; # decrease step : go back to step 2, the step++ just before showing the template will go again to 3
187 if($destination eq "circ"){
188 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$data{'cardnumber'}");
190 if ($loginexist == 0) {
191 print $input->redirect("/cgi-bin/koha/members/moremember.pl?bornum=$borrowerid");
196 if (C4::Context->preference("IndependantBranches")) {
197 my $userenv = C4::Context->userenv;
198 if ($userenv->{flags} != 1){
199 unless ($userenv->{branch} eq $data{'branchcode'}){
200 push @errors, "ERROR_branch";
208 print $input->redirect("/cgi-bin/koha/deletemem.pl?member=$borrowerid");
209 } else { # this else goes down the whole script
210 # retrieve previous values : either in DB or in CGI, in case of errors in values
212 # test to now if u add or modify a borrower (modify =>to take all carateristic of the borrowers)
213 if (!$op and !$data{'surname'}) {
214 $data=borrdata('',$borrowerid);
217 if (C4::Context->preference("IndependantBranches")) {
218 my $userenv = C4::Context->userenv;
219 if ($userenv->{flags} != 1 && $data{branchcode}){
220 unless ($userenv->{branch} eq $data{'branchcode'}){
221 print $input->redirect("/cgi-bin/koha/members/members-home.pl");
226 $template->param( updtype => 'I');
228 $template->param( updtype => 'M');
230 my $cardnumber=$data{'cardnumber'};
231 $cardnumber=fixup_cardnumber($data{'cardnumber'}) if $op eq 'add';
232 if ($data{'sex'} eq 'F'){
233 $template->param(female => 1);
235 my ($categories,$labels)=ethnicitycategories();
236 my $ethnicitycategoriescount=$#{$categories};
238 if ($ethnicitycategoriescount>=0) {
239 $ethcatpopup = CGI::popup_menu(-name=>'ethnicity',
241 -values=>$categories,
242 -default=>$data{'ethnicity'},
244 $template->param(ethcatpopup => $ethcatpopup); # bad style, has to be fixed
248 ($categories,$labels)=borrowercategories($category_type,$op);
250 #if u modify the borrowers u must have the right value for is category code
252 (my $default_category=$data{'categorycode'}) if ($op eq '');
253 my $catcodepopup = CGI::popup_menu(-name=>'categorycode',
254 -id => 'categorycode',
255 -values=>$categories,
256 -default=>$default_category,
261 (my $selectcity=&getidcity($data{'city'})) if ($select_city eq '');
262 $default_city=$selectcity;
264 my($cityid,$name_city)=getcities();
265 $template->param( city_cgipopup => 1) if ($cityid );
266 my $citypopup = CGI::popup_menu(-name=>'select_city',
267 -id => 'select_city',
271 -default=>$default_city
274 my $default_roadtype;
275 $default_roadtype=$data{'streettype'} ;
276 my($roadtypeid,$road_type)=getroadtypes();
277 $template->param( road_cgipopup => 1) if ($roadtypeid );
278 my $roadpopup = CGI::popup_menu(-name=>'streettype',
280 -values=>$roadtypeid,
283 -default=>$default_roadtype
287 my @relationships = split /,|\|/,C4::Context->preference('BorrowerRelationship');
289 while (@relationships) {
290 my $relship = shift @relationships || '';
291 my %row = ('relationship' => $relship);
292 if ($data{'relationship'} eq $relship) {
293 $row{'selected'}=' selected';
297 push(@relshipdata, \%row);
299 my %flags = ( 'gonenoaddress' => ['gonenoaddress', 'Adresse érronée'],
300 'lost' => ['lost', 'Carte Perdue'],
301 'debarred' => ['debarred', 'Lecteur exclu']);
304 foreach (keys(%flags)) {
306 my %row = ('key' => $key,
307 'name' => $flags{$key}[0],
308 'html' => $flags{$key}[1]);
310 $row{'yes'}=' checked';
314 $row{'no'}=' checked';
316 push(@flagdata, \%row);
320 $template->param( modify => 1 );
323 #Convert dateofbirth to correct format
324 $data{'dateofbirth'} = format_date($data{'dateofbirth'});
328 my $branches=getbranches();
330 # -----------------------------------------------------
331 # the value of ip from the branches hash table
333 # $ip is the ip of user when is connect to koha
334 my $ip = $ENV{'REMOTE_ADDR'};
335 # -----------------------------------------------------
336 foreach my $branch (keys %$branches) {
337 if ((not C4::Context->preference("IndependantBranches")) || (C4::Context->userenv->{'flags'} == 1)) {
338 push @select_branch, $branch;
339 $select_branches{$branch} = $branches->{$branch}->{'branchname'};
340 # take the ip number from branches "op"
341 $select_ip = $branches->{$branch}->{'branchip'} || '';
343 # test $select_ip equal $ip to attribute the default value for the scrolling list
344 if ($select_ip eq $ip) {
345 $default = $branches->{$branch}->{'branchcode'};
348 push @select_branch, $branch if ($branch eq C4::Context->userenv->{'branch'});
349 $select_branches{$branch} = $branches->{$branch}->{'branchname'} if ($branch eq C4::Context->userenv->{'branch'});
351 $default = C4::Context->userenv->{'branch'};
355 # --------------------------------------------------------------------------------------------------------
356 my $CGIbranch = CGI::scrolling_list(-id => 'branchcode',
357 -name => 'branchcode',
358 -values => \@select_branch,
359 -labels => \%select_branches,
363 -default => $default,
367 # --------------------------------------------------------------------------------------------------------
369 my $CGIsort1 = buildCGIsort("Bsort1","sort1",$data{'sort1'});
371 $template->param(CGIsort1 => $CGIsort1);
372 $template->param( sort1 => $data{'sort1'});
374 $template->param( sort1 => $data{'sort1'});
377 my $CGIsort2 = buildCGIsort("Bsort2","sort2",$data{'sort2'});
379 $template->param(CGIsort2 =>$CGIsort2);
381 $template->param( sort2 => $data{'sort2'});
385 $data{'opacnotes'} =~ s/\\//g;
386 $data{'borrowernotes'} =~ s/\\//g;
387 # increase step to see next page
389 warn "CITY".$data{city};
391 BorrowerMandatoryField => C4::Context->preference("BorrowerMandatoryField"),#field to test with javascript
392 category_type => $category_type,#to know the category type of the borrower
393 select_city => $select_city,
394 "step_$step" => 1,# associate with step to know where u are
396 destination => $destination,#to know wher u come from and wher u must go in redirect
397 check_member => $check_member,#to know if the borrower already exist(=>1) or not (=>0)
398 # flags =>$data{'flags'},
401 nodouble => $nodouble,
402 borrowerid => $borrowerid,#register number
403 cardnumber => $data{'cardnumber'},
404 surname => uc($data{'surname'}),
405 firstname => ucfirst($data{'firstname'}),
406 "title_".$data{'title'} => " SELECTED ",
407 title => $data{'title'},
408 othernames => $data{'othernames'},
409 initials => $data{'initials'},
410 streetnumber => $data{'streetnumber'},
411 streettype =>$data{'streettype'},
412 address => $data{'address'},
413 address2 => $data{'address2'},
414 city => $data{'city'},
415 zipcode => $data{'zipcode'},
416 email => $data{'email'},
417 phone => $data{'phone'},
418 mobile => $data{'mobile'},
420 phonepro => $data{'phonepro'},
421 emailpro => $data{'emailpro'},
422 b_address => $data{'b_address'},
423 b_city => $data{'b_city'},
424 b_zipcode => $data{'b_zipcode'},
425 b_email => $data{'b_email'},
426 b_phone => $data{'b_phone'},
427 dateofbirth => $data{'dateofbirth'},
428 branchcode => $data{'branchcode'},
429 catcodepopup => $catcodepopup,
430 categorycode => $data{'categorycode'},
431 dateenrolled => format_date($data{'dateenrolled'}),
432 dateexpiry => format_date($data{'dateexpiry'}),
433 debarred => $data{'debarred'},
434 gonenoaddress => $data{'gonenoaddress'},
435 lost => $data{'lost'},
436 contactname => uc($data{'contactname'}),
437 contactfirstname=> ucfirst($data{'contactfirstname'}),
438 "contacttitle_".$data{'contacttitle'} => "SELECTED" ,
439 contacttitle => $data{'contacttitle'},
440 guarantorid => $guarantorid,
441 ethcatpopup => $ethcatpopup,
443 login => $data{'login'},
444 password => $data{'password'},
445 opacnotes => $data{'opacnotes'},
446 contactnotes => $data{'contactnotes'},
447 borrowernotes => $data{'borrowernotes'},
448 relshiploop => \@relshipdata,
449 relationship => $data{'relationship'},
450 citypopup => $citypopup,
451 roadpopup => $roadpopup,
452 contacttype => $data{'contacttype'},
453 flagloop => \@flagdata,
454 # "contacttype_".$data{'contacttype'} =>" SELECTED ",
455 dateformat => display_date_format(),
456 check_categorytype =>$check_categorytype,#to recover the category type with checkcategorytype function
458 # city_choice => $city_choice ,#check if the city was selected
459 nok => $nok,#flag to konw if an error
460 CGIbranch => $CGIbranch,
462 #$template->param(Institution => 1) if ($categorycode eq "I");
463 output_html_with_http_headers $input, $cookie, $template->output;