fix for #471
[koha.git] / admin / aqbookfund.pl
1 #!/usr/bin/perl
2
3 #script to administer the aqbudget table
4 #written 20/02/2002 by paul.poulain@free.fr
5 # This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
6
7 # ALGO :
8 # this script use an $op to know what to do.
9 # if $op is empty or none of the above values,
10 #       - the default screen is build (with all records, or filtered datas).
11 #       - the   user can clic on add, modify or delete record.
12 # if $op=add_form
13 #       - if primkey exists, this is a modification,so we read the $primkey record
14 #       - builds the add/modify form
15 # if $op=add_validate
16 #       - the user has just send datas, so we create/modify the record
17 # if $op=delete_form
18 #       - we show the record having primkey=$primkey and ask for deletion validation form
19 # if $op=delete_confirm
20 #       - we delete the record having primkey=$primkey
21
22
23 # Copyright 2000-2002 Katipo Communications
24 #
25 # This file is part of Koha.
26 #
27 # Koha is free software; you can redistribute it and/or modify it under the
28 # terms of the GNU General Public License as published by the Free Software
29 # Foundation; either version 2 of the License, or (at your option) any later
30 # version.
31 #
32 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
33 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
34 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
35 #
36 # You should have received a copy of the GNU General Public License along with
37 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
38 # Suite 330, Boston, MA  02111-1307 USA
39
40 use strict;
41 use CGI;
42 use C4::Auth;
43 use C4::Context;
44 use C4::Output;
45 use C4::Interface::CGI::Output;
46 use C4::Search;
47 use HTML::Template;
48
49 sub StringSearch  {
50         my ($env,$searchstring,$type)=@_;
51         my $dbh = C4::Context->dbh;
52         $searchstring=~ s/\'/\\\'/g;
53         my @data=split(' ',$searchstring);
54         my $count=@data;
55         my $query="select bookfundid,bookfundname,bookfundgroup from aqbookfund where (bookfundname like \"%$data[0]%\") order by bookfundid";
56         my $sth=$dbh->prepare($query);
57         $sth->execute;
58         my @results;
59         my $cnt=0;
60         while (my $data=$sth->fetchrow_hashref){
61                 push(@results,$data);
62                 $cnt ++;
63         }
64         #  $sth->execute;
65         $sth->finish;
66         return ($cnt,\@results);
67 }
68
69 my $input = new CGI;
70 my $searchfield=$input->param('searchfield');
71 my $offset=$input->param('offset');
72 my $script_name="/cgi-bin/koha/admin/aqbookfund.pl";
73 my $bookfundid=$input->param('bookfundid');
74 my $pagesize=20;
75 my $op = $input->param('op');
76 $searchfield=~ s/\,//g;
77
78 my ($template, $borrowernumber, $cookie)
79     = get_template_and_user({template_name => "parameters/aqbookfund.tmpl",
80                              query => $input,
81                              type => "intranet",
82                              authnotrequired => 0,
83                              flagsrequired => {parameters => 1},
84                              debug => 1,
85                              });
86
87 if ($op) {
88 $template->param(script_name => $script_name,
89                                                 $op              => 1); # we show only the TMPL_VAR names $op
90 } else {
91 $template->param(script_name => $script_name,
92                                                 else              => 1); # we show only the TMPL_VAR names $op
93 }
94 $template->param(action => $script_name);
95
96
97 ################## ADD_FORM ##################################
98 # called by default. Used to create form to add or  modify a record
99 if ($op eq 'add_form') {
100         #---- if primkey exists, it's a modify action, so read values to modify...
101         my $data;
102         my $header;
103         if ($bookfundid) {
104                 my $dbh = C4::Context->dbh;
105                 my $sth=$dbh->prepare("select bookfundid,bookfundname,bookfundgroup from aqbookfund where bookfundid='$bookfundid'");
106                 $sth->execute;
107                 $data=$sth->fetchrow_hashref;
108                 $sth->finish;
109             }
110         if ($bookfundid) {
111             $header = "Modify book fund";
112         } else {
113             $header = "Add book fund";
114         }
115         $template->param(header => $header);
116         my $add_or_modify=0;
117         if ($bookfundid) {
118             $add_or_modify=1;
119         }
120         $template->param(add_or_modify => $add_or_modify);
121         $template->param(bookfundid =>$bookfundid);
122         $template->param(bookfundname =>$data->{'bookfundname'});
123
124                                                                                                         # END $OP eq ADD_FORM
125 ################## ADD_VALIDATE ##################################
126 # called by add_form, used to insert/modify data in DB
127 } elsif ($op eq 'add_validate') {
128         my $dbh = C4::Context->dbh;
129         my $bookfundid=uc($input->param('bookfundid'));
130         my $query = "delete from aqbookfund where bookfundid ='$bookfundid'";
131         my $sth=$dbh->prepare($query);
132         $sth->execute;
133         $sth->finish;
134         $query = "replace aqbookfund (bookfundid,bookfundname) values (";
135         $query.= $dbh->quote($input->param('bookfundid')).",";
136         $query.= $dbh->quote($input->param('bookfundname')).")";
137         my $sth=$dbh->prepare($query);
138         $sth->execute;
139         $sth->finish;
140                                                                                                         # END $OP eq ADD_VALIDATE
141 ################## DELETE_CONFIRM ##################################
142 # called by default form, used to confirm deletion of data in DB
143 } elsif ($op eq 'delete_confirm') {
144         my $dbh = C4::Context->dbh;
145 #       my $sth=$dbh->prepare("select count(*) as total from categoryitem where itemtype='$itemtype'");
146 #       $sth->execute;
147 #       my $total = $sth->fetchrow_hashref;
148 #       $sth->finish;
149         my $sth=$dbh->prepare("select bookfundid,bookfundname,bookfundgroup from aqbookfund where bookfundid='$bookfundid'");
150         $sth->execute;
151         my $data=$sth->fetchrow_hashref;
152         $sth->finish;
153         $template->param(bookfundid => $bookfundid);
154         $template->param(bookfundname => $data->{'bookfundname'});
155                                                                                                         # END $OP eq DELETE_CONFIRM
156 ################## DELETE_CONFIRMED ##################################
157 # called by delete_confirm, used to effectively confirm deletion of data in DB
158 } elsif ($op eq 'delete_confirmed') {
159         my $dbh = C4::Context->dbh;
160         my $bookfundid=uc($input->param('bookfundid'));
161         my $query = "delete from aqbookfund where bookfundid='$bookfundid'";
162         my $sth=$dbh->prepare($query);
163         $sth->execute;
164         $dbh->do("delete from aqbudget where bookfundid='$bookfundid'");
165         $sth->finish;
166                                                                                                         # END $OP eq DELETE_CONFIRMED
167 ################## DEFAULT ##################################
168 } else { # DEFAULT
169         $template->param(scriptname => $script_name);
170         if  ($searchfield ne '') {
171                 $template->param(search => 1);
172                 $template->param(searchfield => $searchfield);
173         }
174         my $env;
175         my ($count,$results)=StringSearch($env,$searchfield,'web');
176         my $toggle="white";
177         my @loop_data =();
178         my $dbh = C4::Context->dbh;
179         my $sth2 = $dbh->prepare("Select aqbudgetid,startdate,enddate,budgetamount from aqbudget where bookfundid = ? order by bookfundid");
180         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
181                 my %row_data;
182                 $row_data{bookfundid} =$results->[$i]{'bookfundid'};
183                 $row_data{bookfundname} = $results->[$i]{'bookfundname'};
184                 $sth2->execute($row_data{bookfundid});
185                 my @budget_loop;
186                 while (my ($aqbudgetid,$startdate,$enddate,$budgetamount) = $sth2->fetchrow) {
187                         my %budgetrow_data;
188                         $budgetrow_data{aqbudgetid} = $aqbudgetid;
189                         $budgetrow_data{startdate} = $startdate;
190                         $budgetrow_data{enddate} = $enddate;
191                         $budgetrow_data{budgetamount} = $budgetamount;
192                         push @budget_loop,\%budgetrow_data;
193                 }
194                 $row_data{budget} = \@budget_loop;
195                 push @loop_data,\%row_data;
196         }
197         $template->param(bookfund => \@loop_data);
198 } #---- END $OP eq DEFAULT
199
200 output_html_with_http_headers $input, $cookie, $template->output;