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 />
  • diff --git a/src/components/FundManagement/FundList.vue b/src/components/FundManagement/FundList.vue index b9e19de..70c1f5c 100644 --- a/src/components/FundManagement/FundList.vue +++ b/src/components/FundManagement/FundList.vue @@ -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 }, }, ] diff --git a/src/components/FundManagement/FundShow.vue b/src/components/FundManagement/FundShow.vue index c47751f..9e1fabe 100644 --- a/src/components/FundManagement/FundShow.vue +++ b/src/components/FundManagement/FundShow.vue @@ -23,7 +23,11 @@ />

    {{ "Fund " + fund.fund_id }}

    -

    {{ "Current total: " + currency.symbol + fund.fund_total }}

    + { 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 => {} ) @@ -162,6 +170,7 @@ export default { ) }, getTableColumns: function () { + const getCurrency = this.getCurrency return [ { title: __("Allocation count"), @@ -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"), @@ -214,7 +227,8 @@ export default { DisplayDataFields, Toolbar, ToolbarButton, - KohaTable + KohaTable, + ValueHeader }, } diff --git a/src/components/FundManagement/LedgerList.vue b/src/components/FundManagement/LedgerList.vue index ee16155..9ed356e 100644 --- a/src/components/FundManagement/LedgerList.vue +++ b/src/components/FundManagement/LedgerList.vue @@ -37,6 +37,7 @@ export default { const acquisitionsStore = inject("acquisitionsStore") const { isUserPermitted, + getCurrency } = acquisitionsStore const table = ref() @@ -45,7 +46,8 @@ export default { table, setConfirmationDialog, setMessage, - isUserPermitted + isUserPermitted, + getCurrency } }, data() { @@ -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: { @@ -113,6 +116,7 @@ export default { ) }, getTableColumns: function () { + const getCurrency = this.getCurrency return [ { title: __("Name"), @@ -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 }, }, ] diff --git a/src/components/FundManagement/LedgerShow.vue b/src/components/FundManagement/LedgerShow.vue index 73b0a9f..971c4a2 100644 --- a/src/components/FundManagement/LedgerShow.vue +++ b/src/components/FundManagement/LedgerShow.vue @@ -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 }, }, ] diff --git a/src/components/FundManagement/ValueHeader.vue b/src/components/FundManagement/ValueHeader.vue new file mode 100644 index 0000000..0b20707 --- /dev/null +++ b/src/components/FundManagement/ValueHeader.vue @@ -0,0 +1,12 @@ + + + \ No newline at end of file