Bug 2889: Removed toggle variable from aqbookfund.pl and aqbookfund.tmpl.
[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 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;
64
65 # use Smart::Comments;
66
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') || '';
74
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,
83         }
84     );
85
86 if ($op) {
87     $template->param(
88         script_name => $script_name,
89         $op => 1,
90     ); # we show only the TMPL_VAR names $op
91 }
92 else {
93     $template->param(script_name => $script_name,
94                 else              => 1); # we show only the TMPL_VAR names $op
95 }
96 $template->param(action => $script_name);
97
98 my $branches = GetBranches;
99
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);
108         }
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);
116         }
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;
122         }
123         $template->param(add_or_modify => $add_or_modify);
124         $template->param(bookfundid =>$bookfundid);
125         $template->param(bookfundname =>$dataaqbookfund->{'bookfundname'});
126
127         my @branchloop;
128         foreach my $branchcode (sort keys %{$branches}) {
129             my $row = {
130                 branchcode => $branchcode,
131                 branchname => $branches->{$branchcode}->{branchname},
132             };
133
134             if (defined $bookfundid
135                 and defined $dataaqbookfund->{branchcode}
136                 and $dataaqbookfund->{branchcode} eq $branchcode) {
137                 $row->{selected} = 1;
138             }
139
140             push @branchloop, $row;
141         }
142
143         $template->param(branches => \@branchloop);
144
145 } # END $OP eq ADD_FORM
146
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;
154
155     my $number = Countbookfund($bookfundid,$branchcodeid);
156     if ($number == 0 ) {
157
158         NewBookFund(
159             $bookfundid,
160             $input->param('bookfundname'),
161             $input->param('branchcode')||''
162         );
163     }
164     $input->redirect('aqbookfund.pl');
165 # END $OP eq ADD_VALIDATE
166 }
167
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";
176
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);
181     }
182    $input->redirect('aqbookfund.pl');
183 }
184
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
193
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);
198
199 }# END $OP eq DELETE_CONFIRMED
200
201 #-----############# DEFAULT ##################################
202 else { # DEFAULT
203     my ($query, $sth);
204
205     $template->param(scriptname => $script_name);
206
207     # filters
208     my @branchloop;
209     foreach my $branchcode (sort keys %{$branches}) {
210         my $row = {
211             code => $branchcode,
212             name => $branches->{$branchcode}->{branchname},
213         };
214         if (defined $input->param('filter_branchcode')
215             and $input->param('filter_branchcode') eq $branchcode) {
216             $row->{selected} = 1;
217         }
218         push @branchloop, $row;
219     }
220
221     my @bookfundids_loop;
222     $sth = GetBookFundsId();
223
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;
227         }
228          push @bookfundids_loop, $row;
229      }
230
231     $template->param(
232         filter_bookfundids => \@bookfundids_loop,
233         filter_branches => \@branchloop,
234         filter_bookfundname => $input->param('filter_bookfundname') || undef,
235     );
236
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'),
243     );
244
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;
250     }
251
252     my ($id,%nb_budgets_of);
253     foreach $id (@loop_id){
254         my $number = Countbookfund($id);
255         $nb_budgets_of{$id} = $number;
256     }
257
258     # pagination informations
259     my $page = $input->param('page') || 1;
260     my @loop;
261
262     my $first = ($page - 1) * $pagesize;
263
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,
269     );
270
271     foreach my $result (@results[$first .. $last]) {
272         push(
273             @loop,
274             {
275                 %{$result},
276                 branchname =>
277                     $branches->{ $result->{branchcode} }->{branchname},
278                 has_budgets => defined $nb_budgets_of{ $result->{bookfundid} },
279             }
280         );
281     }
282
283     $template->param(
284             bookfund => \@loop,
285             pagination_bar => pagination_bar(
286                         $script_name,
287                         getnbpages(scalar @results, $pagesize),
288                         $page,
289                         'page'
290             )
291         );
292 } #---- END $OP eq DEFAULT
293 output_html_with_http_headers $input, $cookie, $template->output;