Bug 24552: Be compatible with Search::Elasticsearch 6.00
[koha.git] / suggestion / suggestion.pl
blob365e1d1c7521821256a19db1ab3818085156dc48
1 #!/usr/bin/perl
3 # This file is part of Koha.
4 # Copyright 2006-2010 BibLibre
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Modern::Perl;
21 require Exporter;
22 use CGI qw ( -utf8 );
23 use C4::Auth; # get_template_and_user
24 use C4::Output;
25 use C4::Suggestions;
26 use C4::Koha;
27 use C4::Budgets;
28 use C4::Search;
29 use C4::Members;
30 use C4::Debug;
31 use Koha::DateUtils qw( dt_from_string );
32 use Koha::AuthorisedValues;
33 use Koha::Acquisition::Currencies;
34 use Koha::Libraries;
35 use Koha::Patrons;
37 use URI::Escape;
39 sub Init{
40 my $suggestion= shift @_;
41 # "Managed by" is used only when a suggestion is being edited (not when created)
42 if ($suggestion->{'suggesteddate'} eq "0000-00-00" ||$suggestion->{'suggesteddate'} eq "") {
43 # new suggestion
44 $suggestion->{suggesteddate} = dt_from_string;
45 $suggestion->{'suggestedby'} = C4::Context->userenv->{"number"} unless ($suggestion->{'suggestedby'});
47 else {
48 # editing of an existing suggestion
49 $suggestion->{manageddate} = dt_from_string;
50 $suggestion->{'managedby'} = C4::Context->userenv->{"number"} unless ($suggestion->{'managedby'});
52 $suggestion->{'branchcode'}=C4::Context->userenv->{"branch"} unless ($suggestion->{'branchcode'});
55 sub GetCriteriumDesc{
56 my ($criteriumvalue,$displayby)=@_;
57 if ($displayby =~ /status/i) {
58 unless ( grep { /$criteriumvalue/ } qw(ASKED ACCEPTED REJECTED CHECKED ORDERED AVAILABLE) ) {
59 my $av = Koha::AuthorisedValues->search({ category => 'SUGGEST_STATUS', authorised_value => $criteriumvalue });
60 return $av->count ? $av->next->lib : 'Unknown';
62 return ($criteriumvalue eq 'ASKED'?"Pending":ucfirst(lc( $criteriumvalue))) if ($displayby =~/status/i);
64 return Koha::Libraries->find($criteriumvalue)->branchname
65 if $displayby =~ /branchcode/;
66 if ( $displayby =~ /itemtype/ ) {
67 my $av = Koha::AuthorisedValues->search({ category => 'SUGGEST_FORMAT', authorised_value => $criteriumvalue });
68 return $av->count ? $av->next->lib : 'Unknown';
70 if ($displayby =~/suggestedby/||$displayby =~/managedby/||$displayby =~/acceptedby/){
71 my $patron = Koha::Patrons->find( $criteriumvalue );
72 return "" unless $patron;
73 return $patron->surname . ", " . $patron->firstname;
75 if ( $displayby =~ /budgetid/) {
76 my $budget = GetBudget($criteriumvalue);
77 return "" unless $budget;
78 return $$budget{budget_name};
82 my $input = CGI->new;
83 my $redirect = $input->param('redirect');
84 my $suggestedbyme = (defined $input->param('suggestedbyme')? $input->param('suggestedbyme'):1);
85 my $op = $input->param('op')||'else';
86 my @editsuggestions = $input->multi_param('suggestionid');
87 my $suggestedby = $input->param('suggestedby');
88 my $returnsuggestedby = $input->param('returnsuggestedby');
89 my $returnsuggested = $input->param('returnsuggested');
90 my $managedby = $input->param('managedby');
91 my $displayby = $input->param('displayby') || '';
92 my $tabcode = $input->param('tabcode');
93 my $save_confirmed = $input->param('save_confirmed') || 0;
95 my $reasonsloop = GetAuthorisedValues("SUGGEST");
97 # filter informations which are not suggestion related.
98 my $suggestion_ref = $input->Vars;
100 # get only the columns of Suggestion
101 my $schema = Koha::Database->new()->schema;
102 my $columns = ' '.join(' ', $schema->source('Suggestion')->columns).' ';
103 my $suggestion_only = { map { $columns =~ / $_ / ? ($_ => $suggestion_ref->{$_}) : () } keys %$suggestion_ref };
104 $suggestion_only->{STATUS} = $suggestion_ref->{STATUS};
106 delete $$suggestion_ref{$_} foreach qw( suggestedbyme op displayby tabcode edit_field );
107 foreach (keys %$suggestion_ref){
108 delete $$suggestion_ref{$_} if (!$$suggestion_ref{$_} && ($op eq 'else' ));
110 my ( $template, $borrowernumber, $cookie, $userflags ) = get_template_and_user(
112 template_name => "suggestion/suggestion.tt",
113 query => $input,
114 type => "intranet",
115 flagsrequired => { suggestions => 'suggestions_manage' },
119 $borrowernumber = $input->param('borrowernumber') if ( $input->param('borrowernumber') );
120 $template->param('borrowernumber' => $borrowernumber);
121 my $branchfilter = $input->param('branchcode') || C4::Context->userenv->{'branch'};
123 #########################################
124 ## Operations
127 if ( $op =~ /save/i ) {
128 my @messages;
129 my $biblio = MarcRecordFromNewSuggestion({
130 title => $suggestion_only->{title},
131 author => $suggestion_only->{author},
132 itemtype => $suggestion_only->{itemtype},
135 if ( !$suggestion_only->{suggestionid} && ( my ($duplicatebiblionumber, $duplicatetitle) = FindDuplicate($biblio) ) && !$save_confirmed ) {
136 push @messages, { type => 'error', code => 'biblio_exists', id => $duplicatebiblionumber, title => $duplicatetitle };
137 $template->param(
138 messages => \@messages,
139 need_confirm => 1
141 delete $suggestion_ref->{suggesteddate};
142 Init($suggestion_ref);
144 else {
146 for my $date_key ( qw( suggesteddate manageddate accepteddate rejecteddate ) ) {
147 $suggestion_only->{$date_key} = dt_from_string( $suggestion_only->{$date_key} )
148 if $suggestion_only->{$date_key};
151 if ( $suggestion_only->{"STATUS"} ) {
152 if ( my $tmpstatus = lc( $suggestion_only->{"STATUS"} ) =~ /ACCEPTED|REJECTED/i ) {
153 $suggestion_only->{ lc( $suggestion_only->{"STATUS"}) . "date" } = dt_from_string;
154 $suggestion_only->{ lc( $suggestion_only->{"STATUS"}) . "by" } = C4::Context->userenv->{number};
156 $suggestion_only->{manageddate} = dt_from_string;
157 $suggestion_only->{"managedby"} = C4::Context->userenv->{number};
160 my $otherreason = $input->param('other_reason');
161 if ($suggestion_only->{reason} eq 'other' && $otherreason) {
162 $suggestion_only->{reason} = $otherreason;
165 if ( $suggestion_only->{'suggestionid'} > 0 ) {
166 &ModSuggestion($suggestion_only);
167 } else {
168 ###FIXME:Search here if suggestion already exists.
169 my $suggestions_loop =
170 SearchSuggestion( $suggestion_only );
171 if (@$suggestions_loop>=1){
172 #some suggestion are answering the request Donot Add
173 my @messages;
174 for my $suggestion ( @$suggestions_loop ) {
175 push @messages, { type => 'error', code => 'already_exists', id => $suggestion->{suggestionid} };
177 $template->param( messages => \@messages );
179 else {
180 ## Adding some informations related to suggestion
181 &NewSuggestion($suggestion_only);
183 # empty fields, to avoid filter in "SearchSuggestion"
185 map{delete $$suggestion_ref{$_}} keys %$suggestion_ref;
186 $op = 'else';
188 if( $redirect eq 'purchase_suggestions' ) {
189 print $input->redirect("/cgi-bin/koha/members/purchase-suggestions.pl?borrowernumber=$borrowernumber");
193 elsif ($op=~/add/) {
194 #Adds suggestion
195 Init($suggestion_ref);
196 $op ='save';
198 elsif ($op=~/edit/) {
199 #Edit suggestion
200 $suggestion_ref=&GetSuggestion($$suggestion_ref{'suggestionid'});
201 $suggestion_ref->{reasonsloop} = $reasonsloop;
202 my $other_reason = 1;
203 foreach my $reason ( @{ $reasonsloop } ) {
204 if ($suggestion_ref->{reason} eq $reason->{lib}) {
205 $other_reason = 0;
208 $other_reason = 0 unless $suggestion_ref->{reason};
209 $template->param(other_reason => $other_reason);
210 Init($suggestion_ref);
211 $op ='save';
213 elsif ($op eq "update_status" ) {
215 my $suggestion;
216 # set accepted/rejected/managed informations if applicable
217 # ie= if the librarian has chosen some action on the suggestions
218 my $STATUS = $input->param('STATUS');
219 my $accepted_by = $input->param('acceptedby');
220 if ( $STATUS eq "ACCEPTED" ) {
221 $suggestion = {
222 accepteddate => dt_from_string,
223 acceptedby => C4::Context->userenv->{number},
226 elsif ( $STATUS eq "REJECTED" ) {
227 $suggestion = {
228 rejecteddate => dt_from_string,
229 rejectedby => C4::Context->userenv->{number},
232 if ($STATUS) {
233 $suggestion->{manageddate} = dt_from_string;
234 $suggestion->{managedby} = C4::Context->userenv->{number};
235 $suggestion->{STATUS} = $STATUS;
237 if ( my $reason = $input->param("reason") ) {
238 if ( $reason eq "other" ) {
239 $reason = $input->param("other_reason");
241 $suggestion->{reason} = $reason;
244 foreach my $suggestionid (@editsuggestions) {
245 next unless $suggestionid;
246 $suggestion->{suggestionid} = $suggestionid;
247 &ModSuggestion($suggestion);
249 redirect_with_params($input);
250 }elsif ($op eq "delete" ) {
251 foreach my $delete_field (@editsuggestions) {
252 &DelSuggestion( $borrowernumber, $delete_field,'intranet' );
254 redirect_with_params($input);
256 elsif ( $op eq 'update_itemtype' ) {
257 my $new_itemtype = $input->param('suggestion_itemtype');
258 foreach my $suggestionid (@editsuggestions) {
259 next unless $suggestionid;
260 &ModSuggestion({ suggestionid => $suggestionid, itemtype => $new_itemtype });
262 redirect_with_params($input);
264 elsif ( $op eq 'show' ) {
265 $suggestion_ref=&GetSuggestion($$suggestion_ref{'suggestionid'});
266 my $budget = GetBudget $$suggestion_ref{budgetid};
267 $$suggestion_ref{budgetname} = $$budget{budget_name};
268 Init($suggestion_ref);
270 if ($op=~/else/) {
271 $op='else';
273 $displayby||="STATUS";
274 # distinct values of display by
275 my $criteria_list=GetDistinctValues("suggestions.".$displayby);
276 my (@criteria_dv, $criteria_has_empty);
277 foreach (@$criteria_list) {
278 if ($_->{value}) {
279 push @criteria_dv, $_->{value};
280 } else {
281 $criteria_has_empty = 1;
284 # aggregate null and empty values under empty value
285 push @criteria_dv, '' if $criteria_has_empty;
287 # Hack to not modify GetDistinctValues for this specific case
288 if ( $displayby eq 'branchcode'
289 && C4::Context->preference('IndependentBranches')
290 && not C4::Context->IsSuperLibrarian )
292 @criteria_dv = ( C4::Context->userenv->{'branch'} );
295 my @allsuggestions;
296 foreach my $criteriumvalue ( @criteria_dv ) {
297 # By default, display suggestions from current working branch
298 unless ( exists $$suggestion_ref{'branchcode'} ) {
299 $$suggestion_ref{'branchcode'} = C4::Context->userenv->{'branch'};
301 my $definedvalue = defined $$suggestion_ref{$displayby} && $$suggestion_ref{$displayby} ne "";
303 next if ( $definedvalue && $$suggestion_ref{$displayby} ne $criteriumvalue ) and ($displayby ne 'branchcode' or $branchfilter ne '__ANY__' );
304 $$suggestion_ref{$displayby} = $criteriumvalue;
306 my $suggestions = &SearchSuggestion($suggestion_ref);
307 foreach my $suggestion (@$suggestions) {
308 if ($suggestion->{budgetid}){
309 my $bud = GetBudget( $suggestion->{budgetid} );
310 $suggestion->{budget_name} = $bud->{budget_name} if $bud;
313 push @allsuggestions,{
314 "suggestiontype"=>$criteriumvalue||"suggest",
315 "suggestiontypelabel"=>GetCriteriumDesc($criteriumvalue,$displayby)||"",
316 "suggestionscount"=>scalar(@$suggestions),
317 'suggestions_loop'=>$suggestions,
318 'reasonsloop' => $reasonsloop,
321 delete $$suggestion_ref{$displayby} unless $definedvalue;
324 $template->param(
325 "displayby"=> $displayby,
326 "notabs"=> $displayby eq "",
327 suggestions => \@allsuggestions,
331 foreach my $element ( qw(managedby suggestedby acceptedby) ) {
332 # $debug || warn $$suggestion_ref{$element};
333 if ($$suggestion_ref{$element}){
334 my $patron = Koha::Patrons->find( $$suggestion_ref{$element} );
335 my $category = $patron->category;
336 $template->param(
337 $element."_borrowernumber"=>$patron->borrowernumber,
338 $element."_firstname"=>$patron->firstname,
339 $element."_surname"=>$patron->surname,
340 $element."_cardnumber"=>$patron->cardnumber,
341 $element."_branchcode"=>$patron->branchcode,
342 $element."_description"=>$category->description,
343 $element."_category_type"=>$category->category_type,
347 $template->param(
348 %$suggestion_ref,
349 "op_$op" => 1,
350 "op" =>$op,
353 if(defined($returnsuggested) and $returnsuggested ne "noone")
355 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=".$returnsuggested."#suggestions");
358 $template->param(
359 branchfilter => $branchfilter,
362 $template->param( returnsuggestedby => $returnsuggestedby );
364 my $patron_reason_loop = GetAuthorisedValues("OPAC_SUG");
365 $template->param(patron_reason_loop=>$patron_reason_loop);
367 #Budgets management
368 my $budgets = GetBudgets;
369 my @budgets_loop;
370 foreach my $budget ( @{$budgets} ) {
371 next unless (CanUserUseBudget($borrowernumber, $budget, $userflags));
373 ## Please see file perltidy.ERR
374 $budget->{'selected'} = 1
375 if ($$suggestion_ref{'budgetid'}
376 && $budget->{'budget_id'} eq $$suggestion_ref{'budgetid'});
378 push @budgets_loop, $budget;
381 $template->param( budgetsloop => \@budgets_loop);
382 if( $suggestion_ref->{STATUS} ) {
383 $template->param(
384 "statusselected_".$suggestion_ref->{STATUS} => 1,
385 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
389 my @currencies = Koha::Acquisition::Currencies->search;
390 $template->param(
391 currencies => \@currencies,
392 suggestion => $suggestion_ref,
393 price => sprintf("%.2f", $$suggestion_ref{'price'}||0),
394 total => sprintf("%.2f", $$suggestion_ref{'total'}||0),
397 # lists of distinct values (without empty) for filters
398 my %hashlists;
399 foreach my $field ( qw(managedby acceptedby suggestedby budgetid) ) {
400 my $values_list;
401 $values_list = GetDistinctValues( "suggestions." . $field );
402 my @codes_list = map {
403 { 'code' => $$_{'value'},
404 'desc' => GetCriteriumDesc( $$_{'value'}, $field ) || $$_{'value'},
405 'selected' => ($$suggestion_ref{$field}) ? $$_{'value'} eq $$suggestion_ref{$field} : 0,
407 } grep {
408 $$_{'value'}
409 } @$values_list;
410 $hashlists{ lc($field) . "_loop" } = \@codes_list;
413 $template->param(
414 %hashlists,
415 borrowernumber => ($input->param('borrowernumber') // undef),
416 SuggestionStatuses => GetAuthorisedValues('SUGGEST_STATUS'),
418 output_html_with_http_headers $input, $cookie, $template->output;
420 sub redirect_with_params {
421 my ( $input ) = @_;
422 my $params = '';
423 foreach my $key (
425 displayby branchcode title author isbn publishercode copyrightdate
426 collectiontitle suggestedby suggesteddate_from suggesteddate_to
427 manageddate_from manageddate_to accepteddate_from
428 accepteddate_to budgetid
432 $params .= $key . '=' . uri_escape(scalar $input->param($key)) . '&'
433 if defined($input->param($key));
435 print $input->redirect("/cgi-bin/koha/suggestion/suggestion.pl?$params");