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)
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.
13 # - if primkey exists, this is a modification,so we read the $primkey record
14 # - builds the add/modify form
16 # - the user has just send datas, so we create/modify the record
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
23 # Copyright 2000-2002 Katipo Communications
25 # This file is part of Koha.
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
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.
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
46 use C4::Interface::CGI::Output;
52 my ($env,$searchstring,%branches)=@_;
53 my $dbh = C4::Context->dbh;
54 $searchstring=~ s/\'/\\\'/g;
55 my @data=split(' ',$searchstring);
57 my $strsth= "select bookfundid,bookfundname,bookfundgroup,branchcode from aqbookfund where bookfundname like ? ";
59 $strsth.= "AND (aqbookfund.branchcode is null " ;
60 foreach my $branchcode (keys %branches){
61 $strsth .= "or aqbookfund.branchcode = '".$branchcode."' ";
65 $strsth.= "order by aqbookfund.bookfundid";
66 warn "chaine de recherche : ".$strsth;
68 my $sth=$dbh->prepare($strsth);
69 $sth->execute("%$data[0]%");
71 while (my $data=$sth->fetchrow_hashref){
76 return (scalar(@results),\@results);
79 my $dbh = C4::Context->dbh;
81 my $searchfield=$input->param('searchfield');
82 my $offset=$input->param('offset');
83 my $script_name="/cgi-bin/koha/admin/aqbookfund.pl";
84 my $bookfundid=$input->param('bookfundid');
86 my $op = $input->param('op');
87 $searchfield=~ s/\,//g;
89 my ($template, $borrowernumber, $cookie)
90 = get_template_and_user({template_name => "parameters/aqbookfund.tmpl",
94 flagsrequired => {parameters => 1, management => 1},
99 $template->param(script_name => $script_name,
100 $op => 1); # we show only the TMPL_VAR names $op
102 $template->param(script_name => $script_name,
103 else => 1); # we show only the TMPL_VAR names $op
105 $template->param(action => $script_name);
110 my ($count2,@branches)=branches();
112 push @select_branch,"";
113 $select_branches{""}="";
115 my $sthtemp = $dbh->prepare("Select flags, branchcode from borrowers where borrowernumber = ?");
116 $sthtemp->execute($borrowernumber);
117 my ($flags, $homebranch)=$sthtemp->fetchrow;
120 push @select_branch, $homebranch;#
121 for (my $i=0;$i<$count2;$i++){
122 $select_branches{$branches[$i]->{'branchcode'}} = $branches[$i]->{'branchname'} if ($branches[$i]->{'branchcode'} eq $homebranch);
126 for (my $i=0;$i<$count2;$i++){
127 push @select_branch, $branches[$i]->{'branchcode'};#
128 $select_branches{$branches[$i]->{'branchcode'}} = $branches[$i]->{'branchname'};
131 my $CGIbranch=CGI::scrolling_list( -name => 'branchcode',
132 -values => \@select_branch,
133 -labels => \%select_branches,
136 $template->param(CGIbranch => $CGIbranch);
138 warn "bornum=".$borrowernumber . "flags = ".$flags. " homebranch= ".$homebranch;
141 ################## ADD_FORM ##################################
142 # called by default. Used to create form to add or modify a record
143 if ($op eq 'add_form') {
144 #---- if primkey exists, it's a modify action, so read values to modify...
148 my $sth=$dbh->prepare("select bookfundid,bookfundname,bookfundgroup from aqbookfund where bookfundid=?");
149 $sth->execute($bookfundid);
150 $data=$sth->fetchrow_hashref;
154 $header = "Modify book fund";
155 $template->param('header-is-modify-p' => 1);
157 $header = "Add book fund";
158 $template->param('header-is-add-p' => 1);
160 $template->param('use-header-flags-p' => 1);
161 $template->param(header => $header); # NOTE deprecated
166 $template->param(add_or_modify => $add_or_modify);
167 $template->param(bookfundid =>$bookfundid);
168 $template->param(bookfundname =>$data->{'bookfundname'});
170 # END $OP eq ADD_FORM
171 ################## ADD_VALIDATE ##################################
172 # called by add_form, used to insert/modify data in DB
173 } elsif ($op eq 'add_validate') {
174 my $bookfundid=uc($input->param('bookfundid'));
175 my $sth=$dbh->prepare("delete from aqbookfund where bookfundid =?");
176 $sth->execute($bookfundid);
178 my $sth=$dbh->prepare("replace aqbookfund (bookfundid,bookfundname, branchcode) values (?,?,?)");
179 $sth->execute($input->param('bookfundid'),$input->param('bookfundname'),$input->param('branchcode'));
181 print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=aqbookfund.pl\"></html>";
184 # END $OP eq ADD_VALIDATE
185 ################## DELETE_CONFIRM ##################################
186 # called by default form, used to confirm deletion of data in DB
187 } elsif ($op eq 'delete_confirm') {
188 my $sth=$dbh->prepare("select bookfundid,bookfundname,bookfundgroup from aqbookfund where bookfundid=?");
189 $sth->execute($bookfundid);
190 my $data=$sth->fetchrow_hashref;
192 $template->param(bookfundid => $bookfundid);
193 $template->param(bookfundname => $data->{'bookfundname'});
194 # END $OP eq DELETE_CONFIRM
195 ################## DELETE_CONFIRMED ##################################
196 # called by delete_confirm, used to effectively confirm deletion of data in DB
197 } elsif ($op eq 'delete_confirmed') {
198 my $bookfundid=uc($input->param('bookfundid'));
199 my $sth=$dbh->prepare("delete from aqbookfund where bookfundid=?");
200 $sth->execute($bookfundid);
202 $sth=$dbh->prepare("delete from aqbudget where bookfundid=?");
203 $sth->execute($bookfundid);
205 # END $OP eq DELETE_CONFIRMED
206 ################## DEFAULT ##################################
208 $template->param(scriptname => $script_name);
209 if ($searchfield ne '') {
210 $template->param(search => 1);
211 $template->param(searchfield => $searchfield);
214 my ($count,$results)=StringSearch($env,$searchfield,%select_branches);
217 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
219 $row_data{bookfundid} =$results->[$i]{'bookfundid'};
220 $row_data{bookfundname} = $results->[$i]{'bookfundname'};
221 warn "".$results->[$i]{'bookfundid'}." ".$results->[$i]{'bookfundname'}." ".$results->[$i]{'branchcode'};
222 $row_data{branchname} = $select_branches{$results->[$i]{'branchcode'}};
223 my $strsth2="Select aqbudgetid,startdate,enddate,budgetamount,aqbudget.branchcode from aqbudget where aqbudget.bookfundid = ?";
225 $strsth2 .= " AND ((aqbudget.branchcode='') OR (aqbudget.branchcode= ".$dbh->quote($homebranch).")) " ;
227 $strsth2 .= " AND (aqbudget.branchcode='') " if ($flags>1);
229 $strsth2 .= " order by aqbudgetid";
231 my $sth2 = $dbh->prepare($strsth2);
232 $sth2->execute($row_data{bookfundid});
234 while (my ($aqbudgetid,$startdate,$enddate,$budgetamount,$branchcode) = $sth2->fetchrow) {
236 $budgetrow_data{aqbudgetid} = $aqbudgetid;
237 $budgetrow_data{startdate} = format_date($startdate);
238 $budgetrow_data{enddate} = format_date($enddate);
239 $budgetrow_data{budgetamount} = $budgetamount;
240 $budgetrow_data{branchcode} = $branchcode;
241 push @budget_loop,\%budgetrow_data;
244 $row_data{budget} = \@budget_loop;
245 push @loop_data,\%row_data;
248 $template->param(bookfund => \@loop_data);
249 } #---- END $OP eq DEFAULT
251 output_html_with_http_headers $input, $cookie, $template->output;