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
31 use C4::Dates qw(format_date);
36 my $suggestion= shift @_;
37 # "Managed by" is used only when a suggestion is being edited (not when created)
38 if ($suggestion->{'suggesteddate'} eq "0000-00-00" ||$suggestion->{'suggesteddate'} eq "") {
40 $suggestion->{'suggesteddate'} = C4::Dates->today;
41 $suggestion->{'suggestedby'} = C4::Context->userenv->{"number"} unless ($suggestion->{'suggestedby'});
44 # editing of an existing suggestion
45 $suggestion->{'manageddate'} = C4::Dates->today;
46 $suggestion->{'managedby'} = C4::Context->userenv->{"number"} unless ($suggestion->{'managedby'});
47 # suggesteddate, when coming from the DB, needs to be formated
48 $suggestion->{'suggesteddate'} = format_date($suggestion->{'suggesteddate'});
50 foreach my $date ( qw(rejecteddate accepteddate) ){
51 $suggestion->{$date}=(($suggestion->{$date} eq "0000-00-00" ||$suggestion->{$date} eq "")?
53 format_date($suggestion->{$date})
56 $suggestion->{'branchcode'}=C4::Context->userenv->{"branch"} unless ($suggestion->{'branchcode'});
60 my ($criteriumvalue,$displayby)=@_;
61 return ($criteriumvalue eq 'ASKED'?"Pending":ucfirst(lc( $criteriumvalue))) if ($displayby =~/status/i);
62 return (GetBranchName($criteriumvalue)) if ($displayby =~/branchcode/);
63 return (GetSupportName($criteriumvalue)) if ($displayby =~/itemtype/);
64 if ($displayby =~/suggestedby/||$displayby =~/managedby/||$displayby =~/acceptedby/){
65 my $borr=C4::Members::GetMember(borrowernumber=>$criteriumvalue);
66 return "" unless $borr;
67 return $$borr{surname} . ", " . $$borr{firstname};
69 if ( $displayby =~ /budgetid/) {
70 my $budget = GetBudget($criteriumvalue);
71 return "" unless $budget;
72 return $$budget{budget_name};
77 my $suggestedbyme = (defined $input->param('suggestedbyme')? $input->param('suggestedbyme'):1);
78 my $op = $input->param('op')||'else';
79 my @editsuggestions = $input->param('edit_field');
80 my $suggestedby = $input->param('suggestedby');
81 my $returnsuggestedby = $input->param('returnsuggestedby');
82 my $returnsuggested = $input->param('returnsuggested');
83 my $managedby = $input->param('managedby');
84 my $displayby = $input->param('displayby') || '';
85 my $branchfilter = ($displayby ne "branchcode") ? $input->param('branchcode') : '';
86 my $tabcode = $input->param('tabcode');
88 # filter informations which are not suggestion related.
89 my $suggestion_ref = $input->Vars;
91 delete $$suggestion_ref{$_} foreach qw( suggestedbyme op displayby tabcode edit_field );
92 foreach (keys %$suggestion_ref){
93 delete $$suggestion_ref{$_} if (!$$suggestion_ref{$_} && ($op eq 'else' || $op eq 'change'));
95 my ( $template, $borrowernumber, $cookie, $userflags ) = get_template_and_user(
97 template_name => "suggestion/suggestion.tmpl",
100 flagsrequired => { catalogue => 1 },
104 #########################################
107 if ( $op =~ /save/i ) {
108 if ( $$suggestion_ref{"STATUS"} ) {
109 if ( my $tmpstatus = lc( $$suggestion_ref{"STATUS"} ) =~ /ACCEPTED|REJECTED/i ) {
110 $$suggestion_ref{ lc( $$suggestion_ref{"STATUS"}) . "date" } = C4::Dates->today;
111 $$suggestion_ref{ lc( $$suggestion_ref{"STATUS"}) . "by" } = C4::Context->userenv->{number};
113 $$suggestion_ref{"manageddate"} = C4::Dates->today;
114 $$suggestion_ref{"managedby"} = C4::Context->userenv->{number};
116 if ( $$suggestion_ref{'suggestionid'} > 0 ) {
117 &ModSuggestion($suggestion_ref);
119 ###FIXME:Search here if suggestion already exists.
120 my $suggestions_loop =
121 SearchSuggestion( $suggestion_ref );
122 if (@$suggestions_loop>=1){
123 #some suggestion are answering the request Donot Add
126 ## Adding some informations related to suggestion
127 &NewSuggestion($suggestion_ref);
129 # empty fields, to avoid filter in "SearchSuggestion"
131 map{delete $$suggestion_ref{$_}} keys %$suggestion_ref;
136 Init($suggestion_ref);
139 elsif ($op=~/edit/) {
141 $suggestion_ref=&GetSuggestion($$suggestion_ref{'suggestionid'});
142 Init($suggestion_ref);
145 elsif ($op eq "change" ) {
146 # set accepted/rejected/managed informations if applicable
147 # ie= if the librarian has choosen some action on the suggestions
148 if ($$suggestion_ref{"STATUS"} eq "ACCEPTED"){
149 $$suggestion_ref{"accepteddate"}=C4::Dates->today;
150 $$suggestion_ref{"acceptedby"}=C4::Context->userenv->{number};
151 } elsif ($$suggestion_ref{"STATUS"} eq "REJECTED"){
152 $$suggestion_ref{"rejecteddate"}=C4::Dates->today;
153 $$suggestion_ref{"rejectedby"}=C4::Context->userenv->{number};
155 if ($$suggestion_ref{"STATUS"}){
156 $$suggestion_ref{"manageddate"}=C4::Dates->today;
157 $$suggestion_ref{"managedby"}=C4::Context->userenv->{number};
159 if ( my $reason = $$suggestion_ref{"reason$tabcode"}){
160 if ( $reason eq "other" ) {
161 $reason = $$suggestion_ref{"other_reason$tabcode"};
163 $$suggestion_ref{'reason'}=$reason;
165 delete $$suggestion_ref{$_} foreach ("reason$tabcode", "other_reason$tabcode");
166 foreach (keys %$suggestion_ref){
167 delete $$suggestion_ref{$_} unless ($$suggestion_ref{$_});
169 foreach my $suggestionid (@editsuggestions) {
170 next unless $suggestionid;
171 $$suggestion_ref{'suggestionid'}=$suggestionid;
172 &ModSuggestion($suggestion_ref);
175 }elsif ($op eq "delete" ) {
176 foreach my $delete_field (@editsuggestions) {
177 &DelSuggestion( $borrowernumber, $delete_field,'intranet' );
181 elsif ( $op eq 'show' ) {
182 $suggestion_ref=&GetSuggestion($$suggestion_ref{'suggestionid'});
183 $$suggestion_ref{branchname} = GetBranchName $$suggestion_ref{branchcode};
184 my $budget = GetBudget $$suggestion_ref{budgetid};
185 $$suggestion_ref{budgetname} = $$budget{budget_name};
186 Init($suggestion_ref);
191 $displayby||="STATUS";
192 delete $$suggestion_ref{'branchcode'} if($displayby eq "branchcode");
193 # distinct values of display by
194 my $criteria_list=GetDistinctValues("suggestions.".$displayby);
195 my (@criteria_dv, $criteria_has_empty);
196 foreach (@$criteria_list) {
198 push @criteria_dv, $_->{value};
200 $criteria_has_empty = 1;
203 # agregate null and empty values under empty value
204 push @criteria_dv, '' if $criteria_has_empty;
207 my $reasonsloop = GetAuthorisedValues("SUGGEST");
208 foreach my $criteriumvalue ( @criteria_dv ) {
209 # By default, display suggestions from current working branch
210 if(not defined $branchfilter) {
211 $$suggestion_ref{'branchcode'} = C4::Context->userenv->{'branch'};
213 my $definedvalue = defined $$suggestion_ref{$displayby} && $$suggestion_ref{$displayby} ne "";
215 next if ( $definedvalue && $$suggestion_ref{$displayby} ne $criteriumvalue );
216 $$suggestion_ref{$displayby} = $criteriumvalue;
218 my $suggestions = &SearchSuggestion($suggestion_ref);
219 foreach my $suggestion (@$suggestions) {
220 if ($suggestion->{budgetid}){
221 my $bud = GetBudget( $suggestion->{budgetid} );
222 $suggestion->{budget_name} = $bud->{budget_name} if $bud;
224 foreach my $date (qw(suggesteddate manageddate accepteddate)) {
225 if ($suggestion->{$date} and $suggestion->{$date} ne "0000-00-00") {
226 $suggestion->{$date} = format_date( $suggestion->{$date} );
228 $suggestion->{$date} = "";
232 push @allsuggestions,{
233 "suggestiontype"=>$criteriumvalue||"suggest",
234 "suggestiontypelabel"=>GetCriteriumDesc($criteriumvalue,$displayby)||"",
235 "suggestionscount"=>scalar(@$suggestions),
236 'suggestions_loop'=>$suggestions,
237 'reasonsloop' => $reasonsloop,
240 delete $$suggestion_ref{$displayby} unless $definedvalue;
244 "displayby"=> $displayby,
245 "notabs"=> $displayby eq "",
246 suggestions => \@allsuggestions,
250 foreach my $element ( qw(managedby suggestedby acceptedby) ) {
251 # $debug || warn $$suggestion_ref{$element};
252 if ($$suggestion_ref{$element}){
253 my $member=GetMember(borrowernumber=>$$suggestion_ref{$element});
255 $element."_borrowernumber"=>$$member{borrowernumber},
256 $element."_firstname"=>$$member{firstname},
257 $element."_surname"=>$$member{surname},
258 $element."_branchcode"=>$$member{branchcode},
259 $element."_description"=>$$member{description},
260 $element."_category_type"=>$$member{category_type}
270 if(defined($returnsuggested) and $returnsuggested ne "noone")
272 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=".$returnsuggested."#suggestions");
276 ## Initializing selection lists
278 #branch display management
279 my $onlymine=C4::Context->preference('IndependantBranches') &&
280 C4::Context->userenv &&
281 C4::Context->userenv->{flags}!=1 &&
282 C4::Context->userenv->{branch};
283 my $branches = GetBranches($onlymine);
286 foreach my $thisbranch ( sort {$branches->{$a}->{'branchname'} cmp $branches->{$b}->{'branchname'}} keys %$branches ) {
288 value => $thisbranch,
289 branchname => $branches->{$thisbranch}->{'branchname'},
290 selected => ($branchfilter and $branches->{$thisbranch}->{'branchcode'} eq $branchfilter ) || ( $$suggestion_ref{'branchcode'} and $branches->{$thisbranch}->{'branchcode'} eq $$suggestion_ref{'branchcode'} )
292 push @branchloop, \%row;
294 $branchfilter=C4::Context->userenv->{'branch'} if ($onlymine && !$branchfilter);
296 $template->param( branchloop => \@branchloop,
297 branchfilter => $branchfilter);
299 # the index parameter is different for item-level itemtypes
300 my $supportlist = GetSupportList();
302 foreach my $support (@$supportlist) {
303 $$support{'selected'} = (defined $$suggestion_ref{'itemtype'})
304 ? $$support{'itemtype'} eq $$suggestion_ref{'itemtype'}
306 if ( $$support{'imageurl'} ) {
307 $$support{'imageurl'} = getitemtypeimagelocation( 'intranet', $$support{'imageurl'} );
309 delete $$support{'imageurl'};
312 $template->param(itemtypeloop=>$supportlist);
313 $template->param( returnsuggestedby => $returnsuggestedby );
315 my $patron_reason_loop = GetAuthorisedValues("OPAC_SUG",$$suggestion_ref{'patronreason'});
316 $template->param(patron_reason_loop=>$patron_reason_loop);
321 my $searchbudgets = { budget_branchcode => $branchfilter };
322 $budgets = GetBudgets($searchbudgets);
324 $budgets = GetBudgets(undef);
328 foreach my $budget ( @{$budgets} ) {
329 next unless (CanUserUseBudget($borrowernumber, $budget, $userflags));
331 ## Please see file perltidy.ERR
332 $budget->{'selected'} = 1
333 if ($$suggestion_ref{'budgetid'}
334 && $budget->{'budget_id'} eq $$suggestion_ref{'budgetid'});
336 push @budgets_loop, $budget;
339 $template->param( budgetsloop => \@budgets_loop);
340 $template->param( "statusselected_$$suggestion_ref{'STATUS'}" =>1) if ($$suggestion_ref{'STATUS'});
342 # get currencies and rates
343 my @rates = GetCurrencies();
344 my $count = scalar @rates;
345 my $active_currency = GetCurrency();
346 my $selected_currency;
347 if ($$suggestion_ref{'currency'}) {
348 $selected_currency = $$suggestion_ref{'currency'};
351 $selected_currency = $active_currency->{currency};
354 my @loop_currency = ();
355 for ( my $i = 0 ; $i < $count ; $i++ ) {
357 $line{currcode} = $rates[$i]->{'currency'};
358 $line{rate} = $rates[$i]->{'rate'};
359 $line{selected} = 1 if ($line{'currcode'} eq $selected_currency);
360 push @loop_currency, \%line;
363 $template->param(loop_currency => \@loop_currency);
366 price => sprintf("%.2f", $$suggestion_ref{'price'}||0),
367 total => sprintf("%.2f", $$suggestion_ref{'total'}||0),
370 # lists of distinct values (without empty) for filters
372 foreach my $field ( qw(managedby acceptedby suggestedby budgetid) ) {
374 $values_list = GetDistinctValues( "suggestions." . $field );
375 my @codes_list = map {
376 { 'code' => $$_{'value'},
377 'desc' => GetCriteriumDesc( $$_{'value'}, $field ) || $$_{'value'},
378 'selected' => ($$suggestion_ref{$field}) ? $$_{'value'} eq $$suggestion_ref{$field} : 0,
383 $hashlists{ lc($field) . "_loop" } = \@codes_list;
385 $template->param(%hashlists);
386 output_html_with_http_headers $input, $cookie, $template->output;