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

130 lines
4.1 KiB
PHP
Raw Normal View History

2016-02-04 17:16:16 +01:00
<?php
2016-02-05 12:08:25 +01:00
declare(strict_types = 1);
2016-02-04 17:16:16 +01:00
/**
* Entry.php
2016-04-01 16:44:46 +02:00
* Copyright (C) 2016 thegrumpydictator@gmail.com
2016-02-04 17:16:16 +01:00
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
2016-04-26 12:39:29 +02:00
namespace FireflyIII\Export\Entry;
2016-02-04 17:16:16 +01:00
use FireflyIII\Models\TransactionJournal;
use Illuminate\Support\Collection;
2016-02-04 17:16:16 +01:00
/**
* To extend the exported object, in case of new features in Firefly III for example,
* do the following:
*
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
*
2016-04-26 12:39:29 +02:00
* @package FireflyIII\Export\Entry
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
{
/** @var string */
public $amount;
2016-04-26 20:49:22 +02:00
/** @var EntryBill */
public $bill;
/** @var EntryBudget */
public $budget;
/** @var EntryCategory */
public $category;
2016-02-04 17:16:16 +01:00
/** @var string */
public $date;
/** @var string */
public $description;
2016-04-26 20:49:22 +02:00
/** @var EntryAccount */
public $destinationAccount;
/** @var Collection */
public $destinationAccounts;
2016-04-26 20:49:22 +02:00
/** @var EntryAccount */
public $sourceAccount;
/** @var Collection */
public $sourceAccounts;
/**
* Entry constructor.
*/
private function __construct()
{
$this->sourceAccounts = new Collection;
$this->destinationAccounts = new Collection;
}
2016-02-04 17:16:16 +01:00
/**
* @param TransactionJournal $journal
*
* @return Entry
*/
public static function fromJournal(TransactionJournal $journal)
{
2016-04-26 20:49:22 +02:00
$entry = new self;
$entry->description = $journal->description;
$entry->date = $journal->date->format('Y-m-d');
$entry->amount = TransactionJournal::amount($journal);
2016-02-05 21:10:55 +01:00
2016-04-26 20:49:22 +02:00
$entry->budget = new EntryBudget($journal->budgets->first());
$entry->category = new EntryCategory($journal->categories->first());
$entry->bill = new EntryBill($journal->bill);
2016-02-05 21:10:55 +01:00
2016-05-15 15:24:23 +02:00
$sources = TransactionJournal::sourceAccountList($journal);
$destinations = TransactionJournal::destinationAccountList($journal);
$entry->sourceAccount = new EntryAccount($sources->first());
2016-05-15 15:24:23 +02:00
$entry->destinationAccount = new EntryAccount($destinations->first());
2016-02-05 21:10:55 +01:00
foreach ($sources as $source) {
$entry->sourceAccounts->push(new EntryAccount($source));
}
foreach ($destinations as $destination) {
$entry->destinationAccounts->push(new EntryAccount($destination));
}
2016-02-04 17:16:16 +01:00
return $entry;
}
/**
* @return array
*/
2016-04-26 20:49:22 +02:00
public static function getFieldsAndTypes(): array
2016-02-04 17:16:16 +01:00
{
// key = field name (see top of class)
// value = field type (see csv.php under 'roles')
return [
2016-04-26 20:49:22 +02:00
'description' => 'description',
'amount' => 'amount',
'date' => 'date-transaction',
'source_account_id' => 'account-id',
'source_account_name' => 'account-name',
'source_account_iban' => 'account-iban',
'source_account_type' => '_ignore',
'source_account_number' => 'account-number',
'destination_account_id' => 'opposing-id',
'destination_account_name' => 'opposing-name',
'destination_account_iban' => 'opposing-iban',
'destination_account_type' => '_ignore',
'destination_account_number' => 'account-number',
'budget_id' => 'budget-id',
'budget_name' => 'budget-name',
'category_id' => 'category-id',
'category_name' => 'category-name',
'bill_id' => 'bill-id',
'bill_name' => 'bill-name',
2016-02-04 17:16:16 +01:00
];
}
2016-02-10 16:01:18 +01:00
}