X-Git-Url: http://koha-dev.rot13.org:8081/gitweb/?a=blobdiff_plain;f=suggestion%2Fsuggestion.pl;h=0e5ec8f80a757f85206f3d5e60f64a4000d38a8c;hb=dc42d6a62d637dd342533b9fd89befa1cf1478df;hp=99f38eb04e18b7f49c8c2cb1e5494e06cc5788af;hpb=31a0ed0a43bb4ecfde0b762eb6e654c51da6f66e;p=koha-ffzg.git diff --git a/suggestion/suggestion.pl b/suggestion/suggestion.pl index 99f38eb04e..0e5ec8f80a 100755 --- a/suggestion/suggestion.pl +++ b/suggestion/suggestion.pl @@ -4,67 +4,73 @@ # Copyright 2006-2010 BibLibre # -# Koha is free software; you can redistribute it and/or modify it under the -# terms of the GNU General Public License as published by the Free Software -# Foundation; either version 2 of the License, or (at your option) any later -# version. +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. # -# Koha is distributed in the hope that it will be useful, but WITHOUT ANY -# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR -# A PARTICULAR PURPOSE. See the GNU General Public License for more details. +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. # -# You should have received a copy of the GNU General Public License along -# with Koha; if not, write to the Free Software Foundation, Inc., -# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . -use strict; -#use warnings; FIXME - Bug 2505 +use Modern::Perl; require Exporter; -use CGI; -use C4::Auth; # get_template_and_user -use C4::Output; +use CGI qw ( -utf8 ); +use C4::Auth qw( get_template_and_user ); +use C4::Output qw( output_html_with_http_headers ); use C4::Suggestions; -use C4::Koha; #GetItemTypes -use C4::Branch; -use C4::Budgets; -use C4::Search; -use C4::Dates qw(format_date); +use C4::Koha qw( GetAuthorisedValues ); +use C4::Budgets qw( GetBudget GetBudgets GetBudgetHierarchy CanUserUseBudget ); +use C4::Search qw( FindDuplicate GetDistinctValues ); 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 qw( uri_escape ); sub Init{ my $suggestion= shift @_; # "Managed by" is used only when a suggestion is being edited (not when created) - if ($suggestion->{'suggesteddate'} eq "0000-00-00" ||$suggestion->{'suggesteddate'} eq "") { + if ($suggestion->{'suggesteddate'} eq "") { # new suggestion - $suggestion->{'suggesteddate'} = C4::Dates->today; + $suggestion->{suggesteddate} = dt_from_string; $suggestion->{'suggestedby'} = C4::Context->userenv->{"number"} unless ($suggestion->{'suggestedby'}); } else { # editing of an existing suggestion - $suggestion->{'manageddate'} = C4::Dates->today; + $suggestion->{manageddate} = dt_from_string; $suggestion->{'managedby'} = C4::Context->userenv->{"number"} unless ($suggestion->{'managedby'}); - # suggesteddate, when coming from the DB, needs to be formated - $suggestion->{'suggesteddate'} = format_date($suggestion->{'suggesteddate'}); - } - foreach my $date ( qw(rejecteddate accepteddate) ){ - $suggestion->{$date}=(($suggestion->{$date} eq "0000-00-00" ||$suggestion->{$date} eq "")? - "": - format_date($suggestion->{$date}) - ); } $suggestion->{'branchcode'}=C4::Context->userenv->{"branch"} unless ($suggestion->{'branchcode'}); } sub GetCriteriumDesc{ my ($criteriumvalue,$displayby)=@_; - return ($criteriumvalue eq 'ASKED'?"Pending":ucfirst(lc( $criteriumvalue))) if ($displayby =~/status/i); - return (GetBranchName($criteriumvalue)) if ($displayby =~/branchcode/); - return (GetSupportName($criteriumvalue)) if ($displayby =~/itemtype/); + if ($displayby =~ /status/i) { + unless ( grep { /$criteriumvalue/ } qw(ASKED ACCEPTED REJECTED CHECKED ORDERED AVAILABLE) ) { + 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); + } + if ( $displayby =~ /branchcode/ ) { + return $criteriumvalue ? Koha::Libraries->find($criteriumvalue)->branchname : "__ANY__"; + } + 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); @@ -74,156 +80,349 @@ sub GetCriteriumDesc{ } 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->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 $branchfilter = ($displayby ne "branchcode") ? $input->param('branchcode') : ''; my $tabcode = $input->param('tabcode'); +my $save_confirmed = $input->param('save_confirmed') || 0; +my $notify = $input->param('notify'); +my $filter_archived = $input->param('filter_archived') || 0; + +my $reasonsloop = GetAuthorisedValues("SUGGEST"); # filter informations which are not suggestion related. -my $suggestion_ref = $input->Vars; +my $suggestion_ref = { %{$input->Vars} }; # Copying, otherwise $input will be modified -delete $$suggestion_ref{$_} foreach qw( suggestedbyme op displayby tabcode edit_field ); +# get only the columns of Suggestion +my $schema = Koha::Database->new()->schema; +my $columns = ' '.join(' ', $schema->source('Suggestion')->columns).' '; +my $suggestion_only = { map { $columns =~ / $_ / ? ($_ => $suggestion_ref->{$_}) : () } keys %$suggestion_ref }; +$suggestion_only->{STATUS} = $suggestion_ref->{STATUS}; + +delete $$suggestion_ref{$_} foreach qw( suggestedbyme op displayby tabcode notify filter_archived ); 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' )); +} +delete $suggestion_only->{branchcode} if $suggestion_only->{branchcode} eq '__ANY__'; +delete $suggestion_only->{budgetid} if $suggestion_only->{budgetid} eq '__ANY__'; +while ( my ( $k, $v ) = each %$suggestion_only ) { + delete $suggestion_only->{$k} if $v eq ''; } + my ( $template, $borrowernumber, $cookie, $userflags ) = get_template_and_user( { - template_name => "suggestion/suggestion.tmpl", + template_name => "suggestion/suggestion.tt", query => $input, type => "intranet", - flagsrequired => { catalogue => 1 }, + 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 ) { - if ( $$suggestion_ref{"STATUS"} ) { - if ( my $tmpstatus = lc( $$suggestion_ref{"STATUS"} ) =~ /ACCEPTED|REJECTED/i ) { - $$suggestion_ref{ lc( $$suggestion_ref{"STATUS"}) . "date" } = C4::Dates->today; - $$suggestion_ref{ lc( $$suggestion_ref{"STATUS"}) . "by" } = C4::Context->userenv->{number}; - } - $$suggestion_ref{"manageddate"} = C4::Dates->today; - $$suggestion_ref{"managedby"} = C4::Context->userenv->{number}; + my @messages; + my $biblio = MarcRecordFromNewSuggestion({ + title => $suggestion_only->{title}, + author => $suggestion_only->{author}, + itemtype => $suggestion_only->{itemtype}, + isbn => $suggestion_only->{isbn}, + }); + + my $manager = Koha::Patrons->find( $suggestion_only->{managedby} ); + if ( $manager && not $manager->has_permission({suggestions => 'suggestions_manage'})) { + push @messages, { type => 'error', code => 'manager_not_enough_permissions' }; + $template->param( + messages => \@messages, + ); + delete $suggestion_ref->{suggesteddate}; + delete $suggestion_ref->{manageddate}; + delete $suggestion_ref->{managedby}; + Init($suggestion_ref); + } + elsif ( !$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}; + delete $suggestion_ref->{manageddate}; + Init($suggestion_ref); } - if ( $$suggestion_ref{'suggestionid'} > 0 ) { - &ModSuggestion($suggestion_ref); - } else { - ###FIXME:Search here if suggestion already exists. - my $suggestions_loop = - SearchSuggestion( $suggestion_ref ); - if (@$suggestions_loop>=1){ - #some suggestion are answering the request Donot Add - } - else { - ## Adding some informations related to suggestion - &NewSuggestion($suggestion_ref); + else { + + for my $date_key ( qw( suggesteddate manageddate accepteddate rejecteddate ) ) { + # FIXME Do we need this? + $suggestion_only->{$date_key} = dt_from_string( $suggestion_only->{$date_key} ) + if $suggestion_only->{$date_key}; + } + + 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}; + } + + my $otherreason = $input->param('other_reason'); + if ($suggestion_only->{reason} eq 'other' && $otherreason) { + $suggestion_only->{reason} = $otherreason; } - # empty fields, to avoid filter in "SearchSuggestion" - } - map{delete $$suggestion_ref{$_}} keys %$suggestion_ref; - $op = 'else'; + + if ( $suggestion_only->{'suggestionid'} > 0 ) { + + $suggestion_only->{lastmodificationdate} = dt_from_string; + $suggestion_only->{lastmodificationby} = C4::Context->userenv->{number}; + $suggestion_only->{branchcode} = undef + if exists $suggestion_only->{branchcode} + && $suggestion_only->{branchcode} eq ""; + + &ModSuggestion($suggestion_only); + + if ( $notify ) { + my $patron = Koha::Patrons->find( $suggestion_only->{managedby} ); + my $email_address = $patron->notice_email_address; + if ($patron->notice_email_address) { + + my $letter = C4::Letters::GetPreparedLetter( + module => 'suggestions', + letter_code => 'NOTIFY_MANAGER', + branchcode => $patron->branchcode, + lang => $patron->lang, + tables => { + suggestions => $suggestion_only->{suggestionid}, + branches => $patron->branchcode, + borrowers => $patron->borrowernumber, + }, + ); + C4::Letters::EnqueueLetter( + { + letter => $letter, + borrowernumber => $patron->borrowernumber, + message_transport_type => 'email' + } + ); + } + } + } else { + ###FIXME:Search here if suggestion already exists. + my $suggestions= Koha::Suggestions->search_limited( $suggestion_only ); + if ( $suggestions->count ) { + #some suggestion are answering the request Donot Add + my @messages; + while ( my $suggestion = $suggestions->next ) { + push @messages, { type => 'error', code => 'already_exists', id => $suggestion->suggestionid }; + } + $template->param( messages => \@messages ); + } + else { + ## Adding some informations related to suggestion + &NewSuggestion($suggestion_only); + } + # empty fields, to avoid filter in "SearchSuggestion" + } + map{delete $$suggestion_ref{$_} unless $_ eq 'branchcode' } keys %$suggestion_ref; + $op = 'else'; + + 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'}); + $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" ) { +elsif ($op eq "update_status" ) { + + my $suggestion; # set accepted/rejected/managed informations if applicable - # ie= if the librarian has choosen some action on the suggestions - if ($$suggestion_ref{"STATUS"} eq "ACCEPTED"){ - $$suggestion_ref{"accepteddate"}=C4::Dates->today; - $$suggestion_ref{"acceptedby"}=C4::Context->userenv->{number}; - } elsif ($$suggestion_ref{"STATUS"} eq "REJECTED"){ - $$suggestion_ref{"rejecteddate"}=C4::Dates->today; - $$suggestion_ref{"rejectedby"}=C4::Context->userenv->{number}; + # ie= if the librarian has chosen some action on the suggestions + my $STATUS = $input->param('STATUS'); + my $accepted_by = $input->param('acceptedby'); + if ( $STATUS eq "ACCEPTED" ) { + $suggestion = { + accepteddate => dt_from_string, + acceptedby => C4::Context->userenv->{number}, + }; + } + elsif ( $STATUS eq "REJECTED" ) { + $suggestion = { + rejecteddate => dt_from_string, + rejectedby => C4::Context->userenv->{number}, + }; + } + if ($STATUS) { + $suggestion->{manageddate} = dt_from_string; + $suggestion->{managedby} = C4::Context->userenv->{number}; + $suggestion->{STATUS} = $STATUS; } - if ($$suggestion_ref{"STATUS"}){ - $$suggestion_ref{"manageddate"}=C4::Dates->today; - $$suggestion_ref{"managedby"}=C4::Context->userenv->{number}; - } - if ( my $reason = $$suggestion_ref{"reason$tabcode"}){ - if ( $reason eq "other" ) { - $reason = $$suggestion_ref{"other_reason$tabcode"}; - } - $$suggestion_ref{'reason'}=$reason; - } - delete $$suggestion_ref{$_} foreach ("reason$tabcode", "other_reason$tabcode"); - foreach (keys %$suggestion_ref){ - delete $$suggestion_ref{$_} unless ($$suggestion_ref{$_}); - } + if ( my $reason = $input->param("reason") ) { + if ( $reason eq "other" ) { + $reason = $input->param("other_reason"); + } + $suggestion->{reason} = $reason; + } + foreach my $suggestionid (@editsuggestions) { next unless $suggestionid; - $$suggestion_ref{'suggestionid'}=$suggestionid; - &ModSuggestion($suggestion_ref); + $suggestion->{suggestionid} = $suggestionid; + &ModSuggestion($suggestion); } - $op = 'else'; + redirect_with_params($input); }elsif ($op eq "delete" ) { foreach my $delete_field (@editsuggestions) { &DelSuggestion( $borrowernumber, $delete_field,'intranet' ); } - $op = 'else'; + redirect_with_params($input); +} +elsif ($op eq "archive" ) { + Koha::Suggestions->find($_)->update({ archived => 1 }) for @editsuggestions; + + redirect_with_params($input); +} +elsif ($op eq "unarchive" ) { + Koha::Suggestions->find($_)->update({ archived => 0 }) for @editsuggestions; + + redirect_with_params($input); +} +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 }); + } + redirect_with_params($input); +} +elsif ( $op eq 'update_manager' ) { + my $managedby = $input->param('suggestion_managedby'); + foreach my $suggestionid (@editsuggestions) { + next unless $suggestionid; + &ModSuggestion({ suggestionid => $suggestionid, managedby => $managedby }); + } + redirect_with_params($input); } elsif ( $op eq 'show' ) { $suggestion_ref=&GetSuggestion($$suggestion_ref{'suggestionid'}); - $$suggestion_ref{branchname} = GetBranchName $$suggestion_ref{branchcode}; my $budget = GetBudget $$suggestion_ref{budgetid}; $$suggestion_ref{budgetname} = $$budget{budget_name}; Init($suggestion_ref); } 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); + foreach (@$criteria_list) { + if ($_->{value}) { + push @criteria_dv, $_->{value}; + } else { + $criteria_has_empty = 1; + } + } + # 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'} ); + } + # Pending tab first + if ( $displayby eq 'STATUS' ) { + @criteria_dv = grep { $_ ne 'ASKED' } @criteria_dv; + unshift @criteria_dv, 'ASKED'; + } + + unless ( exists $suggestion_ref->{branchcode} ) { + $suggestion_ref->{branchcode} = C4::Context->userenv->{'branch'}; + } + my @allsuggestions; - my $reasonsloop = GetAuthorisedValues("SUGGEST"); - foreach my $criteriumvalue ( map { $$_{'value'} } @$criteria_list ) { + foreach my $criteriumvalue ( @criteria_dv ) { + my $search_params = {%$suggestion_ref}; # By default, display suggestions from current working branch - if(not defined $branchfilter) { - $$suggestion_ref{'branchcode'} = C4::Context->userenv->{'branch'}; - } my $definedvalue = defined $$suggestion_ref{$displayby} && $$suggestion_ref{$displayby} ne ""; - next if ( $definedvalue && $$suggestion_ref{$displayby} ne $criteriumvalue ); - $$suggestion_ref{$displayby} = $criteriumvalue; + next if ( $definedvalue && $$suggestion_ref{$displayby} ne $criteriumvalue ) and ($displayby ne 'branchcode' && $branchfilter ne '__ANY__' ); - my $suggestions = &SearchSuggestion($suggestion_ref); - foreach my $suggestion (@$suggestions) { - if ($suggestion->{budgetid}){ - my $bud = GetBudget( $suggestion->{budgetid} ); - $suggestion->{budget_name} = $bud->{budget_name} if $bud; - } - foreach my $date (qw(suggesteddate manageddate accepteddate)) { - if ($suggestion->{$date} and $suggestion->{$date} ne "0000-00-00") { - $suggestion->{$date} = format_date( $suggestion->{$date} ); - } else { - $suggestion->{$date} = ""; + $search_params->{$displayby} = $criteriumvalue; + + # filter on date fields + foreach my $field (qw( suggesteddate manageddate accepteddate )) { + my $from = delete $search_params->{"${field}_from"}; + my $to = delete $search_params->{"${field}_to"}; + + my $from_dt = $from && eval { dt_from_string($from) }; + my $to_dt = $to && eval { dt_from_string($to) }; + + if ( $from_dt || $to_dt ) { + my $dtf = Koha::Database->new->schema->storage->datetime_parser; + if ( $from_dt && $to_dt ) { + $search_params->{$field} = { -between => [ $dtf->format_date($from_dt), $dtf->format_date($to_dt) ] }; + } elsif ( $from_dt ) { + $search_params->{$field} = { '>=' => $dtf->format_date($from_dt) }; + } elsif ( $to_dt ) { + $search_params->{$field} = { '<=' => $dtf->format_date($to_dt) }; } } } - push @allsuggestions,{ - "suggestiontype"=>$criteriumvalue||"suggest", - "suggestiontypelabel"=>GetCriteriumDesc($criteriumvalue,$displayby)||"", - "suggestionscount"=>scalar(@$suggestions), - 'suggestions_loop'=>$suggestions, - 'reasonsloop' => $reasonsloop, - }; + if ( $search_params->{budgetid} && $search_params->{budgetid} eq '__NONE__' ) { + $search_params->{budgetid} = [undef, '' ]; + } + for my $f (qw (branchcode budgetid)) { + delete $search_params->{$f} + if $search_params->{$f} eq '__ANY__' + || $search_params->{$f} eq ''; + } + + my @suggestions = + Koha::Suggestions->search_limited( + { %$search_params, archived => $filter_archived } )->as_list; + + push @allsuggestions, + { + "suggestiontype" => $criteriumvalue || "suggest", + "suggestiontypelabel" => GetCriteriumDesc( $criteriumvalue, $displayby ) || "", + 'suggestions' => \@suggestions, + 'reasonsloop' => $reasonsloop, + } + if scalar @suggestions > 0; delete $$suggestion_ref{$displayby} unless $definedvalue; } @@ -235,84 +434,32 @@ 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}); - $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} - ); - } -} $template->param( - %$suggestion_ref, - "op_$op" => 1, - dateformat => C4::Context->preference("dateformat"), + "${_}_patron" => scalar Koha::Patrons->find( $suggestion_ref->{$_} ) ) + for qw(managedby suggestedby acceptedby lastmodificationby); + +$template->param( + %$suggestion_ref, + filter_archived => $filter_archived, "op" =>$op, ); if(defined($returnsuggested) and $returnsuggested ne "noone") { - print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=".$returnsuggested."#suggestions"); + print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=".$returnsuggested."#suggestions"); } -#################### -## Initializing selection lists - -#branch display management -my $onlymine=C4::Context->preference('IndependantBranches') && - C4::Context->userenv && - C4::Context->userenv->{flags}!=1 && - C4::Context->userenv->{branch}; -my $branches = GetBranches($onlymine); -my @branchloop; - -foreach my $thisbranch ( sort {$branches->{$a}->{'branchname'} cmp $branches->{$b}->{'branchname'}} keys %$branches ) { - my %row = ( - value => $thisbranch, - branchname => $branches->{$thisbranch}->{'branchname'}, - selected => ($branchfilter and $branches->{$thisbranch}->{'branchcode'} eq $branchfilter ) || ( $$suggestion_ref{'branchcode'} and $branches->{$thisbranch}->{'branchcode'} eq $$suggestion_ref{'branchcode'} ) - ); - push @branchloop, \%row; -} -$branchfilter=C4::Context->userenv->{'branch'} if ($onlymine && !$branchfilter); - -$template->param( branchloop => \@branchloop, - branchfilter => $branchfilter); - -# the index parameter is different for item-level itemtypes -my $supportlist = GetSupportList(); - -foreach my $support (@$supportlist) { - $$support{'selected'} = (defined $$suggestion_ref{'itemtype'}) - ? $$support{'itemtype'} eq $$suggestion_ref{'itemtype'} - : 0; - if ( $$support{'imageurl'} ) { - $$support{'imageurl'} = getitemtypeimagelocation( 'intranet', $$support{'imageurl'} ); - } else { - delete $$support{'imageurl'}; - } -} -$template->param(itemtypeloop=>$supportlist); +$template->param( + branchfilter => $branchfilter, +); + $template->param( returnsuggestedby => $returnsuggestedby ); -my $patron_reason_loop = GetAuthorisedValues("OPAC_SUG",$$suggestion_ref{'patronreason'}); +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); -} - +# Budgets for filtering +my $budgets = GetBudgets; my @budgets_loop; foreach my $budget ( @{$budgets} ) { next unless (CanUserUseBudget($borrowernumber, $budget, $userflags)); @@ -324,50 +471,77 @@ foreach my $budget ( @{$budgets} ) { push @budgets_loop, $budget; } - $template->param( budgetsloop => \@budgets_loop); -$template->param( "statusselected_$$suggestion_ref{'STATUS'}" =>1) if ($$suggestion_ref{'STATUS'}); - -# get currencies and rates -my @rates = GetCurrencies(); -my $count = scalar @rates; -my $active_currency = GetCurrency(); -my $selected_currency; -if ($$suggestion_ref{'currency'}) { - $selected_currency = $$suggestion_ref{'currency'}; -} -else { - $selected_currency = $active_currency->{currency}; -} -my @loop_currency = (); -for ( my $i = 0 ; $i < $count ; $i++ ) { - my %line; - $line{currcode} = $rates[$i]->{'currency'}; - $line{rate} = $rates[$i]->{'rate'}; - $line{selected} = 1 if ($line{'currcode'} eq $selected_currency); - push @loop_currency, \%line; +# Budgets for suggestion add or edition +my $sugg_budget_loop = []; +my $sugg_budgets = GetBudgetHierarchy(); +foreach my $r ( @{$sugg_budgets} ) { + next unless ( CanUserUseBudget( $borrowernumber, $r, $userflags ) ); + my $selected = ( $$suggestion_ref{budgetid} && $r->{budget_id} eq $$suggestion_ref{budgetid} ) ? 1 : 0; + push @{$sugg_budget_loop}, + { + b_id => $r->{budget_id}, + b_txt => $r->{budget_name}, + b_active => $r->{budget_period_active}, + selected => $selected, + }; } +@{$sugg_budget_loop} = sort { uc( $a->{b_txt} ) cmp uc( $b->{b_txt} ) } @{$sugg_budget_loop}; +$template->param( sugg_budgets => $sugg_budget_loop); -$template->param(loop_currency => \@loop_currency); +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( - price => sprintf("%.2f", $$suggestion_ref{'price'}||0), - total => sprintf("%.2f", $$suggestion_ref{'total'}||0), + currencies => $currencies, + suggestion => $suggestion_ref, + price => sprintf("%.2f", $$suggestion_ref{'price'}||0), + total => sprintf("%.2f", $$suggestion_ref{'total'}||0), ); +# lists of distinct values (without empty) for filters my %hashlists; foreach my $field ( qw(managedby acceptedby suggestedby budgetid) ) { my $values_list; $values_list = GetDistinctValues( "suggestions." . $field ); my @codes_list = map { { 'code' => $$_{'value'}, - 'desc' => GetCriteriumDesc( $$_{'value'}, $field ), + 'desc' => GetCriteriumDesc( $$_{'value'}, $field ) || $$_{'value'}, 'selected' => ($$suggestion_ref{$field}) ? $$_{'value'} eq $$suggestion_ref{$field} : 0, } + } grep { + $$_{'value'} } @$values_list; $hashlists{ lc($field) . "_loop" } = \@codes_list; } -$template->param(%hashlists); -$template->param(DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),); + +$template->param( + %hashlists, + borrowernumber => ($input->param('borrowernumber') // undef), + SuggestionStatuses => GetAuthorisedValues('SUGGEST_STATUS'), +); output_html_with_http_headers $input, $cookie, $template->output; + +sub redirect_with_params { + my ( $input ) = @_; + my $params = ''; + foreach my $key ( + qw( + displayby branchcode title author isbn publishercode copyrightdate + collectiontitle suggestedby suggesteddate_from suggesteddate_to + manageddate_from manageddate_to accepteddate_from + accepteddate_to budgetid filter_archived + ) + ) + { + $params .= $key . '=' . uri_escape(scalar $input->param($key)) . '&' + if defined($input->param($key)); + } + print $input->redirect("/cgi-bin/koha/suggestion/suggestion.pl?$params"); +}