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