bug 7086 follow-up: removed warn
[koha.git] / suggestion / suggestion.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 # Copyright 2006-2010 BibLibre
5
6 #
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
10 # version.
11 #
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.
15 #
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.
19
20 use strict;
21 #use warnings; FIXME - Bug 2505
22 require Exporter;
23 use CGI;
24 use C4::Auth;    # get_template_and_user
25 use C4::Output;
26 use C4::Suggestions;
27 use C4::Koha; #GetItemTypes
28 use C4::Branch;
29 use C4::Budgets;
30 use C4::Search;
31 use C4::Dates qw(format_date);
32 use C4::Members;
33 use C4::Debug;
34
35 sub Init{
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 "") {
39         # new suggestion
40         $suggestion->{'suggesteddate'} = C4::Dates->today;
41         $suggestion->{'suggestedby'} = C4::Context->userenv->{"number"} unless ($suggestion->{'suggestedby'});
42     }
43     else {
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'});
49     }
50     foreach my $date ( qw(rejecteddate accepteddate) ){
51     $suggestion->{$date}=(($suggestion->{$date} eq "0000-00-00" ||$suggestion->{$date} eq "")?
52                                 "":
53                                 format_date($suggestion->{$date}) 
54                               );
55     }
56     $suggestion->{'branchcode'}=C4::Context->userenv->{"branch"} unless ($suggestion->{'branchcode'});
57 }
58
59 sub GetCriteriumDesc{
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};
68     }
69     if ( $displayby =~ /budgetid/) {
70         my $budget = GetBudget($criteriumvalue);
71         return "" unless $budget;
72         return $$budget{budget_name};
73     }
74 }
75
76 my $input           = CGI->new;
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 $branchfilter   = $input->param('branchcode');
81 my $suggestedby    = $input->param('suggestedby');
82 my $returnsuggested = $input->param('returnsuggested');
83 my $returnsuggestedby = $input->param('returnsuggestedby');
84 my $managedby    = $input->param('managedby');
85 my $displayby    = $input->param('displayby');
86 my $tabcode    = $input->param('tabcode');
87
88 # filter informations which are not suggestion related.
89 my $suggestion_ref  = $input->Vars;
90
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'));
94 }
95 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
96         {
97             template_name   => "suggestion/suggestion.tmpl",
98             query           => $input,
99             type            => "intranet",
100             flagsrequired   => { catalogue => 1 },
101         }
102     );
103
104 #########################################
105 ##  Operations
106 ##
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};
112         }
113         $$suggestion_ref{"manageddate"} = C4::Dates->today;
114         $$suggestion_ref{"managedby"}   = C4::Context->userenv->{number};
115     }
116     if ( $$suggestion_ref{'suggestionid'} > 0 ) {
117         &ModSuggestion($suggestion_ref);
118     } else {
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        
124         } 
125         else {    
126             ## Adding some informations related to suggestion
127             &NewSuggestion($suggestion_ref);
128         }
129         # empty fields, to avoid filter in "SearchSuggestion"
130     }  
131     map{delete $$suggestion_ref{$_}} keys %$suggestion_ref;
132     $op = 'else';
133 }
134 elsif ($op=~/add/) {
135     #Adds suggestion  
136     Init($suggestion_ref);
137     $op ='save';
138
139 elsif ($op=~/edit/) {
140     #Edit suggestion  
141     $suggestion_ref=&GetSuggestion($$suggestion_ref{'suggestionid'});
142     Init($suggestion_ref);
143     $op ='save';
144 }  
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};
154     }
155         if ($$suggestion_ref{"STATUS"}){
156                 $$suggestion_ref{"manageddate"}=C4::Dates->today;
157                 $$suggestion_ref{"managedby"}=C4::Context->userenv->{number};
158         }
159         if ( my $reason = $$suggestion_ref{"reason$tabcode"}){
160                 if ( $reason eq "other" ) {
161                                 $reason = $$suggestion_ref{"other_reason$tabcode"};
162                 }
163                 $$suggestion_ref{'reason'}=$reason;
164         }
165         delete $$suggestion_ref{$_} foreach ("reason$tabcode", "other_reason$tabcode");
166         foreach (keys %$suggestion_ref){
167                 delete $$suggestion_ref{$_} unless ($$suggestion_ref{$_});
168         }
169     foreach my $suggestionid (@editsuggestions) {
170         next unless $suggestionid;
171         $$suggestion_ref{'suggestionid'}=$suggestionid;
172         &ModSuggestion($suggestion_ref);
173     }
174     $op = 'else';
175 }elsif ($op eq "delete" ) {
176     foreach my $delete_field (@editsuggestions) {
177         &DelSuggestion( $borrowernumber, $delete_field,'intranet' );
178     }
179     $op = 'else';
180 }
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);
187 }
188 if ($op=~/else/) {
189     $op='else';
190     
191     $displayby||="STATUS";
192     my $criteria_list=GetDistinctValues("suggestions.".$displayby);
193     my @allsuggestions;
194     my $reasonsloop = GetAuthorisedValues("SUGGEST");
195     foreach my $criteriumvalue (map{$$_{'value'}} @$criteria_list){
196         my $definedvalue = defined $$suggestion_ref{$displayby} && $$suggestion_ref{$displayby} ne "";
197         
198         next if ($definedvalue && $$suggestion_ref{$displayby} ne $criteriumvalue);
199         $$suggestion_ref{$displayby}=$criteriumvalue;
200 #        warn $$suggestion_ref{$displayby}."=$criteriumvalue; $displayby";
201     
202         my $suggestions = &SearchSuggestion($suggestion_ref);
203         foreach my $suggestion (@$suggestions){
204             if($suggestion->{budgetid}) {
205                 my $budget = GetBudget($suggestion->{budgetid});
206                 $suggestion->{budget_name}=$budget->{budget_name} if $budget;
207             }
208             foreach my $date ( qw(suggesteddate manageddate accepteddate) ){
209                 if ($suggestion->{$date} ne "0000-00-00" && $suggestion->{$date} ne "" ){
210                 $suggestion->{$date}=format_date($suggestion->{$date}) ;
211                 } else {
212                 $suggestion->{$date}="" ;
213                 }             
214             }    
215         }
216         push @allsuggestions,{
217                             "suggestiontype"=>$criteriumvalue||"suggest",
218                             "suggestiontypelabel"=>GetCriteriumDesc($criteriumvalue,$displayby)||"",
219                             "suggestionscount"=>scalar(@$suggestions),             
220                             'suggestions_loop'=>$suggestions,
221                             'reasonsloop'     => $reasonsloop,
222                             };
223
224         delete $$suggestion_ref{$displayby} unless $definedvalue;
225     }
226
227     $template->param(
228         "displayby"=> $displayby,
229         "notabs"=> $displayby eq "",
230         suggestions       => \@allsuggestions,
231     );
232 }
233
234 foreach my $element ( qw(managedby suggestedby acceptedby) ) {
235 #    $debug || warn $$suggestion_ref{$element};
236     if ($$suggestion_ref{$element}){
237         my $member=GetMember(borrowernumber=>$$suggestion_ref{$element});
238         $template->param(
239             $element."_borrowernumber"=>$$member{borrowernumber},
240             $element."_firstname"=>$$member{firstname},
241             $element."_surname"=>$$member{surname},
242             $element."_branchcode"=>$$member{branchcode},
243             $element."_description"=>$$member{description},
244             $element."_category_type"=>$$member{category_type}
245         );
246     }
247 }
248 $template->param(
249     %$suggestion_ref,  
250     "op_$op"                => 1,
251     dateformat    => C4::Context->preference("dateformat"),
252     "op"             =>$op,
253 );
254
255 if(defined($returnsuggested) and $returnsuggested ne "noone")
256 {
257         print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=".$returnsuggested."#suggestions");
258 }
259
260 ####################
261 ## Initializing selection lists
262
263 #branch display management
264 my $onlymine=C4::Context->preference('IndependantBranches') && 
265             C4::Context->userenv && 
266             C4::Context->userenv->{flags}!=1 && 
267             C4::Context->userenv->{branch};
268 my $branches = GetBranches($onlymine);
269 my @branchloop;
270
271 foreach my $thisbranch ( sort {$branches->{$a}->{'branchname'} cmp $branches->{$b}->{'branchname'}} keys %$branches ) {
272     my %row = (
273         value      => $thisbranch,
274         branchname => $branches->{$thisbranch}->{'branchname'},
275         selected   => ($branches->{$thisbranch}->{'branchcode'} eq $branchfilter)
276                     ||($branches->{$thisbranch}->{'branchcode'} eq $$suggestion_ref{'branchcode'})    
277     );
278     push @branchloop, \%row;
279 }
280 $branchfilter=C4::Context->userenv->{'branch'} if ($onlymine && !$branchfilter);
281
282 $template->param( branchloop => \@branchloop,
283                 branchfilter => $branchfilter);
284
285 # the index parameter is different for item-level itemtypes
286 my $supportlist=GetSupportList();                               
287 foreach my $support(@$supportlist){
288     $$support{'selected'}= $$support{'itemtype'} eq $$suggestion_ref{'itemtype'};
289     if ($$support{'imageurl'}){
290         $$support{'imageurl'}= getitemtypeimagelocation( 'intranet', $$support{'imageurl'} );
291     }
292     else {
293     delete $$support{'imageurl'}
294     }
295 }
296 $template->param(itemtypeloop=>$supportlist);
297 $template->param( returnsuggestedby => $returnsuggestedby );
298
299 my $patron_reason_loop = GetAuthorisedValues("OPAC_SUG",$$suggestion_ref{'patronreason'});
300 $template->param(patron_reason_loop=>$patron_reason_loop);
301
302 #Budgets management
303 my $budgets = [];
304 if ($branchfilter) {
305     my $searchbudgets = { budget_branchcode => $branchfilter };
306     $budgets = GetBudgets($searchbudgets);
307 } else {
308     $budgets = GetBudgets(undef);
309 }
310
311 foreach my $budget ( @{$budgets} ) {
312 ## Please see file perltidy.ERR
313     $budget->{'selected'}=1 if ($$suggestion_ref{'budgetid'} && $budget->{'budget_id'} eq $$suggestion_ref{'budgetid'})
314 };
315
316 $template->param( budgetsloop => $budgets);
317 $template->param( "statusselected_$$suggestion_ref{'STATUS'}" =>1);
318
319 # get currencies and rates
320 my @rates = GetCurrencies();
321 my $count = scalar @rates;
322 my $active_currency = GetCurrency();
323 my $selected_currency;
324 if ($$suggestion_ref{'currency'}) {
325     $selected_currency = $$suggestion_ref{'currency'};
326 }
327 else {
328     $selected_currency = $active_currency->{currency};
329 }
330
331 my @loop_currency = ();
332 for ( my $i = 0 ; $i < $count ; $i++ ) {
333     my %line;
334     $line{currcode} = $rates[$i]->{'currency'};
335     $line{rate}     = $rates[$i]->{'rate'};
336         $line{selected} = 1 if ($line{'currcode'} eq $selected_currency);
337     push @loop_currency, \%line;
338 }
339
340 $template->param(loop_currency => \@loop_currency);
341
342 $template->param(
343         price        => sprintf("%.2f", $$suggestion_ref{'price'}||0),
344         total            => sprintf("%.2f", $$suggestion_ref{'total'}||0),
345 );
346
347 my %hashlists;
348 foreach my $field ( qw(managedby acceptedby suggestedby budgetid) ) {
349     my $values_list;
350     $values_list=GetDistinctValues("suggestions.".$field) ;
351     my @codes_list = map{
352                         { 'code'=>$$_{'value'},
353                         'desc'=>GetCriteriumDesc($$_{'value'},$field),
354                         'selected'=> $$_{'value'} eq $$suggestion_ref{$field}
355                         }
356                     } @$values_list;
357     $hashlists{lc($field)."_loop"}=\@codes_list;
358 }
359 $template->param(%hashlists);
360 $template->param(DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),);
361 output_html_with_http_headers $input, $cookie, $template->output;