Bug 23413: Fix tests
[koha.git] / suggestion / suggestion.pl
blobe04e2ef6295c0e6a33648c335087dd509e42fb06
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;
32 use Koha::DateUtils qw( dt_from_string );
33 use Koha::AuthorisedValues;
34 use Koha::Acquisition::Currencies;
35 use Koha::Libraries;
36 use Koha::Patrons;
38 use URI::Escape;
40 sub Init{
41 my $suggestion= shift @_;
42 # "Managed by" is used only when a suggestion is being edited (not when created)
43 if ($suggestion->{'suggesteddate'} eq "0000-00-00" ||$suggestion->{'suggesteddate'} eq "") {
44 # new suggestion
45 $suggestion->{suggesteddate} = dt_from_string;
46 $suggestion->{'suggestedby'} = C4::Context->userenv->{"number"} unless ($suggestion->{'suggestedby'});
48 else {
49 # editing of an existing suggestion
50 $suggestion->{manageddate} = dt_from_string;
51 $suggestion->{'managedby'} = C4::Context->userenv->{"number"} unless ($suggestion->{'managedby'});
53 $suggestion->{'branchcode'}=C4::Context->userenv->{"branch"} unless ($suggestion->{'branchcode'});
56 sub GetCriteriumDesc{
57 my ($criteriumvalue,$displayby)=@_;
58 if ($displayby =~ /status/i) {
59 unless ( grep { /$criteriumvalue/ } qw(ASKED ACCEPTED REJECTED CHECKED ORDERED AVAILABLE) ) {
60 my $av = Koha::AuthorisedValues->search({ category => 'SUGGEST_STATUS', authorised_value => $criteriumvalue });
61 return $av->count ? $av->next->lib : 'Unknown';
63 return ($criteriumvalue eq 'ASKED'?"Pending":ucfirst(lc( $criteriumvalue))) if ($displayby =~/status/i);
65 return Koha::Libraries->find($criteriumvalue)->branchname
66 if $displayby =~ /branchcode/;
67 if ( $displayby =~ /itemtype/ ) {
68 my $av = Koha::AuthorisedValues->search({ category => 'SUGGEST_FORMAT', authorised_value => $criteriumvalue });
69 return $av->count ? $av->next->lib : 'Unknown';
71 if ($displayby =~/suggestedby/||$displayby =~/managedby/||$displayby =~/acceptedby/){
72 my $patron = Koha::Patrons->find( $criteriumvalue );
73 return "" unless $patron;
74 return $patron->surname . ", " . $patron->firstname;
76 if ( $displayby =~ /budgetid/) {
77 my $budget = GetBudget($criteriumvalue);
78 return "" unless $budget;
79 return $$budget{budget_name};
83 my $input = CGI->new;
84 my $redirect = $input->param('redirect');
85 my $suggestedbyme = (defined $input->param('suggestedbyme')? $input->param('suggestedbyme'):1);
86 my $op = $input->param('op')||'else';
87 my @editsuggestions = $input->multi_param('edit_field');
88 my $suggestedby = $input->param('suggestedby');
89 my $returnsuggestedby = $input->param('returnsuggestedby');
90 my $returnsuggested = $input->param('returnsuggested');
91 my $managedby = $input->param('managedby');
92 my $displayby = $input->param('displayby') || '';
93 my $tabcode = $input->param('tabcode');
94 my $reasonsloop = GetAuthorisedValues("SUGGEST");
96 # filter informations which are not suggestion related.
97 my $suggestion_ref = $input->Vars;
99 # get only the columns of Suggestion
100 my $schema = Koha::Database->new()->schema;
101 my $columns = ' '.join(' ', $schema->source('Suggestion')->columns).' ';
102 my $suggestion_only = { map { $columns =~ / $_ / ? ($_ => $suggestion_ref->{$_}) : () } keys %$suggestion_ref };
103 $suggestion_only->{STATUS} = $suggestion_ref->{STATUS};
105 delete $$suggestion_ref{$_} foreach qw( suggestedbyme op displayby tabcode edit_field );
106 foreach (keys %$suggestion_ref){
107 delete $$suggestion_ref{$_} if (!$$suggestion_ref{$_} && ($op eq 'else' || $op eq 'change'));
109 my ( $template, $borrowernumber, $cookie, $userflags ) = get_template_and_user(
111 template_name => "suggestion/suggestion.tt",
112 query => $input,
113 type => "intranet",
114 flagsrequired => { acquisition => 'suggestions_manage' },
118 $borrowernumber = $input->param('borrowernumber') if ( $input->param('borrowernumber') );
119 $template->param('borrowernumber' => $borrowernumber);
121 #########################################
122 ## Operations
124 if ( $op =~ /save/i ) {
125 $suggestion_only->{suggesteddate} = dt_from_string( $suggestion_only->{suggesteddate} )
126 if $suggestion_only->{suggesteddate};
128 if ( $suggestion_only->{"STATUS"} ) {
129 if ( my $tmpstatus = lc( $suggestion_only->{"STATUS"} ) =~ /ACCEPTED|REJECTED/i ) {
130 $suggestion_only->{ lc( $suggestion_only->{"STATUS"}) . "date" } = dt_from_string;
131 $suggestion_only->{ lc( $suggestion_only->{"STATUS"}) . "by" } = C4::Context->userenv->{number};
133 $suggestion_only->{manageddate} = dt_from_string;
134 $suggestion_only->{"managedby"} = C4::Context->userenv->{number};
137 my $otherreason = $input->param('other_reason');
138 if ($suggestion_only->{reason} eq 'other' && $otherreason) {
139 $suggestion_only->{reason} = $otherreason;
142 if ( $suggestion_only->{'suggestionid'} > 0 ) {
143 &ModSuggestion($suggestion_only);
144 } else {
145 ###FIXME:Search here if suggestion already exists.
146 my $suggestions_loop =
147 SearchSuggestion( $suggestion_only );
148 if (@$suggestions_loop>=1){
149 #some suggestion are answering the request Donot Add
150 my @messages;
151 for my $suggestion ( @$suggestions_loop ) {
152 push @messages, { type => 'error', code => 'already_exists', id => $suggestion->{suggestionid} };
154 $template->param( messages => \@messages );
156 else {
157 ## Adding some informations related to suggestion
158 &NewSuggestion($suggestion_only);
160 # empty fields, to avoid filter in "SearchSuggestion"
162 map{delete $$suggestion_ref{$_}} keys %$suggestion_ref;
163 $op = 'else';
165 if( $redirect eq 'purchase_suggestions' ) {
166 print $input->redirect("/cgi-bin/koha/members/purchase-suggestions.pl?borrowernumber=$borrowernumber");
170 elsif ($op=~/add/) {
171 #Adds suggestion
172 Init($suggestion_ref);
173 $op ='save';
175 elsif ($op=~/edit/) {
176 #Edit suggestion
177 $suggestion_ref=&GetSuggestion($$suggestion_ref{'suggestionid'});
178 $suggestion_ref->{reasonsloop} = $reasonsloop;
179 my $other_reason = 1;
180 foreach my $reason ( @{ $reasonsloop } ) {
181 if ($suggestion_ref->{reason} eq $reason->{lib}) {
182 $other_reason = 0;
185 $other_reason = 0 unless $suggestion_ref->{reason};
186 $template->param(other_reason => $other_reason);
187 Init($suggestion_ref);
188 $op ='save';
190 elsif ($op eq "change" ) {
192 my $suggestion;
193 # set accepted/rejected/managed informations if applicable
194 # ie= if the librarian has chosen some action on the suggestions
195 my $STATUS = $input->param('STATUS');
196 my $accepted_by = $input->param('acceptedby');
197 if ( $STATUS eq "ACCEPTED" ) {
198 $suggestion = {
199 accepteddate => dt_from_string,
200 acceptedby => C4::Context->userenv->{number},
203 elsif ( $STATUS eq "REJECTED" ) {
204 $suggestion = {
205 rejecteddate => dt_from_string,
206 rejectedby => C4::Context->userenv->{number},
209 if ($STATUS) {
210 $suggestion->{manageddate} = dt_from_string;
211 $suggestion->{managedby} = C4::Context->userenv->{number};
212 $suggestion->{STATUS} = $STATUS;
214 if ( my $reason = $input->param("reason$tabcode") ) {
215 if ( $reason eq "other" ) {
216 $reason = $input->param("other_reason$tabcode");
218 $suggestion->{reason} = $reason;
221 foreach my $suggestionid (@editsuggestions) {
222 next unless $suggestionid;
223 $suggestion->{suggestionid} = $suggestionid;
224 &ModSuggestion($suggestion);
226 my $params = '';
227 foreach my $key (
229 displayby branchcode title author isbn publishercode copyrightdate
230 collectiontitle suggestedby suggesteddate_from suggesteddate_to
231 manageddate_from manageddate_to accepteddate_from
232 accepteddate_to budgetid
236 $params .= $key . '=' . uri_escape($input->param($key)) . '&'
237 if defined($input->param($key));
239 print $input->redirect("/cgi-bin/koha/suggestion/suggestion.pl?$params");
240 }elsif ($op eq "delete" ) {
241 foreach my $delete_field (@editsuggestions) {
242 &DelSuggestion( $borrowernumber, $delete_field,'intranet' );
244 $op = 'else';
246 elsif ( $op eq 'show' ) {
247 $suggestion_ref=&GetSuggestion($$suggestion_ref{'suggestionid'});
248 my $budget = GetBudget $$suggestion_ref{budgetid};
249 $$suggestion_ref{budgetname} = $$budget{budget_name};
250 Init($suggestion_ref);
252 if ($op=~/else/) {
253 $op='else';
255 $displayby||="STATUS";
256 delete $$suggestion_ref{'branchcode'} if($displayby eq "branchcode");
257 # distinct values of display by
258 my $criteria_list=GetDistinctValues("suggestions.".$displayby);
259 my (@criteria_dv, $criteria_has_empty);
260 foreach (@$criteria_list) {
261 if ($_->{value}) {
262 push @criteria_dv, $_->{value};
263 } else {
264 $criteria_has_empty = 1;
267 # aggregate null and empty values under empty value
268 push @criteria_dv, '' if $criteria_has_empty;
270 my @allsuggestions;
271 foreach my $criteriumvalue ( @criteria_dv ) {
272 # By default, display suggestions from current working branch
273 unless ( exists $$suggestion_ref{'branchcode'} ) {
274 $$suggestion_ref{'branchcode'} = C4::Context->userenv->{'branch'};
276 my $definedvalue = defined $$suggestion_ref{$displayby} && $$suggestion_ref{$displayby} ne "";
278 next if ( $definedvalue && $$suggestion_ref{$displayby} ne $criteriumvalue );
279 $$suggestion_ref{$displayby} = $criteriumvalue;
281 my $suggestions = &SearchSuggestion($suggestion_ref);
282 foreach my $suggestion (@$suggestions) {
283 if ($suggestion->{budgetid}){
284 my $bud = GetBudget( $suggestion->{budgetid} );
285 $suggestion->{budget_name} = $bud->{budget_name} if $bud;
288 push @allsuggestions,{
289 "suggestiontype"=>$criteriumvalue||"suggest",
290 "suggestiontypelabel"=>GetCriteriumDesc($criteriumvalue,$displayby)||"",
291 "suggestionscount"=>scalar(@$suggestions),
292 'suggestions_loop'=>$suggestions,
293 'reasonsloop' => $reasonsloop,
296 delete $$suggestion_ref{$displayby} unless $definedvalue;
299 $template->param(
300 "displayby"=> $displayby,
301 "notabs"=> $displayby eq "",
302 suggestions => \@allsuggestions,
306 foreach my $element ( qw(managedby suggestedby acceptedby) ) {
307 # $debug || warn $$suggestion_ref{$element};
308 if ($$suggestion_ref{$element}){
309 my $patron = Koha::Patrons->find( $$suggestion_ref{$element} );
310 my $category = $patron->category;
311 $template->param(
312 $element."_borrowernumber"=>$patron->borrowernumber,
313 $element."_firstname"=>$patron->firstname,
314 $element."_surname"=>$patron->surname,
315 $element."_cardnumber"=>$patron->cardnumber,
316 $element."_branchcode"=>$patron->branchcode,
317 $element."_description"=>$category->description,
318 $element."_category_type"=>$category->category_type,
322 $template->param(
323 %$suggestion_ref,
324 "op_$op" => 1,
325 "op" =>$op,
328 if(defined($returnsuggested) and $returnsuggested ne "noone")
330 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=".$returnsuggested."#suggestions");
333 my $branchfilter = ($displayby ne "branchcode") ? $input->param('branchcode') : C4::Context->userenv->{'branch'};
335 $template->param(
336 branchfilter => $branchfilter,
339 $template->param( returnsuggestedby => $returnsuggestedby );
341 my $patron_reason_loop = GetAuthorisedValues("OPAC_SUG");
342 $template->param(patron_reason_loop=>$patron_reason_loop);
344 #Budgets management
345 my $budgets = [];
346 if ($branchfilter) {
347 my $searchbudgets = { budget_branchcode => $branchfilter };
348 $budgets = GetBudgets($searchbudgets);
349 } else {
350 $budgets = GetBudgets(undef);
353 my @budgets_loop;
354 foreach my $budget ( @{$budgets} ) {
355 next unless (CanUserUseBudget($borrowernumber, $budget, $userflags));
357 ## Please see file perltidy.ERR
358 $budget->{'selected'} = 1
359 if ($$suggestion_ref{'budgetid'}
360 && $budget->{'budget_id'} eq $$suggestion_ref{'budgetid'});
362 push @budgets_loop, $budget;
365 $template->param( budgetsloop => \@budgets_loop);
366 if( $suggestion_ref->{STATUS} ) {
367 $template->param(
368 "statusselected_".$suggestion_ref->{STATUS} => 1,
369 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
373 my @currencies = Koha::Acquisition::Currencies->search;
374 $template->param(
375 currencies => \@currencies,
376 suggestion => $suggestion_ref,
377 price => sprintf("%.2f", $$suggestion_ref{'price'}||0),
378 total => sprintf("%.2f", $$suggestion_ref{'total'}||0),
381 # lists of distinct values (without empty) for filters
382 my %hashlists;
383 foreach my $field ( qw(managedby acceptedby suggestedby budgetid) ) {
384 my $values_list;
385 $values_list = GetDistinctValues( "suggestions." . $field );
386 my @codes_list = map {
387 { 'code' => $$_{'value'},
388 'desc' => GetCriteriumDesc( $$_{'value'}, $field ) || $$_{'value'},
389 'selected' => ($$suggestion_ref{$field}) ? $$_{'value'} eq $$suggestion_ref{$field} : 0,
391 } grep {
392 $$_{'value'}
393 } @$values_list;
394 $hashlists{ lc($field) . "_loop" } = \@codes_list;
397 $template->param(
398 %hashlists,
399 borrowernumber => ($input->param('borrowernumber') // undef),
400 SuggestionStatuses => GetAuthorisedValues('SUGGEST_STATUS'),
402 output_html_with_http_headers $input, $cookie, $template->output;