Files
firefly-iii/app/Export/Entry/Entry.php

257 lines
7.1 KiB
PHP
Raw Normal View History

2016-02-04 17:16:16 +01:00
<?php
declare(strict_types=1);
2016-02-04 17:16:16 +01:00
/**
* Entry.php
2017-10-21 08:40:00 +02:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
2016-02-04 17:16:16 +01:00
*
2017-10-21 08:40:00 +02:00
* This file is part of Firefly III.
*
2017-10-21 08:40:00 +02:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 14:41:58 +01:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2016-02-04 17:16:16 +01:00
*/
2016-04-26 12:39:29 +02:00
namespace FireflyIII\Export\Entry;
2016-02-04 17:16:16 +01:00
2017-08-18 14:45:42 +02:00
use FireflyIII\Models\Transaction;
2016-02-04 17:16:16 +01:00
/**
* To extend the exported object, in case of new features in Firefly III for example,
2017-11-15 12:25:49 +01:00
* do the following:.
2016-02-04 17:16:16 +01:00
*
2016-04-26 20:49:22 +02:00
* - Add the field(s) to this class. If you add more than one related field, add a new object.
2016-02-04 17:16:16 +01:00
* - Make sure the "fromJournal"-routine fills these fields.
* - Add them to the static function that returns its type (key=value. Remember that the only
* valid types can be found in config/csv.php (under "roles").
*
* These new entries should be should be strings and numbers as much as possible.
*
*
*
* Class Entry
2017-11-15 12:25:49 +01:00
*
2016-12-27 19:59:56 +01:00
* @SuppressWarnings(PHPMD.LongVariable)
2017-09-16 07:41:03 +02:00
* @SuppressWarnings(PHPMD.TooManyFields)
2016-02-04 17:16:16 +01:00
*/
2016-05-20 08:00:35 +02:00
final class Entry
2016-02-04 17:16:16 +01:00
{
2016-10-23 14:58:39 +02:00
// @formatter:off
2017-12-17 14:30:53 +01:00
/**
2017-12-26 08:32:00 +01:00
* @var int
2017-12-17 14:30:53 +01:00
*/
2017-11-18 15:50:21 +01:00
public $journal_id;
2017-12-17 14:30:53 +01:00
/**
* @var int
*/
2017-11-18 15:50:21 +01:00
public $transaction_id = 0;
2017-12-17 14:30:53 +01:00
/**
2017-12-26 08:32:00 +01:00
* @var string
2017-12-17 14:30:53 +01:00
*/
2017-11-18 15:50:21 +01:00
public $date;
2017-12-17 14:30:53 +01:00
/**
2017-12-26 08:32:00 +01:00
* @var string
2017-12-17 14:30:53 +01:00
*/
2017-11-18 15:50:21 +01:00
public $description;
2017-12-17 14:30:53 +01:00
/**
2017-12-26 08:32:00 +01:00
* @var string
2017-12-17 14:30:53 +01:00
*/
2017-11-18 15:50:21 +01:00
public $currency_code;
2017-12-17 14:30:53 +01:00
/**
2017-12-26 08:32:00 +01:00
* @var string
2017-12-17 14:30:53 +01:00
*/
2016-10-23 09:44:14 +02:00
public $amount;
2017-12-17 14:30:53 +01:00
/**
* @var string
*/
2017-11-18 15:50:21 +01:00
public $foreign_currency_code = '';
2017-12-17 14:30:53 +01:00
/**
* @var string
*/
public $foreign_amount = '0';
2017-11-18 15:50:21 +01:00
2017-12-17 14:30:53 +01:00
/**
2017-12-26 08:32:00 +01:00
* @var string
2017-12-17 14:30:53 +01:00
*/
2017-11-18 15:50:21 +01:00
public $transaction_type;
2017-12-17 14:30:53 +01:00
/**
2017-12-26 08:32:00 +01:00
* @var string
2017-12-17 14:30:53 +01:00
*/
2017-08-12 06:44:00 +02:00
public $asset_account_id;
2017-12-17 14:30:53 +01:00
/**
2017-12-26 08:32:00 +01:00
* @var string
2017-12-17 14:30:53 +01:00
*/
2017-08-12 06:44:00 +02:00
public $asset_account_name;
2017-12-17 14:30:53 +01:00
/**
2017-12-26 08:32:00 +01:00
* @var string
2017-12-17 14:30:53 +01:00
*/
2017-11-18 15:50:21 +01:00
public $asset_account_iban;
2017-12-17 14:30:53 +01:00
/**
2017-12-26 08:32:00 +01:00
* @var string
2017-12-17 14:30:53 +01:00
*/
2017-11-18 15:50:21 +01:00
public $asset_account_bic;
2017-12-17 14:30:53 +01:00
/**
2017-12-26 08:32:00 +01:00
* @var string
2017-12-17 14:30:53 +01:00
*/
2017-08-18 14:45:42 +02:00
public $asset_account_number;
2017-12-17 14:30:53 +01:00
/**
2017-12-26 08:32:00 +01:00
* @var string
2017-12-17 14:30:53 +01:00
*/
2017-08-18 14:45:42 +02:00
public $asset_currency_code;
2017-11-18 15:50:21 +01:00
2017-12-17 14:30:53 +01:00
/**
2017-12-26 08:32:00 +01:00
* @var string
2017-12-17 14:30:53 +01:00
*/
2017-11-18 15:50:21 +01:00
public $opposing_account_id;
2017-12-17 14:30:53 +01:00
/**
2017-12-26 08:32:00 +01:00
* @var string
2017-12-17 14:30:53 +01:00
*/
2017-11-18 15:50:21 +01:00
public $opposing_account_name;
2017-12-17 14:30:53 +01:00
/**
2017-12-26 08:32:00 +01:00
* @var string
2017-12-17 14:30:53 +01:00
*/
2017-11-18 15:50:21 +01:00
public $opposing_account_iban;
2017-12-17 14:30:53 +01:00
/**
2017-12-26 08:32:00 +01:00
* @var string
2017-12-17 14:30:53 +01:00
*/
2017-11-18 15:50:21 +01:00
public $opposing_account_bic;
2017-12-17 14:30:53 +01:00
/**
2017-12-26 08:32:00 +01:00
* @var string
2017-12-17 14:30:53 +01:00
*/
2017-11-18 15:50:21 +01:00
public $opposing_account_number;
2017-12-17 14:30:53 +01:00
/**
2017-12-26 08:32:00 +01:00
* @var string
2017-12-17 14:30:53 +01:00
*/
2017-11-18 15:50:21 +01:00
public $opposing_currency_code;
2017-12-17 14:30:53 +01:00
/**
2017-12-26 08:32:00 +01:00
* @var string
2017-12-17 14:30:53 +01:00
*/
2016-10-23 09:44:14 +02:00
public $budget_id;
2017-12-17 14:30:53 +01:00
/**
2017-12-26 08:32:00 +01:00
* @var string
2017-12-17 14:30:53 +01:00
*/
2016-10-23 09:44:14 +02:00
public $budget_name;
2017-11-18 15:50:21 +01:00
2017-12-17 14:30:53 +01:00
/**
2017-12-26 08:32:00 +01:00
* @var string
2017-12-17 14:30:53 +01:00
*/
2016-10-23 09:44:14 +02:00
public $category_id;
2017-12-17 14:30:53 +01:00
/**
2017-12-26 08:32:00 +01:00
* @var string
2017-12-17 14:30:53 +01:00
*/
2016-10-23 09:44:14 +02:00
public $category_name;
2017-11-18 15:50:21 +01:00
2017-12-17 14:30:53 +01:00
/**
2017-12-26 08:32:00 +01:00
* @var string
2017-12-17 14:30:53 +01:00
*/
2017-11-18 15:50:21 +01:00
public $bill_id;
2017-12-17 14:30:53 +01:00
/**
2017-12-26 08:32:00 +01:00
* @var string
2017-12-17 14:30:53 +01:00
*/
2017-11-18 15:50:21 +01:00
public $bill_name;
2017-12-17 14:30:53 +01:00
/**
2017-12-26 08:32:00 +01:00
* @var string
2017-12-17 14:30:53 +01:00
*/
2017-08-18 14:45:42 +02:00
public $notes;
2017-11-18 15:50:21 +01:00
2017-12-17 14:30:53 +01:00
/**
2017-12-26 08:32:00 +01:00
* @var string
2017-12-17 14:30:53 +01:00
*/
2017-08-18 14:45:42 +02:00
public $tags;
2017-11-18 15:50:21 +01:00
2016-10-23 14:58:39 +02:00
// @formatter:on
/**
* Entry constructor.
*/
private function __construct()
{
}
2016-02-04 17:16:16 +01:00
2017-08-18 14:45:42 +02:00
/**
* Converts a given transaction (as collected by the collector) into an export entry.
*
2017-09-16 07:41:03 +02:00
* @SuppressWarnings(PHPMD.CyclomaticComplexity) // complex but little choice.
* @SuppressWarnings(PHPMD.ExcessiveMethodLength) // cannot be helped
*
2017-08-18 14:45:42 +02:00
* @param Transaction $transaction
*
* @return Entry
*/
2017-11-25 08:54:52 +01:00
public static function fromTransaction(Transaction $transaction): Entry
2017-08-18 14:45:42 +02:00
{
2017-11-18 15:50:21 +01:00
$entry = new self();
2017-08-18 14:45:42 +02:00
$entry->journal_id = $transaction->journal_id;
$entry->transaction_id = $transaction->id;
$entry->date = $transaction->date->format('Ymd');
$entry->description = $transaction->description;
2018-04-02 14:42:07 +02:00
if (strlen((string)$transaction->transaction_description) > 0) {
2017-08-18 14:45:42 +02:00
$entry->description = $transaction->transaction_description . '(' . $transaction->description . ')';
}
$entry->currency_code = $transaction->transactionCurrency->code;
2018-04-02 14:42:07 +02:00
$entry->amount = (string)round($transaction->transaction_amount, $transaction->transactionCurrency->decimal_places);
2017-08-18 14:45:42 +02:00
2017-11-15 12:25:49 +01:00
$entry->foreign_currency_code = null === $transaction->foreign_currency_id ? null : $transaction->foreignCurrency->code;
$entry->foreign_amount = null === $transaction->foreign_currency_id
2017-08-18 14:45:42 +02:00
? null
2018-04-02 14:42:07 +02:00
: (string)round(
$transaction->transaction_foreign_amount,
$transaction->foreignCurrency->decimal_places
2017-09-09 06:41:45 +02:00
);
2017-08-18 14:45:42 +02:00
$entry->transaction_type = $transaction->transaction_type_type;
2018-04-02 14:42:07 +02:00
$entry->asset_account_id = (string)$transaction->account_id;
2017-08-18 14:45:42 +02:00
$entry->asset_account_name = app('steam')->tryDecrypt($transaction->account_name);
$entry->asset_account_iban = $transaction->account_iban;
$entry->asset_account_number = $transaction->account_number;
$entry->asset_account_bic = $transaction->account_bic;
2017-08-18 15:14:44 +02:00
$entry->asset_currency_code = $transaction->account_currency_code;
2018-04-02 14:42:07 +02:00
$entry->opposing_account_id = (string)$transaction->opposing_account_id;
2017-08-18 14:45:42 +02:00
$entry->opposing_account_name = app('steam')->tryDecrypt($transaction->opposing_account_name);
$entry->opposing_account_iban = $transaction->opposing_account_iban;
$entry->opposing_account_number = $transaction->opposing_account_number;
$entry->opposing_account_bic = $transaction->opposing_account_bic;
2017-08-18 15:14:44 +02:00
$entry->opposing_currency_code = $transaction->opposing_currency_code;
2017-08-18 14:45:42 +02:00
2017-11-15 12:25:49 +01:00
// budget
2018-04-02 14:42:07 +02:00
$entry->budget_id = (string)$transaction->transaction_budget_id;
2017-08-18 14:45:42 +02:00
$entry->budget_name = app('steam')->tryDecrypt($transaction->transaction_budget_name);
2017-11-15 12:25:49 +01:00
if (null === $transaction->transaction_budget_id) {
2017-08-18 14:45:42 +02:00
$entry->budget_id = $transaction->transaction_journal_budget_id;
$entry->budget_name = app('steam')->tryDecrypt($transaction->transaction_journal_budget_name);
}
2017-11-15 12:25:49 +01:00
// category
2018-04-02 14:42:07 +02:00
$entry->category_id = (string)$transaction->transaction_category_id;
2017-08-18 14:45:42 +02:00
$entry->category_name = app('steam')->tryDecrypt($transaction->transaction_category_name);
2017-11-15 12:25:49 +01:00
if (null === $transaction->transaction_category_id) {
2017-08-18 14:45:42 +02:00
$entry->category_id = $transaction->transaction_journal_category_id;
$entry->category_name = app('steam')->tryDecrypt($transaction->transaction_journal_category_name);
}
2017-11-15 12:25:49 +01:00
// budget
2018-04-02 14:42:07 +02:00
$entry->bill_id = (string)$transaction->bill_id;
2017-08-18 14:45:42 +02:00
$entry->bill_name = app('steam')->tryDecrypt($transaction->bill_name);
$entry->tags = $transaction->tags;
$entry->notes = $transaction->notes;
return $entry;
}
2016-02-10 16:01:18 +01:00
}