Bug 15177: Sample notice TO_PROCESS confuses budget and fund
[koha-ffzg.git] / C4 / Members.pm
index ebb8a1f..5f71e89 100644 (file)
@@ -41,10 +41,7 @@ use Koha::Borrower::Debarments qw(IsDebarred);
 use Text::Unaccent qw( unac_string );
 use Koha::AuthUtils qw(hash_password);
 use Koha::Database;
-use Module::Load;
-if ( C4::Context->preference('NorwegianPatronDBEnable') && C4::Context->preference('NorwegianPatronDBEnable') == 1 ) {
-    load Koha::NorwegianPatronDB, qw( NLUpdateHashedPIN NLEncryptPIN NLSync );
-}
+require Koha::NorwegianPatronDB;
 
 our ($VERSION,@ISA,@EXPORT,@EXPORT_OK,$debug);
 
@@ -108,6 +105,7 @@ BEGIN {
         GetBorrowersWithEmail
 
         HasOverdues
+        GetOverduesForPatron
     );
 
     #Modify data
@@ -650,7 +648,7 @@ sub ModMember {
         } else {
             if ( C4::Context->preference('NorwegianPatronDBEnable') && C4::Context->preference('NorwegianPatronDBEnable') == 1 ) {
                 # Update the hashed PIN in borrower_sync.hashed_pin, before Koha hashes it
-                NLUpdateHashedPIN( $data{'borrowernumber'}, $data{password} );
+                Koha::NorwegianPatronDB::NLUpdateHashedPIN( $data{'borrowernumber'}, $data{password} );
             }
             $data{password} = hash_password($data{password});
         }
@@ -704,7 +702,7 @@ sub ModMember {
             # Set the value of 'sync'
             $borrowersync->update( { 'sync' => $data{'sync'} } );
             # Try to do the live sync
-            NLSync({ 'borrowernumber' => $data{'borrowernumber'} });
+            Koha::NorwegianPatronDB::NLSync({ 'borrowernumber' => $data{'borrowernumber'} });
         }
 
         logaction("MEMBERS", "MODIFY", $data{'borrowernumber'}, "UPDATE (executed w/ arg: $data{'borrowernumber'})") if C4::Context->preference("BorrowersLog");
@@ -780,7 +778,7 @@ sub AddMember {
             'synctype'       => 'norwegianpatrondb',
             'sync'           => 1,
             'syncstatus'     => 'new',
-            'hashed_pin'     => NLEncryptPIN( $plain_text_password ),
+            'hashed_pin'     => Koha::NorwegianPatronDB::NLEncryptPIN( $plain_text_password ),
         });
     }
 
@@ -1172,7 +1170,7 @@ sub GetMemberAccountRecords {
                         SELECT * 
                         FROM accountlines 
                         WHERE borrowernumber=?);
-    $strsth.=" ORDER BY date desc,timestamp DESC";
+    $strsth.=" ORDER BY accountlines_id desc";
     my $sth= $dbh->prepare( $strsth );
     $sth->execute( $borrowernumber );
 
@@ -1493,16 +1491,18 @@ sub GetExpiryDate {
 
 sub GetUpcomingMembershipExpires {
     my $dbh = C4::Context->dbh;
-    my $days = C4::Context->preference("MembershipExpiryDaysNotice");
+    my $days = C4::Context->preference("MembershipExpiryDaysNotice") || 0;
+    my $dateexpiry = output_pref({ dt => (dt_from_string()->add( days => $days)), dateformat => 'iso', dateonly => 1 });
+
     my $query = "
         SELECT borrowers.*, categories.description,
         branches.branchname, branches.branchemail FROM borrowers
         LEFT JOIN branches on borrowers.branchcode = branches.branchcode
         LEFT JOIN categories on borrowers.categorycode = categories.categorycode
-        WHERE dateexpiry = DATE_ADD(CURDATE(),INTERVAL $days DAY);
+        WHERE dateexpiry = ?;
     ";
     my $sth = $dbh->prepare($query);
-    $sth->execute;
+    $sth->execute($dateexpiry);
     my $results = $sth->fetchall_arrayref({});
     return $results;
 }
@@ -1813,6 +1813,40 @@ sub DelMember {
     return $sth->rows;
 }
 
+=head2 HandleDelBorrower
+
+     HandleDelBorrower($borrower);
+
+When a member is deleted (DelMember in Members.pm), you should call me first.
+This routine deletes/moves lists and entries for the deleted member/borrower.
+Lists owned by the borrower are deleted, but entries from the borrower to
+other lists are kept.
+
+=cut
+
+sub HandleDelBorrower {
+    my ($borrower)= @_;
+    my $query;
+    my $dbh = C4::Context->dbh;
+
+    #Delete all lists and all shares of this borrower
+    #Consistent with the approach Koha uses on deleting individual lists
+    #Note that entries in virtualshelfcontents added by this borrower to
+    #lists of others will be handled by a table constraint: the borrower
+    #is set to NULL in those entries.
+    $query="DELETE FROM virtualshelves WHERE owner=?";
+    $dbh->do($query,undef,($borrower));
+
+    #NOTE:
+    #We could handle the above deletes via a constraint too.
+    #But a new BZ report 11889 has been opened to discuss another approach.
+    #Instead of deleting we could also disown lists (based on a pref).
+    #In that way we could save shared and public lists.
+    #The current table constraints support that idea now.
+    #This pref should then govern the results of other routines/methods such as
+    #Koha::Virtualshelf->new->delete too.
+}
+
 =head2 ExtendMemberSubscriptionTo (OUEST-PROVENCE)
 
     $date = ExtendMemberSubscriptionTo($borrowerid, $date);
@@ -2550,6 +2584,25 @@ WHERE borrowernumber = 0 AND DATEDIFF( NOW(), timestamp ) > ?|;
     return $cnt eq '0E0'? 0: $cnt;
 }
 
+sub GetOverduesForPatron {
+    my ( $borrowernumber ) = @_;
+
+    my $sql = "
+        SELECT *
+        FROM issues, items, biblio, biblioitems
+        WHERE items.itemnumber=issues.itemnumber
+          AND biblio.biblionumber   = items.biblionumber
+          AND biblio.biblionumber   = biblioitems.biblionumber
+          AND issues.borrowernumber = ?
+          AND date_due < NOW()
+    ";
+
+    my $sth = C4::Context->dbh->prepare( $sql );
+    $sth->execute( $borrowernumber );
+
+    return $sth->fetchall_arrayref({});
+}
+
 END { }    # module clean-up code here (global destructor)
 
 1;