removing useless files
[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::Date;
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
177 warn "$bookfundid, $bookfundname, $branchcode";
178
179     my $number = Countbookfund($bookfundid,$branchcodeid);
180 ### $number
181
182
183     if ($number < 2)  {
184          warn "name :$bookfundname branch:$branchcode";
185         ModBookFund($bookfundname,$bookfundid,$current_branch, $branchcode);
186     }
187    $input->redirect('aqbookfund.pl');
188 # END $OP eq ADD_VALIDATE
189 }
190
191
192
193
194
195 #-----############# DELETE_CONFIRM ##################################
196 # called by default form, used to confirm deletion of data in DB
197
198 elsif ($op eq 'delete_confirm') {
199     my $data = GetBookFund($bookfundid,$branchcodeid);
200         $template->param(bookfundid => $bookfundid);
201         $template->param(bookfundname => $data->{'bookfundname'});
202         $template->param(branchcode => $data->{'branchcode'});
203 } # END $OP eq DELETE_CONFIRM
204
205
206 #-----############# DELETE_CONFIRMED ##################################
207 # called by delete_confirm, used to effectively confirm deletion of data in DB
208 elsif ($op eq 'delete_confirmed') {
209     DelBookFund(uc($input->param('bookfundid')),$branchcodeid);
210
211 }# END $OP eq DELETE_CONFIRMED
212
213
214 #-----############# DEFAULT ##################################
215 else { # DEFAULT
216     my ($query, $sth);
217
218     $template->param(scriptname => $script_name);
219
220     # filters
221     my @branchloop;
222     foreach my $branchcode (sort keys %{$branches}) {
223         my $row = {
224             code => $branchcode,
225             name => $branches->{$branchcode}->{branchname},
226         };
227
228         if (defined $input->param('filter_branchcode')
229             and $input->param('filter_branchcode') eq $branchcode) {
230             $row->{selected} = 1;
231         }
232
233         push @branchloop, $row;
234     }
235
236     my @bookfundids_loop;
237     my $sth = GetBookFundsId();
238
239     while (my $row = $sth->fetchrow_hashref) {
240         if (defined $input->param('filter_bookfundid') and $input->param('filter_bookfundid') eq $row->{bookfundid}){
241             $row->{selected} = 1;
242         }
243          push @bookfundids_loop, $row;
244      }
245
246     $template->param(
247         filter_bookfundids => \@bookfundids_loop,
248         filter_branches => \@branchloop,
249         filter_bookfundname => $input->param('filter_bookfundname') || undef,
250     );
251
252     # searching the bookfunds corresponding to our filtering rules
253     my @results = SearchBookFund(
254         $input->param('filter'),
255         $input->param('filter_bookfundid'),
256         $input->param('filter_bookfundname'),
257         $input->param('filter_branchcode'),
258     );
259
260     # does the book funds have budgets?
261     my @loop_id;
262     my $sth = GetBookFundsId();
263     while (my $row = $sth->fetchrow){
264         push @loop_id, $row;
265     }
266
267     my ($id,%nb_budgets_of);
268     foreach $id (@loop_id){
269         my $number = Countbookfund($id);
270         $nb_budgets_of{$id} = $number;
271     }
272
273     # pagination informations
274     my $page = $input->param('page') || 1;
275     my @loop;
276
277     my $first = ($page - 1) * $pagesize;
278
279     # if we are on the last page, the number of the last word to display
280     # must not exceed the length of the results array
281     my $last = min(
282         $first + $pagesize - 1,
283         scalar(@results) - 1,
284     );
285
286     my $toggle = 0;
287     foreach my $result (@results[$first .. $last]) {
288         push(
289             @loop,
290             {
291                 %{$result},
292                 toggle => $toggle++%2,
293                 branchname =>
294                     $branches->{ $result->{branchcode} }->{branchname},
295                 has_budgets => defined $nb_budgets_of{ $result->{bookfundid} },
296             }
297         );
298     }
299
300     $template->param(
301             bookfund => \@loop,
302             pagination_bar => pagination_bar(
303                         $script_name,
304                         getnbpages(scalar @results, $pagesize),
305                         $page,
306                         'page'
307             )
308         );
309 } #---- END $OP eq DEFAULT
310 output_html_with_http_headers $input, $cookie, $template->output;