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 warn $suggestion->{'manageddate'};
47 $suggestion->{'managedby'} = C4::Context->userenv->{"number"} unless ($suggestion->{'managedby'});
48 # suggesteddate, when coming from the DB, needs to be formated
49 $suggestion->{'suggesteddate'} = format_date($suggestion->{'suggesteddate'});
51 foreach my $date ( qw(rejecteddate accepteddate) ){
52 $suggestion->{$date}=(($suggestion->{$date} eq "0000-00-00" ||$suggestion->{$date} eq "")?
54 format_date($suggestion->{$date})
57 $suggestion->{'branchcode'}=C4::Context->userenv->{"branch"} unless ($suggestion->{'branchcode'});
61 my ($criteriumvalue,$displayby)=@_;
62 return ($criteriumvalue eq 'ASKED'?"Pending":ucfirst(lc( $criteriumvalue))) if ($displayby =~/status/i);
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 $suggestedbyme = (defined $input->param('suggestedbyme')? $input->param('suggestedbyme'):1);
79 my $op = $input->param('op')||'else';
80 my @editsuggestions = $input->param('edit_field');
81 my $branchfilter = $input->param('branchcode');
82 my $suggestedby = $input->param('suggestedby');
83 my $returnsuggested = $input->param('returnsuggested');
84 my $returnsuggestedby = $input->param('returnsuggestedby');
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 delete $$suggestion_ref{$_} foreach qw( suggestedbyme op displayby tabcode edit_field );
93 foreach (keys %$suggestion_ref){
94 delete $$suggestion_ref{$_} if (!$$suggestion_ref{$_} && ($op eq 'else' || $op eq 'change'));
96 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
98 template_name => "suggestion/suggestion.tmpl",
101 flagsrequired => { catalogue => 1 },
105 #########################################
108 if ( $op =~ /save/i ) {
109 if ( $$suggestion_ref{"STATUS"} ) {
110 if ( my $tmpstatus = lc( $$suggestion_ref{"STATUS"} ) =~ /ACCEPTED|REJECTED/i ) {
111 $$suggestion_ref{ lc( $$suggestion_ref{"STATUS"}) . "date" } = C4::Dates->today;
112 $$suggestion_ref{ lc( $$suggestion_ref{"STATUS"}) . "by" } = C4::Context->userenv->{number};
114 $$suggestion_ref{"manageddate"} = C4::Dates->today;
115 $$suggestion_ref{"managedby"} = C4::Context->userenv->{number};
117 if ( $$suggestion_ref{'suggestionid'} > 0 ) {
118 &ModSuggestion($suggestion_ref);
120 ###FIXME:Search here if suggestion already exists.
121 my $suggestions_loop =
122 SearchSuggestion( $suggestion_ref );
123 if (@$suggestions_loop>=1){
124 #some suggestion are answering the request Donot Add
127 ## Adding some informations related to suggestion
128 &NewSuggestion($suggestion_ref);
130 # empty fields, to avoid filter in "SearchSuggestion"
132 map{delete $$suggestion_ref{$_}} keys %$suggestion_ref;
137 Init($suggestion_ref);
140 elsif ($op=~/edit/) {
142 $suggestion_ref=&GetSuggestion($$suggestion_ref{'suggestionid'});
143 Init($suggestion_ref);
146 elsif ($op eq "change" ) {
147 # set accepted/rejected/managed informations if applicable
148 # ie= if the librarian has choosen some action on the suggestions
149 if ($$suggestion_ref{"STATUS"} eq "ACCEPTED"){
150 $$suggestion_ref{"accepteddate"}=C4::Dates->today;
151 $$suggestion_ref{"acceptedby"}=C4::Context->userenv->{number};
152 } elsif ($$suggestion_ref{"STATUS"} eq "REJECTED"){
153 $$suggestion_ref{"rejecteddate"}=C4::Dates->today;
154 $$suggestion_ref{"rejectedby"}=C4::Context->userenv->{number};
156 if ($$suggestion_ref{"STATUS"}){
157 $$suggestion_ref{"manageddate"}=C4::Dates->today;
158 $$suggestion_ref{"managedby"}=C4::Context->userenv->{number};
160 if ( my $reason = $$suggestion_ref{"reason$tabcode"}){
161 if ( $reason eq "other" ) {
162 $reason = $$suggestion_ref{"other_reason$tabcode"};
164 $$suggestion_ref{'reason'}=$reason;
166 delete $$suggestion_ref{$_} foreach ("reason$tabcode", "other_reason$tabcode");
167 foreach (keys %$suggestion_ref){
168 delete $$suggestion_ref{$_} unless ($$suggestion_ref{$_});
170 foreach my $suggestionid (@editsuggestions) {
171 next unless $suggestionid;
172 $$suggestion_ref{'suggestionid'}=$suggestionid;
173 &ModSuggestion($suggestion_ref);
176 }elsif ($op eq "delete" ) {
177 foreach my $delete_field (@editsuggestions) {
178 &DelSuggestion( $borrowernumber, $delete_field,'intranet' );
182 elsif ( $op eq 'show' ) {
183 $suggestion_ref=&GetSuggestion($$suggestion_ref{'suggestionid'});
184 $$suggestion_ref{branchname} = GetBranchName $$suggestion_ref{branchcode};
185 my $budget = GetBudget $$suggestion_ref{budgetid};
186 $$suggestion_ref{budgetname} = $$budget{budget_name};
187 Init($suggestion_ref);
192 $displayby||="STATUS";
193 my $criteria_list=GetDistinctValues("suggestions.".$displayby);
195 my $reasonsloop = GetAuthorisedValues("SUGGEST");
196 foreach my $criteriumvalue (map{$$_{'value'}} @$criteria_list){
197 my $definedvalue = defined $$suggestion_ref{$displayby} && $$suggestion_ref{$displayby} ne "";
199 next if ($definedvalue && $$suggestion_ref{$displayby} ne $criteriumvalue);
200 $$suggestion_ref{$displayby}=$criteriumvalue;
201 # warn $$suggestion_ref{$displayby}."=$criteriumvalue; $displayby";
203 my $suggestions = &SearchSuggestion($suggestion_ref);
204 foreach my $suggestion (@$suggestions){
205 if($suggestion->{budgetid}) {
206 my $budget = GetBudget($suggestion->{budgetid});
207 $suggestion->{budget_name}=$budget->{budget_name} if $budget;
209 foreach my $date ( qw(suggesteddate manageddate accepteddate) ){
210 if ($suggestion->{$date} ne "0000-00-00" && $suggestion->{$date} ne "" ){
211 $suggestion->{$date}=format_date($suggestion->{$date}) ;
213 $suggestion->{$date}="" ;
217 push @allsuggestions,{
218 "suggestiontype"=>$criteriumvalue||"suggest",
219 "suggestiontypelabel"=>GetCriteriumDesc($criteriumvalue,$displayby)||"",
220 "suggestionscount"=>scalar(@$suggestions),
221 'suggestions_loop'=>$suggestions,
222 'reasonsloop' => $reasonsloop,
225 delete $$suggestion_ref{$displayby} unless $definedvalue;
229 "displayby"=> $displayby,
230 "notabs"=> $displayby eq "",
231 suggestions => \@allsuggestions,
235 foreach my $element ( qw(managedby suggestedby acceptedby) ) {
236 # $debug || warn $$suggestion_ref{$element};
237 if ($$suggestion_ref{$element}){
238 my $member=GetMember(borrowernumber=>$$suggestion_ref{$element});
240 $element."_borrowernumber"=>$$member{borrowernumber},
241 $element."_firstname"=>$$member{firstname},
242 $element."_surname"=>$$member{surname},
243 $element."_branchcode"=>$$member{branchcode},
244 $element."_description"=>$$member{description},
245 $element."_category_type"=>$$member{category_type}
252 dateformat => C4::Context->preference("dateformat"),
256 if(defined($returnsuggested) and $returnsuggested ne "noone")
258 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=".$returnsuggested."#suggestions");
262 ## Initializing selection lists
264 #branch display management
265 my $onlymine=C4::Context->preference('IndependantBranches') &&
266 C4::Context->userenv &&
267 C4::Context->userenv->{flags}!=1 &&
268 C4::Context->userenv->{branch};
269 my $branches = GetBranches($onlymine);
272 foreach my $thisbranch ( sort {$branches->{$a}->{'branchname'} cmp $branches->{$b}->{'branchname'}} keys %$branches ) {
274 value => $thisbranch,
275 branchname => $branches->{$thisbranch}->{'branchname'},
276 selected => ($branches->{$thisbranch}->{'branchcode'} eq $branchfilter)
277 ||($branches->{$thisbranch}->{'branchcode'} eq $$suggestion_ref{'branchcode'})
279 push @branchloop, \%row;
281 $branchfilter=C4::Context->userenv->{'branch'} if ($onlymine && !$branchfilter);
283 $template->param( branchloop => \@branchloop,
284 branchfilter => $branchfilter);
286 # the index parameter is different for item-level itemtypes
287 my $supportlist=GetSupportList();
288 foreach my $support(@$supportlist){
289 $$support{'selected'}= $$support{'itemtype'} eq $$suggestion_ref{'itemtype'};
290 if ($$support{'imageurl'}){
291 $$support{'imageurl'}= getitemtypeimagelocation( 'intranet', $$support{'imageurl'} );
294 delete $$support{'imageurl'}
297 $template->param(itemtypeloop=>$supportlist);
298 $template->param( returnsuggestedby => $returnsuggestedby );
300 my $patron_reason_loop = GetAuthorisedValues("OPAC_SUG",$$suggestion_ref{'patronreason'});
301 $template->param(patron_reason_loop=>$patron_reason_loop);
306 my $searchbudgets = { budget_branchcode => $branchfilter };
307 $budgets = GetBudgets($searchbudgets);
309 $budgets = GetBudgets(undef);
312 foreach my $budget ( @{$budgets} ) {
313 ## Please see file perltidy.ERR
314 $budget->{'selected'}=1 if ($$suggestion_ref{'budgetid'} && $budget->{'budget_id'} eq $$suggestion_ref{'budgetid'})
317 $template->param( budgetsloop => $budgets);
318 $template->param( "statusselected_$$suggestion_ref{'STATUS'}" =>1);
320 # get currencies and rates
321 my @rates = GetCurrencies();
322 my $count = scalar @rates;
323 my $active_currency = GetCurrency();
324 my $selected_currency;
325 if ($$suggestion_ref{'currency'}) {
326 $selected_currency = $$suggestion_ref{'currency'};
329 $selected_currency = $active_currency->{currency};
332 my @loop_currency = ();
333 for ( my $i = 0 ; $i < $count ; $i++ ) {
335 $line{currcode} = $rates[$i]->{'currency'};
336 $line{rate} = $rates[$i]->{'rate'};
337 $line{selected} = 1 if ($line{'currcode'} eq $selected_currency);
338 push @loop_currency, \%line;
341 $template->param(loop_currency => \@loop_currency);
344 price => sprintf("%.2f", $$suggestion_ref{'price'}||0),
345 total => sprintf("%.2f", $$suggestion_ref{'total'}||0),
349 foreach my $field ( qw(managedby acceptedby suggestedby budgetid) ) {
351 $values_list=GetDistinctValues("suggestions.".$field) ;
352 my @codes_list = map{
353 { 'code'=>$$_{'value'},
354 'desc'=>GetCriteriumDesc($$_{'value'},$field),
355 'selected'=> $$_{'value'} eq $$suggestion_ref{$field}
358 $hashlists{lc($field)."_loop"}=\@codes_list;
360 $template->param(%hashlists);
361 $template->param(DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),);
362 output_html_with_http_headers $input, $cookie, $template->output;