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

454 lines
15 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\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;
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
*/
public function addColumnValue(ColumnValue $columnValue): void
{
switch ($columnValue->getRole()) {
default:
2018-05-11 12:56:52 -05:00
// @codeCoverageIgnoreStart
2018-05-07 12:21:12 -05:00
throw new FireflyException(
sprintf('ImportTransaction cannot handle role "%s" with value "%s"', $columnValue->getRole(), $columnValue->getValue())
);
2018-05-11 12:56:52 -05:00
// @codeCoverageIgnoreEnd
2018-05-07 12:21:12 -05:00
case 'account-id':
// could be the result of a mapping?
$this->accountId = $this->getMappedValue($columnValue);
break;
2018-05-07 13:35:14 -05:00
case 'account-iban':
$this->accountIban = $columnValue->getValue();
break;
2018-05-07 12:21:12 -05:00
case 'account-name':
$this->accountName = $columnValue->getValue();
break;
2018-05-07 13:35:14 -05:00
case 'account-bic':
$this->accountBic = $columnValue->getValue();
break;
case 'account-number':
$this->accountNumber = $columnValue->getValue();
2018-05-07 12:21:12 -05:00
break;
2018-05-11 12:56:52 -05:00
case 'amount_debit':
2018-05-07 12:21:12 -05:00
$this->amountDebit = $columnValue->getValue();
break;
2018-05-11 12:56:52 -05:00
case 'amount_credit':
2018-05-07 12:21:12 -05:00
$this->amountCredit = $columnValue->getValue();
break;
case 'amount':
$this->amount = $columnValue->getValue();
break;
case 'amount_foreign':
$this->foreignAmount = $columnValue->getValue();
break;
case 'bill-id':
$this->billId = $this->getMappedValue($columnValue);
break;
2018-05-07 13:35:14 -05:00
case 'bill-name':
$this->billName = $columnValue->getValue();
break;
2018-05-07 12:21:12 -05:00
case 'budget-id':
$this->budgetId = $this->getMappedValue($columnValue);
break;
2018-05-07 13:35:14 -05:00
case 'budget-name':
$this->budgetName = $columnValue->getValue();
break;
2018-05-07 12:21:12 -05:00
case 'category-id':
$value = $this->getMappedValue($columnValue);
Log::debug(sprintf('Set category ID to %d in ImportTransaction object', $value));
$this->categoryId = $value;
2018-05-07 12:21:12 -05:00
break;
2018-05-07 13:35:14 -05:00
case 'category-name':
$this->categoryName = $columnValue->getValue();
break;
2018-05-07 12:21:12 -05:00
case 'currency-id':
$this->currencyId = $this->getMappedValue($columnValue);
break;
2018-05-07 13:35:14 -05:00
case 'currency-name':
$this->currencyName = $columnValue->getValue();
break;
case 'currency-code':
$this->currencyCode = $columnValue->getValue();
break;
case 'currency-symbol':
$this->currencySymbol = $columnValue->getValue();
break;
case 'external-id':
$this->externalId = $columnValue->getValue();
break;
case 'sepa-ct-id';
case 'sepa-ct-op';
case 'sepa-db';
case 'sepa-cc':
case 'sepa-country';
case 'sepa-ep';
case 'sepa-ci';
case 'internal-reference':
case 'date-interest':
case 'date-invoice':
case 'date-book':
case 'date-payment':
case 'date-process':
case 'date-due':
$this->meta[$columnValue->getRole()] = $columnValue->getValue();
break;
case 'foreign-currency-id':
$this->foreignCurrencyId = $this->getMappedValue($columnValue);
break;
case 'foreign-currency-code':
$this->foreignCurrencyCode = $columnValue->getValue();
break;
2018-05-07 12:21:12 -05:00
case 'date-transaction':
$this->date = $columnValue->getValue();
break;
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 'opposing-id':
$this->opposingId = $this->getMappedValue($columnValue);
break;
2018-05-07 13:35:14 -05:00
case 'opposing-iban':
$this->opposingIban = $columnValue->getValue();
break;
2018-05-07 12:21:12 -05:00
case 'opposing-name':
$this->opposingName = $columnValue->getValue();
break;
case 'opposing-bic':
$this->opposingBic = $columnValue->getValue();
break;
2018-05-07 13:35:14 -05:00
case 'opposing-number':
$this->opposingNumber = $columnValue->getValue();
break;
case 'rabo-debit-credit':
case 'ing-debit-credit':
$this->modifiers[$columnValue->getRole()] = $columnValue->getValue();
break;
2018-05-07 12:21:12 -05:00
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
* @throws FireflyException
*/
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);
}
if ($conversion === 1) {
$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);
}
if ($conversion === 1) {
$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;
}
$info['class'] = $converterClass;
return $info;
}
2018-05-11 02:51:47 -05:00
}