X-Git-Url: http://git.rot13.org/?a=blobdiff_plain;f=C4%2FBudgets.pm;h=4a8b3b648759a010586cf2b52facfafe0cc3e142;hb=844f63f1f8704e7f02f9fb194e5dc7142a9f12b5;hp=b739303887694d5d1829a9908df0ac16fb3263ae;hpb=5022a8583a9a77394d084402c00f622ed7769350;p=koha.git diff --git a/C4/Budgets.pm b/C4/Budgets.pm index b739303887..4a8b3b6487 100644 --- a/C4/Budgets.pm +++ b/C4/Budgets.pm @@ -20,6 +20,7 @@ package C4::Budgets; use strict; use C4::Context; use C4::Dates qw(format_date format_date_in_iso); +use C4::SQLHelper qw<:all>; use C4::Debug; use vars qw($VERSION @ISA @EXPORT); @@ -38,25 +39,28 @@ BEGIN { &ModBudget &DelBudget &GetBudgetSpent + &GetBudgetOrdered &GetPeriodsCount &GetBudgetPeriod &GetBudgetPeriods &ModBudgetPeriod + &AddBudgetPeriod &DelBudgetPeriod &GetBudgetPeriodsDropbox &GetBudgetSortDropbox - &GetAuthcatDropbox &GetAuthvalueDropbox &GetBudgetPermDropbox &ModBudgetPlan + &GetCurrency &GetCurrencies &ModCurrencies &ConvertCurrency - &GetBudgetsPlanCell + + &GetBudgetsPlanCell &AddBudgetPlanValue &GetBudgetAuthCats &BudgetHasChildren @@ -75,15 +79,6 @@ sub HideCols { my ( $authcat, @hide_cols ) = @_; my $dbh = C4::Context->dbh; -=c - my $sth = $dbh->prepare( - qq| - UPDATE aqbudgets_planning - SET display = 1 where authcat = ? | - ); - $sth->execute( $authcat ); -=cut - my $sth1 = $dbh->prepare( qq| UPDATE aqbudgets_planning SET display = 0 @@ -130,6 +125,10 @@ sub CheckBudgetParentPerm { return 0; } +sub AddBudgetPeriod { + my ($budgetperiod) = @_; + return InsertInTable("aqbudgetperiods",$budgetperiod); +} # ------------------------------------------------------------------- sub GetPeriodsCount { my $dbh = C4::Context->dbh; @@ -174,7 +173,6 @@ sub BudgetHasChildren { WHERE budget_parent_id = ? | ); $sth->execute( $budget_id ); my $sum = $sth->fetchrow_hashref; - $sth->finish; return $sum->{'sum'}; } @@ -228,7 +226,6 @@ sub GetBudgetsPlanCell { ((aqbudgets.sort1_authcat = ? AND sort1 =?) OR (aqbudgets.sort2_authcat = ? AND sort2 =?)) | ); - $sth->{TraceLevel} = 2; $sth->execute( $cell->{'budget_id'}, $budget->{'sort1_authcat'}, $cell->{'authvalue'}, @@ -239,7 +236,7 @@ sub GetBudgetsPlanCell { $actual = $sth->fetchrow_array; # get the estimated amount - my $sth = $dbh->prepare( qq| + $sth = $dbh->prepare( qq| SELECT estimated_amount AS estimated, display FROM aqbudgets_planning WHERE budget_period_id = ? AND @@ -304,14 +301,30 @@ sub GetBudgetSpent { my ($budget_id) = @_; my $dbh = C4::Context->dbh; my $sth = $dbh->prepare(qq| - SELECT SUM(ecost * quantity ) AS sum FROM aqorders + SELECT SUM(ecost * quantity) AS sum FROM aqorders + WHERE budget_id = ? AND + quantityreceived > 0 AND + datecancellationprinted IS NULL + |); + + $sth->execute($budget_id); + my $sum = $sth->fetchrow_array; + return $sum; +} + +# ------------------------------------------------------------------- +sub GetBudgetOrdered { + my ($budget_id) = @_; + my $dbh = C4::Context->dbh; + my $sth = $dbh->prepare(qq| + SELECT SUM(ecost * quantity) AS sum FROM aqorders WHERE budget_id = ? AND - datecancellationprinted IS NULL + quantityreceived = 0 AND + datecancellationprinted IS NULL |); $sth->execute($budget_id); my $sum = $sth->fetchrow_array; -# $sum = sprintf "%.2f", $sum; return $sum; } @@ -323,6 +336,7 @@ sub GetBudgetPermDropbox { $labels{'1'} = 'Owner'; $labels{'2'} = 'Library'; my $radio = CGI::scrolling_list( + -id => 'budget_permission', -name => 'budget_permission', -values => [ '0', '1', '2' ], -default => $perm, @@ -332,58 +346,24 @@ sub GetBudgetPermDropbox { return $radio; } -# ------------------------------------------------------------------- -sub GetAuthcatDropbox { - my ($name, $default ) = @_; - my @authorised_values; - my $value; - my $dbh = C4::Context->dbh; - my $sth = $dbh->prepare(qq| - SELECT distinct(category) - FROM authorised_values WHERE category LIKE 'Asort%' - ORDER BY lib | - ); - $sth->execute(); - - push @authorised_values, ''; - while (my $value = $sth->fetchrow_array) { - push @authorised_values, $value; - } - - my $budget_authcat_dropbox = CGI::scrolling_list( - -name => $name, - -values => \@authorised_values, - -override => 1, - -size => 1, - -default => $default, - -multiple => 0, - -tabindex => 1, - -id => $name, - ); - return $budget_authcat_dropbox; -} - # ------------------------------------------------------------------- sub GetBudgetAuthCats { - my @auth_cats; - my $value; - my $dbh = C4::Context->dbh; - my $sth = $dbh->prepare( - "SELECT distinct(category) - FROM authorised_values where category like 'Asort%' - ORDER BY category" - ); - $sth->execute(); - while ( my $value = $sth->fetchrow_array ) { - push @auth_cats, $value; + my ($budget_period_id) = shift; + # now, populate the auth_cats_loop used in the budget planning button + # we must retrieve all auth values used by at least one budget + my $dbh = C4::Context->dbh; + my $sth=$dbh->prepare("SELECT sort1_authcat,sort2_authcat FROM aqbudgets WHERE budget_period_id=?"); + $sth->execute($budget_period_id); + my %authcats; + while (my ($sort1_authcat,$sort2_authcat) = $sth->fetchrow) { + $authcats{$sort1_authcat}=1; + $authcats{$sort2_authcat}=1; } - my @loop_data = (); # initialize an array to hold your loop - while (@auth_cats) { - my %row_data; # get a fresh hash for the row data - $row_data{authcat} = shift @auth_cats; - push( @loop_data, \%row_data ); + my @auth_cats_loop; + foreach (sort keys %authcats) { + push @auth_cats_loop,{ authcat => $_ }; } - return @loop_data; + return \@auth_cats_loop; } # ------------------------------------------------------------------- @@ -447,25 +427,9 @@ sub GetBudgetPeriodsDropbox { # ------------------------------------------------------------------- sub GetBudgetPeriods { - my $dbh = C4::Context->dbh; - my $sth = $dbh->prepare(qq| - SELECT * - FROM aqbudgetperiods - ORDER BY budget_period_startdate, budget_period_enddate | - ); - $sth->execute(); - my @results; - my $active; - while (my $data = $sth->fetchrow_hashref) { - if ($data->{'budget_period_active'} == 1) { - $active = $data->{'budget_period_id'}; - } - push(@results, $data); - } - $sth->finish; - return ($active, \@results); + my ($filters,$orderby) = @_; + return SearchInTable("aqbudgetperiods",$filters, $orderby, undef,undef, undef, "wide"); } - # ------------------------------------------------------------------- sub GetBudgetPeriod { my ($budget_period_id) = @_; @@ -474,7 +438,7 @@ sub GetBudgetPeriod { my $total = 0; ## get information about the record that will be deleted my $sth; - if ($budget_period_id gt 0) { + if ($budget_period_id) { $sth = $dbh->prepare( qq| SELECT * FROM aqbudgetperiods @@ -490,12 +454,11 @@ sub GetBudgetPeriod { $sth->execute(); } my $data = $sth->fetchrow_hashref; - $sth->finish; return $data; } # ------------------------------------------------------------------- -sub DelBudgetPeriod() { +sub DelBudgetPeriod{ my ($budget_period_id) = @_; my $dbh = C4::Context->dbh; ; ## $total = number of records linked to the record that must be deleted @@ -503,40 +466,17 @@ sub DelBudgetPeriod() { ## get information about the record that will be deleted my $sth = $dbh->prepare(qq| - SELECT budget_period_id - , budget_period_startdate - , budget_period_enddate - , budget_period_amount - , budget_period_ref - , budget_period_description + DELETE FROM aqbudgetperiods WHERE budget_period_id=? | ); - $sth->execute($budget_period_id); - my $data = $sth->fetchrow_hashref; - $sth->finish; + return $sth->execute($budget_period_id); } # ------------------------------------------------------------------- -sub ModBudgetPeriod() { - my ($budget_period_id) = @_; - my $dbh = C4::Context->dbh - ; ## $total = number of records linked to the record that must be deleted my $total = 0; - - ## get information about the record that will be deleted - my $sth = $dbh->prepare(" - SELECT budget_period_id - , budget_period_startdate - , budget_period_enddate - , budget_period_amount - , budget_period_ref - , budget_period_description - FROM aqbudgetperiods - WHERE budget_period_id=?;" - ); - $sth->execute($budget_period_id); - my $data = $sth->fetchrow_hashref; - $sth->finish; +sub ModBudgetPeriod { + my ($budget_period_information) = @_; + return UpdateInTable("aqbudgetperiods",$budget_period_information); } # ------------------------------------------------------------------- @@ -545,31 +485,31 @@ sub GetBudgetHierarchy { my @bind_params; my $dbh = C4::Context->dbh; my $query = qq| - SELECT * - FROM aqbudgets - JOIN aqbudgetperiods USING (budget_period_id) - WHERE budget_period_active=1 |; + SELECT aqbudgets.* + FROM aqbudgets |; # show only period X if requested + my @where_strings; if ($budget_period_id) { - $query .= "AND aqbudgets.budget_period_id = ?"; + push @where_strings," aqbudgets.budget_period_id = ?"; push @bind_params, $budget_period_id; } # show only budgets owned by me, my branch or everyone if ($owner) { if ($branchcode) { - $query .= " AND (budget_owner_id = ? OR budget_branchcode = ? OR (budget_branchcode IS NULL AND budget_owner_id IS NULL))"; - push @bind_params, $owner; - push @bind_params, $branchcode; + push @where_strings,qq{ (budget_owner_id = ? OR budget_branchcode = ? OR (budget_branchcode IS NULL or budget_branchcode="" AND (budget_owner_id IS NULL OR budget_owner_id="")))}; + push @bind_params, ($owner, $branchcode); } else { - $query .= ' AND budget_owner_id = ? OR budget_owner_id IS NULL'; + push @where_strings, ' (budget_owner_id = ? OR budget_owner_id IS NULL or budget_owner_id ="") '; push @bind_params, $owner; } } else { if ($branchcode) { - $query .= " AND (budget_branchcode =? or budget_branchcode is NULL)"; + push @where_strings," (budget_branchcode =? or budget_branchcode is NULL)"; push @bind_params, $branchcode; } } + $query.=" WHERE ".join(' AND ', @where_strings) if @where_strings; + $debug && warn $query,join(",",@bind_params); my $sth = $dbh->prepare($query); $sth->execute(@bind_params); my $results = $sth->fetchall_arrayref({}); @@ -596,8 +536,8 @@ sub GetBudgetHierarchy { } # look for top parents 1st - my @sort; - my ($i, $depth_count) = 0; + my (@sort, $depth_count); + ($i, $depth_count) = 0; while (1) { my $children = 0; foreach my $r (@res) { @@ -611,9 +551,8 @@ sub GetBudgetHierarchy { # add indent my $depth = $r->{depth} * 2; - my $space = pack "A[$depth]"; - $r->{budget_code_indent} = $space . $r->{budget_code}; - $r->{budget_name_indent} = $space . $r->{budget_name}; + $r->{budget_code_indent} = $r->{budget_code}; + $r->{budget_name_indent} = $r->{budget_name}; foreach my $r3 (@sort) { if ($r3->{budget_id} == $r->{budget_parent_id}) { $parent = $i2; @@ -625,7 +564,7 @@ sub GetBudgetHierarchy { $r->{budget_code_indent} = $r->{budget_code}; $r->{budget_name_indent} = $r->{budget_name}; } - + if (defined $parent) { splice @sort, ($parent + 1), 0, $r; } else { @@ -648,142 +587,38 @@ sub GetBudgetHierarchy { $moo =~ s/\ /\ \;/g; $r->{'budget_code_indent'} = $moo; - my $moo = $r->{'budget_name_indent'}; + $moo = $r->{'budget_name_indent'}; $moo =~ s/\ /\ \;/g; $r->{'budget_name_indent'} = $moo; $r->{'budget_spent'} = GetBudgetSpent( $r->{'budget_id'} ); -# $budget->{'budget_alloc'} = sprintf( "%.2f", $budget->{'budget_alloc'} - $budget->{'budget_amount alloc'} ); -# $budget->{'budget_alloc'} = sprintf( "%.2f", $budget->{'budget_alloc'} ); - - $r->{'budget_amount_total'} = $r->{'budget_amount'} + $r->{'budget_amount_sublevel'} ; -# $r->{budget_alloc} = $r->{'budget_amount'} - $r->{'budget_amount_sublevel'} ; - - # $r->{'budget_amount_sublevel'} ; + $r->{'budget_amount_total'} = $r->{'budget_amount'}; # foreach sub-levels my $unalloc_count ; foreach my $sub (@subs_arr) { my $sub_budget = GetBudget($sub); - # $r->{budget_spent_sublevel} += $bud->{'budget_amount'} ; $r->{budget_spent_sublevel} += GetBudgetSpent( $sub_budget->{'budget_id'} ); - $unalloc_count += $sub_budget->{'budget_amount'} + $sub_budget->{'budget_amount_sublevel'}; - } - - $r->{budget_unalloc_sublevel} = $r->{'budget_amount_sublevel'} - $unalloc_count; - - # (($r->{'budget_amount'} - $r->{'budget_alloc'}) / $r->{'budget_amount'}) * 100; - -=c -# my $percent = $r->{'budget_amount'} ? ( $r->{'budget_alloc'} / $r->{'budget_amount'} ) * 100 : 0; - # my $spent_percent = ( $r->{'budget_spent'} / $r->{'budget_amount'} ) * 100 if $r->{'budget_amount'}; - - # (($r->{'budget_amount'} - $r->{'budget_alloc'}) / $r->{'budget_amount'}) * 100; -# my $percent = ( $r->{'budget_alloc'} / $r->{'budget_amount'} ) * 100 if $r->{'budget_amount'}; -# my $spent_percent = ( $r->{'budget_spent'} / $r->{'budget_amount'} ) * 100 if $r->{'budget_amount'}; - if ($percent == 0) { - $r->{budget_alloc_none} = 1; - } elsif ($percent == 100) { - $r->{budget_alloc_full} = 1 - - } else { - $r->{budget_alloc_percent} = sprintf("%00d", $percent); + $unalloc_count += $sub_budget->{'budget_amount'}; } -=cut - - if ( scalar @subs_arr == 0 && $r->{budget_amount_sublevel} > 0 ) { - $r->{warn_no_subs} = 1; - } } return \@sort; } # ------------------------------------------------------------------- + sub AddBudget { -my ($budget) = @_; -my $dbh = C4::Context->dbh; - my $query = qq| - INSERT INTO aqbudgets - SET budget_code = ?, - budget_period_id = ?, - budget_parent_id = ?, - budget_name = ?, - budget_branchcode = ?, - budget_amount = ?, - budget_amount_sublevel = ?, - budget_encumb = ?, - budget_expend = ?, - budget_notes = ?, - sort1_authcat = ?, - sort2_authcat = ?, - budget_owner_id = ?, - budget_permission = ? - |; - my $sth = $dbh->prepare($query); - $sth->execute( - $budget->{'budget_code'} ? $budget->{'budget_code'} : undef, - $budget->{'budget_period_id'} ? $budget->{'budget_period_id'} : undef, - $budget->{'budget_parent_id'} ? $budget->{'budget_parent_id'} : undef, - $budget->{'budget_name'} ? $budget->{'budget_name'} : undef, - $budget->{'budget_branchcode'} ? $budget->{'budget_branchcode'} : undef, - $budget->{'budget_amount'} ? $budget->{'budget_amount'} : undef, - $budget->{'budget_amount_sublevel'} ? $budget->{'budget_amount_sublevel'} : undef, - $budget->{'budget_encumb'} ? $budget->{'budget_encumb'} : undef, - $budget->{'budget_expend'} ? $budget->{'budget_expend'} : undef, - $budget->{'budget_notes'} ? $budget->{'budget_notes'} : undef, - $budget->{'sort1_authcat'} ? $budget->{'sort1_authcat'} : undef, - $budget->{'sort2_authcat'} ? $budget->{'sort2_authcat'} : undef, - $budget->{'budget_owner_id'} ? $budget->{'budget_owner_id'} : undef, - $budget->{'budget_permission'} ? $budget->{'budget_permission'} : undef, - ); - $sth->finish; + my ($budget) = @_; + return InsertInTable("aqbudgets",$budget); } # ------------------------------------------------------------------- sub ModBudget { my ($budget) = @_; - my $dbh = C4::Context->dbh; - my $query = qq| - UPDATE aqbudgets - SET budget_code = ?, - budget_period_id = ?, - budget_parent_id = ?, - budget_name = ?, - budget_branchcode = ?, - budget_amount = ?, - budget_amount_sublevel = ?, - budget_encumb = ?, - budget_expend = ?, - budget_notes = ?, - sort1_authcat = ?, - sort2_authcat = ?, - budget_owner_id = ?, - budget_permission = ? - WHERE budget_id = ? - |; - - my $sth = $dbh->prepare($query); - $sth->execute( - $budget->{'budget_code'} ? $budget->{'budget_code'} : undef, - $budget->{'budget_period_id'} ? $budget->{'budget_period_id'} : undef, - $budget->{'budget_parent_id'} ? $budget->{'budget_parent_id'} : undef, - $budget->{'budget_name'} ? $budget->{'budget_name'} : undef, - $budget->{'budget_branchcode'} ? $budget->{'budget_branchcode'} : undef, - $budget->{'budget_amount'} ? $budget->{'budget_amount'} : undef, - $budget->{'budget_amount_sublevel'} ? $budget->{'budget_amount_sublevel'} : undef, - $budget->{'budget_encumb'} ? $budget->{'budget_encumb'} : undef, - $budget->{'budget_expend'} ? $budget->{'budget_expend'} : undef, - $budget->{'budget_notes'} ? $budget->{'budget_notes'} : undef, - $budget->{'sort1_authcat'} ? $budget->{'sort1_authcat'} : undef, - $budget->{'sort2_authcat'} ? $budget->{'sort2_authcat'} : undef, - $budget->{'budget_owner_id'} ? $budget->{'budget_owner_id'} : undef, - $budget->{'budget_permission'} ? $budget->{'budget_permission'} : undef, - $budget->{'budget_id'}, - ); - $sth->finish; + return UpdateInTable("aqbudgets",$budget); } # ------------------------------------------------------------------- @@ -792,7 +627,6 @@ sub DelBudget { my $dbh = C4::Context->dbh; my $sth = $dbh->prepare("delete from aqbudgets where budget_id=?"); my $rc = $sth->execute($budget_id); - $sth->finish; return $rc; } @@ -820,7 +654,6 @@ get a specific budget sub GetBudget { my ( $budget_id ) = @_; my $dbh = C4::Context->dbh; - my $query; my $query = " SELECT * FROM aqbudgets @@ -836,7 +669,7 @@ sub GetBudget { =over 4 -&GetBudget($budget_id); +&GetBudgets($filter, $order_by); gets all budgets @@ -846,26 +679,8 @@ gets all budgets # ------------------------------------------------------------------- sub GetBudgets { - my ($active) = @_; - my $dbh = C4::Context->dbh; - my $q = "SELECT * from aqbudgets"; - my $row; - my $sth; - unless ($active) { - $sth = $dbh->prepare($q); - $sth->execute(); - } else { - $q = "select budget_period_id from aqbudgetperiods where budget_period_active = 1 "; - $sth = $dbh->prepare($q); - $sth->execute(); - $row = $sth->fetchrow_hashref(); - $q = "select * from aqbudgets WHERE budget_period_id =? "; - $sth = $dbh->prepare($q); - $sth->execute( $row->{'budget_period_id'} ); - } - my $results = $sth->fetchall_arrayref( {} ); - $sth->finish; - return $results; + my ($filters,$orderby) = @_; + return SearchInTable("aqbudgets",$filters, $orderby, undef,undef, undef, "wide"); } # ------------------------------------------------------------------- @@ -893,7 +708,6 @@ sub GetCurrencies { while ( my $data = $sth->fetchrow_hashref ) { push( @results, $data ); } - $sth->finish; return @results; } @@ -906,7 +720,6 @@ sub GetCurrency { my $sth = $dbh->prepare($query); $sth->execute; my $r = $sth->fetchrow_hashref; - $sth->finish; return $r; } @@ -960,6 +773,34 @@ sub ConvertCurrency { return ( $price / $cur ); } +=item + returns an array containing fieldname followed by PRI as value if PRIMARY Key +=cut +sub _columns(;$) { + my $tablename=shift||"aqbudgets"; + return @{C4::Context->dbh->selectcol_arrayref("SHOW columns from $tablename",{Columns=>[1,4]})}; +} + +sub _filter_fields{ + my $budget=shift; + my $tablename=shift; + my @keys; + my @values; + my %columns= _columns($tablename); + #Filter Primary Keys of table + my $elements=join "|",grep {$columns{$_} ne "PRI"} keys %columns; + foreach my $field (grep {/\b($elements)\b/} keys %$budget){ + $$budget{$field}=format_date_in_iso($$budget{$field}) if ($field=~/date/ && $$budget{$field} !~C4::Dates->regexp("iso")); + my $strkeys= " $field = ? "; + if ($field=~/branch/){ + $strkeys="( $strkeys OR $field='' OR $field IS NULL) "; + } + push @values, $$budget{$field}; + push @keys, $strkeys; + } + return (\@keys,\@values); +} + END { } # module clean-up code here (global destructor) 1;