[MT2343] Select in budget add forum
[koha.git] / admin / aqbudgets.pl
1 #!/usr/bin/perl
2
3 #script to administer the aqbudget table
4
5 # Copyright 2008-2009 BibLibre SARL
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 use strict;
23 use CGI;
24 use List::Util qw/min/;
25 use Number::Format qw(format_price);
26
27 use C4::Auth qw/get_user_subpermissions/;
28 use C4::Branch; # GetBranches
29 use C4::Dates qw/format_date format_date_in_iso/;
30 use C4::Auth;
31 use C4::Acquisition;
32 use C4::Budgets;   #
33 use C4::Members;  # calls GetSortDetails()
34 use C4::Context;
35 use C4::Output;
36 use C4::Koha;
37 use C4::Debug;
38 #use POSIX qw(locale_h);
39
40 my $input = new CGI;
41 my $dbh     = C4::Context->dbh;
42
43 my ($template, $borrowernumber, $cookie, $staffflags ) = get_template_and_user(
44     {   template_name   => "admin/aqbudgets.tmpl",
45         query           => $input,
46         type            => "intranet",
47         authnotrequired => 0,
48         flagsrequired   => { acquisition => 'budget_manage' },
49         debug           => 0,
50     }
51 );
52
53 my $op = $input->param('op');
54
55 # see if the user want to see all budgets or only owned ones
56 my $show_mine    = 1; #SHOW BY DEFAULT
57 my $show         = $input->param('show'); # SET TO 1, BY A FORM SUMBIT
58 $show_mine       = $input->param('show_mine') if $show == 1;
59
60 # IF USER DOESNT HAVE PERM FOR AN 'ADD', THEN REDIRECT TO THE DEFAULT VIEW...
61 if  (  not defined $template->{param_map}->{'CAN_user_acquisition_budget_add_del'}  &&  $op ==  'add_form'  )   {
62     $op = '';
63 }
64
65 my $num=FormatNumber;
66
67 my $script_name               = "/cgi-bin/koha/admin/aqbudgets.pl";
68 my $budget_hash=$input->Vars;
69 my $budget_id                 = $$budget_hash{budget_id};
70 my $budget_permission         = $input->param('budget_permission');
71 my $budget_period_dropbox     = $input->param('budget_period_dropbox');
72 #filtering non budget keys
73 delete $$budget_hash{$_} foreach grep {/filter|^op$|show/} keys %$budget_hash;
74 my $filter_budgetbranch       = $input->param('filter_budgetbranch');
75 my $filter_budgetname         = $input->param('filter_budgetname');
76 $template->param(
77     notree => ($filter_budgetbranch or $show_mine)
78 );
79 # ' ------- get periods stuff ------------------'
80 # IF PERIODID IS DEFINED,  GET THE PERIOD - ELSE JUST GET THE ACTIVE PERIOD BY DEFAULT
81 my $period = GetBudgetPeriod($$budget_hash{budget_period_id});
82
83 $template->param(
84         %$period
85 );
86 # ------- get periods stuff ------------------
87
88 # USED FOR PERMISSION COMPARISON LATER
89 my $borrower_id         = $template->{param_map}->{'USER_INFO'}[0]->{'borrowernumber'};
90 my $user                = GetMemberDetails($borrower_id);
91 my $user_branchcode     = $user->{'branchcode'};
92
93 $template->param(
94     action      => $script_name,
95     script_name => $script_name,
96     show_mine   => $show_mine,
97     $op || else => 1,
98 );
99
100
101 # retrieve branches
102 my ( $budget, );
103
104 my $branches = GetBranches($show_mine);
105 my @branchloop2;
106 foreach my $thisbranch (keys %$branches) {
107     my %row = (
108         value      => $thisbranch,
109         branchname => $branches->{$thisbranch}->{'branchname'},
110     );
111     $row{selected} = 1 if $thisbranch eq $filter_budgetbranch;
112     push @branchloop2, \%row;
113 }
114
115 $template->param(auth_cats_loop => GetBudgetAuthCats($$period{budget_period_id}) );
116
117 # Used to create form to add or  modify a record
118 if ($op eq 'add_form') {
119 #### ------------------- ADD_FORM -------------------------
120
121     # if no buget_id is passed then its an add
122     #  pass the period_id to build the dropbox - because we only want to show  budgets from this period
123     my $dropbox_disabled;
124     if (defined $budget_id ) {    ### MOD
125         $budget           = GetBudget($budget_id);
126         $dropbox_disabled = BudgetHasChildren($budget_id);
127         my $borrower = &GetMember( borrowernumber=>$budget->{budget_owner_id} );
128         $budget->{budget_owner_name} = $borrower->{'firstname'} . ' ' . $borrower->{'surname'};
129         $$budget{$_}= sprintf("%.2f", $budget->{$_}) for grep{/amount/} keys %$budget;
130     }
131
132     # build budget hierarchy
133     my %labels;
134     my @values;
135     my $hier = GetBudgetHierarchy($$period{budget_period_id});
136     foreach my $r (@$hier) {
137         $r->{budget_code_indent} =~ s/ /\~/g;    #
138         $labels{"$r->{budget_id}"} = $r->{budget_code_indent};
139         push @values, $r->{budget_id};
140     }
141     push @values, '';
142     # if no buget_id is passed then its an add
143     my $budget_parent;
144     my $budget_parent_id;
145     if ($budget){
146         $budget_parent_id = $budget->{'budget_parent_id'} ;
147     }else{
148         $budget_parent_id = $input->param('budget_parent_id');
149     }
150     $budget_parent = GetBudget($budget_parent_id);
151
152     # build branches select
153     my $branches = GetBranches;
154     my @branchloop_select;
155     foreach my $thisbranch ( sort keys %$branches ) {
156         my %row = (
157             value      => $thisbranch,
158             branchname => $branches->{$thisbranch}->{'branchname'},
159         );
160         $row{selected} = 1 if $thisbranch eq $budget->{'budget_branchcode'};
161         push @branchloop_select, \%row;
162     }
163     
164     # populates the YUI planning button
165     my $categories = GetAuthorisedValueCategories();
166     my @auth_cats_loop1 = ();
167     foreach my $category (@$categories) {
168         my $entry = { category => $category,
169                         selected => $budget->{sort1_authcat} eq $category ?1:0,
170                     };
171         push @auth_cats_loop1, $entry;
172     }
173     my @auth_cats_loop2 = ();
174     foreach my $category (@$categories) {
175         my $entry = { category => $category,
176                         selected => $budget->{sort2_authcat} eq $category ?1:0,
177                     };
178         push @auth_cats_loop2, $entry;
179     }
180     $template->param(authorised_value_categories1 => \@auth_cats_loop1);
181     $template->param(authorised_value_categories2 => \@auth_cats_loop2);
182
183     my $budget_perm_dropbox =
184     GetBudgetPermDropbox($budget->{'budget_permission'});
185     
186     # if no buget_id is passed then its an add
187     $template->param(
188         add_validate                  => 1,
189         dateformat                => C4::Dates->new()->visual(),
190         budget_parent_id                  => $budget_parent->{'budget_id'},
191         budget_parent_name                => $budget_parent->{'budget_name'},
192         budget_perm_dropbox       => $budget_perm_dropbox,
193         branchloop_select         => \@branchloop_select,
194                 %$period,
195                 %$budget,
196     );
197                                                     # END $OP eq ADD_FORM
198 #---------------------- DEFAULT DISPLAY BELOW ---------------------
199
200 # called by default form, used to confirm deletion of data in DB
201 } elsif ($op eq 'delete_confirm') {
202
203     my $budget = GetBudget($budget_id);
204     $template->param(
205         budget_id     => $budget->{'budget_id'},
206         budget_code   => $budget->{'budget_code'},
207         budget_name   => $budget->{'budget_name'},
208         budget_amount => $num->format_price(  $budget->{'budget_amount'} ),
209     );
210                                                     # END $OP eq DELETE_CONFIRM
211 # called by delete_confirm, used to effectively confirm deletion of data in DB
212 }  else{
213     if ( $op eq 'delete_confirmed' ) {
214         my $rc = DelBudget($budget_id);
215     }elsif( $op eq 'add_validate' ) {
216         if ( defined $$budget_hash{budget_id} ) {
217             ModBudget( $budget_hash );
218         } else {
219             AddBudget( $budget_hash );
220         }
221     }            
222     my $branches = GetBranches();
223     my $budget_period_dropbox = GetBudgetPeriodsDropbox($$period{budget_period_id} );
224     $template->param(
225         budget_period_dropbox     => $budget_period_dropbox,
226         budget_id                 => $budget_id,
227                 %$period,
228     );
229
230     my $moo = GetBudgetHierarchy($$period{budget_period_id}, C4::Context->userenv->{branchcode}, $show_mine?$borrower_id:'');
231     my @budgets = @$moo; #FIXME
232
233     my $toggle = 0;
234     my @loop;
235     my $period_total = 0;
236     my ( $period_alloc_total, $base_alloc_total, $base_spent_total, $base_remaining_total );
237
238         use YAML;
239         $debug && warn Dump(@budgets);
240         #This Looks WEIRD to me : should budgets be filtered in such a way ppl who donot own it would not see the amount spent on the budget by others ?
241
242     foreach my $budget (@budgets) {
243         # PERMISSIONS
244         unless($staffflags->{'superlibrarian'} % 2   == 1 ) {
245             #IF NO PERMS, THEN DISABLE EDIT/DELETE
246             unless ( $template->{param_map}->{'CAN_user_acquisition_budget_modify'} ) {
247                 $budget->{'budget_lock'} = 1;
248             }
249             # check budget permission
250             if ( $$period{budget_period_locked} == 1 ) {
251                 $budget->{'budget_lock'} = 1;
252
253             } elsif ( $budget->{budget_permission} == 1 ) {
254
255                 if ( $borrower_id != $budget->{'budget_owner_id'} ) {
256                     $budget->{'budget_lock'} = 1;
257                 }
258                 # check parent perms too
259                 my $parents_perm = 0;
260                 if ( $budget->{depth} > 0 ) {
261                     $parents_perm = CheckBudgetParentPerm( $budget, $borrower_id );
262                     delete $budget->{'budget_lock'} if $parents_perm == '1';
263                 }
264             } elsif ( $budget->{budget_permission} == 2 ) {
265
266                 $budget->{'budget_lock'} = 1 if $user_branchcode ne $budget->{budget_branchcode};
267             }
268         }    # ...SUPER_LIB END
269
270         # if a budget search doesnt match, next
271         if ($filter_budgetname ) {
272             next unless  $budget->{budget_code}  =~ m/$filter_budgetname/  ||
273             $budget->{name}  =~ m/$filter_budgetname/ ;
274         }
275         if ($filter_budgetbranch ) {
276             next unless  $budget->{budget_branchcode}  =~ m/$filter_budgetbranch/;
277         }
278
279 ## TOTALS
280         # adds to total  - only if budget is a 'top-level' budget
281         $period_alloc_total += $budget->{'budget_amount_total'} if $budget->{'depth'} == 0;
282         $base_alloc_total += $budget->{'budget_amount'};
283         $base_spent_total += $budget->{'budget_spent'};
284         $budget->{'budget_remaining'} = $budget->{'budget_amount'} - $budget->{'budget_spent'};
285         $base_remaining_total += $budget->{'budget_remaining'};
286
287 # if amount == 0 dont display...
288         delete  $budget->{'budget_unalloc_sublevel'} if  $budget->{'budget_unalloc_sublevel'} == 0 ;
289
290         $budget->{'remaining_pos'} = 1 if $budget->{'budget_remaining'} > 0;
291         $budget->{'remaining_neg'} = 1 if $budget->{'budget_remaining'} < 0;
292                 for (grep {/budget_spent|budget_amount|budget_remaining|budget_unalloc/} keys %$budget){
293         $$budget{$_}               = $num->format_price( $$budget{$_} ) if defined($$budget{$_})
294                 }
295
296         my $borrower = &GetMember( borrowernumber=>$budget->{budget_owner_id} );
297         $budget->{"budget_owner_name"}     = $borrower->{'firstname'} . ' ' . $borrower->{'surname'};
298         $budget->{"budget_borrowernumber"} = $borrower->{'borrowernumber'};
299
300         push( @loop, {  %{$budget},
301                         branchname  => $branches->{ $budget->{branchcode} }->{branchname},
302                     }
303         );
304     }
305
306     my $budget_period_total = $num->format_price($$period{budget_period_total}) if $$period{budget_period_total};
307     $template->param(
308         else                   => 1,
309         budget                 => \@loop,
310         budget_period_total    => $budget_period_total,
311         period_alloc_total     => $num->format_price($period_alloc_total),
312         base_alloc_total       => $num->format_price($base_alloc_total),
313         base_spent_total       => $num->format_price($base_spent_total),
314         base_remaining_total   => $num->format_price($base_remaining_total),
315         period_remaining_total => $num->format_price( $period_alloc_total - $base_alloc_total ),
316         branchloop             => \@branchloop2,
317     );
318
319 } #---- END $OP eq DEFAULT
320
321 output_html_with_http_headers $input, $cookie, $template->output;