Bug 7086 : "suggested by" fixed for staff members
[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         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'});
50     }
51     foreach my $date ( qw(rejecteddate accepteddate) ){
52     $suggestion->{$date}=(($suggestion->{$date} eq "0000-00-00" ||$suggestion->{$date} eq "")?
53                                 "":
54                                 format_date($suggestion->{$date}) 
55                               );
56     }
57     $suggestion->{'branchcode'}=C4::Context->userenv->{"branch"} unless ($suggestion->{'branchcode'});
58 }
59
60 sub GetCriteriumDesc{
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};
69     }
70     if ( $displayby =~ /budgetid/) {
71         my $budget = GetBudget($criteriumvalue);
72         return "" unless $budget;
73         return $$budget{budget_name};
74     }
75 }
76
77 my $input           = CGI->new;
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');
88
89 # filter informations which are not suggestion related.
90 my $suggestion_ref  = $input->Vars;
91
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'));
95 }
96 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
97         {
98             template_name   => "suggestion/suggestion.tmpl",
99             query           => $input,
100             type            => "intranet",
101             flagsrequired   => { catalogue => 1 },
102         }
103     );
104
105 #########################################
106 ##  Operations
107 ##
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};
113         }
114         $$suggestion_ref{"manageddate"} = C4::Dates->today;
115         $$suggestion_ref{"managedby"}   = C4::Context->userenv->{number};
116     }
117     if ( $$suggestion_ref{'suggestionid'} > 0 ) {
118         &ModSuggestion($suggestion_ref);
119     } else {
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        
125         } 
126         else {    
127             ## Adding some informations related to suggestion
128             &NewSuggestion($suggestion_ref);
129         }
130         # empty fields, to avoid filter in "SearchSuggestion"
131     }  
132     map{delete $$suggestion_ref{$_}} keys %$suggestion_ref;
133     $op = 'else';
134 }
135 elsif ($op=~/add/) {
136     #Adds suggestion  
137     Init($suggestion_ref);
138     $op ='save';
139
140 elsif ($op=~/edit/) {
141     #Edit suggestion  
142     $suggestion_ref=&GetSuggestion($$suggestion_ref{'suggestionid'});
143     Init($suggestion_ref);
144     $op ='save';
145 }  
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};
155     }
156         if ($$suggestion_ref{"STATUS"}){
157                 $$suggestion_ref{"manageddate"}=C4::Dates->today;
158                 $$suggestion_ref{"managedby"}=C4::Context->userenv->{number};
159         }
160         if ( my $reason = $$suggestion_ref{"reason$tabcode"}){
161                 if ( $reason eq "other" ) {
162                                 $reason = $$suggestion_ref{"other_reason$tabcode"};
163                 }
164                 $$suggestion_ref{'reason'}=$reason;
165         }
166         delete $$suggestion_ref{$_} foreach ("reason$tabcode", "other_reason$tabcode");
167         foreach (keys %$suggestion_ref){
168                 delete $$suggestion_ref{$_} unless ($$suggestion_ref{$_});
169         }
170     foreach my $suggestionid (@editsuggestions) {
171         next unless $suggestionid;
172         $$suggestion_ref{'suggestionid'}=$suggestionid;
173         &ModSuggestion($suggestion_ref);
174     }
175     $op = 'else';
176 }elsif ($op eq "delete" ) {
177     foreach my $delete_field (@editsuggestions) {
178         &DelSuggestion( $borrowernumber, $delete_field,'intranet' );
179     }
180     $op = 'else';
181 }
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);
188 }
189 if ($op=~/else/) {
190     $op='else';
191     
192     $displayby||="STATUS";
193     my $criteria_list=GetDistinctValues("suggestions.".$displayby);
194     my @allsuggestions;
195     my $reasonsloop = GetAuthorisedValues("SUGGEST");
196     foreach my $criteriumvalue (map{$$_{'value'}} @$criteria_list){
197         my $definedvalue = defined $$suggestion_ref{$displayby} && $$suggestion_ref{$displayby} ne "";
198         
199         next if ($definedvalue && $$suggestion_ref{$displayby} ne $criteriumvalue);
200         $$suggestion_ref{$displayby}=$criteriumvalue;
201 #        warn $$suggestion_ref{$displayby}."=$criteriumvalue; $displayby";
202     
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;
208             }
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}) ;
212                 } else {
213                 $suggestion->{$date}="" ;
214                 }             
215             }    
216         }
217         push @allsuggestions,{
218                             "suggestiontype"=>$criteriumvalue||"suggest",
219                             "suggestiontypelabel"=>GetCriteriumDesc($criteriumvalue,$displayby)||"",
220                             "suggestionscount"=>scalar(@$suggestions),             
221                             'suggestions_loop'=>$suggestions,
222                             'reasonsloop'     => $reasonsloop,
223                             };
224
225         delete $$suggestion_ref{$displayby} unless $definedvalue;
226     }
227
228     $template->param(
229         "displayby"=> $displayby,
230         "notabs"=> $displayby eq "",
231         suggestions       => \@allsuggestions,
232     );
233 }
234
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});
239         $template->param(
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}
246         );
247     }
248 }
249 $template->param(
250     %$suggestion_ref,  
251     "op_$op"                => 1,
252     dateformat    => C4::Context->preference("dateformat"),
253     "op"             =>$op,
254 );
255
256 if(defined($returnsuggested) and $returnsuggested ne "noone")
257 {
258         print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=".$returnsuggested."#suggestions");
259 }
260
261 ####################
262 ## Initializing selection lists
263
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);
270 my @branchloop;
271
272 foreach my $thisbranch ( sort {$branches->{$a}->{'branchname'} cmp $branches->{$b}->{'branchname'}} keys %$branches ) {
273     my %row = (
274         value      => $thisbranch,
275         branchname => $branches->{$thisbranch}->{'branchname'},
276         selected   => ($branches->{$thisbranch}->{'branchcode'} eq $branchfilter)
277                     ||($branches->{$thisbranch}->{'branchcode'} eq $$suggestion_ref{'branchcode'})    
278     );
279     push @branchloop, \%row;
280 }
281 $branchfilter=C4::Context->userenv->{'branch'} if ($onlymine && !$branchfilter);
282
283 $template->param( branchloop => \@branchloop,
284                 branchfilter => $branchfilter);
285
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'} );
292     }
293     else {
294     delete $$support{'imageurl'}
295     }
296 }
297 $template->param(itemtypeloop=>$supportlist);
298 $template->param( returnsuggestedby => $returnsuggestedby );
299
300 my $patron_reason_loop = GetAuthorisedValues("OPAC_SUG",$$suggestion_ref{'patronreason'});
301 $template->param(patron_reason_loop=>$patron_reason_loop);
302
303 #Budgets management
304 my $budgets = [];
305 if ($branchfilter) {
306     my $searchbudgets = { budget_branchcode => $branchfilter };
307     $budgets = GetBudgets($searchbudgets);
308 } else {
309     $budgets = GetBudgets(undef);
310 }
311
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'})
315 };
316
317 $template->param( budgetsloop => $budgets);
318 $template->param( "statusselected_$$suggestion_ref{'STATUS'}" =>1);
319
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'};
327 }
328 else {
329     $selected_currency = $active_currency->{currency};
330 }
331
332 my @loop_currency = ();
333 for ( my $i = 0 ; $i < $count ; $i++ ) {
334     my %line;
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;
339 }
340
341 $template->param(loop_currency => \@loop_currency);
342
343 $template->param(
344         price        => sprintf("%.2f", $$suggestion_ref{'price'}||0),
345         total            => sprintf("%.2f", $$suggestion_ref{'total'}||0),
346 );
347
348 my %hashlists;
349 foreach my $field ( qw(managedby acceptedby suggestedby budgetid) ) {
350     my $values_list;
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}
356                         }
357                     } @$values_list;
358     $hashlists{lc($field)."_loop"}=\@codes_list;
359 }
360 $template->param(%hashlists);
361 $template->param(DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),);
362 output_html_with_http_headers $input, $cookie, $template->output;