Files
firefly-iii/app/Transformers/TransactionTransformer.php

214 lines
10 KiB
PHP
Raw Normal View History

<?php
/**
* TransactionTransformer.php
* Copyright (c) 2018 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* 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
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Transformers;
2018-02-13 21:04:15 +01:00
use FireflyIII\Exceptions\FireflyException;
2018-02-11 07:46:34 +01:00
use FireflyIII\Models\Transaction;
2018-02-13 21:04:15 +01:00
use FireflyIII\Models\TransactionType;
2018-12-03 15:57:15 +01:00
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
2018-05-29 07:25:04 +02:00
use Log;
/**
* Class TransactionTransformer
*/
class TransactionTransformer extends AbstractTransformer
{
2018-12-03 15:57:15 +01:00
/** @var JournalRepositoryInterface */
protected $repository;
/**
2018-02-17 10:47:06 +01:00
* TransactionTransformer constructor.
*
* @codeCoverageIgnore
*/
public function __construct()
{
2018-12-09 20:54:11 +01:00
$this->repository = app(JournalRepositoryInterface::class);
if ('testing' === config('app.env')) {
Log::warning(sprintf('%s should not be instantiated in the TEST environment!', \get_class($this)));
}
}
/**
2018-02-17 10:47:06 +01:00
* Transform the journal.
*
2018-02-11 07:46:34 +01:00
* @param Transaction $transaction
*
* @return array
2018-02-13 21:04:15 +01:00
* @throws FireflyException
*/
2018-02-11 07:46:34 +01:00
public function transform(Transaction $transaction): array
{
2018-12-20 22:03:34 +01:00
$journal = $transaction->transactionJournal;
$category = $this->getCategory($transaction);
$budget = $this->getBudget($transaction);
$this->repository->setUser($journal->user);
2018-12-03 15:57:15 +01:00
2018-12-20 22:03:34 +01:00
$notes = $this->repository->getNoteText($journal);
$tags = implode(',', $this->repository->getTags($journal));
2018-02-11 07:46:34 +01:00
$data = [
2018-12-18 19:57:23 +01:00
'id' => (int)$transaction->id,
'created_at' => $transaction->created_at->toAtomString(),
'updated_at' => $transaction->updated_at->toAtomString(),
'description' => $transaction->description,
2018-12-20 22:03:34 +01:00
'journal_description' => $transaction->description,
2018-12-18 19:57:23 +01:00
'transaction_description' => $transaction->transaction_description,
2019-02-08 07:14:45 +01:00
'date' => $transaction->date->toAtomString(),
2018-12-18 19:57:23 +01:00
'type' => $transaction->transaction_type_type,
'identifier' => $transaction->identifier,
'journal_id' => (int)$transaction->journal_id,
'reconciled' => (bool)$transaction->reconciled,
'amount' => round($transaction->transaction_amount, (int)$transaction->transaction_currency_dp),
'currency_id' => $transaction->transaction_currency_id,
'currency_code' => $transaction->transaction_currency_code,
'currency_symbol' => $transaction->transaction_currency_symbol,
'currency_decimal_places' => $transaction->transaction_currency_dp,
'foreign_amount' => null,
'foreign_currency_id' => $transaction->foreign_currency_id,
'foreign_currency_code' => $transaction->foreign_currency_code,
'foreign_currency_symbol' => $transaction->foreign_currency_symbol,
'foreign_currency_decimal_places' => $transaction->foreign_currency_dp,
'bill_id' => $transaction->bill_id,
'bill_name' => $transaction->bill_name,
2018-12-20 22:03:34 +01:00
'category_id' => $category['category_id'],
'category_name' => $category['category_name'],
'budget_id' => $budget['budget_id'],
'budget_name' => $budget['budget_name'],
2018-12-18 19:57:23 +01:00
'notes' => $notes,
'sepa_cc' => $this->repository->getMetaField($journal, 'sepa-cc'),
'sepa_ct_op' => $this->repository->getMetaField($journal, 'sepa-ct-op'),
'sepa_ct_id' => $this->repository->getMetaField($journal, 'sepa-ct-ud'),
'sepa_db' => $this->repository->getMetaField($journal, 'sepa-db'),
'sepa_country' => $this->repository->getMetaField($journal, 'sepa-country'),
'sepa_ep' => $this->repository->getMetaField($journal, 'sepa-ep'),
'sepa_ci' => $this->repository->getMetaField($journal, 'sepa-ci'),
'sepa_batch_id' => $this->repository->getMetaField($journal, 'sepa-batch-id'),
'interest_date' => $this->repository->getMetaDateString($journal, 'interest_date'),
'book_date' => $this->repository->getMetaDateString($journal, 'book_date'),
'process_date' => $this->repository->getMetaDateString($journal, 'process_date'),
'due_date' => $this->repository->getMetaDateString($journal, 'due_date'),
'payment_date' => $this->repository->getMetaDateString($journal, 'payment_date'),
'invoice_date' => $this->repository->getMetaDateString($journal, 'invoice_date'),
'internal_reference' => $this->repository->getMetaField($journal, 'internal_reference'),
'bunq_payment_id' => $this->repository->getMetaField($journal, 'bunq_payment_id'),
'importHashV2' => $this->repository->getMetaField($journal, 'importHashV2'),
'recurrence_id' => (int)$this->repository->getMetaField($journal, 'recurrence_id'),
'external_id' => $this->repository->getMetaField($journal, 'external_id'),
'original_source' => $this->repository->getMetaField($journal, 'original-source'),
'tags' => '' === $tags ? null : $tags,
'links' => [
[
'rel' => 'self',
2018-02-11 07:46:34 +01:00
'uri' => '/transactions/' . $transaction->id,
],
],
];
2018-02-13 21:04:15 +01:00
// expand foreign amount:
2018-04-02 14:50:17 +02:00
if (null !== $transaction->transaction_foreign_amount) {
$data['foreign_amount'] = round($transaction->transaction_foreign_amount, (int)$transaction->foreign_currency_dp);
2018-02-13 21:04:15 +01:00
}
2018-12-20 22:03:34 +01:00
2018-02-13 21:04:15 +01:00
// switch on type for consistency
2018-02-17 10:47:06 +01:00
switch ($transaction->transaction_type_type) {
case TransactionType::WITHDRAWAL:
2018-05-29 07:25:04 +02:00
Log::debug(sprintf('%d is a withdrawal', $transaction->journal_id));
2018-02-13 21:04:15 +01:00
$data['source_id'] = $transaction->account_id;
$data['source_name'] = $transaction->account_name;
$data['source_iban'] = $transaction->account_iban;
$data['source_type'] = $transaction->account_type;
$data['destination_id'] = $transaction->opposing_account_id;
$data['destination_name'] = $transaction->opposing_account_name;
$data['destination_iban'] = $transaction->opposing_account_iban;
$data['destination_type'] = $transaction->opposing_account_type;
2018-05-29 07:25:04 +02:00
Log::debug(sprintf('source_id / account_id is %d', $transaction->account_id));
Log::debug(sprintf('source_name / account_name is "%s"', $transaction->account_name));
2018-02-13 21:04:15 +01:00
break;
2018-02-17 10:47:06 +01:00
case TransactionType::DEPOSIT:
case TransactionType::TRANSFER:
case TransactionType::OPENING_BALANCE:
case TransactionType::RECONCILIATION:
2018-02-13 21:04:15 +01:00
$data['source_id'] = $transaction->opposing_account_id;
$data['source_name'] = $transaction->opposing_account_name;
$data['source_iban'] = $transaction->opposing_account_iban;
$data['source_type'] = $transaction->opposing_account_type;
$data['destination_id'] = $transaction->account_id;
$data['destination_name'] = $transaction->account_name;
$data['destination_iban'] = $transaction->account_iban;
$data['destination_type'] = $transaction->account_type;
break;
default:
2018-02-17 10:47:06 +01:00
// @codeCoverageIgnoreStart
throw new FireflyException(
sprintf('Transaction transformer cannot handle transactions of type "%s"!', $transaction->transaction_type_type)
);
// @codeCoverageIgnoreEnd
2018-02-13 21:04:15 +01:00
}
// expand description.
2018-12-20 22:03:34 +01:00
if ('' !== (string)$transaction->transaction_description) {
2018-02-13 21:04:15 +01:00
$data['description'] = $transaction->transaction_description . ' (' . $transaction->description . ')';
}
2018-02-11 07:46:34 +01:00
return $data;
}
2018-12-20 22:03:34 +01:00
/**
* @param Transaction $transaction
*
* @return array
*/
private function getBudget(Transaction $transaction): array
{
if ($transaction->transaction_type_type !== TransactionType::WITHDRAWAL) {
return [
'budget_id' => null,
'budget_name' => null,
];
}
return [
'budget_id' => $transaction->transaction_budget_id ?? $transaction->transaction_journal_budget_id,
'budget_name' => $transaction->transaction_budget_name ?? $transaction->transaction_journal_budget_name,
];
}
/**
* @param Transaction $transaction
*
* @return array
*/
private function getCategory(Transaction $transaction): array
{
return [
'category_id' => $transaction->transaction_category_id ?? $transaction->transaction_journal_category_id,
'category_name' => $transaction->transaction_category_name ?? $transaction->transaction_journal_category_name,
];
}
2018-03-05 19:35:58 +01:00
}