Bug 15163: Do not erase patron attributes if limited to another library
[koha.git] / C4 / Members / Attributes.pm
index b25a813..2f560c7 100644 (file)
@@ -71,7 +71,6 @@ marked for OPAC display are returned.
 sub GetBorrowerAttributes {
     my $borrowernumber = shift;
     my $opac_only = @_ ? shift : 0;
-    my $branch_limit = @_ ? shift : 0;
 
     my $dbh = C4::Context->dbh();
     my $query = "SELECT code, description, attribute, lib, password, display_checkout, category_code, class
@@ -218,10 +217,11 @@ replacing any that existed previously.
 sub SetBorrowerAttributes {
     my $borrowernumber = shift;
     my $attr_list = shift;
+    my $no_branch_limit = shift // 0;
 
     my $dbh = C4::Context->dbh;
-    my $delsth = $dbh->prepare("DELETE FROM borrower_attributes WHERE borrowernumber = ?");
-    $delsth->execute($borrowernumber);
+
+    DeleteBorrowerAttributes( $borrowernumber, $no_branch_limit );
 
     my $sth = $dbh->prepare("INSERT INTO borrower_attributes (borrowernumber, code, attribute, password)
                              VALUES (?, ?, ?, ?)");
@@ -236,6 +236,39 @@ sub SetBorrowerAttributes {
     return 1; # borrower attributes successfully set
 }
 
+=head2 DeleteBorrowerAttributes
+
+  DeleteBorrowerAttributes($borrowernumber);
+
+Delete borrower attributes for the patron identified by C<$borrowernumber>.
+
+=cut
+
+sub DeleteBorrowerAttributes {
+    my $borrowernumber = shift;
+    my $no_branch_limit = @_ ? shift : 0;
+    my $branch_limit = $no_branch_limit
+        ? 0
+        : C4::Context->userenv ? C4::Context->userenv->{"branch"} : 0;
+
+    my $dbh = C4::Context->dbh;
+    my $query = q{
+        DELETE borrower_attributes FROM borrower_attributes
+        };
+
+    $query .= $branch_limit
+        ? q{
+            LEFT JOIN borrower_attribute_types_branches ON bat_code = code
+            WHERE b_branchcode = ? OR b_branchcode IS NULL
+                AND borrowernumber = ?
+        }
+        : q{
+            WHERE borrowernumber = ?
+        };
+
+    $dbh->do( $query, undef, $branch_limit ? $branch_limit : (), $borrowernumber );
+}
+
 =head2 DeleteBorrowerAttribute
 
   DeleteBorrowerAttribute($borrowernumber, $attribute);