Better filters for AC, fix #12957

This commit is contained in:
James Cole committed 2026-10-03 07:44:21 +02:00
1 parent 90d85c2640
commit d02521136c
5 files changed
+70 -49

No files matched your search

@@ -21,9 +21,8 @@
import Autocomplete from "bootstrap5-autocomplete";
function createAutocomplete(fieldIdentifier, url) {
let token = document.querySelector('meta[name="csrf-token"]').getAttribute("content");
Autocomplete.init("#" + fieldIdentifier, {
server: url + "?_token=" + token,
server: url,
labelField: "name",
valueField: "name",
liveServer: true,
@@ -33,7 +32,7 @@ function createAutocomplete(fieldIdentifier, url) {
headers: {
"Content-Type": "application/json",
Accept: "application/json",
"X-CSRF-TOKEN": token,
"X-CSRF-TOKEN": document.querySelector('meta[name="csrf-token"]').getAttribute("content"),
},
},
});
@@ -0,0 +1,43 @@
/*
* get-expected-account-types.js
* Copyright (c) 2026 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
export default function getExpectedAccountTypes(transactionType, sourceType, destinationType) {
transactionType = transactionType.toLowerCase();
console.log('getExpectedAccountTypes: transactionType=' + transactionType + ', sourceType=' + sourceType + ', destinationType=' + destinationType);
// is withdrawal and source is asset? limit destination types
if ("withdrawal" === transactionType && "Asset account" === sourceType) {
return ["Expense account", "Asset account", "Loan", "Debt", "Mortgage"];
}
// is withdrawal and source is liability? Limit destination types
if ("withdrawal" === transactionType && ["Loan", "Debt", "Mortgage"].includes(sourceType)) {
return ["Expense account"];
}
// is deposit? limit destination types.
if ("deposit" === transactionType) {
return ["Asset account", "Loan", "Debt", "Mortgage"];
}
// is transfer or withdrawal? No limit to source types.
// if ("deposit" !== transactionType) {
// return ["Expense account", "Loan", "Debt", "Mortgage", "Asset account"];
// }
return ["Expense account", "Loan", "Debt", "Mortgage", "Asset account"];
}
@@ -29,6 +29,7 @@ import {
selectSourceAccount,
} from "./autocomplete-functions.js";
import Tags from "bootstrap5-tags";
import getExpectedAccountTypes from "../../../form/get-expected-account-types.js";
export function getUrls() {
return {
@@ -51,19 +52,13 @@ export function addAllAutocompleteToForm() {
// depending on the type of the transaction,
// the filters are changed. For edit form, this means
// the available account types may be limited.
if ("edit" === this.formBehaviour.formType) {
if ("withdrawal" === this.groupProperties.transactionType) {
// filters.destination = ['Expense account'];
}
if ("deposit" === this.groupProperties.transactionType) {
// filters.source = ['Revenue account'];
}
if ("transfer" === this.groupProperties.transactionType) {
filters.source = [this.entries[0].source_account.type];
filters.destination = [this.entries[0].source_account.type];
}
}
// console.log("Filters for autocomplete: ", filters);
let transactionType = this.groupProperties.transactionType;
let sourceType = this.entries[0].source_account.type ?? 'unknown';
let destinationType = this.entries[0].destination_account.type ?? 'unknown';
filters.destination = getExpectedAccountTypes(transactionType, sourceType, destinationType);
console.log("Filters for autocomplete: ", filters);
const urls = getUrls();
setTimeout(() => {
@@ -148,6 +143,7 @@ export function addAllAutocompleteToForm() {
}
export function addAutocomplete(options) {
console.log('addAutocomplete("'+options.serverUrl+'")');
const params = {
server: options.serverUrl,
serverParams: {},
@@ -169,6 +165,7 @@ export function addAutocomplete(options) {
liveServer: true,
};
if (typeof options.account_types !== "undefined" && options.account_types.length > 0) {
console.log('Add account types');
params.serverParams["types"] = options.account_types;
}
if (typeof options.onRenderItem !== "undefined" && null !== options.onRenderItem) {
@@ -37,7 +37,7 @@ export function createLinkAutocomplete(fieldIdentifier, url) {
"</small>"
);
};
console.log("Created link AC", fieldIdentifier);
// console.log("Created link AC", fieldIdentifier);
Autocomplete.init("#" + fieldIdentifier, {
server: url,
labelField: "name",
@@ -20,6 +20,8 @@
// what happens when a user adds more than one split?
import Autocomplete from "bootstrap5-autocomplete";
import getExpectedAccountTypes from "../../../form/get-expected-account-types.js";
export function disableSplitInputs() {
let isTransferOrWithdrawal =
@@ -29,38 +31,18 @@ export function disableSplitInputs() {
//console.log('Activate disableSplitInputs');
// disable source and/or destination, based on account type.
for (let i = 0; i < this.entries.length; i++) {
// is withdrawal? limit destination types to expense, loan debt mortgage
if ("withdrawal" === this.groupProperties.transactionType) {
const el = document.getElementById("dest_" + i);
const inst = Autocomplete.getInstance(el);
if (null !== inst) {
let params = inst.getConfig("serverParams");
params.types = ["Expense account", "Loan", "Debt", "Mortgage"];
inst.setConfig("serverParams", params);
}
}
// is deposit? limit destination types.
if ("deposit" === this.groupProperties.transactionType) {
const el = document.getElementById("dest_" + i);
const inst = Autocomplete.getInstance(el);
if (null !== inst) {
let params = inst.getConfig("serverParams");
params.types = ["Asset account", "Loan", "Debt", "Mortgage"];
inst.setConfig("serverParams", params);
}
}
// is transfer? No limit to source types.
if (
"deposit" !== this.groupProperties.transactionType &&
"withdrawal" === this.groupProperties.transactionType
) {
const el = document.getElementById("dest_" + i);
const inst = Autocomplete.getInstance(el);
if (null !== inst) {
let params = inst.getConfig("serverParams");
params.types = [];
inst.setConfig("serverParams", params);
}
let transactionType = this.groupProperties.transactionType;
let sourceType = this.entries[i].source_account.type ?? "unknown";
let destinationType = this.entries[i].destination_account.type ?? "unknown";
let newTypes = getExpectedAccountTypes(transactionType, sourceType, destinationType);
const el = document.getElementById("dest_" + i);
const inst = Autocomplete.getInstance(el);
if (null !== inst) {
let params = inst.getConfig("serverParams");
params.types = newTypes;
inst.setConfig("serverParams", params);
}
if (i > 0) {