Terminology correction: we now use the term "hold" instead of "reserve"
[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_confirm, then
47     - we delete the record having primkey=$primkey
48
49 =cut
50
51 use strict;
52 # use warnings; FIXME
53 use CGI;
54 use List::Util qw/min/;
55 use C4::Branch; # GetBranches
56 use C4::Auth;
57 use C4::Koha;
58 use C4::Context;
59 use C4::Bookfund;
60 use C4::Output;
61 use C4::Dates;
62 use C4::Debug;
63
64 my $input        = new CGI;
65 my $script_name  = "/cgi-bin/koha/admin/aqbookfund.pl";
66 my $bookfundid   = $input->param('bookfundid');
67 my $branchcodeid = $input->param('branchcode') || '';
68 my $op           = $input->param('op')         || '';
69 my $pagesize     = 10;
70
71 $bookfundid = uc $bookfundid if $bookfundid;
72
73 my ($template, $borrowernumber, $cookie) = get_template_and_user(
74     {   template_name   => "admin/aqbookfund.tmpl",
75         query           => $input,
76         type            => "intranet",
77         authnotrequired => 0,
78         flagsrequired   => { parameters => 1 },
79         debug           => 1,
80     }
81 );
82
83 $template->param(
84     action        => $script_name,
85     script_name   => $script_name,
86     ($op||'else') => 1,
87 );
88
89 my $branches = GetBranches;
90
91 #-----############# ADD_FORM ##################################
92 # called by default. Used to create form to add or  modify a record
93 if ($op eq 'add_form') {
94         #---- if primkey exists, it's a modify action, so read values to modify...
95         my $dataaqbookfund;
96         if ($bookfundid) {
97         $dataaqbookfund = GetBookFund($bookfundid, $branchcodeid);
98             $template->param('header-is-modify-p' => 1);
99             $template->param('current_branch' => $branchcodeid);
100         } else {
101             $template->param('header-is-add-p' => 1);
102         }
103         $template->param(
104         'use-header-flags-p' => 1,
105         add_or_modify => $bookfundid ? 1 : 0,
106         bookfundid    => $bookfundid,
107         bookfundname  => $dataaqbookfund->{'bookfundname'}
108     );
109
110     my @branchloop;
111     foreach my $branchcode (sort keys %{$branches}) {
112         push @branchloop, {
113             branchcode => $branchcode,
114             branchname => $branches->{$branchcode}->{branchname},
115             selected   => (defined $bookfundid and defined $dataaqbookfund->{branchcode}
116                             and $dataaqbookfund->{branchcode} eq $branchcode) ? 1 : 0,
117         };
118     }
119
120     $template->param(branches => \@branchloop);
121
122 } # END $OP eq ADD_FORM
123
124 #-----############# ADD_VALIDATE ##################################
125 # called by add_form, used to insert/modify data in DB
126 elsif ($op eq 'add_validate') {
127     my $bookfundname = $input->param('bookfundname');
128     my $branchcode   = $input->param('branchcode') || undef;
129     my $number = Countbookfund($bookfundid,$branchcodeid);
130     if ($number == 0 ) {
131         NewBookFund(
132             $bookfundid,
133             $input->param('bookfundname'),
134             $input->param('branchcode')||''
135         );
136     }
137     print $input->redirect('aqbookfund.pl');    # FIXME: unnecessary redirect
138     exit;
139 # END $OP eq ADD_VALIDATE
140 }
141
142 #-----############# MOD_VALIDATE ##################################
143 # called by add_form, used to insert/modify data in DB
144 elsif ($op eq 'mod_validate') {
145         my $bookfundname   = $input->param('bookfundname');
146         my $branchcode     = $input->param('branchcode'    ) || undef;
147         my $current_branch = $input->param('current_branch') || undef;
148         $debug and warn "$bookfundid, $bookfundname, $branchcode";
149
150         my $number = Countbookfund($bookfundid,$branchcodeid);
151     if ($number < 2)  {
152         $debug and warn "name :$bookfundname branch:$branchcode";
153         ModBookFund($bookfundname,$bookfundid,$current_branch, $branchcode);
154     }
155    print $input->redirect('aqbookfund.pl'); # FIXME: unnecessary redirect
156    exit;
157 }
158
159 #-----############# DELETE_CONFIRM ##################################
160 # called by default form, used to confirm deletion of data in DB
161 elsif ($op eq 'delete_confirm') {
162     my $data = GetBookFund($bookfundid,$branchcodeid);
163         $template->param(bookfundid   => $bookfundid);
164         $template->param(bookfundname => $data->{'bookfundname'});
165         $template->param(branchcode   => $data->{'branchcode'});
166 }
167 # called by delete_confirm, used to effectively confirm deletion of data in DB
168 elsif ($op eq 'delete_confirmed') {
169     DelBookFund($bookfundid, $branchcodeid);
170 }
171 else { # DEFAULT
172     my ($sth);
173     # filters
174     my @branchloop;
175     foreach my $branchcode (sort keys %{$branches}) {
176         my $row = {
177             code => $branchcode,
178             name => $branches->{$branchcode}->{branchname},
179         };
180         if (defined $input->param('filter_branchcode')
181             and $input->param('filter_branchcode') eq $branchcode) {
182             $row->{selected} = 1;
183         }
184         push @branchloop, $row;
185     }
186
187     my @bookfundids_loop;
188     $sth = GetBookFundsId();
189
190     while (my $row = $sth->fetchrow_hashref) {
191         if (defined $input->param('filter_bookfundid') and $input->param('filter_bookfundid') eq $row->{bookfundid}){
192             $row->{selected} = 1;
193         }
194         push @bookfundids_loop, $row;
195     }
196
197     $template->param(
198         filter_bookfundids  => \@bookfundids_loop,
199         filter_branches     => \@branchloop,
200         filter_bookfundname => $input->param('filter_bookfundname') || undef,
201     );
202
203     # searching the bookfunds corresponding to our filtering rules
204     my @results = SearchBookFund(
205         $input->param('filter'),
206         $input->param('filter_bookfundid'),
207         $input->param('filter_bookfundname'),
208         $input->param('filter_branchcode'),
209     );
210
211     # does the book funds have budgets?
212     my @loop_id;
213     $sth = GetBookFundsId();
214     while (my $row = $sth->fetchrow){
215         push @loop_id, $row;
216     }
217
218     my ($id,%nb_budgets_of);
219     foreach $id (@loop_id){
220         my $number = Countbookfund($id);
221         $nb_budgets_of{$id} = $number;
222     }
223
224     # pagination informations
225     my $page = $input->param('page') || 1;
226     my @loop;
227
228     my $first = ($page - 1) * $pagesize;
229
230     # if we are on the last page, the number of the last word to display
231     # must not exceed the length of the results array
232     my $last = min(
233         $first + $pagesize - 1,
234         scalar(@results) - 1,
235     );
236
237     foreach my $result (@results[$first .. $last]) {
238         push @loop, {
239             %{$result},
240             branchname  => $branches->{ $result->{branchcode} }->{branchname},
241             has_budgets => defined $nb_budgets_of{ $result->{bookfundid} },
242         };
243     }
244
245     $template->param(
246             bookfund => \@loop,
247             pagination_bar => pagination_bar(
248                         $script_name,
249                         getnbpages(scalar @results, $pagesize),
250                         $page,
251                         'page'
252             )
253     );
254 }
255
256 output_html_with_http_headers $input, $cookie, $template->output;