More changes to update-child process. Many scripts missing necessary data for checkin...
[koha_fer] / members / memberentry.pl
index 4619fcc..bf7379c 100755 (executable)
@@ -29,14 +29,15 @@ 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::Letters;
 use C4::Branch; # GetBranches
 
-#use Smart::Comments;
-
 use vars qw($debug);
 
 BEGIN {
@@ -69,8 +70,6 @@ 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;
@@ -100,6 +99,8 @@ my $new_c_type = $category_type; #if we have input param, then we've already cho
 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 !
 
@@ -147,8 +148,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... 
@@ -166,9 +169,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 ''){
-    $newdata{'city'}= $city;
-    $newdata{'zipcode'}=$zipcode; 
+if ($guarantorid eq '') {
+    # set only if parameter was passed from the form
+    $newdata{'city'}    = $input->param('city')    if defined($input->param('city'));
+    $newdata{'zipcode'} = $input->param('zipcode') if defined($input->param('zipcode'));
 }
 
 #builds default userid
@@ -180,6 +184,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';
@@ -207,6 +212,16 @@ 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'){
@@ -223,18 +238,52 @@ if ((!$nok) and ($op eq 'insert' or $op eq 'save')){
        $debug and warn "$op dates: " . join "\t", map {"$_: $newdata{$_}"} qw(dateofbirth dateenrolled dateexpiry);
        if ($op eq 'insert'){
                # we know it's not a duplicate borrowernumber or there would already be an error
-               $borrowernumber = &AddMember(%newdata);
+        $borrowernumber = &AddMember(%newdata);
+
+        # If 'AutoEmailOpacUser' syspref is on, email user their account details from the 'notice' that matches the user's branchcode.
+        if ( C4::Context->preference("AutoEmailOpacUser") == 1 && $newdata{'userid'}  && $newdata{'password'}) {
+            #look for defined primary email address, if blank - attempt to use borr.email and borr.emailpro instead
+            my $emailaddr;
+            if  (C4::Context->preference("AutoEmailPrimaryAddress") ne 'OFF'  && 
+                $newdata{C4::Context->preference("AutoEmailPrimaryAddress")} =~  /\w\@\w/ ) {
+                $emailaddr =   $newdata{C4::Context->preference("AutoEmailPrimaryAddress")} 
+            } 
+            elsif ($newdata{email} =~ /\w\@\w/) {
+                $emailaddr = $newdata{email} 
+            }
+            elsif ($newdata{emailpro} =~ /\w\@\w/) {
+                $emailaddr = $newdata{emailpro} 
+            }
+            elsif ($newdata{B_email} =~ /\w\@\w/) {
+                $emailaddr = $newdata{B_email} 
+            }
+            # if we manage to find a valid email address, send notice 
+            if ($emailaddr) {
+                $newdata{emailaddr} = $emailaddr;
+                my $letter = getletter ('members', "ACCTDETAILS:$newdata{'branchcode'}") ;
+                # if $branch notice fails, then email a default notice instead.
+                $letter = getletter ('members', "ACCTDETAILS")  if !$letter;
+                SendAlerts ( 'members' , \%newdata , $letter ) if $letter
+            }
+        } 
+
                if ($data{'organisations'}){            
                        # need to add the members organisations
                        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);    
+        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?borrowernumber=$borrowernumber") :
@@ -253,7 +302,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")) {
@@ -268,12 +317,12 @@ if (C4::Context->preference("IndependantBranches")) {
 if ($op eq 'add'){
        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);
+       $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';
@@ -405,7 +454,7 @@ my $onlymine=(C4::Context->preference('IndependantBranches') &&
 my $branches=GetBranches($onlymine);
 my $default;
 
-foreach my $branch (sort keys %$branches) {
+for my $branch (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
     push @select_branch,$branch;
     $select_branches{$branch} = $branches->{$branch}->{'branchname'};
     $default = C4::Context->userenv->{'branch'} if (C4::Context->userenv && C4::Context->userenv->{'branch'});
@@ -482,6 +531,11 @@ foreach (qw(dateenrolled dateexpiry dateofbirth)) {
        $template->param( $_ => $data{$_});
 }
 
+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);
@@ -522,6 +576,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: