updated release notes for 3.14.0 beta
[koha.git] / members / member.pl
index bef3323..ff6e41a 100755 (executable)
 # with Koha; if not, write to the Free Software Foundation, Inc.,
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
-use strict;
-#use warnings; FIXME - Bug 2505
+use Modern::Perl;
 use C4::Auth;
 use C4::Output;
 use CGI;
 use C4::Members;
 use C4::Branch;
 use C4::Category;
+use Koha::DateUtils;
 use File::Basename;
+use Koha::List::Patron;
 
 my $input = new CGI;
-my $quicksearch = $input->param('quicksearch');
-my $startfrom = $input->param('startfrom')||1;
-my $resultsperpage = $input->param('resultsperpage')||C4::Context->preference("PatronsPerPage")||20;
+my $quicksearch = $input->param('quicksearch') || '';
+my $startfrom = $input->param('startfrom') || 1;
+my $resultsperpage = $input->param('resultsperpage') || C4::Context->preference("PatronsPerPage") || 20;
 
 my ($template, $loggedinuser, $cookie)
     = get_template_and_user({template_name => "members/member.tmpl",
@@ -48,6 +49,15 @@ my ($template, $loggedinuser, $cookie)
 
 my $theme = $input->param('theme') || "default";
 
+my $add_to_patron_list       = $input->param('add_to_patron_list');
+my $add_to_patron_list_which = $input->param('add_to_patron_list_which');
+my $new_patron_list          = $input->param('new_patron_list');
+my @borrowernumbers          = $input->param('borrowernumber');
+$input->delete(
+    'add_to_patron_list', 'add_to_patron_list_which',
+    'new_patron_list',    'borrowernumber',
+);
+
 my $patron = $input->Vars;
 foreach (keys %$patron){
        delete $$patron{$_} unless($$patron{$_});
@@ -59,7 +69,7 @@ my @branchloop;
 
 foreach (sort { $branches->{$a}->{branchname} cmp $branches->{$b}->{branchname} } keys %$branches) {
   my $selected;
-  $selected = 1 if $branches->{$_}->{branchcode} eq $$patron{branchcode};
+  $selected = 1 if $patron->{branchcode} && $branches->{$_}->{branchcode} eq $patron->{branchcode};
   my %row = ( value => $_,
         selected => $selected,
         branchname => $branches->{$_}->{branchname},
@@ -84,8 +94,8 @@ if ($AddPatronLists=~/code/){
     $categories[0]->{'first'}=1;
 }  
 
-my $member=$input->param('member');
-my $orderbyparams=$input->param('orderby');
+my $member=$input->param('member') || '';
+my $orderbyparams=$input->param('orderby') || '';
 my @orderby;
 if ($orderbyparams){
        my @orderbyelt=split(/,/,$orderbyparams);
@@ -95,9 +105,6 @@ else {
        @orderby = ({surname=>0},{firstname=>0});
 }
 
-my $searchfields = $input->param('searchfields');
-my @searchfields = $searchfields ? split( ',', $searchfields ) : ( "firstname", "surname", "othernames", "cardnumber", "userid", "email" );
-
 $member =~ s/,//g;   #remove any commas from search string
 $member =~ s/\*/%/g;
 
@@ -106,17 +113,53 @@ my $to   = $from + $resultsperpage;
 
 my ($count,$results);
 if ($member || keys %$patron) {
-    #($results)=Search($member || $patron,{surname=>1,firstname=>1},[$from,$to],undef,["firstname","surname","email","othernames"]  );
-    my $search_scope = ( $quicksearch ? "field_start_with" : "start_with" );
+    my $searchfields = $input->param('searchfields') || '';
+    my @searchfields = $searchfields ? split( ',', $searchfields ) : ( "firstname", "surname", "othernames", "cardnumber", "userid", "email" );
+
+    if ( $searchfields eq "dateofbirth" ) {
+        $member = output_pref({dt => dt_from_string($member), dateformat => 'iso', dateonly => 1});
+    }
+
+    my $searchtype = $input->param('searchtype');
+    my $search_scope =
+        $quicksearch ? "field_start_with"
+      : $searchtype  ? $searchtype
+      :                "start_with";
+
     ($results) = Search( $member || $patron, \@orderby, undef, undef, \@searchfields, $search_scope );
 }
 
+if ($add_to_patron_list) {
+    my $patron_list;
+
+    if ( $add_to_patron_list eq 'new' ) {
+        $patron_list = AddPatronList( { name => $new_patron_list } );
+    }
+    else {
+        $patron_list =
+          [ GetPatronLists( { patron_list_id => $add_to_patron_list } ) ]->[0];
+    }
+
+    if ( $add_to_patron_list_which eq 'all' ) {
+        @borrowernumbers = map { $_->{borrowernumber} } @$results;
+    }
+
+    my @patrons_added_to_list = AddPatronsToList( { list => $patron_list, borrowernumbers => \@borrowernumbers } );
+
+    $template->param(
+        patron_list           => $patron_list,
+        patrons_added_to_list => \@patrons_added_to_list,
+      )
+}
+
 if ($results) {
        for my $field ('categorycode','branchcode'){
                next unless ($patron->{$field});
                @$results = grep { $_->{$field} eq $patron->{$field} } @$results; 
        }
     $count = scalar(@$results);
+} else {
+    $count = 0;
 }
 
 if($count == 1){
@@ -130,17 +173,18 @@ my $index=$from;
 foreach my $borrower(@$results[$from..$to-1]){
   #find out stats
   my ($od,$issue,$fines)=GetMemberIssuesAndFines($$borrower{'borrowernumber'});
-
+  $fines ||= 0;
   $$borrower{'dateexpiry'}= C4::Dates->new($$borrower{'dateexpiry'},'iso')->output('syspref');
 
   my %row = (
     count => $index++,
-       %$borrower,
-       %{$categories_dislay{$$borrower{categorycode}}},
+    %$borrower,
+    (defined $categories_dislay{ $borrower->{categorycode} }?   %{ $categories_dislay{ $borrower->{categorycode} } }:()),
     overdues => $od,
     issues => $issue,
     odissue => "$od/$issue",
     fines =>  sprintf("%.2f",$fines),
+    branchname => $branches->{$borrower->{branchcode}}->{branchname},
     );
   push(@resultsdata, \%row);
 }
@@ -165,24 +209,29 @@ my $base_url =
 my @letters = map { {letter => $_} } ( 'A' .. 'Z');
 
 $template->param(
-    letters => \@letters,
+    %$patron,
+    letters       => \@letters,
     paginationbar => pagination_bar(
         $base_url,
-        int( $count / $resultsperpage ) + ($count % $resultsperpage ? 1 : 0),
-        $startfrom, 'startfrom'
+        int( $count / $resultsperpage ) + ( $count % $resultsperpage ? 1 : 0 ),
+        $startfrom,
+        'startfrom'
     ),
-    startfrom => $startfrom,
-    from      => ($startfrom-1)*$resultsperpage+1,  
-    to        => $to,
-    multipage => ($count != $to || $startfrom!=1),
-    advsearch => ($$patron{categorycode} || $$patron{branchcode}),
-    branchloop=>\@branchloop,
-    categories=>\@categories,
-    searching       => "1",
-               actionname              =>basename($0),
-               %$patron,
-        numresults      => $count,
-        resultsloop     => \@resultsdata,
-            );
+    startfrom    => $startfrom,
+    from         => ( $startfrom - 1 ) * $resultsperpage + 1,
+    to           => $to,
+    multipage    => ( $count != $to || $startfrom != 1 ),
+    advsearch    => ( $$patron{categorycode} || $$patron{branchcode} ),
+    branchloop   => \@branchloop,
+    categories   => \@categories,
+    searching    => "1",
+    actionname   => basename($0),
+    numresults   => $count,
+    resultsloop  => \@resultsdata,
+    results_per_page => $resultsperpage,
+    member => $member,
+    search_parameters => \%parameters,
+    patron_lists => [ GetPatronLists() ],
+);
 
 output_html_with_http_headers $input, $cookie, $template->output;