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 with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 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 =~/managedby/||$displayby =~/acceptedby/){
60 my $borr=C4::Members::GetMember(borrowernumber=>$criteriumvalue);
61 return "" unless $borr;
62 return $$borr{firstname} . ", " . $$borr{surname};
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 $managedby = $input->param('managedby');
78 my $displayby = $input->param('displayby');
79 my $tabcode = $input->param('tabcode');
81 # filter informations which are not suggestion related.
82 my $suggestion_ref = $input->Vars;
83 delete $$suggestion_ref{$_} foreach qw( suggestedbyme op displayby tabcode edit_field );
84 foreach (keys %$suggestion_ref){
85 delete $$suggestion_ref{$_} if (!$$suggestion_ref{$_} && ($op eq 'else' || $op eq 'change'));
87 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
89 template_name => "suggestion/suggestion.tmpl",
92 flagsrequired => { catalogue => 1 },
96 #########################################
100 if ($$suggestion_ref{'suggestionid'}>0){
101 &ModSuggestion($suggestion_ref);
104 ###FIXME:Search here if suggestion already exists.
105 my $suggestions_loop =
106 SearchSuggestion( $suggestion_ref );
107 if (@$suggestions_loop>=1){
108 #some suggestion are answering the request Donot Add
111 ## Adding some informations related to suggestion
112 &NewSuggestion($suggestion_ref);
114 # empty fields, to avoid filter in "SearchSuggestion"
116 map{delete $$suggestion_ref{$_}} keys %$suggestion_ref;
121 Init($suggestion_ref);
124 elsif ($op=~/edit/) {
126 $suggestion_ref=&GetSuggestion($$suggestion_ref{'suggestionid'});
127 Init($suggestion_ref);
130 elsif ($op eq "change" ) {
131 if ($$suggestion_ref{"STATUS"}){
132 if (my $tmpstatus=lc($$suggestion_ref{"STATUS"}) =~/ACCEPTED|REJECTED/i){
133 $$suggestion_ref{"$tmpstatus"."date"}=C4::Dates->today;
134 $$suggestion_ref{"$tmpstatus"."by"}=C4::Context->userenv->{number};
136 $$suggestion_ref{"manageddate"}=C4::Dates->today;
137 $$suggestion_ref{"managedby"}=C4::Context->userenv->{number};
139 if ( my $reason = $$suggestion_ref{"reason$tabcode"}){
140 if ( $reason eq "other" ) {
141 $reason = $$suggestion_ref{"other_reason$tabcode"};
143 $$suggestion_ref{'reason'}=$reason;
145 delete $$suggestion_ref{$_} foreach ("reason$tabcode", "other_reason$tabcode");
146 foreach (keys %$suggestion_ref){
147 delete $$suggestion_ref{$_} unless ($$suggestion_ref{$_});
149 foreach my $suggestionid (@editsuggestions) {
150 next unless $suggestionid;
151 $$suggestion_ref{'suggestionid'}=$suggestionid;
152 &ModSuggestion($suggestion_ref);
155 }elsif ($op eq "delete" ) {
156 foreach my $delete_field (@editsuggestions) {
157 &DelSuggestion( $borrowernumber, $delete_field,'intranet' );
164 $displayby||="STATUS";
165 my $criteria_list=GetDistinctValues("suggestions.".$displayby);
167 my $reasonsloop = GetAuthorisedValues("SUGGEST");
168 foreach my $criteriumvalue (map{$$_{'value'}} @$criteria_list){
169 my $definedvalue = defined $$suggestion_ref{$displayby} && $$suggestion_ref{$displayby} ne "";
171 next if ($definedvalue && $$suggestion_ref{$displayby} ne $criteriumvalue);
172 $$suggestion_ref{$displayby}=$criteriumvalue;
173 # warn $$suggestion_ref{$displayby}."=$criteriumvalue; $displayby";
175 my $suggestions = &SearchSuggestion($suggestion_ref);
176 foreach my $suggestion (@$suggestions){
177 $suggestion->{budget_name}=GetBudget($suggestion->{budgetid})->{budget_name} if $suggestion->{budgetid};
178 foreach my $date qw(suggesteddate manageddate accepteddate){
179 if ($suggestion->{$date} ne "0000-00-00" && $suggestion->{$date} ne "" ){
180 $suggestion->{$date}=format_date($suggestion->{$date}) ;
182 $suggestion->{$date}="" ;
186 push @allsuggestions,{
187 "suggestiontype"=>$criteriumvalue||"suggest",
188 "suggestiontypelabel"=>GetCriteriumDesc($criteriumvalue,$displayby)||"",
189 "suggestionscount"=>scalar(@$suggestions),
190 'suggestions_loop'=>$suggestions,
191 'reasonsloop' => $reasonsloop,
194 delete $$suggestion_ref{$displayby} unless $definedvalue;
198 "displayby"=> $displayby,
199 "notabs"=> $displayby eq "",
200 suggestions => \@allsuggestions,
204 foreach my $element qw(managedby suggestedby){
205 # $debug || warn $$suggestion_ref{$element};
206 if ($$suggestion_ref{$element}){
207 my $member=GetMember(borrowernumber=>$$suggestion_ref{$element});
209 $element."_borrowernumber"=>$$member{borrowernumber},
210 $element."_firstname"=>$$member{firstname},
211 $element."_surname"=>$$member{surname},
212 $element."_branchcode"=>$$member{branchcode},
213 $element."_description"=>$$member{description},
214 $element."_category_type"=>$$member{category_type}
221 dateformat => C4::Context->preference("dateformat"),
227 ## Initializing selection lists
229 #branch display management
230 my $onlymine=C4::Context->preference('IndependantBranches') &&
231 C4::Context->userenv &&
232 C4::Context->userenv->{flags}!=1 &&
233 C4::Context->userenv->{branch};
234 my $branches = GetBranches($onlymine);
237 foreach my $thisbranch ( sort {$branches->{$a}->{'branchname'} cmp $branches->{$b}->{'branchname'}} keys %$branches ) {
239 value => $thisbranch,
240 branchname => $branches->{$thisbranch}->{'branchname'},
241 selected => ($branches->{$thisbranch}->{'branchcode'} eq $branchfilter)
242 ||($branches->{$thisbranch}->{'branchcode'} eq $$suggestion_ref{'branchcode'})
244 push @branchloop, \%row;
246 $branchfilter=C4::Context->userenv->{'branch'} if ($onlymine && !$branchfilter);
248 $template->param( branchloop => \@branchloop,
249 branchfilter => $branchfilter);
251 # the index parameter is different for item-level itemtypes
252 my $supportlist=GetSupportList();
253 foreach my $support(@$supportlist){
254 $$support{'selected'}= $$support{'itemtype'} eq $$suggestion_ref{'itemtype'};
255 if ($$support{'imageurl'}){
256 $$support{'imageurl'}= getitemtypeimagelocation( 'intranet', $$support{'imageurl'} );
259 delete $$support{'imageurl'}
262 $template->param(itemtypeloop=>$supportlist);
264 my $patron_reason_loop = GetAuthorisedValues("OPAC_SUG",$$suggestion_ref{'patronreason'});
265 $template->param(patron_reason_loop=>$patron_reason_loop);
268 my $searchbudgets={ budget_branchcode=>$branchfilter} if $branchfilter;
269 my $budgets = GetBudgets($searchbudgets);
271 foreach my $budget (@$budgets){
272 $budget->{'selected'}=1 if ($$suggestion_ref{'budgetid'} && $budget->{'budget_id'} eq $$suggestion_ref{'budgetid'})
275 $template->param( budgetsloop => $budgets);
277 # get currencies and rates
278 my @rates = GetCurrencies();
279 my $count = scalar @rates;
281 my @loop_currency = ();
282 for ( my $i = 0 ; $i < $count ; $i++ ) {
284 $line{currcode} = $rates[$i]->{'currency'};
285 $line{rate} = $rates[$i]->{'rate'};
286 $line{selected} = 1 if ($$suggestion_ref{'currency'} && $line{'currcode'} eq $$suggestion_ref{'currency'});
287 push @loop_currency, \%line;
290 $template->param(loop_currency => \@loop_currency);
293 price => sprintf("%.2f", $$suggestion_ref{'price'}||0),
294 total => sprintf("%.2f", $$suggestion_ref{'total'}||0),
298 foreach my $field qw(managedby acceptedby suggestedby budgetid STATUS) {
300 $values_list=GetDistinctValues("suggestions.".$field) ;
301 my @codes_list = map{
302 { 'code'=>$$_{'value'},
303 'desc'=>GetCriteriumDesc($$_{'value'},$field),
304 'selected'=> $$_{'value'} eq $$suggestion_ref{$field}
307 $hashlists{lc($field)."_loop"}=\@codes_list;
309 $template->param(%hashlists);
310 $template->param(DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),);
311 output_html_with_http_headers $input, $cookie, $template->output;