Bug 9978: Replace license header with the correct license (GPLv3+)
[koha.git] / admin / check_parent_total.pl
1 #!/usr/bin/perl
2
3 # Copyright 2000-2002 Katipo Communications
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use strict;
21 #use warnings; FIXME - Bug 2505
22 use CGI qw ( -utf8 );
23 use C4::Context;
24 use C4::Output;
25 use C4::Auth;
26 use C4::Budgets;
27
28 =head1 DESCRIPTION
29
30 This script checks the amount unallocated from the new parent budget , or the period - if no parent_id is given
31
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.
34
35 =cut
36
37 my $dbh = C4::Context->dbh;
38 my $input = new CGI;
39
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;
45
46 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
47     {   template_name   => "acqui/ajax.tt",
48         query           => $input,
49         type            => "intranet",
50         authnotrequired => 0,
51         debug           => 0,
52     }
53 );
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;
58
59 # CHECK THE PARENT BUDGET FOR ENOUGHT AMOUNT UNALLOCATED,  IF NOT THEN RETURN 1
60 my ($sub_unalloc , $period_sum, $budget_period_unalloc);
61
62 if ($parent) {
63     my $query = "  SELECT SUM(budget_amount) as sum FROM aqbudgets where budget_parent_id = ? ";
64     my @sql_params;
65     my $sth   = $dbh->prepare($query);
66     $sth->execute( $parent->{'budget_id'} );
67     my $sum = $sth->fetchrow_hashref;
68     $sth->finish;
69     $sub_unalloc = $parent->{'budget_amount'} - $sum->{sum};
70 #    TRICKY.. , IF THE PARENT IS THE CURRENT PARENT  - THEN SUBSTRACT CURRENT BUDGET FROM TOTAL
71     $sub_unalloc           += $budget->{'budget_amount'} if ( $budget->{'budget_parent_id'} == $parent_id ) ;
72 }
73
74 # ELSE , IF NO PARENT PASSED, THEN CHECK UNALLOCATED FOR PERIOD, IF NOT THEN RETURN 2
75 else {
76     my $query = qq| SELECT SUM(budget_amount) as sum
77                 FROM aqbudgets WHERE budget_period_id = ? and budget_parent_id IS NULL |;
78     my @sql_params;
79     push @sql_params, $period_id;
80     if ($budget_id){
81         $query.=qq| and budget_id <> ? |;
82         push @sql_params,$budget_id;
83     }
84
85     my $sth = $dbh->prepare($query);
86     $sth->execute(@sql_params);
87     $period_sum = $sth->fetchrow_hashref;
88     $sth->finish;
89     $budget_period_unalloc = $period->{'budget_period_total'} - $period_sum->{'sum'} if $period->{'budget_period_total'};
90 }
91
92 # FIXME - we really need a better way to do this consistently
93 # and across the board, be it bigints, Math::FixedPoint, a
94 # modernized version of Math::Curency that isn't tied to the system
95 # locale, or something else.
96 $total                 = sprintf( "%.2f", $total );
97 $sub_unalloc           = sprintf( "%.2f", $sub_unalloc );
98 $budget_period_unalloc = sprintf( "%.2f", $budget_period_unalloc );
99
100 if ( $parent_id) {
101     if ( ($total > $sub_unalloc ) && $sub_unalloc )  {
102         $returncode = 1;
103     }
104 } elsif ( ( $total > $budget_period_unalloc ) && $budget_period_unalloc ) {
105     $returncode = 2;
106
107 } else {
108     $returncode = 0;
109 }
110
111
112 output_html_with_http_headers $input, $cookie, $returncode;
113 1;