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