adding some fields available for Letters
[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,%branches)=@_;
53         my $dbh = C4::Context->dbh;
54         $searchstring=~ s/\'/\\\'/g;
55         my @data=split(' ',$searchstring);
56         my $count=@data;
57         my $strsth= "select bookfundid,bookfundname,bookfundgroup,branchcode from aqbookfund where bookfundname like ? ";
58         if (%branches){
59                 $strsth.= "AND (aqbookfund.branchcode is null " ;
60                 foreach my $branchcode (keys %branches){
61                         $strsth .= "or aqbookfund.branchcode = '".$branchcode."' "; 
62                 }
63                 $strsth .= ") ";
64         }
65         $strsth.= "order by aqbookfund.bookfundid";
66         warn "chaine de recherche : ".$strsth;
67         
68         my $sth=$dbh->prepare($strsth);
69         $sth->execute("%$data[0]%");
70         my @results;
71         while (my $data=$sth->fetchrow_hashref){
72                 push(@results,$data);
73         }
74         #  $sth->execute;
75         $sth->finish;
76         return (scalar(@results),\@results);
77 }
78
79 my $dbh = C4::Context->dbh;
80 my $input = new CGI;
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');
85 my $pagesize=20;
86 my $op = $input->param('op');
87 $searchfield=~ s/\,//g;
88
89 my ($template, $borrowernumber, $cookie)
90     = get_template_and_user({template_name => "parameters/aqbookfund.tmpl",
91                              query => $input,
92                              type => "intranet",
93                              authnotrequired => 0,
94                              flagsrequired => {parameters => 1, management => 1},
95                              debug => 1,
96                              });
97
98 if ($op) {
99 $template->param(script_name => $script_name,
100                 $op              => 1); # we show only the TMPL_VAR names $op
101 } else {
102 $template->param(script_name => $script_name,
103                 else              => 1); # we show only the TMPL_VAR names $op
104 }
105 $template->param(action => $script_name);
106
107 my @branches;
108 my @select_branch;
109 my %select_branches;
110 my ($count2,@branches)=branches();
111
112 push @select_branch,"";
113 $select_branches{""}="";
114
115 my $sthtemp = $dbh->prepare("Select flags, branchcode from borrowers where borrowernumber = ?");
116 $sthtemp->execute($borrowernumber);
117 my ($flags, $homebranch)=$sthtemp->fetchrow;
118 if ($flags>1){
119         if ($homebranch){
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);
123                 }
124         }
125 } else {
126         for (my $i=0;$i<$count2;$i++){
127                 push @select_branch, $branches[$i]->{'branchcode'};#
128                 $select_branches{$branches[$i]->{'branchcode'}} = $branches[$i]->{'branchname'};
129         }
130 }
131 my $CGIbranch=CGI::scrolling_list( -name     => 'branchcode',
132                         -values   => \@select_branch,
133                         -labels   => \%select_branches,
134                         -size     => 1,
135                         -multiple => 0 );
136 $template->param(CGIbranch => $CGIbranch);
137
138 warn "bornum=".$borrowernumber . "flags = ".$flags. " homebranch= ".$homebranch;
139 $sthtemp->finish;
140
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...
145         my $data;
146         my $header;
147         if ($bookfundid) {
148                 my $sth=$dbh->prepare("select bookfundid,bookfundname,bookfundgroup from aqbookfund where bookfundid=?");
149                 $sth->execute($bookfundid);
150                 $data=$sth->fetchrow_hashref;
151                 $sth->finish;
152             }
153         if ($bookfundid) {
154             $header = "Modify book fund";
155             $template->param('header-is-modify-p' => 1);
156         } else {
157             $header = "Add book fund";
158             $template->param('header-is-add-p' => 1);
159         }
160         $template->param('use-header-flags-p' => 1);
161         $template->param(header => $header); # NOTE deprecated
162         my $add_or_modify=0;
163         if ($bookfundid) {
164             $add_or_modify=1;
165         }
166         $template->param(add_or_modify => $add_or_modify);
167         $template->param(bookfundid =>$bookfundid);
168         $template->param(bookfundname =>$data->{'bookfundname'});
169
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);
177         $sth->finish;
178         my $sth=$dbh->prepare("replace aqbookfund (bookfundid,bookfundname, branchcode) values (?,?,?)");
179         $sth->execute($input->param('bookfundid'),$input->param('bookfundname'),$input->param('branchcode'));
180         $sth->finish;
181         print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=aqbookfund.pl\"></html>";
182         exit;
183                         
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;
191         $sth->finish;
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);
201         $sth->finish;
202         $sth=$dbh->prepare("delete from aqbudget where bookfundid=?");
203         $sth->execute($bookfundid);
204         $sth->finish;
205                                                                                                         # END $OP eq DELETE_CONFIRMED
206 ################## DEFAULT ##################################
207 } else { # DEFAULT
208         $template->param(scriptname => $script_name);
209         if  ($searchfield ne '') {
210                 $template->param(search => 1);
211                 $template->param(searchfield => $searchfield);
212         }
213         my $env;
214         my ($count,$results)=StringSearch($env,$searchfield,%select_branches);
215         my $toggle="white";
216         my @loop_data =();
217         for (my $i=$offset; $i < ($offset+$pagesize<$count?$offset+$pagesize:$count); $i++){
218                 my %row_data;
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 = ?";
224                 if ($homebranch){
225                         $strsth2 .= " AND ((aqbudget.branchcode='') OR (aqbudget.branchcode= ".$dbh->quote($homebranch).")) " ;
226                 } else {
227                         $strsth2 .= " AND (aqbudget.branchcode='') " if ($flags>1);
228                 }
229                 $strsth2 .= " order by aqbudgetid";
230                 warn "".$strsth2;
231                 my $sth2 = $dbh->prepare($strsth2);
232                 $sth2->execute($row_data{bookfundid});
233                 my @budget_loop;
234                 while (my ($aqbudgetid,$startdate,$enddate,$budgetamount,$branchcode) = $sth2->fetchrow) {
235                         my %budgetrow_data;
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;
242                 }
243                 if ($sth2->rows){
244                         $row_data{budget} = \@budget_loop;
245                         push @loop_data,\%row_data;
246                 }
247         }
248         $template->param(bookfund => \@loop_data);
249 } #---- END $OP eq DEFAULT
250
251 output_html_with_http_headers $input, $cookie, $template->output;