Bug 30642: DBRev 22.12.00.004
[srvgit] / tools / modborrowers.pl
index 8aed3f4..46f79d7 100755 (executable)
@@ -35,7 +35,7 @@ use Koha::DateUtils qw( dt_from_string );
 use Koha::List::Patron qw( GetPatronLists );
 use Koha::Libraries;
 use Koha::Patron::Categories;
-use Koha::Patron::Debarments qw( AddDebarment DelDebarment GetDebarments );
+use Koha::Patron::Debarments qw( AddDebarment DelDebarment );
 use Koha::Patrons;
 use List::MoreUtils qw(uniq);
 
@@ -51,6 +51,8 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
 
 my $logged_in_user = Koha::Patrons->find( $loggedinuser );
 
+$template->param( CanUpdatePasswordExpiration => 1 ) if $logged_in_user->is_superlibrarian;
+
 my $dbh       = C4::Context->dbh;
 
 # Show borrower informations
@@ -330,6 +332,8 @@ if ( $op eq 'show' ) {
         },
     );
 
+    push @fields, { name => "password_expiration_date", type => "date" } if $logged_in_user->is_superlibrarian;
+
     $template->param('patron_attributes_codes', \@patron_attributes_codes);
     $template->param('patron_attributes_values', \@patron_attributes_values);
 
@@ -341,16 +345,18 @@ if ( $op eq 'do' ) {
 
     my @disabled = $input->multi_param('disable_input');
     my $infos;
-    for my $field ( qw/surname firstname branchcode categorycode streetnumber address address2 city state zipcode country email phone mobile sort1 sort2 dateenrolled dateexpiry borrowernotes opacnote debarred debarredcomment/ ) {
+    for my $field ( qw/surname firstname branchcode categorycode streetnumber address address2 city state zipcode country email phone mobile sort1 sort2 dateenrolled dateexpiry password_expiration_date borrowernotes opacnote debarred debarredcomment/ ) {
         my $value = $input->param($field);
         $infos->{$field} = $value if $value;
         $infos->{$field} = "" if grep { $_ eq $field } @disabled;
     }
 
-    for my $field ( qw( dateenrolled dateexpiry debarred ) ) {
+    for my $field ( qw( dateenrolled dateexpiry debarred password_expiration_date ) ) {
         $infos->{$field} = dt_from_string($infos->{$field}) if $infos->{$field};
     }
 
+    delete $infos->{password_expiration_date} unless $logged_in_user->is_superlibrarian;
+
     my @attributes = $input->multi_param('patron_attributes');
     my @attr_values = $input->multi_param('patron_attributes_value');
 
@@ -358,6 +364,7 @@ if ( $op eq 'do' ) {
     my @borrowernumbers = $input->multi_param('borrowernumber');
     # For each borrower selected
     for my $borrowernumber ( @borrowernumbers ) {
+
         # If at least one field are filled, we want to modify the borrower
         if ( defined $infos ) {
             # If a debarred date or debarred comment has been submitted make a new debarment
@@ -372,19 +379,19 @@ if ( $op eq 'do' ) {
             }
 
             # If debarment date or debarment comment are disabled then remove all debarrments
+            my $patron = Koha::Patrons->find( $borrowernumber );
             if ( grep { /debarred/ } @disabled ) {
                 eval {
-                   my $debarrments = GetDebarments( { borrowernumber => $borrowernumber } );
-                   foreach my $debarment (@$debarrments) {
-                      DelDebarment( $debarment->{'borrower_debarment_id'} );
+                   my $debarrments = $patron->restrictions;
+                   while( my $debarment = $debarrments->next ) {
+                      DelDebarment( $debarment->borrower_debarment_id );
                    }
                 };
             }
 
             $infos->{borrowernumber} = $borrowernumber;
-            eval { Koha::Patrons->find( $borrowernumber )->set($infos)->store; };
+            eval { $patron->set($infos)->store; };
             if ( $@ ) { # FIXME We could provide better error handling here
-                my $patron = Koha::Patrons->find( $borrowernumber );
                 $infos->{cardnumber} = $patron ? $patron->cardnumber || '' : '';
                 push @errors, { error => "can_not_update", borrowernumber => $infos->{borrowernumber}, cardnumber => $infos->{cardnumber} };
             }