synch'ing 2.2 and head
[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 C4::Date;
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 bookfundid,bookfundname,bookfundgroup from aqbookfund where (bookfundname like ?) order by bookfundid");
57         $sth->execute("%$data[0]%");
58         my @results;
59         while (my $data=$sth->fetchrow_hashref){
60                 push(@results,$data);
61         }
62         #  $sth->execute;
63         $sth->finish;
64         return (scalar(@results),\@results);
65 }
66
67 my $input = new CGI;
68 my $searchfield=$input->param('searchfield');
69 my $offset=$input->param('offset');
70 my $script_name="/cgi-bin/koha/admin/aqbookfund.pl";
71 my $bookfundid=$input->param('bookfundid');
72 my $pagesize=20;
73 my $op = $input->param('op');
74 $searchfield=~ s/\,//g;
75
76 my ($template, $borrowernumber, $cookie)
77     = get_template_and_user({template_name => "parameters/aqbookfund.tmpl",
78                              query => $input,
79                              type => "intranet",
80                              authnotrequired => 0,
81                              flagsrequired => {parameters => 1, management => 1},
82                              debug => 1,
83                              });
84
85 if ($op) {
86 $template->param(script_name => $script_name,
87                 $op              => 1); # we show only the TMPL_VAR names $op
88 } else {
89 $template->param(script_name => $script_name,
90                 else              => 1); # we show only the TMPL_VAR names $op
91 }
92 $template->param(action => $script_name);
93
94
95 ################## ADD_FORM ##################################
96 # called by default. Used to create form to add or  modify a record
97 if ($op eq 'add_form') {
98         #---- if primkey exists, it's a modify action, so read values to modify...
99         my $data;
100         my $header;
101         if ($bookfundid) {
102                 my $dbh = C4::Context->dbh;
103                 my $sth=$dbh->prepare("select bookfundid,bookfundname,bookfundgroup from aqbookfund where bookfundid=?");
104                 $sth->execute($bookfundid);
105                 $data=$sth->fetchrow_hashref;
106                 $sth->finish;
107             }
108         if ($bookfundid) {
109             $header = "Modify book fund";
110             $template->param('header-is-modify-p' => 1);
111         } else {
112             $header = "Add book fund";
113             $template->param('header-is-add-p' => 1);
114         }
115         $template->param('use-header-flags-p' => 1);
116         $template->param(header => $header); # NOTE deprecated
117         my $add_or_modify=0;
118         if ($bookfundid) {
119             $add_or_modify=1;
120         }
121         $template->param(add_or_modify => $add_or_modify);
122         $template->param(bookfundid =>$bookfundid);
123         $template->param(bookfundname =>$data->{'bookfundname'});
124
125                                                                                                         # END $OP eq ADD_FORM
126 ################## ADD_VALIDATE ##################################
127 # called by add_form, used to insert/modify data in DB
128 } elsif ($op eq 'add_validate') {
129         my $dbh = C4::Context->dbh;
130         my $bookfundid=uc($input->param('bookfundid'));
131         my $sth=$dbh->prepare("delete from aqbookfund where bookfundid =?");
132         $sth->execute($bookfundid);
133         $sth->finish;
134         my $sth=$dbh->prepare("replace aqbookfund (bookfundid,bookfundname) values (?,?)");
135         $sth->execute($input->param('bookfundid'),$input->param('bookfundname'));
136         $sth->finish;
137         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=aqbookfund.pl\"></html>";
138         exit;
139                         
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 bookfundid,bookfundname,bookfundgroup from aqbookfund where bookfundid=?");
146         $sth->execute($bookfundid);
147         my $data=$sth->fetchrow_hashref;
148         $sth->finish;
149         $template->param(bookfundid => $bookfundid);
150         $template->param(bookfundname => $data->{'bookfundname'});
151                                                                                                         # END $OP eq DELETE_CONFIRM
152 ################## DELETE_CONFIRMED ##################################
153 # called by delete_confirm, used to effectively confirm deletion of data in DB
154 } elsif ($op eq 'delete_confirmed') {
155         my $dbh = C4::Context->dbh;
156         my $bookfundid=uc($input->param('bookfundid'));
157         my $sth=$dbh->prepare("delete from aqbookfund where bookfundid=?");
158         $sth->execute($bookfundid);
159         $sth->finish;
160         $sth=$dbh->prepare("delete from aqbudget where bookfundid=?");
161         $sth->execute($bookfundid);
162         $sth->finish;
163                                                                                                         # END $OP eq DELETE_CONFIRMED
164 ################## DEFAULT ##################################
165 } else { # DEFAULT
166         $template->param(scriptname => $script_name);
167         if  ($searchfield ne '') {
168                 $template->param(search => 1);
169                 $template->param(searchfield => $searchfield);
170         }
171         my $env;
172         my ($count,$results)=StringSearch($env,$searchfield,'web');
173         my $toggle="white";
174         my @loop_data =();
175         my $dbh = C4::Context->dbh;
176         my $sth2 = $dbh->prepare("Select aqbudgetid,startdate,enddate,budgetamount from aqbudget where bookfundid = ? order by bookfundid");
177         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
178                 my %row_data;
179                 $row_data{bookfundid} =$results->[$i]{'bookfundid'};
180                 $row_data{bookfundname} = $results->[$i]{'bookfundname'};
181                 $sth2->execute($row_data{bookfundid});
182                 my @budget_loop;
183                 while (my ($aqbudgetid,$startdate,$enddate,$budgetamount) = $sth2->fetchrow) {
184                         my %budgetrow_data;
185                         $budgetrow_data{aqbudgetid} = $aqbudgetid;
186                         $budgetrow_data{startdate} = format_date($startdate);
187                         $budgetrow_data{enddate} = format_date($enddate);
188                         $budgetrow_data{budgetamount} = $budgetamount;
189                         push @budget_loop,\%budgetrow_data;
190                 }
191                 $row_data{budget} = \@budget_loop;
192                 push @loop_data,\%row_data;
193         }
194         $template->param(bookfund => \@loop_data);
195 } #---- END $OP eq DEFAULT
196
197 output_html_with_http_headers $input, $cookie, $template->output;