Skip to content

Firefly III: a personal finances manager

4.6 KB
Raw Blame
/*
 * parse-downloaded-splits.js
 * Copyright (c) 2024 [email protected]
 *
 * 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/>.
 */

import { createEmptySplit } from "./create-empty-split.js";
import formatDate from "../../../util/format-date.js";

export function parseDownloadedSplits(downloads, groupId) {
    let returnArray = [];
    for (let i in downloads) {
        if (Object.hasOwn(downloads, i)) {
            // we have at least all default values!
            let download = downloads[i];
            let current = createEmptySplit();

            // meta data
            current.transaction_journal_id = parseInt(download.transaction_journal_id);
            current.transaction_group_id = groupId;
            current.reconciled = download.reconciled;
            current.ogReconciled = download.reconciled;
            current.bill_id = null === download.bill_id ? 0 : parseInt(download.bill_id);
            current.bill_name = download.bill_name;
            current.budget_id = download.budget_id;
            current.budget_name = download.budget_name;
            current.category_name = download.category_name;
            current.category_id = download.category_id;
            current.piggy_bank_id = download.piggy_bank_id;
            current.piggy_bank_name = download.piggy_bank_name;

            // meta dates
            current.book_date =
                null === download.book_date ? "" : formatDate(new Date(download.book_date), "yyyy-MM-dd");
            current.due_date = null === download.due_date ? "" : formatDate(new Date(download.due_date), "yyyy-MM-dd");
            current.interest_date =
                null === download.interest_date ? "" : formatDate(new Date(download.interest_date), "yyyy-MM-dd");
            current.invoice_date =
                null === download.invoice_date ? "" : formatDate(new Date(download.invoice_date), "yyyy-MM-dd");
            current.payment_date =
                null === download.payment_date ? "" : formatDate(new Date(download.payment_date), "yyyy-MM-dd");
            current.process_date =
                null === download.process_date ? "" : formatDate(new Date(download.process_date), "yyyy-MM-dd");

            // more meta
            current.external_url = download.external_url;
            current.internal_reference = download.internal_reference;
            current.notes = download.notes;
            current.tags = download.tags;

            // amount
            current.amount = parseFloat(download.amount).toFixed(download.currency_decimal_places);
            current.currency_code = download.currency_code;
            if (null !== download.foreign_amount) {
                current.foreign_currency_code = download.foreign_currency_code;
                current.foreign_amount = parseFloat(download.foreign_amount).toFixed(
                    download.foreign_currency_decimal_places,
                );
            }

            // date and description
            current.date = formatDate(new Date(download.date), "yyyy-MM-dd HH:mm");
            current.description = download.description;

            // source and destination
            current.destination_account = {
                id: download.destination_id,
                name: download.destination_name,
                type: download.destination_type,
                alpine_name: download.destination_name,
            };

            current.source_account = {
                id: download.source_id,
                name: download.source_name,
                type: download.source_type,
                alpine_name: download.source_name,
            };

            if (null !== download.latitude) {
                current.hasLocation = true;
                current.latitude = download.latitude;
                current.longitude = download.longitude;
                current.zoom_level = download.zoom_level;
            }
            returnArray.push(current);
        }
    }
    // reverse return array:
    returnArray.reverse();

    return returnArray;
}