members.js :translate javascript message
[koha.git] / members / memberentry.pl
1 #!/usr/bin/perl
2 # $Id$
3
4 # Copyright 2006 SAN OUEST PROVENCE et Paul POULAIN
5 #
6 # This file is part of Koha.
7 #
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
11 # version.
12 #
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.
16 #
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
20
21 # pragma
22 use strict;
23
24 # external modules
25 use Date::Calc qw/Today/;
26 use CGI;
27 use HTML::Template;
28 use Date::Manip;
29 use Digest::MD5 qw(md5_base64);
30
31 # internal modules
32 use C4::Auth;
33 use C4::Context;
34 use C4::Output;
35 use C4::Interface::CGI::Output;
36 use C4::Search;
37 use C4::Members;
38 use C4::Koha;
39 use C4::Date;
40 use C4::Input;
41 use C4::Log;
42
43 my $input = new CGI;
44 my %data;
45
46
47 my $dbh = C4::Context->dbh;
48
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",
53                              query => $input,
54                              type => "intranet",
55                              authnotrequired => 0,
56                              flagsrequired => {borrowers => 1},
57                              debug => 1,
58                              });
59
60 my $borrowerid=$input->param('borrowerid');
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 $categorycode=$input->param('categorycode');
68 my $destination=$input->param('destination');
69 my $cardnumber=$input->param('cardnumber');
70 my $check_member=$input->param('check_member');
71 my $name_city=$input->param('name_city');
72 my $nodouble=$input->param('nodouble');
73 my $select_city=$input->param('select_city');
74 my $nok=$input->param('nok');
75
76 my @errors;
77
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
81
82
83 #function  to automatic setup the mandatory  fields (visual with css)
84 my $check_BorrowerMandatoryField=C4::Context->preference("BorrowerMandatoryField");
85 my @field_check=split(/\|/,$check_BorrowerMandatoryField);
86 foreach (@field_check) {
87 $template->param( "mandatory$_" => 1);          
88 }       
89
90 $template->param( "checked" => 1) if ($nodouble eq 1);
91
92 my $borrower_data=borrdata('',$borrowernumber);
93 # if a add or modify is requested => check validity of data.
94 if ($step eq 0){
95     foreach my $column (keys %$borrower_data){
96         $data{$column}=$borrower_data->{$column};
97     }
98     $data{'borrowerid'}=$borrowernumber;
99     if (!$borrowerid){
100         $borrowerid=$borrowernumber;
101     }
102 }    
103
104 if ($op eq 'add' or $op eq 'modify') {
105         my @names=$input->param;
106         foreach my $key (@names){
107                 $data{$key}=$input->param($key)||'';
108                 $data{$key}=~ s/\'/\\\'/g;
109                 $data{$key}=~ s/\"/\\\"/g;
110         }
111
112         # WARN : some tests must be done whatever the step, because the librarian can click on any tab.
113         #############test for member being unique #############
114         if ($op eq 'add'){
115                 (my $category_type_send=$category_type ) if ($category_type eq 'I'); 
116                 my $check_category; # recover the category code of the doublon suspect borrowers
117            ($check_member,$check_category)= checkuniquemember($category_type_send,$data{'surname'},$data{'firstname'},format_date_in_iso($data{'dateofbirth'}));
118         
119 #       recover the category type if the borrowers is a doublon 
120         ($check_categorytype,undef)=getcategorytype($check_category);
121         }
122         # CHECKS step by step
123 # STEP 1
124         if ($step eq 1) {
125                 ###############test to take the right zipcode and city name ##############
126                 if ($category_type ne 'I' and $guarantorid){
127                         my ($borrower_city,$borrower_zipcode)=&getzipnamecity($select_city);
128                         $data{'city'}= $borrower_city;
129                         $data{'zipcode'}=$borrower_zipcode;
130                 }
131                 if ($category_type eq 'C' and $guarantorid){
132                         my $guarantordata=getguarantordata($guarantorid);
133                         if (($data{'contactname'} eq '' or $data{'contactname'} ne $guarantordata->{'surname'})) {
134                                 $data{'contactfirstname'}=$guarantordata->{'firstname'};        
135                                 $data{'contactname'}=$guarantordata->{'surname'};
136                                 $data{'contacttitle'}=$guarantordata->{'title'};
137                                 $data{'streetnumber'}=$guarantordata->{'streetnumber'};
138                                 $data{'address'}=$guarantordata->{'address'};
139                                 $data{'streettype'}=$guarantordata->{'streettype'};
140                                 $data{'address2'}=$guarantordata->{'address2'};
141                                 $data{'zipcode'}=$guarantordata->{'zipcode'};
142                                 $data{'city'}=$guarantordata->{'city'};
143                                 $data{'phone'}=$guarantordata->{'phone'};
144                                 $data{'phonepro'}=$guarantordata->{'phonepro'};
145                                 $data{'mobile'}=$guarantordata->{'mobile'};
146                                 $data{'fax'}=$guarantordata->{'fax'};
147                                 $data{'email'}=$guarantordata->{'email'};
148                                 $data{'emailpro'}=$guarantordata->{'emailpro'};
149                         }
150                     }
151                 if ($categorycode ne 'I') {
152                     # is the age of the borrower compatible with age limitations of
153                     # the borrower category
154                     my $query = '
155 SELECT upperagelimit,
156        dateofbirthrequired
157   FROM categories
158   WHERE categorycode = ?
159 ';
160                     my $sth=$dbh->prepare($query);
161                     $sth->execute($categorycode);
162                     my $category_info = $sth->fetchrow_hashref;
163                     my $age = get_age(format_date_in_iso($data{dateofbirth}));
164                     if ($age > $category_info->{upperagelimit}
165                             or $age < $category_info->{dateofbirthrequired}
166                         ) {
167                         push @errors, 'ERROR_age_limitations';
168                         $nok = 1;
169                     }
170                 }
171         }
172 # STEP 2
173         if ($step eq 2) {
174                         if ( ($data{'userid'} eq '')){
175                                 my $onefirstnameletter=substr($data{'firstname'},0,1);
176                                 my $fivesurnameletter=substr($data{'surname'},0,5);
177                                 $data{'userid'}=lc($onefirstnameletter.$fivesurnameletter);
178                         }
179                         if ($op eq 'add' and $data{'dateenrolled'} eq ''){
180                                 my $today= sprintf('%04d-%02d-%02d', Today());
181                                 #insert ,in field "dateenrolled" , the current date
182                                 $data{'dateenrolled'}=$today;
183                                 #if date expiry is null u must calculate the value only in this case
184                                 $data{'dateexpiry'} = calcexpirydate($data{'categorycode'},$today);
185                         }
186                         if ($op eq 'modify' ){
187                         my $today= sprintf('%04d-%02d-%02d', Today());
188 #                       if date expiry is null u must calculate the value only in this case
189                         if ($data{'dateexpiry'} eq ''){
190                         $data{'dateexpiry'} = calcexpirydate($data{'categorycode'},$today);
191                         }
192                 }
193         }
194 # STEP 3
195         if ($step eq 3) {
196                 # this value show if the login and password are been used
197                 my $loginexist=checkuserpassword($borrowernumber,$data{'userid'},$data{'password'});
198                 # test to know if u must save or create the borrowers
199                 if ($op eq 'modify'){
200                         # test to know if another user have the same password and same login            
201                         if ($loginexist eq 0) {
202                                 &modmember(%data);              
203                                 logaction($loggedinuser,"MEMBERS","modify member", $borrowernumber, "");
204                         }
205                         else {
206                                 push @errors, "ERROR_login_exist";
207                                 $nok=1;
208                         }
209                 }else{
210                         # test to know if another user have the same password and same login     
211                         if ($loginexist) {
212                                 push @errors, "ERROR_login_exist";
213                                 $nok=1;
214                         } else {
215                                 $borrowernumber = &newmember(%data);
216                                 if ($data{'organisations'}){                                
217                                     # need to add the members organisations
218                                     my @orgs=split(/\|/,$data{'organisations'});
219                                     add_member_orgs($borrowerid,\@orgs);
220                                  }
221                                 logaction($loggedinuser,"MEMBERS","add member", $borrowernumber, "");
222                         }
223                 }
224
225                 unless ($nok) {
226                         if($destination eq "circ"){
227                                 print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$data{'cardnumber'}");
228                         } else {
229                                 if ($loginexist == 0) {
230                                 print $input->redirect("/cgi-bin/koha/members/moremember.pl?bornum=$borrowernumber");
231                                 }
232                         }
233                 }
234         }
235         if (C4::Context->preference("IndependantBranches")) {
236                 my $userenv = C4::Context->userenv;
237                 if ($userenv->{flags} != 1){
238                         unless ($userenv->{branch} eq $data{'branchcode'}){
239                                 push @errors, "ERROR_branch";
240                                 $nok=1;
241                         }
242                 }
243         }
244 }
245
246 if ($delete){
247         print $input->redirect("/cgi-bin/koha/deletemem.pl?member=$borrowernumber");
248 } else {  # this else goes down the whole script
249         # retrieve previous values : either in DB or in CGI, in case of errors in values
250         my $data;
251 # test to now if u add or modify a borrower (modify =>to take all carateristic of the borrowers)
252         if (!$op and !$data{'surname'}) {
253                 $data=borrdata('',$borrowernumber);
254                 %data=%$data;
255         }
256         if (C4::Context->preference("IndependantBranches")) {
257                 my $userenv = C4::Context->userenv;
258                 if ($userenv->{flags} != 1 && $data{branchcode}){
259                         unless ($userenv->{branch} eq $data{'branchcode'}){
260                                 print $input->redirect("/cgi-bin/koha/members/members-home.pl");
261                         }
262                 }
263         }
264         if ($op eq 'add'){
265                 $template->param( updtype => 'I');
266         } else {
267                 $template->param( updtype => 'M');
268         }
269         my $cardnumber=$data{'cardnumber'};
270         $cardnumber=fixup_cardnumber($data{'cardnumber'}) if $op eq 'add';
271         if ($data{'sex'} eq 'F'){
272                 $template->param(female => 1);
273         }
274         my ($categories,$labels)=ethnicitycategories();
275         my $ethnicitycategoriescount=$#{$categories};
276         my $ethcatpopup;
277         if ($ethnicitycategoriescount>=0) {
278                 $ethcatpopup = CGI::popup_menu(-name=>'ethnicity',
279                                         -id => 'ethnicity',
280                                         -values=>$categories,
281                                         -default=>$data{'ethnicity'},
282                                         -labels=>$labels);
283                 $template->param(ethcatpopup => $ethcatpopup); # bad style, has to be fixed
284         }
285         
286         
287         ($categories,$labels)=borrowercategories($category_type,$op);
288         
289         #if u modify the borrowers u must have the right value for is category code
290         
291         (my $default_category=$data{'categorycode'}) if ($op  eq '');
292         my $catcodepopup = CGI::popup_menu(-name=>'categorycode',
293                                         -id => 'categorycode',
294                                         -values=>$categories,
295                                         -default=>$default_category,
296                                         -labels=>$labels);
297         #test in city
298         my $default_city;
299         if ($op eq ''){
300         (my $selectcity=&getidcity($data{'city'})) if ($select_city eq '');
301         $default_city=$selectcity;
302         }
303         my($cityid,$name_city)=getcities();
304         $template->param( city_cgipopup => 1) if ($cityid );
305         my $citypopup = CGI::popup_menu(-name=>'select_city',
306                                         -id => 'select_city',
307                                         -values=>$cityid,
308                                         -labels=>$name_city,
309 #                                       -override => 1,
310                                         -default=>$default_city
311                                         );      
312         
313         my $default_roadtype;
314         $default_roadtype=$data{'streettype'} ;
315         my($roadtypeid,$road_type)=getroadtypes();
316         $template->param( road_cgipopup => 1) if ($roadtypeid );
317         my $roadpopup = CGI::popup_menu(-name=>'streettype',
318                                         -id => 'streettype',
319                                         -values=>$roadtypeid,
320                                         -labels=>$road_type,
321                                         -override => 1,
322                                         -default=>$default_roadtype
323                                         );      
324
325         
326         my @relationships = split /,|\|/,C4::Context->preference('BorrowerRelationship');
327         my @relshipdata;
328         while (@relationships) {
329                 my $relship = shift @relationships || '';
330                 my %row = ('relationship' => $relship);
331                 if ($data{'relationship'} eq $relship) {
332                         $row{'selected'}=' selected';
333                 } else {
334                         $row{'selected'}='';
335                 }
336                 push(@relshipdata, \%row);
337         }
338         my %flags = ( 'gonenoaddress' => ['gonenoaddress', 'Adresse érronée'],
339                       'lost'          => ['lost', 'Carte Perdue'],
340                       'debarred'      => ['debarred', 'Lecteur exclu']);
341
342         my @flagdata;
343         foreach (keys(%flags)) {
344         my $key = $_;
345         my %row =  ('key'   => $key,
346                         'name'  => $flags{$key}[0],
347                         'html'  => $flags{$key}[1]);
348         if ($data{$key}) {
349                 $row{'yes'}=' checked';
350                 $row{'no'}='';
351         } else {
352                 $row{'yes'}='';
353                 $row{'no'}=' checked';
354         }
355         push(@flagdata, \%row);
356         }
357
358         if ($modify){
359         $template->param( modify => 1 );
360         }
361
362         #Convert dateofbirth to correct format
363         $data{'dateofbirth'} = format_date($data{'dateofbirth'});
364         my @branches;
365         my @select_branch;
366         my %select_branches;
367         my $branches=getbranches();
368         my $default;
369         # -----------------------------------------------------
370         #  the value of ip from the branches hash table
371                 my $select_ip;
372         # $ip is the ip of user when is connect to koha 
373                 my $ip = $ENV{'REMOTE_ADDR'};
374         # -----------------------------------------------------
375         foreach my $branch (keys %$branches) {
376                 if ((not C4::Context->preference("IndependantBranches")) || (C4::Context->userenv->{'flags'} == 1)) {
377                         push @select_branch, $branch;
378                         $select_branches{$branch} = $branches->{$branch}->{'branchname'};
379 #                take the ip number from branches "op"
380                         $select_ip = $branches->{$branch}->{'branchip'} || '';
381                                 
382 #               test $select_ip equal $ip to attribute the default value for the scrolling list
383                         if ($select_ip eq $ip)  {
384                                                 $default = $branches->{$branch}->{'branchcode'};
385                                                 }
386                         } else {
387                                 push @select_branch, $branch if ($branch eq C4::Context->userenv->{'branch'});
388                                 $select_branches{$branch} = $branches->{$branch}->{'branchname'} if ($branch eq C4::Context->userenv->{'branch'});
389                                         
390                                 $default = C4::Context->userenv->{'branch'};
391                                         
392                                 }
393         }
394 # --------------------------------------------------------------------------------------------------------
395         my $CGIbranch = CGI::scrolling_list(-id    => 'branchcode',
396                                            -name   => 'branchcode',
397                                            -values => \@select_branch,
398                                            -labels => \%select_branches,
399                                            -size   => 1,
400                                            -multiple =>0,
401                                            -override => 1,      
402                                            -default => $default,
403                                         );
404        my $CGIorganisations;
405        my $member_of_institution;
406        if (C4::Context->preference("memberofinstitution")){
407            my $organisations=get_institutions();
408            my @orgs;
409            my %org_labels;
410            foreach my $organisation (keys %$organisations) {
411                push @orgs,$organisation;
412                $org_labels{$organisation}=$organisations->{$organisation}->{'surname'};
413            }
414            $member_of_institution=1;
415            
416            $CGIorganisations = CGI::scrolling_list( -id => 'organisations',
417                -name     => 'organisations',
418                -labels   => \%org_labels,
419                -values   => \@orgs,
420                -size     => 5,
421                -multiple => 'true'
422
423                
424            );
425        }
426
427
428 # --------------------------------------------------------------------------------------------------------
429         
430         my $CGIsort1 = buildCGIsort("Bsort1","sort1",$data{'sort1'});
431         if ($CGIsort1) {
432                 $template->param(CGIsort1 => $CGIsort1);
433                 $template->param( sort1 => $data{'sort1'});
434         } else {
435                 $template->param( sort1 => $data{'sort1'});
436         }
437         
438         my $CGIsort2 = buildCGIsort("Bsort2","sort2",$data{'sort2'});
439         if ($CGIsort2) {
440                 $template->param(CGIsort2 =>$CGIsort2);
441         } else {
442                 $template->param( sort2 => $data{'sort2'});
443         }
444
445         
446         $data{'opacnotes'} =~ s/\\//g;
447         $data{'borrowernotes'} =~ s/\\//g;
448
449         # increase step to see next page
450         if ($nok) {
451             foreach my $error (@errors) {
452                 $template->param( $error => 1);
453             }
454             $template->param(nok => 1);
455         }
456         else {
457             $step++;
458         }
459
460
461         $template->param(
462                 BorrowerMandatoryField => C4::Context->preference("BorrowerMandatoryField"),#field to test with javascript
463                 category_type   => $category_type,#to know the category type of the borrower
464                 select_city     => $select_city,
465                 "step_$step"    => 1,# associate with step to know where u are
466                 step            => $step,
467                 destination     => $destination,#to know wher u come from and wher u must go in redirect
468                 check_member    => $check_member,#to know if the borrower already exist(=>1) or not (=>0) 
469 #                               flags           =>$data{'flags'},               
470                 "op$op"         => 1,
471 #               op                      => $op,
472                 nodouble        => $nodouble,
473                 borrowernumber  => $borrowernumber,#register number
474                 cardnumber      => $data{'cardnumber'},
475                 surname         => uc($data{'surname'}),
476                 firstname       => ucfirst($data{'firstname'}),
477                 "title_".$data{'title'}   => " SELECTED ",
478                 title           => $data{'title'},
479                 othernames      => $data{'othernames'},
480                 initials        => $data{'initials'},
481                 streetnumber    => $data{'streetnumber'},
482                 streettype      =>$data{'streettype'},
483                 address          => $data{'address'},
484                 address2        => $data{'address2'},   
485                 city            => $data{'city'},
486                 zipcode         => $data{'zipcode'},
487                 email           => $data{'email'},
488                 phone           => $data{'phone'},
489                 mobile          => $data{'mobile'},
490                 fax             => $data{'fax'},
491                 phonepro        => $data{'phonepro'},
492                 emailpro        => $data{'emailpro'},
493                 b_address       => $data{'b_address'},
494                 b_city          => $data{'b_city'},
495                 b_zipcode       => $data{'b_zipcode'},
496                 b_email         => $data{'b_email'},
497                 b_phone        => $data{'b_phone'},
498                 dateofbirth     => $data{'dateofbirth'},
499                 branchcode      => $data{'branchcode'},
500                 catcodepopup    => $catcodepopup,
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($data{'contactfirstname'}),
509                 "contacttitle_".$data{'contacttitle'} => "SELECTED" ,
510                 contacttitle    => $data{'contacttitle'},
511                 guarantorid     => $guarantorid,
512                 ethcatpopup     => $ethcatpopup,
513                 sex             => $data{'sex'},
514                 userid          => $data{'userid'},     
515                 password        => $data{'password'},   
516                 opacnote        => $data{'opacnote'},   
517                 contactnotes    => $data{'contactnotes'},
518                 borrowernotes   => $data{'borrowernotes'},
519                 relshiploop     => \@relshipdata,
520                 relationship    => $data{'relationship'},
521                 citypopup       => $citypopup,
522                 roadpopup       => $roadpopup,  
523                 contacttype     => $data{'contacttype'},
524                 organisations   => $data{'organisations'},
525                 flagloop        => \@flagdata,
526 #               "contacttype_".$data{'contacttype'} =>" SELECTED ",
527                 dateformat      => display_date_format(),
528                 check_categorytype =>$check_categorytype,#to recover the category type with checkcategorytype function
529                 modify          => $modify,
530 #               city_choice       => $city_choice ,#check if the city was selected
531                 nok             => $nok,#flag to konw if an error 
532                 CGIbranch => $CGIbranch,
533                 memberofinstution => $member_of_institution,
534                 CGIorganisations => $CGIorganisations,
535                 );
536         #$template->param(Institution => 1) if ($categorycode eq "I");
537         output_html_with_http_headers $input, $cookie, $template->output;
538 }
539
540 # Local Variables:
541 # tab-width: 8
542 # End: