suggestions changes (probably useless)
[koha.git] / suggestion / suggestiondetailspd.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 ## modules
23 ###################################################################################
24
25 use strict;
26 require Exporter;
27 use CGI;
28
29 use List::Util qw/min/;
30 use C4::Context;
31 use C4::Auth;    # get_template_and_user
32 use C4::Output;
33 use C4::Suggestions;
34 use C4::Koha;    # GetAuthorisedValue
35 use C4::Dates qw/format_date format_date_in_iso/;
36 use C4::Branch; # GetBranches
37
38 ## variables
39 ###############################################################################
40
41 my $input           = new CGI;
42
43 my $ordernumber     = $input->param('ordernumber');
44 my $bookfundgroupname = $input->param('bookfundgroupname');
45
46 my $op              = $input->param('op');
47
48 my $bookfundgroupnumber   = $input->param('bookfundgroupnumber');
49
50
51 my $dbh = C4::Context->dbh;
52 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
53     {
54         template_name   =>
55         "/suggestion/suggestiondetailspd.tmpl",
56         type            => "intranet",
57         query           => $input,
58         authnotrequired => 0,
59         flagsrequired   => { catalogue => 1 },
60     }
61 );
62
63
64
65 if ($op eq "add_confirm") {
66
67 ################################################################################################
68 ################################################################################################
69 ## modify a suggestion
70 ################################################################################################
71 ################################################################################################
72
73     ## 'bookfundgroupnumber', 'bookfundnumber', 'audiencenumber'
74     my $bookfund_loop = ($input->param('bookfund_loop') eq "")?undef:$input->param('bookfund_loop');
75     my $audience_loop = ($input->param('audience_loop') eq "")?undef:$input->param('audience_loop');
76
77     ## modify the suggestion
78     my $dbh = C4::Context->dbh;
79     my $sth = $dbh->prepare('
80         UPDATE aq2orders
81         SET       title = ?
82                 , author = ?
83                 , copyrightdate = ?
84                 , isbn = ?
85                 , publishercode = ?
86                 , seriestitle = ?
87                 , note = ?
88                 , itemtype = ?
89                 , rrp = ?
90                 , bookfundgroupnumber = ?
91                 , bookfundnumber = ?
92                 , audiencenumber = ?
93                 , branchcode = ?
94         WHERE ordernumber = ?
95     ');
96
97
98     $sth->execute(
99         $input->param('title')?$input->param('title'):"",
100         $input->param('author')?$input->param('author'):"",
101         $input->param('copyrightdate')?$input->param('copyrightdate'):"",
102         $input->param('isbn')?$input->param('isbn'):"",
103         $input->param('publishercode')?$input->param('publishercode'):"",
104         $input->param('seriestitle')?$input->param('seriestitle'):"",
105         $input->param('note')?$input->param('note'):"",
106         $input->param('itemtype')?$input->param('itemtype'):"",
107         $input->param('rrp')?$input->param('rrp'):undef,
108         $bookfundgroupnumber ? $bookfundgroupnumber : undef,
109         $bookfundgroupnumber ? $bookfund_loop: undef,
110         $bookfundgroupnumber ? $audience_loop : undef,
111         $input->param('branch_loop')?$input->param('branch_loop'):"",
112         $input->param('ordernumber'),
113     );
114     $sth->finish;
115
116
117     if ($input->param("aorr")) {
118
119         ModStatus(
120             $input->param('ordernumber'),
121             $input->param("aorr"),
122             $input->param('reason'),
123             $bookfundgroupnumber,
124             $bookfund_loop,
125             $loggedinuser,
126             2,                              ##$step
127             '',
128             $input,
129             );
130     }
131
132
133
134     print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=acceptorrejectpd.pl?bookfundgroupnumber=".$bookfundgroupnumber."\"></html>";
135     exit;
136
137 }
138 else {
139
140 ################################################################################################
141 ################################################################################################
142 ## display the suggestion details
143 ################################################################################################
144 ################################################################################################
145     my @bookfund_loop;
146     my @audience_loop;
147     my $chooseabookfund=0;
148     my $chooseanaudience=0;
149     my $bookfundgroupname;
150     my $bookfundname;
151     my $audiencename;
152     my $itemtypedescription;
153
154     ###################################################################################
155     ## get the suggestion (from 'aq2orders')
156     ###################################################################################
157     my $suggestion;
158     my $dbh = C4::Context->dbh;
159     my $sth = $dbh->prepare("
160     SELECT *
161     FROM aq2orders
162     WHERE ordernumber=?
163     ");
164
165     $sth->execute($ordernumber);
166     my $suggestion = $sth->fetchrow_hashref;
167     my $bookfundgroupnumber=$suggestion->{'bookfundgroupnumber'};
168     my $bookfundnumber=$suggestion->{'bookfundnumber'};
169     my $branchcode=$suggestion->{'branchcode'};
170     my $itemtype=$suggestion->{'itemtype'};
171     my $audiencenumber=$suggestion->{'audiencenumber'};
172     my $step=$suggestion->{'step'};
173     $sth->finish;
174
175
176     ###################################################################################
177     ## get data from other tables
178     ###################################################################################
179
180     ## get data about book fund groups
181     ###################################################################################
182
183     my $dbh = C4::Context->dbh;
184     my $sth = $dbh->prepare("
185     SELECT bookfundgroupname
186     FROM aq2bookfundgroups
187     WHERE bookfundgroupnumber=?
188     ");
189
190     $sth->execute($bookfundgroupnumber);
191
192     $bookfundgroupname = $sth->fetchrow_hashref->{'bookfundgroupname'};
193     $sth->finish;
194
195
196     ## get data about book funds
197     ###################################################################################
198
199     if ($bookfundgroupnumber) {
200         my $dbh = C4::Context->dbh;
201         my $sth = $dbh->prepare("
202         SELECT bookfundnumber, bookfundname
203         FROM aq2bookfunds
204         WHERE bookfundgroupnumber=?
205         ORDER BY bookfundname
206         ");
207
208         $sth->execute($bookfundgroupnumber);
209
210         while (my $data = $sth->fetchrow_hashref) {
211
212             if ($data->{'bookfundnumber'} eq $bookfundnumber) {
213                 $data->{selected}=1;
214                 $bookfundname=$data->{'bookfundname'};
215             }
216             else {
217                 $data->{selected}=0;
218             }
219
220             push(@bookfund_loop, $data);
221             }
222         $sth->finish;
223     }
224
225
226     ## get data about audiences
227     ###################################################################################
228
229     if ($bookfundgroupnumber) {
230         my $dbh = C4::Context->dbh;
231         my $sth = $dbh->prepare("
232         SELECT audiencenumber, audiencename
233         FROM aq2audiences
234         WHERE bookfundgroupnumber=?
235         ORDER BY audiencename
236         ");
237
238         $sth->execute($bookfundgroupnumber);
239
240     ## get 'audiencenumber' value (in the table 'aq2orders')
241
242         while (my $data = $sth->fetchrow_hashref) {
243             if ($data->{'audiencenumber'} eq $audiencenumber) {
244                 $data->{selected}=1;
245                 $audiencename = $data->{'audiencename'};
246             }
247             else {$data->{selected}=0;}
248
249             push(@audience_loop, $data);
250             }
251         $sth->finish;
252     }
253
254
255     ## get branch names
256     ###################################################################################
257     my $branches = GetBranches;
258     my @branch_loop;
259     my $branchname;
260
261     foreach my $thisbranch (keys %$branches) {
262         my %row =   (branchcode => $thisbranch,
263                     branchname => $branches->{$thisbranch}->{'branchname'},
264         );
265         if ($thisbranch eq $branchcode) {
266             $row{"selected"}=1;
267             $branchname=$branches->{$thisbranch}->{'branchname'};
268         }
269         else {$row{"selected"}=0;}
270
271         push @branch_loop, \%row;
272     }
273
274
275     ## get item types
276     ###################################################################################
277     my $dbh = C4::Context->dbh;
278     my $sth=$dbh->prepare("
279     SELECT description,itemtype
280     FROM itemtypes
281     ORDER BY description");
282     $sth->execute();
283
284     my @itemtypeloop;
285
286     while ( my $data = $sth->fetchrow_hashref ) {
287         if ($data->{'itemtype'} eq $itemtype) {
288             $data->{selected}=1;
289             $itemtypedescription=$data->{'description'};
290         }
291         else { $data->{selected}=0;}
292         push( @itemtypeloop, $data);
293     }
294     $sth->finish;
295
296
297     ### get names
298     ###################################################################################
299
300     ## name of the person who 1st wrote the suggestion
301     my $dbh = C4::Context->dbh;
302     my $sth = $dbh->prepare("
303     SELECT  firstname AS firstnamesuggestedby,
304             surname AS surnamesuggestedby
305     FROM borrowers
306     WHERE borrowernumber=?
307     ");
308     $sth->execute($suggestion->{'suggestedby'});
309     my $namesuggestedby = $sth->fetchrow_hashref;
310     $sth->finish;
311
312     ## name of the person who managed the suggestion in the General Purchase Department
313
314     my $dbh = C4::Context->dbh;
315     my $sth = $dbh->prepare("
316     SELECT  firstname AS firstnamemanagedingpdby,
317             surname AS surnamemanagedingpdby
318     FROM borrowers
319     WHERE borrowernumber=?
320     ");
321     $sth->execute($suggestion->{'suggestionmanagedingpdby'});
322     my $namemanagedingpdby = $sth->fetchrow_hashref;
323     $sth->finish;
324
325
326     ## name of the person who managed the suggestion in a Purchase Department
327     my $dbh = C4::Context->dbh;
328     my $sth = $dbh->prepare("
329     SELECT  firstname AS firstnamemanagedinpdby,
330             surname AS surnamemanagedinpdby
331     FROM borrowers
332     WHERE borrowernumber=?
333     ");
334     $sth->execute($suggestion->{'suggestionmanagedinpdby'});
335     my $namemanagedinpdby = $sth->fetchrow_hashref;
336     $sth->finish;
337
338     if ($chooseabookfund) {
339         $template->param(chooseabookfund => $chooseabookfund,);
340         warn "valeur de chooseabookfund = $chooseabookfund";
341     }
342
343     if ($chooseanaudience) {
344         $template->param(chooseanaudience => $chooseanaudience,);
345         warn "valeur de chooseanaudience = $chooseanaudience";
346     }
347
348
349     ## list of reasons why a suggestion can be rejected
350     ###################################################################################
351
352     my $reasons_loop = GetAuthorisedValues("SUGGEST");
353
354
355 ################################################################################################
356 ################################################################################################
357 ### $template
358 ################################################################################################
359 ################################################################################################
360
361     $template->param(
362
363         ##argument en entrĂ©e du script
364
365         ordernumber     => $ordernumber,
366
367         ## other variables from 'aq2orders'
368         ########################################################################################
369
370         title           => $suggestion->{'title'},
371         author          => $suggestion->{'author'},
372         copyrightdate   => $suggestion->{'copyrightdate'},
373         isbn            => $suggestion->{'isbn'},
374         rrp             => $suggestion->{'rrp'},
375         publishercode   => $suggestion->{'publishercode'},
376         seriestitle     => $suggestion->{'seriestitle'},
377         note            => $suggestion->{'note'},
378         reason          => $suggestion->{'reason'},
379         reasons_loop            => $reasons_loop,
380         bookfundgroupnumber     => $bookfundgroupnumber,
381         bookfundgroup_loop      => $bookfundgroupnumber,
382
383         ## dates 'metric format'
384         suggestioncreatedon  => format_date($suggestion->{'suggestioncreatedon'}),
385         suggestionmanagedingpdon  => format_date($suggestion->{'suggestionmanagedingpdon'}),
386         suggestionmanagedinpdon  => format_date($suggestion->{'suggestionmanagedinpdon'}),
387
388         ## suggestions'status
389         status          => $suggestion->{'status'},
390         step2           => ($step==2)?1:0,
391         step3           => ($step==3)?1:0,
392         step3ormore        => ($step>=3)?1:0,
393         rejected3       => (($suggestion->{'status'} eq "REJECTED") and ($step==3))?1:0,
394
395
396         ## variables from other tables
397         ########################################################################################
398
399         ## loop variables
400         bookfund_loop    => \@bookfund_loop,
401         audience_loop    => \@audience_loop,
402         branch_loop      => \@branch_loop,
403         itemtypeloop    => \@itemtypeloop,
404
405         ## names (person who 1st write the suggestion, persons who managed the suggestion)
406         firstnamesuggestedby   => $namesuggestedby->{'firstnamesuggestedby'},
407         surnamesuggestedby     => $namesuggestedby->{'surnamesuggestedby'},
408
409         firstnamemanagedingpdby =>$namemanagedingpdby->{'firstnamemanagedingpdby'},
410         surnamemanagedingpdby =>$namemanagedingpdby->{'surnamemanagedingpdby'},
411
412         firstnamemanagedinpdby =>$namemanagedinpdby->{'firstnamemanagedinpdby'},
413         surnamemanagedinpdby =>$namemanagedinpdby->{'surnamemanagedinpdby'},
414
415         ## other variables
416         branchname      => $branchname,
417         bookfundgroupname => $bookfundgroupname,
418         bookfundname    => $bookfundname,
419         audiencename    => $audiencename,
420         itemtypedescription => $itemtypedescription,
421
422         template        => C4::Context->preference('template'),
423     );
424
425 }
426
427
428 output_html_with_http_headers $input, $cookie, $template->output;