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 unless ( grep { /$criteriumvalue/ } qw(ASKED ACCEPTED REJECTED CHECKED) ) {
62 return GetAuthorisedValueByCode
('SUGGEST_STATUS', $criteriumvalue ) || "Unknown";
64 return ($criteriumvalue eq 'ASKED'?
"Pending":ucfirst(lc( $criteriumvalue))) if ($displayby =~/status/i);
65 return (GetBranchName
($criteriumvalue)) if ($displayby =~/branchcode/);
66 return (GetSupportName
($criteriumvalue)) if ($displayby =~/itemtype/);
67 if ($displayby =~/suggestedby/||$displayby =~/managedby/||$displayby =~/acceptedby/){
68 my $borr=C4
::Members
::GetMember
(borrowernumber
=>$criteriumvalue);
69 return "" unless $borr;
70 return $$borr{surname
} . ", " . $$borr{firstname
};
72 if ( $displayby =~ /budgetid/) {
73 my $budget = GetBudget
($criteriumvalue);
74 return "" unless $budget;
75 return $$budget{budget_name
};
80 my $redirect = $input->param('redirect');
81 my $suggestedbyme = (defined $input->param('suggestedbyme')?
$input->param('suggestedbyme'):1);
82 my $op = $input->param('op')||'else';
83 my @editsuggestions = $input->param('edit_field');
84 my $suggestedby = $input->param('suggestedby');
85 my $returnsuggestedby = $input->param('returnsuggestedby');
86 my $returnsuggested = $input->param('returnsuggested');
87 my $managedby = $input->param('managedby');
88 my $displayby = $input->param('displayby') || '';
89 my $tabcode = $input->param('tabcode');
91 # filter informations which are not suggestion related.
92 my $suggestion_ref = $input->Vars;
94 delete $$suggestion_ref{$_} foreach qw( suggestedbyme op displayby tabcode edit_field );
95 foreach (keys %$suggestion_ref){
96 delete $$suggestion_ref{$_} if (!$$suggestion_ref{$_} && ($op eq 'else' || $op eq 'change'));
98 my ( $template, $borrowernumber, $cookie, $userflags ) = get_template_and_user
(
100 template_name
=> "suggestion/suggestion.tmpl",
103 flagsrequired
=> { catalogue
=> 1 },
107 $borrowernumber = $input->param('borrowernumber') if ( $input->param('borrowernumber') );
108 $template->param('borrowernumber' => $borrowernumber);
110 #########################################
113 if ( $op =~ /save/i ) {
114 if ( $$suggestion_ref{"STATUS"} ) {
115 if ( my $tmpstatus = lc( $$suggestion_ref{"STATUS"} ) =~ /ACCEPTED|REJECTED/i ) {
116 $$suggestion_ref{ lc( $$suggestion_ref{"STATUS"}) . "date" } = C4
::Dates
->today;
117 $$suggestion_ref{ lc( $$suggestion_ref{"STATUS"}) . "by" } = C4
::Context
->userenv->{number
};
119 $$suggestion_ref{"manageddate"} = C4
::Dates
->today;
120 $$suggestion_ref{"managedby"} = C4
::Context
->userenv->{number
};
122 if ( $$suggestion_ref{'suggestionid'} > 0 ) {
123 &ModSuggestion
($suggestion_ref);
125 ###FIXME:Search here if suggestion already exists.
126 my $suggestions_loop =
127 SearchSuggestion
( $suggestion_ref );
128 if (@
$suggestions_loop>=1){
129 #some suggestion are answering the request Donot Add
132 ## Adding some informations related to suggestion
133 &NewSuggestion
($suggestion_ref);
135 # empty fields, to avoid filter in "SearchSuggestion"
137 map{delete $$suggestion_ref{$_}} keys %$suggestion_ref;
140 if( $redirect eq 'purchase_suggestions' ) {
141 print $input->redirect("/cgi-bin/koha/members/purchase-suggestions.pl?borrowernumber=$borrowernumber");
147 Init
($suggestion_ref);
150 elsif ($op=~/edit/) {
152 $suggestion_ref=&GetSuggestion
($$suggestion_ref{'suggestionid'});
153 Init
($suggestion_ref);
156 elsif ($op eq "change" ) {
157 # set accepted/rejected/managed informations if applicable
158 # ie= if the librarian has choosen some action on the suggestions
159 if ($$suggestion_ref{"STATUS"} eq "ACCEPTED"){
160 $$suggestion_ref{"accepteddate"}=C4
::Dates
->today;
161 $$suggestion_ref{"acceptedby"}=C4
::Context
->userenv->{number
};
162 } elsif ($$suggestion_ref{"STATUS"} eq "REJECTED"){
163 $$suggestion_ref{"rejecteddate"}=C4
::Dates
->today;
164 $$suggestion_ref{"rejectedby"}=C4
::Context
->userenv->{number
};
166 if ($$suggestion_ref{"STATUS"}){
167 $$suggestion_ref{"manageddate"}=C4
::Dates
->today;
168 $$suggestion_ref{"managedby"}=C4
::Context
->userenv->{number
};
170 if ( my $reason = $$suggestion_ref{"reason$tabcode"}){
171 if ( $reason eq "other" ) {
172 $reason = $$suggestion_ref{"other_reason$tabcode"};
174 $$suggestion_ref{'reason'}=$reason;
176 delete $$suggestion_ref{$_} foreach ("reason$tabcode", "other_reason$tabcode");
177 foreach (keys %$suggestion_ref){
178 delete $$suggestion_ref{$_} unless ($$suggestion_ref{$_});
180 foreach my $suggestionid (@editsuggestions) {
181 next unless $suggestionid;
182 $$suggestion_ref{'suggestionid'}=$suggestionid;
183 &ModSuggestion
($suggestion_ref);
186 }elsif ($op eq "delete" ) {
187 foreach my $delete_field (@editsuggestions) {
188 &DelSuggestion
( $borrowernumber, $delete_field,'intranet' );
192 elsif ( $op eq 'show' ) {
193 $suggestion_ref=&GetSuggestion
($$suggestion_ref{'suggestionid'});
194 $$suggestion_ref{branchname
} = GetBranchName
$$suggestion_ref{branchcode
};
195 my $budget = GetBudget
$$suggestion_ref{budgetid
};
196 $$suggestion_ref{budgetname
} = $$budget{budget_name
};
197 Init
($suggestion_ref);
202 $displayby||="STATUS";
203 delete $$suggestion_ref{'branchcode'} if($displayby eq "branchcode");
204 # distinct values of display by
205 my $criteria_list=GetDistinctValues
("suggestions.".$displayby);
206 my (@criteria_dv, $criteria_has_empty);
207 foreach (@
$criteria_list) {
209 push @criteria_dv, $_->{value
};
211 $criteria_has_empty = 1;
214 # agregate null and empty values under empty value
215 push @criteria_dv, '' if $criteria_has_empty;
218 my $reasonsloop = GetAuthorisedValues
("SUGGEST");
219 foreach my $criteriumvalue ( @criteria_dv ) {
220 # By default, display suggestions from current working branch
221 unless ( exists $$suggestion_ref{'branchcode'} ) {
222 $$suggestion_ref{'branchcode'} = C4
::Context
->userenv->{'branch'};
224 my $definedvalue = defined $$suggestion_ref{$displayby} && $$suggestion_ref{$displayby} ne "";
226 next if ( $definedvalue && $$suggestion_ref{$displayby} ne $criteriumvalue );
227 $$suggestion_ref{$displayby} = $criteriumvalue;
229 my $suggestions = &SearchSuggestion
($suggestion_ref);
230 foreach my $suggestion (@
$suggestions) {
231 if ($suggestion->{budgetid
}){
232 my $bud = GetBudget
( $suggestion->{budgetid
} );
233 $suggestion->{budget_name
} = $bud->{budget_name
} if $bud;
235 foreach my $date (qw(suggesteddate manageddate accepteddate)) {
236 if ($suggestion->{$date} and $suggestion->{$date} ne "0000-00-00") {
237 $suggestion->{$date} = format_date
( $suggestion->{$date} );
239 $suggestion->{$date} = "";
243 push @allsuggestions,{
244 "suggestiontype"=>$criteriumvalue||"suggest",
245 "suggestiontypelabel"=>GetCriteriumDesc
($criteriumvalue,$displayby)||"",
246 "suggestionscount"=>scalar(@
$suggestions),
247 'suggestions_loop'=>$suggestions,
248 'reasonsloop' => $reasonsloop,
251 delete $$suggestion_ref{$displayby} unless $definedvalue;
255 "displayby"=> $displayby,
256 "notabs"=> $displayby eq "",
257 suggestions
=> \
@allsuggestions,
261 foreach my $element ( qw(managedby suggestedby acceptedby) ) {
262 # $debug || warn $$suggestion_ref{$element};
263 if ($$suggestion_ref{$element}){
264 my $member=GetMember
(borrowernumber
=>$$suggestion_ref{$element});
266 $element."_borrowernumber"=>$$member{borrowernumber
},
267 $element."_firstname"=>$$member{firstname
},
268 $element."_surname"=>$$member{surname
},
269 $element."_branchcode"=>$$member{branchcode
},
270 $element."_description"=>$$member{description
},
271 $element."_category_type"=>$$member{category_type
}
281 if(defined($returnsuggested) and $returnsuggested ne "noone")
283 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=".$returnsuggested."#suggestions");
287 ## Initializing selection lists
289 #branch display management
290 my $branchfilter = ($displayby ne "branchcode") ?
$input->param('branchcode') : '';
291 my $onlymine=C4
::Context
->preference('IndependentBranches') &&
292 C4
::Context
->userenv &&
293 C4
::Context
->userenv->{flags
}!=1 &&
294 C4
::Context
->userenv->{branch
};
295 my $branches = GetBranches
($onlymine);
298 foreach my $thisbranch ( sort {$branches->{$a}->{'branchname'} cmp $branches->{$b}->{'branchname'}} keys %$branches ) {
300 value
=> $thisbranch,
301 branchname
=> $branches->{$thisbranch}->{'branchname'},
302 selected
=> ($branchfilter and $branches->{$thisbranch}->{'branchcode'} eq $branchfilter ) || ( $$suggestion_ref{'branchcode'} and $branches->{$thisbranch}->{'branchcode'} eq $$suggestion_ref{'branchcode'} )
304 push @branchloop, \
%row;
306 $branchfilter=C4
::Context
->userenv->{'branch'} if ($onlymine && !$branchfilter);
308 $template->param( branchloop
=> \
@branchloop,
309 branchfilter
=> $branchfilter);
311 # the index parameter is different for item-level itemtypes
312 my $supportlist = GetSupportList
();
314 foreach my $support (@
$supportlist) {
315 $$support{'selected'} = (defined $$suggestion_ref{'itemtype'})
316 ?
$$support{'itemtype'} eq $$suggestion_ref{'itemtype'}
318 if ( $$support{'imageurl'} ) {
319 $$support{'imageurl'} = getitemtypeimagelocation
( 'intranet', $$support{'imageurl'} );
321 delete $$support{'imageurl'};
324 $template->param(itemtypeloop
=>$supportlist);
325 $template->param( returnsuggestedby
=> $returnsuggestedby );
327 my $patron_reason_loop = GetAuthorisedValues
("OPAC_SUG",$$suggestion_ref{'patronreason'});
328 $template->param(patron_reason_loop
=>$patron_reason_loop);
333 my $searchbudgets = { budget_branchcode
=> $branchfilter };
334 $budgets = GetBudgets
($searchbudgets);
336 $budgets = GetBudgets
(undef);
340 foreach my $budget ( @
{$budgets} ) {
341 next unless (CanUserUseBudget
($borrowernumber, $budget, $userflags));
343 ## Please see file perltidy.ERR
344 $budget->{'selected'} = 1
345 if ($$suggestion_ref{'budgetid'}
346 && $budget->{'budget_id'} eq $$suggestion_ref{'budgetid'});
348 push @budgets_loop, $budget;
351 $template->param( budgetsloop
=> \
@budgets_loop);
352 $template->param( "statusselected_$$suggestion_ref{'STATUS'}" =>1) if ($$suggestion_ref{'STATUS'});
354 # get currencies and rates
355 my @rates = GetCurrencies
();
356 my $count = scalar @rates;
357 my $active_currency = GetCurrency
();
358 my $selected_currency;
359 if ($$suggestion_ref{'currency'}) {
360 $selected_currency = $$suggestion_ref{'currency'};
363 $selected_currency = $active_currency->{currency
};
366 my @loop_currency = ();
367 for ( my $i = 0 ; $i < $count ; $i++ ) {
369 $line{currcode
} = $rates[$i]->{'currency'};
370 $line{rate
} = $rates[$i]->{'rate'};
371 $line{selected
} = 1 if ($line{'currcode'} eq $selected_currency);
372 push @loop_currency, \
%line;
375 loop_currency
=> \
@loop_currency,
376 price
=> sprintf("%.2f", $$suggestion_ref{'price'}||0),
377 total
=> sprintf("%.2f", $$suggestion_ref{'total'}||0),
380 # lists of distinct values (without empty) for filters
382 foreach my $field ( qw(managedby acceptedby suggestedby budgetid) ) {
384 $values_list = GetDistinctValues
( "suggestions." . $field );
385 my @codes_list = map {
386 { 'code' => $$_{'value'},
387 'desc' => GetCriteriumDesc
( $$_{'value'}, $field ) || $$_{'value'},
388 'selected' => ($$suggestion_ref{$field}) ?
$$_{'value'} eq $$suggestion_ref{$field} : 0,
393 $hashlists{ lc($field) . "_loop" } = \
@codes_list;
398 borrowernumber
=> $input->param('borrowernumber'),
399 SuggestionStatuses
=> GetAuthorisedValues
('SUGGEST_STATUS'),
401 output_html_with_http_headers
$input, $cookie, $template->output;