Merge commit 'pianohacker-koha/prefs-submit' into master
[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 $op                   = $input->param('op')||"else";
64 my $check_duplicate      = $input->param('confirm_not_duplicate')||0;
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( cur => $cur->{symbol} );
86 my $cur_format = C4::Context->preference("CurrencyFormat");
87 my $num;
88
89 if ( $cur_format eq 'US' ) {
90     $num = new Number::Format(
91         'int_curr_symbol'   => '',
92         'mon_thousands_sep' => ',',
93         'mon_decimal_point' => '.'
94     );
95 } elsif ( $cur_format eq 'FR' ) {
96     $num = new Number::Format(
97         'decimal_fill'      => '2',
98         'decimal_point'     => ',',
99         'int_curr_symbol'   => '',
100         'mon_thousands_sep' => ' ',
101         'thousands_sep'     => ' ',
102         'mon_decimal_point' => ','
103     );
104 }
105
106
107 # ADD OR MODIFY A BUDGET PERIOD - BUILD SCREEN
108 if ( $op eq 'add_form' ) {
109     ## add or modify a budget period (preparation)
110     ## get information about the budget period that must be modified
111
112
113     if ($budget_period_id) {    # MOD
114                 my $budgetperiod_hash=GetBudgetPeriod($budget_period_id);
115         # get dropboxes
116                 FormatData($budgetperiod_hash);
117
118         my $editnum = new Number::Format(
119             'int_curr_symbol'   => '',
120             'thousands_sep'     => '',
121             'mon_decimal_point' => '.'
122         );
123
124         $$budgetperiod_hash{budget_period_total}= $editnum->format_price($$budgetperiod_hash{'budget_period_total'});
125         $template->param(
126                         %$budgetperiod_hash
127         );
128     } # IF-MOD
129     $template->param( DHTMLcalendar_dateformat  => C4::Dates->DHTMLcalendar(),);
130     $template->param( confirm_not_duplicate             => $check_duplicate               );
131 }
132
133 elsif ( $op eq 'add_validate' ) {
134 ## add or modify a budget period (confirmation)
135
136         ## update budget period data
137         if ( $budget_period_id ne '' ) {
138                 $$budget_period_hashref{$_}||=0 for qw(budget_period_active budget_period_locked);
139                 my $status=ModBudgetPeriod($budget_period_hashref);
140         } 
141         else {    # ELSE ITS AN ADD
142                 unless ($check_duplicate){
143                         my $candidates=GetBudgetPeriods({ 
144                                                                                         budget_period_startdate => $$budget_period_hashref{budget_period_startdate}
145                                                                                         , budget_period_enddate => $$budget_period_hashref{budget_period_enddate}
146                                                                                         });
147                         if (@$candidates){
148                                 my @duplicates=map{
149                                                                         { dupid                            => $$_{budget_period_id}
150                                                                         , duplicateinformation =>
151                                                                                         $$_{budget_period_description}." ".$$_{budget_period_startdate}." ".$$_{budget_period_enddate}
152                                                                         }
153                                                                   } @$candidates;
154                                 $template->param(url                      => "aqbudgetperiods.pl", 
155                                                                 field_name                => "budget_period_id",
156                                                                 action_dup_yes_url=> "aqbudgets.pl",
157                                                                 action_dup_no_url => "aqbudgetperiods.pl?op=add_validate",
158                                                                 confirm_not_duplicate     => 0
159                                                                         );
160                                 delete $$budget_period_hashref{budget_period_id};
161                                 $template->param(duplicates=>\@duplicates,%$budget_period_hashref);
162                                 $template->param("add_form"=>1);
163                                 output_html_with_http_headers $input, $cookie, $template->output;
164                                 exit;
165                         }
166                 }
167                 my $budget_period_id=AddBudgetPeriod($budget_period_hashref);
168         }
169         $op='else';
170 }
171
172 #--------------------------------------------------
173 elsif ( $op eq 'delete_confirm' ) {
174 ## delete a budget period (preparation)
175     my $dbh = C4::Context->dbh;
176     ## $total = number of records linked to the record that must be deleted
177     my $total = 0;
178     my $data = GetBudgetPeriod( $budget_period_id);
179
180         FormatData($data);
181         $$data{'budget_period_total'}=$num->format_price(  $data->{'budget_period_total'});
182     $template->param(
183                 %$data
184     );
185 }
186
187 elsif ( $op eq 'delete_confirmed' ) {
188 ## delete the budget period record
189
190     my $data = GetBudgetPeriod( $budget_period_id);
191     DelBudgetPeriod($budget_period_id);
192         $op='else';
193 }
194
195 # DEFAULT - DISPLAY AQPERIODS TABLE
196 # -------------------------------------------------------------------
197 # display the list of budget periods
198     my $results = GetBudgetPeriods();
199         $template->param( period_button_only => 1 ) unless (@$results) ;
200     my $page = $input->param('page') || 1;
201     my $first = ( $page - 1 ) * $pagesize;
202     # if we are on the last page, the number of the last word to display
203     # must not exceed the length of the results array
204     my $last = min( $first + $pagesize - 1, scalar @{$results} - 1, );
205     my $toggle = 0;
206     my @period_loop;
207     foreach my $result ( @{$results}[ $first .. $last ] ) {
208         my $budgetperiod = $result;
209                 FormatData($budgetperiod);
210         $budgetperiod->{'budget_period_total'}     = $num->format_price( $budgetperiod->{'budget_period_total'} );
211         $budgetperiod->{budget_active} = 1;
212         push( @period_loop, $budgetperiod );
213     }
214     my $budget_period_dropbox = GetBudgetPeriodsDropbox();
215
216     $template->param(
217         budget_period_dropbox => $budget_period_dropbox,
218         period_loop           => \@period_loop,
219                 pagination_bar            => pagination_bar("aqbudgetperiods.pl",getnbpages(scalar(@$results),$pagesize),$page),
220     );
221
222 $template->param($op=>1);
223 output_html_with_http_headers $input, $cookie, $template->output;