2
0
mirror of https://github.com/frappe/books.git synced 2025-02-02 12:08:27 +00:00

fix: remove account from CoA without refresh

- prevent opening datetime if readonly
This commit is contained in:
18alantom 2022-12-14 16:10:02 +05:30
parent d1c2b17ae3
commit 6b8c74c35f
7 changed files with 83 additions and 37 deletions

View File

@ -8,7 +8,7 @@
<div <div
:class="[containerClasses, sizeClasses]" :class="[containerClasses, sizeClasses]"
class="flex" class="flex"
@click="togglePopover" @click="() => !isReadOnly && togglePopover()"
> >
<p <p
:class="[baseInputClasses]" :class="[baseInputClasses]"

View File

@ -145,7 +145,7 @@ import PageHeader from 'src/components/PageHeader';
import { fyo } from 'src/initFyo'; import { fyo } from 'src/initFyo';
import { docsPathMap } from 'src/utils/misc'; import { docsPathMap } from 'src/utils/misc';
import { docsPath, openQuickEdit } from 'src/utils/ui'; import { docsPath, openQuickEdit } from 'src/utils/ui';
import { getMapFromList } from 'utils/index'; import { getMapFromList, removeAtIndex } from 'utils/index';
import { nextTick } from 'vue'; import { nextTick } from 'vue';
import { handleErrorWithDialog } from '../errorHandling'; import { handleErrorWithDialog } from '../errorHandling';
@ -175,6 +175,7 @@ export default {
if (fyo.store.isDevelopment) { if (fyo.store.isDevelopment) {
window.coa = this; window.coa = this;
} }
docsPath.value = docsPathMap.ChartOfAccounts; docsPath.value = docsPathMap.ChartOfAccounts;
if (this.refetchTotals) { if (this.refetchTotals) {
@ -229,13 +230,52 @@ export default {
return; return;
} }
await openQuickEdit({
schemaName: ModelNameEnum.Account,
name: account.name,
});
const doc = await fyo.doc.getDoc(ModelNameEnum.Account, account.name); const doc = await fyo.doc.getDoc(ModelNameEnum.Account, account.name);
doc.once('afterDelete', () => this.fetchAccounts()); this.setOpenAccountDocListener(doc, account);
await openQuickEdit({ doc });
},
setOpenAccountDocListener(doc, account, parentAccount) {
if (doc.hasListener('afterDelete')) {
return;
}
doc.once('afterDelete', () => {
this.removeAccount(doc.name, account, parentAccount);
});
},
removeAccount(name, account, parentAccount) {
if (account == null && parentAccount == null) {
return;
}
if (account == null) {
account = parentAccount.children.find((ch) => ch.name === name);
}
if (account == null) {
return;
}
const indices = account.location.slice(1).map((i) => Number(i));
let i = Number(account.location[0]);
let parent = this.accounts[i];
let children = this.accounts[i].children;
while (indices.length > 1) {
i = indices.shift();
parent = children[i];
children = children[i].children;
}
i = indices[0];
if (children[i].name !== name) {
return;
}
parent.children = removeAtIndex(children, i);
}, },
async toggleChildren(account) { async toggleChildren(account) {
const hasChildren = await this.fetchChildren(account); const hasChildren = await this.fetchChildren(account);
@ -263,13 +303,7 @@ export default {
filters: { filters: {
parentAccount: parent, parentAccount: parent,
}, },
fields: [ fields: ['name', 'parentAccount', 'isGroup', 'rootType', 'accountType'],
'name',
'parentAccount',
'isGroup',
'rootType',
'accountType',
],
orderBy: 'name', orderBy: 'name',
order: 'asc', order: 'asc',
}); });
@ -307,17 +341,17 @@ export default {
this.insertingAccount = true; this.insertingAccount = true;
const accountName = this.newAccountName.trim(); const accountName = this.newAccountName.trim();
let account = await fyo.doc.getNewDoc('Account'); const doc = await fyo.doc.getNewDoc('Account');
try { try {
let { name, rootType, accountType } = parentAccount; let { name, rootType, accountType } = parentAccount;
await account.set({ await doc.set({
name: accountName, name: accountName,
parentAccount: name, parentAccount: name,
rootType, rootType,
accountType, accountType,
isGroup, isGroup,
}); });
await account.sync(); await doc.sync();
// turn off editing // turn off editing
parentAccount.addingAccount = 0; parentAccount.addingAccount = 0;
@ -327,16 +361,16 @@ export default {
await this.fetchChildren(parentAccount, true); await this.fetchChildren(parentAccount, true);
// open quick edit // open quick edit
await openQuickEdit({ await openQuickEdit({ doc });
schemaName: 'Account', this.setOpenAccountDocListener(doc, null, parentAccount);
name: account.name,
});
// unfreeze input // unfreeze input
this.insertingAccount = false; this.insertingAccount = false;
this.newAccountName = '';
} catch (e) { } catch (e) {
// unfreeze input // unfreeze input
this.insertingAccount = false; this.insertingAccount = false;
await handleErrorWithDialog(e, account); await handleErrorWithDialog(e, doc);
} }
}, },
isQuickEditOpen(account) { isQuickEditOpen(account) {
@ -385,19 +419,23 @@ export default {
}, },
computed: { computed: {
allAccounts() { allAccounts() {
let allAccounts = []; const allAccounts = [];
getAccounts(this.accounts, 0);
return allAccounts; (function getAccounts(accounts, level, location) {
for (let i in accounts) {
const account = accounts[i];
function getAccounts(accounts, level) {
for (let account of accounts) {
account.level = level; account.level = level;
account.location = [...location, i];
allAccounts.push(account); allAccounts.push(account);
if (account.children != null && account.expanded) { if (account.children != null && account.expanded) {
getAccounts(account.children, level + 1); getAccounts(account.children, level + 1, account.location);
}
} }
} }
})(this.accounts, 0, []);
return allAccounts;
}, },
}, },
}; };

View File

@ -14,10 +14,10 @@
</p> </p>
<feather-icon v-else name="more-horizontal" class="w-4 h-4" /> <feather-icon v-else name="more-horizontal" class="w-4 h-4" />
</DropdownWithActions> </DropdownWithActions>
<Button v-if="doc.canSave" type="primary" @click="sync"> <Button v-if="doc?.canSave" type="primary" @click="sync">
{{ t`Save` }} {{ t`Save` }}
</Button> </Button>
<Button v-else-if="doc.canSubmit" type="primary" @click="submit">{{ <Button v-else-if="doc?.canSubmit" type="primary" @click="submit">{{
t`Submit` t`Submit`
}}</Button> }}</Button>
</template> </template>

View File

@ -38,10 +38,10 @@
</p> </p>
<feather-icon v-else name="more-horizontal" class="w-4 h-4" /> <feather-icon v-else name="more-horizontal" class="w-4 h-4" />
</DropdownWithActions> </DropdownWithActions>
<Button v-if="doc.canSave" type="primary" @click="sync"> <Button v-if="doc?.canSave" type="primary" @click="sync">
{{ t`Save` }} {{ t`Save` }}
</Button> </Button>
<Button v-else-if="doc.canSubmit" type="primary" @click="submit">{{ <Button v-else-if="doc?.canSubmit" type="primary" @click="submit">{{
t`Submit` t`Submit`
}}</Button> }}</Button>
</template> </template>

View File

@ -15,7 +15,7 @@
<feather-icon v-else name="more-horizontal" class="w-4 h-4" /> <feather-icon v-else name="more-horizontal" class="w-4 h-4" />
</DropdownWithActions> </DropdownWithActions>
<Button <Button
v-if="doc.canSave" v-if="doc?.canSave"
type="primary" type="primary"
class="text-white text-xs" class="text-white text-xs"
@click="sync" @click="sync"

View File

@ -26,7 +26,7 @@
:icon="true" :icon="true"
@click="sync" @click="sync"
type="primary" type="primary"
v-if="doc.canSave" v-if="doc?.canSave"
class="text-white text-xs" class="text-white text-xs"
> >
{{ t`Save` }} {{ t`Save` }}
@ -35,7 +35,7 @@
:icon="true" :icon="true"
@click="submit" @click="submit"
type="primary" type="primary"
v-else-if="doc.canSubmit" v-else-if="doc?.canSubmit"
class="text-white text-xs" class="text-white text-xs"
> >
{{ t`Submit` }} {{ t`Submit` }}

View File

@ -217,3 +217,11 @@ export function joinMapLists<A, B>(
return joint; return joint;
} }
export function removeAtIndex<T>(array: T[], index: number): T[] {
if (index < 0 || index >= array.length) {
return array;
}
return [...array.slice(0, index), ...array.slice(index + 1)];
}