suggestion.pl has to be updated on C4/Budgets
[wip/koha-chris_n.git] / suggestion / suggestion.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it under the
6 # terms of the GNU General Public License as published by the Free Software
7 # Foundation; either version 2 of the License, or (at your option) any later
8 # version.
9 #
10 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
11 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License along with
15 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
16 # Suite 330, Boston, MA  02111-1307 USA
17
18 use strict;
19 require Exporter;
20 use CGI;
21 use C4::Auth;    # get_template_and_user
22 use C4::Output;
23 use C4::Suggestions;
24 use C4::Koha; #GetItemTypes
25 use C4::Branch;
26 use C4::Budgets;
27 use C4::Search;
28 use C4::Dates qw(format_date);
29 use C4::Members;
30 use C4::Debug;
31
32 sub Init{
33     my $suggestion= shift @_;
34     foreach my $date qw(createdon managedon){
35         $suggestion->{$date}=(($suggestion->{$date} eq "0000-00-00" ||$suggestion->{$date} eq "")?
36                                 $suggestion->{$date}=C4::Dates->today:
37                                 format_date($suggestion->{$date}) 
38                               );
39     }               
40     $suggestion->{'acceptedon'}=(($suggestion->{'acceptedon'} eq "0000-00-00" ||$suggestion->{'acceptedon'} eq "")?
41                                 "":
42                                 format_date($suggestion->{'acceptedon'}) 
43                               );
44     $suggestion->{'managedby'}=C4::Context->userenv->{"id"} unless ($suggestion->{'managedby'});
45     $suggestion->{'createdby'}=C4::Context->userenv->{"id"} unless ($suggestion->{'createdby'});
46     $suggestion->{'branchcode'}=C4::Context->userenv->{"branch"} unless ($suggestion->{'branchcode'});
47 }
48
49 sub GetCriteriumDesc{
50     my ($criteriumvalue,$displayby)=@_;
51     return ($criteriumvalue eq 'ASKED'?"pending":lc $criteriumvalue) if ($displayby =~/status/i);
52     return (GetBranchName($criteriumvalue)) if ($displayby =~/branchcode/);
53     return (GetSupportName($criteriumvalue)) if ($displayby =~/itemtype/);
54     if ($displayby =~/managedby/||$displayby =~/acceptedby/){
55         my $borr=C4::Members::GetMember(borrowernumber=>$criteriumvalue);
56                 return "" unless $borr;
57 #               warn '$borr : ',Data::Dumper::Dumper($borr);
58         return $$borr{firstname}.", ".$$borr{surname};
59     }  
60 }
61
62 my $input           = CGI->new;
63 my $suggestedbyme   = (defined $input->param('suggestedbyme')? $input->param('suggestedbyme'):1);
64 my $op              = $input->param('op')||'else';
65 my @editsuggestions = $input->param('edit_field');
66 my $branchfilter   = $input->param('branchcode');
67 my $suggestedby    = $input->param('suggestedby');
68 my $managedby    = $input->param('managedby');
69 my $displayby    = $input->param('displayby');
70 my $tabcode    = $input->param('tabcode');
71
72 # filter informations which are not suggestion related.
73 my $suggestion_ref  = $input->Vars;
74 delete $$suggestion_ref{$_} foreach qw<suggestedbyme op displayby tabcode edit_field>;
75 foreach (keys %$suggestion_ref){
76         delete $$suggestion_ref{$_} if (!$$suggestion_ref{$_} && ($op eq 'else' || $op eq 'change'));
77 }
78 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
79         {
80             template_name   => "suggestion/suggestion.tmpl",
81             query           => $input,
82             type            => "intranet",
83             flagsrequired   => { catalogue => 1 },
84         }
85     );
86
87 #########################################
88 ##  Operations
89 ##
90 if ($op =~/save/i){
91     if ($$suggestion_ref{'suggestionid'}>0){
92       &ModSuggestion($suggestion_ref);
93     }  
94     else {
95         ###FIXME:Search here if suggestion already exists.
96         my $suggestions_loop =
97             SearchSuggestion( $suggestion_ref );
98             if (@$suggestions_loop>=1){
99                     #some suggestion are answering the request Donot Add        
100             } 
101             else {    
102                 ## Adding some informations related to suggestion
103                 &NewSuggestion($suggestion_ref);
104         }
105         # empty fields, to avoid filter in "SearchSuggestion"
106     }  
107     map{delete $$suggestion_ref{$_}} keys %$suggestion_ref;
108     $op = 'else';
109 }
110 elsif ($op=~/add/) {
111     #Adds suggestion  
112     Init($suggestion_ref);
113     $op ='save';
114
115 elsif ($op=~/edit/) {
116     #Edit suggestion  
117     $suggestion_ref=&GetSuggestion($$suggestion_ref{'suggestionid'});
118     Init($suggestion_ref);
119     $op ='save';
120 }  
121 elsif ($op eq "change" ) {
122         if ($$suggestion_ref{"STATUS"}){
123                 my $tmpstatus=($$suggestion_ref{"STATUS"} eq "ACCEPTED"?"accepted":"managed");
124                 $$suggestion_ref{"$tmpstatus"."on"}=C4::Dates->today;
125                 $$suggestion_ref{"$tmpstatus"."by"}=C4::Context->userenv->{number};
126         }
127         if ( my $reason = $$suggestion_ref{"reason$tabcode"}){
128                 if ( $reason eq "other" ) {
129                                 $reason = $$suggestion_ref{"other_reason$tabcode"};
130                 }
131                 $$suggestion_ref{'reason'}=$reason;
132         }
133         delete $$suggestion_ref{$_} foreach ("reason$tabcode", "other_reason$tabcode");
134         foreach (keys %$suggestion_ref){
135                 delete $$suggestion_ref{$_} unless ($$suggestion_ref{$_});
136         }
137     foreach my $suggestionid (@editsuggestions) {
138                 next unless $suggestionid;
139                 $$suggestion_ref{'suggestionid'}=$suggestionid;
140         &ModSuggestion($suggestion_ref);
141     }
142     $op = 'else';
143 }elsif ($op eq "delete" ) {
144     foreach my $delete_field (@editsuggestions) {
145         &DelSuggestion( $borrowernumber, $delete_field,'intranet' );
146     }
147     $op = 'else';
148 }
149 if ($op=~/else/) {
150     $op='else';
151     
152         $displayby||="STATUS";
153     my $criteria_list=GetDistinctValues("suggestions.".$displayby);
154     my @allsuggestions;
155     foreach my $criteriumvalue (map{$$_{'value'}} @$criteria_list){
156         my $definedvalue = defined $$suggestion_ref{$displayby} && $$suggestion_ref{$displayby} ne "";
157         
158         next if ($definedvalue && $$suggestion_ref{$displayby} ne $criteriumvalue);
159         $$suggestion_ref{$displayby}=$criteriumvalue;
160         warn $$suggestion_ref{$displayby}."=$criteriumvalue; $displayby";
161     
162         my $suggestions = &SearchSuggestion($suggestion_ref);
163         foreach (@$suggestions){
164             foreach my $date qw(createdon managedon acceptedon){
165                 if ($_->{$date} ne "0000-00-00" && $_->{$date} ne "" ){
166                 $_->{$date}=format_date($_->{$date}) ;
167                 } else {
168                 $_->{$date}="" ;
169                 }             
170             }    
171         }
172         push @allsuggestions,{
173                             "suggestiontype"=>$criteriumvalue||"suggest",
174                             "suggestiontypelabel"=>GetCriteriumDesc($criteriumvalue,$displayby)||"",
175                             "suggestionscount"=>scalar(@$suggestions),             
176                             'suggestions_loop'=>$suggestions,
177                                                         };
178                 
179         delete $$suggestion_ref{$displayby} unless $definedvalue;
180     }
181     my $reasonsloop = GetAuthorisedValues("SUGGEST");
182     $template->param(
183         "displayby"=> $displayby,        
184         "notabs"=> $displayby eq "",        
185         suggestions       => \@allsuggestions,
186         reasonsloop       => $reasonsloop,
187     );
188 }
189
190 foreach my $element qw<managedby createdby suggestedby rejectedby>{
191         warn $$suggestion_ref{$element};
192         if ($$suggestion_ref{$element}){
193                 my $member=GetMember(borrowernumber=>$$suggestion_ref{$element});
194                 my $presentation_string=$$member{firstname}." ".$$member{surname}." ".GetBranchName($$member{branchcode})." ".$$member{description}." ".$$member{category_type};
195                 warn $presentation_string;
196                 $template->param($element."information"=>$presentation_string);
197         }
198 }
199 $template->param(
200     %$suggestion_ref,  
201     "op_$op"                => 1,
202     dateformat    => C4::Context->preference("dateformat"),
203     "op"             =>$op,
204 );
205
206
207 ####################
208 ## Initializing selection lists
209
210 #branch display management
211 my $onlymine=C4::Context->preference('IndependantBranches') && 
212              C4::Context->userenv && 
213              C4::Context->userenv->{flags}!=1 && 
214              C4::Context->userenv->{branch};
215 my $branches = GetBranches($onlymine);
216 my @branchloop;
217
218 foreach my $thisbranch ( sort {$branches->{$a}->{'branchname'} cmp $branches->{$b}->{'branchname'}} keys %$branches ) {
219      my %row = (
220         value      => $thisbranch,
221         branchname => $branches->{$thisbranch}->{'branchname'},
222         selected   => ($branches->{$thisbranch}->{'branchcode'} eq $branchfilter)
223                       ||($branches->{$thisbranch}->{'branchcode'} eq $$suggestion_ref{'branchcode'})    
224     );
225     push @branchloop, \%row;
226 }
227 $branchfilter=C4::Context->userenv->{'branch'} if ($onlymine && !$branchfilter);
228
229 $template->param( branchloop => \@branchloop,
230                   branchfilter => $branchfilter);
231
232 # the index parameter is different for item-level itemtypes
233 my $supportlist=GetSupportList();                               
234 foreach my $support(@$supportlist){
235     $$support{'selected'}= $$support{'code'} eq $$suggestion_ref{'itemtype'};
236         if ($$support{'imageurl'}){
237                 $$support{'imageurl'}= getitemtypeimagelocation( 'intranet', $$support{'imageurl'} );
238         }
239         else {
240            delete $$support{'imageurl'}
241         }
242 }
243 $template->param(itemtypeloop=>$supportlist);
244
245 #Budgets management
246 my $searchbudgets={ budget_branchcode=>$branchfilter} if $branchfilter;
247 my $budgets = GetBudgets($searchbudgets);
248
249 foreach (@$budgets){
250         $_->{'selected'}=1 if ($$suggestion_ref{'budget_id'} && $_{'budget_id'} eq $$suggestion_ref{'budget_id'})
251 };
252
253 $template->param( budgetsloop => $budgets);
254
255 my %hashlists; 
256 foreach my $field qw(managedby acceptedby suggestedby STATUS){
257     my $values_list;
258     $values_list=GetDistinctValues("suggestions.".$field) ;
259     my @codes_list = map{
260                                                 { 'code'=>$$_{'value'},
261                                                   'desc'=>GetCriteriumDesc($$_{'value'},$field),
262                                                   'selected'=> $$_{'value'} eq $$suggestion_ref{$field}
263                                                 }
264                                       } @$values_list;
265     $hashlists{lc($field)."_loop"}=\@codes_list;
266 }
267 $template->param(%hashlists);
268
269 output_html_with_http_headers $input, $cookie, $template->output;