Followup : fb4366cdad0ad96d0427810581763dc2fc189af1
[koha.git] / suggestion / acceptorrejectpd.pl
1 #!/usr/bin/perl
2
3 # Copyright 2008 BibLibre, Olivier SAURY
4 #                SAN Ouest Provence
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21
22 =head1 NAME
23
24 acceptorreject.pl
25
26 =head1 DESCRIPTION
27
28 this script modify the status of a subscription to ACCEPTED or to REJECTED
29
30 =head1 PARAMETERS
31
32 =over 4
33
34 =item op
35
36 op can be :
37  * aorr_confirm : to confirm accept or reject
38  * accepted : to display only accepted.
39  * rejected : to display only rejected.
40
41 =back
42
43
44 =cut
45
46 ## modules
47 ###################################################################################
48
49 use strict;
50 require Exporter;
51 use CGI;
52
53 use C4::Auth;    # get_template_and_user
54 use C4::Output;
55 use C4::Suggestions;
56 use C4::Koha;    # GetAuthorisedValue
57 use C4::Dates qw/format_date format_date_in_iso/;
58
59
60 ## variables
61 ###################################################################################
62
63 ## input variables
64 my $input           = new CGI;
65 my $bookfundgroupnumber = $input->param('bookfundgroupnumber');
66 my $op              = $input->param('op') || "aorr_confirm";
67
68 ## other variables
69 my $bookfundgroupname;
70 my @suggestions_loop;
71
72 my $dbh = C4::Context->dbh;
73
74 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
75     {
76         template_name   => "suggestion/acceptorrejectpd.tmpl",
77         type            => "intranet",
78         query           => $input,
79         authnotrequired => 1,
80         flagsrequired   => { catalogue => 1 },
81     }
82 );
83
84 my $suggestion_loop0;
85
86
87 ###################################################################################
88 ###################################################################################
89 ## modify suggestions'status. Choose a list of suggestions
90 ###################################################################################
91 ###################################################################################
92
93
94 if ( $op eq "aorr_confirm" ) {
95
96 ## modify suggestions'status
97 ###################################################################################
98
99     my @suggestionlist = $input->param("aorr");
100
101     foreach my $suggestion (@suggestionlist) {
102         if ( $suggestion =~ /(A|R)(.*)/ ) {
103
104             my ( $newstatus, $ordernumber ) = ( $1, $2 );
105             $newstatus = "REJECTED" if $newstatus eq "R";
106             $newstatus = "ACCEPTED" if $newstatus eq "A";
107             my $reason = $input->param( "reason" . $ordernumber );
108             if ( $reason eq "other" ) {
109                 $reason = $input->param( "other-reason" . $ordernumber );
110             }
111
112             my $bookfundnumber = $input->param( "bookfunds_loop".$ordernumber);
113             my $step = $input->param( "step".$ordernumber);
114
115             ModStatus(
116                 $ordernumber,
117                 $newstatus,
118                 $reason,
119                 $bookfundgroupnumber,
120                 $bookfundnumber,
121                 $loggedinuser,
122                 $step,
123                 '',
124                 $input,
125                 );
126         }
127     }
128     $op = "else";
129     $suggestion_loop0 = &SearchSuggestion("", "", "", "", 'ASKED', "","",2,$bookfundgroupnumber);
130 }
131
132
133 if ( $op eq "accepted" ) {
134
135 ## accepted suggestions
136 ###################################################################################
137
138     $suggestion_loop0 = &GetSuggestionByStatus('ACCEPTED',3,$bookfundgroupnumber);
139     $template->param(done => 1);
140 }
141
142
143 if ( $op eq "rejected" ) {
144
145 ## rejected suggestions
146 ###################################################################################
147
148     $suggestion_loop0 = &GetSuggestionByStatus('REJECTED',3,$bookfundgroupnumber);
149     $template->param(done => 1);
150 }
151
152
153 ## book fund group name
154 #########################################################################################
155 my $dbh = C4::Context->dbh;
156 my $sth = $dbh->prepare("
157 SELECT bookfundgroupname
158 FROM aq2bookfundgroups
159 WHERE bookfundgroupnumber=?
160 ");
161 $sth->execute($bookfundgroupnumber);
162 my $data = $sth->fetchrow_hashref;
163 $bookfundgroupname = $data->{'bookfundgroupname'};
164 $sth->finish;
165
166
167
168 foreach my $suggestion (@$suggestion_loop0) {
169
170 ###################################################################################
171 ###################################################################################
172 ## get more information about suggestions
173 ###################################################################################
174 ###################################################################################
175
176
177     ## reasonsloop
178     $suggestion->{'reasonsloop'} = GetAuthorisedValues("SUGGEST");
179
180     ## dates
181     $suggestion->{'suggestioncreatedon'} = format_date($suggestion->{'suggestioncreatedon'});
182     $suggestion->{'suggestionmanagedingpdon'} = format_date($suggestion->{'suggestionmanagedingpdon'});
183     $suggestion->{'suggestionmanagedinpdon'} =
184     format_date($suggestion->{'suggestionmanagedinpdon'});
185
186
187     ## bookfunds_loop
188     ###############################################################################
189     my $dbh = C4::Context->dbh;
190     my $sth = $dbh->prepare("
191     SELECT bookfundnumber, bookfundname
192     FROM aq2bookfunds
193     WHERE bookfundgroupnumber=?
194     ORDER BY bookfundname
195     ");
196     $sth->execute($bookfundgroupnumber);
197
198     my @bookfunds_loop;
199
200     while (my $data = $sth->fetchrow_hashref) {
201
202         ## book fund number of the suggestion
203         $data->{selected}=($data->{'bookfundnumber'} eq $suggestion->{'bookfundnumber'})? 1:0;
204
205         ## book fund name of the suggestion
206         if ($data->{'bookfundnumber'} eq $suggestion->{'bookfundnumber'}) {
207             $suggestion->{'bookfundname'} = $data->{'bookfundname'};
208         }
209
210         push( @bookfunds_loop, $data);
211     }
212     $sth->finish;
213
214     $suggestion->{'bookfunds_loop'} = \@bookfunds_loop;
215
216
217     ## name of the person who managed the suggestion in the General Purchase Department
218     ###################################################################################
219
220     my $dbh = C4::Context->dbh;
221     my $sth = $dbh->prepare("
222     SELECT  firstname AS firstnamemanagedingpdby,
223             surname AS surnamemanagedingpdby
224     FROM borrowers
225     WHERE borrowernumber=?
226     ");
227     $sth->execute($suggestion->{'suggestionmanagedingpdby'});
228     my $namemanagedingpdby = $sth->fetchrow_hashref;
229     $sth->finish;
230     $suggestion->{'firstnamemanagedingpdby'} = $namemanagedingpdby->{'firstnamemanagedingpdby'};
231     $suggestion->{'surnamemanagedingpdby'} = $namemanagedingpdby->{'surnamemanagedingpdby'};
232
233
234     ## name of the person who managed the suggestion in a Purchase Department
235     ###################################################################################
236     my $dbh = C4::Context->dbh;
237     my $sth = $dbh->prepare("
238     SELECT  firstname AS firstnamemanagedinpdby,
239             surname AS surnamemanagedinpdby
240     FROM borrowers
241     WHERE borrowernumber=?
242     ");
243     $sth->execute($suggestion->{'suggestionmanagedinpdby'});
244     my $namemanagedinpdby = $sth->fetchrow_hashref;
245     $sth->finish;
246     $suggestion->{'firstnamemanagedinpdby'} = $namemanagedinpdby->{'firstnamemanagedinpdby'};
247     $suggestion->{'surnamemanagedinpdby'} = $namemanagedinpdby->{'surnamemanagedinpdby'};
248
249
250     ## insert the suggestion into the table of suggestions
251     ################################################################################
252     push @suggestions_loop, $suggestion ;
253
254
255 ###################################################################################
256 ## $template
257 ###################################################################################
258 }
259
260 $template->param(
261     suggestions_loop        => \@suggestions_loop,
262     bookfundgroupnumber     => $bookfundgroupnumber,
263     bookfundgroupname       => $bookfundgroupname,
264     "op_$op"                => 1,
265 );
266
267 output_html_with_http_headers $input, $cookie, $template->output;