Bug 18423 Follow-up - Add 'Add child' button to statistics tab
[koha.git] / members / memberentry.pl
index 8fc5d9b..768b163 100755 (executable)
@@ -25,7 +25,6 @@ use warnings;
 # external modules
 use CGI qw ( -utf8 );
 use List::MoreUtils qw/uniq/;
-use Digest::MD5 qw(md5_base64);
 
 # internal modules
 use C4::Auth;
@@ -38,11 +37,14 @@ use C4::Koha;
 use C4::Log;
 use C4::Letters;
 use C4::Form::MessagingPreferences;
+use Koha::AuthorisedValues;
 use Koha::Patron::Debarments;
 use Koha::Cities;
 use Koha::DateUtils;
 use Koha::Libraries;
 use Koha::Patron::Categories;
+use Koha::Patron::HouseboundRole;
+use Koha::Patron::HouseboundRoles;
 use Koha::Token;
 use Email::Valid;
 use Module::Load;
@@ -286,8 +288,7 @@ if ($op eq 'save' || $op eq 'insert'){
 
     die "Wrong CSRF token"
         unless Koha::Token->new->check_csrf({
-            id     => C4::Context->userenv->{id},
-            secret => md5_base64( C4::Context->config('pass') ),
+            session_id => scalar $input->cookie('CGISESSID'),
             token  => scalar $input->param('csrf_token'),
         });
 
@@ -302,9 +303,19 @@ if ($op eq 'save' || $op eq 'insert'){
                 : ()
     }
 
-    if ( $newdata{dateofbirth} ) {
-        my $age = GetAge($newdata{dateofbirth});
-        my $borrowercategory = Koha::Patron::Categories->find($newdata{categorycode});
+    my $dateofbirth;
+    if ($op eq 'save' && $step == 3) {
+        my $borrower = C4::Members::GetMember(borrowernumber => $borrowernumber);
+        $dateofbirth = $borrower->{dateofbirth};
+    }
+    else {
+        $dateofbirth = $newdata{dateofbirth};
+    }
+
+    if ( $dateofbirth ) {
+        my $patron = Koha::Patron->new({ dateofbirth => $dateofbirth });
+        my $age = $patron->get_age;
+        my $borrowercategory = Koha::Patron::Categories->find($categorycode);
         my ($low,$high) = ($borrowercategory->dateofbirthrequired, $borrowercategory->upperagelimit);
         if (($high && ($age > $high)) or ($age < $low)) {
             push @errors, 'ERROR_age_limitations';
@@ -435,11 +446,53 @@ if ((!$nok) and $nodouble and ($op eq 'insert' or $op eq 'save')){
         if ( exists $data{'borrowernumber'} && C4::Context->preference('NorwegianPatronDBEnable') && C4::Context->preference('NorwegianPatronDBEnable') == 1 ) {
             NLSync({ 'borrowernumber' => $borrowernumber });
         }
-       } elsif ($op eq 'save'){ 
-               if ($NoUpdateLogin) {
-                       delete $newdata{'password'};
-                       delete $newdata{'userid'};
-               }
+
+        # Create HouseboundRole if necessary.
+        # Borrower did not exist, so HouseboundRole *cannot* yet exist.
+        my ( $hsbnd_chooser, $hsbnd_deliverer ) = ( 0, 0 );
+        $hsbnd_chooser = 1 if $input->param('housebound_chooser');
+        $hsbnd_deliverer = 1 if $input->param('housebound_deliverer');
+        # Only create a HouseboundRole if patron has a role.
+        if ( $hsbnd_chooser || $hsbnd_deliverer ) {
+            Koha::Patron::HouseboundRole->new({
+                borrowernumber_id    => $borrowernumber,
+                housebound_chooser   => $hsbnd_chooser,
+                housebound_deliverer => $hsbnd_deliverer,
+            })->store;
+        }
+
+    } elsif ($op eq 'save') {
+
+        # Update or create our HouseboundRole if necessary.
+        my $housebound_role = Koha::Patron::HouseboundRoles->find($borrowernumber);
+        my ( $hsbnd_chooser, $hsbnd_deliverer ) = ( 0, 0 );
+        $hsbnd_chooser = 1 if $input->param('housebound_chooser');
+        $hsbnd_deliverer = 1 if $input->param('housebound_deliverer');
+        if ( $housebound_role ) {
+            if ( $hsbnd_chooser || $hsbnd_deliverer ) {
+                # Update our HouseboundRole.
+                $housebound_role
+                    ->housebound_chooser($hsbnd_chooser)
+                    ->housebound_deliverer($hsbnd_deliverer)
+                    ->store;
+            } else {
+                $housebound_role->delete; # No longer needed.
+            }
+        } else {
+            # Only create a HouseboundRole if patron has a role.
+            if ( $hsbnd_chooser || $hsbnd_deliverer ) {
+                $housebound_role = Koha::Patron::HouseboundRole->new({
+                    borrowernumber_id    => $borrowernumber,
+                    housebound_chooser   => $hsbnd_chooser,
+                    housebound_deliverer => $hsbnd_deliverer,
+                })->store;
+            }
+        }
+
+        if ($NoUpdateLogin) {
+            delete $newdata{'password'};
+            delete $newdata{'userid'};
+        }
         &ModMember(%newdata) unless scalar(keys %newdata) <= 1; # bug 4508 - avoid crash if we're not
                                                                 # updating any columns in the borrowers table,
                                                                 # which can happen if we're only editing the
@@ -505,7 +558,6 @@ if ( $op eq "duplicate" ) {
     $data{'cardnumber'} = "";
 }
 
-$data{'cardnumber'}=fixup_cardnumber($data{'cardnumber'}) if ( ( $op eq 'add' ) or ( $op eq 'duplicate' ) );
 if(!defined($data{'sex'})){
     $template->param( none => 1);
 } elsif($data{'sex'} eq 'F'){
@@ -695,12 +747,13 @@ $template->param(
   );
 
 # Generate CSRF token
+$template->param( csrf_token =>
+      Koha::Token->new->generate_csrf( { session_id => scalar $input->cookie('CGISESSID'), } ),
+);
+
+# HouseboundModule data
 $template->param(
-    csrf_token => Koha::Token->new->generate_csrf(
-        {   id     => C4::Context->userenv->{id},
-            secret => md5_base64( C4::Context->config('pass') ),
-        }
-    ),
+    housebound_role  => Koha::Patron::HouseboundRoles->find($borrowernumber),
 );
 
 if(defined($data{'flags'})){
@@ -795,7 +848,8 @@ sub patron_attributes_form {
         }
     }
     while ( my ($class, @items) = each %items_by_class ) {
-        my $lib = GetAuthorisedValueByCode( 'PA_CLASS', $class ) || $class;
+        my $av = Koha::AuthorisedValues->search({ category => 'PA_CLASS', authorised_value => $class });
+        my $lib = $av->count ? $av->next->lib : $class;
         push @attribute_loop, {
             class => $class,
             items => @items,