small bugfix (execute with ?)
[koha.git] / admin / aqbudget.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
50
51 sub StringSearch  {
52         my ($env,$searchstring,$type)=@_;
53         my $dbh = C4::Context->dbh;
54         $searchstring=~ s/\'/\\\'/g;
55         my @data=split(' ',$searchstring);
56         my $count=@data;
57         my $query="Select bookfundid,startdate,enddate,budgetamount from aqbudget where (bookfundid like \"$data[0]%\") order by bookfundid";
58         my $sth=$dbh->prepare($query);
59         $sth->execute;
60         my @results;
61         my $cnt=0;
62         while (my $data=$sth->fetchrow_hashref){
63         push(@results,$data);
64         $cnt ++;
65         }
66         #  $sth->execute;
67         $sth->finish;
68         return ($cnt,\@results);
69 }
70
71 my $input = new CGI;
72 my $searchfield=$input->param('searchfield');
73 my $offset=$input->param('offset');
74 my $script_name="/cgi-bin/koha/admin/aqbudget.pl";
75 my $bookfundid=$input->param('bookfundid');
76 my $pagesize=20;
77 my $op = $input->param('op');
78 $searchfield=~ s/\,//g;
79
80 my ($template, $borrowernumber, $cookie)
81     = get_template_and_user({template_name => "parameters/aqbudget.tmpl",
82                              query => $input,
83                              type => "intranet",
84                              authnotrequired => 0,
85                              flagsrequired => {parameters => 1},
86                              debug => 1,
87                              });
88
89 if ($op) {
90 $template->param(script_name => $script_name,
91                                                 $op              => 1); # we show only the TMPL_VAR names $op
92 } else {
93 $template->param(script_name => $script_name,
94                                                 else              => 1); # we show only the TMPL_VAR names $op
95 }
96
97 $template->param(action => $script_name);
98 ################## ADD_FORM ##################################
99 # called by default. Used to create form to add or  modify a record
100 if ($op eq 'add_form') {
101         #---- if primkey exists, it's a modify action, so read values to modify...
102         my $dataaqbudget;
103         my $dataaqbookfund;
104         if ($bookfundid) {
105                 my $dbh = C4::Context->dbh;
106                 my $query="select bookfundid,startdate,enddate,budgetamount from aqbudget where bookfundid='$bookfundid'";
107 #               print $query;
108                 my $sth=$dbh->prepare($query);
109                 $sth->execute;
110                 $dataaqbudget=$sth->fetchrow_hashref;
111                 $sth->finish;
112                 my $query="select bookfundid,bookfundname from aqbookfund where bookfundid='$bookfundid'";
113 #               print $query;
114                 my $sth=$dbh->prepare($query);
115                 $sth->execute;
116                 $dataaqbookfund=$sth->fetchrow_hashref;
117                 $sth->finish;
118         }
119         my $header;
120         if ($bookfundid) {
121                 $header = "Modify budget";
122         } else {
123                 $header = "Add budget";
124         }
125         $template->param(header => $header);
126         if ($bookfundid) {
127             $template->param(modify => 1);
128             $template->param(bookfundid => $bookfundid);
129             $template->param(bookfundname => $dataaqbookfund->{bookfundname});
130         } else {
131             $template->param(adding => 1);
132         }
133         $template->param(startdate => $dataaqbudget->{'startdate'});
134         $template->param(enddate => $dataaqbudget->{'enddate'});
135         $template->param(budgetamount => $dataaqbudget->{'budgetamount'});
136                                                                                                         # END $OP eq ADD_FORM
137 ################## ADD_VALIDATE ##################################
138 # called by add_form, used to insert/modify data in DB
139 } elsif ($op eq 'add_validate') {
140         my $dbh = C4::Context->dbh;
141         my $query = "replace aqbudget (bookfundid,startdate,enddate,budgetamount) values (";
142         $query.= $dbh->quote($input->param('bookfundid')).",";
143         $query.= $dbh->quote($input->param('startdate')).",";
144         $query.= $dbh->quote($input->param('enddate')).",";
145         $query.= $dbh->quote($input->param('budgetamount')).")";
146         my $sth=$dbh->prepare($query);
147         $sth->execute;
148         $sth->finish;
149                                                                                                         # END $OP eq ADD_VALIDATE
150 ################## DELETE_CONFIRM ##################################
151 # called by default form, used to confirm deletion of data in DB
152 } elsif ($op eq 'delete_confirm') {
153         my $dbh = C4::Context->dbh;
154 #       my $sth=$dbh->prepare("select count(*) as total from categoryitem where itemtype='$itemtype'");
155 #       $sth->execute;
156 #       my $total = $sth->fetchrow_hashref;
157 #       $sth->finish;
158         my $sth=$dbh->prepare("select bookfundid,startdate,enddate,budgetamount from aqbudget where bookfundid='$bookfundid'");
159         $sth->execute;
160         my $data=$sth->fetchrow_hashref;
161         $sth->finish;
162         $template->param(bookfundid => $bookfundid);
163         $template->param(startdate => $data->{'startdate'});
164         $template->param(enddate => $data->{'enddate'});
165         $template->param(budgetamount => $data->{'budgetamount'});
166                                                                                                         # END $OP eq DELETE_CONFIRM
167 ################## DELETE_CONFIRMED ##################################
168 # called by delete_confirm, used to effectively confirm deletion of data in DB
169 } elsif ($op eq 'delete_confirmed') {
170         my $dbh = C4::Context->dbh;
171         my $bookfundid=uc($input->param('bookfundid'));
172         my $query = "delete from aqbudget where bookfundid='$bookfundid'";
173         my $sth=$dbh->prepare($query);
174         $sth->execute;
175         $sth->finish;
176                                                                                                         # END $OP eq DELETE_CONFIRMED
177 ################## DEFAULT ##################################
178 } else { # DEFAULT
179         if  ($searchfield ne '') {
180                 $template->param(search => 1);
181                 $template->param(searchfield => $searchfield);
182         }
183         my $env;
184         my ($count,$results)=StringSearch($env,$searchfield,'web');
185         my $toggle="white";
186         my @loop_data =();
187         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
188                 #find out stats
189         #       my ($od,$issue,$fines)=categdata2($env,$results->[$i]{'borrowernumber'});
190         #       $fines=$fines+0;
191                 my $dataaqbookfund;
192                 my $dbh = C4::Context->dbh;
193                 my $query="select bookfundid,bookfundname from aqbookfund where bookfundid=?";
194 #               print $query;
195                 my $sth=$dbh->prepare($query);
196                 $sth->execute($results->[$i]{'bookfundid'});
197                 $dataaqbookfund=$sth->fetchrow_hashref;
198                 $sth->finish;
199                 my @toggle = ();
200                 my @bookfundid = ();
201                 my @bookfundname = ();
202                 my @startdate = ();
203                 my @enddate = ();
204                 my @budgetamount = ();
205                 push(@toggle,$toggle);
206                 push(@bookfundid,$results->[$i]{'bookfundid'});
207                 push(@bookfundname,$dataaqbookfund->{'bookfundname'});
208                 push(@startdate,$results->[$i]{'startdate'});
209                 push(@enddate,$results->[$i]{'enddate'});
210                 push(@budgetamount,$results->[$i]{'budgetamount'});
211                 if ($toggle eq 'white'){
212                         $toggle="#ffffcc";
213                 } else {
214                         $toggle="white";
215                 }
216                 while (@toggle and @bookfundid and @bookfundname and @startdate and @enddate and @budgetamount) { 
217            my %row_data;
218            $row_data{toggle} = shift @toggle;
219            $row_data{bookfundid} = shift @bookfundid;
220            $row_data{bookfundname} = shift @bookfundname;
221            $row_data{startdate} = shift @startdate;
222            $row_data{enddate} = shift @enddate;
223            $row_data{budgetamount} = shift @budgetamount;
224            push(@loop_data, \%row_data);
225        }
226        }
227        $template->param(budget => \@loop_data);
228 } #---- END $OP eq DEFAULT
229
230 output_html_with_http_headers $input, $cookie, $template->output;
231