New XML API
[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::Koha;
46 use C4::Interface::CGI::Output;
47 use C4::Search;
48 use C4::Date;
49
50
51 sub StringSearch  {
52         my ($env,$searchstring,%branches)=@_;
53         my $dbh = C4::Context->dbh;
54         $searchstring=~ s/\'/\\\'/g;
55         my @data=split(' ',$searchstring) if ($searchstring ne "");
56         my $count=@data;
57         my $strsth= "select bookfundid,bookfundname,bookfundgroup,branchcode from aqbookfund where 1 ";
58         $strsth.=" AND bookfundname like ? " if ($searchstring ne "");
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         if ($searchstring){
71                 $sth->execute("%$data[0]%");
72         } else {
73                 $sth->execute;
74         }
75         my @results;
76         while (my $data=$sth->fetchrow_hashref){
77                 push(@results,$data);
78 #               warn "id ".$data->{bookfundid}." name ".$data->{bookfundname}." branchcode ".$data->{branchcode};
79         }
80         #  $sth->execute;
81         $sth->finish;
82         return (scalar(@results),\@results);
83 }
84
85 my $input = new CGI;
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');
90 my $pagesize=20;
91 my $op = $input->param('op');
92 $searchfield=~ s/\,//g;
93
94 my ($template, $borrowernumber, $cookie)
95     = get_template_and_user({template_name => "admin/aqbookfund.tmpl",
96                              query => $input,
97                              type => "intranet",
98                              authnotrequired => 0,
99                              flagsrequired => {parameters => 1, management => 1},
100                              debug => 1,
101                              });
102
103 if ($op) {
104 $template->param(script_name => $script_name,
105                 $op              => 1); # we show only the TMPL_VAR names $op
106 } else {
107 $template->param(script_name => $script_name,
108                 else              => 1); # we show only the TMPL_VAR names $op
109 }
110 $template->param(action => $script_name);
111
112
113 my @select_branch;
114 my %select_branches;
115 my ($branches)=GetBranches();
116
117 push @select_branch,"";
118 $select_branches{""}="";
119
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'};
124 }
125
126 my $CGIbranch=CGI::scrolling_list( -name     => 'branchcode',
127                         -values   => \@select_branch,
128                         -labels   => \%select_branches,
129                         -size     => 1,
130                         -multiple => 0 );
131 $template->param(CGIbranch => $CGIbranch);
132
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...
137         my $data;
138         my $header;
139         if ($bookfundid) {
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;
144                 $sth->finish;
145             }
146         if ($bookfundid) {
147             $header = "Modify book fund";
148             $template->param('header-is-modify-p' => 1);
149         } else {
150             $header = "Add book fund";
151             $template->param('header-is-add-p' => 1);
152         }
153         $template->param('use-header-flags-p' => 1);
154         $template->param(header => $header); # NOTE deprecated
155         my $add_or_modify=0;
156         if ($bookfundid) {
157             $add_or_modify=1;
158         }
159         $template->param(add_or_modify => $add_or_modify);
160         $template->param(bookfundid =>$bookfundid);
161         $template->param(bookfundname =>$data->{'bookfundname'});
162
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);
171         $sth->finish;
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'));
175                 $sth->finish;
176         } else {
177                 my $sth=$dbh->prepare("replace aqbookfund (bookfundid,bookfundname) values (?,?)");
178                 $sth->execute($input->param('bookfundid'),$input->param('bookfundname'));
179                 $sth->finish;
180         }
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 $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;
192         $sth->finish;
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);
203         $sth->finish;
204         $sth=$dbh->prepare("delete from aqbudget where bookfundid=?");
205         $sth->execute($bookfundid);
206         $sth->finish;
207                                                                                                         # END $OP eq DELETE_CONFIRMED
208 ################## DEFAULT ##################################
209 } else { # DEFAULT
210         $template->param(scriptname => $script_name);
211         if  ($searchfield ne '') {
212                 $template->param(search => 1);
213                 $template->param(searchfield => $searchfield);
214         }
215         my $env;
216         my ($count,$results)=StringSearch($env,$searchfield,%select_branches);
217         my $toggle="white";
218         my @loop_data =();
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};
222                 my %row_data;
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 = ?";
229 #               if ($homebranch){
230 #                       $strsth2 .= " AND ((aqbudget.branchcode is null) OR (aqbudget.branchcode='') OR (aqbudget.branchcode= ".$dbh->quote($homebranch).")) " ;
231 #               } else {
232 #                       $strsth2 .= " AND (aqbudget.branchcode='') " if ((C4::Context->userenv) && (C4::Context->userenv->{flags}>1));
233 #               }
234                 $strsth2 .= " order by aqbudgetid";
235 #               warn "".$strsth2;
236                 my $sth2 = $dbh->prepare($strsth2);
237                 $sth2->execute($row_data{bookfundid});
238                 my @budget_loop;
239 #               while (my ($aqbudgetid,$startdate,$enddate,$budgetamount,$branchcode) = $sth2->fetchrow) {
240                 while (my ($aqbudgetid,$startdate,$enddate,$budgetamount) = $sth2->fetchrow) {
241                         my %budgetrow_data;
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;
248                 }
249                 $row_data{budget} = \@budget_loop;
250                 push @loop_data,\%row_data;
251         }
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"),
261                 );
262 output_html_with_http_headers $input, $cookie, $template->output;