improving systempreferences to show lists, radio button where applicable (was partial...
[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::Date;
43 use C4::Auth;
44 use C4::Context;
45 use C4::Output;
46 use C4::Interface::CGI::Output;
47 use C4::Search;
48 use HTML::Template;
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 $sth=$dbh->prepare("Select aqbudgetid,bookfundid,startdate,enddate,budgetamount from aqbudget where (bookfundid like ?) order by bookfundid,aqbudgetid");
57         $sth->execute("$data[0]%");
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 $aqbudgetid=$input->param('aqbudgetid');
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 ($aqbudgetid) {
104                 my $dbh = C4::Context->dbh;
105                 my $sth=$dbh->prepare("select aqbudgetid,bookfundname,aqbookfund.bookfundid,startdate,enddate,budgetamount from aqbudget,aqbookfund where aqbudgetid=? and aqbudget.bookfundid=aqbookfund.bookfundid");
106                 $sth->execute($aqbudgetid);
107                 $dataaqbudget=$sth->fetchrow_hashref;
108                 $sth->finish;
109         }
110         my $header;
111         if ($aqbudgetid) {
112                 $header = "Modify budget";
113         } else {
114                 $header = "Add budget";
115         }
116         $template->param(header => $header);
117         if ($aqbudgetid) {
118             $template->param(modify => 1);
119             $template->param(bookfundid => $dataaqbudget->{bookfundid});
120             $template->param(bookfundname => $dataaqbudget->{bookfundname});
121         } else {
122             $template->param(bookfundid => $bookfundid,
123                                                                 adding => 1);
124         }
125         $template->param(dateformat => display_date_format(),
126                                                         aqbudgetid => $dataaqbudget->{'aqbudgetid'},
127                                                         startdate => format_date($dataaqbudget->{'startdate'}),
128                                                         enddate => format_date($dataaqbudget->{'enddate'}),
129                                                         budgetamount => $dataaqbudget->{'budgetamount'}
130         );
131                                                                                                         # END $OP eq ADD_FORM
132 ################## ADD_VALIDATE ##################################
133 # called by add_form, used to insert/modify data in DB
134 } elsif ($op eq 'add_validate') {
135         my $dbh = C4::Context->dbh;
136         my $sth=$dbh->prepare("replace aqbudget (aqbudgetid,bookfundid,startdate,enddate,budgetamount) values (?,?,?,?,?)");
137         $sth->execute($input->param('aqbudgetid'),$input->param('bookfundid'),
138                                                 format_date_in_iso($input->param('startdate')),
139                                                 format_date_in_iso($input->param('enddate')),
140                                                 $input->param('budgetamount')
141                                                 );
142         $sth->finish;
143          print $input->redirect("aqbookfund.pl");
144          exit;
145 # END $OP eq ADD_VALIDATE
146 ################## DELETE_CONFIRM ##################################
147 # called by default form, used to confirm deletion of data in DB
148 } elsif ($op eq 'delete_confirm') {
149         my $dbh = C4::Context->dbh;
150         my $sth=$dbh->prepare("select aqbudgetid,bookfundid,startdate,enddate,budgetamount from aqbudget where aqbudgetid=?");
151         $sth->execute($aqbudgetid);
152         my $data=$sth->fetchrow_hashref;
153         $sth->finish;
154         $template->param(bookfundid => $bookfundid);
155         $template->param(aqbudgetid => $data->{'aqbudgetid'});
156         $template->param(startdate => format_date($data->{'startdate'}));
157         $template->param(enddate => format_date($data->{'enddate'}));
158         $template->param(budgetamount => $data->{'budgetamount'});
159                                                                                                         # END $OP eq DELETE_CONFIRM
160 ################## DELETE_CONFIRMED ##################################
161 # called by delete_confirm, used to effectively confirm deletion of data in DB
162 } elsif ($op eq 'delete_confirmed') {
163         my $dbh = C4::Context->dbh;
164         my $aqbudgetid=uc($input->param('aqbudgetid'));
165         my $sth=$dbh->prepare("delete from aqbudget where aqbudgetid=?");
166         $sth->execute($aqbudgetid);
167         $sth->finish;
168          print $input->redirect("aqbookfund.pl");
169          return;
170                                                                                                         # END $OP eq DELETE_CONFIRMED
171 ################## DEFAULT ##################################
172 } else { # DEFAULT
173         if  ($searchfield ne '') {
174                 $template->param(search => 1);
175                 $template->param(searchfield => $searchfield);
176         }
177         my $env;
178         my ($count,$results)=StringSearch($env,$searchfield,'web');
179         my $toggle="white";
180         my @loop_data =();
181         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
182                 #find out stats
183         #       my ($od,$issue,$fines)=categdata2($env,$results->[$i]{'borrowernumber'});
184         #       $fines=$fines+0;
185                 my $dataaqbookfund;
186                 my $dbh = C4::Context->dbh;
187                 my $sth=$dbh->prepare("select bookfundid,bookfundname from aqbookfund where bookfundid=?");
188                 $sth->execute($results->[$i]{'bookfundid'});
189                 $dataaqbookfund=$sth->fetchrow_hashref;
190                 $sth->finish;
191                 my @toggle = ();
192                 my @bookfundid = ();
193                 my @bookfundname = ();
194                 my @startdate = ();
195                 my @enddate = ();
196                 my @budgetamount = ();
197                 push(@toggle,$toggle);
198                 push(@bookfundid,$results->[$i]{'bookfundid'});
199                 push(@bookfundname,$dataaqbookfund->{'bookfundname'});
200                 push(@startdate,format_date($results->[$i]{'startdate'}));
201                 push(@enddate,format_date($results->[$i]{'enddate'}));
202                 push(@budgetamount,$results->[$i]{'budgetamount'});
203                 if ($toggle eq 'white'){
204                         $toggle="#ffffcc";
205                 } else {
206                         $toggle="white";
207                 }
208                 while (@toggle and @bookfundid and @bookfundname and @startdate and @enddate and @budgetamount) { 
209            my %row_data;
210            $row_data{toggle} = shift @toggle;
211            $row_data{bookfundid} = shift @bookfundid;
212            $row_data{bookfundname} = shift @bookfundname;
213            $row_data{startdate} = shift @startdate;
214            $row_data{enddate} = shift @enddate;
215            $row_data{budgetamount} = shift @budgetamount;
216            push(@loop_data, \%row_data);
217        }
218        }
219        $template->param(budget => \@loop_data);
220 } #---- END $OP eq DEFAULT
221
222 output_html_with_http_headers $input, $cookie, $template->output;
223