rebuild_zebra.pl: removed disused $limit option
[koha.git] / admin / aqbookfund.pl
1 #!/usr/bin/perl
2
3 #written 20/02/2002 by paul.poulain@free.fr
4
5 # Copyright 2000-2002 Katipo Communications
6 #
7 # This file is part of Koha.
8 #
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.
13 #
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.
17 #
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
21
22
23 =head1 NAME
24
25 aqbookfund.pl
26
27 =head1 DESCRIPTION
28
29 script to administer the aqbudget table.
30
31 =head1 CGI PARAMETERS
32
33 =over 4
34
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
50
51 =cut
52
53 use strict;
54 use CGI;
55 use C4::Branch; # GetBranches
56 use List::Util qw/min/;
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
64 # use Smart::Comments;
65
66 my $dbh = C4::Context->dbh;
67 my $input = new CGI;
68 my $script_name="/cgi-bin/koha/admin/aqbookfund.pl";
69 my $bookfundid=$input->param('bookfundid');
70 my $branchcodeid=$input->param('branchcode')|'';
71 my $pagesize = 10;
72 my $op = $input->param('op') || '';
73
74 my ($template, $borrowernumber, $cookie)
75     = get_template_and_user(
76         {template_name => "admin/aqbookfund.tmpl",
77          query => $input,
78          type => "intranet",
79          authnotrequired => 0,
80          flagsrequired => {parameters => 1},
81          debug => 1,
82         }
83     );
84
85 if ($op) {
86     $template->param(
87         script_name => $script_name,
88         $op => 1,
89     ); # we show only the TMPL_VAR names $op
90 }
91 else {
92     $template->param(script_name => $script_name,
93                 else              => 1); # we show only the TMPL_VAR names $op
94 }
95 $template->param(action => $script_name);
96
97 my $branches = GetBranches;
98
99 #-----############# ADD_FORM ##################################
100 # called by default. Used to create form to add or  modify a record
101 if ($op eq 'add_form') {
102         #---- if primkey exists, it's a modify action, so read values to modify...
103         my $dataaqbookfund;
104         my $header;
105         if ($bookfundid) {
106         $dataaqbookfund = GetBookFund($bookfundid,$branchcodeid);
107         }
108         if ($bookfundid) {
109             $header = "Modify book fund";
110             $template->param('header-is-modify-p' => 1);
111             $template->param('current_branch' =>  $branchcodeid);
112         } else {
113             $header = "Add book fund";
114             $template->param('header-is-add-p' => 1);
115         }
116         $template->param('use-header-flags-p' => 1);
117         $template->param(header => $header); # NOTE deprecated
118         my $add_or_modify=0;
119         if ($bookfundid) {
120             $add_or_modify=1;
121         }
122         $template->param(add_or_modify => $add_or_modify);
123         $template->param(bookfundid =>$bookfundid);
124         $template->param(bookfundname =>$dataaqbookfund->{'bookfundname'});
125
126         my @branchloop;
127         foreach my $branchcode (sort keys %{$branches}) {
128             my $row = {
129                 branchcode => $branchcode,
130                 branchname => $branches->{$branchcode}->{branchname},
131             };
132
133             if (defined $bookfundid
134                 and defined $dataaqbookfund->{branchcode}
135                 and $dataaqbookfund->{branchcode} eq $branchcode) {
136                 $row->{selected} = 1;
137             }
138
139             push @branchloop, $row;
140         }
141
142         $template->param(branches => \@branchloop);
143
144 } # END $OP eq ADD_FORM
145
146 #-----############# ADD_VALIDATE ##################################
147 # called by add_form, used to insert/modify data in DB
148 elsif ($op eq 'add_validate') {
149 ### add
150         my $bookfundid = uc $input->param('bookfundid');
151         my $bookfundname = $input->param('bookfundname');
152         my $branchcode = $input->param('branchcode') || undef;
153
154     my $number = Countbookfund($bookfundid,$branchcodeid);
155     if ($number == 0 ) {
156
157         NewBookFund(
158             $bookfundid,
159             $input->param('bookfundname'),
160             $input->param('branchcode')||''
161         );
162     }
163     $input->redirect('aqbookfund.pl');
164 # END $OP eq ADD_VALIDATE
165 }
166
167 #-----############# MOD_VALIDATE ##################################
168 # called by add_form, used to insert/modify data in DB
169 elsif ($op eq 'mod_validate') {
170 ### mod ddddddddddddddddd
171
172         my $bookfundid  = uc $input->param('bookfundid');
173         my $bookfundname   = $input->param('bookfundname');
174         my $branchcode     = $input->param('branchcode'    ) || undef;
175         my $current_branch = $input->param('current_branch') || undef;
176         warn "$bookfundid, $bookfundname, $branchcode";
177
178         my $number = Countbookfund($bookfundid,$branchcodeid);
179 ### $number
180
181
182     if ($number < 2)  {
183          warn "name :$bookfundname branch:$branchcode";
184         ModBookFund($bookfundname,$bookfundid,$current_branch, $branchcode);
185     }
186    $input->redirect('aqbookfund.pl');
187 # END $OP eq ADD_VALIDATE
188 }
189
190 #-----############# DELETE_CONFIRM ##################################
191 # called by default form, used to confirm deletion of data in DB
192
193 elsif ($op eq 'delete_confirm') {
194     my $data = GetBookFund($bookfundid,$branchcodeid);
195         $template->param(bookfundid => $bookfundid);
196         $template->param(bookfundname => $data->{'bookfundname'});
197         $template->param(branchcode => $data->{'branchcode'});
198 } # END $OP eq DELETE_CONFIRM
199
200
201 #-----############# DELETE_CONFIRMED ##################################
202 # called by delete_confirm, used to effectively confirm deletion of data in DB
203 elsif ($op eq 'delete_confirmed') {
204     DelBookFund(uc($input->param('bookfundid')),$branchcodeid);
205
206 }# END $OP eq DELETE_CONFIRMED
207
208
209 #-----############# DEFAULT ##################################
210 else { # DEFAULT
211     my ($query, $sth);
212
213     $template->param(scriptname => $script_name);
214
215     # filters
216     my @branchloop;
217     foreach my $branchcode (sort keys %{$branches}) {
218         my $row = {
219             code => $branchcode,
220             name => $branches->{$branchcode}->{branchname},
221         };
222         if (defined $input->param('filter_branchcode')
223             and $input->param('filter_branchcode') eq $branchcode) {
224             $row->{selected} = 1;
225         }
226         push @branchloop, $row;
227     }
228
229     my @bookfundids_loop;
230     $sth = GetBookFundsId();
231
232     while (my $row = $sth->fetchrow_hashref) {
233         if (defined $input->param('filter_bookfundid') and $input->param('filter_bookfundid') eq $row->{bookfundid}){
234             $row->{selected} = 1;
235         }
236          push @bookfundids_loop, $row;
237      }
238
239     $template->param(
240         filter_bookfundids => \@bookfundids_loop,
241         filter_branches => \@branchloop,
242         filter_bookfundname => $input->param('filter_bookfundname') || undef,
243     );
244
245     # searching the bookfunds corresponding to our filtering rules
246     my @results = SearchBookFund(
247         $input->param('filter'),
248         $input->param('filter_bookfundid'),
249         $input->param('filter_bookfundname'),
250         $input->param('filter_branchcode'),
251     );
252
253     # does the book funds have budgets?
254     my @loop_id;
255     $sth = GetBookFundsId();
256     while (my $row = $sth->fetchrow){
257         push @loop_id, $row;
258     }
259
260     my ($id,%nb_budgets_of);
261     foreach $id (@loop_id){
262         my $number = Countbookfund($id);
263         $nb_budgets_of{$id} = $number;
264     }
265
266     # pagination informations
267     my $page = $input->param('page') || 1;
268     my @loop;
269
270     my $first = ($page - 1) * $pagesize;
271
272     # if we are on the last page, the number of the last word to display
273     # must not exceed the length of the results array
274     my $last = min(
275         $first + $pagesize - 1,
276         scalar(@results) - 1,
277     );
278
279     my $toggle = 0;
280     foreach my $result (@results[$first .. $last]) {
281         push(
282             @loop,
283             {
284                 %{$result},
285                 toggle => $toggle++%2,
286                 branchname =>
287                     $branches->{ $result->{branchcode} }->{branchname},
288                 has_budgets => defined $nb_budgets_of{ $result->{bookfundid} },
289             }
290         );
291     }
292
293     $template->param(
294             bookfund => \@loop,
295             pagination_bar => pagination_bar(
296                         $script_name,
297                         getnbpages(scalar @results, $pagesize),
298                         $page,
299                         'page'
300             )
301         );
302 } #---- END $OP eq DEFAULT
303 output_html_with_http_headers $input, $cookie, $template->output;