Bug 12844: Use Koha::Number::Price where it can be useful
[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  if $op=duplicate_form
41   - displays the duplication of budget period form (allowing specification of dates)
42  if $op=duplicate_budget
43   - we perform the duplication of the budget period specified as budget_period_id
44
45 =cut
46
47 use Modern::Perl;
48
49 use Number::Format qw(format_price);
50 use CGI;
51 use List::Util qw/min/;
52 use Koha::DateUtils;
53 use Koha::Database;
54 use C4::Koha;
55 use C4::Context;
56 use C4::Auth;
57 use C4::Output;
58 use C4::Acquisition;
59 use C4::Budgets;
60 use C4::Debug;
61
62 my $dbh = C4::Context->dbh;
63
64 my $input       = new CGI;
65
66 my $searchfield          = $input->param('searchfield') // '';
67 my $budget_period_id     = $input->param('budget_period_id');
68 my $op                   = $input->param('op')||"else";
69 #my $sort1_authcat = $input->param('sort1_authcat');
70 #my $sort2_authcat = $input->param('sort2_authcat');
71
72 # get only the columns of aqbudgetperiods in budget_period_hashref
73 my @columns = Koha::Database->new()->schema->source('Aqbudgetperiod')->columns;
74 my $budget_period_hashref = { map { join(' ',@columns) =~ /$_/ ? ( $_ => $input->param($_) )  : () } keys( %{$input->Vars()} ) } ;
75 $budget_period_hashref->{budget_period_startdate} = dt_from_string( $input->param('budget_period_startdate') );
76 $budget_period_hashref->{budget_period_enddate}   = dt_from_string( $input->param('budget_period_enddate') );
77
78 my $activepagesize = 20;
79 my $inactivepagesize = 20;
80 $searchfield =~ s/\,//g;
81
82 my ($template, $borrowernumber, $cookie, $staff_flags ) = get_template_and_user(
83     {
84         template_name   => "admin/aqbudgetperiods.tt",
85         query           => $input,
86         type            => "intranet",
87         authnotrequired => 0,
88         flagsrequired   => { acquisition => 'period_manage' },
89         debug           => 1,
90     }
91 );
92
93
94 # This is used in incbudgets-active-currency.inc
95 my $cur = GetCurrency();
96 $template->param( symbol => $cur->{symbol},
97                   currency => $cur->{currency}
98                );
99
100 # ADD OR MODIFY A BUDGET PERIOD - BUILD SCREEN
101 if ( $op eq 'add_form' ) {
102     ## add or modify a budget period (preparation)
103     ## get information about the budget period that must be modified
104
105     if ($budget_period_id) {    # MOD
106                 my $budgetperiod_hash=GetBudgetPeriod($budget_period_id);
107         # get dropboxes
108
109         my $editnum = new Number::Format(
110             'int_curr_symbol'   => '',
111             'thousands_sep'     => '',
112             'mon_thousands_sep' => '',
113             'mon_decimal_point' => '.'
114         );
115
116         $$budgetperiod_hash{budget_period_total}= $editnum->format_price($$budgetperiod_hash{'budget_period_total'});
117         $template->param(
118                         %$budgetperiod_hash
119         );
120     } # IF-MOD
121 }
122
123 elsif ( $op eq 'add_validate' ) {
124 ## add or modify a budget period (confirmation)
125
126     ## update budget period data
127         if ( $budget_period_id ne '' ) {
128                 $$budget_period_hashref{$_}||=0 for qw(budget_period_active budget_period_locked);
129                 my $status=ModBudgetPeriod($budget_period_hashref);
130         } 
131         else {    # ELSE ITS AN ADD
132                 my $budget_period_id=AddBudgetPeriod($budget_period_hashref);
133         }
134         $op='else';
135 }
136
137 #--------------------------------------------------
138 elsif ( $op eq 'delete_confirm' ) {
139 ## delete a budget period (preparation)
140     my $dbh = C4::Context->dbh;
141     ## $total = number of records linked to the record that must be deleted
142     my $total = 0;
143     my $data = GetBudgetPeriod( $budget_period_id);
144
145     $template->param(
146                 %$data
147     );
148 }
149
150 elsif ( $op eq 'delete_confirmed' ) {
151 ## delete the budget period record
152
153     my $data = GetBudgetPeriod( $budget_period_id);
154     DelBudgetPeriod($budget_period_id);
155         $op='else';
156 }
157
158 # display the form for duplicating
159 elsif ( $op eq 'duplicate_form'){
160     my $budgetperiod = GetBudgetPeriod($budget_period_id, $input);
161     $template->param(
162         'duplicate_form' => '1',
163         'budget_period_id' => $budget_period_id,
164         'budgetperiod' => $budgetperiod,
165     );
166 }
167
168 # handle the actual duplication
169 elsif ( $op eq 'duplicate_budget' ){
170     die "please specify a budget period id\n" if( !defined $budget_period_id || $budget_period_id eq '' );
171
172     my $budget_period_startdate = dt_from_string $input->param('budget_period_startdate');
173     my $budget_period_enddate   = dt_from_string $input->param('budget_period_enddate');
174     my $budget_period_description = $input->param('budget_period_description');
175     my $mark_original_budget_as_inactive = $input->param('mark_original_budget_as_inactive');
176     my $reset_all_budgets = $input->param('reset_all_budgets');
177
178     my $new_budget_period_id = C4::Budgets::CloneBudgetPeriod(
179         {
180             budget_period_id        => $budget_period_id,
181             budget_period_startdate => $budget_period_startdate,
182             budget_period_enddate   => $budget_period_enddate,
183             budget_period_description => $budget_period_description,
184             mark_original_budget_as_inactive => $mark_original_budget_as_inactive,
185             reset_all_budgets => $reset_all_budgets,
186         }
187     );
188
189     # display the list of budgets
190     $op = 'else';
191 }
192
193 elsif ( $op eq 'close_form' ) {
194
195     my $budget_period = GetBudgetPeriod($budget_period_id);
196
197     my $active_budget_periods =
198       C4::Budgets::GetBudgetPeriods( { budget_period_active => 1 } );
199
200     # Remove the budget period from the list
201     $active_budget_periods =
202       [ map { ( $_->{budget_period_id} == $budget_period_id ) ? () : $_ }
203           @$active_budget_periods ];
204
205     my $budgets_to_move = GetBudgetHierarchy($budget_period_id);
206
207     # C4::Context->userenv->{branchcode}, $show_mine ? $borrower_id : '')
208
209     my $number_of_unreceived_orders = 0;
210     for my $budget (@$budgets_to_move) {
211
212         # We want to move funds from this budget
213         my $unreceived_orders = C4::Acquisition::SearchOrders(
214             {
215                 budget_id => $budget->{budget_id},
216                 pending   => 1,
217             }
218         );
219         $budget->{unreceived_orders} = $unreceived_orders;
220         $number_of_unreceived_orders += scalar(@$unreceived_orders);
221     }
222
223     $template->param(
224         close_form       => 1,
225         budget_period_id => $budget_period_id,
226         budget_period_description =>
227           $budget_period->{budget_period_description},
228         budget_periods              => $active_budget_periods,
229         budgets_to_move             => $budgets_to_move,
230         number_of_unreceived_orders => $number_of_unreceived_orders,
231     );
232 }
233
234 elsif ( $op eq 'close_confirmed' ) {
235     my $to_budget_period_id    = $input->param('to_budget_period_id');
236     my $move_remaining_unspent = $input->param('move_remaining_unspent');
237     my $report                 = C4::Budgets::MoveOrders(
238         {
239             from_budget_period_id  => $budget_period_id,
240             to_budget_period_id    => $to_budget_period_id,
241             move_remaining_unspent => $move_remaining_unspent,
242         }
243     );
244
245     my $from_budget_period = GetBudgetPeriod($budget_period_id);
246     my $to_budget_period   = GetBudgetPeriod($to_budget_period_id);
247     $template->param(
248         closed           => 1,
249         budget_period_id => $from_budget_period->{budget_period_id},
250         budget_period_description => $from_budget_period->{budget_period_description},
251         from_budget_period => $from_budget_period,
252         to_budget_period   => $to_budget_period,
253         report             => $report,
254     );
255 }
256
257 # DEFAULT - DISPLAY AQPERIODS TABLE
258 # -------------------------------------------------------------------
259 # display the list of budget periods
260
261 my $activepage = $input->param('apage') || 1;
262 my $inactivepage = $input->param('ipage') || 1;
263 # Get active budget periods
264 my $results = GetBudgetPeriods(
265     { budget_period_active => 1 },
266     { -asc => 'budget_period_description' },
267 );
268 my $first = ( $activepage - 1 ) * $activepagesize;
269 my $last = min( $first + $activepagesize - 1, scalar @{$results} - 1, );
270 my @period_active_loop;
271
272 foreach my $result ( @{$results}[ $first .. $last ] ) {
273     my $budgetperiod = $result;
274     $budgetperiod->{budget_active} = 1;
275     push( @period_active_loop, $budgetperiod );
276 }
277 my $url = "aqbudgetperiods.pl";
278 $url .=  "?ipage=$inactivepage" if($inactivepage != 1);
279 my $active_pagination_bar = pagination_bar ($url, getnbpages( scalar(@$results), $activepagesize), $activepage, "apage");
280
281 # Get inactive budget periods
282 $results = GetBudgetPeriods(
283     { budget_period_active => 0 },
284     { -desc => 'budget_period_enddate' },
285 );
286
287 $first = ( $inactivepage - 1 ) * $inactivepagesize;
288 $last = min( $first + $inactivepagesize - 1, scalar @{$results} - 1, );
289 my @period_inactive_loop;
290 foreach my $result ( @{$results}[ $first .. $last ] ) {
291     my $budgetperiod = $result;
292     $budgetperiod->{budget_active} = 1;
293     push( @period_inactive_loop, $budgetperiod );
294 }
295 $url = "aqbudgetperiods.pl?tab=2";
296 $url .= "&apage=$activepage" if($activepage != 1);
297 my $inactive_pagination_bar = pagination_bar ($url, getnbpages( scalar(@$results), $inactivepagesize), $inactivepage, "ipage");
298
299 my $tab = $input->param('tab') ? $input->param('tab') - 1 : 0;
300 $template->param(
301     period_active_loop      => \@period_active_loop,
302     period_inactive_loop    => \@period_inactive_loop,
303     active_pagination_bar   => $active_pagination_bar,
304     inactive_pagination_bar => $inactive_pagination_bar,
305     tab                     => $tab,
306 );
307
308 $template->param($op=>1);
309 output_html_with_http_headers $input, $cookie, $template->output;