adding a 'use ZOOM' to biblio.pm, needed for non-mod_perl install.
[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::Koha;
44 use C4::Context;
45 use C4::Acquisition;
46 use C4::Output;
47 use C4::Interface::CGI::Output;
48 use C4::Search;
49 use C4::Date;
50 use HTML::Template;
51
52 sub StringSearch  {
53         my ($env,$searchstring,$branches)=@_;
54         my $dbh = C4::Context->dbh;
55         $searchstring=~ s/\'/\\\'/g;
56         my @data=split(' ',$searchstring);
57         my $count=@data;
58         my $strsth= "select bookfundid,bookfundname,bookfundgroup,branchcode from aqbookfund where bookfundname like ? ";
59         if ($branches){
60                 $strsth.= "AND (aqbookfund.branchcode is null " ;
61                 foreach my $branchcode (keys %$branches){
62                         $strsth .= "or aqbookfund.branchcode = '".$branchcode."' "; 
63                 }
64                 $strsth .= ") ";
65         }
66         $strsth.= "order by aqbookfund.bookfundid";
67 #       warn "chaine de recherche : ".$strsth;
68         
69         my $sth=$dbh->prepare($strsth);
70         $sth->execute("%$data[0]%");
71         my @results;
72         while (my $data=$sth->fetchrow_hashref){
73                 push(@results,$data);
74         }
75         #  $sth->execute;
76         $sth->finish;
77         return (scalar(@results),\@results);
78 }
79
80 my $dbh = C4::Context->dbh;
81 my $input = new CGI;
82 my $searchfield=$input->param('searchfield') || '';
83 my $offset=$input->param('offset');
84 my $script_name="/cgi-bin/koha/admin/aqbookfund.pl";
85 my $bookfundid=$input->param('bookfundid');
86 my $pagesize=20;
87 my $op = $input->param('op') || '';
88 $searchfield=~ s/\,//g;
89
90 my ($template, $borrowernumber, $cookie)
91     = get_template_and_user({template_name => "parameters/aqbookfund.tmpl",
92                              query => $input,
93                              type => "intranet",
94                              authnotrequired => 0,
95                              flagsrequired => {parameters => 1, management => 1},
96                              debug => 1,
97                              });
98
99 if ($op) {
100 $template->param(script_name => $script_name,
101                 $op              => 1); # we show only the TMPL_VAR names $op
102 } else {
103 $template->param(script_name => $script_name,
104                 else              => 1); # we show only the TMPL_VAR names $op
105 }
106 $template->param(action => $script_name);
107
108 # my @branches;
109 # my @select_branch;
110 # my %select_branches;
111
112 my $branches = getbranches;
113 my @branchloop;
114 foreach my $thisbranch (sort keys %$branches) {
115 #       my $selected = 1 if $thisbranch eq $branch;
116         my %row =(value => $thisbranch,
117 #                               selected => $selected,
118                                 branchname => $branches->{$thisbranch}->{'branchname'},
119                         );
120         push @branchloop, \%row;
121 #       $select_branches{$thisbranch} = $branches->{$thisbranch}->{'branchname'};
122 }
123
124 # my $homebranch=C4::Context->userenv->{branch};
125 # for (my $i=0;$i<$count2;$i++){
126 #       push @select_branch, $branches[$i]->{'branchcode'};#
127 #       $select_branches{$branches[$i]->{'branchcode'}} = $branches[$i]->{'branchname'};
128 # }
129
130 # my $CGIbranch=CGI::scrolling_list( -name     => 'branchcode',
131 #                       -values   => \@select_branch,
132 #                       -labels   => \%select_branches,
133 #                       -size     => 1,
134 #                       -multiple => 0 );
135 $template->param(branchloop => \@branchloop);
136
137 ################## ADD_FORM ##################################
138 # called by default. Used to create form to add or  modify a record
139 if ($op eq 'add_form') {
140         #---- if primkey exists, it's a modify action, so read values to modify...
141         my $data;
142         my $header;
143         if ($bookfundid) {
144                 my $sth=$dbh->prepare("select bookfundid,bookfundname,bookfundgroup from aqbookfund where bookfundid=?");
145                 $sth->execute($bookfundid);
146                 $data=$sth->fetchrow_hashref;
147                 $sth->finish;
148             }
149         if ($bookfundid) {
150             $header = "Modify book fund";
151             $template->param('header-is-modify-p' => 1);
152         } else {
153             $header = "Add book fund";
154             $template->param('header-is-add-p' => 1);
155         }
156         $template->param('use-header-flags-p' => 1);
157         $template->param(header => $header); # NOTE deprecated
158         my $add_or_modify=0;
159         if ($bookfundid) {
160             $add_or_modify=1;
161         }
162         $template->param(add_or_modify => $add_or_modify);
163         $template->param(bookfundid =>$bookfundid);
164         $template->param(bookfundname =>$data->{'bookfundname'});
165
166                                                                                                         # END $OP eq ADD_FORM
167 ################## ADD_VALIDATE ##################################
168 # called by add_form, used to insert/modify data in DB
169 } elsif ($op eq 'add_validate') {
170         my $bookfundid=uc($input->param('bookfundid'));
171         my $sth=$dbh->prepare("delete from aqbookfund where bookfundid =?");
172         $sth->execute($bookfundid);
173         $sth->finish;
174         $sth=$dbh->prepare("replace aqbookfund (bookfundid,bookfundname,branchcode) values (?,?,?)");
175         $sth->execute($input->param('bookfundid'),$input->param('bookfundname'),$input->param('branchcode'));
176         $sth->finish;
177         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=aqbookfund.pl\"></html>";
178         exit;
179                         
180                                                                                 # END $OP eq ADD_VALIDATE
181 ################## DELETE_CONFIRM ##################################
182 # called by default form, used to confirm deletion of data in DB
183 } elsif ($op eq 'delete_confirm') {
184         my $sth=$dbh->prepare("select bookfundid,bookfundname,bookfundgroup from aqbookfund where bookfundid=?");
185         $sth->execute($bookfundid);
186         my $data=$sth->fetchrow_hashref;
187         $sth->finish;
188         $template->param(bookfundid => $bookfundid);
189         $template->param(bookfundname => $data->{'bookfundname'});
190                                                                                                         # END $OP eq DELETE_CONFIRM
191 ################## DELETE_CONFIRMED ##################################
192 # called by delete_confirm, used to effectively confirm deletion of data in DB
193 } elsif ($op eq 'delete_confirmed') {
194         my $bookfundid=uc($input->param('bookfundid'));
195         my $sth=$dbh->prepare("delete from aqbookfund where bookfundid=?");
196         $sth->execute($bookfundid);
197         $sth->finish;
198         $sth=$dbh->prepare("delete from aqbudget where bookfundid=?");
199         $sth->execute($bookfundid);
200         $sth->finish;
201                                                                                                         # END $OP eq DELETE_CONFIRMED
202 ################## DEFAULT ##################################
203 } else { # DEFAULT
204         $template->param(scriptname => $script_name);
205         if  ($searchfield ne '') {
206                 $template->param(search => 1);
207                 $template->param(searchfield => $searchfield);
208         }
209         my $env;
210         my ($count,$results)=StringSearch($env,$searchfield,$branches);
211         my $toggle="white";
212         my @loop_data =();
213         my $dbh = C4::Context->dbh;
214         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
215                 my %row_data;
216                 $row_data{bookfundid} =$results->[$i]{'bookfundid'};
217                 $row_data{bookfundname} = $results->[$i]{'bookfundname'};
218 #               warn "".$results->[$i]{'bookfundid'}." ".$results->[$i]{'bookfundname'}." ".$results->[$i]{'branchcode'};
219                 $row_data{branchname} = $branches->{$results->[$i]{'branchcode'}}->{branchname};
220                 my $strsth2="Select aqbudgetid,startdate,enddate,budgetamount from aqbudget where aqbudget.bookfundid = ?";
221 #               my $strsth2="Select aqbudgetid,startdate,enddate,budgetamount,branchcode from aqbudget where aqbudget.bookfundid = ?";
222 #               if ($homebranch){
223 #                       $strsth2 .= " AND ((aqbudget.branchcode is null) OR (aqbudget.branchcode='') OR (aqbudget.branchcode= ".$dbh->quote($homebranch).")) " ;
224 #               } else {
225 #                       $strsth2 .= " AND (aqbudget.branchcode='') " if ((C4::Context->userenv) && (C4::Context->userenv->{flags}>1));
226 #               }
227                 $strsth2 .= " order by aqbudgetid";
228 #               warn "".$strsth2;
229                 my $sth2 = $dbh->prepare($strsth2);
230                 $sth2->execute($row_data{bookfundid});
231                 my @budget_loop;
232 #               while (my ($aqbudgetid,$startdate,$enddate,$budgetamount,$branchcode) = $sth2->fetchrow) {
233                 while (my ($aqbudgetid,$startdate,$enddate,$budgetamount) = $sth2->fetchrow) {
234                         my %budgetrow_data;
235                         $budgetrow_data{aqbudgetid} = $aqbudgetid;
236                         $budgetrow_data{startdate} = format_date($startdate);
237                         $budgetrow_data{enddate} = format_date($enddate);
238                         $budgetrow_data{budgetamount} = $budgetamount;
239 #                       $budgetrow_data{branchcode} = $branchcode;
240                         push @budget_loop,\%budgetrow_data;
241                 }
242                 if ($sth2->rows){
243                         $row_data{budget} = \@budget_loop;
244                         push @loop_data,\%row_data;
245                 }
246         }
247         $template->param(bookfund => \@loop_data);
248 } #---- END $OP eq DEFAULT
249
250 output_html_with_http_headers $input, $cookie, $template->output;