Date management update Suggestions
[koha.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(suggesteddate manageddate){
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     foreach my $date qw(rejecteddate accepteddate){
41     $suggestion->{$date}=(($suggestion->{$date} eq "0000-00-00" ||$suggestion->{$date} eq "")?
42                                 "":
43                                 format_date($suggestion->{$date}) 
44                               );
45         }
46     $suggestion->{'managedby'}=C4::Context->userenv->{"number"} unless ($suggestion->{'managedby'});
47     $suggestion->{'createdby'}=C4::Context->userenv->{"number"} unless ($suggestion->{'createdby'});
48     $suggestion->{'branchcode'}=C4::Context->userenv->{"branch"} unless ($suggestion->{'branchcode'});
49 }
50
51 sub GetCriteriumDesc{
52     my ($criteriumvalue,$displayby)=@_;
53     return ($criteriumvalue eq 'ASKED'?"pending":lc $criteriumvalue) if ($displayby =~/status/i);
54     return (GetBranchName($criteriumvalue)) if ($displayby =~/branchcode/);
55     return (GetSupportName($criteriumvalue)) if ($displayby =~/itemtype/);
56     if ($displayby =~/managedby/||$displayby =~/acceptedby/){
57         my $borr=C4::Members::GetMember(borrowernumber=>$criteriumvalue);
58         return "" unless $borr;
59 #               warn '$borr : ',Data::Dumper::Dumper($borr);
60         return $$borr{firstname}.", ".$$borr{surname};
61     }  
62 }
63
64 my $input           = CGI->new;
65 my $suggestedbyme   = (defined $input->param('suggestedbyme')? $input->param('suggestedbyme'):1);
66 my $op              = $input->param('op')||'else';
67 my @editsuggestions = $input->param('edit_field');
68 my $branchfilter   = $input->param('branchcode');
69 my $suggestedby    = $input->param('suggestedby');
70 my $managedby    = $input->param('managedby');
71 my $displayby    = $input->param('displayby');
72 my $tabcode    = $input->param('tabcode');
73
74 # filter informations which are not suggestion related.
75 my $suggestion_ref  = $input->Vars;
76 delete $$suggestion_ref{$_} foreach qw( suggestedbyme op displayby tabcode edit_field );
77 foreach (keys %$suggestion_ref){
78     delete $$suggestion_ref{$_} if (!$$suggestion_ref{$_} && ($op eq 'else' || $op eq 'change'));
79 }
80 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
81         {
82             template_name   => "suggestion/suggestion.tmpl",
83             query           => $input,
84             type            => "intranet",
85             flagsrequired   => { catalogue => 1 },
86         }
87     );
88
89 #########################################
90 ##  Operations
91 ##
92 if ($op =~/save/i){
93     if ($$suggestion_ref{'suggestionid'}>0){
94     &ModSuggestion($suggestion_ref);
95     }  
96     else {
97         ###FIXME:Search here if suggestion already exists.
98         my $suggestions_loop =
99             SearchSuggestion( $suggestion_ref );
100         if (@$suggestions_loop>=1){
101             #some suggestion are answering the request Donot Add        
102         } 
103         else {    
104             ## Adding some informations related to suggestion
105             &NewSuggestion($suggestion_ref);
106         }
107         # empty fields, to avoid filter in "SearchSuggestion"
108     }  
109     map{delete $$suggestion_ref{$_}} keys %$suggestion_ref;
110     $op = 'else';
111 }
112 elsif ($op=~/add/) {
113     #Adds suggestion  
114     Init($suggestion_ref);
115     $op ='save';
116
117 elsif ($op=~/edit/) {
118     #Edit suggestion  
119     $suggestion_ref=&GetSuggestion($$suggestion_ref{'suggestionid'});
120     Init($suggestion_ref);
121     $op ='save';
122 }  
123 elsif ($op eq "change" ) {
124 <<<<<<< HEAD:suggestion/suggestion.pl
125     if ($$suggestion_ref{"STATUS"}){
126         my $tmpstatus=($$suggestion_ref{"STATUS"} eq "ACCEPTED"?"accepted":"managed");
127         $$suggestion_ref{"$tmpstatus"."on"}=C4::Dates->today;
128         $$suggestion_ref{"$tmpstatus"."by"}=C4::Context->userenv->{id};
129     }
130     if ( my $reason = $$suggestion_ref{"reason$tabcode"}){
131         if ( $reason eq "other" ) {
132                 $reason = $$suggestion_ref{"other_reason$tabcode"};
133         }
134         $$suggestion_ref{'reason'}=$reason;
135     }
136     delete $$suggestion_ref{$_} foreach ("reason$tabcode", "other_reason$tabcode");
137     foreach (keys %$suggestion_ref){
138         delete $$suggestion_ref{$_} unless ($$suggestion_ref{$_});
139     }
140 =======
141         if ($$suggestion_ref{"STATUS"}){
142                 if (my $tmpstatus=lc($$suggestion_ref{"STATUS"}) =~/ACCEPTED|REJECTED/i){
143                         $$suggestion_ref{"$tmpstatus"."date"}=C4::Dates->today;
144                         $$suggestion_ref{"$tmpstatus"."by"}=C4::Context->userenv->{number};
145                 }
146                 $$suggestion_ref{"manageddate"}=C4::Dates->today;
147                 $$suggestion_ref{"managedby"}=C4::Context->userenv->{number};
148         }
149         if ( my $reason = $$suggestion_ref{"reason$tabcode"}){
150                 if ( $reason eq "other" ) {
151                                 $reason = $$suggestion_ref{"other_reason$tabcode"};
152                 }
153                 $$suggestion_ref{'reason'}=$reason;
154         }
155         delete $$suggestion_ref{$_} foreach ("reason$tabcode", "other_reason$tabcode");
156         foreach (keys %$suggestion_ref){
157                 delete $$suggestion_ref{$_} unless ($$suggestion_ref{$_});
158         }
159 >>>>>>> Date management update Suggestions:suggestion/suggestion.pl
160     foreach my $suggestionid (@editsuggestions) {
161         next unless $suggestionid;
162         $$suggestion_ref{'suggestionid'}=$suggestionid;
163         &ModSuggestion($suggestion_ref);
164     }
165     $op = 'else';
166 }elsif ($op eq "delete" ) {
167     foreach my $delete_field (@editsuggestions) {
168         &DelSuggestion( $borrowernumber, $delete_field,'intranet' );
169     }
170     $op = 'else';
171 }
172 if ($op=~/else/) {
173     $op='else';
174     
175     $displayby||="STATUS";
176     my $criteria_list=GetDistinctValues("suggestions.".$displayby);
177     my @allsuggestions;
178     foreach my $criteriumvalue (map{$$_{'value'}} @$criteria_list){
179         my $definedvalue = defined $$suggestion_ref{$displayby} && $$suggestion_ref{$displayby} ne "";
180         
181         next if ($definedvalue && $$suggestion_ref{$displayby} ne $criteriumvalue);
182         $$suggestion_ref{$displayby}=$criteriumvalue;
183         warn $$suggestion_ref{$displayby}."=$criteriumvalue; $displayby";
184     
185         my $suggestions = &SearchSuggestion($suggestion_ref);
186         foreach (@$suggestions){
187             foreach my $date qw(suggesteddate manageddate accepteddate){
188                 if ($_->{$date} ne "0000-00-00" && $_->{$date} ne "" ){
189                 $_->{$date}=format_date($_->{$date}) ;
190                 } else {
191                 $_->{$date}="" ;
192                 }             
193             }    
194         }
195         push @allsuggestions,{
196                             "suggestiontype"=>$criteriumvalue||"suggest",
197                             "suggestiontypelabel"=>GetCriteriumDesc($criteriumvalue,$displayby)||"",
198                             "suggestionscount"=>scalar(@$suggestions),             
199                             'suggestions_loop'=>$suggestions,
200                             };
201
202         delete $$suggestion_ref{$displayby} unless $definedvalue;
203     }
204     my $reasonsloop = GetAuthorisedValues("SUGGEST");
205     $template->param(
206         "displayby"=> $displayby,
207         "notabs"=> $displayby eq "",
208         suggestions       => \@allsuggestions,
209         reasonsloop       => $reasonsloop,
210     );
211 }
212
213 foreach my $element qw(managedby suggestedby){
214     $debug || warn $$suggestion_ref{$element};
215     if ($$suggestion_ref{$element}){
216         my $member=GetMember(borrowernumber=>$$suggestion_ref{$element});
217         my $presentation_string=$$member{firstname}." ".$$member{surname}." ".GetBranchName($$member{branchcode})." ".$$member{description}." ".$$member{category_type};
218         $debug || warn $presentation_string;
219         $template->param($element."information"=>$presentation_string);
220     }
221 }
222 $template->param(
223     %$suggestion_ref,  
224     "op_$op"                => 1,
225     dateformat    => C4::Context->preference("dateformat"),
226     "op"             =>$op,
227 );
228
229
230 ####################
231 ## Initializing selection lists
232
233 #branch display management
234 my $onlymine=C4::Context->preference('IndependantBranches') && 
235             C4::Context->userenv && 
236             C4::Context->userenv->{flags}!=1 && 
237             C4::Context->userenv->{branch};
238 my $branches = GetBranches($onlymine);
239 my @branchloop;
240
241 foreach my $thisbranch ( sort {$branches->{$a}->{'branchname'} cmp $branches->{$b}->{'branchname'}} keys %$branches ) {
242     my %row = (
243         value      => $thisbranch,
244         branchname => $branches->{$thisbranch}->{'branchname'},
245         selected   => ($branches->{$thisbranch}->{'branchcode'} eq $branchfilter)
246                     ||($branches->{$thisbranch}->{'branchcode'} eq $$suggestion_ref{'branchcode'})    
247     );
248     push @branchloop, \%row;
249 }
250 $branchfilter=C4::Context->userenv->{'branch'} if ($onlymine && !$branchfilter);
251
252 $template->param( branchloop => \@branchloop,
253                 branchfilter => $branchfilter);
254
255 # the index parameter is different for item-level itemtypes
256 my $supportlist=GetSupportList();                               
257 foreach my $support(@$supportlist){
258     $$support{'selected'}= $$support{'code'} eq $$suggestion_ref{'itemtype'};
259     if ($$support{'imageurl'}){
260         $$support{'imageurl'}= getitemtypeimagelocation( 'intranet', $$support{'imageurl'} );
261     }
262     else {
263     delete $$support{'imageurl'}
264     }
265 }
266 $template->param(itemtypeloop=>$supportlist);
267
268 #Budgets management
269 my $searchbudgets={ budget_branchcode=>$branchfilter} if $branchfilter;
270 my $budgets = GetBudgets($searchbudgets);
271
272 foreach (@$budgets){
273     $_->{'selected'}=1 if ($$suggestion_ref{'budget_id'} && $_{'budget_id'} eq $$suggestion_ref{'budget_id'})
274 };
275
276 $template->param( budgetsloop => $budgets);
277
278 my %hashlists; 
279 foreach my $field qw(managedby acceptedby suggestedby STATUS){
280     my $values_list;
281     $values_list=GetDistinctValues("suggestions.".$field) ;
282     my @codes_list = map{
283                         { 'code'=>$$_{'value'},
284                         'desc'=>GetCriteriumDesc($$_{'value'},$field),
285                         'selected'=> $$_{'value'} eq $$suggestion_ref{$field}
286                         }
287                     } @$values_list;
288     $hashlists{lc($field)."_loop"}=\@codes_list;
289 }
290 $template->param(%hashlists);
291
292 output_html_with_http_headers $input, $cookie, $template->output;