fix "Add Tag" button in MARC framework editor
[koha.git] / admin / aqbookfund.pl
blobd5673c8c36488cbe4f0dcd822e6d815f74dc9e87
1 #!/usr/bin/perl
3 #written 20/02/2002 by paul.poulain@free.fr
5 # Copyright 2000-2002 Katipo Communications
7 # This file is part of Koha.
9 # Koha is free software; you can redistribute it and/or modify it under the
10 # terms of the GNU General Public License as published by the Free Software
11 # Foundation; either version 2 of the License, or (at your option) any later
12 # version.
14 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License along with
19 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
20 # Suite 330, Boston, MA 02111-1307 USA
23 =head1 NAME
25 aqbookfund.pl
27 =head1 DESCRIPTION
29 script to administer the aqbudget table.
31 =head1 CGI PARAMETERS
33 =over 4
35 =item op
36 this script use an C<$op> to know what to do.
37 C<op> can be equal to:
38 * empty or none of the above values, then
39 - the default screen is build (with all records, or filtered datas).
40 - the user can clic on add, modify or delete record.
41 * add_form, then
42 - if primkey exists, this is a modification,so we read the $primkey record
43 - builds the add/modify form
44 * add_validate, then
45 - the user has just send datas, so we create/modify the record
46 * delete_form, then
47 - we show the record having primkey=$primkey and ask for deletion validation form
48 * delete_confirm, then
49 - we delete the record having primkey=$primkey
51 =cut
53 use strict;
54 use CGI;
55 use List::Util qw/min/;
56 use C4::Branch; # GetBranches
57 use C4::Auth;
58 use C4::Koha;
59 use C4::Context;
60 use C4::Bookfund;
61 use C4::Output;
62 use C4::Dates;
63 use C4::Debug;
65 # use Smart::Comments;
67 my $dbh = C4::Context->dbh;
68 my $input = new CGI;
69 my $script_name="/cgi-bin/koha/admin/aqbookfund.pl";
70 my $bookfundid=$input->param('bookfundid');
71 my $branchcodeid=$input->param('branchcode')|'';
72 my $pagesize = 10;
73 my $op = $input->param('op') || '';
75 my ($template, $borrowernumber, $cookie)
76 = get_template_and_user(
77 {template_name => "admin/aqbookfund.tmpl",
78 query => $input,
79 type => "intranet",
80 authnotrequired => 0,
81 flagsrequired => {parameters => 1},
82 debug => 1,
86 if ($op) {
87 $template->param(
88 script_name => $script_name,
89 $op => 1,
90 ); # we show only the TMPL_VAR names $op
92 else {
93 $template->param(script_name => $script_name,
94 else => 1); # we show only the TMPL_VAR names $op
96 $template->param(action => $script_name);
98 my $branches = GetBranches;
100 #-----############# ADD_FORM ##################################
101 # called by default. Used to create form to add or modify a record
102 if ($op eq 'add_form') {
103 #---- if primkey exists, it's a modify action, so read values to modify...
104 my $dataaqbookfund;
105 my $header;
106 if ($bookfundid) {
107 $dataaqbookfund = GetBookFund($bookfundid,$branchcodeid);
109 if ($bookfundid) {
110 $header = "Modify book fund";
111 $template->param('header-is-modify-p' => 1);
112 $template->param('current_branch' => $branchcodeid);
113 } else {
114 $header = "Add book fund";
115 $template->param('header-is-add-p' => 1);
117 $template->param('use-header-flags-p' => 1);
118 $template->param(header => $header); # NOTE deprecated
119 my $add_or_modify=0;
120 if ($bookfundid) {
121 $add_or_modify=1;
123 $template->param(add_or_modify => $add_or_modify);
124 $template->param(bookfundid =>$bookfundid);
125 $template->param(bookfundname =>$dataaqbookfund->{'bookfundname'});
127 my @branchloop;
128 foreach my $branchcode (sort keys %{$branches}) {
129 my $row = {
130 branchcode => $branchcode,
131 branchname => $branches->{$branchcode}->{branchname},
134 if (defined $bookfundid
135 and defined $dataaqbookfund->{branchcode}
136 and $dataaqbookfund->{branchcode} eq $branchcode) {
137 $row->{selected} = 1;
140 push @branchloop, $row;
143 $template->param(branches => \@branchloop);
145 } # END $OP eq ADD_FORM
147 #-----############# ADD_VALIDATE ##################################
148 # called by add_form, used to insert/modify data in DB
149 elsif ($op eq 'add_validate') {
150 ### add
151 my $bookfundid = uc $input->param('bookfundid');
152 my $bookfundname = $input->param('bookfundname');
153 my $branchcode = $input->param('branchcode') || undef;
155 my $number = Countbookfund($bookfundid,$branchcodeid);
156 if ($number == 0 ) {
158 NewBookFund(
159 $bookfundid,
160 $input->param('bookfundname'),
161 $input->param('branchcode')||''
164 $input->redirect('aqbookfund.pl');
165 # END $OP eq ADD_VALIDATE
168 #-----############# MOD_VALIDATE ##################################
169 # called by add_form, used to insert/modify data in DB
170 elsif ($op eq 'mod_validate') {
171 my $bookfundid = uc $input->param('bookfundid');
172 my $bookfundname = $input->param('bookfundname');
173 my $branchcode = $input->param('branchcode' ) || undef;
174 my $current_branch = $input->param('current_branch') || undef;
175 $debug and warn "$bookfundid, $bookfundname, $branchcode";
177 my $number = Countbookfund($bookfundid,$branchcodeid);
178 if ($number < 2) {
179 $debug and warn "name :$bookfundname branch:$branchcode";
180 ModBookFund($bookfundname,$bookfundid,$current_branch, $branchcode);
182 $input->redirect('aqbookfund.pl');
185 #-----############# DELETE_CONFIRM ##################################
186 # called by default form, used to confirm deletion of data in DB
187 elsif ($op eq 'delete_confirm') {
188 my $data = GetBookFund($bookfundid,$branchcodeid);
189 $template->param(bookfundid => $bookfundid);
190 $template->param(bookfundname => $data->{'bookfundname'});
191 $template->param(branchcode => $data->{'branchcode'});
192 } # END $OP eq DELETE_CONFIRM
194 #-----############# DELETE_CONFIRMED ##################################
195 # called by delete_confirm, used to effectively confirm deletion of data in DB
196 elsif ($op eq 'delete_confirmed') {
197 DelBookFund(uc($input->param('bookfundid')),$branchcodeid);
199 }# END $OP eq DELETE_CONFIRMED
201 #-----############# DEFAULT ##################################
202 else { # DEFAULT
203 my ($query, $sth);
205 $template->param(scriptname => $script_name);
207 # filters
208 my @branchloop;
209 foreach my $branchcode (sort keys %{$branches}) {
210 my $row = {
211 code => $branchcode,
212 name => $branches->{$branchcode}->{branchname},
214 if (defined $input->param('filter_branchcode')
215 and $input->param('filter_branchcode') eq $branchcode) {
216 $row->{selected} = 1;
218 push @branchloop, $row;
221 my @bookfundids_loop;
222 $sth = GetBookFundsId();
224 while (my $row = $sth->fetchrow_hashref) {
225 if (defined $input->param('filter_bookfundid') and $input->param('filter_bookfundid') eq $row->{bookfundid}){
226 $row->{selected} = 1;
228 push @bookfundids_loop, $row;
231 $template->param(
232 filter_bookfundids => \@bookfundids_loop,
233 filter_branches => \@branchloop,
234 filter_bookfundname => $input->param('filter_bookfundname') || undef,
237 # searching the bookfunds corresponding to our filtering rules
238 my @results = SearchBookFund(
239 $input->param('filter'),
240 $input->param('filter_bookfundid'),
241 $input->param('filter_bookfundname'),
242 $input->param('filter_branchcode'),
245 # does the book funds have budgets?
246 my @loop_id;
247 $sth = GetBookFundsId();
248 while (my $row = $sth->fetchrow){
249 push @loop_id, $row;
252 my ($id,%nb_budgets_of);
253 foreach $id (@loop_id){
254 my $number = Countbookfund($id);
255 $nb_budgets_of{$id} = $number;
258 # pagination informations
259 my $page = $input->param('page') || 1;
260 my @loop;
262 my $first = ($page - 1) * $pagesize;
264 # if we are on the last page, the number of the last word to display
265 # must not exceed the length of the results array
266 my $last = min(
267 $first + $pagesize - 1,
268 scalar(@results) - 1,
271 my $toggle = 0;
272 foreach my $result (@results[$first .. $last]) {
273 push(
274 @loop,
276 %{$result},
277 toggle => $toggle++%2,
278 branchname =>
279 $branches->{ $result->{branchcode} }->{branchname},
280 has_budgets => defined $nb_budgets_of{ $result->{bookfundid} },
285 $template->param(
286 bookfund => \@loop,
287 pagination_bar => pagination_bar(
288 $script_name,
289 getnbpages(scalar @results, $pagesize),
290 $page,
291 'page'
294 } #---- END $OP eq DEFAULT
295 output_html_with_http_headers $input, $cookie, $template->output;