Bug 23594: Batch modification for itemtypes on suggestions page
[koha-ffzg.git] / suggestion / suggestion.pl
index e04e2ef..bc9c14c 100755 (executable)
@@ -28,7 +28,6 @@ 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;
@@ -84,13 +83,15 @@ my $input           = CGI->new;
 my $redirect  = $input->param('redirect');
 my $suggestedbyme   = (defined $input->param('suggestedbyme')? $input->param('suggestedbyme'):1);
 my $op              = $input->param('op')||'else';
-my @editsuggestions = $input->multi_param('edit_field');
+my @editsuggestions = $input->multi_param('suggestionid');
 my $suggestedby     = $input->param('suggestedby');
 my $returnsuggestedby = $input->param('returnsuggestedby');
 my $returnsuggested = $input->param('returnsuggested');
 my $managedby       = $input->param('managedby');
 my $displayby       = $input->param('displayby') || '';
 my $tabcode         = $input->param('tabcode');
+my $save_confirmed  = $input->param('save_confirmed') || 0;
+
 my $reasonsloop     = GetAuthorisedValues("SUGGEST");
 
 # filter informations which are not suggestion related.
@@ -104,74 +105,96 @@ $suggestion_only->{STATUS} = $suggestion_ref->{STATUS};
 
 delete $$suggestion_ref{$_} foreach qw( suggestedbyme op displayby tabcode edit_field );
 foreach (keys %$suggestion_ref){
-    delete $$suggestion_ref{$_} if (!$$suggestion_ref{$_} && ($op eq 'else' || $op eq 'change'));
+    delete $$suggestion_ref{$_} if (!$$suggestion_ref{$_} && ($op eq 'else' ));
 }
 my ( $template, $borrowernumber, $cookie, $userflags ) = get_template_and_user(
         {
             template_name   => "suggestion/suggestion.tt",
             query           => $input,
             type            => "intranet",
-            flagsrequired   => { acquisition => 'suggestions_manage' },
+            flagsrequired   => { suggestions => 'suggestions_manage' },
         }
     );
 
 $borrowernumber = $input->param('borrowernumber') if ( $input->param('borrowernumber') );
 $template->param('borrowernumber' => $borrowernumber);
+my $branchfilter = $input->param('branchcode') || C4::Context->userenv->{'branch'};
 
 #########################################
 ##  Operations
 ##
+
 if ( $op =~ /save/i ) {
-    $suggestion_only->{suggesteddate} = dt_from_string( $suggestion_only->{suggesteddate} )
-        if $suggestion_only->{suggesteddate};
+    my @messages;
+    my $biblio = MarcRecordFromNewSuggestion({
+            title => $suggestion_only->{title},
+            author => $suggestion_only->{author},
+            itemtype => $suggestion_only->{itemtype},
+    });
+
+    if ( !$suggestion_only->{suggestionid} && ( my ($duplicatebiblionumber, $duplicatetitle) = FindDuplicate($biblio) ) && !$save_confirmed ) {
+        push @messages, { type => 'error', code => 'biblio_exists', id => $duplicatebiblionumber, title => $duplicatetitle };
+        $template->param(
+            messages => \@messages,
+            need_confirm => 1
+        );
+        delete $suggestion_ref->{suggesteddate};
+        Init($suggestion_ref);
+    }
+    else {
 
-    if ( $suggestion_only->{"STATUS"} ) {
-        if ( my $tmpstatus = lc( $suggestion_only->{"STATUS"} ) =~ /ACCEPTED|REJECTED/i ) {
-            $suggestion_only->{ lc( $suggestion_only->{"STATUS"}) . "date" } = dt_from_string;
-            $suggestion_only->{ lc( $suggestion_only->{"STATUS"}) . "by" }   = C4::Context->userenv->{number};
+        for my $date_key ( qw( suggesteddate manageddate accepteddate rejecteddate ) ) {
+            $suggestion_only->{$date_key} = dt_from_string( $suggestion_only->{$date_key} )
+                if $suggestion_only->{$date_key};
         }
-        $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->{"STATUS"} ) {
+            if ( my $tmpstatus = lc( $suggestion_only->{"STATUS"} ) =~ /ACCEPTED|REJECTED/i ) {
+                $suggestion_only->{ lc( $suggestion_only->{"STATUS"}) . "date" } = dt_from_string;
+                $suggestion_only->{ lc( $suggestion_only->{"STATUS"}) . "by" }   = C4::Context->userenv->{number};
+            }
+            $suggestion_only->{manageddate} = dt_from_string;
+            $suggestion_only->{"managedby"}   = C4::Context->userenv->{number};
+        }
 
-    if ( $suggestion_only->{'suggestionid'} > 0 ) {
-        &ModSuggestion($suggestion_only);
-    } else {
-        ###FIXME:Search here if suggestion already exists.
-        my $suggestions_loop =
-            SearchSuggestion( $suggestion_only );
-        if (@$suggestions_loop>=1){
-            #some suggestion are answering the request Donot Add
-            my @messages;
-            for my $suggestion ( @$suggestions_loop ) {
-                push @messages, { type => 'error', code => 'already_exists', id => $suggestion->{suggestionid} };
+        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 {
+            ###FIXME:Search here if suggestion already exists.
+            my $suggestions_loop =
+                SearchSuggestion( $suggestion_only );
+            if (@$suggestions_loop>=1){
+                #some suggestion are answering the request Donot Add
+                my @messages;
+                for my $suggestion ( @$suggestions_loop ) {
+                    push @messages, { type => 'error', code => 'already_exists', id => $suggestion->{suggestionid} };
+                }
+                $template->param( messages => \@messages );
+            }
+            else {
+                ## Adding some informations related to suggestion
+                &NewSuggestion($suggestion_only);
             }
-            $template->param( messages => \@messages );
-        } 
-        else {    
-            ## Adding some informations related to suggestion
-            &NewSuggestion($suggestion_only);
+            # empty fields, to avoid filter in "SearchSuggestion"
         }
-        # empty fields, to avoid filter in "SearchSuggestion"
-    }  
-    map{delete $$suggestion_ref{$_}} keys %$suggestion_ref;
-    $op = 'else';
+        map{delete $$suggestion_ref{$_}} keys %$suggestion_ref;
+        $op = 'else';
 
-    if( $redirect eq 'purchase_suggestions' ) {
-        print $input->redirect("/cgi-bin/koha/members/purchase-suggestions.pl?borrowernumber=$borrowernumber");
+        if( $redirect eq 'purchase_suggestions' ) {
+            print $input->redirect("/cgi-bin/koha/members/purchase-suggestions.pl?borrowernumber=$borrowernumber");
+        }
     }
-
 }
 elsif ($op=~/add/) {
-    #Adds suggestion  
+    #Adds suggestion
     Init($suggestion_ref);
     $op ='save';
-} 
+}
 elsif ($op=~/edit/) {
     #Edit suggestion  
     $suggestion_ref=&GetSuggestion($$suggestion_ref{'suggestionid'});
@@ -187,7 +210,7 @@ elsif ($op=~/edit/) {
     Init($suggestion_ref);
     $op ='save';
 }  
-elsif ($op eq "change" ) {
+elsif ($op eq "update_status" ) {
 
     my $suggestion;
     # set accepted/rejected/managed informations if applicable
@@ -211,9 +234,9 @@ elsif ($op eq "change" ) {
         $suggestion->{managedby}   = C4::Context->userenv->{number};
         $suggestion->{STATUS}      = $STATUS;
     }
-    if ( my $reason = $input->param("reason$tabcode") ) {
+    if ( my $reason = $input->param("reason") ) {
         if ( $reason eq "other" ) {
-            $reason = $input->param("other_reason$tabcode");
+            $reason = $input->param("other_reason");
         }
         $suggestion->{reason} = $reason;
     }
@@ -243,6 +266,13 @@ elsif ($op eq "change" ) {
     }
     $op = 'else';
 }
+elsif ( $op eq 'update_itemtype' ) {
+    my $new_itemtype = $input->param('suggestion_itemtype');
+    foreach my $suggestionid (@editsuggestions) {
+        next unless $suggestionid;
+        &ModSuggestion({ suggestionid => $suggestionid, itemtype => $new_itemtype });
+    }
+}
 elsif ( $op eq 'show' ) {
     $suggestion_ref=&GetSuggestion($$suggestion_ref{'suggestionid'});
     my $budget = GetBudget $$suggestion_ref{budgetid};
@@ -253,7 +283,6 @@ if ($op=~/else/) {
     $op='else';
     
     $displayby||="STATUS";
-    delete $$suggestion_ref{'branchcode'} if($displayby eq "branchcode");
     # distinct values of display by
     my $criteria_list=GetDistinctValues("suggestions.".$displayby);
     my (@criteria_dv, $criteria_has_empty);
@@ -267,6 +296,14 @@ if ($op=~/else/) {
     # aggregate null and empty values under empty value
     push @criteria_dv, '' if $criteria_has_empty;
 
+    # Hack to not modify GetDistinctValues for this specific case
+    if (   $displayby eq 'branchcode'
+        && C4::Context->preference('IndependentBranches')
+        && not C4::Context->IsSuperLibrarian )
+    {
+        @criteria_dv = ( C4::Context->userenv->{'branch'} );
+    }
+
     my @allsuggestions;
     foreach my $criteriumvalue ( @criteria_dv ) {
         # By default, display suggestions from current working branch
@@ -275,7 +312,7 @@ if ($op=~/else/) {
         }
         my $definedvalue = defined $$suggestion_ref{$displayby} && $$suggestion_ref{$displayby} ne "";
 
-        next if ( $definedvalue && $$suggestion_ref{$displayby} ne $criteriumvalue );
+        next if ( $definedvalue && $$suggestion_ref{$displayby} ne $criteriumvalue ) and ($displayby ne 'branchcode' or $branchfilter ne '__ANY__' );
         $$suggestion_ref{$displayby} = $criteriumvalue;
 
         my $suggestions = &SearchSuggestion($suggestion_ref);
@@ -330,8 +367,6 @@ if(defined($returnsuggested) and $returnsuggested ne "noone")
     print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=".$returnsuggested."#suggestions");
 }
 
-my $branchfilter = ($displayby ne "branchcode") ? $input->param('branchcode') : C4::Context->userenv->{'branch'};
-
 $template->param(
     branchfilter => $branchfilter,
 );
@@ -342,14 +377,7 @@ my $patron_reason_loop = GetAuthorisedValues("OPAC_SUG");
 $template->param(patron_reason_loop=>$patron_reason_loop);
 
 #Budgets management
-my $budgets = [];
-if ($branchfilter) {
-    my $searchbudgets = { budget_branchcode => $branchfilter };
-    $budgets = GetBudgets($searchbudgets);
-} else {
-    $budgets = GetBudgets(undef);
-}
-
+my $budgets = GetBudgets;
 my @budgets_loop;
 foreach my $budget ( @{$budgets} ) {
     next unless (CanUserUseBudget($borrowernumber, $budget, $userflags));