diff --git a/Koha/Plugin/Acquire/Controllers/FundManagement/FundAllocations.pm b/Koha/Plugin/Acquire/Controllers/FundManagement/FundAllocations.pm index c3e034f..c63cb3b 100644 --- a/Koha/Plugin/Acquire/Controllers/FundManagement/FundAllocations.pm +++ b/Koha/Plugin/Acquire/Controllers/FundManagement/FundAllocations.pm @@ -49,7 +49,8 @@ sub list { Koha::Plugin::Acquire::Controllers::ControllerUtils->filter_data_by_group( { dataset => $fund_allocations } ); my @sorted_allocations = - sort { $a->{fund_allocation_id} cmp $b->{fund_allocation_id} } @{$filtered_fund_allocations}; + sort { $a->{fund_allocation_id} <=> $b->{fund_allocation_id} } @{$filtered_fund_allocations}; + my $total = 0; foreach my $allocation_index ( 1 .. scalar(@sorted_allocations) ) { my $allocation = $sorted_allocations[ $allocation_index - 1 ]; @@ -205,7 +206,9 @@ sub delete { } return try { + my $fund_id = $fund_allocation->fund_id; $fund_allocation->delete; + return $c->render( status => 204, openapi => q{} diff --git a/Koha/Plugin/Acquire/Controllers/FundManagement/Funds.pm b/Koha/Plugin/Acquire/Controllers/FundManagement/Funds.pm index 266bebb..75a63c6 100644 --- a/Koha/Plugin/Acquire/Controllers/FundManagement/Funds.pm +++ b/Koha/Plugin/Acquire/Controllers/FundManagement/Funds.pm @@ -49,10 +49,6 @@ sub list { my $filtered_funds = Koha::Plugin::Acquire::Controllers::ControllerUtils->filter_data_by_group( { dataset => $funds } ); - foreach my $fund (@$filtered_funds) { - $fund = _fund_total($fund); - } - return $c->render( status => 200, openapi => $filtered_funds ); } catch { $c->unhandled_exception($_); @@ -82,7 +78,6 @@ sub get { { data => $fund, field => 'owned_by', key => "owner" } ); $fund = Koha::Plugin::Acquire::Controllers::ControllerUtils->add_lib_group_data( { data => $fund } ); - $fund = _fund_total($fund); return $c->render( status => 200, @@ -226,19 +221,4 @@ sub _inherit_currency_and_owner { return $fund; } -sub _fund_total { - my ($fund) = @_; - - my $allocations = $fund->{koha_plugin_acquire_fund_allocations} || (); - my $total = 0; - - foreach my $allocation (@$allocations) { - $total += $allocation->{allocation_amount}; - }; - - $fund->{fund_total} = $total; - - return $fund; -} - 1; diff --git a/Koha/Plugin/Acquire/installer/sql/fundManagement.sql b/Koha/Plugin/Acquire/installer/sql/fundManagement.sql index b229c1e..ae4df03 100644 --- a/Koha/Plugin/Acquire/installer/sql/fundManagement.sql +++ b/Koha/Plugin/Acquire/installer/sql/fundManagement.sql @@ -45,6 +45,7 @@ CREATE TABLE IF NOT EXISTS { { funds } } ( `currency` VARCHAR(10) DEFAULT '' COMMENT 'currency of the fund', `status` TINYINT(1) DEFAULT '1' COMMENT 'is the fund currently active', `owner` INT(11) DEFAULT NULL COMMENT 'owner of the fund', + `fund_value` decimal(28,6) DEFAULT 0.000000 COMMENT 'value of the fund', `last_updated` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp() COMMENT 'time of the last update to the fund', `visible_to` VARCHAR(255) DEFAULT '' COMMENT 'library groups the fund is visible to', PRIMARY KEY (`fund_id`), diff --git a/Koha/Plugin/Acquire/lib/Koha/Acquire/Funds/Fund.pm b/Koha/Plugin/Acquire/lib/Koha/Acquire/Funds/Fund.pm index de68ce1..68030bc 100644 --- a/Koha/Plugin/Acquire/lib/Koha/Acquire/Funds/Fund.pm +++ b/Koha/Plugin/Acquire/lib/Koha/Acquire/Funds/Fund.pm @@ -23,20 +23,20 @@ use base qw(Koha::Object); use Mojo::JSON qw(decode_json); use JSON qw ( encode_json ); -=head3 fund_total +=head3 update_fund_total =cut -sub fund_total { +sub update_fund_total { my ( $self, $args ) = @_; - my $allocations = $self->koha_plugin_acquire_fund_allocations; + my @allocations = $self->koha_plugin_acquire_fund_allocations->as_list; my $total = 0; - foreach my $allocation ( @$allocations ) { + foreach my $allocation ( @allocations ) { $total += $allocation->allocation_amount; } - + $self->fund_value($total)->store; return $total; } diff --git a/Koha/Plugin/Acquire/lib/Koha/Acquire/Funds/FundAllocation.pm b/Koha/Plugin/Acquire/lib/Koha/Acquire/Funds/FundAllocation.pm index fbb6053..d399d60 100644 --- a/Koha/Plugin/Acquire/lib/Koha/Acquire/Funds/FundAllocation.pm +++ b/Koha/Plugin/Acquire/lib/Koha/Acquire/Funds/FundAllocation.pm @@ -23,6 +23,38 @@ use base qw(Koha::Object); use Mojo::JSON qw(decode_json); use JSON qw ( encode_json ); +use Koha::Acquire::Funds::Fund; + +=head3 store + +=cut + +sub store { + my ($self, $args) = @_; + + $self->SUPER::store; + + my $fund = $self->fund; + $fund->update_fund_total; + + return $self; +} + +=head3 delete + +=cut + +sub delete { + my ( $self, $args ) = @_; + + my $deleted = $self->_result()->delete; + + my $fund = $self->fund; + $fund->update_fund_total; + + return $self; +} + =head3 fiscal_yr Method to embed the fiscal year to a given fund allocation diff --git a/Koha/Plugin/Acquire/lib/Koha/Schema/Result/KohaPluginAcquireFund.pm b/Koha/Plugin/Acquire/lib/Koha/Schema/Result/KohaPluginAcquireFund.pm index 7a5b15a..5b742aa 100644 --- a/Koha/Plugin/Acquire/lib/Koha/Schema/Result/KohaPluginAcquireFund.pm +++ b/Koha/Plugin/Acquire/lib/Koha/Schema/Result/KohaPluginAcquireFund.pm @@ -116,6 +116,16 @@ is the fund currently active owner of the fund + +=head2 fund_value + + data_type: 'decimal' + default_value: 0.000000 + is_nullable: 1 + size: [28,6] + +value of the fund + =head2 last_updated data_type: 'timestamp' @@ -159,6 +169,13 @@ __PACKAGE__->add_columns( { data_type => "tinyint", default_value => 1, is_nullable => 1 }, "owner", { data_type => "integer", is_foreign_key => 1, is_nullable => 1 }, + "fund_value", + { + data_type => "decimal", + default_value => "0.000000", + is_nullable => 1, + size => [ 28, 6 ], + }, "last_updated", { data_type => "timestamp", diff --git a/src/components/FundManagement/FundFormAdd.vue b/src/components/FundManagement/FundFormAdd.vue index 7bba0e8..0ab1500 100644 --- a/src/components/FundManagement/FundFormAdd.vue +++ b/src/components/FundManagement/FundFormAdd.vue @@ -31,7 +31,6 @@ placeholder="Description" rows="10" cols="50" - required />