From 949937263de39db8895ba877a85a3c844bc10667 Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Date: Mon, 21 Dec 2015 17:27:08 +0000
Subject: [PATCH] Bug 15407: Koha::Patron::Categories - replace
 GetBorrowercategoryList

C4::Members::GetBorrowercategoryList returned all the patron categories,
ordered by description.
This can be done using the search method of the Koha::Patron::Categories
module.

Test plan:
- Same prerequisite as before
For the following pages, you should not see patron categories limited to other
libraries. They should be ordered as before this patch, by description.
- On the "patron attribute types" admin page (admin/patron-attr-types.pl).
- On the "Batch patron deletion/anonymization"
  (tools/cleanborrowers.pl), the dropdown list should be filled
  correctly.
- On the import patron tool
- On the following report:
    reports/reserves_stats.pl
Confirm that the patron categories are displayed and that the report
still works as before.

Signed-off-by: Chris Cormack <chris@bigballofwax.co.nz>
---
 C4/Members.pm              | 27 ---------------------------
 admin/patron-attr-types.pl |  9 ++++++---
 reports/issues_stats.pl    | 28 ++++++++++++++--------------
 reports/reserves_stats.pl  | 11 ++++++-----
 tools/cleanborrowers.pl    |  5 ++++-
 tools/import_borrowers.pl  |  4 ++--
 tools/modborrowers.pl      |  9 ++++-----
 7 files changed, 36 insertions(+), 57 deletions(-)

diff --git a/C4/Members.pm b/C4/Members.pm
index ca56296..d10a18f 100644
--- a/C4/Members.pm
+++ b/C4/Members.pm
@@ -82,7 +82,6 @@ BEGIN {
 
         &GetborCatFromCatType
         GetBorrowerCategorycode
-        &GetBorrowercategoryList
 
         &GetBorrowersToExpunge
         &GetBorrowersWhoHaveNeverBorrowed
@@ -1474,32 +1473,6 @@ sub GetBorrowerCategorycode {
     return $sth->fetchrow;
 }
 
-=head2 GetBorrowercategoryList
-
-  $arrayref_hashref = &GetBorrowercategoryList;
-If no category code provided, the function returns all the categories.
-
-=cut
-
-sub GetBorrowercategoryList {
-    my $no_branch_limit = @_ ? shift : 0;
-    my $branch_limit = $no_branch_limit
-        ? 0
-        : C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
-    my $dbh       = C4::Context->dbh;
-    my $query = "SELECT categories.* FROM categories";
-    $query .= qq{
-        LEFT JOIN categories_branches ON categories.categorycode = categories_branches.categorycode
-        WHERE branchcode = ? OR branchcode IS NULL GROUP BY description
-    } if $branch_limit;
-    $query .= " ORDER BY description";
-    my $sth = $dbh->prepare( $query );
-    $sth->execute( $branch_limit ? $branch_limit : () );
-    my $data = $sth->fetchall_arrayref( {} );
-    $sth->finish;
-    return $data;
-}    # sub getborrowercategory
-
 =head2 GetAge
 
   $dateofbirth,$date = &GetAge($date);
diff --git a/admin/patron-attr-types.pl b/admin/patron-attr-types.pl
index ceb1184..8d44f70 100755
--- a/admin/patron-attr-types.pl
+++ b/admin/patron-attr-types.pl
@@ -29,9 +29,10 @@ use C4::Branch;
 use C4::Context;
 use C4::Output;
 use C4::Koha;
-use C4::Members qw/GetBorrowercategoryList/;
 use C4::Members::AttributeTypes;
 
+use Koha::Patron::Categories;
+
 my $script_name = "/cgi-bin/koha/admin/patron-attr-types.pl";
 
 our $input = new CGI;
@@ -92,10 +93,11 @@ sub add_attribute_type_form {
         };
     }
 
+    my $patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['description']});
     $template->param(
         attribute_type_form => 1,
         confirm_op => 'add_attribute_type_confirmed',
-        categories => GetBorrowercategoryList,
+        categories => $patron_categories,
         branches_loop => \@branches_loop,
     );
     authorised_value_category_list($template);
@@ -269,11 +271,12 @@ sub edit_attribute_type_form {
         category_description => $attr_type->category_description,
     );
 
+    my @patron_categories = Koha::Patron::Categories->search({}, {order_by => ['description']});
     $template->param(
         attribute_type_form => 1,
         edit_attribute_type => 1,
         confirm_op => 'edit_attribute_type_confirmed',
-        categories => GetBorrowercategoryList,
+        categories => \@patron_categories,
     );
 
 }
diff --git a/reports/issues_stats.pl b/reports/issues_stats.pl
index 0de1a85..48df561 100755
--- a/reports/issues_stats.pl
+++ b/reports/issues_stats.pl
@@ -76,7 +76,7 @@ $template->param(do_it => $do_it,
 );
 
 our $itemtypes = GetItemTypes();
-our $categoryloop = GetBorrowercategoryList;
+our @patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['description']});
 
 our $ccodes    = GetKohaAuthorisedValues("items.ccode");
 our $locations = GetKohaAuthorisedValues("items.location");
@@ -152,7 +152,7 @@ my $CGIextChoice = ( 'CSV' ); # FIXME translation
 my $CGIsepChoice=GetDelimiterChoices;
  
 $template->param(
-	categoryloop => $categoryloop,
+    categoryloop => \@patron_categories,
 	itemtypeloop => \@itemtypeloop,
 	locationloop => \@locations,
 	   ccodeloop => \@ccodes,
@@ -322,12 +322,12 @@ sub calculate {
 				($celvalue eq $_->{authorised_value}) or next;
 				$cell{rowtitle_display} = $_->{lib} and last;
 			}
-		} elsif ($line =~ /category/) {
-			foreach (@$categoryloop) {
-				($celvalue eq $_->{categorycode}) or next;
-				$cell{rowtitle_display} = $_->{description} and last;
-			}
-		}
+        } elsif ($line =~ /category/) {
+            foreach my $patron_category ( @patron_categories ) {
+                ($celvalue eq $patron_category->categorycode) or next;
+                $cell{rowtitle_display} = $patron_category->description and last;
+            }
+        }
 		push @loopline, \%cell;
 	}
 
@@ -396,12 +396,12 @@ sub calculate {
 				($celvalue eq $_->{authorised_value}) or next;
 				$cell{coltitle_display} = $_->{lib} and last;
 			}
-		} elsif ($column =~ /category/) {
-			foreach (@$categoryloop) {
-				($celvalue eq $_->{categorycode}) or next;
-				$cell{coltitle_display} = $_->{description} and last;
-			}
-		}
+        } elsif ($column =~ /category/) {
+            foreach my $patron_category ( @patron_categories ) {
+                ($celvalue eq $patron_category->categorycode) or next;
+                $cell{coltitle_display} = $patron_category->description and last;
+            }
+        }
 		push @loopcol, \%cell;
 	}
 
diff --git a/reports/reserves_stats.pl b/reports/reserves_stats.pl
index 067b815..b335cf4 100755
--- a/reports/reserves_stats.pl
+++ b/reports/reserves_stats.pl
@@ -32,6 +32,7 @@ use C4::Output;
 use C4::Reports;
 use C4::Members;
 use Koha::DateUtils;
+use Koha::Patron::Categories;
 use List::MoreUtils qw/any/;
 use YAML;
 
@@ -80,7 +81,7 @@ $template->param(do_it => $do_it,
 );
 
 my $itemtypes = GetItemTypes();
-my $categoryloop = GetBorrowercategoryList;
+my @patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['description']});
 
 my $ccodes    = GetKohaAuthorisedValues("items.ccode");
 my $locations = GetKohaAuthorisedValues("items.location");
@@ -158,7 +159,7 @@ my $CGIextChoice = ( 'CSV' ); # FIXME translation
 my $CGIsepChoice=GetDelimiterChoices;
  
 $template->param(
-	categoryloop => $categoryloop,
+    categoryloop => \@patron_categories,
 	itemtypeloop => \@itemtypeloop,
 	locationloop => \@locations,
 	   ccodeloop => \@ccodes,
@@ -353,9 +354,9 @@ sub display_value {
         }
     }
     elsif ( $crit =~ /category/ ) {
-        foreach (@$categoryloop) {
-            ( $value eq $_->{categorycode} ) or next;
-            $display_value = $_->{description} and last;
+        foreach my $patron_category ( @patron_categories ) {
+            ( $value eq $patron_category->categorycode ) or next;
+            $display_value = $patron_category->description and last;
         }
     }
     return $display_value;
diff --git a/tools/cleanborrowers.pl b/tools/cleanborrowers.pl
index d108d53..b349ee6 100755
--- a/tools/cleanborrowers.pl
+++ b/tools/cleanborrowers.pl
@@ -40,6 +40,7 @@ use C4::Output;
 use C4::Members;        # GetBorrowersWhoHavexxxBorrowed.
 use C4::Circulation;    # AnonymiseIssueHistory.
 use Koha::DateUtils qw( dt_from_string output_pref );
+use Koha::Patron::Categories;
 use Date::Calc qw/Today Add_Delta_YM/;
 use Koha::List::Patron;
 
@@ -161,12 +162,14 @@ elsif ( $step == 3 ) {
     $template->param( patron_lists => [ @non_empty_lists ] );
 }
 
+my $patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['description']});
+
 $template->param(
     step                   => $step,
     not_borrowed_since   => $not_borrowed_since,
     borrower_dateexpiry    => $borrower_dateexpiry,
     last_issue_date        => $last_issue_date,
-    borrower_categorycodes => GetBorrowercategoryList(),
+    borrower_categorycodes => $patron_categories,
     borrower_categorycode  => $borrower_categorycode,
 );
 
diff --git a/tools/import_borrowers.pl b/tools/import_borrowers.pl
index e422a67..be5f006 100755
--- a/tools/import_borrowers.pl
+++ b/tools/import_borrowers.pl
@@ -85,8 +85,8 @@ my ( $template, $loggedinuser, $cookie ) = get_template_and_user({
 my $branches = GetBranchesLoop();
 $template->param( branches => $branches ) if ( $branches );
 # get the patron categories and pass them to the template
-my $categories = GetBorrowercategoryList();
-$template->param( categories => $categories ) if ( $categories );
+my @patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['description']});
+$template->param( categories => \@patron_categories );
 my $columns = C4::Templates::GetColumnDefs( $input )->{borrowers};
 $columns = [ grep { $_->{field} ne 'borrowernumber' ? $_ : () } @$columns ];
 $template->param( borrower_fields => $columns );
diff --git a/tools/modborrowers.pl b/tools/modborrowers.pl
index c904f09..d54ab77 100755
--- a/tools/modborrowers.pl
+++ b/tools/modborrowers.pl
@@ -105,7 +105,7 @@ if ( $op eq 'show' ) {
     my @patron_attributes_values;
     my @patron_attributes_codes;
     my $patron_attribute_types = C4::Members::AttributeTypes::GetAttributeTypes_hashref('all');
-    my $patron_categories = C4::Members::GetBorrowercategoryList;
+    my @patron_categories = Koha::Patron::Categories->search_limited({}, {order_by => ['description']});
     for ( values %$patron_attribute_types ) {
         my $attr_type = C4::Members::AttributeTypes->fetch( $_->{code} );
         # TODO Repeatable attributes are not correctly managed and can cause data lost.
@@ -123,8 +123,8 @@ if ( $op eq 'show' ) {
 
         my $category_code = $_->{category_code};
         my ( $category_lib ) = map {
-            ( defined $category_code and $_->{categorycode} eq $category_code ) ? $_->{description} : ()
-        } @$patron_categories;
+            ( defined $category_code and $_->categorycode eq $category_code ) ? $_->description : ()
+        } @patron_categories;
         push @patron_attributes_codes,
             {
                 attribute_code => $_->{code},
@@ -149,9 +149,8 @@ if ( $op eq 'show' ) {
     my @branches_option;
     push @branches_option, { value => $_->{value}, lib => $_->{branchname} } for @$branches;
     unshift @branches_option, { value => "", lib => "" };
-    my $categories = GetBorrowercategoryList;
     my @categories_option;
-    push @categories_option, { value => $_->{categorycode}, lib => $_->{description} } for @$categories;
+    push @categories_option, { value => $_->categorycode, lib => $_->description } for @patron_categories;
     unshift @categories_option, { value => "", lib => "" };
     my $bsort1 = GetAuthorisedValues("Bsort1");
     my @sort1_option;
-- 
2.8.1