Files
firefly-iii/resources/assets/v2/pages/transactions/shared/parse-from-entries.js

81 lines
2.9 KiB
JavaScript
Raw Normal View History

/*
* parse-from-entries.js
* Copyright (c) 2023 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/>.
*/
/**
*
* @param entries
*/
export function parseFromEntries(entries, transactionType) {
let returnArray = [];
for (let i in entries) {
if (entries.hasOwnProperty(i)) {
const entry = entries[i];
let current = {};
// fields for transaction
current.description = entry.description;
2024-01-02 15:41:14 +01:00
// source and destination
current.source_name = entry.source_account.name;
current.destination_name = entry.destination_account.name;
2024-01-02 15:41:14 +01:00
// amount information:
current.amount = entry.amount;
2023-10-28 17:17:09 +02:00
current.currency_code = entry.currency_code;
2024-01-02 15:41:14 +01:00
// dates
current.date = entry.date;
// meta
current.budget_id = entry.budget_id;
current.category_name = entry.category_name;
current.piggy_bank_id = entry.piggy_bank_id;
// if foreign amount currency code is set:
if (typeof entry.foreign_currency_code !== 'undefined' && '' !== entry.foreign_currency_code.toString()) {
current.foreign_currency_code = entry.foreign_currency_code;
if(typeof entry.foreign_amount !== 'undefined' && '' !== entry.foreign_amount.toString()) {
current.foreign_amount = entry.foreign_amount;
}
if(typeof entry.foreign_amount === 'undefined' || '' === entry.foreign_amount.toString()) {
delete current.foreign_amount;
delete current.foreign_currency_code;
}
}
// if ID is set:
if (typeof entry.source_account.id !== 'undefined' && '' !== entry.source_account.id.toString()) {
current.source_id = entry.source_account.id;
}
if (typeof entry.destination_account.id !== 'undefined' && '' !== entry.destination_account.id.toString()) {
current.destination_id = entry.destination_account.id;
}
// TODO transaction type is hard coded:
current.type = transactionType;
returnArray.push(current);
}
}
return returnArray;
}