3 #script to administer the aqbudget table
5 # Copyright 2008-2009 BibLibre SARL
7 # This file is part of Koha.
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
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.
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
24 use List::Util qw/min/;
25 use Number::Format qw(format_price);
27 use C4::Auth qw/get_user_subpermissions/;
28 use C4::Branch; # GetBranches
29 use C4::Dates qw/format_date format_date_in_iso/;
32 use C4::Budgets; # 1ST ADD
33 use C4::Members; # calls GetSortDetails()
38 #use POSIX qw(locale_h);
41 my $dbh = C4::Context->dbh;
43 my ($template, $borrowernumber, $cookie, $staffflags ) = get_template_and_user(
44 { template_name => "admin/aqbudgets.tmpl",
48 flagsrequired => { acquisition => 'budget_manage' },
53 my $op = $input->param('op');
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;
61 # IF USER DOESNT HAVE PERM FOR AN 'ADD', THEN REDIRECT TO THE DEFAULT VIEW...
62 if ( not defined $template->{param_map}->{'CAN_user_acquisition_budget_add_del'} && $op == 'add_form' ) {
66 my $count = GetPeriodsCount();
67 $template->param( period_button_only => 1 ) if $count == 0 ;
68 my $cur = GetCurrency;
69 my $cur_format = C4::Context->preference("CurrencyFormat");
73 # TODO: at some stage this currency handling should be done properly
74 with locales and POSIX.pm - and a proper 'locales' lookup and syspref
76 setlocale( LC_MONETARY , "french");
77 my $num = new Number::Format();
78 my $formatted_number = $num->format_price('123456789.123'):
82 if ( $cur_format eq 'FR' ) {
83 $num = new Number::Format(
84 'decimal_fill' => '2',
85 'decimal_point' => ',',
86 'int_curr_symbol' => '',
87 'mon_thousands_sep' => ' ',
88 'thousands_sep' => ' ',
89 'mon_decimal_point' => ','
91 } else { # US by default..
92 $num = new Number::Format(
93 'int_curr_symbol' => '',
94 'mon_thousands_sep' => ',',
95 'mon_decimal_point' => '.'
99 # ' ------- get periods stuff ------------------'
100 # IF PERIODID IS DEFINED, GET THE PERIOD - ELSE JUST GET THE ACTIVE PERIOD BY DEFAULT
101 my $budget_period_id = $input->param('budget_period_id');
102 my $period = GetBudgetPeriod($budget_period_id);
104 # authcats_loop populates the YUI planning button
105 my @auth_cats_loop = GetBudgetAuthCats();
107 my $budget_period_id = $period->{'budget_period_id'};
108 my $budget_period_locked = $period->{'budget_period_locked'};
109 my $budget_period_description = $period->{'budget_period_description'};
110 my $budget_period_total = $period->{'budget_period_total'};
113 budget_period_id => $budget_period_id,
114 budget_period_locked => $budget_period_locked,
115 budget_period_description => $budget_period_description,
116 auth_cats_loop => \@auth_cats_loop,
118 # ------- get periods stuff ------------------
120 my $script_name = "/cgi-bin/koha/admin/aqbudgets.pl";
121 my $budget_id = $input->param('budget_id');
122 my $budget_code = $input->param('budget_code');
123 my $budget_name = $input->param('budget_name');
124 my $budget_amount = $input->param('budget_amount');
125 my $budget_amount_sublevel = $input->param('budget_amount_sublevel');
126 my $budget_encumb = $input->param('budget_encumb');
127 my $budget_expend = $input->param('budget_expend');
128 my $budget_notes = $input->param('budget_notes');
129 my $sort1_authcat = $input->param('sort1_authcat');
130 my $sort2_authcat = $input->param('sort2_authcat');
131 my $budget_description = $input->param('budget_description');
132 my $budget_branchcode = $input->param('budget_branchcode');
133 my $budget_owner_id = $input->param('budget_owner_id');
134 my $budget_parent_id = $input->param('budget_parent_id');
135 my $budget_permission = $input->param('budget_permission');
136 my $budget_period_dropbox = $input->param('budget_period_dropbox');
137 my $filter_budgetname = $input->param('filter_budgetname');
138 my $filter_budgetbranch = $input->param('filter_budgetbranch');
140 my $pagesize = 20; # del
142 # USED FOR PERMISSION COMPARSION LATER
143 my $borrower_id = $template->{param_map}->{'USER_INFO'}[0]->{'borrowernumber'};
144 my $user = GetMemberDetails($borrower_id);
145 my $user_branchcode = $user->{'branchcode'};
148 action => $script_name,
149 script_name => $script_name,
150 show_mine => $show_mine,
154 my $sthtemp = $dbh->prepare("Select flags, branchcode from borrowers where borrowernumber = ?");
155 $sthtemp->execute($borrowernumber);
156 my ( $flags, $homebranch ) = $sthtemp->fetchrow;
158 my ( $budget, $period, $query, $sth );
160 my $branches = GetBranches;
162 foreach my $thisbranch (keys %$branches) {
164 value => $thisbranch,
165 branchname => $branches->{$thisbranch}->{'branchname'},
167 $row{selected} = 1 if $thisbranch eq $filter_budgetbranch;
168 push @branchloop2, \%row;
172 # budget_id => $budget->{'budget_id'}
175 # Used to create form to add or modify a record
176 if ($op eq 'add_form') {
177 #### ------------------- ADD_FORM -------------------------
179 #---- if primkey exists, it's a modify action, so read values to modify...
180 # pass the period_id to build the dropbox - because we only want to show budgets from this period
182 my $dropbox_disabled;
183 if ( defined $budget_id ) { ### MOD
184 $budget = GetBudget($budget_id);
185 $dropbox_disabled = BudgetHasChildren($budget_id);
186 my $borrower = &GetMember( $budget->{budget_owner_id} );
187 $budget->{budget_owner_name} = $borrower->{'firstname'} . ' ' . $borrower->{'surname'};
190 my $sort1_authcat_dropbox = GetAuthcatDropbox( 'sort1_authcat', $budget->{'sort1_authcat'} );
191 my $sort2_authcat_dropbox = GetAuthcatDropbox( 'sort2_authcat', $budget->{'sort2_authcat'} );
195 my $hier = GetBudgetHierarchy($budget_period_id);
196 foreach my $r (@$hier) {
197 $r->{budget_code_indent} =~ s/ /\~/g; #
198 $labels{"$r->{budget_id}"} = $r->{budget_code_indent};
199 push @values, $r->{budget_id};
202 # if no buget_id is passed then its an add
203 my $budget_parent_dropbox;
204 my $budget_parent_id = $budget->{'budget_parent_id'} if $budget;
205 $budget_parent_dropbox = CGI::scrolling_list(
206 -name => 'budget_parent_id',
208 -default => $budget_parent_id ? $budget_parent_id : undef,
210 -style => "min-width:100px;",
214 my $branches = GetBranches;
215 my @branchloop_select;
216 foreach my $thisbranch ( keys %$branches ) {
218 value => $thisbranch,
219 branchname => $branches->{$thisbranch}->{'branchname'},
221 $row{selected} = 1 if $thisbranch eq $budget->{'budget_branchcode'};
222 push @branchloop_select, \%row;
225 # $period = GetBudgetPeriod($budget->{'budget_period_id'});
226 # $budget_period_description = $period->{'budget_period_description'};
228 my $budget_perm_dropbox =
229 GetBudgetPermDropbox($budget->{'budget_permission'});
231 # if no buget_id is passed then its an add
234 dateformat => C4::Dates->new()->visual(),
235 budget_id => $budget->{'budget_id'},
236 budget_parent_id => $budget->{'budget_parent_id'},
237 budget_parent_dropbox => $budget_parent_dropbox,
238 sort1_authcat_dropbox => $sort1_authcat_dropbox,
239 sort2_authcat_dropbox => $sort2_authcat_dropbox,
240 budget_perm_dropbox => $budget_perm_dropbox,
241 budget_code => $budget->{'budget_code'},
242 budget_code_indent => $budget->{'budget_code_indent'},
243 budget_name => $budget->{'budget_name'},
244 budget_branchcode => $budget->{'budget_branchcode'},
245 budget_amount => sprintf("%.2f", $budget->{'budget_amount'}),
246 budget_amount_sublevel => sprintf("%.2f", $budget->{'budget_amount_sublevel'}),
247 budget_encumb => $budget->{'budget_encumb'},
248 budget_expend => $budget->{'budget_expend'},
249 budget_notes => $budget->{'budget_notes'},
250 budget_description => $budget->{'budget_description'},
251 budget_owner_id => $budget->{'budget_owner_id'},
252 budget_owner_name => $budget->{'budget_owner_name'},
253 budget_permission => $budget->{'budget_permission'},
254 budget_period_id => $budget_period_id,
255 budget_period_description => $budget_period_description,
256 branchloop_select => \@branchloop_select,
258 # END $OP eq ADD_FORM
259 #---------------------- DEFAULT DISPLAPY BELOW ---------------------
261 # called by default form, used to confirm deletion of data in DB
262 } elsif ($op eq 'delete_confirm') {
264 my $budget = GetBudget($budget_id);
266 budget_id => $budget->{'budget_id'},
267 budget_code => $budget->{'budget_code'},
268 budget_name => $budget->{'budget_name'},
269 budget_amount => $num->format_price( $budget->{'budget_amount'} ),
271 # END $OP eq DELETE_CONFIRM
272 # called by delete_confirm, used to effectively confirm deletion of data in DB
274 if ( $op eq 'delete_confirmed' ) {
275 my $rc = DelBudget($budget_id);
277 if ( $op eq 'add_validate' ) {
279 budget_id => $budget_id,
280 budget_parent_id => $budget_parent_id,
281 budget_period_id => $budget_period_id,
282 budget_code => $budget_code,
283 budget_name => $budget_name,
284 budget_branchcode => $budget_branchcode,
285 budget_amount => $budget_amount,
286 budget_amount_sublevel => $budget_amount_sublevel,
287 budget_encumb => $budget_encumb,
288 budget_expend => $budget_expend,
289 budget_notes => $budget_notes,
290 sort1_authcat => $sort1_authcat,
291 sort2_authcat => $sort2_authcat,
292 budget_description => $budget_description,
293 budget_owner_id => $budget_owner_id,
294 budget_permission => $budget_permission,
296 if ( defined $budget_id ) {
297 ModBudget( \%budget_hash );
299 AddBudget( \%budget_hash );
302 my $branches = GetBranches();
303 my $budget_period_dropbox = GetBudgetPeriodsDropbox($budget_period_id );
305 budget_period_dropbox => $budget_period_dropbox,
306 budget_id => $budget_id,
307 budget_period_startdate => $period->{'budget_period_startdate'},
308 budget_period_enddate => $period->{'budget_period_enddate'},
310 my $moo = GetBudgetHierarchy($budget_period_id, $template->{param_map}->{'USER_INFO'}[0]->{'branchcode'}, $show_mine?$borrower_id:'');
311 my @budgets = @$moo; #FIXME
315 my $period_total = 0;
316 my ( $period_alloc_total, $base_alloc_total, $sub_alloc_total, $base_spent_total, $base_remaining_total );
318 foreach my $budget (@budgets) {
321 unless($staffflags->{'superlibrarian'} == 1 ) {
322 #IF NO PERMS, THEN DISABLE EDIT/DELETE
323 unless ( $template->{param_map}->{'CAN_user_acquisition_budget_modify'} ) {
324 $budget->{'budget_lock'} = 1;
326 # check budget permission
327 if ( $budget_period_locked == 1 ) {
328 $budget->{'budget_lock'} = 1;
330 } elsif ( $budget->{budget_permission} == 1 ) {
332 if ( $borrower_id != $budget->{'budget_owner_id'} ) {
333 $budget->{'budget_lock'} = 1;
335 # check parent perms too
336 my $parents_perm = 0;
337 if ( $budget->{depth} > 0 ) {
338 $parents_perm = CheckBudgetParentPerm( $budget, $borrower_id );
339 delete $budget->{'budget_lock'} if $parents_perm == '1';
341 } elsif ( $budget->{budget_permission} == 2 ) {
343 $budget->{'budget_lock'} = 1 if $user_branchcode ne $budget->{budget_branchcode};
347 # if a budget search doesnt match, next
348 if ($filter_budgetname ) {
349 next unless $budget->{budget_code} =~ m/$filter_budgetname/ ||
350 $budget->{name} =~ m/$filter_budgetname/ ;
352 if ($filter_budgetbranch ) {
353 next unless $budget->{budget_branchcode} =~ m/$filter_budgetbranch/;
357 # adds to total - only if budget is a 'top-level' budget
358 $period_alloc_total += $budget->{'budget_amount_total'} if $budget->{'depth'} == 0;
359 $base_alloc_total += $budget->{'budget_amount'};
360 $sub_alloc_total += $budget->{'budget_amount_sublevel'};
361 $base_spent_total += $budget->{'budget_spent'};
362 $budget->{'budget_remaining'} = $budget->{'budget_amount'} - $budget->{'budget_spent'};
363 $base_remaining_total += $budget->{'budget_remaining'};
365 # if amount == 0 dont display...
366 delete $budget->{'budget_unalloc_sublevel'} if $budget->{'budget_unalloc_sublevel'} == 0 ;
367 delete $budget->{'budget_amount_sublevel'} if $budget->{'budget_amount_sublevel'} == 0 ;
369 $budget->{'remaining_pos'} = 1 if $budget->{'budget_remaining'} > 0;
370 $budget->{'remaining_neg'} = 1 if $budget->{'budget_remaining'} < 0;
371 $budget->{'budget_amount'} = $num->format_price( $budget->{'budget_amount'} );
372 $budget->{'budget_spent'} = $num->format_price( $budget->{'budget_spent'} );
373 $budget->{'budget_remaining'} = $num->format_price( $budget->{'budget_remaining'} );
374 $budget->{'budget_amount_total'} = $num->format_price( $budget->{'budget_amount_total'} );
375 $budget->{'budget_amount_sublevel'} = $num->format_price( $budget->{'budget_amount_sublevel'} ) if defined $budget->{'budget_amount_sublevel'};
376 $budget->{'budget_unalloc_sublevel'} = $num->format_price( $budget->{'budget_unalloc_sublevel'} ) if defined $budget->{'budget_unalloc_sublevel'};
378 my $borrower = &GetMember( $budget->{budget_owner_id} );
379 $budget->{budget_owner_name} = $borrower->{'firstname'} . ' ' . $borrower->{'surname'};
380 $budget->{budget_borrowernumber} = $borrower->{'borrowernumber'};
382 push( @loop, { %{$budget},
383 toggle => $toggle++%2,
384 branchname => $branches->{ $budget->{branchcode} }->{branchname},
385 startdate => format_date($budget->{startdate}),
386 enddate => format_date($budget->{enddate}),
394 budget_period_total => $num->format_price($budget_period_total),
395 period_alloc_total => $num->format_price($period_alloc_total),
396 base_alloc_total => $num->format_price($base_alloc_total),
397 sub_alloc_total => $num->format_price($sub_alloc_total),
398 base_spent_total => $num->format_price($base_spent_total),
399 base_remaining_total => $num->format_price($base_remaining_total),
400 period_remaining_total => $num->format_price( $period_alloc_total - $base_alloc_total ),
401 branchloop => \@branchloop2,
402 cur => $cur->{symbol},
403 cur_format => $cur_format,
404 # pagination_bar => pagination_bar(
405 # $script_name, getnbpages(scalar @results, $pagesize),
409 } #---- END $OP eq DEFAULT
411 output_html_with_http_headers $input, $cookie, $template->output;