firefly-iii/app/Support/Import/Placeholder/ImportTransaction.php

422 lines
14 KiB
PHP
Raw Normal View History

2018-05-07 12:21:12 -05:00
<?php
/**
* Transaction.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\Support\Import\Placeholder;
use FireflyIII\Exceptions\FireflyException;
2018-05-09 13:53:39 -05:00
use FireflyIII\Import\Converter\Amount;
use FireflyIII\Import\Converter\AmountCredit;
use FireflyIII\Import\Converter\AmountDebit;
use FireflyIII\Import\Converter\AmountNegated;
2018-05-09 13:53:39 -05:00
use FireflyIII\Import\Converter\ConverterInterface;
use Log;
2018-05-07 12:21:12 -05:00
/**
* Class ImportTransaction
*/
class ImportTransaction
{
/** @var string */
2018-05-11 12:56:52 -05:00
public $accountBic;
2018-05-07 12:21:12 -05:00
/** @var string */
2018-05-11 12:56:52 -05:00
public $accountIban;
2018-05-07 12:21:12 -05:00
/** @var int */
2018-05-11 12:56:52 -05:00
public $accountId;
2018-05-07 12:21:12 -05:00
/** @var string */
2018-05-11 12:56:52 -05:00
public $accountName;
2018-05-07 12:21:12 -05:00
/** @var string */
2018-05-11 12:56:52 -05:00
public $accountNumber;
2018-05-07 13:35:14 -05:00
/** @var string */
2018-05-11 12:56:52 -05:00
public $amount;
2018-05-07 12:21:12 -05:00
/** @var string */
2018-05-11 12:56:52 -05:00
public $amountCredit;
2018-05-07 12:21:12 -05:00
/** @var string */
2018-05-11 12:56:52 -05:00
public $amountDebit;
/** @var string */
public $amountNegated;
2018-05-07 12:21:12 -05:00
/** @var int */
2018-05-11 12:56:52 -05:00
public $billId;
2018-05-07 13:35:14 -05:00
/** @var string */
2018-05-11 12:56:52 -05:00
public $billName;
2018-05-07 12:21:12 -05:00
/** @var int */
2018-05-11 12:56:52 -05:00
public $budgetId;
2018-05-07 13:35:14 -05:00
/** @var string */
2018-05-11 12:56:52 -05:00
public $budgetName;
2018-05-07 12:21:12 -05:00
/** @var int */
2018-05-11 12:56:52 -05:00
public $categoryId;
2018-05-07 13:35:14 -05:00
/** @var string */
2018-05-11 12:56:52 -05:00
public $categoryName;
2018-05-07 13:35:14 -05:00
/** @var string */
2018-05-11 12:56:52 -05:00
public $currencyCode;
2018-05-07 12:21:12 -05:00
/** @var int */
2018-05-11 12:56:52 -05:00
public $currencyId;
2018-05-07 12:21:12 -05:00
/** @var string */
2018-05-11 12:56:52 -05:00
public $currencyName;
2018-05-07 13:35:14 -05:00
/** @var string */
2018-05-11 12:56:52 -05:00
public $currencySymbol;
2018-05-07 13:35:14 -05:00
/** @var string */
2018-05-11 12:56:52 -05:00
public $date;
2018-05-07 12:21:12 -05:00
/** @var string */
2018-05-11 12:56:52 -05:00
public $description;
2018-05-07 12:21:12 -05:00
/** @var string */
2018-05-11 12:56:52 -05:00
public $externalId;
2018-05-07 12:21:12 -05:00
/** @var string */
2018-05-11 12:56:52 -05:00
public $foreignAmount;
2018-05-07 13:35:14 -05:00
/** @var string */
2018-05-11 12:56:52 -05:00
public $foreignCurrencyCode;
2018-05-07 12:21:12 -05:00
/** @var int */
2018-05-11 12:56:52 -05:00
public $foreignCurrencyId;
2018-05-07 12:21:12 -05:00
/** @var array */
2018-05-11 12:56:52 -05:00
public $meta;
2018-05-07 12:21:12 -05:00
/** @var array */
2018-05-11 12:56:52 -05:00
public $modifiers;
2018-05-07 12:21:12 -05:00
/** @var string */
2018-05-11 12:56:52 -05:00
public $note;
2018-05-07 12:21:12 -05:00
/** @var string */
2018-05-11 12:56:52 -05:00
public $opposingBic;
2018-05-07 12:21:12 -05:00
/** @var string */
2018-05-11 12:56:52 -05:00
public $opposingIban;
2018-05-07 12:21:12 -05:00
/** @var int */
2018-05-11 12:56:52 -05:00
public $opposingId;
2018-05-07 12:21:12 -05:00
/** @var string */
2018-05-11 12:56:52 -05:00
public $opposingName;
2018-05-07 13:35:14 -05:00
/** @var string */
2018-05-11 12:56:52 -05:00
public $opposingNumber;
2018-05-07 12:21:12 -05:00
/** @var array */
2018-05-11 12:56:52 -05:00
public $tags;
2018-05-07 12:21:12 -05:00
/**
* ImportTransaction constructor.
*/
public function __construct()
{
$this->tags = [];
$this->modifiers = [];
$this->meta = [];
$this->description = '';
$this->note = '';
2018-05-07 13:35:14 -05:00
// mappable items, set to 0:
$this->accountId = 0;
$this->budgetId = 0;
$this->billId = 0;
$this->currencyId = 0;
$this->categoryId = 0;
$this->foreignCurrencyId = 0;
$this->opposingId = 0;
}
2018-05-07 12:21:12 -05:00
/**
* @param ColumnValue $columnValue
*
* @throws FireflyException
2018-07-28 03:45:16 -05:00
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
2018-05-07 12:21:12 -05:00
*/
public function addColumnValue(ColumnValue $columnValue): void
{
2018-07-17 23:58:51 -05:00
$role = $columnValue->getRole();
$basics = [
'account-iban' => 'accountIban',
'account-name' => 'accountName',
'account-bic' => 'accountBic',
'account-number' => 'accountNumber',
'amount_debit' => 'amountDebit',
'amount_credit' => 'amountCredit',
'amount_negated' => 'amountNegated',
2018-07-17 23:58:51 -05:00
'amount' => 'amount',
'amount_foreign' => 'foreignAmount',
'bill-name' => 'billName',
'budget-name' => 'budgetName',
'category-name' => 'categoryName',
'currency-name' => 'currencyName',
'currency-code' => 'currencyCode',
'currency-symbol' => 'currencySymbol',
'external-id' => 'externalId',
'foreign-currency-code' => 'foreignCurrencyCode',
'date-transaction' => 'date',
'opposing-iban' => 'opposingIban',
'opposing-name' => 'opposingName',
'opposing-bic' => 'opposingBic',
'opposing-number' => 'opposingNumber',
];
if (isset($basics[$role])) {
$field = $basics[$role];
$this->$field = $columnValue->getValue();
return;
}
2018-07-18 00:16:32 -05:00
$mapped = [
'account-id' => 'accountId',
'bill-id' => 'billId',
'budget-id' => 'budgetId',
'category-id' => 'categoryId',
'currency-id' => 'currencyId',
'foreign-currency-id' => 'foreignCurrencyId',
'opposing-id' => 'opposingId',
];
if (isset($mapped[$role])) {
2018-07-18 00:29:58 -05:00
$field = $mapped[$role];
2018-07-18 00:16:32 -05:00
$mappedValue = $this->getMappedValue($columnValue);
$this->$field = $mappedValue;
Log::debug(sprintf('Going to set the %s. Original value is "%s", mapped value is "%s".', $role, $columnValue->getValue(), $mappedValue));
return;
}
$meta = ['sepa-ct-id', 'sepa-ct-op', 'sepa-db', 'sepa-cc', 'sepa-country', 'sepa-batch-id', 'sepa-ep', 'sepa-ci', 'internal-reference', 'date-interest',
'date-invoice', 'date-book', 'date-payment', 'date-process', 'date-due','original-source'];
2018-08-03 09:55:10 -05:00
Log::debug(sprintf('Now going to check role "%s".', $role));
2018-07-19 09:57:38 -05:00
if (\in_array($role, $meta, true)) {
Log::debug(sprintf('Role "%s" is in allowed meta roles, so store its value "%s".', $role, $columnValue->getValue()));
2018-07-18 00:16:32 -05:00
$this->meta[$role] = $columnValue->getValue();
return;
}
2018-07-17 23:58:51 -05:00
2018-07-19 09:57:38 -05:00
$modifiers = ['rabo-debit-credit', 'ing-debit-credit'];
if (\in_array($role, $modifiers, true)) {
$this->modifiers[$role] = $columnValue->getValue();
return;
}
2018-07-18 00:16:32 -05:00
switch ($role) {
2018-05-07 12:21:12 -05:00
default:
2018-05-11 12:56:52 -05:00
// @codeCoverageIgnoreStart
2018-05-07 12:21:12 -05:00
throw new FireflyException(
2018-07-18 00:16:32 -05:00
sprintf('ImportTransaction cannot handle role "%s" with value "%s"', $role, $columnValue->getValue())
2018-05-07 12:21:12 -05:00
);
2018-05-11 12:56:52 -05:00
// @codeCoverageIgnoreEnd
2018-05-07 12:21:12 -05:00
case 'description':
2018-05-11 12:56:52 -05:00
$this->description = trim($this->description . ' ' . $columnValue->getValue());
2018-05-07 12:21:12 -05:00
break;
case 'note':
2018-05-11 12:56:52 -05:00
$this->note = trim($this->note . ' ' . $columnValue->getValue());
2018-05-07 12:21:12 -05:00
break;
case 'tags-comma':
2018-05-11 12:56:52 -05:00
$tags = explode(',', $columnValue->getValue());
$this->tags = array_unique(array_merge($this->tags, $tags));
2018-05-07 12:21:12 -05:00
break;
case 'tags-space':
2018-05-11 12:56:52 -05:00
$tags = explode(' ', $columnValue->getValue());
$this->tags = array_unique(array_merge($this->tags, $tags));
2018-05-07 12:21:12 -05:00
break;
case '_ignore':
2018-05-07 13:35:14 -05:00
break;
2018-05-07 12:21:12 -05:00
}
}
2018-05-09 13:53:39 -05:00
/**
* Calculate the amount of this transaction.
*
* @return string
*/
public function calculateAmount(): string
{
Log::debug('Now in importTransaction->calculateAmount()');
2018-05-11 12:56:52 -05:00
$info = $this->selectAmountInput();
2018-05-09 13:53:39 -05:00
$class = $info['class'] ?? '';
if ('' === $class) {
Log::error('No amount information (conversion class) for this row.');
return '';
2018-05-09 13:53:39 -05:00
}
Log::debug(sprintf('Converter class is %s', $info['class']));
/** @var ConverterInterface $amountConverter */
$amountConverter = app($info['class']);
$result = $amountConverter->convert($info['amount']);
Log::debug(sprintf('First attempt to convert gives "%s"', $result));
// modify
/**
* @var string $role
* @var string $modifier
*/
foreach ($this->modifiers as $role => $modifier) {
2018-05-11 02:51:47 -05:00
$class = sprintf('FireflyIII\\Import\\Converter\\%s', config(sprintf('csv.import_roles.%s.converter', $role)));
2018-05-09 13:53:39 -05:00
/** @var ConverterInterface $converter */
$converter = app($class);
Log::debug(sprintf('Now launching converter %s', $class));
$conversion = $converter->convert($modifier);
if ($conversion === -1) {
$result = app('steam')->negative($result);
}
2018-07-26 22:03:37 -05:00
if (1 === $conversion) {
2018-05-09 13:53:39 -05:00
$result = app('steam')->positive($result);
}
Log::debug(sprintf('convertedAmount after conversion is %s', $result));
}
Log::debug(sprintf('After modifiers the result is: "%s"', $result));
return $result;
}
/**
* The method that calculates the foreign amount isn't nearly as complex,\
* because Firefly III only supports one foreign amount field. So the foreign amount is there
* or isn't. That's about it. However, if it's there, modifiers will be applied too.
*
* @return string
*/
public function calculateForeignAmount(): string
{
if (null === $this->foreignAmount) {
Log::debug('ImportTransaction holds no foreign amount info.');
2018-05-11 12:56:52 -05:00
return '';
}
/** @var ConverterInterface $amountConverter */
$amountConverter = app(Amount::class);
$result = $amountConverter->convert($this->foreignAmount);
Log::debug(sprintf('First attempt to convert foreign amount gives "%s"', $result));
/**
* @var string $role
* @var string $modifier
*/
foreach ($this->modifiers as $role => $modifier) {
2018-05-11 02:51:47 -05:00
$class = sprintf('FireflyIII\\Import\\Converter\\%s', config(sprintf('csv.import_roles.%s.converter', $role)));
/** @var ConverterInterface $converter */
$converter = app($class);
Log::debug(sprintf('Now launching converter %s', $class));
$conversion = $converter->convert($modifier);
if ($conversion === -1) {
$result = app('steam')->negative($result);
}
2018-07-26 22:03:37 -05:00
if (1 === $conversion) {
$result = app('steam')->positive($result);
}
Log::debug(sprintf('Foreign amount after conversion is %s', $result));
}
Log::debug(sprintf('After modifiers the foreign amount is: "%s"', $result));
return $result;
}
2018-05-09 13:53:39 -05:00
/**
* This array is being used to map the account the user is using.
*
2018-05-11 12:56:52 -05:00
* @codeCoverageIgnore
2018-05-09 13:53:39 -05:00
* @return array
*/
public function getAccountData(): array
{
return [
'iban' => $this->accountIban,
'name' => $this->accountName,
'number' => $this->accountNumber,
'bic' => $this->accountBic,
];
}
/**
2018-05-11 12:56:52 -05:00
* @codeCoverageIgnore
* @return array
*/
public function getCurrencyData(): array
{
return [
'name' => $this->currencyName,
'code' => $this->currencyCode,
'symbol' => $this->currencySymbol,
];
}
2018-05-12 03:46:18 -05:00
/**
* @codeCoverageIgnore
* @return array
*/
public function getForeignCurrencyData(): array
{
return [
'code' => $this->foreignCurrencyCode,
];
}
2018-05-09 13:53:39 -05:00
/**
2018-05-11 12:56:52 -05:00
* @codeCoverageIgnore
* @return array
*/
2018-05-09 13:53:39 -05:00
public function getOpposingAccountData(): array
{
return [
'iban' => $this->opposingIban,
'name' => $this->opposingName,
'number' => $this->opposingNumber,
'bic' => $this->opposingBic,
];
}
2018-05-07 12:21:12 -05:00
/**
* Returns the mapped value if it exists in the ColumnValue object.
*
* @param ColumnValue $columnValue
*
* @return int
*/
private function getMappedValue(ColumnValue $columnValue): int
{
return $columnValue->getMappedValue() > 0 ? $columnValue->getMappedValue() : (int)$columnValue->getValue();
}
2018-05-09 13:53:39 -05:00
/**
* This methods decides which input value to use for the amount calculation.
*
* @return array
*/
private function selectAmountInput(): array
2018-05-09 13:53:39 -05:00
{
$info = [];
$converterClass = '';
if (null !== $this->amount) {
Log::debug('Amount value is not NULL, assume this is the correct value.');
$converterClass = Amount::class;
$info['amount'] = $this->amount;
}
if (null !== $this->amountDebit) {
Log::debug('Amount DEBIT value is not NULL, assume this is the correct value (overrules Amount).');
$converterClass = AmountDebit::class;
$info['amount'] = $this->amountDebit;
}
if (null !== $this->amountCredit) {
Log::debug('Amount CREDIT value is not NULL, assume this is the correct value (overrules Amount and AmountDebit).');
$converterClass = AmountCredit::class;
$info['amount'] = $this->amountCredit;
}
if (null !== $this->amountNegated) {
Log::debug('Amount NEGATED value is not NULL, assume this is the correct value (overrules Amount and AmountDebit and AmountCredit).');
$converterClass = AmountNegated::class;
$info['amount'] = $this->amountNegated;
}
2018-05-09 13:53:39 -05:00
$info['class'] = $converterClass;
return $info;
}
2018-05-11 02:51:47 -05:00
}