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