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