improvement/bugfix : search budget
[koha.git] / admin / aqbudgetperiods.pl
1 #!/usr/bin/perl
2
3 # Copyright 2008 BibLibre, BibLibre, Paul POULAIN
4 #                SAN Ouest Provence
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 USA
20
21 =head1 admin/aqbudgetperiods.pl
22
23 script to administer the budget periods table
24  This software is placed under the gnu General Public License, v2 (http://www.gnu.org/licenses/gpl.html)
25
26  ALGO :
27  this script use an $op to know what to do.
28  if $op is empty or none of the above values,
29         - the default screen is build (with all records, or filtered datas).
30         - the   user can clic on add, modify or delete record.
31  if $op=add_form
32         - if primkey exists, this is a modification,so we read the $primkey record
33         - builds the add/modify form
34  if $op=add_validate
35         - the user has just send datas, so we create/modify the record
36  if $op=delete_confirm
37         - we show the record having primkey=$primkey and ask for deletion validation form
38  if $op=delete_confirmed
39         - we delete the record having primkey=$primkey
40
41 =cut
42
43 ## modules
44 use strict;
45 use Number::Format qw(format_price);
46 use CGI;
47 use List::Util qw/min/;
48 use C4::Dates qw/format_date format_date_in_iso/;
49 use C4::Koha;
50 use C4::Context;
51 use C4::Auth;
52 use C4::Output;
53 use C4::Acquisition;
54 use C4::Budgets;
55 use C4::Debug;
56
57 my $dbh = C4::Context->dbh;
58
59 my $input       = new CGI;
60
61 my $searchfield          = $input->param('searchfield');
62 my $budget_period_id     = $input->param('budget_period_id');
63 my $budget_period_active = $input->param('budget_period_active');
64 my $budget_period_locked = $input->param('budget_period_locked');
65 my $op                   = $input->param('op');
66
67 #my $sort1_authcat = $input->param('sort1_authcat');
68 #my $sort2_authcat = $input->param('sort2_authcat');
69
70 my $pagesize    = 10;
71 $searchfield =~ s/\,//g;
72
73 my ($template, $borrowernumber, $cookie, $staff_flags ) = get_template_and_user(
74         {   template_name   => "admin/aqbudgetperiods.tmpl",
75                 query           => $input,
76                 type            => "intranet",
77                 authnotrequired => 0,
78                 flagsrequired   => { acquisition => 'period_manage' },
79                 debug           => 1,
80         }
81 );
82
83 my $script_name = "/cgi-bin/koha/admin/aqbudgetperiods.pl";  # ???
84
85 my ( $count, $results ) = GetBudgetPeriods();
86 ### $count
87 $template->param( period_button_only => 1 ) if ($count == 0) ;
88
89 my $cur = GetCurrency();
90 $template->param( cur => $cur->{symbol} );
91 my $cur_format = C4::Context->preference("CurrencyFormat");
92 my $num;
93
94 if ( $cur_format eq 'US' ) {
95     $num = new Number::Format(
96         'int_curr_symbol'   => '',
97         'mon_thousands_sep' => ',',
98         'mon_decimal_point' => '.'
99     );
100 } elsif ( $cur_format eq 'FR' ) {
101     $num = new Number::Format(
102         'decimal_fill'      => '2',
103         'decimal_point'     => ',',
104         'int_curr_symbol'   => '',
105         'mon_thousands_sep' => ' ',
106         'thousands_sep'     => ' ',
107         'mon_decimal_point' => ','
108     );
109 }
110
111 if   ($op) { $template->param( $op    => 1 ); }
112 else       { $template->param( 'else' => 1 ); }
113
114 # ADD OR MODIFY A BUDGET PERIOD - BUILD SCREEN
115 if ( $op eq 'add_form' ) {
116     ## add or modify a budget period (preparation)
117     ## get information about the budget period that must be modified
118
119 #    my ( $default, $sort1_authcat_dropbox, $sort1_default, $sort2_default );
120 #    my ( $default, t );
121
122     if ($budget_period_id) {    # MOD
123         my $data;
124         my $dbh = C4::Context->dbh;
125         my $sth = $dbh->prepare(qq|
126                                         SELECT * FROM aqbudgetperiods
127                                         WHERE budget_period_id=?    | );
128         $sth->execute($budget_period_id);
129         $data = $sth->fetchrow_hashref;
130         $sth->finish;
131
132         # get dropboxes
133         $template->param(
134             budget_period_id          => $budget_period_id,
135             budget_period_startdate   => format_date( $data->{'budget_period_startdate'} ),
136             budget_period_enddate     => format_date( $data->{'budget_period_enddate'} ),
137             budget_period_description => $data->{'budget_period_description'},
138             budget_period_total       => sprintf ("%.2f",  $data->{'budget_period_total'} ),
139             budget_period_active      => $data->{'budget_period_active'},
140             budget_period_locked      => $data->{'budget_period_locked'},
141         );
142     } # IF-MOD
143     $template->param(              DHTMLcalendar_dateformat => C4::Dates->DHTMLcalendar(),     );
144 }
145
146 elsif ( $op eq 'add_validate' ) {
147 ## add or modify a budget period (confirmation)
148
149     ## update budget period data
150     if ( $budget_period_id ne '' ) {
151         my $query = '
152                 UPDATE aqbudgetperiods
153                 SET    budget_period_startdate  = ?
154                     , budget_period_enddate     = ?
155                     , budget_period_description = ?
156                     , budget_period_total       = ?
157                     , budget_period_locked      = ?
158                     , budget_period_active      = ?
159                 WHERE budget_period_id          = ?
160             ';
161
162         my $sth = $dbh->prepare($query);
163         $sth->execute(
164             $input->param('budget_period_startdate')   ? format_date_in_iso( $input->param('budget_period_startdate') ) : undef,
165             $input->param('budget_period_enddate')     ? format_date_in_iso( $input->param('budget_period_enddate') )   : undef,
166             $input->param('budget_period_description') ? $input->param('budget_period_description')                     : undef,
167             $input->param('budget_period_total')       ? $input->param('budget_period_total')                           : undef,
168             $input->param('budget_period_locked')      ? $input->param('budget_period_locked')                          : undef,
169             $input->param('budget_period_active')      ? $input->param('budget_period_active')                          : undef,
170             $input->param('budget_period_id'),
171         );
172
173     } else {    # ELSE ITS AN ADD
174         my $query = "
175                 INSERT INTO aqbudgetperiods (
176                     budget_period_id
177                     , budget_period_startdate
178                     , budget_period_enddate
179                     , budget_period_total
180                     , budget_period_description
181                     , budget_period_locked
182                     , budget_period_active)
183                 VALUES  (?,?,?,?,?,?,? );
184             ";
185         my $sth = $dbh->prepare($query);
186         $sth->execute(
187             $budget_period_id,
188             $input->param('budget_period_startdate')   ? format_date_in_iso( $input->param('budget_period_startdate') ) : undef,
189             $input->param('budget_period_enddate')     ? format_date_in_iso( $input->param('budget_period_enddate') )   : undef,
190             $input->param('budget_period_total')       ? $input->param('budget_period_total')                           : undef,
191             $input->param('budget_period_description') ? $input->param('budget_period_description')                     : undef,
192             $input->param('budget_period_locked')      ? $input->param('budget_period_locked')                          : undef,
193             $input->param('budget_period_active')      ? $input->param('budget_period_active')                          : undef,
194         );
195         $budget_period_id = $dbh->last_insert_id( undef, undef, 'aqbudgetperiods', undef );
196     }
197
198     print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=aqbudgetperiods.pl\"></html>";    #YUCK
199     #    output_html_with_http_headers $input, $cookie, $template->output;   # FIXME: THIS WOULD BE NICER THAN THE PREVIOUS PRINT
200     exit;
201 }
202
203 #--------------------------------------------------
204 elsif ( $op eq 'delete_confirm' ) {
205 ## delete a budget period (preparation)
206     my $dbh = C4::Context->dbh;
207     ## $total = number of records linked to the record that must be deleted
208     my $total = 0;
209     my $data = GetBudgetPeriod( $budget_period_id);
210
211     $template->param(
212             budget_period_id            => $budget_period_id,
213             budget_period_startdate     => format_date($data->{'budget_period_startdate'}),
214             budget_period_enddate       => format_date($data->{'budget_period_enddate'}),
215             budget_period_total         => $num->format_price(  $data->{'budget_period_total'}   )
216
217 #            budget_period_active            => $data->{'budget_period_active'},
218 #            budget_period_description    => $data->{'budget_period_description'},
219 #            template                    => C4::Context->preference('template'),  ##  ??!?
220     );
221 }
222
223 elsif ( $op eq 'delete_confirmed' ) {
224 ## delete the budget period record
225
226     my $dbh              = C4::Context->dbh;
227     my $budget_period_id = uc( $input->param('budget_period_id') );
228     my $sth              = $dbh->prepare("DELETE FROM aqbudgetperiods WHERE budget_period_id=?");
229     $sth->execute($budget_period_id);
230     $sth->finish;
231     print "Content-Type: text/html\n\n<META HTTP-EQUIV=Refresh CONTENT=\"0; URL=aqbudgetperiods.pl\"></html>";
232     exit;
233 }
234
235 else {
236
237 # DEFAULT - DISPLAY AQPERIODS TABLE
238 # -------------------------------------------------------------------
239 # display the list of budget periods
240     my ( $count, $results ) = GetBudgetPeriods();
241     my $page = $input->param('page') || 1;
242     my $first = ( $page - 1 ) * $pagesize;
243
244     # if we are on the last page, the number of the last word to display
245     # must not exceed the length of the results array
246     my $last = min( $first + $pagesize - 1, scalar @{$results} - 1, );
247     my $toggle = 0;
248     my @period_loop;
249     foreach my $result ( @{$results}[ $first .. $last ] ) {
250         my $budgetperiod = $result;
251         $budgetperiod->{'budget_period_startdate'} = format_date( $budgetperiod->{'budget_period_startdate'} );
252         $budgetperiod->{'budget_period_enddate'}   = format_date( $budgetperiod->{'budget_period_enddate'} );
253         $budgetperiod->{'budget_period_total'}     = $num->format_price( $budgetperiod->{'budget_period_total'} );
254         $budgetperiod->{toggle} = ( $toggle++ % 2 eq 0 ? 1 : 0 );
255         $budgetperiod->{budget_active} = 1;
256         push( @period_loop, $budgetperiod );
257     }
258     my $budget_period_dropbox = GetBudgetPeriodsDropbox();
259
260     $template->param(
261         budget_period_dropbox => $budget_period_dropbox,
262         period_loop           => \@period_loop,
263 #        pagination_bar        => pagination_bar( $script_name, 
264 #                                                getnbpages( scalar @{$results}, 
265 #                                                $pagesize ), $page, 'page' )
266     );
267 }
268
269 output_html_with_http_headers $input, $cookie, $template->output;