9ecfa5509ad16732d615ab47aa3f8189db55c87e
[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     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}) 
41                               );
42     }               
43     foreach my $date qw(rejecteddate accepteddate){
44     $suggestion->{$date}=(($suggestion->{$date} eq "0000-00-00" ||$suggestion->{$date} eq "")?
45                                 "":
46                                 format_date($suggestion->{$date}) 
47                               );
48         }
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'});
52 }
53
54 sub GetCriteriumDesc{
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};
63     }
64     if ( $displayby =~ /budgetid/) {
65         my $budget = GetBudget($criteriumvalue);
66         return "" unless $budget;
67         return $$budget{budget_name};
68     }
69 }
70
71 my $input           = CGI->new;
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');
82
83 # filter informations which are not suggestion related.
84 my $suggestion_ref  = $input->Vars;
85
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'));
89 }
90 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
91         {
92             template_name   => "suggestion/suggestion.tmpl",
93             query           => $input,
94             type            => "intranet",
95             flagsrequired   => { catalogue => 1 },
96         }
97     );
98
99 #########################################
100 ##  Operations
101 ##
102 if ($op =~/save/i){
103     if ($$suggestion_ref{'suggestionid'}>0){
104     &ModSuggestion($suggestion_ref);
105     }  
106     else {
107         ###FIXME:Search here if suggestion already exists.
108         my $suggestions_loop =
109             SearchSuggestion( $suggestion_ref );
110         if (@$suggestions_loop>=1){
111             #some suggestion are answering the request Donot Add        
112         } 
113         else {    
114             ## Adding some informations related to suggestion
115             &NewSuggestion($suggestion_ref);
116         }
117         # empty fields, to avoid filter in "SearchSuggestion"
118     }  
119     map{delete $$suggestion_ref{$_}} keys %$suggestion_ref;
120     $op = 'else';
121 }
122 elsif ($op=~/add/) {
123     #Adds suggestion  
124     Init($suggestion_ref);
125     $op ='save';
126
127 elsif ($op=~/edit/) {
128     #Edit suggestion  
129     $suggestion_ref=&GetSuggestion($$suggestion_ref{'suggestionid'});
130     Init($suggestion_ref);
131     $op ='save';
132 }  
133 elsif ($op eq "change" ) {
134         if ($$suggestion_ref{"STATUS"}){
135                 if (my $tmpstatus=lc($$suggestion_ref{"STATUS"}) =~/ACCEPTED|REJECTED/i){
136                         $$suggestion_ref{"$tmpstatus"."date"}=C4::Dates->today;
137                         $$suggestion_ref{"$tmpstatus"."by"}=C4::Context->userenv->{number};
138                 }
139                 $$suggestion_ref{"manageddate"}=C4::Dates->today;
140                 $$suggestion_ref{"managedby"}=C4::Context->userenv->{number};
141         }
142         if ( my $reason = $$suggestion_ref{"reason$tabcode"}){
143                 if ( $reason eq "other" ) {
144                                 $reason = $$suggestion_ref{"other_reason$tabcode"};
145                 }
146                 $$suggestion_ref{'reason'}=$reason;
147         }
148         delete $$suggestion_ref{$_} foreach ("reason$tabcode", "other_reason$tabcode");
149         foreach (keys %$suggestion_ref){
150                 delete $$suggestion_ref{$_} unless ($$suggestion_ref{$_});
151         }
152     foreach my $suggestionid (@editsuggestions) {
153         next unless $suggestionid;
154         $$suggestion_ref{'suggestionid'}=$suggestionid;
155         &ModSuggestion($suggestion_ref);
156     }
157     $op = 'else';
158 }elsif ($op eq "delete" ) {
159     foreach my $delete_field (@editsuggestions) {
160         &DelSuggestion( $borrowernumber, $delete_field,'intranet' );
161     }
162     $op = 'else';
163 }
164 if ($op=~/else/) {
165     $op='else';
166     
167     $displayby||="STATUS";
168     my $criteria_list=GetDistinctValues("suggestions.".$displayby);
169     my @allsuggestions;
170     my $reasonsloop = GetAuthorisedValues("SUGGEST");
171     foreach my $criteriumvalue (map{$$_{'value'}} @$criteria_list){
172         my $definedvalue = defined $$suggestion_ref{$displayby} && $$suggestion_ref{$displayby} ne "";
173         
174         next if ($definedvalue && $$suggestion_ref{$displayby} ne $criteriumvalue);
175         $$suggestion_ref{$displayby}=$criteriumvalue;
176 #        warn $$suggestion_ref{$displayby}."=$criteriumvalue; $displayby";
177     
178         my $suggestions = &SearchSuggestion($suggestion_ref);
179         foreach my $suggestion (@$suggestions){
180             $suggestion->{budget_name}=GetBudget($suggestion->{budgetid})->{budget_name} if $suggestion->{budgetid};
181             foreach my $date qw(suggesteddate manageddate accepteddate){
182                 if ($suggestion->{$date} ne "0000-00-00" && $suggestion->{$date} ne "" ){
183                 $suggestion->{$date}=format_date($suggestion->{$date}) ;
184                 } else {
185                 $suggestion->{$date}="" ;
186                 }             
187             }    
188         }
189         push @allsuggestions,{
190                             "suggestiontype"=>$criteriumvalue||"suggest",
191                             "suggestiontypelabel"=>GetCriteriumDesc($criteriumvalue,$displayby)||"",
192                             "suggestionscount"=>scalar(@$suggestions),             
193                             'suggestions_loop'=>$suggestions,
194                             'reasonsloop'     => $reasonsloop,
195                             };
196
197         delete $$suggestion_ref{$displayby} unless $definedvalue;
198     }
199
200     $template->param(
201         "displayby"=> $displayby,
202         "notabs"=> $displayby eq "",
203         suggestions       => \@allsuggestions,
204     );
205 }
206
207 foreach my $element qw(managedby suggestedby acceptedby) {
208 #    $debug || warn $$suggestion_ref{$element};
209     if ($$suggestion_ref{$element}){
210         my $member=GetMember(borrowernumber=>$$suggestion_ref{$element});
211         $template->param(
212             $element."_borrowernumber"=>$$member{borrowernumber},
213             $element."_firstname"=>$$member{firstname},
214             $element."_surname"=>$$member{surname},
215             $element."_branchcode"=>$$member{branchcode},
216             $element."_description"=>$$member{description},
217             $element."_category_type"=>$$member{category_type}
218         );
219     }
220 }
221 $template->param(
222     %$suggestion_ref,  
223     "op_$op"                => 1,
224     dateformat    => C4::Context->preference("dateformat"),
225     "op"             =>$op,
226 );
227
228 if(defined($returnsuggested) and $returnsuggested ne "noone")
229 {
230         print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=".$returnsuggested."#suggestions");
231 }
232
233 ####################
234 ## Initializing selection lists
235
236 #branch display management
237 my $onlymine=C4::Context->preference('IndependantBranches') && 
238             C4::Context->userenv && 
239             C4::Context->userenv->{flags}!=1 && 
240             C4::Context->userenv->{branch};
241 my $branches = GetBranches($onlymine);
242 my @branchloop;
243
244 foreach my $thisbranch ( sort {$branches->{$a}->{'branchname'} cmp $branches->{$b}->{'branchname'}} keys %$branches ) {
245     my %row = (
246         value      => $thisbranch,
247         branchname => $branches->{$thisbranch}->{'branchname'},
248         selected   => ($branches->{$thisbranch}->{'branchcode'} eq $branchfilter)
249                     ||($branches->{$thisbranch}->{'branchcode'} eq $$suggestion_ref{'branchcode'})    
250     );
251     push @branchloop, \%row;
252 }
253 $branchfilter=C4::Context->userenv->{'branch'} if ($onlymine && !$branchfilter);
254
255 $template->param( branchloop => \@branchloop,
256                 branchfilter => $branchfilter);
257
258 # the index parameter is different for item-level itemtypes
259 my $supportlist=GetSupportList();                               
260 foreach my $support(@$supportlist){
261     $$support{'selected'}= $$support{'itemtype'} eq $$suggestion_ref{'itemtype'};
262     if ($$support{'imageurl'}){
263         $$support{'imageurl'}= getitemtypeimagelocation( 'intranet', $$support{'imageurl'} );
264     }
265     else {
266     delete $$support{'imageurl'}
267     }
268 }
269 $template->param(itemtypeloop=>$supportlist);
270 $template->param( returnsuggestedby => $returnsuggestedby );
271
272 my $patron_reason_loop = GetAuthorisedValues("OPAC_SUG",$$suggestion_ref{'patronreason'});
273 $template->param(patron_reason_loop=>$patron_reason_loop);
274
275 #Budgets management
276 my $searchbudgets={ budget_branchcode=>$branchfilter} if $branchfilter;
277 my $budgets = GetBudgets($searchbudgets);
278
279 foreach my $budget (@$budgets){
280     $budget->{'selected'}=1 if ($$suggestion_ref{'budgetid'} && $budget->{'budget_id'} eq $$suggestion_ref{'budgetid'})
281 };
282
283 $template->param( budgetsloop => $budgets);
284 $template->param( "statusselected_$$suggestion_ref{'STATUS'}" =>1);
285
286 # get currencies and rates
287 my @rates = GetCurrencies();
288 my $count = scalar @rates;
289
290 my @loop_currency = ();
291 for ( my $i = 0 ; $i < $count ; $i++ ) {
292     my %line;
293     $line{currcode} = $rates[$i]->{'currency'};
294     $line{rate}     = $rates[$i]->{'rate'};
295         $line{selected} = 1 if ($$suggestion_ref{'currency'} && $line{'currcode'} eq $$suggestion_ref{'currency'});
296     push @loop_currency, \%line;
297 }
298
299 $template->param(loop_currency => \@loop_currency);
300
301 $template->param(
302         price        => sprintf("%.2f", $$suggestion_ref{'price'}||0),
303         total            => sprintf("%.2f", $$suggestion_ref{'total'}||0),
304 );
305
306 my %hashlists;
307 foreach my $field qw(managedby acceptedby suggestedby budgetid) {
308     my $values_list;
309     $values_list=GetDistinctValues("suggestions.".$field) ;
310     my @codes_list = map{
311                         { 'code'=>$$_{'value'},
312                         'desc'=>GetCriteriumDesc($$_{'value'},$field),
313                         'selected'=> $$_{'value'} eq $$suggestion_ref{$field}
314                         }
315                     } @$values_list;
316     $hashlists{lc($field)."_loop"}=\@codes_list;
317 }
318 $template->param(%hashlists);
319 $template->param(DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),);
320 output_html_with_http_headers $input, $cookie, $template->output;