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