templating this file and modify it so that all the budgets are on the screen
[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::Context;
43 use C4::Output;
44 use C4::Search;
45 use HTML::Template;
46
47
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,startdate,enddate,budgetamount from aqbudget where bookfundid and (bookfundid 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/aqbudget.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 = gettemplate("parameters/aqbudget.tmpl",0);
79 if ($op) {
80 $template->param(script_name => $script_name,
81                                                 $op              => 1); # we show only the TMPL_VAR names $op
82 } else {
83 $template->param(script_name => $script_name,
84                                                 else              => 1); # we show only the TMPL_VAR names $op
85 }
86
87 $template->param(action => $script_name);
88 ################## ADD_FORM ##################################
89 # called by default. Used to create form to add or  modify a record
90 if ($op eq 'add_form') {
91         #---- if primkey exists, it's a modify action, so read values to modify...
92         my $dataaqbudget;
93         my $dataaqbookfund;
94         if ($bookfundid) {
95                 my $dbh = C4::Context->dbh;
96                 my $query="select bookfundid,startdate,enddate,budgetamount from aqbudget where bookfundid='$bookfundid'";
97 #               print $query;
98                 my $sth=$dbh->prepare($query);
99                 $sth->execute;
100                 $dataaqbudget=$sth->fetchrow_hashref;
101                 $sth->finish;
102                 my $query="select bookfundid,bookfundname from aqbookfund where bookfundid='$bookfundid'";
103 #               print $query;
104                 my $sth=$dbh->prepare($query);
105                 $sth->execute;
106                 $dataaqbookfund=$sth->fetchrow_hashref;
107                 $sth->finish;
108         }
109         my $header;
110         if ($bookfundid) {
111                 $header = "Modify budget";
112         } else {
113                 $header = "Add budget";
114         }
115         $template->param(header => $header);
116         if ($bookfundid) {
117             $template->param(modify => 1);
118             $template->param(bookfundid => $bookfundid);
119             $template->param(bookfundname => $dataaqbookfund->{bookfundname});
120         } else {
121             $template->param(adding => 1);
122         }
123         $template->param(startdate => $dataaqbudget->{'startdate'});
124         $template->param(enddate => $dataaqbudget->{'enddate'});
125         $template->param(budgetamount => $dataaqbudget->{'budgetamount'});
126                                                                                                         # END $OP eq ADD_FORM
127 ################## ADD_VALIDATE ##################################
128 # called by add_form, used to insert/modify data in DB
129 } elsif ($op eq 'add_validate') {
130         my $dbh = C4::Context->dbh;
131         my $query = "replace aqbudget (bookfundid,startdate,enddate,budgetamount) values (";
132         $query.= $dbh->quote($input->param('bookfundid')).",";
133         $query.= $dbh->quote($input->param('startdate')).",";
134         $query.= $dbh->quote($input->param('enddate')).",";
135         $query.= $dbh->quote($input->param('budgetamount')).")";
136         my $sth=$dbh->prepare($query);
137         $sth->execute;
138         $sth->finish;
139                                                                                                         # END $OP eq ADD_VALIDATE
140 ################## DELETE_CONFIRM ##################################
141 # called by default form, used to confirm deletion of data in DB
142 } elsif ($op eq 'delete_confirm') {
143         my $dbh = C4::Context->dbh;
144 #       my $sth=$dbh->prepare("select count(*) as total from categoryitem where itemtype='$itemtype'");
145 #       $sth->execute;
146 #       my $total = $sth->fetchrow_hashref;
147 #       $sth->finish;
148         my $sth=$dbh->prepare("select bookfundid,startdate,enddate,budgetamount from aqbudget where bookfundid='$bookfundid'");
149         $sth->execute;
150         my $data=$sth->fetchrow_hashref;
151         $sth->finish;
152         $template->param(bookfundid => $bookfundid);
153         $template->param(startdate => $data->{'startdate'});
154         $template->param(enddate => $data->{'enddate'});
155         $template->param(budgetamount => $data->{'budgetamount'});
156                                                                                                         # END $OP eq DELETE_CONFIRM
157 ################## DELETE_CONFIRMED ##################################
158 # called by delete_confirm, used to effectively confirm deletion of data in DB
159 } elsif ($op eq 'delete_confirmed') {
160         my $dbh = C4::Context->dbh;
161         my $bookfundid=uc($input->param('bookfundid'));
162         my $query = "delete from aqbudget where bookfundid='$bookfundid'";
163         my $sth=$dbh->prepare($query);
164         $sth->execute;
165         $sth->finish;
166                                                                                                         # END $OP eq DELETE_CONFIRMED
167 ################## DEFAULT ##################################
168 } else { # DEFAULT
169         if  ($searchfield ne '') {
170                 $template->param(search => 1);
171                 $template->param(searchfield => $searchfield);
172         }
173         my $env;
174         my ($count,$results)=StringSearch($env,$searchfield,'web');
175         my $toggle="white";
176         my @loop_data =();
177         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
178                 #find out stats
179         #       my ($od,$issue,$fines)=categdata2($env,$results->[$i]{'borrowernumber'});
180         #       $fines=$fines+0;
181                 my $dataaqbookfund;
182                 my $dbh = C4::Context->dbh;
183                 my $query="select bookfundid,bookfundname from aqbookfund where bookfundid=$results->[$i]{'bookfundid'}";
184 #               print $query;
185                 my $sth=$dbh->prepare($query);
186                 $sth->execute;
187                 $dataaqbookfund=$sth->fetchrow_hashref;
188                 $sth->finish;
189                 my @toggle = ();
190                 my @bookfundid = ();
191                 my @bookfundname = ();
192                 my @startdate = ();
193                 my @enddate = ();
194                 my @budgetamount = ();
195                 push(@toggle,$toggle);
196                 push(@bookfundid,$results->[$i]{'bookfundid'});
197                 push(@bookfundname,$dataaqbookfund->{'bookfundname'});
198                 push(@startdate,$results->[$i]{'startdate'});
199                 push(@enddate,$results->[$i]{'enddate'});
200                 push(@budgetamount,$results->[$i]{'budgetamount'});
201                 if ($toggle eq 'white'){
202                         $toggle="#ffffcc";
203                 } else {
204                         $toggle="white";
205                 }
206                 while (@toggle and @bookfundid and @bookfundname and @startdate and @enddate and @budgetamount) { 
207            my %row_data;
208            $row_data{toggle} = shift @toggle;
209            $row_data{bookfundid} = shift @bookfundid;
210            $row_data{bookfundname} = shift @bookfundname;
211            $row_data{startdate} = shift @startdate;
212            $row_data{enddate} = shift @enddate;
213            $row_data{budgetamount} = shift @budgetamount;
214            push(@loop_data, \%row_data);
215        }
216        }
217        $template->param(budget => \@loop_data);
218 } #---- END $OP eq DEFAULT
219
220 print "Content-Type: text/html\n\n", $template->output;