batch removing specified tag or batch removing invalid tag in import transaction tool

This commit is contained in:
MaysWind
2025-09-13 22:35:18 +08:00
parent 77d2426c14
commit 422cf49517
14 changed files with 172 additions and 23 deletions
@@ -8,6 +8,7 @@
<h4 class="text-h4" v-if="mode === 'batchReplace' && type === 'transferCategory'">{{ tt('Batch Replace Selected Transfer Categories') }}</h4>
<h4 class="text-h4" v-if="mode === 'batchReplace' && type === 'account'">{{ tt('Batch Replace Selected Accounts') }}</h4>
<h4 class="text-h4" v-if="mode === 'batchReplace' && type === 'destinationAccount'">{{ tt('Batch Replace Selected Destination Accounts') }}</h4>
<h4 class="text-h4" v-if="mode === 'batchReplace' && type === 'tag'">{{ tt('Batch Replace Selected Transaction Tags') }}</h4>
<h4 class="text-h4" v-if="mode === 'replaceInvalidItems' && type === 'expenseCategory'">{{ tt('Replace Invalid Expense Categories') }}</h4>
<h4 class="text-h4" v-if="mode === 'replaceInvalidItems' && type === 'incomeCategory'">{{ tt('Replace Invalid Income Categories') }}</h4>
<h4 class="text-h4" v-if="mode === 'replaceInvalidItems' && type === 'transferCategory'">{{ tt('Replace Invalid Transfer Categories') }}</h4>
@@ -131,6 +132,19 @@
</v-card-text>
<v-card-text class="my-md-4 w-100 d-flex justify-center" v-if="type === 'tag'">
<v-row>
<v-col cols="12" v-if="mode === 'batchReplace'">
<v-autocomplete
item-title="name"
item-value="value"
persistent-placeholder
:disabled="loading"
:label="tt('Source Value')"
:placeholder="tt('Source Value')"
:items="allSourceTagItems"
:no-data-text="tt('No available tag')"
v-model="sourceItem">
</v-autocomplete>
</v-col>
<v-col cols="12" v-if="mode === 'replaceInvalidItems'">
<v-autocomplete
item-title="name"
@@ -150,7 +164,7 @@
item-value="id"
persistent-placeholder
chips
:disabled="loading"
:disabled="loading || removeTag"
:label="tt('Target Tag')"
:placeholder="tt('Target Tag')"
:items="allTags"
@@ -175,11 +189,15 @@
</template>
</v-autocomplete>
</v-col>
<v-col cols="12" class="pt-0">
<v-switch :disabled="loading"
:label="tt('Remove Tag')" v-model="removeTag"/>
</v-col>
</v-row>
</v-card-text>
<v-card-text class="overflow-y-visible">
<div class="w-100 d-flex justify-center gap-4">
<v-btn :disabled="loading || (mode === 'replaceInvalidItems' && !sourceItem && sourceItem !== '') || (!targetItem && targetItem !== '')" @click="confirm">{{ tt('OK') }}</v-btn>
<v-btn :disabled="loading || ((mode === 'replaceInvalidItems' || (mode === 'batchReplace' && type === 'tag')) && !sourceItem && sourceItem !== '') || (!targetItem && targetItem !== '' && !removeTag)" @click="confirm">{{ tt('OK') }}</v-btn>
<v-btn color="secondary" variant="tonal" :disabled="loading" @click="cancel">{{ tt('Cancel') }}</v-btn>
</div>
</v-card-text>
@@ -192,7 +210,7 @@
<script setup lang="ts">
import SnackBar from '@/components/desktop/SnackBar.vue';
import { ref, computed, useTemplateRef } from 'vue';
import { ref, computed, useTemplateRef, watch } from 'vue';
import { useI18n } from '@/locales/helpers.ts';
@@ -241,8 +259,10 @@ const loading = ref<boolean>(false);
const mode = ref<BatchReplaceDialogMode | ''>('');
const type = ref<BatchReplaceDialogDataType | ''>('');
const invalidItems = ref<NameValue[] | undefined>([]);
const allSourceTagItems = ref<NameValue[] | undefined>([]);
const sourceItem = ref<string | undefined>(undefined);
const targetItem = ref<string | undefined>(undefined);
const removeTag = ref<boolean>(false);
let resolveFunc: ((response: BatchReplaceDialogResponse) => void) | null = null;
let rejectFunc: ((reason?: unknown) => void) | null = null;
@@ -266,7 +286,7 @@ function getAccountDisplayName(accountId?: string): string {
}
}
function open(options: { mode: BatchReplaceDialogMode; type: BatchReplaceDialogDataType; invalidItems?: NameValue[] }): Promise<BatchReplaceDialogResponse> {
function open(options: { mode: BatchReplaceDialogMode; type: BatchReplaceDialogDataType; invalidItems?: NameValue[], allSourceTagItems?: NameValue[] }): Promise<BatchReplaceDialogResponse> {
mode.value = options.mode;
type.value = options.type;
sourceItem.value = undefined;
@@ -277,7 +297,14 @@ function open(options: { mode: BatchReplaceDialogMode; type: BatchReplaceDialogD
invalidItems.value = options.invalidItems;
}
if (type.value === 'tag' && mode.value === 'batchReplace') {
allSourceTagItems.value = options.allSourceTagItems;
} else {
allSourceTagItems.value = undefined;
}
targetItem.value = undefined;
removeTag.value = false;
showState.value = true;
return new Promise((resolve, reject) => {
@@ -327,14 +354,25 @@ function reload(): void {
}
function confirm(): void {
if (mode.value === 'batchReplace') {
let targetItemValue: string | undefined = targetItem.value;
if (type.value === 'tag' && removeTag.value) {
targetItemValue = undefined;
}
if (mode.value === 'batchReplace' && type.value !== 'tag') {
resolveFunc?.({
targetItem: targetItem.value
targetItem: targetItemValue
});
} else if (mode.value === 'batchReplace' && type.value === 'tag') {
resolveFunc?.({
sourceItem: sourceItem.value,
targetItem: targetItemValue
});
} else if (mode.value === 'replaceInvalidItems') {
resolveFunc?.({
sourceItem: sourceItem.value,
targetItem: targetItem.value
targetItem: targetItemValue
});
}
@@ -346,6 +384,12 @@ function cancel(): void {
showState.value = false;
}
watch(removeTag, (newValue) => {
if (newValue) {
targetItem.value = undefined;
}
});
defineExpose({
open
});
@@ -348,7 +348,7 @@ import { useAccountsStore } from '@/stores/account.ts';
import { useTransactionCategoriesStore } from '@/stores/transactionCategory.ts';
import { useTransactionTagsStore } from '@/stores/transactionTag.ts';
import { type NameValue, type NameNumeralValue, itemAndIndex, keys } from '@/core/base.ts';
import { type NameValue, type NameNumeralValue, itemAndIndex, reversed, keys } from '@/core/base.ts';
import { type NumeralSystem } from '@/core/numeral.ts';
import { CategoryType } from '@/core/category.ts';
import { TransactionType } from '@/core/transaction.ts';
@@ -665,6 +665,12 @@ const toolMenus = computed<ImportTransactionCheckDataMenu[]>(() => [
disabled: isEditing.value || selectedTransferTransactionCount.value < 1,
onClick: () => showBatchReplaceDialog('destinationAccount')
},
{
prependIcon: mdiFindReplace,
title: tt('Batch Replace Selected Transaction Tags'),
disabled: isEditing.value || selectedImportTransactionCount.value < 1,
onClick: () => showBatchReplaceDialog('tag', allOriginalTransactionTagNames.value)
},
{
prependIcon: mdiFindReplace,
title: tt('Replace Invalid Expense Categories'),
@@ -984,6 +990,7 @@ const allInvalidIncomeCategoryNames = computed<NameValue[]>(() => getCurrentInva
const allInvalidTransferCategoryNames = computed<NameValue[]>(() => getCurrentInvalidCategoryNames(TransactionType.Transfer));
const allInvalidAccountNames = computed<NameValue[]>(() => getCurrentInvalidAccountNames());
const allInvalidTransactionTagNames = computed<NameValue[]>(() => getCurrentInvalidTagNames());
const allOriginalTransactionTagNames = computed<NameValue[]>(() => getAllOriginalTagNames());
const displayFilterCustomDateRange = computed<string>(() => {
if (filters.value.minDatetime === null || filters.value.maxDatetime === null) {
@@ -1279,6 +1286,34 @@ function getCurrentInvalidTagNames(): NameValue[] {
return invalidTags;
}
function getAllOriginalTagNames(): NameValue[] {
const allOriginalTagNames: Record<string, boolean> = {};
const allOriginalTags: NameValue[] = [];
if (!props.importTransactions || props.importTransactions.length < 1) {
return allOriginalTags;
}
for (const importTransaction of props.importTransactions) {
if (!importTransaction.originalTagNames) {
continue;
}
for (const tagName of importTransaction.originalTagNames) {
allOriginalTagNames[tagName] = true;
}
}
for (const name of keys(allOriginalTagNames)) {
allOriginalTags.push({
name: name || tt('(Empty)'),
value: name
});
}
return allOriginalTags;
}
function importTransactionsFilter(value: string, query: string, item?: { value: unknown, raw: ImportTransaction }): boolean {
if (!item || !item.raw) {
return false;
@@ -1396,19 +1431,26 @@ function updateTransactionData(transaction: ImportTransaction): void {
}
}
function showBatchReplaceDialog(type: BatchReplaceDialogDataType): void {
function showBatchReplaceDialog(type: BatchReplaceDialogDataType, allSourceTagItems?: NameValue[]): void {
if (isEditing.value) {
return;
}
batchReplaceDialog.value?.open({
mode: 'batchReplace',
type: type
type: type,
allSourceTagItems: allSourceTagItems
}).then(result => {
if (!result || !result.targetItem) {
if (!result) {
return;
}
if (type !== 'tag') {
if (!result.targetItem) {
return;
}
}
let updatedCount = 0;
if (props.importTransactions) {
@@ -1421,27 +1463,48 @@ function showBatchReplaceDialog(type: BatchReplaceDialogDataType): void {
if (type === 'expenseCategory') {
if (importTransaction.type === TransactionType.Expense) {
importTransaction.categoryId = result.targetItem;
importTransaction.categoryId = result.targetItem as string;
updated = true;
}
} else if (type === 'incomeCategory') {
if (importTransaction.type === TransactionType.Income) {
importTransaction.categoryId = result.targetItem;
importTransaction.categoryId = result.targetItem as string;
updated = true;
}
} else if (type === 'transferCategory') {
if (importTransaction.type === TransactionType.Transfer) {
importTransaction.categoryId = result.targetItem;
importTransaction.categoryId = result.targetItem as string;
updated = true;
}
} else if (type === 'account') {
importTransaction.sourceAccountId = result.targetItem;
importTransaction.sourceAccountId = result.targetItem as string;
updated = true;
} else if (type === 'destinationAccount') {
if (importTransaction.type === TransactionType.Transfer) {
importTransaction.destinationAccountId = result.targetItem;
importTransaction.destinationAccountId = result.targetItem as string;
updated = true;
}
} else if (type === 'tag') {
let removeIndex: number[] = [];
for (let tagIndex = 0; tagIndex < importTransaction.originalTagNames.length; tagIndex++) {
const originalTagName = importTransaction.originalTagNames ? (importTransaction.originalTagNames[tagIndex] ?? '') : '';
if (originalTagName === result.sourceItem) {
if (result.targetItem) {
importTransaction.tagIds[tagIndex] = result.targetItem;
importTransaction.originalTagNames[tagIndex] = allTagsMap.value[result.targetItem]?.name || '';
} else {
removeIndex.push(tagIndex);
}
updated = true;
}
}
for (const tagIndex of reversed(removeIndex)) {
importTransaction.tagIds.splice(tagIndex, 1);
importTransaction.originalTagNames.splice(tagIndex, 1);
}
}
if (updated) {
@@ -1469,10 +1532,16 @@ function showReplaceInvalidItemDialog(type: BatchReplaceDialogDataType, invalidI
type: type,
invalidItems: invalidItems
}).then(result => {
if (!result || (!result.sourceItem && result.sourceItem !== '') || !result.targetItem) {
if (!result || (!result.sourceItem && result.sourceItem !== '')) {
return;
}
if (type !== 'tag') {
if (!result.targetItem) {
return;
}
}
let updatedCount = 0;
if (props.importTransactions) {
@@ -1489,13 +1558,13 @@ function showReplaceInvalidItemDialog(type: BatchReplaceDialogDataType, invalidI
if (importTransaction.type !== TransactionType.ModifyBalance && originalCategoryName === result.sourceItem && (!categoryId || categoryId === '0' || !allCategoriesMap.value[categoryId])) {
if (type === 'expenseCategory' && importTransaction.type === TransactionType.Expense) {
importTransaction.categoryId = result.targetItem;
importTransaction.categoryId = result.targetItem as string;
updated = true;
} else if (type === 'incomeCategory' && importTransaction.type === TransactionType.Income) {
importTransaction.categoryId = result.targetItem;
importTransaction.categoryId = result.targetItem as string;
updated = true;
} else if (type === 'transferCategory' && importTransaction.type === TransactionType.Transfer) {
importTransaction.categoryId = result.targetItem;
importTransaction.categoryId = result.targetItem as string;
updated = true;
}
}
@@ -1506,24 +1575,36 @@ function showReplaceInvalidItemDialog(type: BatchReplaceDialogDataType, invalidI
const originalDestinationAccountName = importTransaction.originalDestinationAccountName;
if (originalSourceAccountName === result.sourceItem && (!sourceAccountId || sourceAccountId === '0' || !allAccountsMap.value[sourceAccountId])) {
importTransaction.sourceAccountId = result.targetItem;
importTransaction.sourceAccountId = result.targetItem as string;
updated = true;
}
if (importTransaction.type === TransactionType.Transfer && originalDestinationAccountName === result.sourceItem && (!destinationAccountId || destinationAccountId === '0' || !allAccountsMap.value[destinationAccountId])) {
importTransaction.destinationAccountId = result.targetItem;
importTransaction.destinationAccountId = result.targetItem as string;
updated = true;
}
} else if (type === 'tag' && importTransaction.tagIds) {
let removeIndex: number[] = [];
for (let tagIndex = 0; tagIndex < importTransaction.tagIds.length; tagIndex++) {
const tagId = importTransaction.tagIds[tagIndex] as string;
const originalTagName = importTransaction.originalTagNames ? (importTransaction.originalTagNames[tagIndex] ?? '') : '';
if (originalTagName === result.sourceItem && (!tagId || tagId === '0' || !allTagsMap.value[tagId])) {
importTransaction.tagIds[tagIndex] = result.targetItem;
if (result.targetItem) {
importTransaction.tagIds[tagIndex] = result.targetItem;
importTransaction.originalTagNames[tagIndex] = allTagsMap.value[result.targetItem]?.name || '';
} else {
removeIndex.push(tagIndex);
}
updated = true;
}
}
for (const tagIndex of reversed(removeIndex)) {
importTransaction.tagIds.splice(tagIndex, 1);
importTransaction.originalTagNames.splice(tagIndex, 1);
}
}
if (updated) {