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