Bug 8096 - card number twice on patron search
[koha.git] / suggestion / suggestion.pl
blobb54703a11f4a809c45468c73d5b12bc2f132300e
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 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.
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.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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; #GetItemTypes
28 use C4::Branch;
29 use C4::Budgets;
30 use C4::Search;
31 use C4::Members;
32 use C4::Debug;
34 use Koha::DateUtils qw( dt_from_string );
36 use URI::Escape;
38 sub Init{
39 my $suggestion= shift @_;
40 # "Managed by" is used only when a suggestion is being edited (not when created)
41 if ($suggestion->{'suggesteddate'} eq "0000-00-00" ||$suggestion->{'suggesteddate'} eq "") {
42 # new suggestion
43 $suggestion->{suggesteddate} = dt_from_string;
44 $suggestion->{'suggestedby'} = C4::Context->userenv->{"number"} unless ($suggestion->{'suggestedby'});
46 else {
47 # editing of an existing suggestion
48 $suggestion->{manageddate} = dt_from_string;
49 $suggestion->{'managedby'} = C4::Context->userenv->{"number"} unless ($suggestion->{'managedby'});
51 $suggestion->{'branchcode'}=C4::Context->userenv->{"branch"} unless ($suggestion->{'branchcode'});
54 sub GetCriteriumDesc{
55 my ($criteriumvalue,$displayby)=@_;
56 if ($displayby =~ /status/i) {
57 if ( grep { /^($criteriumvalue)$/ } qw(ASKED ACCEPTED REJECTED CHECKED ORDERED AVAILABLE) ) {
58 return ($criteriumvalue eq 'ASKED'?"Pending":ucfirst(lc( $criteriumvalue)));
59 } else {
60 return GetAuthorisedValueByCode('SUGGEST_STATUS', $criteriumvalue) || $criteriumvalue;
63 return (GetBranchName($criteriumvalue)) if ($displayby =~/branchcode/);
64 return (GetSupportName($criteriumvalue)) if ($displayby =~/itemtype/);
65 if ($displayby =~/suggestedby/||$displayby =~/managedby/||$displayby =~/acceptedby/){
66 my $borr=C4::Members::GetMember(borrowernumber=>$criteriumvalue);
67 return "" unless $borr;
68 return $$borr{surname} . ", " . $$borr{firstname};
70 if ( $displayby =~ /budgetid/) {
71 my $budget = GetBudget($criteriumvalue);
72 return "" unless $budget;
73 return $$budget{budget_name};
77 my $input = CGI->new;
78 my $redirect = $input->param('redirect');
79 my $suggestedbyme = (defined $input->param('suggestedbyme')? $input->param('suggestedbyme'):1);
80 my $op = $input->param('op')||'else';
81 my @editsuggestions = $input->param('edit_field');
82 my $suggestedby = $input->param('suggestedby');
83 my $returnsuggestedby = $input->param('returnsuggestedby');
84 my $returnsuggested = $input->param('returnsuggested');
85 my $managedby = $input->param('managedby');
86 my $displayby = $input->param('displayby') || '';
87 my $tabcode = $input->param('tabcode');
89 # filter informations which are not suggestion related.
90 my $suggestion_ref = $input->Vars;
92 # get only the columns of Suggestion
93 my $schema = Koha::Database->new()->schema;
94 my $columns = ' '.join(' ', $schema->source('Suggestion')->columns).' ';
95 my $suggestion_only = { map { $columns =~ / $_ / ? ($_ => $suggestion_ref->{$_}) : () } keys %$suggestion_ref };
96 $suggestion_only->{STATUS} = $suggestion_ref->{STATUS};
98 delete $$suggestion_ref{$_} foreach qw( suggestedbyme op displayby tabcode edit_field );
99 foreach (keys %$suggestion_ref){
100 delete $$suggestion_ref{$_} if (!$$suggestion_ref{$_} && ($op eq 'else' || $op eq 'change'));
102 my ( $template, $borrowernumber, $cookie, $userflags ) = get_template_and_user(
104 template_name => "suggestion/suggestion.tt",
105 query => $input,
106 type => "intranet",
107 flagsrequired => { catalogue => 1 },
111 $borrowernumber = $input->param('borrowernumber') if ( $input->param('borrowernumber') );
112 $template->param('borrowernumber' => $borrowernumber);
114 #########################################
115 ## Operations
117 if ( $op =~ /save/i ) {
118 $suggestion_only->{suggesteddate} = dt_from_string( $suggestion_only->{suggesteddate} )
119 if $suggestion_only->{suggesteddate};
121 if ( $suggestion_only->{"STATUS"} ) {
122 if ( my $tmpstatus = lc( $suggestion_only->{"STATUS"} ) =~ /ACCEPTED|REJECTED/i ) {
123 $suggestion_only->{ lc( $suggestion_only->{"STATUS"}) . "date" } = dt_from_string;
124 $suggestion_only->{ lc( $suggestion_only->{"STATUS"}) . "by" } = C4::Context->userenv->{number};
126 $suggestion_only->{manageddate} = dt_from_string;
127 $suggestion_only->{"managedby"} = C4::Context->userenv->{number};
129 if ( $suggestion_only->{'suggestionid'} > 0 ) {
130 &ModSuggestion($suggestion_only);
131 } else {
132 ###FIXME:Search here if suggestion already exists.
133 my $suggestions_loop =
134 SearchSuggestion( $suggestion_only );
135 if (@$suggestions_loop>=1){
136 #some suggestion are answering the request Donot Add
138 else {
139 ## Adding some informations related to suggestion
140 &NewSuggestion($suggestion_only);
142 # empty fields, to avoid filter in "SearchSuggestion"
144 map{delete $$suggestion_ref{$_}} keys %$suggestion_ref;
145 $op = 'else';
147 if( $redirect eq 'purchase_suggestions' ) {
148 print $input->redirect("/cgi-bin/koha/members/purchase-suggestions.pl?borrowernumber=$borrowernumber");
152 elsif ($op=~/add/) {
153 #Adds suggestion
154 Init($suggestion_ref);
155 $op ='save';
157 elsif ($op=~/edit/) {
158 #Edit suggestion
159 $suggestion_ref=&GetSuggestion($$suggestion_ref{'suggestionid'});
160 Init($suggestion_ref);
161 $op ='save';
163 elsif ($op eq "change" ) {
164 # set accepted/rejected/managed informations if applicable
165 # ie= if the librarian has choosen some action on the suggestions
166 if ($suggestion_only->{"STATUS"} eq "ACCEPTED"){
167 $suggestion_only->{accepteddate} = dt_from_string;
168 $suggestion_only->{"acceptedby"}=C4::Context->userenv->{number};
169 } elsif ($suggestion_only->{"STATUS"} eq "REJECTED"){
170 $suggestion_only->{rejecteddate} = dt_from_string;
171 $suggestion_only->{"rejectedby"}=C4::Context->userenv->{number};
173 if ($suggestion_only->{"STATUS"}){
174 $suggestion_only->{manageddate} = dt_from_string;
175 $suggestion_only->{"managedby"}=C4::Context->userenv->{number};
177 if ( my $reason = $$suggestion_ref{"reason$tabcode"}){
178 if ( $reason eq "other" ) {
179 $reason = $$suggestion_ref{"other_reason$tabcode"};
181 $$suggestion_ref{'reason'}=$reason;
183 delete $$suggestion_ref{$_} foreach ("reason$tabcode", "other_reason$tabcode");
184 foreach (keys %$suggestion_ref){
185 delete $$suggestion_ref{$_} unless ($$suggestion_ref{$_});
187 foreach my $suggestionid (@editsuggestions) {
188 next unless $suggestionid;
189 $suggestion_only->{'suggestionid'}=$suggestionid;
190 &ModSuggestion($suggestion_only);
192 my $params = '';
193 foreach my $key (
195 displayby branchcode title author isbn publishercode copyrightdate
196 collectiontitle suggestedby suggesteddate_from suggesteddate_to
197 manageddate_from manageddate_to accepteddate_from
198 accepteddate_to budgetid
202 $params .= $key . '=' . uri_escape($input->param($key)) . '&'
203 if defined($input->param($key));
205 print $input->redirect("/cgi-bin/koha/suggestion/suggestion.pl?$params");
206 }elsif ($op eq "delete" ) {
207 foreach my $delete_field (@editsuggestions) {
208 &DelSuggestion( $borrowernumber, $delete_field,'intranet' );
210 $op = 'else';
212 elsif ( $op eq 'show' ) {
213 $suggestion_ref=&GetSuggestion($$suggestion_ref{'suggestionid'});
214 $$suggestion_ref{branchname} = GetBranchName $$suggestion_ref{branchcode};
215 my $budget = GetBudget $$suggestion_ref{budgetid};
216 $$suggestion_ref{budgetname} = $$budget{budget_name};
217 Init($suggestion_ref);
219 if ($op=~/else/) {
220 $op='else';
222 $displayby||="STATUS";
223 delete $$suggestion_ref{'branchcode'} if($displayby eq "branchcode");
224 # distinct values of display by
225 my $criteria_list=GetDistinctValues("suggestions.".$displayby);
226 my (@criteria_dv, $criteria_has_empty);
227 foreach (@$criteria_list) {
228 if ($_->{value}) {
229 push @criteria_dv, $_->{value};
230 } else {
231 $criteria_has_empty = 1;
234 # agregate null and empty values under empty value
235 push @criteria_dv, '' if $criteria_has_empty;
237 my @allsuggestions;
238 my $reasonsloop = GetAuthorisedValues("SUGGEST");
239 foreach my $criteriumvalue ( @criteria_dv ) {
240 # By default, display suggestions from current working branch
241 unless ( exists $$suggestion_ref{'branchcode'} ) {
242 $$suggestion_ref{'branchcode'} = C4::Context->userenv->{'branch'};
244 my $definedvalue = defined $$suggestion_ref{$displayby} && $$suggestion_ref{$displayby} ne "";
246 next if ( $definedvalue && $$suggestion_ref{$displayby} ne $criteriumvalue );
247 $$suggestion_ref{$displayby} = $criteriumvalue;
249 my $suggestions = &SearchSuggestion($suggestion_ref);
250 foreach my $suggestion (@$suggestions) {
251 if ($suggestion->{budgetid}){
252 my $bud = GetBudget( $suggestion->{budgetid} );
253 $suggestion->{budget_name} = $bud->{budget_name} if $bud;
256 push @allsuggestions,{
257 "suggestiontype"=>$criteriumvalue||"suggest",
258 "suggestiontypelabel"=>GetCriteriumDesc($criteriumvalue,$displayby)||"",
259 "suggestionscount"=>scalar(@$suggestions),
260 'suggestions_loop'=>$suggestions,
261 'reasonsloop' => $reasonsloop,
264 delete $$suggestion_ref{$displayby} unless $definedvalue;
267 $template->param(
268 "displayby"=> $displayby,
269 "notabs"=> $displayby eq "",
270 suggestions => \@allsuggestions,
274 foreach my $element ( qw(managedby suggestedby acceptedby) ) {
275 # $debug || warn $$suggestion_ref{$element};
276 if ($$suggestion_ref{$element}){
277 my $member=GetMember(borrowernumber=>$$suggestion_ref{$element});
278 $template->param(
279 $element."_borrowernumber"=>$$member{borrowernumber},
280 $element."_firstname"=>$$member{firstname},
281 $element."_surname"=>$$member{surname},
282 $element."_branchcode"=>$$member{branchcode},
283 $element."_description"=>$$member{description},
284 $element."_category_type"=>$$member{category_type}
288 $template->param(
289 %$suggestion_ref,
290 "op_$op" => 1,
291 "op" =>$op,
294 if(defined($returnsuggested) and $returnsuggested ne "noone")
296 print $input->redirect("/cgi-bin/koha/members/moremember.pl?borrowernumber=".$returnsuggested."#suggestions");
299 ####################
300 ## Initializing selection lists
302 #branch display management
303 my $branchfilter = ($displayby ne "branchcode") ? $input->param('branchcode') : '';
304 my $onlymine =
305 C4::Context->preference('IndependentBranches')
306 && C4::Context->userenv
307 && !C4::Context->IsSuperLibrarian()
308 && C4::Context->userenv->{branch};
309 my $branches = GetBranches($onlymine);
310 my @branchloop;
312 foreach my $thisbranch ( sort {$branches->{$a}->{'branchname'} cmp $branches->{$b}->{'branchname'}} keys %$branches ) {
313 my %row = (
314 value => $thisbranch,
315 branchname => $branches->{$thisbranch}->{'branchname'},
316 selected => ($branchfilter and $branches->{$thisbranch}->{'branchcode'} eq $branchfilter ) || ( $$suggestion_ref{'branchcode'} and $branches->{$thisbranch}->{'branchcode'} eq $$suggestion_ref{'branchcode'} )
318 push @branchloop, \%row;
320 $branchfilter=C4::Context->userenv->{'branch'} if ($onlymine && !$branchfilter);
322 $template->param( branchloop => \@branchloop,
323 branchfilter => $branchfilter);
325 # the index parameter is different for item-level itemtypes
326 my $supportlist = GetSupportList();
328 foreach my $support (@$supportlist) {
329 $$support{'selected'} = (defined $$suggestion_ref{'itemtype'})
330 ? $$support{'itemtype'} eq $$suggestion_ref{'itemtype'}
331 : 0;
332 if ( $$support{'imageurl'} ) {
333 $$support{'imageurl'} = getitemtypeimagelocation( 'intranet', $$support{'imageurl'} );
334 } else {
335 delete $$support{'imageurl'};
338 $template->param(itemtypeloop=>$supportlist);
339 $template->param( returnsuggestedby => $returnsuggestedby );
341 my $patron_reason_loop = GetAuthorisedValues("OPAC_SUG",$$suggestion_ref{'patronreason'});
342 $template->param(patron_reason_loop=>$patron_reason_loop);
344 #Budgets management
345 my $budgets = [];
346 if ($branchfilter) {
347 my $searchbudgets = { budget_branchcode => $branchfilter };
348 $budgets = GetBudgets($searchbudgets);
349 } else {
350 $budgets = GetBudgets(undef);
353 my @budgets_loop;
354 foreach my $budget ( @{$budgets} ) {
355 next unless (CanUserUseBudget($borrowernumber, $budget, $userflags));
357 ## Please see file perltidy.ERR
358 $budget->{'selected'} = 1
359 if ($$suggestion_ref{'budgetid'}
360 && $budget->{'budget_id'} eq $$suggestion_ref{'budgetid'});
362 push @budgets_loop, $budget;
365 $template->param( budgetsloop => \@budgets_loop);
366 $template->param( "statusselected_$$suggestion_ref{'STATUS'}" =>1) if ($$suggestion_ref{'STATUS'});
368 # get currencies and rates
369 my @rates = GetCurrencies();
370 my $count = scalar @rates;
371 my $active_currency = GetCurrency();
372 my $selected_currency;
373 if ($$suggestion_ref{'currency'}) {
374 $selected_currency = $$suggestion_ref{'currency'};
376 else {
377 $selected_currency = $active_currency->{currency};
380 my @loop_currency = ();
381 for ( my $i = 0 ; $i < $count ; $i++ ) {
382 my %line;
383 $line{currcode} = $rates[$i]->{'currency'};
384 $line{rate} = $rates[$i]->{'rate'};
385 $line{selected} = 1 if ($line{'currcode'} eq $selected_currency);
386 push @loop_currency, \%line;
388 $template->param(
389 loop_currency => \@loop_currency,
390 price => sprintf("%.2f", $$suggestion_ref{'price'}||0),
391 total => sprintf("%.2f", $$suggestion_ref{'total'}||0),
394 # lists of distinct values (without empty) for filters
395 my %hashlists;
396 foreach my $field ( qw(managedby acceptedby suggestedby budgetid) ) {
397 my $values_list;
398 $values_list = GetDistinctValues( "suggestions." . $field );
399 my @codes_list = map {
400 { 'code' => $$_{'value'},
401 'desc' => GetCriteriumDesc( $$_{'value'}, $field ) || $$_{'value'},
402 'selected' => ($$suggestion_ref{$field}) ? $$_{'value'} eq $$suggestion_ref{$field} : 0,
404 } grep {
405 $$_{'value'}
406 } @$values_list;
407 $hashlists{ lc($field) . "_loop" } = \@codes_list;
410 $template->param(
411 %hashlists,
412 borrowernumber => ($input->param('borrowernumber') // undef),
413 SuggestionStatuses => GetAuthorisedValues('SUGGEST_STATUS'),
415 output_html_with_http_headers $input, $cookie, $template->output;