Bug 8918: Add a unit test for CalculatePriority
[koha.git] / admin / check_parent_total.pl
blobce7453a99fdd55b8cbdfa2a0b459b5211ebed5ed
1 #!/usr/bin/perl
3 # Copyright 2000-2002 Katipo Communications
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 use strict;
21 #use warnings; FIXME - Bug 2505
22 use CGI;
23 use C4::Context;
24 use C4::Output;
25 use C4::Auth;
26 use C4::Budgets;
28 =head1 DESCRIPTION
30 This script checks the amount unallocated from the new parent budget , or the period - if no parent_id is given
32 This script is called from aqbudgets.pl during an 'add' or 'mod' budget, from the JSscript Check() function,
33 to determine whether the new parent budget (or period) has enough unallocated funds for the save to complete.
35 =cut
37 my $dbh = C4::Context->dbh;
38 my $input = new CGI;
40 my $total = $input->param('total');
41 my $budget_id = $input->param('budget_id');
42 my $parent_id = $input->param('parent_id');
43 my $period_id = $input->param('period_id');
44 my $returncode;
46 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
47 { template_name => "acqui/ajax.tmpl",
48 query => $input,
49 type => "intranet",
50 authnotrequired => 0,
51 debug => 0,
54 my ($period, $parent , $budget);
55 $period = GetBudgetPeriod($period_id) if $period_id;
56 $parent = GetBudget($parent_id) if defined $parent_id;
57 $budget = GetBudget($budget_id) if defined $budget_id;
59 # CHECK THE PARENT BUDGET FOR ENOUGHT AMOUNT UNALLOCATED, IF NOT THEN RETURN 1
60 my ($sub_unalloc , $period_sum, $budget_period_unalloc);
62 if ($parent) {
63 my $query = " SELECT SUM(budget_amount) as sum FROM aqbudgets where budget_parent_id = ? ";
64 my $sth = $dbh->prepare($query);
65 my @sql_params;
66 push @sql_params, $parent->{'budget_id'} ;
67 if ($budget_id){
68 $query.=qq| and budget_id <> ? |;
69 push @sql_params,$budget_id;
71 $sth->execute( @sql_params );
72 my $sum = $sth->fetchrow_hashref;
73 $sth->finish;
75 $sub_unalloc = $parent->{'budget_amount'} - $sum->{sum};
77 # TRICKY.. , IF THE PARENT IS THE CURRENT PARENT - THEN SUBSTRACT CURRENT BUDGET FROM TOTAL
78 $sub_unalloc += $budget->{'budget_amount'} if ( $budget->{'budget_parent_id'} == $parent_id ) ;
81 # ELSE , IF NO PARENT PASSED, THEN CHECK UNALLOCATED FOR PERIOD, IF NOT THEN RETURN 2
82 else {
83 my $query = qq| SELECT SUM(budget_amount) as sum
84 FROM aqbudgets WHERE budget_period_id = ? and budget_parent_id IS NULL |;
85 my @sql_params;
86 push @sql_params, $period_id;
87 if ($budget_id){
88 $query.=qq| and budget_id <> ? |;
89 push @sql_params,$budget_id;
92 my $sth = $dbh->prepare($query);
93 $sth->execute(@sql_params);
94 $period_sum = $sth->fetchrow_hashref;
95 $sth->finish;
96 $budget_period_unalloc = $period->{'budget_period_total'} - $period_sum->{'sum'} if $period->{'budget_period_total'};
99 # FIXME - we really need a better way to do this consistently
100 # and across the board, be it bigints, Math::FixedPoint, a
101 # modernized version of Math::Curency that isn't tied to the system
102 # locale, or something else.
103 $total = sprintf( "%.2f", $total );
104 $sub_unalloc = sprintf( "%.2f", $sub_unalloc );
105 $budget_period_unalloc = sprintf( "%.2f", $budget_period_unalloc );
107 if ( $parent_id) {
108 if ( ($total > $sub_unalloc ) && $sub_unalloc ) {
109 $returncode = 1;
111 } elsif ( ( $total > $budget_period_unalloc ) && $budget_period_unalloc ) {
112 $returncode = 2;
114 } else {
115 $returncode = 0;
119 output_html_with_http_headers $input, $cookie, $returncode;