Bug 12222: members-update.tt has a giant hash variable
[koha.git] / suggestion / suggestion.pl
blob046e347d1437ade834494f47b727bd893320be73
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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use strict;
21 #use warnings; FIXME - Bug 2505
22 require Exporter;
23 use CGI;
24 use C4::Auth; # get_template_and_user
25 use C4::Output;
26 use C4::Suggestions;
27 use C4::Koha; #GetItemTypes
28 use C4::Branch;
29 use C4::Budgets;
30 use C4::Search;
31 use C4::Dates qw(format_date);
32 use C4::Members;
33 use C4::Debug;
34 use URI::Escape;
36 sub Init{
37 my $suggestion= shift @_;
38 # "Managed by" is used only when a suggestion is being edited (not when created)
39 if ($suggestion->{'suggesteddate'} eq "0000-00-00" ||$suggestion->{'suggesteddate'} eq "") {
40 # new suggestion
41 $suggestion->{'suggesteddate'} = C4::Dates->today;
42 $suggestion->{'suggestedby'} = C4::Context->userenv->{"number"} unless ($suggestion->{'suggestedby'});
44 else {
45 # editing of an existing suggestion
46 $suggestion->{'manageddate'} = C4::Dates->today;
47 $suggestion->{'managedby'} = C4::Context->userenv->{"number"} unless ($suggestion->{'managedby'});
48 # suggesteddate, when coming from the DB, needs to be formated
49 $suggestion->{'suggesteddate'} = format_date($suggestion->{'suggesteddate'});
51 foreach my $date ( qw(rejecteddate accepteddate) ){
52 $suggestion->{$date}=(($suggestion->{$date} eq "0000-00-00" ||$suggestion->{$date} eq "")?
53 "":
54 format_date($suggestion->{$date})
57 $suggestion->{'branchcode'}=C4::Context->userenv->{"branch"} unless ($suggestion->{'branchcode'});
60 sub GetCriteriumDesc{
61 my ($criteriumvalue,$displayby)=@_;
62 if ($displayby =~ /status/i) {
63 if ( grep { /^($criteriumvalue)$/ } qw(ASKED ACCEPTED REJECTED CHECKED ORDERED AVAILABLE) ) {
64 return ($criteriumvalue eq 'ASKED'?"Pending":ucfirst(lc( $criteriumvalue)));
65 } else {
66 return GetAuthorisedValueByCode('SUGGEST_STATUS', $criteriumvalue) || $criteriumvalue;
69 return (GetBranchName($criteriumvalue)) if ($displayby =~/branchcode/);
70 return (GetSupportName($criteriumvalue)) if ($displayby =~/itemtype/);
71 if ($displayby =~/suggestedby/||$displayby =~/managedby/||$displayby =~/acceptedby/){
72 my $borr=C4::Members::GetMember(borrowernumber=>$criteriumvalue);
73 return "" unless $borr;
74 return $$borr{surname} . ", " . $$borr{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->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');
95 # filter informations which are not suggestion related.
96 my $suggestion_ref = $input->Vars;
98 delete $$suggestion_ref{$_} foreach qw( suggestedbyme op displayby tabcode edit_field );
99 foreach (keys %$suggestion_ref){
100 delete $$suggestion_ref{$_} if (!$$suggestion_ref{$_} && ($op eq 'else' || $op eq 'change'));
102 my ( $template, $borrowernumber, $cookie, $userflags ) = get_template_and_user(
104 template_name => "suggestion/suggestion.tt",
105 query => $input,
106 type => "intranet",
107 flagsrequired => { catalogue => 1 },
111 $borrowernumber = $input->param('borrowernumber') if ( $input->param('borrowernumber') );
112 $template->param('borrowernumber' => $borrowernumber);
114 #########################################
115 ## Operations
117 if ( $op =~ /save/i ) {
118 if ( $$suggestion_ref{"STATUS"} ) {
119 if ( my $tmpstatus = lc( $$suggestion_ref{"STATUS"} ) =~ /ACCEPTED|REJECTED/i ) {
120 $$suggestion_ref{ lc( $$suggestion_ref{"STATUS"}) . "date" } = C4::Dates->today;
121 $$suggestion_ref{ lc( $$suggestion_ref{"STATUS"}) . "by" } = C4::Context->userenv->{number};
123 $$suggestion_ref{"manageddate"} = C4::Dates->today;
124 $$suggestion_ref{"managedby"} = C4::Context->userenv->{number};
126 if ( $$suggestion_ref{'suggestionid'} > 0 ) {
127 &ModSuggestion($suggestion_ref);
128 } else {
129 ###FIXME:Search here if suggestion already exists.
130 my $suggestions_loop =
131 SearchSuggestion( $suggestion_ref );
132 if (@$suggestions_loop>=1){
133 #some suggestion are answering the request Donot Add
135 else {
136 ## Adding some informations related to suggestion
137 &NewSuggestion($suggestion_ref);
139 # empty fields, to avoid filter in "SearchSuggestion"
141 map{delete $$suggestion_ref{$_}} keys %$suggestion_ref;
142 $op = 'else';
144 if( $redirect eq 'purchase_suggestions' ) {
145 print $input->redirect("/cgi-bin/koha/members/purchase-suggestions.pl?borrowernumber=$borrowernumber");
149 elsif ($op=~/add/) {
150 #Adds suggestion
151 Init($suggestion_ref);
152 $op ='save';
154 elsif ($op=~/edit/) {
155 #Edit suggestion
156 $suggestion_ref=&GetSuggestion($$suggestion_ref{'suggestionid'});
157 Init($suggestion_ref);
158 $op ='save';
160 elsif ($op eq "change" ) {
161 # set accepted/rejected/managed informations if applicable
162 # ie= if the librarian has choosen some action on the suggestions
163 if ($$suggestion_ref{"STATUS"} eq "ACCEPTED"){
164 $$suggestion_ref{"accepteddate"}=C4::Dates->today;
165 $$suggestion_ref{"acceptedby"}=C4::Context->userenv->{number};
166 } elsif ($$suggestion_ref{"STATUS"} eq "REJECTED"){
167 $$suggestion_ref{"rejecteddate"}=C4::Dates->today;
168 $$suggestion_ref{"rejectedby"}=C4::Context->userenv->{number};
170 if ($$suggestion_ref{"STATUS"}){
171 $$suggestion_ref{"manageddate"}=C4::Dates->today;
172 $$suggestion_ref{"managedby"}=C4::Context->userenv->{number};
174 if ( my $reason = $$suggestion_ref{"reason$tabcode"}){
175 if ( $reason eq "other" ) {
176 $reason = $$suggestion_ref{"other_reason$tabcode"};
178 $$suggestion_ref{'reason'}=$reason;
180 delete $$suggestion_ref{$_} foreach ("reason$tabcode", "other_reason$tabcode");
181 foreach (keys %$suggestion_ref){
182 delete $$suggestion_ref{$_} unless ($$suggestion_ref{$_});
184 foreach my $suggestionid (@editsuggestions) {
185 next unless $suggestionid;
186 $$suggestion_ref{'suggestionid'}=$suggestionid;
187 &ModSuggestion($suggestion_ref);
189 my $params = '';
190 foreach my $key (
192 displayby branchcode title author isbn publishercode copyrightdate
193 collectiontitle suggestedby suggesteddate_from suggesteddate_to
194 manageddate_from manageddate_to accepteddate_from
195 accepteddate_to budgetid
199 $params .= $key . '=' . uri_escape($input->param($key)) . '&'
200 if defined($input->param($key));
202 print $input->redirect("/cgi-bin/koha/suggestion/suggestion.pl?$params");
203 }elsif ($op eq "delete" ) {
204 foreach my $delete_field (@editsuggestions) {
205 &DelSuggestion( $borrowernumber, $delete_field,'intranet' );
207 $op = 'else';
209 elsif ( $op eq 'show' ) {
210 $suggestion_ref=&GetSuggestion($$suggestion_ref{'suggestionid'});
211 $$suggestion_ref{branchname} = GetBranchName $$suggestion_ref{branchcode};
212 my $budget = GetBudget $$suggestion_ref{budgetid};
213 $$suggestion_ref{budgetname} = $$budget{budget_name};
214 Init($suggestion_ref);
216 if ($op=~/else/) {
217 $op='else';
219 $displayby||="STATUS";
220 delete $$suggestion_ref{'branchcode'} if($displayby eq "branchcode");
221 # distinct values of display by
222 my $criteria_list=GetDistinctValues("suggestions.".$displayby);
223 my (@criteria_dv, $criteria_has_empty);
224 foreach (@$criteria_list) {
225 if ($_->{value}) {
226 push @criteria_dv, $_->{value};
227 } else {
228 $criteria_has_empty = 1;
231 # agregate null and empty values under empty value
232 push @criteria_dv, '' if $criteria_has_empty;
234 my @allsuggestions;
235 my $reasonsloop = GetAuthorisedValues("SUGGEST");
236 foreach my $criteriumvalue ( @criteria_dv ) {
237 # By default, display suggestions from current working branch
238 unless ( exists $$suggestion_ref{'branchcode'} ) {
239 $$suggestion_ref{'branchcode'} = C4::Context->userenv->{'branch'};
241 my $definedvalue = defined $$suggestion_ref{$displayby} && $$suggestion_ref{$displayby} ne "";
243 next if ( $definedvalue && $$suggestion_ref{$displayby} ne $criteriumvalue );
244 $$suggestion_ref{$displayby} = $criteriumvalue;
246 my $suggestions = &SearchSuggestion($suggestion_ref);
247 foreach my $suggestion (@$suggestions) {
248 if ($suggestion->{budgetid}){
249 my $bud = GetBudget( $suggestion->{budgetid} );
250 $suggestion->{budget_name} = $bud->{budget_name} if $bud;
252 foreach my $date (qw(suggesteddate manageddate accepteddate)) {
253 if ($suggestion->{$date} and $suggestion->{$date} ne "0000-00-00") {
254 $suggestion->{$date} = format_date( $suggestion->{$date} );
255 } else {
256 $suggestion->{$date} = "";
260 push @allsuggestions,{
261 "suggestiontype"=>$criteriumvalue||"suggest",
262 "suggestiontypelabel"=>GetCriteriumDesc($criteriumvalue,$displayby)||"",
263 "suggestionscount"=>scalar(@$suggestions),
264 'suggestions_loop'=>$suggestions,
265 'reasonsloop' => $reasonsloop,
268 delete $$suggestion_ref{$displayby} unless $definedvalue;
271 $template->param(
272 "displayby"=> $displayby,
273 "notabs"=> $displayby eq "",
274 suggestions => \@allsuggestions,
278 foreach my $element ( qw(managedby suggestedby acceptedby) ) {
279 # $debug || warn $$suggestion_ref{$element};
280 if ($$suggestion_ref{$element}){
281 my $member=GetMember(borrowernumber=>$$suggestion_ref{$element});
282 $template->param(
283 $element."_borrowernumber"=>$$member{borrowernumber},
284 $element."_firstname"=>$$member{firstname},
285 $element."_surname"=>$$member{surname},
286 $element."_branchcode"=>$$member{branchcode},
287 $element."_description"=>$$member{description},
288 $element."_category_type"=>$$member{category_type}
292 $template->param(
293 %$suggestion_ref,
294 "op_$op" => 1,
295 "op" =>$op,
298 if(defined($returnsuggested) and $returnsuggested ne "noone")
300 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=".$returnsuggested."#suggestions");
303 ####################
304 ## Initializing selection lists
306 #branch display management
307 my $branchfilter = ($displayby ne "branchcode") ? $input->param('branchcode') : '';
308 my $onlymine =
309 C4::Context->preference('IndependentBranches')
310 && C4::Context->userenv
311 && !C4::Context->IsSuperLibrarian()
312 && C4::Context->userenv->{branch};
313 my $branches = GetBranches($onlymine);
314 my @branchloop;
316 foreach my $thisbranch ( sort {$branches->{$a}->{'branchname'} cmp $branches->{$b}->{'branchname'}} keys %$branches ) {
317 my %row = (
318 value => $thisbranch,
319 branchname => $branches->{$thisbranch}->{'branchname'},
320 selected => ($branchfilter and $branches->{$thisbranch}->{'branchcode'} eq $branchfilter ) || ( $$suggestion_ref{'branchcode'} and $branches->{$thisbranch}->{'branchcode'} eq $$suggestion_ref{'branchcode'} )
322 push @branchloop, \%row;
324 $branchfilter=C4::Context->userenv->{'branch'} if ($onlymine && !$branchfilter);
326 $template->param( branchloop => \@branchloop,
327 branchfilter => $branchfilter);
329 # the index parameter is different for item-level itemtypes
330 my $supportlist = GetSupportList();
332 foreach my $support (@$supportlist) {
333 $$support{'selected'} = (defined $$suggestion_ref{'itemtype'})
334 ? $$support{'itemtype'} eq $$suggestion_ref{'itemtype'}
335 : 0;
336 if ( $$support{'imageurl'} ) {
337 $$support{'imageurl'} = getitemtypeimagelocation( 'intranet', $$support{'imageurl'} );
338 } else {
339 delete $$support{'imageurl'};
342 $template->param(itemtypeloop=>$supportlist);
343 $template->param( returnsuggestedby => $returnsuggestedby );
345 my $patron_reason_loop = GetAuthorisedValues("OPAC_SUG",$$suggestion_ref{'patronreason'});
346 $template->param(patron_reason_loop=>$patron_reason_loop);
348 #Budgets management
349 my $budgets = [];
350 if ($branchfilter) {
351 my $searchbudgets = { budget_branchcode => $branchfilter };
352 $budgets = GetBudgets($searchbudgets);
353 } else {
354 $budgets = GetBudgets(undef);
357 my @budgets_loop;
358 foreach my $budget ( @{$budgets} ) {
359 next unless (CanUserUseBudget($borrowernumber, $budget, $userflags));
361 ## Please see file perltidy.ERR
362 $budget->{'selected'} = 1
363 if ($$suggestion_ref{'budgetid'}
364 && $budget->{'budget_id'} eq $$suggestion_ref{'budgetid'});
366 push @budgets_loop, $budget;
369 $template->param( budgetsloop => \@budgets_loop);
370 $template->param( "statusselected_$$suggestion_ref{'STATUS'}" =>1) if ($$suggestion_ref{'STATUS'});
372 # get currencies and rates
373 my @rates = GetCurrencies();
374 my $count = scalar @rates;
375 my $active_currency = GetCurrency();
376 my $selected_currency;
377 if ($$suggestion_ref{'currency'}) {
378 $selected_currency = $$suggestion_ref{'currency'};
380 else {
381 $selected_currency = $active_currency->{currency};
384 my @loop_currency = ();
385 for ( my $i = 0 ; $i < $count ; $i++ ) {
386 my %line;
387 $line{currcode} = $rates[$i]->{'currency'};
388 $line{rate} = $rates[$i]->{'rate'};
389 $line{selected} = 1 if ($line{'currcode'} eq $selected_currency);
390 push @loop_currency, \%line;
392 $template->param(
393 loop_currency => \@loop_currency,
394 price => sprintf("%.2f", $$suggestion_ref{'price'}||0),
395 total => sprintf("%.2f", $$suggestion_ref{'total'}||0),
398 # lists of distinct values (without empty) for filters
399 my %hashlists;
400 foreach my $field ( qw(managedby acceptedby suggestedby budgetid) ) {
401 my $values_list;
402 $values_list = GetDistinctValues( "suggestions." . $field );
403 my @codes_list = map {
404 { 'code' => $$_{'value'},
405 'desc' => GetCriteriumDesc( $$_{'value'}, $field ) || $$_{'value'},
406 'selected' => ($$suggestion_ref{$field}) ? $$_{'value'} eq $$suggestion_ref{$field} : 0,
408 } grep {
409 $$_{'value'}
410 } @$values_list;
411 $hashlists{ lc($field) . "_loop" } = \@codes_list;
414 $template->param(
415 %hashlists,
416 borrowernumber => ($input->param('borrowernumber') // undef),
417 SuggestionStatuses => GetAuthorisedValues('SUGGEST_STATUS'),
419 output_html_with_http_headers $input, $cookie, $template->output;