Bug 18692 - same with syspref TalkingTechItivaPhone
[koha.git] / suggestion / suggestion.pl
blob929e65ad51f143c11d73d30ee3a08223c3b15c56
1 #!/usr/bin/perl
3 # This file is part of Koha.
4 # Copyright 2006-2010 BibLibre
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use strict;
21 #use warnings; FIXME - Bug 2505
22 require Exporter;
23 use CGI qw ( -utf8 );
24 use C4::Auth; # get_template_and_user
25 use C4::Output;
26 use C4::Suggestions;
27 use C4::Koha;
28 use C4::Budgets;
29 use C4::Search;
30 use C4::Members;
31 use C4::Debug;
33 use Koha::DateUtils qw( dt_from_string );
34 use Koha::AuthorisedValues;
35 use Koha::Acquisition::Currencies;
36 use Koha::Libraries;
37 use Koha::Patrons;
39 use URI::Escape;
41 sub Init{
42 my $suggestion= shift @_;
43 # "Managed by" is used only when a suggestion is being edited (not when created)
44 if ($suggestion->{'suggesteddate'} eq "0000-00-00" ||$suggestion->{'suggesteddate'} eq "") {
45 # new suggestion
46 $suggestion->{suggesteddate} = dt_from_string;
47 $suggestion->{'suggestedby'} = C4::Context->userenv->{"number"} unless ($suggestion->{'suggestedby'});
49 else {
50 # editing of an existing suggestion
51 $suggestion->{manageddate} = dt_from_string;
52 $suggestion->{'managedby'} = C4::Context->userenv->{"number"} unless ($suggestion->{'managedby'});
54 $suggestion->{'branchcode'}=C4::Context->userenv->{"branch"} unless ($suggestion->{'branchcode'});
57 sub GetCriteriumDesc{
58 my ($criteriumvalue,$displayby)=@_;
59 if ($displayby =~ /status/i) {
60 unless ( grep { /$criteriumvalue/ } qw(ASKED ACCEPTED REJECTED CHECKED ORDERED AVAILABLE) ) {
61 my $av = Koha::AuthorisedValues->search({ category => 'SUGGEST_STATUS', authorised_value => $criteriumvalue });
62 return $av->count ? $av->next->lib : 'Unknown';
64 return ($criteriumvalue eq 'ASKED'?"Pending":ucfirst(lc( $criteriumvalue))) if ($displayby =~/status/i);
66 return Koha::Libraries->find($criteriumvalue)->branchname
67 if $displayby =~ /branchcode/;
68 if ( $displayby =~ /itemtype/ ) {
69 my $av = Koha::AuthorisedValues->search({ category => 'SUGGEST_FORMAT', authorised_value => $criteriumvalue });
70 return $av->count ? $av->next->lib : 'Unknown';
72 if ($displayby =~/suggestedby/||$displayby =~/managedby/||$displayby =~/acceptedby/){
73 my $patron = Koha::Patrons->find( $criteriumvalue );
74 return "" unless $patron;
75 return $patron->surname . ", " . $patron->firstname;
77 if ( $displayby =~ /budgetid/) {
78 my $budget = GetBudget($criteriumvalue);
79 return "" unless $budget;
80 return $$budget{budget_name};
84 my $input = CGI->new;
85 my $redirect = $input->param('redirect');
86 my $suggestedbyme = (defined $input->param('suggestedbyme')? $input->param('suggestedbyme'):1);
87 my $op = $input->param('op')||'else';
88 my @editsuggestions = $input->multi_param('edit_field');
89 my $suggestedby = $input->param('suggestedby');
90 my $returnsuggestedby = $input->param('returnsuggestedby');
91 my $returnsuggested = $input->param('returnsuggested');
92 my $managedby = $input->param('managedby');
93 my $displayby = $input->param('displayby') || '';
94 my $tabcode = $input->param('tabcode');
96 # filter informations which are not suggestion related.
97 my $suggestion_ref = $input->Vars;
99 # get only the columns of Suggestion
100 my $schema = Koha::Database->new()->schema;
101 my $columns = ' '.join(' ', $schema->source('Suggestion')->columns).' ';
102 my $suggestion_only = { map { $columns =~ / $_ / ? ($_ => $suggestion_ref->{$_}) : () } keys %$suggestion_ref };
103 $suggestion_only->{STATUS} = $suggestion_ref->{STATUS};
105 delete $$suggestion_ref{$_} foreach qw( suggestedbyme op displayby tabcode edit_field );
106 foreach (keys %$suggestion_ref){
107 delete $$suggestion_ref{$_} if (!$$suggestion_ref{$_} && ($op eq 'else' || $op eq 'change'));
109 my ( $template, $borrowernumber, $cookie, $userflags ) = get_template_and_user(
111 template_name => "suggestion/suggestion.tt",
112 query => $input,
113 type => "intranet",
114 flagsrequired => { catalogue => 1 },
118 $borrowernumber = $input->param('borrowernumber') if ( $input->param('borrowernumber') );
119 $template->param('borrowernumber' => $borrowernumber);
121 #########################################
122 ## Operations
124 if ( $op =~ /save/i ) {
125 $suggestion_only->{suggesteddate} = dt_from_string( $suggestion_only->{suggesteddate} )
126 if $suggestion_only->{suggesteddate};
128 if ( $suggestion_only->{"STATUS"} ) {
129 if ( my $tmpstatus = lc( $suggestion_only->{"STATUS"} ) =~ /ACCEPTED|REJECTED/i ) {
130 $suggestion_only->{ lc( $suggestion_only->{"STATUS"}) . "date" } = dt_from_string;
131 $suggestion_only->{ lc( $suggestion_only->{"STATUS"}) . "by" } = C4::Context->userenv->{number};
133 $suggestion_only->{manageddate} = dt_from_string;
134 $suggestion_only->{"managedby"} = C4::Context->userenv->{number};
136 if ( $suggestion_only->{'suggestionid'} > 0 ) {
137 &ModSuggestion($suggestion_only);
138 } else {
139 ###FIXME:Search here if suggestion already exists.
140 my $suggestions_loop =
141 SearchSuggestion( $suggestion_only );
142 if (@$suggestions_loop>=1){
143 #some suggestion are answering the request Donot Add
144 my @messages;
145 for my $suggestion ( @$suggestions_loop ) {
146 push @messages, { type => 'error', code => 'already_exists', id => $suggestion->{suggestionid} };
148 $template->param( messages => \@messages );
150 else {
151 ## Adding some informations related to suggestion
152 &NewSuggestion($suggestion_only);
154 # empty fields, to avoid filter in "SearchSuggestion"
156 map{delete $$suggestion_ref{$_}} keys %$suggestion_ref;
157 $op = 'else';
159 if( $redirect eq 'purchase_suggestions' ) {
160 print $input->redirect("/cgi-bin/koha/members/purchase-suggestions.pl?borrowernumber=$borrowernumber");
164 elsif ($op=~/add/) {
165 #Adds suggestion
166 Init($suggestion_ref);
167 $op ='save';
169 elsif ($op=~/edit/) {
170 #Edit suggestion
171 $suggestion_ref=&GetSuggestion($$suggestion_ref{'suggestionid'});
172 Init($suggestion_ref);
173 $op ='save';
175 elsif ($op eq "change" ) {
176 # set accepted/rejected/managed informations if applicable
177 # ie= if the librarian has chosen some action on the suggestions
178 if ($suggestion_only->{"STATUS"} eq "ACCEPTED"){
179 $suggestion_only->{accepteddate} = dt_from_string;
180 $suggestion_only->{"acceptedby"}=C4::Context->userenv->{number};
181 } elsif ($suggestion_only->{"STATUS"} eq "REJECTED"){
182 $suggestion_only->{rejecteddate} = dt_from_string;
183 $suggestion_only->{"rejectedby"}=C4::Context->userenv->{number};
185 if ($suggestion_only->{"STATUS"}){
186 $suggestion_only->{manageddate} = dt_from_string;
187 $suggestion_only->{"managedby"}=C4::Context->userenv->{number};
189 if ( my $reason = $$suggestion_ref{"reason$tabcode"}){
190 if ( $reason eq "other" ) {
191 $reason = $$suggestion_ref{"other_reason$tabcode"};
193 $suggestion_only->{reason}=$reason;
196 foreach my $suggestionid (@editsuggestions) {
197 next unless $suggestionid;
198 $suggestion_only->{'suggestionid'}=$suggestionid;
199 &ModSuggestion($suggestion_only);
201 my $params = '';
202 foreach my $key (
204 displayby branchcode title author isbn publishercode copyrightdate
205 collectiontitle suggestedby suggesteddate_from suggesteddate_to
206 manageddate_from manageddate_to accepteddate_from
207 accepteddate_to budgetid
211 $params .= $key . '=' . uri_escape($input->param($key)) . '&'
212 if defined($input->param($key));
214 print $input->redirect("/cgi-bin/koha/suggestion/suggestion.pl?$params");
215 }elsif ($op eq "delete" ) {
216 foreach my $delete_field (@editsuggestions) {
217 &DelSuggestion( $borrowernumber, $delete_field,'intranet' );
219 $op = 'else';
221 elsif ( $op eq 'show' ) {
222 $suggestion_ref=&GetSuggestion($$suggestion_ref{'suggestionid'});
223 my $budget = GetBudget $$suggestion_ref{budgetid};
224 $$suggestion_ref{budgetname} = $$budget{budget_name};
225 Init($suggestion_ref);
227 if ($op=~/else/) {
228 $op='else';
230 $displayby||="STATUS";
231 delete $$suggestion_ref{'branchcode'} if($displayby eq "branchcode");
232 # distinct values of display by
233 my $criteria_list=GetDistinctValues("suggestions.".$displayby);
234 my (@criteria_dv, $criteria_has_empty);
235 foreach (@$criteria_list) {
236 if ($_->{value}) {
237 push @criteria_dv, $_->{value};
238 } else {
239 $criteria_has_empty = 1;
242 # aggregate null and empty values under empty value
243 push @criteria_dv, '' if $criteria_has_empty;
245 my @allsuggestions;
246 my $reasonsloop = GetAuthorisedValues("SUGGEST");
247 foreach my $criteriumvalue ( @criteria_dv ) {
248 # By default, display suggestions from current working branch
249 unless ( exists $$suggestion_ref{'branchcode'} ) {
250 $$suggestion_ref{'branchcode'} = C4::Context->userenv->{'branch'};
252 my $definedvalue = defined $$suggestion_ref{$displayby} && $$suggestion_ref{$displayby} ne "";
254 next if ( $definedvalue && $$suggestion_ref{$displayby} ne $criteriumvalue );
255 $$suggestion_ref{$displayby} = $criteriumvalue;
257 my $suggestions = &SearchSuggestion($suggestion_ref);
258 foreach my $suggestion (@$suggestions) {
259 if ($suggestion->{budgetid}){
260 my $bud = GetBudget( $suggestion->{budgetid} );
261 $suggestion->{budget_name} = $bud->{budget_name} if $bud;
264 push @allsuggestions,{
265 "suggestiontype"=>$criteriumvalue||"suggest",
266 "suggestiontypelabel"=>GetCriteriumDesc($criteriumvalue,$displayby)||"",
267 "suggestionscount"=>scalar(@$suggestions),
268 'suggestions_loop'=>$suggestions,
269 'reasonsloop' => $reasonsloop,
272 delete $$suggestion_ref{$displayby} unless $definedvalue;
275 $template->param(
276 "displayby"=> $displayby,
277 "notabs"=> $displayby eq "",
278 suggestions => \@allsuggestions,
282 foreach my $element ( qw(managedby suggestedby acceptedby) ) {
283 # $debug || warn $$suggestion_ref{$element};
284 if ($$suggestion_ref{$element}){
285 my $patron = Koha::Patrons->find( $$suggestion_ref{$element} );
286 my $category = $patron->category;
287 $template->param(
288 $element."_borrowernumber"=>$patron->borrowernumber,
289 $element."_firstname"=>$patron->firstname,
290 $element."_surname"=>$patron->surname,
291 $element."_cardnumber"=>$patron->cardnumber,
292 $element."_branchcode"=>$patron->branchcode,
293 $element."_description"=>$category->description,
294 $element."_category_type"=>$category->category_type,
298 $template->param(
299 %$suggestion_ref,
300 "op_$op" => 1,
301 "op" =>$op,
304 if(defined($returnsuggested) and $returnsuggested ne "noone")
306 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=".$returnsuggested."#suggestions");
309 my $branchfilter = ($displayby ne "branchcode") ? $input->param('branchcode') : C4::Context->userenv->{'branch'};
311 $template->param(
312 branchfilter => $branchfilter,
315 $template->param( returnsuggestedby => $returnsuggestedby );
317 my $patron_reason_loop = GetAuthorisedValues("OPAC_SUG");
318 $template->param(patron_reason_loop=>$patron_reason_loop);
320 #Budgets management
321 my $budgets = [];
322 if ($branchfilter) {
323 my $searchbudgets = { budget_branchcode => $branchfilter };
324 $budgets = GetBudgets($searchbudgets);
325 } else {
326 $budgets = GetBudgets(undef);
329 my @budgets_loop;
330 foreach my $budget ( @{$budgets} ) {
331 next unless (CanUserUseBudget($borrowernumber, $budget, $userflags));
333 ## Please see file perltidy.ERR
334 $budget->{'selected'} = 1
335 if ($$suggestion_ref{'budgetid'}
336 && $budget->{'budget_id'} eq $$suggestion_ref{'budgetid'});
338 push @budgets_loop, $budget;
341 $template->param( budgetsloop => \@budgets_loop);
342 $template->param( "statusselected_$$suggestion_ref{'STATUS'}" =>1) if ($$suggestion_ref{'STATUS'});
344 my @currencies = Koha::Acquisition::Currencies->search;
345 $template->param(
346 currencies => \@currencies,
347 suggestion => $suggestion_ref,
348 price => sprintf("%.2f", $$suggestion_ref{'price'}||0),
349 total => sprintf("%.2f", $$suggestion_ref{'total'}||0),
352 # lists of distinct values (without empty) for filters
353 my %hashlists;
354 foreach my $field ( qw(managedby acceptedby suggestedby budgetid) ) {
355 my $values_list;
356 $values_list = GetDistinctValues( "suggestions." . $field );
357 my @codes_list = map {
358 { 'code' => $$_{'value'},
359 'desc' => GetCriteriumDesc( $$_{'value'}, $field ) || $$_{'value'},
360 'selected' => ($$suggestion_ref{$field}) ? $$_{'value'} eq $$suggestion_ref{$field} : 0,
362 } grep {
363 $$_{'value'}
364 } @$values_list;
365 $hashlists{ lc($field) . "_loop" } = \@codes_list;
368 $template->param(
369 %hashlists,
370 borrowernumber => ($input->param('borrowernumber') // undef),
371 SuggestionStatuses => GetAuthorisedValues('SUGGEST_STATUS'),
373 output_html_with_http_headers $input, $cookie, $template->output;