DBRev 24.06.00.000: Start of a new release cycle
[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 Modern::Perl;
21 use CGI qw ( -utf8 );
22 use C4::Context;
23 use C4::Output qw( output_html_with_http_headers );
24 use C4::Auth qw( get_template_and_user );
25 use C4::Budgets qw( GetBudget GetBudgetPeriod );
26
27 =head1 DESCRIPTION
28
29 This script checks the amount unallocated from the new parent budget , or the period - if no parent_id is given
30
31 This script is called from aqbudgets.pl during an 'cud-add' or 'cud-mod' budget, from the JSscript Check() function, 
32 to determine whether the new parent budget (or period) has enough unallocated funds for the save to complete.
33
34 =cut
35
36 my $dbh = C4::Context->dbh;
37 my $input = CGI->new;
38
39 my $total     = $input->param('total');
40 my $budget_id = $input->param('budget_id');
41 my $parent_id = $input->param('parent_id');
42 my $period_id = $input->param('period_id');
43 my $returncode;
44
45 my ( $template, $loggedinuser, $cookie ) = get_template_and_user(
46     {   template_name   => "acqui/ajax.tt",
47         query           => $input,
48         type            => "intranet",
49     }
50 );
51 my ($period, $parent , $budget);
52 $period = GetBudgetPeriod($period_id) if $period_id;
53 $parent = GetBudget($parent_id)       if defined $parent_id;
54 $budget = GetBudget($budget_id)       if defined $budget_id;
55
56 # CHECK THE PARENT BUDGET FOR ENOUGHT AMOUNT UNALLOCATED,  IF NOT THEN RETURN 1
57 my ($sub_unalloc , $period_sum, $budget_period_unalloc);
58
59 if ($parent) {
60     my $query = "  SELECT SUM(budget_amount) as sum FROM aqbudgets where budget_parent_id = ? ";
61     my @sql_params;
62     my $sth   = $dbh->prepare($query);
63     $sth->execute( $parent->{'budget_id'} );
64     my $sum = $sth->fetchrow_hashref;
65     $sth->finish;
66     $sub_unalloc = $parent->{'budget_amount'} - $sum->{sum};
67 #    TRICKY.. , IF THE PARENT IS THE CURRENT PARENT  - THEN SUBTRACT CURRENT BUDGET FROM TOTAL
68     $sub_unalloc           += $budget->{'budget_amount'} if ( $budget->{'budget_parent_id'} == $parent_id ) ;
69 }
70
71 # ELSE , IF NO PARENT PASSED, THEN CHECK UNALLOCATED FOR PERIOD, IF NOT THEN RETURN 2
72 else {
73     my $query = qq| SELECT SUM(budget_amount) as sum
74                 FROM aqbudgets WHERE budget_period_id = ? and budget_parent_id IS NULL |;
75     my @sql_params;
76     push @sql_params, $period_id;
77     if ($budget_id){
78         $query.=qq| and budget_id <> ? |;
79         push @sql_params,$budget_id;
80     }
81
82     my $sth = $dbh->prepare($query);
83     $sth->execute(@sql_params);
84     $period_sum = $sth->fetchrow_hashref;
85     $sth->finish;
86     $budget_period_unalloc = $period->{'budget_period_total'} - $period_sum->{'sum'} if $period->{'budget_period_total'};
87 }
88
89 # FIXME - we really need a better way to do this consistently
90 # and across the board, be it bigints, Math::FixedPoint, a
91 # modernized version of Math::Curency that isn't tied to the system
92 # locale, or something else.
93 $total                 = sprintf( "%.2f", $total );
94 $sub_unalloc           = sprintf( "%.2f", $sub_unalloc );
95 $budget_period_unalloc = sprintf( "%.2f", $budget_period_unalloc );
96
97 if ( $parent_id) {
98     if ( ($total > $sub_unalloc ) && $sub_unalloc )  {
99         $returncode = 1;
100     }
101 } elsif ( ( $total > $budget_period_unalloc ) && $budget_period_unalloc ) {
102     $returncode = 2;
103
104 } else {
105     $returncode = 0;
106 }
107
108
109 output_html_with_http_headers $input, $cookie, $returncode;
110 1;