Skip to content

Commit

Permalink
ACQUI-97: Add logic for handling fund allocations
Browse files Browse the repository at this point in the history
This patch adds logic to the "store" method for fund allocations to update the fund value. It also adds a new property to the funds table to hold the total value of the fund so that this can be displayed in the ledgers components where appropriate without the need for multiple layers of data embedding
  • Loading branch information
mblenk committed Mar 1, 2024
1 parent a246908 commit 31b7419
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 ];
Expand Down Expand Up @@ -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{}
Expand Down
20 changes: 0 additions & 20 deletions Koha/Plugin/Acquire/Controllers/FundManagement/Funds.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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($_);
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
1 change: 1 addition & 0 deletions Koha/Plugin/Acquire/installer/sql/fundManagement.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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`),
Expand Down
10 changes: 5 additions & 5 deletions Koha/Plugin/Acquire/lib/Koha/Acquire/Funds/Fund.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
32 changes: 32 additions & 0 deletions Koha/Plugin/Acquire/lib/Koha/Acquire/Funds/FundAllocation.pm
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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",
Expand Down
1 change: 0 additions & 1 deletion src/components/FundManagement/FundFormAdd.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
placeholder="Description"
rows="10"
cols="50"
required
/>
</li>
<li>
Expand Down
4 changes: 2 additions & 2 deletions src/components/FundManagement/FundList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,12 @@ export default {
},
{
title: __("Fund value"),
data: "fund_total",
data: "fund_value",
searchable: true,
orderable: true,
render: function (data, type, row, meta) {
const { symbol } = getCurrency(row.currency)
return symbol + row.fund_total
return symbol + row.fund_value
},
},
]
Expand Down
22 changes: 18 additions & 4 deletions src/components/FundManagement/FundShow.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@
/>
</Toolbar>
<h2>{{ "Fund " + fund.fund_id }}</h2>
<h3>{{ "Current total: " + currency.symbol + fund.fund_total }}</h3>
<ValueHeader
:symbol="currency.symbol"
:value="fund.fund_value"
:key="forceRender"
/>
<DisplayDataFields
:data="fund"
homeRoute="FundList"
Expand Down Expand Up @@ -52,6 +56,7 @@ import { inject, ref } from "vue"
import { APIClient } from "../../fetch/api-client.js"
import DisplayDataFields from "../DisplayDataFields.vue"
import KohaTable from "../KohaTable.vue"
import ValueHeader from './ValueHeader.vue'
export default {
setup() {
Expand Down Expand Up @@ -91,7 +96,8 @@ export default {
},
},
fundTotal: 0,
currency: null
currency: null,
forceRender: 'no'
}
},
beforeRouteEnter(to, from, next) {
Expand Down Expand Up @@ -151,17 +157,20 @@ export default {
},
() => {
const client = APIClient.acquisition
client.tasks.delete(fund_allocation.fund_allocation_id).then(
client.fund_allocations.delete(fund_allocation.fund_allocation_id).then(
success => {
this.setMessage(`Fund allocation deleted`, true)
dt.draw()
this.fund.fund_value = this.fund.fund_value - fund_allocation.allocation_amount
this.forceRender = 'yes'
},
error => {}
)
}
)
},
getTableColumns: function () {
const getCurrency = this.getCurrency
return [
{
title: __("Allocation count"),
Expand Down Expand Up @@ -192,6 +201,10 @@ export default {
data: "new_fund_value",
searchable: true,
orderable: true,
render: function (data, type, row, meta) {
const { symbol } = getCurrency(row.currency)
return symbol + row.new_fund_value
},
},
{
title: __("Reference"),
Expand All @@ -214,7 +227,8 @@ export default {
DisplayDataFields,
Toolbar,
ToolbarButton,
KohaTable
KohaTable,
ValueHeader
},
}
</script>
Expand Down
27 changes: 22 additions & 5 deletions src/components/FundManagement/LedgerList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export default {
const acquisitionsStore = inject("acquisitionsStore")
const {
isUserPermitted,
getCurrency
} = acquisitionsStore
const table = ref()
Expand All @@ -45,7 +46,8 @@ export default {
table,
setConfirmationDialog,
setMessage,
isUserPermitted
isUserPermitted,
getCurrency
}
},
data() {
Expand All @@ -58,6 +60,7 @@ export default {
tableOptions: {
columns: this.getTableColumns(),
url: "/api/v1/contrib/acquire/ledgers",
options: { embed: "koha_plugin_acquire_funds" },
table_settings: null,
add_filters: true,
actions: {
Expand Down Expand Up @@ -113,6 +116,7 @@ export default {
)
},
getTableColumns: function () {
const getCurrency = this.getCurrency
return [
{
title: __("Name"),
Expand All @@ -136,18 +140,31 @@ export default {
orderable: true,
},
{
title: __("Description"),
data: "description",
title: __("Status"),
data: "status",
searchable: true,
orderable: true,
render: function (data, type, row, meta) {
return row.status ? "Active" : "Inactive"
},
},
{
title: __("Status"),
title: __("Fund count"),
data: "status",
searchable: true,
orderable: true,
render: function (data, type, row, meta) {
return row.status ? "Active" : "Inactive"
return row.koha_plugin_acquire_funds.length
},
},
{
title: __("Ledger value"),
searchable: true,
orderable: true,
render: function (data, type, row, meta) {
const sum = row.koha_plugin_acquire_funds.reduce((acc, curr) => acc + curr.fund_value, 0)
const { symbol } = getCurrency(row.currency)
return symbol + sum
},
},
]
Expand Down
2 changes: 1 addition & 1 deletion src/components/FundManagement/LedgerShow.vue
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export default {
orderable: true,
render: function (data, type, row, meta) {
const { symbol } = getCurrency(row.currency)
return symbol + row.fund_total
return symbol + row.fund_value
},
},
]
Expand Down
12 changes: 12 additions & 0 deletions src/components/FundManagement/ValueHeader.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<template>
<h3>{{ "Current total: " + symbol + value }}</h3>
</template>

<script>
export default {
props: {
symbol: String,
value: Number
}
}
</script>

0 comments on commit 31b7419

Please sign in to comment.