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
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.
21 #use warnings; FIXME - Bug 2505
24 use C4
::Auth
; # get_template_and_user
27 use C4
::Koha
; #GetItemTypes
34 use Koha
::DateUtils
qw( dt_from_string );
39 my $suggestion= shift @_;
40 # "Managed by" is used only when a suggestion is being edited (not when created)
41 if ($suggestion->{'suggesteddate'} eq "0000-00-00" ||$suggestion->{'suggesteddate'} eq "") {
43 $suggestion->{suggesteddate
} = dt_from_string
;
44 $suggestion->{'suggestedby'} = C4
::Context
->userenv->{"number"} unless ($suggestion->{'suggestedby'});
47 # editing of an existing suggestion
48 $suggestion->{manageddate
} = dt_from_string
;
49 $suggestion->{'managedby'} = C4
::Context
->userenv->{"number"} unless ($suggestion->{'managedby'});
51 $suggestion->{'branchcode'}=C4
::Context
->userenv->{"branch"} unless ($suggestion->{'branchcode'});
55 my ($criteriumvalue,$displayby)=@_;
56 if ($displayby =~ /status/i) {
57 if ( grep { /^($criteriumvalue)$/ } qw(ASKED ACCEPTED REJECTED CHECKED ORDERED AVAILABLE) ) {
58 return ($criteriumvalue eq 'ASKED'?
"Pending":ucfirst(lc( $criteriumvalue)));
60 return GetAuthorisedValueByCode
('SUGGEST_STATUS', $criteriumvalue) || $criteriumvalue;
63 return (GetBranchName
($criteriumvalue)) if ($displayby =~/branchcode/);
64 return (GetSupportName
($criteriumvalue)) if ($displayby =~/itemtype/);
65 if ($displayby =~/suggestedby/||$displayby =~/managedby/||$displayby =~/acceptedby/){
66 my $borr=C4
::Members
::GetMember
(borrowernumber
=>$criteriumvalue);
67 return "" unless $borr;
68 return $$borr{surname
} . ", " . $$borr{firstname
};
70 if ( $displayby =~ /budgetid/) {
71 my $budget = GetBudget
($criteriumvalue);
72 return "" unless $budget;
73 return $$budget{budget_name
};
78 my $redirect = $input->param('redirect');
79 my $suggestedbyme = (defined $input->param('suggestedbyme')?
$input->param('suggestedbyme'):1);
80 my $op = $input->param('op')||'else';
81 my @editsuggestions = $input->param('edit_field');
82 my $suggestedby = $input->param('suggestedby');
83 my $returnsuggestedby = $input->param('returnsuggestedby');
84 my $returnsuggested = $input->param('returnsuggested');
85 my $managedby = $input->param('managedby');
86 my $displayby = $input->param('displayby') || '';
87 my $tabcode = $input->param('tabcode');
89 # filter informations which are not suggestion related.
90 my $suggestion_ref = $input->Vars;
92 # get only the columns of Suggestion
93 my $schema = Koha
::Database
->new()->schema;
94 my $columns = ' '.join(' ', $schema->source('Suggestion')->columns).' ';
95 my $suggestion_only = { map { $columns =~ / $_ / ?
($_ => $suggestion_ref->{$_}) : () } keys %$suggestion_ref };
96 $suggestion_only->{STATUS
} = $suggestion_ref->{STATUS
};
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",
107 flagsrequired
=> { catalogue
=> 1 },
111 $borrowernumber = $input->param('borrowernumber') if ( $input->param('borrowernumber') );
112 $template->param('borrowernumber' => $borrowernumber);
114 #########################################
117 if ( $op =~ /save/i ) {
118 $suggestion_only->{suggesteddate
} = dt_from_string
( $suggestion_only->{suggesteddate
} )
119 if $suggestion_only->{suggesteddate
};
121 if ( $suggestion_only->{"STATUS"} ) {
122 if ( my $tmpstatus = lc( $suggestion_only->{"STATUS"} ) =~ /ACCEPTED|REJECTED/i ) {
123 $suggestion_only->{ lc( $suggestion_only->{"STATUS"}) . "date" } = dt_from_string
;
124 $suggestion_only->{ lc( $suggestion_only->{"STATUS"}) . "by" } = C4
::Context
->userenv->{number
};
126 $suggestion_only->{manageddate
} = dt_from_string
;
127 $suggestion_only->{"managedby"} = C4
::Context
->userenv->{number
};
129 if ( $suggestion_only->{'suggestionid'} > 0 ) {
130 &ModSuggestion
($suggestion_only);
132 ###FIXME:Search here if suggestion already exists.
133 my $suggestions_loop =
134 SearchSuggestion
( $suggestion_only );
135 if (@
$suggestions_loop>=1){
136 #some suggestion are answering the request Donot Add
139 ## Adding some informations related to suggestion
140 &NewSuggestion
($suggestion_only);
142 # empty fields, to avoid filter in "SearchSuggestion"
144 map{delete $$suggestion_ref{$_}} keys %$suggestion_ref;
147 if( $redirect eq 'purchase_suggestions' ) {
148 print $input->redirect("/cgi-bin/koha/members/purchase-suggestions.pl?borrowernumber=$borrowernumber");
154 Init
($suggestion_ref);
157 elsif ($op=~/edit/) {
159 $suggestion_ref=&GetSuggestion
($$suggestion_ref{'suggestionid'});
160 Init
($suggestion_ref);
163 elsif ($op eq "change" ) {
164 # set accepted/rejected/managed informations if applicable
165 # ie= if the librarian has choosen some action on the suggestions
166 if ($suggestion_only->{"STATUS"} eq "ACCEPTED"){
167 $suggestion_only->{accepteddate
} = dt_from_string
;
168 $suggestion_only->{"acceptedby"}=C4
::Context
->userenv->{number
};
169 } elsif ($suggestion_only->{"STATUS"} eq "REJECTED"){
170 $suggestion_only->{rejecteddate
} = dt_from_string
;
171 $suggestion_only->{"rejectedby"}=C4
::Context
->userenv->{number
};
173 if ($suggestion_only->{"STATUS"}){
174 $suggestion_only->{manageddate
} = dt_from_string
;
175 $suggestion_only->{"managedby"}=C4
::Context
->userenv->{number
};
177 if ( my $reason = $$suggestion_ref{"reason$tabcode"}){
178 if ( $reason eq "other" ) {
179 $reason = $$suggestion_ref{"other_reason$tabcode"};
181 $suggestion_only->{reason
}=$reason;
184 foreach my $suggestionid (@editsuggestions) {
185 next unless $suggestionid;
186 $suggestion_only->{'suggestionid'}=$suggestionid;
187 &ModSuggestion
($suggestion_only);
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' );
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);
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) {
226 push @criteria_dv, $_->{value
};
228 $criteria_has_empty = 1;
231 # agregate null and empty values under empty value
232 push @criteria_dv, '' if $criteria_has_empty;
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;
253 push @allsuggestions,{
254 "suggestiontype"=>$criteriumvalue||"suggest",
255 "suggestiontypelabel"=>GetCriteriumDesc
($criteriumvalue,$displayby)||"",
256 "suggestionscount"=>scalar(@
$suggestions),
257 'suggestions_loop'=>$suggestions,
258 'reasonsloop' => $reasonsloop,
261 delete $$suggestion_ref{$displayby} unless $definedvalue;
265 "displayby"=> $displayby,
266 "notabs"=> $displayby eq "",
267 suggestions
=> \
@allsuggestions,
271 foreach my $element ( qw(managedby suggestedby acceptedby) ) {
272 # $debug || warn $$suggestion_ref{$element};
273 if ($$suggestion_ref{$element}){
274 my $member=GetMember
(borrowernumber
=>$$suggestion_ref{$element});
276 $element."_borrowernumber"=>$$member{borrowernumber
},
277 $element."_firstname"=>$$member{firstname
},
278 $element."_surname"=>$$member{surname
},
279 $element."_branchcode"=>$$member{branchcode
},
280 $element."_description"=>$$member{description
},
281 $element."_category_type"=>$$member{category_type
}
291 if(defined($returnsuggested) and $returnsuggested ne "noone")
293 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=".$returnsuggested."#suggestions");
297 ## Initializing selection lists
299 #branch display management
300 my $branchfilter = ($displayby ne "branchcode") ?
$input->param('branchcode') : '';
302 C4
::Context
->preference('IndependentBranches')
303 && C4
::Context
->userenv
304 && !C4
::Context
->IsSuperLibrarian()
305 && C4
::Context
->userenv->{branch
};
306 my $branches = GetBranches
($onlymine);
309 foreach my $thisbranch ( sort {$branches->{$a}->{'branchname'} cmp $branches->{$b}->{'branchname'}} keys %$branches ) {
311 value
=> $thisbranch,
312 branchname
=> $branches->{$thisbranch}->{'branchname'},
313 selected
=> ($branchfilter and $branches->{$thisbranch}->{'branchcode'} eq $branchfilter ) || ( $$suggestion_ref{'branchcode'} and $branches->{$thisbranch}->{'branchcode'} eq $$suggestion_ref{'branchcode'} )
315 push @branchloop, \
%row;
317 $branchfilter=C4
::Context
->userenv->{'branch'} if ($onlymine && !$branchfilter);
319 $template->param( branchloop
=> \
@branchloop,
320 branchfilter
=> $branchfilter);
322 # the index parameter is different for item-level itemtypes
323 my $supportlist = GetSupportList
();
325 foreach my $support (@
$supportlist) {
326 $$support{'selected'} = (defined $$suggestion_ref{'itemtype'})
327 ?
$$support{'itemtype'} eq $$suggestion_ref{'itemtype'}
329 if ( $$support{'imageurl'} ) {
330 $$support{'imageurl'} = getitemtypeimagelocation
( 'intranet', $$support{'imageurl'} );
332 delete $$support{'imageurl'};
335 $template->param(itemtypeloop
=>$supportlist);
336 $template->param( returnsuggestedby
=> $returnsuggestedby );
338 my $patron_reason_loop = GetAuthorisedValues
("OPAC_SUG",$$suggestion_ref{'patronreason'});
339 $template->param(patron_reason_loop
=>$patron_reason_loop);
344 my $searchbudgets = { budget_branchcode
=> $branchfilter };
345 $budgets = GetBudgets
($searchbudgets);
347 $budgets = GetBudgets
(undef);
351 foreach my $budget ( @
{$budgets} ) {
352 next unless (CanUserUseBudget
($borrowernumber, $budget, $userflags));
354 ## Please see file perltidy.ERR
355 $budget->{'selected'} = 1
356 if ($$suggestion_ref{'budgetid'}
357 && $budget->{'budget_id'} eq $$suggestion_ref{'budgetid'});
359 push @budgets_loop, $budget;
362 $template->param( budgetsloop
=> \
@budgets_loop);
363 $template->param( "statusselected_$$suggestion_ref{'STATUS'}" =>1) if ($$suggestion_ref{'STATUS'});
365 # get currencies and rates
366 my @rates = GetCurrencies
();
367 my $count = scalar @rates;
368 my $active_currency = GetCurrency
();
369 my $selected_currency;
370 if ($$suggestion_ref{'currency'}) {
371 $selected_currency = $$suggestion_ref{'currency'};
374 $selected_currency = $active_currency->{currency
};
377 my @loop_currency = ();
378 for ( my $i = 0 ; $i < $count ; $i++ ) {
380 $line{currcode
} = $rates[$i]->{'currency'};
381 $line{rate
} = $rates[$i]->{'rate'};
382 $line{selected
} = 1 if ($line{'currcode'} eq $selected_currency);
383 push @loop_currency, \
%line;
386 loop_currency
=> \
@loop_currency,
387 price
=> sprintf("%.2f", $$suggestion_ref{'price'}||0),
388 total
=> sprintf("%.2f", $$suggestion_ref{'total'}||0),
391 # lists of distinct values (without empty) for filters
393 foreach my $field ( qw(managedby acceptedby suggestedby budgetid) ) {
395 $values_list = GetDistinctValues
( "suggestions." . $field );
396 my @codes_list = map {
397 { 'code' => $$_{'value'},
398 'desc' => GetCriteriumDesc
( $$_{'value'}, $field ) || $$_{'value'},
399 'selected' => ($$suggestion_ref{$field}) ?
$$_{'value'} eq $$suggestion_ref{$field} : 0,
404 $hashlists{ lc($field) . "_loop" } = \
@codes_list;
409 borrowernumber
=> ($input->param('borrowernumber') // undef),
410 SuggestionStatuses
=> GetAuthorisedValues
('SUGGEST_STATUS'),
412 output_html_with_http_headers
$input, $cookie, $template->output;