Bug 22905: (QA follow-up) remove bad warn
[srvgit] / suggestion / suggestion.pl
index 472ab64..ed04308 100755 (executable)
 # You should have received a copy of the GNU General Public License
 # along with Koha; if not, see <http://www.gnu.org/licenses>.
 
-use strict;
-#use warnings; FIXME - Bug 2505
+use Modern::Perl;
 require Exporter;
 use CGI qw ( -utf8 );
 use C4::Auth;    # get_template_and_user
 use C4::Output;
 use C4::Suggestions;
-use C4::Koha; #GetItemTypes
+use C4::Koha;
 use C4::Budgets;
 use C4::Search;
 use C4::Members;
 use C4::Debug;
 
 use Koha::DateUtils qw( dt_from_string );
+use Koha::AuthorisedValues;
 use Koha::Acquisition::Currencies;
 use Koha::Libraries;
+use Koha::Patrons;
 
 use URI::Escape;
 
@@ -56,17 +57,21 @@ sub GetCriteriumDesc{
     my ($criteriumvalue,$displayby)=@_;
     if ($displayby =~ /status/i) {
         unless ( grep { /$criteriumvalue/ } qw(ASKED ACCEPTED REJECTED CHECKED ORDERED AVAILABLE) ) {
-            return GetAuthorisedValueByCode('SUGGEST_STATUS', $criteriumvalue ) || "Unknown";
+            my $av = Koha::AuthorisedValues->search({ category => 'SUGGEST_STATUS', authorised_value => $criteriumvalue });
+            return $av->count ? $av->next->lib : 'Unknown';
         }
         return ($criteriumvalue eq 'ASKED'?"Pending":ucfirst(lc( $criteriumvalue))) if ($displayby =~/status/i);
     }
     return Koha::Libraries->find($criteriumvalue)->branchname
         if $displayby =~ /branchcode/;
-    return GetAuthorisedValueByCode('SUGGEST_FORMAT', $criteriumvalue) || "Unknown" if ($displayby =~/itemtype/);
+    if ( $displayby =~ /itemtype/ ) {
+        my $av = Koha::AuthorisedValues->search({ category => 'SUGGEST_FORMAT', authorised_value => $criteriumvalue });
+        return $av->count ? $av->next->lib : 'Unknown';
+    }
     if ($displayby =~/suggestedby/||$displayby =~/managedby/||$displayby =~/acceptedby/){
-        my $borr=C4::Members::GetMember(borrowernumber=>$criteriumvalue);
-        return "" unless $borr;
-        return $$borr{surname} . ", " . $$borr{firstname};
+        my $patron = Koha::Patrons->find( $criteriumvalue );
+        return "" unless $patron;
+        return $patron->surname . ", " . $patron->firstname;
     }
     if ( $displayby =~ /budgetid/) {
         my $budget = GetBudget($criteriumvalue);
@@ -86,6 +91,7 @@ my $returnsuggested = $input->param('returnsuggested');
 my $managedby       = $input->param('managedby');
 my $displayby       = $input->param('displayby') || '';
 my $tabcode         = $input->param('tabcode');
+my $reasonsloop     = GetAuthorisedValues("SUGGEST");
 
 # filter informations which are not suggestion related.
 my $suggestion_ref  = $input->Vars;
@@ -105,7 +111,7 @@ my ( $template, $borrowernumber, $cookie, $userflags ) = get_template_and_user(
             template_name   => "suggestion/suggestion.tt",
             query           => $input,
             type            => "intranet",
-            flagsrequired   => { catalogue => 1 },
+            flagsrequired   => { acquisition => 'suggestions_manage' },
         }
     );
 
@@ -127,6 +133,12 @@ if ( $op =~ /save/i ) {
         $suggestion_only->{manageddate} = dt_from_string;
         $suggestion_only->{"managedby"}   = C4::Context->userenv->{number};
     }
+
+    my $otherreason = $input->param('other_reason');
+    if ($suggestion_only->{reason} eq 'other' && $otherreason) {
+        $suggestion_only->{reason} = $otherreason;
+    }
+
     if ( $suggestion_only->{'suggestionid'} > 0 ) {
         &ModSuggestion($suggestion_only);
     } else {
@@ -163,34 +175,55 @@ elsif ($op=~/add/) {
 elsif ($op=~/edit/) {
     #Edit suggestion  
     $suggestion_ref=&GetSuggestion($$suggestion_ref{'suggestionid'});
+    $suggestion_ref->{reasonsloop} = $reasonsloop;
+    my $other_reason = 1;
+    foreach my $reason ( @{ $reasonsloop } ) {
+        if ($suggestion_ref->{reason} eq $reason->{lib}) {
+            $other_reason = 0;
+        }
+    }
+    $other_reason = 0 unless $suggestion_ref->{reason};
+    $template->param(other_reason => $other_reason);
     Init($suggestion_ref);
     $op ='save';
 }  
 elsif ($op eq "change" ) {
+
+    my $suggestion;
     # set accepted/rejected/managed informations if applicable
     # ie= if the librarian has chosen some action on the suggestions
-    if ($suggestion_only->{"STATUS"} eq "ACCEPTED"){
-        $suggestion_only->{accepteddate} = dt_from_string;
-        $suggestion_only->{"acceptedby"}=C4::Context->userenv->{number};
-    } elsif ($suggestion_only->{"STATUS"} eq "REJECTED"){
-        $suggestion_only->{rejecteddate} = dt_from_string;
-        $suggestion_only->{"rejectedby"}=C4::Context->userenv->{number};
+    my $STATUS      = $input->param('STATUS');
+    my $accepted_by = $input->param('acceptedby');
+    if ( $STATUS eq "ACCEPTED" ) {
+        $suggestion = {
+            STATUS       => $STATUS,
+            accepteddate => dt_from_string,
+            acceptedby => C4::Context->userenv->{number},
+        };
     }
-    if ($suggestion_only->{"STATUS"}){
-        $suggestion_only->{manageddate} = dt_from_string;
-        $suggestion_only->{"managedby"}=C4::Context->userenv->{number};
+    elsif ( $STATUS eq "REJECTED" ) {
+        $suggestion = {
+
+            STATUS       => $STATUS,
+            rejecteddate => dt_from_string,
+            rejectedby   => C4::Context->userenv->{number},
+        };
+    }
+    if ($STATUS) {
+        $suggestion->{manageddate} = dt_from_string;
+        $suggestion->{managedby}   = C4::Context->userenv->{number};
     }
-    if ( my $reason = $$suggestion_ref{"reason$tabcode"}){
+    if ( my $reason = $input->param("reason$tabcode") ) {
         if ( $reason eq "other" ) {
-            $reason = $$suggestion_ref{"other_reason$tabcode"};
+            $reason = $input->param("other_reason$tabcode");
         }
-        $suggestion_only->{reason}=$reason;
+        $suggestion->{reason} = $reason;
     }
 
     foreach my $suggestionid (@editsuggestions) {
         next unless $suggestionid;
-        $suggestion_only->{'suggestionid'}=$suggestionid;
-        &ModSuggestion($suggestion_only);
+        $suggestion->{suggestionid} = $suggestionid;
+        &ModSuggestion($suggestion);
     }
     my $params = '';
     foreach my $key (
@@ -237,7 +270,6 @@ if ($op=~/else/) {
     push @criteria_dv, '' if $criteria_has_empty;
 
     my @allsuggestions;
-    my $reasonsloop = GetAuthorisedValues("SUGGEST");
     foreach my $criteriumvalue ( @criteria_dv ) {
         # By default, display suggestions from current working branch
         unless ( exists $$suggestion_ref{'branchcode'} ) {
@@ -276,14 +308,16 @@ if ($op=~/else/) {
 foreach my $element ( qw(managedby suggestedby acceptedby) ) {
 #    $debug || warn $$suggestion_ref{$element};
     if ($$suggestion_ref{$element}){
-        my $member=GetMember(borrowernumber=>$$suggestion_ref{$element});
+        my $patron = Koha::Patrons->find( $$suggestion_ref{$element} );
+        my $category = $patron->category;
         $template->param(
-            $element."_borrowernumber"=>$$member{borrowernumber},
-            $element."_firstname"=>$$member{firstname},
-            $element."_surname"=>$$member{surname},
-            $element."_branchcode"=>$$member{branchcode},
-            $element."_description"=>$$member{description},
-            $element."_category_type"=>$$member{category_type}
+            $element."_borrowernumber"=>$patron->borrowernumber,
+            $element."_firstname"=>$patron->firstname,
+            $element."_surname"=>$patron->surname,
+            $element."_cardnumber"=>$patron->cardnumber,
+            $element."_branchcode"=>$patron->branchcode,
+            $element."_description"=>$category->description,
+            $element."_category_type"=>$category->category_type,
         );
     }
 }
@@ -331,7 +365,12 @@ foreach my $budget ( @{$budgets} ) {
 }
 
 $template->param( budgetsloop => \@budgets_loop);
-$template->param( "statusselected_$$suggestion_ref{'STATUS'}" =>1) if ($$suggestion_ref{'STATUS'});
+if( $suggestion_ref->{STATUS} ) {
+    $template->param(
+        "statusselected_".$suggestion_ref->{STATUS} => 1,
+        selected_status => $suggestion_ref->{STATUS}, # We need template var selected_status in the second part of the template where template var suggestion.STATUS is out of scope
+    );
+}
 
 my @currencies = Koha::Acquisition::Currencies->search;
 $template->param(