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) if ($searchstring ne "");
57 my $strsth= "select bookfundid,bookfundname,bookfundgroup,branchcode from aqbookfund where 1 ";
58 $strsth.=" AND bookfundname like ? " if ($searchstring ne "");
60 $strsth.= "AND (aqbookfund.branchcode is null " ;
61 foreach my $branchcode (keys %branches){
62 $strsth .= "or aqbookfund.branchcode = '".$branchcode."' ";
66 $strsth.= "order by aqbookfund.bookfundid";
67 # warn "chaine de recherche : ".$strsth;
69 my $sth=$dbh->prepare($strsth);
71 $sth->execute("%$data[0]%");
76 while (my $data=$sth->fetchrow_hashref){
78 # warn "id ".$data->{bookfundid}." name ".$data->{bookfundname}." branchcode ".$data->{branchcode};
82 return (scalar(@results),\@results);
86 my $searchfield=$input->param('searchfield');
87 my $offset=$input->param('offset');
88 my $script_name="/cgi-bin/koha/admin/aqbookfund.pl";
89 my $bookfundid=$input->param('bookfundid');
91 my $op = $input->param('op');
92 $searchfield=~ s/\,//g;
94 my ($template, $borrowernumber, $cookie)
95 = get_template_and_user({template_name => "admin/aqbookfund.tmpl",
99 flagsrequired => {parameters => 1, management => 1},
104 $template->param(script_name => $script_name,
105 $op => 1); # we show only the TMPL_VAR names $op
107 $template->param(script_name => $script_name,
108 else => 1); # we show only the TMPL_VAR names $op
110 $template->param(action => $script_name);
115 my ($branches)=GetBranches();
117 push @select_branch,"";
118 $select_branches{""}="";
120 my $homebranch=C4::Context->userenv->{branch};
121 foreach my $brnch (keys %$branches){
122 push @select_branch, $branches->{$brnch}->{'branchcode'};#
123 $select_branches{$branches->{$brnch}->{'branchcode'}} = $branches->{$brnch}->{'branchname'};
126 my $CGIbranch=CGI::scrolling_list( -name => 'branchcode',
127 -values => \@select_branch,
128 -labels => \%select_branches,
131 $template->param(CGIbranch => $CGIbranch);
133 ################## ADD_FORM ##################################
134 # called by default. Used to create form to add or modify a record
135 if ($op eq 'add_form') {
136 #---- if primkey exists, it's a modify action, so read values to modify...
140 my $dbh = C4::Context->dbh;
141 my $sth=$dbh->prepare("select bookfundid,bookfundname,bookfundgroup from aqbookfund where bookfundid=?");
142 $sth->execute($bookfundid);
143 $data=$sth->fetchrow_hashref;
147 $header = "Modify book fund";
148 $template->param('header-is-modify-p' => 1);
150 $header = "Add book fund";
151 $template->param('header-is-add-p' => 1);
153 $template->param('use-header-flags-p' => 1);
154 $template->param(header => $header); # NOTE deprecated
159 $template->param(add_or_modify => $add_or_modify);
160 $template->param(bookfundid =>$bookfundid);
161 $template->param(bookfundname =>$data->{'bookfundname'});
163 # END $OP eq ADD_FORM
164 ################## ADD_VALIDATE ##################################
165 # called by add_form, used to insert/modify data in DB
166 } elsif ($op eq 'add_validate') {
167 my $dbh = C4::Context->dbh;
168 my $bookfundid=uc($input->param('bookfundid'));
169 my $sth=$dbh->prepare("delete from aqbookfund where bookfundid =?");
170 $sth->execute($bookfundid);
172 if ($input->param('branchcode') ne ""){
173 my $sth=$dbh->prepare("replace aqbookfund (bookfundid,bookfundname,branchcode) values (?,?,?)");
174 $sth->execute($input->param('bookfundid'),$input->param('bookfundname'),$input->param('branchcode'));
177 my $sth=$dbh->prepare("replace aqbookfund (bookfundid,bookfundname) values (?,?)");
178 $sth->execute($input->param('bookfundid'),$input->param('bookfundname'));
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 $dbh = C4::Context->dbh;
189 my $sth=$dbh->prepare("select bookfundid,bookfundname,bookfundgroup from aqbookfund where bookfundid=?");
190 $sth->execute($bookfundid);
191 my $data=$sth->fetchrow_hashref;
193 $template->param(bookfundid => $bookfundid);
194 $template->param(bookfundname => $data->{'bookfundname'});
195 # END $OP eq DELETE_CONFIRM
196 ################## DELETE_CONFIRMED ##################################
197 # called by delete_confirm, used to effectively confirm deletion of data in DB
198 } elsif ($op eq 'delete_confirmed') {
199 my $dbh = C4::Context->dbh;
200 my $bookfundid=uc($input->param('bookfundid'));
201 my $sth=$dbh->prepare("delete from aqbookfund where bookfundid=?");
202 $sth->execute($bookfundid);
204 $sth=$dbh->prepare("delete from aqbudget where bookfundid=?");
205 $sth->execute($bookfundid);
207 # END $OP eq DELETE_CONFIRMED
208 ################## DEFAULT ##################################
210 $template->param(scriptname => $script_name);
211 if ($searchfield ne '') {
212 $template->param(search => 1);
213 $template->param(searchfield => $searchfield);
216 my ($count,$results)=StringSearch($env,$searchfield,%select_branches);
219 my $dbh = C4::Context->dbh;
220 for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
221 # warn "i ".$i." offset".$offset." pagesize ".$pagesize." id ".$results->[$i]{bookfundid}." name ".$results->[$i]{bookfundname}." branchcode ".$results->[$i]{branchcode};
223 $row_data{bookfundid} =$results->[$i]{'bookfundid'};
224 $row_data{bookfundname} = $results->[$i]{'bookfundname'};
225 # warn "".$results->[$i]{'bookfundid'}." ".$results->[$i]{'bookfundname'}." ".$results->[$i]{'branchcode'};
226 $row_data{branchname} = $select_branches{$results->[$i]{'branchcode'}};
227 my $strsth2="Select aqbudgetid,startdate,enddate,budgetamount from aqbudget where aqbudget.bookfundid = ?";
228 # my $strsth2="Select aqbudgetid,startdate,enddate,budgetamount,branchcode from aqbudget where aqbudget.bookfundid = ?";
230 # $strsth2 .= " AND ((aqbudget.branchcode is null) OR (aqbudget.branchcode='') OR (aqbudget.branchcode= ".$dbh->quote($homebranch).")) " ;
232 # $strsth2 .= " AND (aqbudget.branchcode='') " if ((C4::Context->userenv) && (C4::Context->userenv->{flags}>1));
234 $strsth2 .= " order by aqbudgetid";
236 my $sth2 = $dbh->prepare($strsth2);
237 $sth2->execute($row_data{bookfundid});
239 # while (my ($aqbudgetid,$startdate,$enddate,$budgetamount,$branchcode) = $sth2->fetchrow) {
240 while (my ($aqbudgetid,$startdate,$enddate,$budgetamount) = $sth2->fetchrow) {
242 $budgetrow_data{aqbudgetid} = $aqbudgetid;
243 $budgetrow_data{startdate} = format_date($startdate);
244 $budgetrow_data{enddate} = format_date($enddate);
245 $budgetrow_data{budgetamount} = $budgetamount;
246 # $budgetrow_data{branchcode} = $branchcode;
247 push @budget_loop,\%budgetrow_data;
249 $row_data{budget} = \@budget_loop;
250 push @loop_data,\%row_data;
252 $template->param(max => (($count>$offset+$pagesize)?$offset+$pagesize:$count));
253 $template->param(min => ($offset?$offset:1));
254 $template->param(nbresults => $count);
255 $template->param(Next => ($count>$offset+$pagesize)) if ($count>$offset+$pagesize);
256 $template->param(bookfund => \@loop_data);
257 } #---- END $OP eq DEFAULT
258 $template->param(intranetcolorstylesheet => C4::Context->preference("intranetcolorstylesheet"),
259 intranetstylesheet => C4::Context->preference("intranetstylesheet"),
260 IntranetNav => C4::Context->preference("IntranetNav"),
262 output_html_with_http_headers $input, $cookie, $template->output;