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 foreach my $date ( qw(suggesteddate manageddate) ){
38 $suggestion->{$date}=(($suggestion->{$date} eq "0000-00-00" ||$suggestion->{$date} eq "")?
39 $suggestion->{$date}=C4
::Dates
->today:
40 format_date
($suggestion->{$date})
43 foreach my $date ( qw(rejecteddate accepteddate) ){
44 $suggestion->{$date}=(($suggestion->{$date} eq "0000-00-00" ||$suggestion->{$date} eq "")?
46 format_date
($suggestion->{$date})
49 $suggestion->{'managedby'}=C4
::Context
->userenv->{"number"} unless ($suggestion->{'managedby'});
50 $suggestion->{'createdby'}=C4
::Context
->userenv->{"number"} unless ($suggestion->{'createdby'});
51 $suggestion->{'branchcode'}=C4
::Context
->userenv->{"branch"} unless ($suggestion->{'branchcode'});
55 my ($criteriumvalue,$displayby)=@_;
56 return ($criteriumvalue eq 'ASKED'?
"Pending":ucfirst(lc( $criteriumvalue))) if ($displayby =~/status/i);
57 return (GetBranchName
($criteriumvalue)) if ($displayby =~/branchcode/);
58 return (GetSupportName
($criteriumvalue)) if ($displayby =~/itemtype/);
59 if ($displayby =~/suggestedby/||$displayby =~/managedby/||$displayby =~/acceptedby/){
60 my $borr=C4
::Members
::GetMember
(borrowernumber
=>$criteriumvalue);
61 return "" unless $borr;
62 return $$borr{surname
} . ", " . $$borr{firstname
};
64 if ( $displayby =~ /budgetid/) {
65 my $budget = GetBudget
($criteriumvalue);
66 return "" unless $budget;
67 return $$budget{budget_name
};
72 my $suggestedbyme = (defined $input->param('suggestedbyme')?
$input->param('suggestedbyme'):1);
73 my $op = $input->param('op')||'else';
74 my @editsuggestions = $input->param('edit_field');
75 my $branchfilter = $input->param('branchcode');
76 my $suggestedby = $input->param('suggestedby');
77 my $returnsuggested = $input->param('returnsuggested');
78 my $returnsuggestedby = $input->param('returnsuggestedby');
79 my $managedby = $input->param('managedby');
80 my $displayby = $input->param('displayby');
81 my $tabcode = $input->param('tabcode');
83 # filter informations which are not suggestion related.
84 my $suggestion_ref = $input->Vars;
86 delete $$suggestion_ref{$_} foreach qw( suggestedbyme op displayby tabcode edit_field );
87 foreach (keys %$suggestion_ref){
88 delete $$suggestion_ref{$_} if (!$$suggestion_ref{$_} && ($op eq 'else' || $op eq 'change'));
90 my ( $template, $borrowernumber, $cookie ) = get_template_and_user
(
92 template_name
=> "suggestion/suggestion.tmpl",
95 flagsrequired
=> { catalogue
=> 1 },
99 #########################################
102 if ( $op =~ /save/i ) {
103 if ( $$suggestion_ref{"STATUS"} ) {
104 if ( my $tmpstatus = lc( $$suggestion_ref{"STATUS"} ) =~ /ACCEPTED|REJECTED/i ) {
105 $$suggestion_ref{ lc( $$suggestion_ref{"STATUS"}) . "date" } = C4
::Dates
->today;
106 $$suggestion_ref{ lc( $$suggestion_ref{"STATUS"}) . "by" } = C4
::Context
->userenv->{number
};
108 $$suggestion_ref{"manageddate"} = C4
::Dates
->today;
109 $$suggestion_ref{"managedby"} = C4
::Context
->userenv->{number
};
111 if ( $$suggestion_ref{'suggestionid'} > 0 ) {
112 &ModSuggestion
($suggestion_ref);
114 ###FIXME:Search here if suggestion already exists.
115 my $suggestions_loop =
116 SearchSuggestion
( $suggestion_ref );
117 if (@
$suggestions_loop>=1){
118 #some suggestion are answering the request Donot Add
121 ## Adding some informations related to suggestion
122 &NewSuggestion
($suggestion_ref);
124 # empty fields, to avoid filter in "SearchSuggestion"
126 map{delete $$suggestion_ref{$_}} keys %$suggestion_ref;
131 Init
($suggestion_ref);
134 elsif ($op=~/edit/) {
136 $suggestion_ref=&GetSuggestion
($$suggestion_ref{'suggestionid'});
137 Init
($suggestion_ref);
140 elsif ($op eq "change" ) {
141 # set accepted/rejected/managed informations if applicable
142 # ie= if the librarian has choosen some action on the suggestions
143 if ($$suggestion_ref{"STATUS"} eq "ACCEPTED"){
144 $$suggestion_ref{"accepteddate"}=C4
::Dates
->today;
145 $$suggestion_ref{"acceptedby"}=C4
::Context
->userenv->{number
};
146 } elsif ($$suggestion_ref{"STATUS"} eq "REJECTED"){
147 $$suggestion_ref{"rejecteddate"}=C4
::Dates
->today;
148 $$suggestion_ref{"rejectedby"}=C4
::Context
->userenv->{number
};
150 if ($$suggestion_ref{"STATUS"}){
151 $$suggestion_ref{"manageddate"}=C4
::Dates
->today;
152 $$suggestion_ref{"managedby"}=C4
::Context
->userenv->{number
};
154 if ( my $reason = $$suggestion_ref{"reason$tabcode"}){
155 if ( $reason eq "other" ) {
156 $reason = $$suggestion_ref{"other_reason$tabcode"};
158 $$suggestion_ref{'reason'}=$reason;
160 delete $$suggestion_ref{$_} foreach ("reason$tabcode", "other_reason$tabcode");
161 foreach (keys %$suggestion_ref){
162 delete $$suggestion_ref{$_} unless ($$suggestion_ref{$_});
164 foreach my $suggestionid (@editsuggestions) {
165 next unless $suggestionid;
166 $$suggestion_ref{'suggestionid'}=$suggestionid;
167 &ModSuggestion
($suggestion_ref);
170 }elsif ($op eq "delete" ) {
171 foreach my $delete_field (@editsuggestions) {
172 &DelSuggestion
( $borrowernumber, $delete_field,'intranet' );
176 elsif ( $op eq 'show' ) {
177 $suggestion_ref=&GetSuggestion
($$suggestion_ref{'suggestionid'});
178 $$suggestion_ref{branchname
} = GetBranchName
$$suggestion_ref{branchcode
};
179 my $budget = GetBudget
$$suggestion_ref{budgetid
};
180 $$suggestion_ref{budgetname
} = $$budget{budget_name
};
181 Init
($suggestion_ref);
186 $displayby||="STATUS";
187 my $criteria_list=GetDistinctValues
("suggestions.".$displayby);
189 my $reasonsloop = GetAuthorisedValues
("SUGGEST");
190 foreach my $criteriumvalue (map{$$_{'value'}} @
$criteria_list){
191 my $definedvalue = defined $$suggestion_ref{$displayby} && $$suggestion_ref{$displayby} ne "";
193 next if ($definedvalue && $$suggestion_ref{$displayby} ne $criteriumvalue);
194 $$suggestion_ref{$displayby}=$criteriumvalue;
195 # warn $$suggestion_ref{$displayby}."=$criteriumvalue; $displayby";
197 my $suggestions = &SearchSuggestion
($suggestion_ref);
198 foreach my $suggestion (@
$suggestions){
199 if($suggestion->{budgetid
}) {
200 my $budget = GetBudget
($suggestion->{budgetid
});
201 $suggestion->{budget_name
}=$budget->{budget_name
} if $budget;
203 foreach my $date ( qw(suggesteddate manageddate accepteddate) ){
204 if ($suggestion->{$date} ne "0000-00-00" && $suggestion->{$date} ne "" ){
205 $suggestion->{$date}=format_date
($suggestion->{$date}) ;
207 $suggestion->{$date}="" ;
211 push @allsuggestions,{
212 "suggestiontype"=>$criteriumvalue||"suggest",
213 "suggestiontypelabel"=>GetCriteriumDesc
($criteriumvalue,$displayby)||"",
214 "suggestionscount"=>scalar(@
$suggestions),
215 'suggestions_loop'=>$suggestions,
216 'reasonsloop' => $reasonsloop,
219 delete $$suggestion_ref{$displayby} unless $definedvalue;
223 "displayby"=> $displayby,
224 "notabs"=> $displayby eq "",
225 suggestions
=> \
@allsuggestions,
229 foreach my $element ( qw(managedby suggestedby acceptedby) ) {
230 # $debug || warn $$suggestion_ref{$element};
231 if ($$suggestion_ref{$element}){
232 my $member=GetMember
(borrowernumber
=>$$suggestion_ref{$element});
234 $element."_borrowernumber"=>$$member{borrowernumber
},
235 $element."_firstname"=>$$member{firstname
},
236 $element."_surname"=>$$member{surname
},
237 $element."_branchcode"=>$$member{branchcode
},
238 $element."_description"=>$$member{description
},
239 $element."_category_type"=>$$member{category_type
}
246 dateformat
=> C4
::Context
->preference("dateformat"),
250 if(defined($returnsuggested) and $returnsuggested ne "noone")
252 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=".$returnsuggested."#suggestions");
256 ## Initializing selection lists
258 #branch display management
259 my $onlymine=C4
::Context
->preference('IndependantBranches') &&
260 C4
::Context
->userenv &&
261 C4
::Context
->userenv->{flags
}!=1 &&
262 C4
::Context
->userenv->{branch
};
263 my $branches = GetBranches
($onlymine);
266 foreach my $thisbranch ( sort {$branches->{$a}->{'branchname'} cmp $branches->{$b}->{'branchname'}} keys %$branches ) {
268 value
=> $thisbranch,
269 branchname
=> $branches->{$thisbranch}->{'branchname'},
270 selected
=> ($branches->{$thisbranch}->{'branchcode'} eq $branchfilter)
271 ||($branches->{$thisbranch}->{'branchcode'} eq $$suggestion_ref{'branchcode'})
273 push @branchloop, \
%row;
275 $branchfilter=C4
::Context
->userenv->{'branch'} if ($onlymine && !$branchfilter);
277 $template->param( branchloop
=> \
@branchloop,
278 branchfilter
=> $branchfilter);
280 # the index parameter is different for item-level itemtypes
281 my $supportlist=GetSupportList
();
282 foreach my $support(@
$supportlist){
283 $$support{'selected'}= $$support{'itemtype'} eq $$suggestion_ref{'itemtype'};
284 if ($$support{'imageurl'}){
285 $$support{'imageurl'}= getitemtypeimagelocation
( 'intranet', $$support{'imageurl'} );
288 delete $$support{'imageurl'}
291 $template->param(itemtypeloop
=>$supportlist);
292 $template->param( returnsuggestedby
=> $returnsuggestedby );
294 my $patron_reason_loop = GetAuthorisedValues
("OPAC_SUG",$$suggestion_ref{'patronreason'});
295 $template->param(patron_reason_loop
=>$patron_reason_loop);
300 my $searchbudgets = { budget_branchcode
=> $branchfilter };
301 $budgets = GetBudgets
($searchbudgets);
303 $budgets = GetBudgets
(undef);
306 foreach my $budget ( @
{$budgets} ) {
307 ## Please see file perltidy.ERR
308 $budget->{'selected'}=1 if ($$suggestion_ref{'budgetid'} && $budget->{'budget_id'} eq $$suggestion_ref{'budgetid'})
311 $template->param( budgetsloop
=> $budgets);
312 $template->param( "statusselected_$$suggestion_ref{'STATUS'}" =>1);
314 # get currencies and rates
315 my @rates = GetCurrencies
();
316 my $count = scalar @rates;
317 my $active_currency = GetCurrency
();
318 my $selected_currency;
319 if ($$suggestion_ref{'currency'}) {
320 $selected_currency = $$suggestion_ref{'currency'};
323 $selected_currency = $active_currency->{currency
};
326 my @loop_currency = ();
327 for ( my $i = 0 ; $i < $count ; $i++ ) {
329 $line{currcode
} = $rates[$i]->{'currency'};
330 $line{rate
} = $rates[$i]->{'rate'};
331 $line{selected
} = 1 if ($line{'currcode'} eq $selected_currency);
332 push @loop_currency, \
%line;
335 $template->param(loop_currency
=> \
@loop_currency);
338 price
=> sprintf("%.2f", $$suggestion_ref{'price'}||0),
339 total
=> sprintf("%.2f", $$suggestion_ref{'total'}||0),
343 foreach my $field ( qw(managedby acceptedby suggestedby budgetid) ) {
345 $values_list=GetDistinctValues
("suggestions.".$field) ;
346 my @codes_list = map{
347 { 'code'=>$$_{'value'},
348 'desc'=>GetCriteriumDesc
($$_{'value'},$field),
349 'selected'=> $$_{'value'} eq $$suggestion_ref{$field}
352 $hashlists{lc($field)."_loop"}=\
@codes_list;
354 $template->param(%hashlists);
355 $template->param(DHTMLcalendar_dateformat
=> C4
::Dates
->DHTMLcalendar(),);
356 output_html_with_http_headers
$input, $cookie, $template->output;