Bug 16845: Remove unused C4::Members::ModPrivacy
[srvgit] / C4 / Members.pm
index a003809..8d3df66 100644 (file)
@@ -24,6 +24,7 @@ use strict;
 #use warnings; FIXME - Bug 2505
 use C4::Context;
 use String::Random qw( random_string );
+use Scalar::Util qw( looks_like_number );
 use Date::Calc qw/Today Add_Delta_YM check_date Date_to_Days/;
 use C4::Log; # logaction
 use C4::Overdues;
@@ -36,12 +37,14 @@ use C4::NewsChannels; #get slip news
 use DateTime;
 use Koha::Database;
 use Koha::DateUtils;
-use Koha::Borrower::Debarments qw(IsDebarred);
+use Koha::Patron::Debarments qw(IsDebarred);
 use Text::Unaccent qw( unac_string );
 use Koha::AuthUtils qw(hash_password);
 use Koha::Database;
+use Koha::Holds;
+use Koha::List::Patron;
 
-our ($VERSION,@ISA,@EXPORT,@EXPORT_OK,$debug);
+our (@ISA,@EXPORT,@EXPORT_OK,$debug);
 
 use Module::Load::Conditional qw( can_load );
 if ( ! can_load( modules => { 'Koha::NorwegianPatronDB' => undef } ) ) {
@@ -50,7 +53,6 @@ if ( ! can_load( modules => { 'Koha::NorwegianPatronDB' => undef } ) ) {
 
 
 BEGIN {
-    $VERSION = 3.07.00.049;
     $debug = $ENV{DEBUG} || 0;
     require Exporter;
     @ISA = qw(Exporter);
@@ -61,8 +63,6 @@ BEGIN {
         &GetMemberRelatives
         &GetMember
 
-        &GetGuarantees
-
         &GetMemberIssuesAndFines
         &GetPendingIssues
         &GetAllIssues
@@ -74,10 +74,6 @@ BEGIN {
         &GetSortDetails
         &GetTitles
 
-        &GetPatronImage
-        &PutPatronImage
-        &RmPatronImage
-
         &GetHideLostItemsPreference
 
         &IsMemberBlocked
@@ -332,6 +328,28 @@ sub patronflags {
         $flaginfo{'amount'}  = sprintf "%.02f", $balance;
         $flags{'CREDITS'} = \%flaginfo;
     }
+
+    # Check the debt of the guarntees of this patron
+    my $no_issues_charge_guarantees = C4::Context->preference("NoIssuesChargeGuarantees");
+    $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;
+        foreach my $g ( @guarantees ) {
+            my ( $b, $n, $o ) = C4::Members::GetMemberAccountBalance( $g->id );
+            $guarantees_non_issues_charges += $n;
+        }
+
+        if ( $guarantees_non_issues_charges > $no_issues_charge_guarantees ) {
+            my %flaginfo;
+            $flaginfo{'message'} = sprintf 'patron guarantees owe %.02f', $guarantees_non_issues_charges;
+            $flaginfo{'amount'}  = $guarantees_non_issues_charges;
+            $flaginfo{'noissues'} = 1 unless C4::Context->preference("allowfineoverride");
+            $flags{'CHARGES_GUARANTEES'} = \%flaginfo;
+        }
+    }
+
     if (   $patroninformation->{'gonenoaddress'}
         && $patroninformation->{'gonenoaddress'} == 1 )
     {
@@ -439,7 +457,7 @@ sub GetMember {
     }
     $debug && warn $select, " ",values %information;
     my $sth = $dbh->prepare("$select");
-    $sth->execute(map{$information{$_}} keys %information);
+    $sth->execute(@values);
     my $data = $sth->fetchall_arrayref({});
     #FIXME interface to this routine now allows generation of a result set
     #so whole array should be returned but bowhere in the current code expects this
@@ -450,48 +468,6 @@ sub GetMember {
     return;
 }
 
-=head2 GetMemberRelatives
-
- @borrowernumbers = GetMemberRelatives($borrowernumber);
-
- C<GetMemberRelatives> returns a borrowersnumber's list of guarantor/guarantees of the member given in parameter
-
-=cut
-
-sub GetMemberRelatives {
-    my $borrowernumber = shift;
-    my $dbh = C4::Context->dbh;
-    my @glist;
-
-    # Getting guarantor
-    my $query = "SELECT guarantorid FROM borrowers WHERE borrowernumber=?";
-    my $sth = $dbh->prepare($query);
-    $sth->execute($borrowernumber);
-    my $data = $sth->fetchrow_arrayref();
-    push @glist, $data->[0] if $data->[0];
-    my $guarantor = $data->[0] ? $data->[0] : undef;
-
-    # Getting guarantees
-    $query = "SELECT borrowernumber FROM borrowers WHERE guarantorid=?";
-    $sth = $dbh->prepare($query);
-    $sth->execute($borrowernumber);
-    while ($data = $sth->fetchrow_arrayref()) {
-       push @glist, $data->[0];
-    }
-
-    # Getting sibling guarantees
-    if ($guarantor) {
-        $query = "SELECT borrowernumber FROM borrowers WHERE guarantorid=?";
-        $sth = $dbh->prepare($query);
-        $sth->execute($guarantor);
-        while ($data = $sth->fetchrow_arrayref()) {
-           push @glist, $data->[0] if ($data->[0] != $borrowernumber);
-        }
-    }
-
-    return @glist;
-}
-
 =head2 IsMemberBlocked
 
   my ($block_status, $count) = IsMemberBlocked( $borrowernumber );
@@ -516,7 +492,7 @@ sub IsMemberBlocked {
     my $borrowernumber = shift;
     my $dbh            = C4::Context->dbh;
 
-    my $blockeddate = Koha::Borrower::Debarments::IsDebarred($borrowernumber);
+    my $blockeddate = Koha::Patron::Debarments::IsDebarred($borrowernumber);
 
     return ( 1, $blockeddate ) if $blockeddate;
 
@@ -656,18 +632,10 @@ sub ModMember {
         borrowernumber => $new_borrower->{borrowernumber},
      });
 
+    delete $new_borrower->{userid} if exists $new_borrower->{userid} and not $new_borrower->{userid};
+
     my $execute_success = $rs->update($new_borrower);
     if ($execute_success ne '0E0') { # only proceed if the update was a success
-
-        # ok if its an adult (type) it may have borrowers that depend on it as a guarantor
-        # so when we update information for an adult we should check for guarantees and update the relevant part
-        # of their records, ie addresses and phone numbers
-        my $borrowercategory= GetBorrowercategory( $data{'category_type'} );
-        if ( exists  $borrowercategory->{'category_type'} && $borrowercategory->{'category_type'} eq ('A' || 'S') ) {
-            # is adult check guarantees;
-            UpdateGuarantees(%data);
-        }
-
         # If the patron changes to a category with enrollment fee, we add a fee
         if ( $data{categorycode} and $data{categorycode} ne $old_categorycode ) {
             if ( C4::Context->preference('FeeOnChangePatronCategory') ) {
@@ -758,6 +726,7 @@ sub AddMember {
     # get only the columns of Borrower
     my @columns = $schema->source('Borrower')->columns;
     my $new_member = { map { join(' ',@columns) =~ /$_/ ? ( $_ => $data{$_} )  : () } keys(%data) } ;
+    $new_member->{checkprevcheckout} ||= 'inherit';
     delete $new_member->{borrowernumber};
 
     my $rs = $schema->resultset('Borrower');
@@ -945,61 +914,6 @@ sub fixup_cardnumber {
     return $cardnumber;     # just here as a fallback/reminder 
 }
 
-=head2 GetGuarantees
-
-  ($num_children, $children_arrayref) = &GetGuarantees($parent_borrno);
-  $child0_cardno = $children_arrayref->[0]{"cardnumber"};
-  $child0_borrno = $children_arrayref->[0]{"borrowernumber"};
-
-C<&GetGuarantees> takes a borrower number (e.g., that of a patron
-with children) and looks up the borrowers who are guaranteed by that
-borrower (i.e., the patron's children).
-
-C<&GetGuarantees> returns two values: an integer giving the number of
-borrowers guaranteed by C<$parent_borrno>, and a reference to an array
-of references to hash, which gives the actual results.
-
-=cut
-
-#'
-sub GetGuarantees {
-    my ($borrowernumber) = @_;
-    my $dbh              = C4::Context->dbh;
-    my $sth              =
-      $dbh->prepare(
-"select cardnumber,borrowernumber, firstname, surname from borrowers where guarantorid=?"
-      );
-    $sth->execute($borrowernumber);
-
-    my @dat;
-    my $data = $sth->fetchall_arrayref({}); 
-    return ( scalar(@$data), $data );
-}
-
-=head2 UpdateGuarantees
-
-  &UpdateGuarantees($parent_borrno);
-  
-
-C<&UpdateGuarantees> borrower data for an adult and updates all the guarantees
-with the modified information
-
-=cut
-
-#'
-sub UpdateGuarantees {
-    my %data = shift;
-    my $dbh = C4::Context->dbh;
-    my ( $count, $guarantees ) = GetGuarantees( $data{'borrowernumber'} );
-    foreach my $guarantee (@$guarantees){
-        my $guaquery = qq|UPDATE borrowers 
-              SET address=?,fax=?,B_city=?,mobile=?,city=?,phone=?
-              WHERE borrowernumber=?
-        |;
-        my $sth = $dbh->prepare($guaquery);
-        $sth->execute($data{'address'},$data{'fax'},$data{'B_city'},$data{'mobile'},$data{'city'},$data{'phone'},$guarantee->{'borrowernumber'});
-    }
-}
 =head2 GetPendingIssues
 
   my $issues = &GetPendingIssues(@borrowernumber);
@@ -1013,7 +927,6 @@ The keys include C<biblioitems> fields except marc and marcxml.
 
 =cut
 
-#'
 sub GetPendingIssues {
     my @borrowernumbers = @_;
 
@@ -1176,7 +1089,7 @@ sub GetMemberAccountRecords {
         }
         $acctlines[$numlines] = $data;
         $numlines++;
-        $total += int(1000 * $data->{'amountoutstanding'}); # convert float to integer to avoid round-off errors
+        $total += sprintf "%.0f", 1000*$data->{amountoutstanding}; # convert float to integer to avoid round-off errors
     }
     $total /= 1000;
     return ( $total, \@acctlines,$numlines);
@@ -1444,25 +1357,48 @@ sub GetExpiryDate {
 
 =head2 GetUpcomingMembershipExpires
 
-  my $upcoming_mem_expires = GetUpcomingMembershipExpires();
+    my $expires = GetUpcomingMembershipExpires({
+        branch => $branch, before => $before, after => $after,
+    });
+
+    $branch is an optional branch code.
+    $before/$after is an optional number of days before/after the date that
+    is set by the preference MembershipExpiryDaysNotice.
+    If the pref would be 14, before 2 and after 3, you will get all expires
+    from 12 to 17 days.
 
 =cut
 
 sub GetUpcomingMembershipExpires {
+    my ( $params ) = @_;
+    my $before = $params->{before} || 0;
+    my $after  = $params->{after} || 0;
+    my $branch = $params->{branch};
+
     my $dbh = C4::Context->dbh;
     my $days = C4::Context->preference("MembershipExpiryDaysNotice") || 0;
-    my $dateexpiry = output_pref({ dt => (dt_from_string()->add( days => $days)), dateformat => 'iso', dateonly => 1 });
+    my $date1 = dt_from_string->add( days => $days - $before );
+    my $date2 = dt_from_string->add( days => $days + $after );
+    $date1= output_pref({ dt => $date1, dateformat => 'iso', dateonly => 1 });
+    $date2= output_pref({ dt => $date2, dateformat => 'iso', dateonly => 1 });
 
-    my $query = "
+    my $query = q|
         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 = ?;
-    ";
-    my $sth = $dbh->prepare($query);
-    $sth->execute($dateexpiry);
-    my $results = $sth->fetchall_arrayref({});
+        LEFT JOIN branches USING (branchcode)
+        LEFT JOIN categories USING (categorycode)
+    |;
+    if( $branch ) {
+        $query.= 'WHERE branchcode=? AND dateexpiry BETWEEN ? AND ?';
+    } else {
+        $query.= 'WHERE dateexpiry BETWEEN ? AND ?';
+    }
+
+    my $sth = $dbh->prepare( $query );
+    my @pars = $branch? ( $branch ): ();
+    push @pars, $date1, $date2;
+    $sth->execute( @pars );
+    my $results = $sth->fetchall_arrayref( {} );
     return $results;
 }
 
@@ -1726,18 +1662,16 @@ sub DelMember {
     my $borrowernumber = shift;
     #warn "in delmember with $borrowernumber";
     return unless $borrowernumber;    # borrowernumber is mandatory.
+    # Delete Patron's holds
+    my @holds = Koha::Holds->search({ borrowernumber => $borrowernumber });
+    $_->delete for @holds;
 
-    my $query = qq|DELETE 
-          FROM  reserves 
-          WHERE borrowernumber=?|;
-    my $sth = $dbh->prepare($query);
-    $sth->execute($borrowernumber);
-    $query = "
+    my $query = "
        DELETE
        FROM borrowers
        WHERE borrowernumber = ?
    ";
-    $sth = $dbh->prepare($query);
+    my $sth = $dbh->prepare($query);
     $sth->execute($borrowernumber);
     logaction("MEMBERS", "DELETE", $borrowernumber, "") if C4::Context->preference("BorrowersLog");
     return $sth->rows;
@@ -1830,66 +1764,6 @@ sub GetTitles {
     }
 }
 
-=head2 GetPatronImage
-
-    my ($imagedata, $dberror) = GetPatronImage($borrowernumber);
-
-Returns the mimetype and binary image data of the image for the patron with the supplied borrowernumber.
-
-=cut
-
-sub GetPatronImage {
-    my ($borrowernumber) = @_;
-    warn "Borrowernumber passed to GetPatronImage is $borrowernumber" if $debug;
-    my $dbh = C4::Context->dbh;
-    my $query = 'SELECT mimetype, imagefile FROM patronimage WHERE borrowernumber = ?';
-    my $sth = $dbh->prepare($query);
-    $sth->execute($borrowernumber);
-    my $imagedata = $sth->fetchrow_hashref;
-    warn "Database error!" if $sth->errstr;
-    return $imagedata, $sth->errstr;
-}
-
-=head2 PutPatronImage
-
-    PutPatronImage($cardnumber, $mimetype, $imgfile);
-
-Stores patron binary image data and mimetype in database.
-NOTE: This function is good for updating images as well as inserting new images in the database.
-
-=cut
-
-sub PutPatronImage {
-    my ($cardnumber, $mimetype, $imgfile) = @_;
-    warn "Parameters passed in: Cardnumber=$cardnumber, Mimetype=$mimetype, " . ($imgfile ? "Imagefile" : "No Imagefile") if $debug;
-    my $dbh = C4::Context->dbh;
-    my $query = "INSERT INTO patronimage (borrowernumber, mimetype, imagefile) VALUES ( ( SELECT borrowernumber from borrowers WHERE cardnumber = ? ),?,?) ON DUPLICATE KEY UPDATE imagefile = ?;";
-    my $sth = $dbh->prepare($query);
-    $sth->execute($cardnumber,$mimetype,$imgfile,$imgfile);
-    warn "Error returned inserting $cardnumber.$mimetype." if $sth->errstr;
-    return $sth->errstr;
-}
-
-=head2 RmPatronImage
-
-    my ($dberror) = RmPatronImage($borrowernumber);
-
-Removes the image for the patron with the supplied borrowernumber.
-
-=cut
-
-sub RmPatronImage {
-    my ($borrowernumber) = @_;
-    warn "Borrowernumber passed to GetPatronImage is $borrowernumber" if $debug;
-    my $dbh = C4::Context->dbh;
-    my $query = "DELETE FROM patronimage WHERE borrowernumber = ?;";
-    my $sth = $dbh->prepare($query);
-    $sth->execute($borrowernumber);
-    my $dberror = $sth->errstr;
-    warn "Database error!" if $sth->errstr;
-    return $dberror;
-}
-
 =head2 GetHideLostItemsPreference
 
   $hidelostitemspref = &GetHideLostItemsPreference($borrowernumber);
@@ -1912,9 +1786,10 @@ sub GetHideLostItemsPreference {
 =head2 GetBorrowersToExpunge
 
   $borrowers = &GetBorrowersToExpunge(
-      not_borrowered_since => $not_borrowered_since,
+      not_borrowed_since => $not_borrowed_since,
       expired_before       => $expired_before,
       category_code        => $category_code,
+      patron_list_id       => $patron_list_id,
       branchcode           => $branchcode
   );
 
@@ -1923,18 +1798,19 @@ sub GetHideLostItemsPreference {
 =cut
 
 sub GetBorrowersToExpunge {
-    my $params = shift;
 
-    my $filterdate     = $params->{'not_borrowered_since'};
-    my $filterexpiry   = $params->{'expired_before'};
-    my $filtercategory = $params->{'category_code'};
-    my $filterbranch   = $params->{'branchcode'} ||
+    my $params = shift;
+    my $filterdate       = $params->{'not_borrowed_since'};
+    my $filterexpiry     = $params->{'expired_before'};
+    my $filtercategory   = $params->{'category_code'};
+    my $filterbranch     = $params->{'branchcode'} ||
                         ((C4::Context->preference('IndependentBranches')
                              && 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;
     my $query = q|
@@ -1950,11 +1826,13 @@ sub GetBorrowersToExpunge {
                 AND guarantorid <> 0
         ) as tmp ON borrowers.borrowernumber=tmp.guarantorid
         LEFT JOIN old_issues USING (borrowernumber)
-        LEFT JOIN issues USING (borrowernumber) 
-        WHERE  category_type <> 'S'
+        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
    |;
-
     my @query_params;
     if ( $filterbranch && $filterbranch ne "" ) {
         $query.= " AND borrowers.branchcode = ? ";
@@ -1968,6 +1846,10 @@ sub GetBorrowersToExpunge {
         $query .= " AND categorycode = ? ";
         push( @query_params, $filtercategory );
     }
+    if ( $filterpatronlist ){
+        $query.=" AND patron_list_id = ? ";
+        push( @query_params, $filterpatronlist );
+    }
     $query.=" GROUP BY borrowers.borrowernumber HAVING currentissue IS NULL ";
     if ( $filterdate ) {
         $query.=" AND ( latestissue < ? OR latestissue IS NULL ) ";
@@ -1978,10 +1860,10 @@ sub GetBorrowersToExpunge {
     my $sth = $dbh->prepare($query);
     if (scalar(@query_params)>0){  
         $sth->execute(@query_params);
-    } 
+    }
     else {
         $sth->execute;
-    }      
+    }
     
     my @results;
     while ( my $data = $sth->fetchrow_hashref ) {
@@ -2108,27 +1990,6 @@ sub GetBorrowersNamesAndLatestIssue {
     return $results;
 }
 
-=head2 ModPrivacy
-
-  my $success = ModPrivacy( $borrowernumber, $privacy );
-
-Update the privacy of a patron.
-
-return :
-true on success, false on failure
-
-=cut
-
-sub ModPrivacy {
-    my $borrowernumber = shift;
-    my $privacy = shift;
-    return unless defined $borrowernumber;
-    return unless $borrowernumber =~ /^\d+$/;
-
-    return ModMember( borrowernumber => $borrowernumber,
-                      privacy        => $privacy );
-}
-
 =head2 IssueSlip
 
   IssueSlip($branchcode, $borrowernumber, $quickslip)