Merge remote-tracking branch 'origin/new/bug_5327'
[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
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 warnings; FIXME - Bug 2505
46 use Number::Format qw(format_price);
47 use CGI;
48 use List::Util qw/min/;
49 use C4::Dates qw/format_date format_date_in_iso/;
50 use C4::Koha;
51 use C4::Context;
52 use C4::Auth;
53 use C4::Output;
54 use C4::Acquisition;
55 use C4::Budgets;
56 use C4::Debug;
57
58 my $dbh = C4::Context->dbh;
59
60 my $input       = new CGI;
61
62 my $searchfield          = $input->param('searchfield');
63 my $budget_period_id     = $input->param('budget_period_id');
64 my $op                   = $input->param('op')||"else";
65
66 my $budget_period_hashref= $input->Vars;
67 #my $sort1_authcat = $input->param('sort1_authcat');
68 #my $sort2_authcat = $input->param('sort2_authcat');
69
70 my $pagesize    = 20;
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
84 my $cur = GetCurrency();
85 $template->param( symbol => $cur->{symbol},
86                   currency => $cur->{currency}
87                );
88 my $cur_format = C4::Context->preference("CurrencyFormat");
89 my $num;
90
91 if ( $cur_format eq 'US' ) {
92     $num = new Number::Format(
93         'int_curr_symbol'   => '',
94         'mon_thousands_sep' => ',',
95         'mon_decimal_point' => '.'
96     );
97 } elsif ( $cur_format eq 'FR' ) {
98     $num = new Number::Format(
99         'decimal_fill'      => '2',
100         'decimal_point'     => ',',
101         'int_curr_symbol'   => '',
102         'mon_thousands_sep' => ' ',
103         'thousands_sep'     => ' ',
104         'mon_decimal_point' => ','
105     );
106 }
107
108
109 # ADD OR MODIFY A BUDGET PERIOD - BUILD SCREEN
110 if ( $op eq 'add_form' ) {
111     ## add or modify a budget period (preparation)
112     ## get information about the budget period that must be modified
113
114
115     if ($budget_period_id) {    # MOD
116                 my $budgetperiod_hash=GetBudgetPeriod($budget_period_id);
117         # get dropboxes
118                 FormatData($budgetperiod_hash);
119
120         my $editnum = new Number::Format(
121             'int_curr_symbol'   => '',
122             'thousands_sep'     => '',
123             'mon_thousands_sep' => '',
124             'mon_decimal_point' => '.'
125         );
126
127         $$budgetperiod_hash{budget_period_total}= $editnum->format_price($$budgetperiod_hash{'budget_period_total'});
128         $template->param(
129                         %$budgetperiod_hash
130         );
131     } # IF-MOD
132     $template->param( DHTMLcalendar_dateformat  => C4::Dates->DHTMLcalendar(),);
133 }
134
135 elsif ( $op eq 'add_validate' ) {
136 ## add or modify a budget period (confirmation)
137
138         ## update budget period data
139         if ( $budget_period_id ne '' ) {
140                 $$budget_period_hashref{$_}||=0 for qw(budget_period_active budget_period_locked);
141                 my $status=ModBudgetPeriod($budget_period_hashref);
142         } 
143         else {    # ELSE ITS AN ADD
144                 my $budget_period_id=AddBudgetPeriod($budget_period_hashref);
145         }
146         $op='else';
147 }
148
149 #--------------------------------------------------
150 elsif ( $op eq 'delete_confirm' ) {
151 ## delete a budget period (preparation)
152     my $dbh = C4::Context->dbh;
153     ## $total = number of records linked to the record that must be deleted
154     my $total = 0;
155     my $data = GetBudgetPeriod( $budget_period_id);
156
157         FormatData($data);
158         $$data{'budget_period_total'}=$num->format_price(  $data->{'budget_period_total'});
159     $template->param(
160                 %$data
161     );
162 }
163
164 elsif ( $op eq 'delete_confirmed' ) {
165 ## delete the budget period record
166
167     my $data = GetBudgetPeriod( $budget_period_id);
168     DelBudgetPeriod($budget_period_id);
169         $op='else';
170 }
171
172 # DEFAULT - DISPLAY AQPERIODS TABLE
173 # -------------------------------------------------------------------
174 # display the list of budget periods
175     my $results = GetBudgetPeriods();
176         $template->param( period_button_only => 1 ) unless (@$results) ;
177     my $page = $input->param('page') || 1;
178     my $first = ( $page - 1 ) * $pagesize;
179     # if we are on the last page, the number of the last word to display
180     # must not exceed the length of the results array
181     my $last = min( $first + $pagesize - 1, scalar @{$results} - 1, );
182     my $toggle = 0;
183     my @period_loop;
184     foreach my $result ( @{$results}[ $first .. $last ] ) {
185         my $budgetperiod = $result;
186                 FormatData($budgetperiod);
187         $budgetperiod->{'budget_period_total'}     = $num->format_price( $budgetperiod->{'budget_period_total'} );
188         $budgetperiod->{budget_active} = 1;
189         push( @period_loop, $budgetperiod );
190     }
191
192     $template->param(
193         period_loop           => \@period_loop,
194                 pagination_bar            => pagination_bar("aqbudgetperiods.pl",getnbpages(scalar(@$results),$pagesize),$page),
195     );
196
197 $template->param($op=>1);
198 output_html_with_http_headers $input, $cookie, $template->output;