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