Bug Fixing 1782 patch
[koha.git] / members / memberentry.pl
index 8c69311..3ee30c6 100755 (executable)
@@ -21,7 +21,6 @@
 use strict;
 
 # external modules
-use Date::Calc qw/Today/;
 use CGI;
 # use Digest::MD5 qw(md5_base64);
 
@@ -30,12 +29,16 @@ use C4::Auth;
 use C4::Context;
 use C4::Output;
 use C4::Members;
+use C4::Members::Attributes;
+use C4::Members::AttributeTypes;
 use C4::Koha;
 use C4::Dates qw/format_date format_date_in_iso/;
 use C4::Input;
 use C4::Log;
 use C4::Branch; # GetBranches
 
+#use Smart::Comments;
+
 use vars qw($debug);
 
 BEGIN {
@@ -68,6 +71,8 @@ my $check_member=$input->param('check_member');
 my $name_city=$input->param('name_city');
 my $nodouble=$input->param('nodouble');
 my $select_city=$input->param('select_city');
+my $zipcode=$input->param('zipcode');
+my $city=$input->param('city');
 my $nok=$input->param('nok');
 my $guarantorinfo=$input->param('guarantorinfo');
 my $step=$input->param('step') || 0;
@@ -93,9 +98,12 @@ $template->param("checked" => 1) if ($nodouble eq 1);
 ($borrower_data = GetMember($borrowernumber,'borrowernumber')) if ($op eq 'modify' or $op eq 'save');
 my $categorycode = $input->param('categorycode') || $borrower_data->{'categorycode'};
 my $category_type = $input->param('category_type');
+my $new_c_type = $category_type; #if we have input param, then we've already chosen the cat_type.
 unless ($category_type or !($categorycode)){
   my $borrowercategory= GetBorrowercategory($categorycode);
   $category_type = $borrowercategory->{'category_type'};
+  my $category_name = $borrowercategory->{'description'}; 
+  $template->param("categoryname"=>$category_name);
 }
 $category_type="A" unless $category_type; # FIXME we should display a error message instead of a 500 error !
 
@@ -143,8 +151,10 @@ if ($op eq 'insert'){
                );
           
   #   recover the category type if the borrowers is a doublon 
-        my $tmpborrowercategory=GetBorrowercategory($check_category);
-        $check_categorytype=$tmpborrowercategory->{'category_type'};
+    if ($check_category) {
+      my $tmpborrowercategory=GetBorrowercategory($check_category);
+      $check_categorytype=$tmpborrowercategory->{'category_type'};
+    }   
 }
 
   #recover all data from guarantor address phone ,fax... 
@@ -163,12 +173,10 @@ if (($category_type eq 'C' || $category_type eq 'P') and $guarantorid ne '' ){
 
 ###############test to take the right zipcode and city name ##############
 if ( $guarantorid eq ''){
-  if ($select_city){
-    my ($borrower_city,$borrower_zipcode)=&getzipnamecity($select_city);
-    $newdata{'city'}= $borrower_city;
-    $newdata{'zipcode'}=$borrower_zipcode;
-    }
+    $newdata{'city'}= $city;
+    $newdata{'zipcode'}=$zipcode; 
 }
+
 #builds default userid
 if ( (defined $newdata{'userid'}) && ($newdata{'userid'} eq '')){
   my $onefirstnameletter = substr($data{'firstname'},0,1);
@@ -178,6 +186,7 @@ if ( (defined $newdata{'userid'}) && ($newdata{'userid'} eq '')){
   
 $debug and warn join "\t", map {"$_: $newdata{$_}"} qw(dateofbirth dateenrolled dateexpiry);
 my $loginexist=0;
+my $extended_patron_attributes = ();
 if ($op eq 'save' || $op eq 'insert'){
   if (checkcardnumber($newdata{cardnumber},$newdata{borrowernumber})){ 
     push @errors, 'ERROR_cardnumber';
@@ -187,7 +196,7 @@ if ($op eq 'save' || $op eq 'insert'){
     my $age = GetAge($newdata{dateofbirth});
     my $borrowercategory=GetBorrowercategory($newdata{'categorycode'});   
        my ($low,$high) = ($borrowercategory->{'dateofbirthrequired'}, $borrowercategory->{'upperagelimit'});
-    if (($age > $high) or ($age < $low)) {
+    if (($high && ($age > $high)) or ($age < $low)) {
       push @errors, 'ERROR_age_limitations';
          $template->param('ERROR_age_limitations' => "$low to $high");
     }
@@ -205,11 +214,21 @@ if ($op eq 'save' || $op eq 'insert'){
     push @errors, "ERROR_login_exist";
     $loginexist=1; 
   }
+
+  if (C4::Context->preference('ExtendedPatronAttributes')) {
+    $extended_patron_attributes = parse_extended_patron_attributes($input);
+    foreach my $attr (@$extended_patron_attributes) {
+        unless (C4::Members::Attributes::CheckUniqueness($attr->{code}, $attr->{value}, $borrowernumber)) {
+            push @errors, "ERROR_extended_unique_id_failed";
+            $template->param(ERROR_extended_unique_id_failed => "$attr->{code}/$attr->{value}");
+        }
+    }
+  }
 }
 
 if ($op eq 'modify' || $op eq 'insert'){
   unless ($newdata{'dateexpiry'}){
-       my $arg2 = $newdata{'dateenrolled'} || sprintf('%04d-%02d-%02d', Today());
+       my $arg2 = $newdata{'dateenrolled'} || C4::Dates->today('iso');
     $newdata{'dateexpiry'} = GetExpiryDate($newdata{'categorycode'},$arg2);
   }
 }
@@ -227,15 +246,21 @@ if ((!$nok) and ($op eq 'insert' or $op eq 'save')){
                        my @orgs=split(/\|/,$data{'organisations'});
                        add_member_orgs($borrowernumber,\@orgs);
                }
+        if (C4::Context->preference('ExtendedPatronAttributes') and $input->param('setting_extended_patron_attributes')) {
+            C4::Members::Attributes::SetBorrowerAttributes($borrowernumber, $extended_patron_attributes);
+        }
        } elsif ($op eq 'save'){ 
                if ($NoUpdateLogin) {
                        delete $newdata{'password'};
                        delete $newdata{'userid'};
                }
-               &ModMember(%newdata);   # this is the last server-changing line.  the rest is "presentation"
+               &ModMember(%newdata);    
+        if (C4::Context->preference('ExtendedPatronAttributes') and $input->param('setting_extended_patron_attributes')) {
+            C4::Members::Attributes::SetBorrowerAttributes($borrowernumber, $extended_patron_attributes);
+        }
        }
        print scalar ($destination eq "circ") ? 
-               $input->redirect("/cgi-bin/koha/circ/circulation.pl?findborrower=$data{'cardnumber'}") :
+               $input->redirect("/cgi-bin/koha/circ/circulation.pl?borrowernumber=$borrowernumber") :
                $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=$borrowernumber") ;
        exit;           # You can only send 1 redirect!  After that, content or other headers don't matter.
 }
@@ -251,7 +276,7 @@ if ($nok){
   %data=%newdata; 
   $template->param( updtype => ($op eq 'add' ?'I':'M'));       # used to check for $op eq "insert"... but we just changed $op!
   unless ($step){  
-    $template->param( step_1 => 1,step_2 => 1,step_3 => 1);
+    $template->param( step_1 => 1,step_2 => 1,step_3 => 1, step_4 => 1);
   }  
 } 
 if (C4::Context->preference("IndependantBranches")) {
@@ -264,11 +289,14 @@ if (C4::Context->preference("IndependantBranches")) {
   }
 }
 if ($op eq 'add'){
-  $template->param( updtype => 'I',step_1=>1,step_2=>1,step_3=>1);
+       my $arg2 = $newdata{'dateenrolled'} || C4::Dates->today('iso');
+       $data{'dateexpiry'} = GetExpiryDate($newdata{'categorycode'},$arg2);
+       $template->param( updtype => 'I',step_1=>1,step_2=>1,step_3=>1, step_4 => 1);
+       
 } 
 if ($op eq "modify")  {
   $template->param( updtype => 'M',modify => 1 );
-  $template->param( step_1=>1,step_2=>1,step_3=>1) unless $step;
+  $template->param( step_1=>1,step_2=>1,step_3=>1, step_4 => 1) unless $step;
 }
 # my $cardnumber=$data{'cardnumber'};
 $data{'cardnumber'}=fixup_cardnumber($data{'cardnumber'}) if $op eq 'add';
@@ -296,14 +324,14 @@ if ($ethnicitycategoriescount>=0) {
 }
 
 my @typeloop;
-foreach (qw(C A S P I)){
+foreach ( ($category_type) ? ($category_type) : qw(C A S P I X)){
        my $action="WHERE category_type=?";
        ($categories,$labels)=GetborCatFromCatType($_,$action);
        my @categoryloop;
        foreach my $cat (@$categories){
                push @categoryloop,{'categorycode' => $cat,
                          'categoryname' => $labels->{$cat},
-                         'categorycodeselected' => ($cat eq $borrower_data->{'categorycode'}),
+                         'categorycodeselected' => ($cat eq $borrower_data->{'categorycode'} || $cat eq $categorycode),
                };
        }
        my %typehash;
@@ -466,8 +494,7 @@ if ($nok) {
   #Formatting data for display    
   
 if ($data{'dateenrolled'} eq ''){
-  my $today = sprintf('%04d-%02d-%02d', Today());      # ISO format
-  $data{'dateenrolled'}=$today;
+  $data{'dateenrolled'}=C4::Dates->today('iso');
 }
 if (C4::Context->preference('uppercasesurnames')) {
        $data{'surname'}    =uc($data{'surname'}    );
@@ -478,7 +505,12 @@ foreach (qw(dateenrolled dateexpiry dateofbirth)) {
        $template->param( $_ => $data{$_});
 }
 
-$template->param( "showguarantor"  => ($category_type=~/A|I|S/) ? 0 : 1); # associate with step to know where you are
+if (C4::Context->preference('ExtendedPatronAttributes')) {
+    $template->param(ExtendedPatronAttributes => 1);
+    patron_attributes_form($template, $borrowernumber);
+}
+
+$template->param( "showguarantor"  => ($category_type=~/A|I|S|X/) ? 0 : 1); # associate with step to know where you are
 $debug and warn "memberentry step: $step";
 $template->param(%data);
 $template->param( "step_$step"  => 1) if $step;        # associate with step to know where u are
@@ -518,6 +550,83 @@ $template->param(
   
 output_html_with_http_headers $input, $cookie, $template->output;
 
+sub  parse_extended_patron_attributes {
+    my ($input) = @_;
+    my @patron_attr = grep { /^patron_attr_\d+$/ } $input->param();
+
+    my @attr = ();
+    my %dups = ();
+    foreach my $key (@patron_attr) {
+        my $value = $input->param($key);
+        next unless defined($value) and $value ne '';
+        my $password = $input->param("${key}_password");
+        my $code = $input->param("${key}_code");
+        next if exists $dups{$code}->{$value};
+        $dups{$code}->{$value} = 1;
+        push @attr, { code => $code, value => $value, password => $password };
+    }
+    return \@attr;
+}
+
+sub patron_attributes_form {
+    my $template = shift;
+    my $borrowernumber = shift;
+
+    my @types = C4::Members::AttributeTypes::GetAttributeTypes();
+    if (scalar(@types) == 0) {
+        $template->param(no_patron_attribute_types => 1);
+        return;
+    }
+    my $attributes = C4::Members::Attributes::GetBorrowerAttributes($borrowernumber);
+
+    # map patron's attributes into a more convenient structure
+    my %attr_hash = ();
+    foreach my $attr (@$attributes) {
+        push @{ $attr_hash{$attr->{code}} }, $attr;
+    }
+
+    my @attribute_loop = ();
+    my $i = 0;
+    foreach my $type_code (map { $_->{code} } @types) {
+        my $attr_type = C4::Members::AttributeTypes->fetch($type_code);
+        my $entry = {
+            code              => $attr_type->code(),
+            description       => $attr_type->description(),
+            repeatable        => $attr_type->repeatable(),
+            password_allowed  => $attr_type->password_allowed(),
+            category          => $attr_type->authorised_value_category(),
+            password          => '',
+        };
+        if (exists $attr_hash{$attr_type->code()}) {
+            foreach my $attr (@{ $attr_hash{$attr_type->code()} }) {
+                my $newentry = { map { $_ => $entry->{$_} } %$entry };
+                $newentry->{value} = $attr->{value};
+                $newentry->{password} = $attr->{password};
+                $newentry->{use_dropdown} = 0;
+                if ($attr_type->authorised_value_category()) {
+                    $newentry->{use_dropdown} = 1;
+                    $newentry->{auth_val_loop} = GetAuthorisedValues($attr_type->authorised_value_category(), $attr->{value});
+                }
+                $i++;
+                $newentry->{form_id} = "patron_attr_$i";
+                #use Data::Dumper; die Dumper($entry) if  $entry->{use_dropdown};
+                push @attribute_loop, $newentry;
+            }
+        } else {
+            $i++;
+            my $newentry = { map { $_ => $entry->{$_} } %$entry };
+            if ($attr_type->authorised_value_category()) {
+                $newentry->{use_dropdown} = 1;
+                $newentry->{auth_val_loop} = GetAuthorisedValues($attr_type->authorised_value_category());
+            }
+            $newentry->{form_id} = "patron_attr_$i";
+            push @attribute_loop, $newentry;
+        }
+    }
+    $template->param(patron_attributes => \@attribute_loop);
+
+}
+
 # Local Variables:
 # tab-width: 8
 # End: