Bug 15407: Koha::Patron::Categories - replace GetBorrowercategoryList
authorJonathan Druart <jonathan.druart@bugs.koha-community.org>
Mon, 21 Dec 2015 17:27:08 +0000 (17:27 +0000)
committerKyle M Hall <kyle@bywatersolutions.com>
Thu, 8 Sep 2016 13:29:22 +0000 (13:29 +0000)
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>

Signed-off-by: Marcel de Rooy <m.de.rooy@rijksmuseum.nl>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

C4/Members.pm
admin/patron-attr-types.pl
reports/issues_stats.pl
reports/reserves_stats.pl
tools/cleanborrowers.pl
tools/import_borrowers.pl
tools/modborrowers.pl

index bb17796..138d244 100644 (file)
@@ -80,7 +80,6 @@ BEGIN {
 
         &GetborCatFromCatType
         GetBorrowerCategorycode
-        &GetBorrowercategoryList
 
         &GetBorrowersToExpunge
         &GetBorrowersWhoHaveNeverBorrowed
@@ -1370,32 +1369,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);
index ceb1184..8d44f70 100755 (executable)
@@ -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,
     );
 
 }
index 0de1a85..48df561 100755 (executable)
@@ -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;
        }
 
index 067b815..b335cf4 100755 (executable)
@@ -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;
index 4cc9169..ccf13ca 100755 (executable)
@@ -39,6 +39,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;
 
@@ -171,12 +172,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,
 );
 
index 8f86d7f..9ab3d90 100755 (executable)
@@ -88,8 +88,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 );
index f165440..a8fbcf1 100755 (executable)
@@ -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;