Bug 32852: Fix cataloguing/value_builder/unimarc_field_125b.pl
[koha-ffzg.git] / C4 / Members.pm
index 81afcad..c17bbb2 100644 (file)
@@ -20,57 +20,43 @@ package C4::Members;
 # along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 
-use strict;
-#use warnings; FIXME - Bug 2505
+use Modern::Perl;
 use C4::Context;
-use String::Random qw( random_string );
 use Scalar::Util qw( looks_like_number );
-use Date::Calc qw/Today check_date Date_to_Days/;
-use List::MoreUtils qw( uniq );
-use JSON qw(to_json);
-use C4::Log; # logaction
-use C4::Overdues;
+use Date::Calc qw( check_date Date_to_Days );
+use C4::Overdues qw( checkoverdues );
 use C4::Reserves;
 use C4::Accounts;
-use C4::Biblio;
-use C4::Letters;
-use C4::Members::Attributes qw(SearchIdMatchingAttribute UpdateBorrowerAttribute);
-use C4::NewsChannels; #get slip news
+use C4::Letters qw( GetPreparedLetter );
 use DateTime;
 use Koha::Database;
-use Koha::DateUtils;
-use Koha::AuthUtils qw(hash_password);
+use Koha::DateUtils qw( dt_from_string output_pref );
 use Koha::Database;
 use Koha::Holds;
-use Koha::List::Patron;
+use Koha::AdditionalContents;
 use Koha::Patrons;
 use Koha::Patron::Categories;
 
-our (@ISA,@EXPORT,@EXPORT_OK,$debug);
-
+our (@ISA, @EXPORT_OK);
 BEGIN {
-    $debug = $ENV{DEBUG} || 0;
     require Exporter;
     @ISA = qw(Exporter);
-    #Get data
-    push @EXPORT, qw(
+    @EXPORT_OK = qw(
+      GetMemberDetails
+      GetMember
 
-        &GetAllIssues
+      GetAllIssues
 
-        &GetBorrowersToExpunge
+      GetBorrowersToExpunge
 
-        &IssueSlip
-    );
+      IssueSlip
 
-    #Modify data
-    push @EXPORT, qw(
-        &changepassword
-    );
+      checkuserpassword
+      get_cardnumber_length
+      checkcardnumber
 
-    #Check data
-    push @EXPORT, qw(
-        &checkuserpassword
-        &checkcardnumber
+      DeleteUnverifiedOpacRegistrations
+      DeleteExpiredOpacRegistrations
     );
 }
 
@@ -84,7 +70,7 @@ use C4::Members;
 
 =head1 DESCRIPTION
 
-This module contains routines for adding, modifying and deleting members/patrons/borrowers 
+This module contains routines for adding, modifying and deleting members/patrons/borrowers
 
 =head1 FUNCTIONS
 
@@ -126,7 +112,7 @@ The following will be set where applicable:
  $flags->{WAITING}->{message}       Message -- deprecated
  $flags->{WAITING}->{itemlist}      ref-to-array: list of available items
 
-=over 
+=over
 
 =item C<$flags-E<gt>{ODUES}-E<gt>{itemlist}> is a reference-to-array listing the
 overdue items. Its elements are references-to-hash, each describing an
@@ -142,7 +128,7 @@ fields from the reserves table of the Koha database.
 
 =back
 
-All the "message" fields that include language generated in this function are deprecated, 
+All the "message" fields that include language generated in this function are deprecated,
 because such strings belong properly in the display layer.
 
 The "message" field that comes from the DB is OK.
@@ -181,8 +167,8 @@ sub patronflags {
     $no_issues_charge_guarantees = undef unless looks_like_number( $no_issues_charge_guarantees );
     if ( defined $no_issues_charge_guarantees ) {
         my $p = Koha::Patrons->find( $patroninformation->{borrowernumber} );
-        my @guarantees = $p->guarantees();
-        my $guarantees_non_issues_charges;
+        my @guarantees = map { $_->guarantee } $p->guarantee_relationships->as_list;
+        my $guarantees_non_issues_charges = 0;
         foreach my $g ( @guarantees ) {
             $guarantees_non_issues_charges += $g->account->non_issues_charges;
         }
@@ -280,19 +266,21 @@ sub GetAllIssues {
 
     my $dbh = C4::Context->dbh;
     my $query =
-'SELECT *, issues.timestamp as issuestimestamp, issues.renewals AS renewals,items.renewals AS totalrenewals,items.timestamp AS itemstimestamp
-  FROM issues 
+'SELECT issues.*, items.*, biblio.*, biblioitems.*, issues.timestamp as issuestimestamp, issues.renewals_count AS renewals,items.renewals AS totalrenewals,items.timestamp AS itemstimestamp,borrowers.firstname,borrowers.surname
+  FROM issues
   LEFT JOIN items on items.itemnumber=issues.itemnumber
+  LEFT JOIN borrowers on borrowers.borrowernumber=issues.issuer_id
   LEFT JOIN biblio ON items.biblionumber=biblio.biblionumber
   LEFT JOIN biblioitems ON items.biblioitemnumber=biblioitems.biblioitemnumber
-  WHERE borrowernumber=? 
+  WHERE issues.borrowernumber=?
   UNION ALL
-  SELECT *, old_issues.timestamp as issuestimestamp, old_issues.renewals AS renewals,items.renewals AS totalrenewals,items.timestamp AS itemstimestamp 
-  FROM old_issues 
+  SELECT old_issues.*, items.*, biblio.*, biblioitems.*, old_issues.timestamp as issuestimestamp, old_issues.renewals_count AS renewals,items.renewals AS totalrenewals,items.timestamp AS itemstimestamp,borrowers.firstname,borrowers.surname
+  FROM old_issues
   LEFT JOIN items on items.itemnumber=old_issues.itemnumber
+  LEFT JOIN borrowers on borrowers.borrowernumber=old_issues.issuer_id
   LEFT JOIN biblio ON items.biblionumber=biblio.biblionumber
   LEFT JOIN biblioitems ON items.biblioitemnumber=biblioitems.biblioitemnumber
-  WHERE borrowernumber=? AND old_issues.itemnumber IS NOT NULL
+  WHERE old_issues.borrowernumber=? AND old_issues.itemnumber IS NOT NULL
   order by ' . $order;
     if ($limit) {
         $query .= " limit $limit";
@@ -367,7 +355,7 @@ sub get_cardnumber_length {
   $borrowers = &GetBorrowersToExpunge(
       not_borrowed_since => $not_borrowed_since,
       expired_before       => $expired_before,
-      category_code        => $category_code,
+      category_code        => \@category_code,
       patron_list_id       => $patron_list_id,
       branchcode           => $branchcode
   );
@@ -385,11 +373,11 @@ sub GetBorrowersToExpunge {
     my $filtercategory   = $params->{'category_code'};
     my $filterbranch     = $params->{'branchcode'} ||
                         ((C4::Context->preference('IndependentBranches')
-                             && C4::Context->userenv 
+                             && C4::Context->userenv
                              && !C4::Context->IsSuperLibrarian()
                              && C4::Context->userenv->{branch})
                          ? C4::Context->userenv->{branch}
-                         : "");  
+                         : "");
     my $filterpatronlist = $params->{'patron_list_id'};
 
     my $dbh   = C4::Context->dbh;
@@ -402,18 +390,19 @@ sub GetBorrowersToExpunge {
             FROM   borrowers
             JOIN   categories USING (categorycode)
             LEFT JOIN (
-                SELECT guarantorid
-                FROM borrowers
-                WHERE guarantorid IS NOT NULL
-                    AND guarantorid <> 0
-            ) as tmp ON borrowers.borrowernumber=tmp.guarantorid
+                SELECT guarantor_id
+                FROM borrower_relationships
+                WHERE guarantor_id IS NOT NULL
+                    AND guarantor_id <> 0
+            ) as tmp ON borrowers.borrowernumber=tmp.guarantor_id
             LEFT JOIN old_issues USING (borrowernumber)
             LEFT JOIN issues USING (borrowernumber)|;
     if ( $filterpatronlist  ){
         $query .= q| LEFT JOIN patron_list_patrons USING (borrowernumber)|;
     }
     $query .= q| WHERE  category_type <> 'S'
-        AND tmp.guarantorid IS NULL
+        AND ( borrowers.flags IS NULL OR borrowers.flags = 0 )
+        AND tmp.guarantor_id IS NULL
     |;
     my @query_params;
     if ( $filterbranch && $filterbranch ne "" ) {
@@ -429,8 +418,13 @@ sub GetBorrowersToExpunge {
         push @query_params, $filterlastseen;
     }
     if ( $filtercategory ) {
-        $query .= " AND categorycode = ? ";
-        push( @query_params, $filtercategory );
+        if (ref($filtercategory) ne 'ARRAY' ) {
+            $filtercategory = [ $filtercategory ];
+        }
+        if ( @$filtercategory ) {
+            $query .= " AND categorycode IN (" . join(',', ('?') x @$filtercategory) . ") ";
+            push( @query_params, @$filtercategory );
+        }
     }
     if ( $filterpatronlist ){
         $query.=" AND patron_list_id = ? ";
@@ -444,16 +438,19 @@ sub GetBorrowersToExpunge {
         push @query_params,$filterdate;
     }
 
-    warn $query if $debug;
+    if ( my $anonymous_patron = C4::Context->preference("AnonymousPatron") ) {
+        $query .= q{ AND borrowernumber != ? };
+        push( @query_params, $anonymous_patron );
+    }
 
     my $sth = $dbh->prepare($query);
-    if (scalar(@query_params)>0){  
+    if (scalar(@query_params)>0){
         $sth->execute(@query_params);
     }
     else {
         $sth->execute;
     }
-    
+
     my @results;
     while ( my $data = $sth->fetchrow_hashref ) {
         push @results, $data;
@@ -493,7 +490,7 @@ sub GetBorrowersToExpunge {
       </overdue>
 
       <news>
-         <<opac_news.*>>
+         <<additional_contents.*>>
       </news>
 
   ISSUEQSLIP:
@@ -585,11 +582,29 @@ sub IssueSlip {
                 issues      => $all,
             };
         }
-        my $news = GetNewsToDisplay( "slip", $branch );
-        my @news = map {
-            $_->{'timestamp'} = $_->{'newdate'};
-            { opac_news => $_ }
-        } @$news;
+        my $news = Koha::AdditionalContents->search_for_display(
+            {
+                category   => 'news',
+                location   => 'slip',
+                lang       => $patron->lang,
+                library_id => $branch,
+            }
+        );
+        my @news;
+        while ( my $n = $news->next ) {
+            my $all = $n->unblessed_all_relateds;
+
+            # FIXME We keep newdate and timestamp for backward compatibility (from GetNewsToDisplay)
+            # But we should remove them and adjust the existing templates in a db rev
+            # FIXME This must be formatted in the notice template
+            my $published_on_dt = output_pref({ dt => dt_from_string( $all->{published_on} ), dateonly => 1 });
+            $all->{newdate} = $published_on_dt;
+            $all->{timestamp} = $published_on_dt;
+
+            push @news, {
+                additional_contents => $all,
+            };
+        }
         $letter_code = 'ISSUESLIP';
         %repeat      = (
             checkedout => \@checkouts,
@@ -599,7 +614,8 @@ sub IssueSlip {
         %loops = (
             issues => [ map { $_->{issues}{itemnumber} } @checkouts ],
             overdues   => [ map { $_->{issues}{itemnumber} } @overdues ],
-            opac_news => [ map { $_->{opac_news}{idnew} } @news ],
+            opac_news => [ map { $_->{additional_contents}{idnew} } @news ],
+            additional_contents => [ map { $_->{additional_contents}{idnew} } @news ],
         );
     }
 
@@ -629,19 +645,23 @@ sub DeleteExpiredOpacRegistrations {
     my $delay = C4::Context->preference('PatronSelfRegistrationExpireTemporaryAccountsDelay');
     my $category_code = C4::Context->preference('PatronSelfRegistrationDefaultCategory');
 
-    return 0 if not $category_code or not defined $delay or $delay eq q||;
+    return 0 unless $category_code && $delay;
+        # DO NOT REMOVE test on delay here!
+        # Some libraries may not use a temporary category, but want to keep patrons.
+        # We should not delete patrons when the value is NULL, empty string or 0.
 
-    my $query = qq|
-SELECT borrowernumber
-FROM borrowers
-WHERE categorycode = ? AND DATEDIFF( NOW(), dateenrolled ) > ? |;
+    my $date_enrolled = dt_from_string();
+    $date_enrolled->subtract( days => $delay );
+
+    my $registrations_to_del = Koha::Patrons->search({
+        dateenrolled => {'<=' => $date_enrolled->ymd},
+        categorycode => $category_code,
+    });
 
-    my $dbh = C4::Context->dbh;
-    my $sth = $dbh->prepare($query);
-    $sth->execute( $category_code, $delay );
     my $cnt=0;
-    while ( my ($borrowernumber) = $sth->fetchrow_array() ) {
-        Koha::Patrons->find($borrowernumber)->delete;
+    while ( my $registration = $registrations_to_del->next() ) {
+        next if $registration->checkouts->count || $registration->account->balance;
+        $registration->delete;
         $cnt++;
     }
     return $cnt;