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