Skip to content

Commit

Permalink
ACQUI-129: Add an update_sub_fund_value method chain to parent data
Browse files Browse the repository at this point in the history
This commit adds  the update_sub_fund_value method and chains it to the fund allocation store method
  • Loading branch information
mblenk committed Mar 27, 2024
1 parent bb802d0 commit 8b20e9b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
12 changes: 10 additions & 2 deletions Koha/Plugin/Acquire/lib/Koha/Acquire/Funds/Fund.pm
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,16 @@ It updates the value of the fund based on the fund allocations and then triggers
sub update_fund_total {
my ( $self, $args ) = @_;

my @allocations = $self->koha_plugin_acquire_fund_allocations->as_list;
my $total = 0;
my @allocations;
if ( $self->has_sub_funds ) {
my @sub_funds = $self->koha_plugin_acquire_sub_funds->as_list;
foreach my $sub_fund (@sub_funds) {
push( @allocations, $sub_fund->koha_plugin_acquire_fund_allocations->as_list );
}
} else {
push( @allocations, $self->koha_plugin_acquire_fund_allocations->as_list );
}
my $total = 0;

foreach my $allocation (@allocations) {
$total += $allocation->allocation_amount;
Expand Down
18 changes: 17 additions & 1 deletion Koha/Plugin/Acquire/lib/Koha/Acquire/Funds/FundAllocation.pm
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ sub store {

if(!$block_fund_value_update) {
my $fund = $self->fund;
$fund->update_fund_total;
$fund->update_fund_total if $fund && !$self->sub_fund_id;
my $sub_fund = $self->sub_fund;
$sub_fund->update_sub_fund_total if $sub_fund;
}

return $self;
Expand Down Expand Up @@ -93,9 +95,23 @@ Method to embed the fund to a given fund allocation
sub fund {
my ($self) = @_;
my $fund_rs = $self->_result->fund;
return unless $fund_rs;
return Koha::Acquire::Funds::Fund->_new_from_dbic($fund_rs);
}

=head3 sub_fund
Method to embed the sub_fund to a given fund allocation
=cut

sub sub_fund {
my ($self) = @_;
my $sub_fund_rs = $self->_result->sub_fund;
return unless $sub_fund_rs;
return Koha::Acquire::Funds::SubFund->_new_from_dbic($sub_fund_rs);
}

=head2 Internal methods
=head3 _type
Expand Down
22 changes: 10 additions & 12 deletions Koha/Plugin/Acquire/lib/Koha/Acquire/Funds/SubFund.pm
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ package Koha::Acquire::Funds::SubFund;
# along with Koha; if not, see <http://www.gnu.org/licenses>.

use Modern::Perl;
use base qw(Koha::Acquire::Funds::Fund);
use base qw(Koha::Object);


use Mojo::JSON qw(decode_json);
use JSON qw ( encode_json );
Expand Down Expand Up @@ -46,10 +47,8 @@ sub delete {

my $deleted = $self->_result()->delete;

# TODO: Needs a call to update the parent fund

my $ledger = $self->ledger;
$ledger->update_ledger_total;
my $fund = $self->fund;
$fund->update_fund_total;

return $self;
}
Expand Down Expand Up @@ -87,15 +86,14 @@ sub cascade_to_fund_allocations {
}
}

=head3 update_fund_total
=head3 update_sub_fund_total
This method is called whenever a fund allocation is made.
It updates the value of the fund based on the fund allocations and then triggers an update to the ledger value
=cut

sub update_fund_total {
# TODO: Needs to work with fund and sub fund level
sub update_sub_fund_total {
my ( $self, $args ) = @_;

my @allocations = $self->koha_plugin_acquire_fund_allocations->as_list;
Expand All @@ -104,10 +102,10 @@ sub update_fund_total {
foreach my $allocation (@allocations) {
$total += $allocation->allocation_amount;
}
$self->fund_value($total)->store;
$self->sub_fund_value($total)->store;

my $ledger = $self->ledger;
$ledger->update_ledger_total;
my $fund = $self->fund;
$fund->update_fund_total;

return $total;
}
Expand Down Expand Up @@ -145,7 +143,7 @@ Method to embed the fund to a given sub fund
sub fund {
my ($self) = @_;
my $fund_rs = $self->_result->fund;
return Koha::Acquire::Funds::Ledger->_new_from_dbic($fund_rs);
return Koha::Acquire::Funds::Fund->_new_from_dbic($fund_rs);
}

=head3 koha_plugin_acquire_fund_allocations
Expand Down

0 comments on commit 8b20e9b

Please sign in to comment.