Skip to content

Commit

Permalink
ACQUI-136: Allow for transfers to sub funds
Browse files Browse the repository at this point in the history
  • Loading branch information
mblenk committed Mar 28, 2024
1 parent 46ea677 commit c43b790
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 57 deletions.
22 changes: 18 additions & 4 deletions Koha/Plugin/Acquire/Controllers/FundManagement/FundAllocations.pm
Original file line number Diff line number Diff line change
Expand Up @@ -234,14 +234,27 @@ sub transfer {

my $fund_transferring_from = Koha::Acquire::Funds::Funds->find( { fund_id => $body->{fund_id_from} } );
my $fund_transferring_to = Koha::Acquire::Funds::Funds->find( { fund_id => $body->{fund_id_to} } );
my $note_from = "Transfer to " . $fund_transferring_to->name;
my $sub_fund_transferring_from =
Koha::Acquire::Funds::SubFunds->find( { sub_fund_id => $body->{sub_fund_id_from} } );
my $sub_fund_transferring_to =
Koha::Acquire::Funds::SubFunds->find( { sub_fund_id => $body->{sub_fund_id_to} } );

my $note_from = "Transfer to "
. ( $sub_fund_transferring_to ? $sub_fund_transferring_to->name : $fund_transferring_to->name );
$note_from = $note_from . ": " . $body->{note} if $body->{note};
my $note_to = "Transfer from " . $fund_transferring_from->name;
my $note_to =
"Transfer from "
. (
$sub_fund_transferring_from ? $sub_fund_transferring_from->name : $fund_transferring_from->name );
$note_to = $note_to . ": " . $body->{note} if $body->{note};

my $fund_id_from = $body->{sub_fund_id_from} ? undef : $body->{fund_id_from};
my $fund_id_to = $body->{sub_fund_id_to} ? undef : $body->{fund_id_to};

my $allocation_from = Koha::Acquire::Funds::FundAllocation->new(
{
fund_id => $body->{fund_id_from},
fund_id => $fund_id_from,
sub_fund_id => $body->{sub_fund_id_from},
ledger_id => $fund_transferring_from->ledger_id,
fiscal_yr_id => $fund_transferring_from->fiscal_yr_id,
allocation_amount => -$body->{transfer_amount},
Expand All @@ -255,7 +268,8 @@ sub transfer {
)->store();
my $allocation_to = Koha::Acquire::Funds::FundAllocation->new(
{
fund_id => $body->{fund_id_to},
fund_id => $fund_id_to,
sub_fund_id => $body->{sub_fund_id_to},
ledger_id => $fund_transferring_to->ledger_id,
fiscal_yr_id => $fund_transferring_to->fiscal_yr_id,
allocation_amount => $body->{transfer_amount},
Expand Down
38 changes: 23 additions & 15 deletions src/components/FundManagement/FundAllocationFormAdd.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,25 @@
<h2 v-else>New fund allocation</h2>
<div>
<form @submit="onSubmit($event)">
<h3>Allocate to fund: {{ selectedFund.name }}</h3>
<fieldset class="rows">
<ol>
<li>
<!-- <li>
<label for="fund_allocation_fund_id" class="required"
>Fund:</label
>
<InfiniteScrollSelect
id="fund_allocation_fund_id"
v-model="fund_allocation.fund_id"
v-model="fund_allocation[isSubFund ? 'sub_fund_id' : 'fund_id']"
:selectedData="selectedFund"
dataType="funds"
dataIdentifier="fund_id"
:dataType="isSubFund ? 'sub_funds' : 'funds'"
:dataIdentifier="isSubFund ? 'sub_fund_id' : 'fund_id'"
label="name"
apiClient="acquisition"
:required="true"
/>
<span class="required">Required</span>
</li>
</li> -->
<li>
<label for="fund_allocation_amount" class="required"
>Allocation amount:</label
Expand Down Expand Up @@ -98,6 +99,7 @@ export default {
initialized: false,
fund_allocation: {
fund_id: null,
sub_fund_id: null,
fiscal_yr_id: null,
ledger_id: null,
reference: '',
Expand All @@ -107,7 +109,8 @@ export default {
visible_to: '',
},
funds: [],
selectedFund: null
selectedFund: null,
isSubFund: false
}
},
beforeRouteEnter(to, from, next) {
Expand All @@ -132,16 +135,21 @@ export default {
})
},
async getFund(params) {
const { fund_id, sub_fund_id } = params
const whichClient = sub_fund_id ? 'subFunds' : 'funds'
const whichParam = sub_fund_id ? 'sub_fund_id' : 'fund_id'
if(sub_fund_id) this.isSubFund = true
const client = APIClient.acquisition
await client.funds.get(params.fund_id).then(
fund => {
this.selectedFund = fund
this.fund_allocation.fund_id = fund.fund_id
this.fund_allocation.ledger_id = fund.ledger_id
this.fund_allocation.fiscal_yr_id = fund.fiscal_yr_id
this.fund_allocation.currency = fund.currency
this.fund_allocation.owner = fund.owner
this.fund_allocation.visible_to = fund.visible_to
await client[whichClient].get(params[whichParam]).then(
result => {
this.selectedFund = result
this.fund_allocation[whichParam] = result[whichParam]
this.fund_allocation.ledger_id = result.ledger_id
this.fund_allocation.fiscal_yr_id = result.fiscal_yr_id
this.fund_allocation.currency = result.currency
this.fund_allocation.owner = result.owner
this.fund_allocation.visible_to = result.visible_to
},
error => {}
)
Expand Down
111 changes: 73 additions & 38 deletions src/components/FundManagement/TransferFunds.vue
Original file line number Diff line number Diff line change
@@ -1,61 +1,79 @@
<template>
<div v-if="!initialized">Loading...</div>
<div v-else id="fund_allocation_add">
<div v-else id="fund_transfer_add">
<h2>Transfer between funds</h2>
<div>
<form @submit="onSubmit($event)">
<fieldset class="rows">
<ol>
<li>
<label for="fund_allocation_fund_id" class="required"
<label for="fund_transfer_fund_id" class="required"
>Fund to transfer to:</label
>
<v-select
id="fund_allocation_fund_id"
<InfiniteScrollSelect
id="fund_transfer_fund_id"
v-model="fund_transfer.fund_id_to"
:reduce="av => av.fund_id"
:options="funds"
dataType="funds"
dataIdentifier="fund_id"
label="name"
apiClient="acquisition"
:required="!fund_transfer.fund_id_to"
:filters="{ fund_id: { 'not_in': [ $route.query.fund_id ] }, status: '1'}"
@update:modelValue="handleSubFunds"
/>
<span class="required">Required</span>
</li>
<li>
<label for="fund_transfer_sub_fund" :class="noSubFunds ? '' :'required'"
>Sub fund:</label
>
<v-select
id="fund_transfer_sub_fund"
v-model="fund_transfer.sub_fund_id_to"
:reduce="av => av.sub_fund_id"
:options="subFunds"
label="name"
:disabled="noSubFunds"
>
<template #search="{ attributes, events }">
<input
:required="!fund_transfer.fund_id_to"
:required="!noSubFunds ? false : !fund_transfer.sub_fund_it_to"
class="vs__search"
v-bind="attributes"
v-on="events"
/>
</template>
</v-select>
<span class="required">Required</span>
<span v-if="!noSubFunds" class="required">Required</span>
</li>
<li>
<label for="fund_allocation_amount" class="required"
<label for="fund_transfer_amount" class="required"
>Allocation amount:</label
>
<input
id="fund_allocation_amount"
id="fund_transfer_amount"
v-model="fund_transfer.transfer_amount"
type="number"
step=".01"
/>
<span class="required">Required</span>
</li>
<li>
<label for="fund_allocation_reference"
<label for="fund_transfer_reference"
>Reference:</label
>
<input
id="fund_allocation_reference"
id="fund_transfer_reference"
v-model="fund_transfer.reference"
placeholder="Fund transfer reference"
/>
</li>
<li>
<label for="fund_allocation_note"
<label for="fund_transfer_note"
>Note:
</label>
<textarea
id="fund_allocation_note"
id="fund_transfer_note"
v-model="fund_transfer.note"
placeholder="Notes"
rows="10"
Expand All @@ -65,9 +83,9 @@
</ol>
</fieldset>
<fieldset class="action">
<input type="submit" value="Submit" />
<input type="submit" value="Submit" :disabled="stopSubmit" />
<router-link
:to="{ name: 'FundShow', params: { fund_id: fund_transfer.fund_id_from } }"
:to="{ name: 'FundShow', params: { fund_id: $route.query.fund_id } }"
role="button"
class="cancel"
>Cancel</router-link
Expand All @@ -83,6 +101,7 @@
import { inject } from "vue"
import { APIClient } from "../../fetch/api-client.js"
import { setMessage, setWarning } from "../../messages"
import InfiniteScrollSelect from '../InfiniteScrollSelect.vue'
export default {
setup() {
Expand All @@ -96,37 +115,50 @@ export default {
}
},
data() {
const { sub_fund_id, fund_id } = this.$route.query
const fund_transfer = {
fund_id_from: null,
sub_fund_id_from: null,
fund_id_to: null,
sub_fund_id_to: null,
reference: '',
note: '',
transfer_amount: null,
}
if(sub_fund_id) fund_transfer.sub_fund_id_from = parseInt(sub_fund_id)
if(fund_id && !sub_fund_id) fund_transfer.fund_id_from = parseInt(fund_id)
return {
initialized: false,
fund_transfer: {
fund_id_from: null,
fund_id_to: null,
reference: '',
note: '',
transfer_amount: null,
},
funds: [],
initialized: true,
fund_transfer,
subFunds: [],
noSubFunds: true,
stopSubmit: false
}
},
beforeRouteEnter(to, from, next) {
next(vm => {
vm.getFunds(to)
})
},
methods: {
async getFunds(route) {
const { query } = route
async getFund(fund_id) {
const client = APIClient.acquisition
await client.funds.getAll(null, {}).then(
funds => {
const query_id = parseInt(query.fund_id)
this.funds = funds.filter(fund => (fund.fund_id !== query_id && fund.status))
this.fund_transfer.fund_id_from = query_id
this.initialized = true
await client.funds.get(fund_id, { "x-koha-embed": "koha_plugin_acquire_sub_funds" }).then(
fund => {
this.selectedFund = fund
if(fund.koha_plugin_acquire_sub_funds && fund.koha_plugin_acquire_sub_funds.length > 0) {
this.noSubFunds = false
this.subFunds = fund.koha_plugin_acquire_sub_funds
}
},
error => {}
)
},
async handleSubFunds() {
this.stopSubmit = true
await this.getFund(this.fund_transfer.fund_id_to)
if(this.selectedFund.koha_plugin_acquire_sub_funds && this.selectedFund.koha_plugin_acquire_sub_funds.length > 0) {
this.noSubFunds = false
}
this.stopSubmit = false
},
onSubmit(e) {
e.preventDefault()
Expand All @@ -147,6 +179,9 @@ export default {
error => {}
)
}
},
components: {
InfiniteScrollSelect
}
}
</script>
Expand Down

0 comments on commit c43b790

Please sign in to comment.