fixing horrible terrible merge problem by reverting
[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 Digest::MD5 qw(md5_base64);
28
29 # internal modules
30 use C4::Auth;
31 use C4::Context;
32 use C4::Output;
33 use C4::Members;
34 use C4::Koha;
35 use C4::Date;
36 use C4::Input;
37 use C4::Log;
38 use C4::Branch; # GetBranches
39
40 my $input = new CGI;
41 my %data;
42
43 my $dbh = C4::Context->dbh;
44
45
46
47 my ($template, $loggedinuser, $cookie)
48     = get_template_and_user({template_name => "members/memberentrygen.tmpl",
49            query => $input,
50            type => "intranet",
51            authnotrequired => 0,
52            flagsrequired => {borrowers => 1},
53            debug => 1,
54            });
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;
70 my @errors;
71 my $default_city;
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
75 my $borrower_data;
76
77
78 $template->param("uppercasesurnames" => C4::Context->preference('uppercasesurnames'));
79
80 #function  to automatic setup the mandatory  fields (visual with css)
81 my $check_BorrowerMandatoryField=C4::Context->preference("BorrowerMandatoryField");
82 my @field_check=split(/\|/,$check_BorrowerMandatoryField);
83 foreach (@field_check) {
84 $template->param( "mandatory$_" => 1);    
85 }
86 $template->param("add"=>1) if ($op eq 'add');
87 $template->param("checked" => 1) if ($nodouble eq 1);
88 my $categorycode=$input->param('categorycode');
89 ($borrower_data=GetMember($borrowernumber,'borrowernumber')) if ($op eq 'modify' or $op eq 'save');
90 $categorycode=$borrower_data->{'categorycode'} unless $categorycode;
91 my $category_type;
92 $category_type = $input->param('category_type');
93 unless ($category_type or !($categorycode)){
94   my $borrowercategory= GetBorrowercategory($categorycode);
95   $category_type = $borrowercategory->{'category_type'};
96 }
97 $category_type="A" unless $category_type; # FIXME we should display a error message instead of a 500 error !
98
99 # if a add or modify is requested => check validity of data.
100 %data= %$borrower_data if ($borrower_data);
101
102 my %newdata;
103 if ($op eq 'insert' || $op eq 'modify' || $op eq 'save') {
104     my @names= ($borrower_data && $op ne 'save') ? keys %$borrower_data : $input->param();
105     foreach my $key (@names) {
106         $newdata{$key} = $input->param($key) if ($input->param($key));
107         $newdata{$key} =~ s/\"/"/gg unless ($key eq 'borrowernotes' or $key eq 'opacnote');
108     }
109     $newdata{'dateenrolled'}=format_date_in_iso($newdata{'dateenrolled'}) if ($newdata{dateenrolled});  
110     $newdata{'dateexpiry'}=format_date_in_iso($newdata{'dateexpiry'}) if ($newdata{dateexpiry});  
111     $newdata{'dateofbirth'}=format_date_in_iso($newdata{'dateofbirth'}) if ($newdata{dateofbirth});  
112 }
113
114 #############test for member being unique #############
115 if ($op eq 'insert'){
116         my $category_type_send=$category_type if ($category_type eq 'I'); 
117         my $check_category; # recover the category code of the doublon suspect borrowers
118         ($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'}));
119           
120   #   recover the category type if the borrowers is a doublon 
121         my $tmpborrowercategory=GetBorrowercategory($check_category);
122         $check_categorytype=$tmpborrowercategory->{'category_type'};
123 }
124
125   #recover all data from guarantor address phone ,fax... 
126 if (($category_type eq 'C' || $category_type eq 'P') and $guarantorid ne '' ){
127   my $guarantordata=GetMember($guarantorid);
128   $guarantorinfo=$guarantordata->{'surname'}." , ".$guarantordata->{'firstname'};
129   if (($data{'contactname'} eq '' or $data{'contactname'} ne $guarantordata->{'surname'})) {
130     $data{'contactfirstname'}=$guarantordata->{'firstname'}; 
131     $data{'contactname'}=$guarantordata->{'surname'};
132     $data{'contacttitle'}=$guarantordata->{'title'};  
133     map {$data{$_}=$guarantordata->{$_}}('streetnumber','address','streettype','address2','zipcode','city','phone','phonepro','mobile','fax','email','emailpro');
134   }
135 }
136
137 ###############test to take the right zipcode and city name ##############
138 if ( $guarantorid eq ''){
139   if ($select_city){
140     my ($borrower_city,$borrower_zipcode)=&getzipnamecity($select_city);
141     $newdata{'city'}= $borrower_city;
142     $newdata{'zipcode'}=$borrower_zipcode;
143     }
144 }
145 #builds default userid
146 if ( (defined $newdata{'userid'}) && ($newdata{'userid'} eq '')){
147   my $onefirstnameletter=substr($data{'firstname'},0,1);
148   my $fivesurnameletter=substr($data{'surname'},0,9);
149   $newdata{'userid'}=lc($onefirstnameletter.$fivesurnameletter);
150 }
151   
152 my $loginexist=0;
153 if ($op eq 'save' || $op eq 'insert'){
154   if (checkcardnumber($newdata{cardnumber},$newdata{borrowernumber})){ 
155     push @errors, 'ERROR_cardnumber';
156     $nok = 1;
157   } 
158   my $dateofbirthmandatory=0;
159   map {$dateofbirthmandatory=1 if $_ eq "dateofbirth"} @field_check;
160   if ($newdata{dateofbirth} && $dateofbirthmandatory) {
161     my $age = GetAge($newdata{dateofbirth});
162     my $borrowercategory=GetBorrowercategory($newdata{'categorycode'});   
163     if (($age > $borrowercategory->{'upperagelimit'}) or ($age < $borrowercategory->{'dateofbirthrequired'})) {
164       push @errors, 'ERROR_age_limitations';
165       $nok = 1;
166     }
167   }
168   
169   if (C4::Context->preference("IndependantBranches")) {
170     my $userenv = C4::Context->userenv;
171     if ($userenv && $userenv->{flags} != 1){
172       warn "  $newdata{'branchcode'} : ".$userenv->{flags}.":".$userenv->{branch};
173       unless (!$newdata{'branchcode'} || $userenv->{branch} eq $newdata{'branchcode'}){
174         push @errors, "ERROR_branch";
175         $nok=1;
176       }
177     }
178   }
179   # Check if the userid is unique
180   if (($op eq 'insert' || $op eq 'save') && !Check_Userid($newdata{'userid'},$borrowernumber)) {
181     push @errors, "ERROR_login_exist";
182     $nok=1;
183     $loginexist=1; 
184   }
185 }
186
187 if ($op eq 'modify' || $op eq 'insert'){
188   unless ($newdata{'dateexpiry'}){
189     if ($newdata{'dateenrolled'}){
190       $newdata{'dateexpiry'} = GetExpiryDate($newdata{'categorycode'},$newdata{'dateenrolled'});
191     } else {  
192       my $today= sprintf('%04d-%02d-%02d', Today());
193       $newdata{'dateexpiry'} = GetExpiryDate($newdata{'categorycode'},$today) ;
194     }  
195   }
196 }
197
198                               
199
200 if ($op eq 'insert'){
201   # Check if the userid is unique
202   unless ($nok){
203     $borrowernumber = &AddMember(%newdata);
204     if ($data{'organisations'}){            
205       # need to add the members organisations
206       my @orgs=split(/\|/,$data{'organisations'});
207       add_member_orgs($borrowernumber,\@orgs);
208     }
209     if($destination eq "circ"){
210       print $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$data{'cardnumber'}");
211     } else {
212       if ($loginexist == 0) {
213       print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$borrowernumber");
214       }
215     }
216   }
217 }
218 if ($op eq 'save'){
219         # test to know if another user have the same password and same login                                
220         unless ($nok){
221                 &ModMember(%newdata);    
222                 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$borrowernumber");
223         }
224 }
225
226 if ($delete){
227         print $input->redirect("/cgi-bin/koha/deletemem.pl?member=$borrowernumber");
228 }
229
230 if ($nok){
231   $op="add" if ($op eq "insert");
232   $op="modify" if ($op eq "save");
233   %data=%newdata; 
234   $template->param( updtype => ($op eq "insert"?'I':'M'));
235   unless ($step){  
236     $template->param( step_1 => 1,step_2 => 1,step_3 => 1);
237   }  
238
239 if (C4::Context->preference("IndependantBranches")) {
240   my $userenv = C4::Context->userenv;
241   if ($userenv->{flags} != 1 && $data{branchcode}){
242     unless ($userenv->{branch} eq $data{'branchcode'}){
243       print $input->redirect("/cgi-bin/koha/members/members-home.pl");
244     }
245   }
246 }
247 if ($op eq 'add'){
248   $template->param( updtype => 'I',step_1=>1,step_2=>1,step_3=>1);
249
250 if ($op eq "modify")  {
251   $template->param( updtype => 'M',modify => 1 );
252   $template->param( step_1=>1,step_2=>1,step_3=>1) unless $step;
253 }
254 # my $cardnumber=$data{'cardnumber'};
255 $data{'cardnumber'}=fixup_cardnumber($data{'cardnumber'}) if $op eq 'add';
256 if ($data{'sex'} eq 'F'){
257   $template->param(female => 1);
258 } elsif ($data{'sex'} eq 'M'){
259     $template->param(male => 1);
260 } else {
261     $template->param(none => 1);
262 }
263
264 ##Now all the data to modify a member.
265 my ($categories,$labels)=ethnicitycategories();
266   
267 my $ethnicitycategoriescount=$#{$categories};
268 my $ethcatpopup;
269 if ($ethnicitycategoriescount>=0) {
270   $ethcatpopup = CGI::popup_menu(-name=>'ethnicity',
271         -id => 'ethnicity',
272         -tabindex=>'',
273         -values=>$categories,
274         -default=>$data{'ethnicity'},
275         -labels=>$labels);
276   $template->param(ethcatpopup => $ethcatpopup); # bad style, has to be fixed
277 }
278   
279   
280 my $action="WHERE category_type=?";
281 ($categories,$labels)=GetborCatFromCatType($category_type,$action);
282   
283 if(scalar(@$categories)){
284       #if you modify the borrowers you must have the right value for his category code
285   my $default_category=$newdata{'categorycode'} if ($op  eq 'modify');
286   my $catcodepopup = CGI::popup_menu(
287       -name=>'categorycode',
288   -id => 'categorycode',
289   -values=>$categories,
290     -labels=>$labels,
291   -default=>$default_category
292   );
293   $template->param(catcodepopup=>$catcodepopup);
294 }
295   #test in city
296 $select_city=getidcity($data{'city'}) if ($guarantorid ne '0');
297 ($default_city=$select_city) if ($step eq 0);
298 if ($select_city eq '' ){
299 my $selectcity=&getidcity($data{'city'});
300 $default_city=$selectcity;
301 }
302 my($cityid,$name_city)=GetCities();
303 $template->param( city_cgipopup => 1) if ($cityid );
304 my $citypopup = CGI::popup_menu(-name=>'select_city',
305         -id => 'select_city',
306         -values=>$cityid,
307         -labels=>$name_city,
308         -default=>$default_city
309         );  
310   
311 my $default_roadtype;
312 $default_roadtype=$data{'streettype'} ;
313 my($roadtypeid,$road_type)=GetRoadTypes();
314   $template->param( road_cgipopup => 1) if ($roadtypeid );
315 my $roadpopup = CGI::popup_menu(-name=>'streettype',
316         -id => 'streettype',
317         -values=>$roadtypeid,
318           -labels=>$road_type,
319           -override => 1,
320         -default=>$default_roadtype
321         );  
322
323 my $default_borrowertitle;
324 $default_borrowertitle=$data{'title'} ;
325 my($borrowertitle)=GetTitles();
326 my $borrotitlepopup = CGI::popup_menu(-name=>'title',
327               -id => 'btitle',
328               -values=>$borrowertitle,
329               -override => 1,
330               -default=>$default_borrowertitle
331         );    
332
333
334 my @relationships = split /,|\|/,C4::Context->preference('BorrowerRelationship');
335 my @relshipdata;
336 while (@relationships) {
337   my $relship = shift @relationships || '';
338   my %row = ('relationship' => $relship);
339   if ($data{'relationship'} eq $relship) {
340     $row{'selected'}=' selected';
341   } else {
342     $row{'selected'}='';
343   }
344   push(@relshipdata, \%row);
345 }
346 my %flags = ( 'gonenoaddress' => ['gonenoaddress', 'Gone no address '],
347         'lost'          => ['lost', 'Lost'],
348         'debarred'      => ['debarred', 'Debarred']);
349
350 my @flagdata;
351 foreach (keys(%flags)) {
352 my $key = $_;
353 my %row =  ('key'   => $key,
354     'name'  => $flags{$key}[0],
355     'html'  => $flags{$key}[1]);
356 if ($data{$key}) {
357   $row{'yes'}=' checked';
358   $row{'no'}='';
359 } else {
360   $row{'yes'}='';
361   $row{'no'}=' checked';
362 }
363 push(@flagdata, \%row);
364 }
365
366
367 #get Branches
368 my @branches;
369 my @select_branch;
370 my %select_branches;
371
372 my $onlymine=(C4::Context->preference('IndependantBranches') && 
373               C4::Context->userenv && 
374               C4::Context->userenv->{flags} !=1  && 
375               C4::Context->userenv->{branch}?1:0);
376               
377 my $branches=GetBranches($onlymine);
378 my $default;
379
380 foreach my $branch (sort keys %$branches) {
381     push @select_branch,$branch;
382     $select_branches{$branch} = $branches->{$branch}->{'branchname'};
383     $default = C4::Context->userenv->{'branch'} if (C4::Context->userenv && C4::Context->userenv->{'branch'});
384 }
385 # --------------------------------------------------------------------------------------------------------
386   #in modify mod :default value from $CGIbranch comes from borrowers table
387   #in add mod: default value come from branches table (ip correspendence)
388 $default=$data{'branchcode'}  if ($op eq 'modify');
389 my $CGIbranch = CGI::scrolling_list(-id    => 'branchcode',
390             -name   => 'branchcode',
391             -values => \@select_branch,
392             -labels => \%select_branches,
393             -size   => 1,
394             -override => 1,  
395             -multiple =>0,
396             -default => $default,
397         );
398 my $CGIorganisations;
399 my $member_of_institution;
400 if (C4::Context->preference("memberofinstitution")){
401     my $organisations=get_institutions();
402     my @orgs;
403     my %org_labels;
404     foreach my $organisation (keys %$organisations) {
405         push @orgs,$organisation;
406         $org_labels{$organisation}=$organisations->{$organisation}->{'surname'};
407     }
408     $member_of_institution=1;
409     
410     $CGIorganisations = CGI::scrolling_list( -id => 'organisations',
411         -name     => 'organisations',
412         -labels   => \%org_labels,
413         -values   => \@orgs,
414         -size     => 5,
415         -multiple => 'true'
416
417         
418     );
419 }
420
421
422 # --------------------------------------------------------------------------------------------------------
423
424 my $CGIsort1 = buildCGIsort("Bsort1","sort1",$data{'sort1'});
425 if ($CGIsort1) {
426   $template->param(CGIsort1 => $CGIsort1);
427   $template->param( sort1 => $data{'sort1'});
428 } else {
429   $template->param( sort1 => $data{'sort1'});
430 }
431
432 my $CGIsort2 = buildCGIsort("Bsort2","sort2",$data{'sort2'});
433 if ($CGIsort2) {
434   $template->param(CGIsort2 =>$CGIsort2);
435 } else {
436   $template->param( sort2 => $data{'sort2'});
437 }
438 if ($nok) {
439     foreach my $error (@errors) {
440         $template->param( $error => 1);
441     }
442     $template->param(nok => 1);
443 }
444   
445   #Formatting data for display    
446   
447 if ($data{'dateenrolled'} eq ''){
448   my $today= sprintf('%04d-%02d-%02d', Today());
449   $data{'dateenrolled'}=$today;
450 }
451
452 $data{'surname'}=uc($data{'surname'});
453 $data{'firstname'}=ucfirst(lc $data{'firstname'});
454 $data{'dateenrolled'}=format_date($data{'dateenrolled'});
455 $data{'dateexpiry'}=format_date($data{'dateexpiry'});
456 $data{'contactname'}=uc($data{'contactname'});
457 $data{'contactfirstname'}= ucfirst( lc $data{'contactfirstname'});
458 $data{'dateofbirth'} = format_date($data{'dateofbirth'});
459
460 $template->param( "showguarantor"  => 1) if ($category_type!~/A|I/);# associate with step to know where u are
461 $template->param( "showguarantor"  => 0) if ($category_type=~/A|I/);# associate with step to know where u are
462   warn "$step";
463 $template->param(%data);
464 $template->param( "step_$step"  => 1) if $step;# associate with step to know where u are
465 $template->param( "step"  => $step) if $step;# associate with step to know where u are
466 $template->param(
467   BorrowerMandatoryField => C4::Context->preference("BorrowerMandatoryField"),#field to test with javascript
468   category_type => $category_type,#to know the category type of the borrower
469   DHTMLcalendar_dateformat => get_date_format_string_for_DHTMLcalendar(),
470   select_city => $select_city,
471   "$category_type"  => 1,# associate with step to know where u are
472   destination   => $destination,#to know wher u come from and wher u must go in redirect
473   check_member    => $check_member,#to know if the borrower already exist(=>1) or not (=>0) 
474   flags   =>$data{'flags'},   
475   "op$op"   => 1,
476   nodouble  => $nodouble,
477   borrowernumber  => $borrowernumber,#register number
478   "contacttitle_".$data{'contacttitle'} => "SELECTED" ,
479   guarantorid => $guarantorid,
480   ethcatpopup => $ethcatpopup,
481   relshiploop => \@relshipdata,
482   citypopup => $citypopup,
483   roadpopup => $roadpopup,  
484   borrotitlepopup => $borrotitlepopup,
485   guarantorinfo   => $guarantorinfo,
486   flagloop  => \@flagdata,
487   dateformat      => display_date_format(),
488   check_categorytype =>$check_categorytype,#to recover the category type with checkcategorytype function
489   modify          => $modify,
490   nok     => $nok,#flag to konw if an error 
491   CGIbranch => $CGIbranch,
492   memberofinstution => $member_of_institution,
493   CGIorganisations => $CGIorganisations,
494   
495   );
496 output_html_with_http_headers $input, $cookie, $template->output;
497
498 # Local Variables:
499 # tab-width: 8
500 # End: